4

Here I'd like to generate the other address use xpub.

Is there any library (python) could help to derive the new address from extended key?

such as:

gen_new_pub(xpub, new_path) to get the derived public key.

Of course, the library supposes could support

gen_new_prv(xprv, new_path) to generate the corresponding private key according to the xprv and path.

2 Answers 2

4

the btclib library (https://github.com/dginst/btclib) is a python3 type annotated library for bitcoin cryptography; it provides BIP32 functions, tested to successfully reproduce the BIP32 test vectors (https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki).

Among the btclib functions you can find:

  • bip32.ckd(xparentkey, index) [child key derivation]
  • bip32.xpub_from_xprv(xparentkey) [neutered derivation]
  • bip32.derive(xkey, path) [child key path derivation]
  • bip32.address_from_xpub(xpub) [address derivation]
  • bip32.mprv_from_seed(seed) [master private key from seed]
3
  • Fantastic! Thanks very much! This library is easy to understand and use! If there is a function or other easy way could convert the mprv (extended private key) to the private key, that will be great!
    – Carpemer
    Jan 21, 2019 at 14:35
  • you're right, at least some wif_from_xprv(xprv) similar to address_from_xpub should be implemented. Look for a library update in a week time... Jan 22, 2019 at 23:56
  • Hi Ferdinando M. Ametrano, how is the progress, is there anything I can help? Do you have any manual for the algorithm? I could have a try to implement the code.
    – Carpemer
    Mar 22, 2019 at 6:45
1

here is a python BIP32 library: https://github.com/pezcore/obsidian

the command line interface is in bin/oku. Make sure the top level project forlder is on your PYTHONPATH and you can use it like this :

$ oku -p44/0/0/0 xpub661MyMwAqRbcF3DZvDPYWEzuFxL1Xt6RDdjGsuinnueNLiUSUYb7gaXEWxGMS4T96kDrBTdxxfVx6vqhYT9ZcJdNcevVpzjK2HYFN2PGmke
Root key info -------------------------------------------------------------------------------------------------
depth    : 0
index    : 00000000
parent   : 00000000
chaincode: 31D8745D939CD85B7E956A03908AA09C6E2FB7A3A7D74D3A90889DF5FED6AAA9
keydata  : 03B72E8FB7C8F2CA7664C8DADC23AD452888F0347C790E6473245AC2EF9E541724
BIP32 str: xpub661MyMwAqRbcF3DZvDPYWEzuFxL1Xt6RDdjGsuinnueNLiUSUYb7gaXEWxGMS4T96kDrBTdxxfVx6vqhYT9ZcJdNcevVpzjK2HYFN2PGmke

Derived Key info ----------------------------------------------------------------------------------------------
path     : 44/0/0/0
depth    : 4
index    : 00000000
parent   : A18376A9
chaincode: 17D3059131888335A8D3A664A015F519FD89D811154492725823D9829A1D6EE3
keydata  : 023FEED34B38497B005CA91BEA5AD5B1FCD0318DA01AC7EFDD1635B72FE3055CD3
BIP32 str: xpub6EikzRzgQiSsjiL3nz2SUZTi964wvqa1Je1WtRr5cXEwFEbMJvhas6BauDXoAVRhShfrJVTF1CKoq8zFmA9x72KocKxmRPBy4DizNPxLw9g

leaves--------------------------------------------------------------------------------------------------
 0 1LDB6EomUoB4uXaqutFes9uduQ9Mz7PgHh 021367BDC3D91C499DE400BA561F526C77DC4241CCA7768C7A6A76B4A0AAF32CAF
 1 1CfZgDBKAy9W6o4wJiGjCeMe4HPb3sibsY 037FEB277F7EE6EE89DC8CE25F6DDACEA12BCF9D937E5606A83CBC0A25C302CE8D
 2 1L5CaYTqgyimJwT8RY2WBBtMuVxzPdH2X3 039D2328D0617BDA04C7D1F775B2E5C9A16105D9E4F14F922ACC61869689E3F53F
 3 1AkdX6HckmWsq2j1RCWqM46n4ZEbgvbRdk 03DDC12460B3EE9152B89F9074E83D45A5823327BC2F947296E02157BB4939E20F
 4 1PTCkaUS7jVVvQ7VwDGNK4nNNdt6XVqLbE 03E8DDB7AE466FD33D61DA6D26B69C0647BB743AB5C88E7D949BDE4F2707FD7C18
 5 1CPWrifcbSNR3VjuqYZeSxWqqbCsiD8Mvk 024DD8FBDA96181F354AC9983BCC65CED733F991B2149003F6E064C84722C538F4
 6 16ir5E3NZbqKhYaUZVp33NvX2iJijxN6GL 02807DB9F04E1EDCECC5975E0444F7A93C78C2124FE1D10BBF7B948E47A914ADF6
 7 1BgeRDg7rfzwP4nWWEGPAzMheQJgQnC8Yk 034B8D9372A533F0320DA504CD73F21DB3E32E7A13C4241A52F92551C558F28E7A
 8 1F98cQLJsErpztRiB46qxcoKUToQcpCneY 034266D06746682BEDC0B617B98596C3FF1D6BB57E6D0C2C36A680CDAFA6EB0DB5

The pubkey derivation functionality is in detemrinistic.py take a look at the node_from_str() function to see about creating PubBIP32Node and PrivBIP32Node objects from xpub and xpriv BIP32 extended key strings.

Take a look at the ckd() methods of the classes in this file to see how the actual public and private child key derivation is performed.

4
  • Thanks! One more question, is there any library you met, could support arbitrary string as the path instead of 'a[H]/b[H]/c[H]/d[H]/'?
    – Carpemer
    Jan 20, 2019 at 1:16
  • @Capemer path can't be an arbitrary string according to bip32
    – Abdussamad
    Jan 20, 2019 at 3:15
  • I know the BIP32 does not allow it, but in practice, it supposes to be doable, right? If that is allowed, I guess we could even generate an address for each query.
    – Carpemer
    Jan 20, 2019 at 12:34
  • Paths in BIP32 are sequences of unsigned 32bit numbers. Theres really no other way to specify path in string format. Obsidian uses H instead of ' to signify hardened nodes because ' has special meaning to the shell and would be really annoying to deal with.
    – RBF06
    Jan 20, 2019 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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