The aim of this tutorial is to describe a way of generating unique Bitcoin Cash receiving addresses without having the private keys or seed present on that machine. Particularly useful if you want to build point of sale or e-commerce applications. BIP 32 Hierarchical Deterministic Wallets (wallets which can be shared partially or entirely with different systems, each with or without the ability to spend coins) offer this funtionality through its deterministic key generation scheme, more precicely the use of an xpub key.
For this example I will be using Electron-Cash to generate the full HD wallet with spending functionality. Then I will use the pycoin ku utility, which is a cli implementation of the pycoin python libs, to generate the same receiving addresses as Electron Cash Would, but without the private keys present.
Generate Electron Cash Wallet
Open Electron Cash and open the ‘File > New/Restore’ wizzard. Create a new standard wallet with a new seed and protect it with a password. Electron Cash generates a bunch of receiving and change addresses by default, we can use these to compare and validate the addresses we will generate later on. What we really need is the xpub key, which can be found under the menu ‘Wallet > Information’.
Generate addresses with Pycoin KU
Now that we have the master public key or xpub key, we can generate addresses with ku. Since the Electron Cash wallet nicely follows the default wallet layout we can predictably generate new receiving addresses from the default external chain (m/iH/0/k -> 0/k). And for the sake of experimentation, we can also generate some change addresses from the default internal chain (m/iH/1/k -> 1/k).
I will use pycoin-ku with the following options:
E:electrum value (either a master public, master private, or initial data) -a show just Bitcoin address -s SUBKEY subkey path -n specify network
To generate the first receiving address (0) of the external chain (0):
~> pycoin-ku -n BTC \ E:xpub661MyMwAqRbcFshhFafKAMuob4vCw69LJ3wHD3uB1dzsPn15icrQDXadrQMHQCivZfRaXrec9LHDfQT9MRqqUqTonL6YKnjPpqad4S6YCJB \ -a -s 0/0
To generate the first 20 addresses (0-19) of the external chain (0):
~> pycoin-ku -n BTC \ E:xpub661MyMwAqRbcFshhFafKAMuob4vCw69LJ3wHD3uB1dzsPn15icrQDXadrQMHQCivZfRaXrec9LHDfQT9MRqqUqTonL6YKnjPpqad4S6YCJB \ -a -s 0/0-19
Compared to the addresses Electron Cash generated:
Now let’s do some change addresses (0-5) on the internal chain (1):
~> pycoin-ku -n BTC \ E:xpub661MyMwAqRbcFshhFafKAMuob4vCw69LJ3wHD3uB1dzsPn15icrQDXadrQMHQCivZfRaXrec9LHDfQT9MRqqUqTonL6YKnjPpqad4S6YCJB \ -a -s 1/0-5
Compared to Electron Cash’s change addresses:
Pretty neat isn’t it! :)
Now you can generate unique receiving addresses for each payment on your webshop or in your store. You want to do this for your own and your customer’s privacy. As is best practice for bitcoin transactions. It also makes it easier to handle payment monitoring. And all this without having to have the private keys present at location or on your webshop’s server. This way you do not have to trust the cashiers with the payments received. Or in case of a hack of your webshop or point of sale system, or robbery of your store, the hacker/robber can not steal your funds. All your earnings or more precisely the access to them, will be safe in an Electron wallet at a safe location.
If you would like to have better integration or customized functionality, you can implement this scheme with the Pycoin libs.