Author

Topic: Python MPK cmd line tool (Read 2741 times)

legendary
Activity: 1896
Merit: 1353
March 04, 2013, 04:42:32 AM
#4
you can also install the electrum library, and use it without installing a wallet
Oh. Didn't know that. Would that reduce this to just an import and a few lines?

Sure. The syntax might still change in the future, but currently all you need is:

Code:
from electrum.bitcoin import ElectrumSequence
ElectrumSequence.mpk_from_seed("your seed here")
hero member
Activity: 784
Merit: 1009
firstbits:1MinerQ
March 04, 2013, 04:31:51 AM
#3
you can also install the electrum library, and use it without installing a wallet
Oh. Didn't know that. Would that reduce this to just an import and a few lines?
legendary
Activity: 1896
Merit: 1353
March 04, 2013, 03:05:43 AM
#2
you can also install the electrum library, and use it without installing a wallet
hero member
Activity: 784
Merit: 1009
firstbits:1MinerQ
March 04, 2013, 01:26:34 AM
#1
Just because I was curious I decided to make a simple cmd line python tool that can output the MPK given a seed. It just uses the same code as Electrum client but is much simpler. I was thinking this may be handy as a simple way to generate a wallet offline. The idea is that you only need this very simple script, which can either be copy/pasted or typed in by hand, and a LiveCD/USB to create a wallet.

I was thinking that you can roll dice to gen the seed. Write it down as you go. Then save the MPK for later restore on your desktop Electrum. This way the seed never exists on your desktop and cannot be hacked.

Restoring a wallet from MPK allows watching address balances and generating new addresses but not spending from the addresses. Type command "electrum restore" and pasting the MPK when it asks for seed will create a watch-only wallet.

Code:
#!/usr/bin/env python
#
# Read a seed from stdin and output MPK (Master Public Key).
# Input one seed per line hex (32 chars).
#
# Outputs the SEED and MPK for restoring to Electrum.
# SEED for full wallet, MPK for watch only wallet.
# eg.
#     hexdump -v -e '/1 "%02X"' -n 16 /dev/urandom | mpk
# or
#     echo "1279666de665e6c662d2f45538842acd" |mpk

import sys, ecdsa, hashlib

# secp256k1, http://www.oid-info.com/get/1.3.132.0.10
_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2FL
_r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141L
_b = 0x0000000000000000000000000000000000000000000000000000000000000007L
_a = 0x0000000000000000000000000000000000000000000000000000000000000000L
_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798L
_Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8L
curve_secp256k1 = ecdsa.ellipticcurve.CurveFp( _p, _a, _b )
generator_secp256k1 = ecdsa.ellipticcurve.Point( curve_secp256k1, _Gx, _Gy, _r )
oid_secp256k1 = (1,3,132,0,10)
SECP256k1 = ecdsa.curves.Curve("SECP256k1", curve_secp256k1, generator_secp256k1, oid_secp256k1 )

for seed in sys.stdin:
    oldseed = seed = seed[:32].replace(' ', '')
    for i in range(100000):
        seed = hashlib.sha256(seed + oldseed).digest()
    master_private_key = ecdsa.SigningKey.from_secret_exponent( ecdsa.util.string_to_number( seed ), curve = SECP256k1 )
    print "Seed:", oldseed, "\nMPK:", master_private_key.get_verifying_key().to_string().encode('hex')
This is in my "misc" github repo with my other small scripts.
Although I have tested this to work I suggest you verify yourself. No warranty implied.
Jump to: