<p>Bip32 is the implementation of hierarchical deterministic wallets. This fully describes what you are discussing here. The full text of bip32 can be found <a href="https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki" rel="nofollow">https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki</a>
The mnemonic maps to specific bits for the encryption, the words themselves are not used. Read the bit about extending the keys it will also explain how the master public key is generated.  It also explains the three derivations that can be made from the public and private keys which are: </p>

<p>Parent private key -> child private key</p>

<p>Parent private key -> child public key</p>

<p>Parent public key -> child public key</p>

<p>It is worth noting the child private key cannot be generated from the parent public key. </p>

CC BY-SA 3.0
2016-06-09T16:01:48.880Z
many thanks Mark ... I think I get the concept and have read BIP32(thanks for the link!) ; I suppose my post was more about the python implementation of this: I am being lazy I know, but was kindof hoping someone's been in my shoes before and could just spit the code out :D  I see the pycoin implementation but was unable to work out how it does this in testnet.
kumarz
CC BY-SA 3.0
2016-05-26T01:25:29.133Z
2016-05-26T01:25:29.133Z
Mark S.
<p>I am trying to replicate in python what the Electrum wallet does during (deterministic) wallet creation. IMHO the docs are a bit sparse on this one bit maybe because its only performed once for most users, all the tutorials etc. seem to focus on the GUI, and I was unable to work out the python code from the <a href="https://github.com/spesmilo/electrum" rel="nofollow noreferrer">spesmilo github for electrum</a>.</p>

<p>From what I can see, Electrum generates a random Wallet Generation Seed</p>

<ol>
<li><p>a 13-word mnemonic(<a href="https://bitcoin.stackexchange.com/questions/36839/electrum-2-0-non-bip39-32-standardisation-complicates-matters-immensely-why/36945">or 12 words plus checksum?</a>)</p></li>
<li><p>which is unencrypted This is also the Master Private Key?</p></li>
<li><p>Then the user enters a password/passphrase(or not) that encrypts the seed so it can be saved as a wallet(file)</p></li>
<li><p>The wallet file can only be decrypted with the password/passphrase</p></li>
<li><p>Then a Master Public Key is generated from the wallet <i>[Anybody have more detail on this?]</i>.</p></li>
<li><p>Then the 25-byte binary Bitcoin Address = 
{[MPK>sha256>RIPEMD160]+[MPK>sha256>RIPEMD160>versionByteadd>sha256>sha256>first4bytes]}</p></li>
<li><p>which in turn is Base58Check encoded to the <a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses" rel="nofollow noreferrer">Bitcoin Address format</a></p></li>
</ol>

<p>Can someone please check if my assumptions (1 to 7) are okay/correct me?
Also, below is how far I got with python(probably grossly incorrect!):</p>

<pre><code>from electrum import mnemonic
import ecdsa
import hashlib
import base58
import getpass

mnemonicInstance = mnemonic.Mnemonic(lang='en')
randseed = mnemonicInstance.make_seed()
print "this is my 13 word wallet gen seed" + randseed
private_key = mnemonicInstance.mnemonic_to_seed(randseed , getpass.getpass()).encode('hex')
print "this is my private key: " + private_key
#the line of code below failed!:
sk = ecdsa.SigningKey.from_string(private_key.decode("hex"), curve = ecdsa.SECP256k1)
vk = sk.verifying_key
public_key = ('\04' + vk.to_string()).encode("hex")
print "this is my public key: " + public_key
ripemd160 = hashlib.new('ripemd160')
ripemd160.update(hashlib.sha256(public_key.decode('hex')).digest())
middle_man = '\00' + ripemd160.digest()
checksum = hashlib.sha256(hashlib.sha256(middle_man).digest()).digest()[:4]
binary_addr = middle_man + checksum
addr = base58.b58encode(binary_addr)
print "this is my BTC address: " + addr
</code></pre>

<p>(some of the above lifted from <a href="https://github.com/Shultzi/Mybitcoin/blob/master/keys.py" rel="nofollow noreferrer">Shultzi's github</a>) I also consulted Andreas' <a href="https://github.com/bitcoinbook/bitcoinbook" rel="nofollow noreferrer">Mastering Bitcoin</a> but not had much joy with this bit.</p>

CC BY-SA 3.0
2016-05-25T23:27:08.977Z
Have you experimented with [pybitcointools](https://github.com/vbuterin/pybitcointools)?  Might be a good place to start when learning.
Jonathan Cross
CC BY-SA 3.0
2016-05-27T08:19:01.103Z
yes I have actually Jon, didn't bother putting Vitalik's code in as it does essentially the same thing. Actually I prefer pybitcointools coz it supports testnet(electrum doesn't)  Anyway, not had much joy so wanted this clarified/corrected if poss: maybe edit my post(please) like Nick did ?
kumarz
CC BY-SA 3.0
2016-06-08T11:49:30.757Z
Seems there is some work underway now that people want to test Segregated Witness: https://github.com/spesmilo/electrum/issues/541
Jonathan Cross
CC BY-SA 3.0
2016-06-09T15:55:40.270Z
yeah that'll be awesome coz as I just figured out, it seems its not possible to push multisig txns(mk_multisig_script) to testnet using pybitcointools (not without tweaking Vitalik's transaction.py).
kumarz
<electrum><bip32-hd-wallets>
replicating in python what the electrum wallet does during (deterministic) wallet creation
kumarz
# replicating in python what the electrum wallet does during (deterministic) wallet creation

(Asked by: username_0 on 2016-05-24)

I am trying to replicate in python what the Electrum wallet does during (deterministic) wallet creation. IMHO the docs are a bit sparse on this one bit maybe because its only performed once for most users, all the tutorials etc. seem to focus on the GUI, and I was unable to work out the python code from the [spesmilo github for electrum](https://github.com/spesmilo/electrum).

From what I can see, Electrum generates a random Wallet Generation Seed

1. a 13-word mnemonic([or 12 words plus checksum?](https://bitcoin.stackexchange.com/questions/36839/electrum-2-0-non-bip39-32-standardisation-complicates-matters-immensely-why/36945))
2. which is unencrypted This is also the Master Private Key?
3. Then the user enters a password/passphrase(or not) that encrypts the seed so it can be saved as a wallet(file)
4. The wallet file can only be decrypted with the password/passphrase
5. Then a Master Public Key is generated from the wallet *[Anybody have more detail on this?]*.
6. Then the 25-byte binary Bitcoin Address =
   {[MPK>sha256>RIPEMD160]+[MPK>sha256>RIPEMD160>versionByteadd>sha256>sha256>first4bytes]}
7. which in turn is Base58Check encoded to the [Bitcoin Address format](https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses)

Can someone please check if my assumptions (1 to 7) are okay/correct me?
Also, below is how far I got with python(probably grossly incorrect!):

```
from electrum import mnemonic
import ecdsa
import hashlib
import base58
import getpass

mnemonicInstance = mnemonic.Mnemonic(lang='en')
randseed = mnemonicInstance.make_seed()
print "this is my 13 word wallet gen seed" + randseed
private_key = mnemonicInstance.mnemonic_to_seed(randseed , getpass.getpass()).encode('hex')
print "this is my private key: " + private_key
#the line of code below failed!:
sk = ecdsa.SigningKey.from_string(private_key.decode("hex"), curve = ecdsa.SECP256k1)
vk = sk.verifying_key
public_key = ('\04' + vk.to_string()).encode("hex")
print "this is my public key: " + public_key
ripemd160 = hashlib.new('ripemd160')
ripemd160.update(hashlib.sha256(public_key.decode('hex')).digest())
middle_man = '\00' + ripemd160.digest()
checksum = hashlib.sha256(hashlib.sha256(middle_man).digest()).digest()[:4]
binary_addr = middle_man + checksum
addr = base58.b58encode(binary_addr)
print "this is my BTC address: " + addr

```

(some of the above lifted from [Shultzi's github](https://github.com/Shultzi/Mybitcoin/blob/master/keys.py)) I also consulted Andreas' [Mastering Bitcoin](https://github.com/bitcoinbook/bitcoinbook) but not had much joy with this bit.

**Tags:** electrum, bip32-hd-wallets

> **Comments:**
> **username_1**: Have you experimented with [pybitcointools](https://github.com/vbuterin/pybitcointools)?  Might be a good place to start when learning.  
> **username_0**: yes I have actually Jon, didn't bother putting Vitalik's code in as it does essentially the same thing. Actually I prefer pybitcointools coz it supports testnet(electrum doesn't)  Anyway, not had much joy so wanted this clarified/corrected if poss: maybe edit my post(please) like Nick did ?  
> **username_1**: Seems there is some work underway now that people want to test Segregated Witness: https://github.com/spesmilo/electrum/issues/541  
> **username_0**: yeah that'll be awesome coz as I just figured out, it seems its not possible to push multisig txns(mk_multisig_script) to testnet using pybitcointools (not without tweaking Vitalik's transaction.py).  

---

## 1 answers

### Answer by username_2 on 2016-05-26. Score: 1

Bip32 is the implementation of hierarchical deterministic wallets. This fully describes what you are discussing here. The full text of bip32 can be found <https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki>
The mnemonic maps to specific bits for the encryption, the words themselves are not used. Read the bit about extending the keys it will also explain how the master public key is generated. It also explains the three derivations that can be made from the public and private keys which are:

Parent private key -> child private key

Parent private key -> child public key

Parent public key -> child public key

It is worth noting the child private key cannot be generated from the parent public key.

> **Comments:**
> **username_0**: many thanks Mark ... I think I get the concept and have read BIP32(thanks for the link!) ; I suppose my post was more about the python implementation of this: I am being lazy I know, but was kindof hoping someone's been in my shoes before and could just spit the code out :D  I see the pycoin implementation but was unable to work out how it does this in testnet.  

---

