3

I run an application written in PHP and need to add the option to buy services with bitcoins. So, for each user i need to generate a unique, unused bitcoin address.

I could generate X number wallets with Electrum and assing 1 address to each user, but then i will have to check manually if someone send a payment, match the address with the user, wait for confirmations, import data, update the balance, every day. This take too much time and it's tricky.

What's the best and easier approach to achieve this? from where i start? Using external api and services is not a choice, using a single shared wallet can be okay but how i associate user/transaction?

3 Answers 3

1

I do something similar in PHP using https://github.com/aceat64/EasyBitcoin-PHP connecting to a full bitcoind client rather than Electrum. An internal API, if you will.

On the downside, it will download the entire blockchain. On the upside, once up-to-date it handles almost all of the heavy lifting described in your second paragraph.

While I don't associate users/transactions as you describe, if I did I would investigate whether accounts supported in the bitcoind client would assist you:

$address = $bitcoin->getnewaddress($user_id)
looks to associate the two such that
$balance = $bitcoin->getbalance($user_id, 6)
will give you the balance with more than six confirmations held by that user across all his addresses.

1

You can do this easily using bitcoincore. Brief overview of process (Source: bitcointalk thread)

  1. Generate unique address using getnewaddress
  2. Register on walletnotify to get txid as notification
  3. Call gettransaction with txid from step 2 to get details/address of payment received
1

Depends, shared or dedicated hosting?

With shared you really are limited to 3rd party APIs or the method you already mentioned of generating bulk keys and tracking them in a database. You could use a client-side javascript combined with an xpub you own, but then you have the privacy issue of sharing your xpub in the source and you have to manually track payments.

If dedicated, you can run a pruned node prune=550 which can be done on a relatively affordable VPS and generate addresses on the fly with getnewaddress RPC calls and auto update the database with walletnotify=curl https://mysite.com/callback.php?txid=%s where %s is the transaction id, but you will want to use a secret parameter as well for security. Wallet notify is fired once upon the transaction entering your mempool and then once again after confirmation. gettransaction <txid> can be used to get transaction details and determine who in your database paid based on the receiving address.

Not the answer you're looking for? Browse other questions tagged or ask your own question.