Generate Deterministic Bitcoin Receive Addresses Using PHP

Previously I wrote about deterministic wallets and pointed out that one of the advantages is that you can safely generate bitcoin addresses without exposing your private key. You can do this with an Electrum wallet Master Public Key (MPK) and some open source code.

The code in question is available in many forms out “there”. A compact version of it, using code sourced from a few different authors, is found in bitcointalk user stick’s github repository. Using it is fairly simple. Just include the file addrgen.php and call the function addr_from_mpk:

$mpk = '43434343...';
$index = 0;
$address = addr_from_mpk( $mpk, $index );

The first argument is the MPK and the second the index. The first address has an index of 0 and the second an index of 1 and so on. In this fashion you can generate an unlimited number of addresses without risking your private keys.

Another project that uses this feature of Electrum is the bitcoin payments for WooCommerce WordPress plugin. WooCommerce is of course the popular free WordPress shopping cart plugin and together with the bitcoin payments plugin you can create an ecommerce site that takes bitcoin.