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
a 13-word mnemonic(or 12 words plus checksum?)
which is unencrypted This is also the Master Private Key?
Then the user enters a password/passphrase(or not) that encrypts the seed so it can be saved as a wallet(file)
The wallet file can only be decrypted with the password/passphrase
Then a Master Public Key is generated from the wallet [Anybody have more detail on this?].
Then the 25-byte binary Bitcoin Address = {[MPK>sha256>RIPEMD160]+[MPK>sha256>RIPEMD160>versionByteadd>sha256>sha256>first4bytes]}
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.
1
Have you experimented with pybitcointools? Might be a good place to start when learning.
– Jonathan Cross – 2016-05-25T23:27:08.977yes 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 – 2016-05-27T08:19:01.103
1
Seems there is some work underway now that people want to test Segregated Witness: https://github.com/spesmilo/electrum/issues/541
– Jonathan Cross – 2016-06-08T11:49:30.757yeah 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 – 2016-06-09T15:55:40.270