chatgpt/btc_wallets/Generate_btc_wallet_kdbx.py

75 lines
1.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import os
import qrcode
import base64
from mnemonic import Mnemonic
from bitcoinlib.keys import HDKey
from pykeepass import PyKeePass, create_database
from getpass import getpass
import gc
# Generate a BIP-0039 mnemonic seed phrase
mnemonic = Mnemonic("english")
seed_phrase = mnemonic.generate(strength=128)
# Derive the HDKey from the seed phrase
hd_key = HDKey.from_passphrase(seed_phrase)
# Derive the Bitcoin address from the HDKey
child_key = hd_key.subkey_for_path("m/0/0")
address = child_key.address()
# Create a QR code instance
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
# Add the data to the QR code
qr.add_data(seed_phrase)
# Generate the QR code image
qr.make(fit=True)
qr_image = qr.make_image(fill_color="black", back_color="white")
# Convert the QR code image to base64
qr_base64 = base64.b64encode(qr_image.tobytes()).decode()
# Prompt for custom name for the wallet
wallet_name = input(
"Whould you like to name this wallet? (empty for using the address as name): "
)
# Prompt the user for the passphrase
passphrase = getpass("Enter passphrase for the KeePassXC database: ")
# Create the database filename with the wallet number
if wallet_name == "":
wallet_name = address
db_filename = f"{wallet_name}.kdbx"
# Create a KeePassXC database file
db = create_database(db_filename, password=passphrase)
# Create an entry in the root group
entry = db.add_entry(db.root_group, wallet_name, username=address, password=seed_phrase)
# Add the QR code as a note (base64 encoded)
entry.notes = qr_base64
# Save the database
db.save()
del mnemonic
del seed_phrase, address, hd_key, passphrase, qr_image, qr_base64
gc.collect()
print("---")
print("Bitcoin address was successfully created. You can find at: " + db_filename)
print("---")