def _print_master_key_and_derived(vbytes: bytes, magicbyte: int): seed = _mnemonic_to_seed(b'This is a very funny mnemonic') master_key = bitcoin.bip32_master_key(seed, vbytes=vbytes) bitprint('Master key:', master_key) _derive_and_print(master_key, 1, 2, 3, magicbyte=magicbyte) _derive_and_print(master_key, 1 + 2**31, 2, 3, derive_pub_key=False, magicbyte=magicbyte)
from pprint import pprint from bitcoin_101 import bitprint from bitcoin_101.bitcoin_client import BitcoinClient # make start if __name__ == '__main__': bitprint(BitcoinClient().getinfo()) # show keys_regtest.py
BTC address: {} '''.format( ', '.join( str(x) if x <= 2**31 else str(x - 2**31) + '\'' for x in path), derived_key, derived_bip32_pub_key, derived_pub_key_from_key, bitcoin.bip32_extract_key(derived_key), derived_pub_key, bitcoin.privtopub(bitcoin.bip32_extract_key(derived_key)), bitcoin.pubtoaddr(bitcoin.privtopub( bitcoin.bip32_extract_key(derived_key)), magicbyte=magicbyte))) def _print_master_key_and_derived(vbytes: bytes, magicbyte: int): seed = _mnemonic_to_seed(b'This is a very funny mnemonic') master_key = bitcoin.bip32_master_key(seed, vbytes=vbytes) bitprint('Master key:', master_key) _derive_and_print(master_key, 1, 2, 3, magicbyte=magicbyte) _derive_and_print(master_key, 1 + 2**31, 2, 3, derive_pub_key=False, magicbyte=magicbyte) if __name__ == '__main__': bitprint('\nMAINNET\n') _print_master_key_and_derived(bitcoin.MAINNET_PRIVATE, 0) bitprint('\nTESTNET\n') _print_master_key_and_derived(bitcoin.TESTNET_PRIVATE, 111)
outpoints = { '97f7c7d8ac85e40c255f8a763b6cd9a68f3a94d2e93e8bfa08f977b92e55465e:0': 50000, '4cc806bb04f730c445c60b3e0f4f44b54769a1c196ca37d8d4002135e4abd171:1': 50000 } tx = bitcoin.mktx([{ 'output': k, 'value': v } for k, v in outpoints.items()], [{ 'value': 90000, 'address': '16iw1MQ1sy1DtRPYw3ao1bCamoyBJtRB4t' }]) bitprint('Raw transaction:', tx) bitprint('Deserialized transaction') bitprint(bitcoin.deserialize(tx)) one_signed_tx = bitcoin.sign(tx, 0, key) bitprint(one_signed_tx) bitprint('ScriptSig for the first input') bitprint( bitcoin.deserialize_script( bitcoin.deserialize(one_signed_tx)['ins'][0]['script'])) bitprint('ScriptPubKey for the first output') bitprint( bitcoin.deserialize_script( bitcoin.deserialize(one_signed_tx)['outs'][0]['script']))
import bitcoin from bitcoin_101 import bitprint if __name__ == '__main__': key = bitcoin.random_key() bitprint('Private key:', key) pub_key = bitcoin.privtopub(key) pub_compact_key = bitcoin.privtopub(key + '01') bitprint('Public key:', pub_key) bitprint('Public compact key:', pub_compact_key) signature = bitcoin.ecdsa_raw_sign(b'a message', key) bitprint('Signature verified:', bitcoin.ecdsa_raw_verify(b'a message', signature, pub_key)) bitprint( 'Signature verified with compact public key:', bitcoin.ecdsa_raw_verify(b'a message', signature, pub_compact_key)) bitprint('Address:', bitcoin.pubtoaddr(pub_key)) bitprint('Address (compact):', bitcoin.pubtoaddr(pub_compact_key))
if out['scriptPubKey']['addresses'][0] in btc_addresses: yield { 'output': '{}:{}'.format(btc_transaction, out['n']), 'value': int(out['value'] * 10**8) } if __name__ == '__main__': bitcoin_client = BitcoinClient() _ensure_funds(bitcoin_client) keys = [bitcoin.random_key() + '01' for _ in range(3)] pubkeys = [bitcoin.privtopub(k) for k in keys] addresses = [bitcoin.pubtoaddr(p, magicbyte=111) for p in pubkeys] an_external_address = 'mwDWoUg8vdkpDWtFY6GZJybBqNEM6mmYaz' bitprint('\nPublic keys\n', pubkeys) bitprint('\nAddresses\n', addresses) # Total: 0.06 funding_txs = [ bitcoin_client.sendtoaddress(address, 0.01 * (i + 1)) for i, address in enumerate(addresses) ] bitprint('\nFunding txs') bitprint(funding_txs) outpoints = list(_get_outpoints(bitcoin_client, addresses, funding_txs)) bitprint(outpoints) tx = bitcoin.mktx( outpoints, [ {'value': int(0.055 * 10**8), 'address': an_external_address},