<p>here is a python BIP32 library: <a href="https://github.com/pezcore/obsidian" rel="nofollow noreferrer">https://github.com/pezcore/obsidian</a></p>

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

<pre><code>$ 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
</code></pre>

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

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

CC BY-SA 4.0
2019-01-20T01:16:44.440Z
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
CC BY-SA 4.0
2019-01-20T03:15:13.580Z
@Capemer path can't be an arbitrary string according to bip32
Abdussamad
CC BY-SA 4.0
2019-01-20T12:34:24.687Z
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
CC BY-SA 4.0
2019-01-20T16:59:02.590Z
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
CC BY-SA 4.0
2019-01-19T17:50:26.657Z
2019-01-19T20:00:06.613Z
2019-01-19T20:00:06.613Z
RBF06
<p>the btclib library (<a href="https://github.com/dginst/btclib" rel="nofollow noreferrer">https://github.com/dginst/btclib</a>) is a python3 type annotated library for bitcoin cryptography; it provides BIP32 functions, tested to successfully reproduce the BIP32 test vectors (<a href="https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki" rel="nofollow noreferrer">https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki</a>).</p>

<p>Among the btclib functions you can find:</p>

<ul>
<li>bip32.ckd(xparentkey, index) [child key derivation]</li>
<li>bip32.xpub_from_xprv(xparentkey) [neutered derivation] </li>
<li>bip32.derive(xkey, path) [child key path derivation]</li>
<li>bip32.address_from_xpub(xpub) [address derivation]</li>
<li>bip32.mprv_from_seed(seed) [master private key from seed]</li>
</ul>

CC BY-SA 4.0
2019-01-21T14:35:36.577Z
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
CC BY-SA 4.0
2019-01-22T23:56:11.627Z
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...
Ferdinando M. Ametrano
CC BY-SA 4.0
2019-03-22T06:45:05.717Z
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
CC BY-SA 4.0
2019-01-20T03:22:33.327Z
2019-01-20T03:22:33.327Z
Ferdinando M. Ametrano
<p>Here I'd like to generate the other address use xpub.</p>

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

<p>such as:</p>

<p>gen_new_pub(xpub, new_path) to get the derived public key. </p>

<p>Of course, the library supposes could support </p>

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

<wallet><address-generation>
How to get the derived public key from xpub?
Carpemer
# How to get the derived public key from xpub?

(Asked by: username_0 on 2019-01-19)

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.

**Tags:** wallet, address-generation


---

## 2 answers

### Answer by username_3 on 2019-01-20. Score: 4 | Accepted answer

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]

> **Comments:**
> **username_0**: 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!  
> **username_3**: 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...  
> **username_0**: Hi username_3 M. username_3, 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.  

---

### Answer by username_1 on 2019-01-19. Score: 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.

> **Comments:**
> **username_0**: 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]/'?  
> **username_2**: @Capemer path can't be an arbitrary string according to bip32  
> **username_0**: 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.  
> **username_1**: 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.  

---

