4

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.

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

  1. a 13-word mnemonic(or 12 words plus checksum?)

  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

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) I also consulted Andreas' Mastering Bitcoin but not had much joy with this bit.

4
  • 1
    Have you experimented with pybitcointools? Might be a good place to start when learning. May 25, 2016 at 23:27
  • 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
    May 27, 2016 at 8:19
  • 1
    Seems there is some work underway now that people want to test Segregated Witness: github.com/spesmilo/electrum/issues/541 Jun 8, 2016 at 11:49
  • 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
    Jun 9, 2016 at 15:55

1 Answer 1

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.

1
  • 1
    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
    Jun 9, 2016 at 16:01

Your Answer

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

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