The simplest way to accept Bitcoin payments is to set up a wallet and tell people to send you bitcoins. The next level of complexity is setting up a system where each person who pays you is given a unique address (to pay to) and then automatically collecting those payments and marking that person in a database as having paid. How can you do this?

Electrum Master Public Keys (BIP 32) allow you to generate new addresses on the fly that "deposit" automatically into one Electrum wallet. The trick is that you can generate addresses using the Master Public Key + Offset (an integer). So for each customer you assign an ID number and then generate a Bitcoin address that corresponds to that ID. The code that generates these addresses doesn't require access to your private keys, only the master public key (which is used to derive the other public keys that become the addresses).

Here's the algorithm:

Master Public Key + Offset --> Convert to Address

To build an e-commerce platform out of this example you would create a customer record in your database that has an identifying number and an amount that the person should pay. You then generate an address using the Master Public Key + identifying number and monitor that address for new transactions in the amount that you're expecting (check every X seconds). Once the system spots the transaction that was sent to that address the customer is marked as having paid. The bitcoins will have been deposited right into your Electrum wallet and you'll have an address that corresponds to the payment if you ever want to go back and see what happened with that customer's payment.

How can you do this with actual code?

Download Pybitcointools and run the command line pybitcointools program using the following commands:


Step 2: pybitcointools pubkey_to_address [RESULT OF COMMAND FROM STEP 1]

These two steps will result in an address that corresponds to a customer and automatically feeds into Electrum. You could incorporate the above shell commands into your programming language of choice (such as using the shell_exec command in PHP).

Reddit user @chairoverflow pointed out that there's an easy library for doing this with PHP, Ruby and Python without dependencies (i.e. no shell commands required):