def test_from_address(): # testing from address address = "mqLyrNDjpENRMZAoDpspH7kR9RtgvhWzYE" # Initialize bitcoin wallet bitcoin_from_address = Wallet(network="testnet").from_address(address) assert bitcoin_from_address.address() == address _hash = "6bce65e58a50b97989930e9a4ff1ac1a77515ef1" assert bitcoin_from_address.hash() == _hash p2pkh = "76a9146bce65e58a50b97989930e9a4ff1ac1a77515ef188ac" assert bitcoin_from_address.p2pkh() == p2pkh p2sh = "a914347283eee92ad685909044619adaa70370b2538787" assert bitcoin_from_address.p2sh() == p2sh
def test_bitcoin_wallet_from_mnemonic(): wallet = Wallet(network=_["bitcoin"]["network"]) wallet.from_mnemonic( mnemonic=_["bitcoin"]["wallet"]["sender"]["mnemonic"], language=_["bitcoin"]["wallet"]["sender"]["language"], passphrase=_["bitcoin"]["wallet"]["sender"]["passphrase"]) wallet.from_path( path=_["bitcoin"]["wallet"]["sender"]["derivation"]["path"]) assert wallet.entropy() == _["bitcoin"]["wallet"]["sender"]["entropy"] assert wallet.mnemonic() == _["bitcoin"]["wallet"]["sender"]["mnemonic"] assert wallet.language() == _["bitcoin"]["wallet"]["sender"]["language"] assert wallet.passphrase() is None assert wallet.seed() == _["bitcoin"]["wallet"]["sender"]["seed"] assert wallet.root_xprivate_key( ) == _["bitcoin"]["wallet"]["sender"]["root_xprivate_key"] assert wallet.root_xpublic_key( ) == _["bitcoin"]["wallet"]["sender"]["root_xpublic_key"] assert wallet.xprivate_key( ) == _["bitcoin"]["wallet"]["sender"]["xprivate_key"] assert wallet.xpublic_key( ) == _["bitcoin"]["wallet"]["sender"]["xpublic_key"] assert wallet.uncompressed( ) == _["bitcoin"]["wallet"]["sender"]["uncompressed"] assert wallet.compressed( ) == _["bitcoin"]["wallet"]["sender"]["compressed"] assert wallet.chain_code( ) == _["bitcoin"]["wallet"]["sender"]["chain_code"] assert wallet.private_key( ) == _["bitcoin"]["wallet"]["sender"]["private_key"] assert wallet.public_key( ) == _["bitcoin"]["wallet"]["sender"]["public_key"] assert wallet.wif() == _["bitcoin"]["wallet"]["sender"]["wif"] assert wallet.hash() == _["bitcoin"]["wallet"]["sender"]["hash"] assert wallet.finger_print( ) == _["bitcoin"]["wallet"]["sender"]["finger_print"] assert wallet.path( ) == _["bitcoin"]["wallet"]["sender"]["derivation"]["path"] assert wallet.address() == _["bitcoin"]["wallet"]["sender"]["address"]
# Choose network mainnet or testnet NETWORK: str = "testnet" # Default to mainnet # Choose strength 128, 160, 192, 224 or 256 STRENGTH: int = 160 # Default is 128 # Choose language english, french, italian, spanish, chinese_simplified, chinese_traditional, japanese or korean LANGUAGE: str = "english" # Default is english # Set passphrase length LENGTH: int = 32 # Default is 32 # Generate new entropy hex string ENTROPY: str = generate_entropy(strength=STRENGTH) # Generate new passphrase PASSPHRASE: str = generate_passphrase(length=LENGTH) # Initialize Bitcoin wallet wallet: Wallet = Wallet(network=NETWORK) # Get Bitcoin wallet from entropy wallet.from_entropy(entropy=ENTROPY, language=LANGUAGE, passphrase=PASSPHRASE) # Drive Bitcoin wallet from path wallet.from_path(path=DEFAULT_PATH) # Print all Bitcoin wallet info's print("Strength:", wallet.strength()) print("Entropy:", wallet.entropy()) print("Mnemonic:", wallet.mnemonic()) print("Language:", wallet.language()) print("Passphrase:", wallet.passphrase()) print("Seed:", wallet.seed()) print("Root XPrivate Key:", wallet.root_xprivate_key()) print("Root XPublic Key:", wallet.root_xpublic_key()) print("XPrivate Key:", wallet.xprivate_key())
def test_bitcoin_wallet_from_xprivate_key(): wallet = Wallet(network=_["bitcoin"]["network"]) wallet.from_xprivate_key( xprivate_key=_["bitcoin"]["wallet"]["recipient"]["xprivate_key"]) assert wallet.entropy() is None assert wallet.mnemonic() is None assert wallet.language() is None assert wallet.passphrase() is None assert wallet.seed() is None assert wallet.root_xprivate_key() is None assert wallet.root_xpublic_key() is None assert wallet.xprivate_key( ) == _["bitcoin"]["wallet"]["recipient"]["xprivate_key"] assert wallet.xpublic_key( ) == _["bitcoin"]["wallet"]["recipient"]["xpublic_key"] assert wallet.uncompressed( ) == _["bitcoin"]["wallet"]["recipient"]["uncompressed"] assert wallet.compressed( ) == _["bitcoin"]["wallet"]["recipient"]["compressed"] assert wallet.chain_code( ) == _["bitcoin"]["wallet"]["recipient"]["chain_code"] assert wallet.private_key( ) == _["bitcoin"]["wallet"]["recipient"]["private_key"] assert wallet.public_key( ) == _["bitcoin"]["wallet"]["recipient"]["public_key"] assert wallet.wif() == _["bitcoin"]["wallet"]["recipient"]["wif"] assert wallet.hash() == _["bitcoin"]["wallet"]["recipient"]["hash"] assert wallet.p2pkh() == _["bitcoin"]["wallet"]["recipient"]["p2pkh"] assert wallet.finger_print( ) == _["bitcoin"]["wallet"]["recipient"]["finger_print"] assert wallet.path() is None assert wallet.address() == _["bitcoin"]["wallet"]["recipient"]["address"]
#!/usr/bin/env python3 from swap.providers.bitcoin.wallet import Wallet from swap.cli.__main__ import main as cli_main from swap.utils import sha256 network = "testnet" sender_wallet = Wallet( network=network).from_passphrase("meheret tesfaye batu bayou") recipient_wallet = Wallet(network=network).from_passphrase("meheret") secret_hash = sha256("Hello Meheret!") sequence = 1000 def test_bitcoin_cli_htlc(cli_tester): htlc = cli_tester.invoke(cli_main, [ "bitcoin", "htlc", "--secret-hash", secret_hash, "--recipient-address", recipient_wallet.address(), "--sender-address", sender_wallet.address(), "--sequence", sequence, "--network", network ]) assert htlc.exit_code == 0 assert htlc.output == "63aa20821124b554d13f247b1e5d10b84e44fb1296f18f38bbaa1bea34a12c843e01588876a91498f8" \ "79fb7f8b4951dee9bc8a0327b792fbe332b888ac6702e803b27576a91464a8390b0b1685fcbf2d4b45" \ "7118dc8da92d553488ac68" + "\n" htlc = cli_tester.invoke(cli_main, [ "bitcoin", "htlc", "--secret-hash", secret_hash, "--recipient-address", "L5tUq6mCbE84XobZ1mphBPZf15cRFcvg7Q", "--sender-address", sender_wallet.address(), "--sequence", sequence, "--network", network ])
# Bitcoin network NETWORK = "testnet" # Sender passphrase/password SENDER_PASSPHRASE = "Boom!" # Hash Time Lock Contract (HTLC) bytecode HTLC_BYTECODE = "63aa20821124b554d13f247b1e5d10b84e44fb1296f18f38bbaa1bea34a12c843e01588876" \ "a914acf8419eecab574c494febbe03fd07fdae7bf2f488ac6702e803b27576a9141d0f671c" \ "26a3ef7a865d1eda0fbd085e98adcc2388ac68" # Bitcoin fund amount AMOUNT = 10_000 print("=" * 10, "Sender Bitcoin Account") # Initializing sender Bitcoin wallet sender_wallet = Wallet(network=NETWORK) # Initializing Bitcoin wallet from passphrase sender_wallet.from_passphrase(passphrase=SENDER_PASSPHRASE) # Getting sender wallet information's sender_private_key = sender_wallet.private_key() print("Sender Private Key:", sender_private_key) sender_public_key = sender_wallet.public_key() print("Sender Public Key:", sender_public_key) sender_compressed = sender_wallet.compressed() print("Sender Compressed:", sender_compressed) sender_uncompressed = sender_wallet.uncompressed() print("Sender Uncompressed:", sender_uncompressed) sender_address = sender_wallet.address() print("Sender Address:", sender_address) sender_hash = sender_wallet.hash() print("Sender Hash:", sender_hash)
def test_from_private_key(): # testing from private keky private_key = "92cbbc5990cb5090326a76feeb321cad01048635afe5756523bbf9f7a75bf38b" # Initialize bitcoin wallet bitcoin_from_private_key = Wallet(network="testnet")\ .from_private_key(private_key) private_key = "92cbbc5990cb5090326a76feeb321cad01048635afe5756523bbf9f7a75bf38b" assert bitcoin_from_private_key.private_key() == private_key public_key = "03c56a6005d4a8892d28cc3f7265e5685b548627d59108973e474c4e26f69a4c84" assert bitcoin_from_private_key.public_key() == public_key address = "mphBPZf15cRFcL5tUq6mCbE84XobZ1vg7Q" assert bitcoin_from_private_key.address() == address # Initialize bitcoin wallet bitcoin_from_private_key = Wallet(network="testnet") \ .from_private_key(private_key, False) private_key = "92cbbc5990cb5090326a76feeb321cad01048635afe5756523bbf9f7a75bf38b" assert bitcoin_from_private_key.private_key() == private_key public_key = "04c56a6005d4a8892d28cc3f7265e5685b548627d59108973e474c4e26f69a4c84fee63d89a5979801c9659" \ "94963c77bfb470dff5afd351a442ebf329f3b2c2835" assert bitcoin_from_private_key.public_key() == public_key compressed = "03c56a6005d4a8892d28cc3f7265e5685b548627d59108973e474c4e26f69a4c84" assert bitcoin_from_private_key.compressed() == compressed uncompressed = "04c56a6005d4a8892d28cc3f7265e5685b548627d59108973e474c4e26f69a4c84fee63d89a5979801c96" \ "5994963c77bfb470dff5afd351a442ebf329f3b2c2835" assert bitcoin_from_private_key.uncompressed() == uncompressed assert bitcoin_from_private_key.uncompressed() == bitcoin_from_private_key.public_key() address = "mqLyrNDjpENRMZAoDpspH7kR9RtgvhWzYE" assert bitcoin_from_private_key.address() == address _hash = "6bce65e58a50b97989930e9a4ff1ac1a77515ef1" assert bitcoin_from_private_key.hash() == _hash p2pkh = "76a9146bce65e58a50b97989930e9a4ff1ac1a77515ef188ac" assert bitcoin_from_private_key.p2pkh() == p2pkh p2sh = "a914347283eee92ad685909044619adaa70370b2538787" assert bitcoin_from_private_key.p2sh() == p2sh
def test_bitcoin_wallet_tools(): wallet = Wallet(network="testnet") private_key = "92cbbc5990cb5090326a76feeb321cad01048635afe5756523bbf9f7a75bf38b" public_key = wallet.public_key(private_key=private_key) assert public_key == "03c56a6005d4a8892d28cc3f7265e5685b548627d59108973e474c4e26f69a4c84" public_key = "04c56a6005d4a8892d28cc3f7265e5685b548627d59108973e474c4e26f69a4c84fee63d89a5979801c96" \ "5994963c77bfb470dff5afd351a442ebf329f3b2c2835" compressed = wallet.compressed(public_key=public_key) assert compressed == "03c56a6005d4a8892d28cc3f7265e5685b548627d59108973e474c4e26f69a4c84" uncompressed = wallet.uncompressed(public_key=public_key) assert uncompressed == public_key address = wallet.address(public_key=uncompressed) assert address == "mqLyrNDjpENRMZAoDpspH7kR9RtgvhWzYE" _hash = wallet.hash(public_key=uncompressed) assert _hash == "6bce65e58a50b97989930e9a4ff1ac1a77515ef1" p2pkh = wallet.p2pkh(address=address) assert p2pkh == "76a9146bce65e58a50b97989930e9a4ff1ac1a77515ef188ac" p2sh = wallet.p2sh(address=address) assert p2sh == "a914347283eee92ad685909044619adaa70370b2538787" balance = wallet.balance(address=address, network="testnet") assert isinstance(balance, int) unspent = wallet.unspent(address=address, network="testnet", limit=1) assert isinstance(unspent, list)
NETWORK: str = "testnet" # Bitcoin funded transaction hash/id TRANSACTION_HASH: str = "853a27875a51ba8290cf5e5b32a0e1bbc9273343ff2b65ffad949bce942b9379" # Bitcoin recipient wallet mnemonic RECIPIENT_MNEMONIC: str = "hint excuse upgrade sleep easily deputy erase cluster section other ugly limit" # The preimage of HTLC contract SECRET_KEY: str = "Hello Meheret!" # Witness Hash Time Lock Contract (HTLC) bytecode BYTECODE: str = "63aa20821124b554d13f247b1e5d10b84e44fb1296f18f38bbaa1bea34a12c843e01588876a9140e259e08f2" \ "ec9fc99a92b6f66fdfcb3c7914fd6888ac67043e3be060b17576a91493162bcadf4406af6429b59958964f62" \ "5d550fcd88ac68" print("=" * 10, "Recipient Bitcoin Account") # Initialize Bitcoin recipient wallet recipient_wallet: Wallet = Wallet(network=NETWORK) # Get Bitcoin recipient wallet from mnemonic recipient_wallet.from_mnemonic(mnemonic=RECIPIENT_MNEMONIC) # Drive Bitcoin recipient wallet from path recipient_wallet.from_path(path=DEFAULT_PATH) # Print some Bitcoin recipient wallet info's print("Root XPrivate Key:", recipient_wallet.root_xprivate_key()) print("Root XPublic Key:", recipient_wallet.root_xprivate_key()) print("Private Key:", recipient_wallet.private_key()) print("Public Key:", recipient_wallet.public_key()) print("Path:", recipient_wallet.path()) print("Address:", recipient_wallet.address()) print("Balance:", recipient_wallet.balance(unit="BTC"), "BTC") print("=" * 10, "Unsigned Withdraw Transaction")
# Bitcoin network NETWORK = "testnet" # Bitcoin transaction id/hash TRANSACTION_ID = "86b21bf4ac171dac4483dc063353b4db3f91541c81e93c66449542b314ed0f8b" # Recipient passphrase/password RECIPIENT_PASSPHRASE = "Woo!" # Sender Bitcoin address SENDER_ADDRESS = "miAcLpYbaqE8KowBu2PwvqXG6y6vpQcfTJ" # Bitcoin claim amount AMOUNT = 10_000 print("=" * 10, "Recipient Bitcoin Account") # Initializing recipient Bitcoin wallet recipient_wallet = Wallet(network=NETWORK) # Initializing Bitcoin wallet from passphrase recipient_wallet.from_passphrase(passphrase=RECIPIENT_PASSPHRASE) # Getting recipient wallet information's recipient_private_key = recipient_wallet.private_key() print("Recipient Private Key:", recipient_private_key) recipient_public_key = recipient_wallet.public_key() print("Recipient Public Key:", recipient_public_key) recipient_compressed = recipient_wallet.compressed() print("Recipient Compressed:", recipient_compressed) recipient_uncompressed = recipient_wallet.uncompressed() print("Recipient Uncompressed:", recipient_uncompressed) recipient_address = recipient_wallet.address() print("Recipient Address:", recipient_address) recipient_hash = recipient_wallet.hash() print("Recipient Hash:", recipient_hash)
# Bitcoin network NETWORK = "testnet" # Secret password/passphrase hash SECRET_HASH = sha256("Hello Meheret!") # Recipient Bitcoin address RECIPIENT_ADDRESS = "mwHXvCcug5Rn24c2rpgcRDSo3PyfxZJQQT" # Sender Bitcoin address SENDER_ADDRESS = "miAcLpYbaqE8KowBu2PwvqXG6y6vpQcfTJ" # Expiration block (Sequence) SEQUENCE = 1000 print("=" * 10, "Sender Bitcoin Account") # Initializing sender Bitcoin wallet sender_wallet = Wallet(network=NETWORK) # Initializing Bitcoin wallet from address sender_wallet.from_address(address=SENDER_ADDRESS) # Getting sender wallet information's sender_address = sender_wallet.address() print("Sender Address:", sender_address) sender_hash = sender_wallet.hash() print("Sender Hash:", sender_hash) sender_p2pkh = sender_wallet.p2pkh() print("Sender P2PKH:", sender_p2pkh) sender_p2sh = sender_wallet.p2sh() print("Sender P2SH:", sender_p2sh) sender_balance = sender_wallet.balance() print("Sender Balance:", sender_balance) print("=" * 10, "Recipient Bitcoin Account")