Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
venv
3 changes: 3 additions & 0 deletions address.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tb1q4w2rwta9tpca67qggdgwmqjh8vecreq7kmv2fm
tb1qaqtshfgw39ym9fzcdj3ljnjaf3pvhsyr3qhymp
tb1q5g9nrgqykeqevqqgvfm0lzrmn4w74x4p6ssgc6
83 changes: 83 additions & 0 deletions wallet.py
Original file line number Diff line number Diff line change
@@ -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}")