Skip to content

Instantly share code, notes, and snippets.

@RCasatta
Forked from kumrzz/seed2keys.py
Last active May 23, 2021 09:08
Show Gist options
  • Save RCasatta/7a9f28f04326e755ca55ddac57ccf3de to your computer and use it in GitHub Desktop.
Save RCasatta/7a9f28f04326e755ca55ddac57ccf3de to your computer and use it in GitHub Desktop.
Creates public and private keys from Electrum 2.0 seed
#!/usr/bin/env python
''' Run from "electrum/lib" directory.
ThomaxV started a discussion on https://bitcointalk.org/index.php?topic=274182.msg2939599#msg2939599
about changing the HD derivation logic in electrum 2. The below post:
http://bitcoin.stackexchange.com/questions/37013/how-to-import-a-hd-wallet-from-an-extended-private-key
confirms that the format is actually m/c/i (and not m/44'/0'/a'/c/i)
so I have altered https://gist.github.com/romanz/4b32782bfc0ff4984713 accordingly:
tested to work with keys/addresses exported from electrum 2.7.0
note Electrum has a "gap limit" of 20, so loop @ level "c" to get the next 20 addresses
'''
from electrum import bitcoin, mnemonic
mn = raw_input("Enter mnemonic (13 words)").strip()
s = mnemonic.Mnemonic.mnemonic_to_seed(mn, '')
print 'seed:', repr(s)
xprv, xpub = bitcoin.bip32_root(s,0)
print 'root:', xprv, xpub
xprv, xpub = bitcoin.bip32_private_derivation(xprv, "m", "m/0") # 1st account
# generate first receiving addresses:
for i in range(20):
prv, pub = bitcoin.bip32_private_derivation(xprv, "", "/%d" % (i,))
pub = bitcoin.deserialize_xkey(pub, False)[-1]
prv = bitcoin.deserialize_xkey(prv, True)[-1]
addr = bitcoin.public_key_to_p2pkh(pub)
wif = bitcoin.SecretToASecret(prv, compressed=True)
print '%4d: %s %s' % (i, addr, wif)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment