2

I'm goofing around with this fork of pybitcointools and I got stock with generating multiple address for receiving coins with a master public key. So far, this is what I have:

import bitcoin as btc

privatekey = btc.sha256("large example seed for this great test")
print privatekey

# 03e02b95a485112b543ac29cd262afbbd64dca9b4496e264e47312cb193ae5ab

bip32_masterprivatekey = btc.bip32_master_key(privatekey)
bip32_masterpublickey = btc.bip32_privtopub(bip32_masterprivatekey)

print bip32_masterprivatekey
# xprv9s21ZrQH143K3tQmGrVtKc7B6ZPtCJAmD6wDAJXxb7YXAoZFTb55UroSxrU7k823vSQmYPDhdseRKqP1mgSUWDneinai2seUd7RLX2xkmGW

print bip32_masterpublickey
# xpub661MyMwAqRbcGNVENt2tgk3uebENbktcaKroxgwa9T5W3btQ18PL2f7vp78LNoioGhZcgSH1i2cH48YcQttiEaAh86TuJpsUu2J7jQWnmbC
2

You want to use bip32_bin_extract_key on the bip32_masterpublickey:

https://github.com/vbuterin/pybitcointools/blob/8e8a33d7281c871950519e5f256ad08cf0d5df69/bitcoin/deterministic.py#L140

And then use pubkey_to_address on the result:

https://github.com/vbuterin/pybitcointools/blob/8e8a33d7281c871950519e5f256ad08cf0d5df69/bitcoin/main.py#L446

simply put:

btc.pubkey_to_address(btc.bip32_bin_extract_key(bip32_masterpublickey))
  • That would generate one bitcoin address, right? How do I generate two or more using the master public key? – shackra Jul 22 '16 at 4:57
  • 1
    Indeed, that would. You'll want to do bip32_descend to get keys that are not the root key. – Jimmy Song Jul 22 '16 at 13:45
  • Sorry for being annoying, but what parameters should I exactly feed bip32_descend with, besides the public master key (I guess) to achieve what I'm looking? I did this btc.pubkey_to_address(btc.bip32_descend(bip32_masterpublickey, [1, 0, 0])) and I'm getting a different address changing the third 0 of the list, but I'm not sure if that equals to M/i'/0 – shackra Jul 22 '16 at 21:42
  • 1
    So the last argument is the BIP32 path. [1,0,0] => M/1/0/0 path. Changing any of the numbers in the path or making it longer by an element will change the address. – Jimmy Song Jul 23 '16 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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