From e078f0453830f3c56254a551ead374710e818394 Mon Sep 17 00:00:00 2001 From: Busayo Dada Date: Mon, 24 Mar 2025 18:28:27 +0100 Subject: [PATCH] bdk wallet --- .gitignore | 1 + address.txt | 3 ++ wallet.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 .gitignore create mode 100644 address.txt create mode 100644 wallet.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f5e96db --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv \ No newline at end of file diff --git a/address.txt b/address.txt new file mode 100644 index 0000000..b9d0e90 --- /dev/null +++ b/address.txt @@ -0,0 +1,3 @@ +tb1q4w2rwta9tpca67qggdgwmqjh8vecreq7kmv2fm +tb1qaqtshfgw39ym9fzcdj3ljnjaf3pvhsyr3qhymp +tb1q5g9nrgqykeqevqqgvfm0lzrmn4w74x4p6ssgc6 diff --git a/wallet.py b/wallet.py new file mode 100644 index 0000000..9a7883c --- /dev/null +++ b/wallet.py @@ -0,0 +1,83 @@ +import os +import bdkpython as bdk + +# Function to generate and save addresses to file +def generate_and_save_addresses(wallet, address_file): + generated_addresses = [] + for i in range(3): # Generate 3 addresses + address_info = wallet.peek_address(index=i, keychain=bdk.KeychainKind.EXTERNAL) + address = str(address_info.address) # Convert Address to string + generated_addresses.append(address) + + # Save the generated addresses to the address file + with open(address_file, 'w') as file: + for address in generated_addresses: + file.write(f"{address}\n") + + print(f"Generated and saved new addresses: {generated_addresses}") + return generated_addresses + +# Initialize wallet +def create_wallet(): + network = bdk.Network.TESTNET + # Generate mnemonic seed + entropy = os.urandom(16) # 128 bits = 16 bytes + mnemonic = bdk.Mnemonic.from_entropy(entropy) + descriptor_secret_key = bdk.DescriptorSecretKey(network, mnemonic, "") + derivation_path = bdk.DerivationPath("m/84h/1h/0h") + xprv = descriptor_secret_key.extend(derivation_path) + xpub = xprv.as_public() + + clean_xpub = xpub.as_string().split("]")[-1].replace("/*", "") + descriptor = bdk.Descriptor(f"wpkh({clean_xpub}/0/*)", network) + change_descriptor = bdk.Descriptor(f"wpkh({clean_xpub}/1/*)", network) + connection = bdk.Connection.new_in_memory() + wallet = bdk.Wallet(descriptor=descriptor, change_descriptor=change_descriptor, network=network, connection=connection) + + return wallet + +# Path to the file where the addresses will be saved +address_file = 'address.txt' + +# Create the wallet +wallet = create_wallet() + +# Check if the address file exists and read the addresses if available +if os.path.exists(address_file) and os.path.getsize(address_file) > 0: + with open(address_file, 'r') as file: + saved_addresses = file.read().splitlines() + if len(saved_addresses) == 3: + print(f"Using saved addresses from {address_file}:") + for idx, address in enumerate(saved_addresses, 1): + print(f"Address {idx}: {address}") + else: + print(f"Invalid address file, generating new addresses...") + saved_addresses = generate_and_save_addresses(wallet, address_file) +else: + print("No saved addresses found or file is empty, generating new ones...") + saved_addresses = generate_and_save_addresses(wallet, address_file) + +# Connect to Electrum server (using a public Electrum server for Testnet) +electrum_url = "ssl://electrum.blockstream.info:60002" +electrum_client = bdk.ElectrumClient(electrum_url) + +# Test connection with block_count instead of ping +try: + connection_status = electrum_client.sync # This checks if the server is reachable + print("Successfully connected to Electrum server") + + # Fee estimates + fee = electrum_client.estimate_fee(6) # 6 block target + print(f"Estimated fee for 6 blocks: {fee}") + +except Exception as e: + print(f"Error connecting to Electrum server: {e}") + +# Get wallet balance +print("\n----- Wallet Balance -----") +balance = wallet.balance() + +# Accessing and printing the confirmed balance +confirmed_balance = balance.total.to_sat() +print(f"Confirmed balance: {confirmed_balance}") +