2023-05-21 17:20:46 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Bitcoin Wallet Generation Script
|
|
|
|
|
|
|
|
This script generates a Bitcoin wallet and stores it in a KeePassXC database file (KDBX format).
|
|
|
|
The wallet includes a BIP-0039 mnemonic seed phrase, Bitcoin address, and a QR code representation
|
|
|
|
of the seed phrase. The database file is password protected to ensure the security of the wallet.
|
|
|
|
|
|
|
|
Usage: ./generate_btc_wallet.py
|
|
|
|
"""
|
|
|
|
|
|
|
|
import gc
|
|
|
|
from getpass import getpass
|
|
|
|
from mnemonic import Mnemonic
|
|
|
|
from bitcoinlib.keys import HDKey
|
|
|
|
from pykeepass import create_database
|
|
|
|
|
2023-05-23 10:49:19 +03:00
|
|
|
MNEMONIC_STRENGTH=256
|
|
|
|
SUBKEY_PATH="m/0/0"
|
|
|
|
|
2023-05-21 17:20:46 +03:00
|
|
|
# Generate a BIP-0039 mnemonic seed phrase
|
|
|
|
mnemonic = Mnemonic("english")
|
2023-05-23 10:49:19 +03:00
|
|
|
SEED_PHRASE = mnemonic.generate(strength=MNEMONIC_STRENGTH)
|
2023-05-21 17:20:46 +03:00
|
|
|
|
|
|
|
# Derive the HDKey from the seed phrase
|
|
|
|
hd_key = HDKey.from_passphrase(SEED_PHRASE)
|
|
|
|
|
|
|
|
# Derive the Bitcoin address from the HDKey
|
2023-05-23 10:49:19 +03:00
|
|
|
child_key = hd_key.subkey_for_path(SUBKEY_PATH)
|
2023-05-21 17:20:46 +03:00
|
|
|
address = child_key.address()
|
|
|
|
|
|
|
|
# Prompt for custom name for the wallet
|
|
|
|
wallet_name = input(
|
|
|
|
"Whould you like to name this wallet? (empty for using the address as name): "
|
|
|
|
).strip()
|
|
|
|
|
|
|
|
# Create the database filename with the wallet number
|
|
|
|
if wallet_name == "":
|
|
|
|
wallet_name = address
|
|
|
|
else:
|
|
|
|
wallet_name = wallet_name[:100].strip()
|
|
|
|
|
|
|
|
db_filename = f"{wallet_name}.kdbx"
|
|
|
|
|
|
|
|
# Prompt the user for the passphrase
|
|
|
|
passphrase = getpass("Enter passphrase for the KeePassXC database: ")
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Create a KeePassXC database file
|
|
|
|
db = create_database(db_filename, password=passphrase)
|
|
|
|
|
2023-05-23 10:49:19 +03:00
|
|
|
# Create an entrys in the root group
|
|
|
|
address_and_path_entry = db.add_entry(db.root_group, wallet_name, username=address, password=SUBKEY_PATH)
|
|
|
|
seed_phrase_entry = db.add_entry(db.root_group, "Bitcoin Master Seed", username="put me in safe", password=SEED_PHRASE)
|
2023-05-21 17:20:46 +03:00
|
|
|
|
|
|
|
# Save the database
|
|
|
|
db.save()
|
|
|
|
except Exception as e:
|
|
|
|
print("Error while creating keepassxc database. Disk full? Readonly?")
|
|
|
|
finally:
|
2023-05-23 10:49:19 +03:00
|
|
|
del SEED_PHRASE, address, hd_key, passphrase, mnemonic
|
2023-05-21 17:20:46 +03:00
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
print("---")
|
|
|
|
print("Bitcoin address was successfully created. You can find at: " + db_filename)
|
|
|
|
print("---")
|