Skip to content

Instantly share code, notes, and snippets.

@shkschneider
Created September 18, 2017 08:43
Show Gist options
  • Save shkschneider/f1c6d434f61fe526ea0172be3b4e3a4d to your computer and use it in GitHub Desktop.
Save shkschneider/f1c6d434f61fe526ea0172be3b4e3a4d to your computer and use it in GitHub Desktop.
Bitcoin brainwallet (mainnet)
#!/usr/bin/env python
#
# <https://github.com/arzzen/python-simple-brainwallet>
# <https://github.com/jgilmour/brainwallet-check>
# <https://bitcointalk.org/index.php?topic=84238.0>
# JeromeS
# ShkSchneider <https://github.com/shkschneider>
import base58
import binascii
import ecdsa
import hashlib
import requests
import sys
# https://en.bitcoin.it/wiki/Secp256k1
def secp256k1():
a = 0x0000000000000000000000000000000000000000000000000000000000000000L
b = 0x0000000000000000000000000000000000000000000000000000000000000007L
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2FL
Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798L
Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8L
n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141L
curve = ecdsa.ellipticcurve.CurveFp(p, a, b)
point = ecdsa.ellipticcurve.Point(curve, Gx, Gy, n)
return ecdsa.curves.Curve("secp256k1", curve, point, (1, 3, 132, 0, 10))
# https://en.bitcoin.it/wiki/Brainwallet
def brainwallet(private_key):
pko = ecdsa.SigningKey.from_secret_exponent(private_key, secp256k1())
pubkey1 = binascii.hexlify(pko.get_verifying_key().to_string())
pubkey2 = hashlib.sha256(binascii.unhexlify('04' + pubkey1)).hexdigest()
pubkey3 = hashlib.new('ripemd160', binascii.unhexlify(pubkey2)).hexdigest()
pubkey4 = hashlib.sha256(binascii.unhexlify('00' + pubkey3)).hexdigest()
pubkey5 = hashlib.sha256(binascii.unhexlify(pubkey4)).hexdigest()
pubkey6 = pubkey3 + pubkey5[:8]
pubnum = int(pubkey6, 16)
pubnumlist = []
while pubnum != 0:
pubnumlist.append(pubnum % 58);
pubnum /= 58
address=''
b58_digits = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
for l in [b58_digits[x] for x in pubnumlist]:
address = l + address
return '1' + address
def public(seed):
key = int(hashlib.sha256(seed).hexdigest(), 16)
return brainwallet(key)
def private(seed):
key = hashlib.sha256(seed).hexdigest()
return str(key)
# https://en.bitcoin.it/wiki/Wallet_import_format
def wif(key):
extended = "80" + key
first = hashlib.sha256(binascii.unhexlify(extended)).hexdigest()
second = hashlib.sha256(binascii.unhexlify(first)).hexdigest()
key = extended + second[:8]
return base58.b58encode(binascii.unhexlify(key))
def balance(addr):
r = requests.get('https://blockchain.info/q/addressbalance/' + addr)
return float(r.text)
def seen(addr):
r = requests.get('https://blockchain.info/q/addressfirstseen/' + addr)
return (int(r.text) != 0)
if __name__ == "__main__":
seeds = sys.stdin if len(sys.argv) <= 1 else sys.argv[1:]
for seed in seeds:
seed = str(seed)
wif = wif(private(seed))
addr = public(seed)
balance = balance(addr)
print wif, addr, str(balance) + ' BTC', ('safe' if seen else 'UNSAFE')
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment