Creating a brainwallet with Python

0 5
Avatar for cashdev
4 years ago

Using a brainwallet is a terrible idea. We humans are terrible at creating phrases that cannot be easily brute force attacked.

Using a brainwallet is a terrible idea and you're likely to loose all your coins.

But its also awesome and convenient when developing and testing.

What's a brainwallet?

A brainwallet is simply a text phrase, such as steal these coins that is encoded into a private key. Encoded with sha256 and some magic –sha256("steal these coins").

Creating a brainwallet with Python

...using the libraries bitcoincash and qrcode

from bitcoincash.wallet import CBitcoinSecret, P2PKHBitcoinAddress
import hashlib

def calculate_brainwallet(phrase):
    h = hashlib.sha256(phrase).digest()
    private_key = CBitcoinSecret.from_secret_bytes(h)
    address = P2PKHBitcoinAddress.from_pubkey(private_key.pub)
    return address, private_key

SECRET_PHRASE = b'secret phrase replace or loose all coins'
cashaddr, privkey = calculate_brainwallet(SECRET_PHRASE)

For extra convenience, output the addresses as QR codes in the terminal

def print_qrcode(data):
    import qrcode
    qr = qrcode.QRCode()
    qr.add_data(data)
    qr.print_ascii()

print_qrcode(str(cashaddr))
print(f"Cashadddr: {cashaddr}")
print_qrcode(str(privkey))
print(f"Private key: {privkey}")

Sponsors of cashdev
empty
empty
empty

1
$ 0.00
Avatar for cashdev
4 years ago

Comments