def test_spend_p2sh_utxos(setup_tx_creation): #make a multisig address from 3 privs privs = [chr(x) * 32 + '\x01' for x in range(1, 4)] pubs = [ bitcoin.privkey_to_pubkey(binascii.hexlify(priv)) for priv in privs ] script = bitcoin.mk_multisig_script(pubs, 2) msig_addr = bitcoin.scriptaddr(script, magicbyte=196) #pay into it wallet = make_wallets(1, [[2, 0, 0, 0, 1]], 3)[0]['wallet'] sync_wallet(wallet) amount = 350000000 ins_full = wallet.select_utxos(0, amount) txid = make_sign_and_push(ins_full, wallet, amount, output_addr=msig_addr) assert txid #wait for mining time.sleep(1) #spend out; the input can be constructed from the txid of previous msig_in = txid + ":0" ins = [msig_in] #random output address and change addr output_addr = wallet.get_new_addr(1, 1) amount2 = amount - 50000 outs = [{'value': amount2, 'address': output_addr}] tx = bitcoin.mktx(ins, outs) sigs = [] for priv in privs[:2]: sigs.append(bitcoin.multisign(tx, 0, script, binascii.hexlify(priv))) tx = bitcoin.apply_multisignatures(tx, 0, script, sigs) txid = jm_single().bc_interface.pushtx(tx) assert txid
def test_spend_p2wsh(setup_tx_creation): #make 2 x 2 of 2multisig outputs; will need 4 privs privs = [struct.pack(b'B', x) * 32 + b'\x01' for x in range(1, 5)] privs = [binascii.hexlify(priv).decode('ascii') for priv in privs] pubs = [bitcoin.privkey_to_pubkey(priv) for priv in privs] redeemScripts = [ bitcoin.mk_multisig_script(pubs[i:i + 2], 2) for i in [0, 2] ] scriptPubKeys = [ bitcoin.pubkeys_to_p2wsh_script(pubs[i:i + 2]) for i in [0, 2] ] addresses = [ bitcoin.pubkeys_to_p2wsh_address(pubs[i:i + 2]) for i in [0, 2] ] #pay into it wallet_service = make_wallets(1, [[3, 0, 0, 0, 0]], 3)[0]['wallet'] wallet_service.sync_wallet(fast=True) amount = 35000000 p2wsh_ins = [] for addr in addresses: ins_full = wallet_service.select_utxos(0, amount) txid = make_sign_and_push(ins_full, wallet_service, amount, output_addr=addr) assert txid p2wsh_ins.append(txid + ":0") #wait for mining time.sleep(1) #random output address and change addr output_addr = wallet_service.get_internal_addr(1) amount2 = amount * 2 - 50000 outs = [{'value': amount2, 'address': output_addr}] tx = bitcoin.mktx(p2wsh_ins, outs) sigs = [] for i in range(2): sigs = [] for priv in privs[i * 2:i * 2 + 2]: # sign input j with each of 2 keys sig = bitcoin.multisign(tx, i, redeemScripts[i], priv, amount=amount) sigs.append(sig) # check that verify_tx_input correctly validates; assert bitcoin.verify_tx_input(tx, i, scriptPubKeys[i], sig, bitcoin.privkey_to_pubkey(priv), scriptCode=redeemScripts[i], amount=amount) tx = bitcoin.apply_p2wsh_multisignatures(tx, i, redeemScripts[i], sigs) txid = jm_single().bc_interface.pushtx(tx) assert txid
def test_create_p2sh_output_tx(setup_tx_creation, nw, wallet_structures, mean_amt, sdev_amt, amount, pubs, k): wallets = make_wallets(nw, wallet_structures, mean_amt, sdev_amt) for w in wallets.values(): sync_wallet(w['wallet']) for k, w in enumerate(wallets.values()): wallet = w['wallet'] ins_full = wallet.select_utxos(0, amount) script = bitcoin.mk_multisig_script(pubs, k) #try the alternative argument passing pubs.append(k) script2 = bitcoin.mk_multisig_script(*pubs) assert script2 == script output_addr = bitcoin.scriptaddr(script, magicbyte=196) txid = make_sign_and_push(ins_full, wallet, amount, output_addr=output_addr) assert txid
def msig_data_from_pubkeys(pubkeys, N): """Create a p2sh address for the list of pubkeys given, N signers required. Return both the multisig redeem script and the p2sh address created. Order of pubkeys is respected (see TODO). """ #TODO: lexicographical ordering is better multisig_script = btc.mk_multisig_script(pubkeys, N) p2sh_address = btc.p2sh_scriptaddr(multisig_script, magicbyte=get_p2sh_vbyte()) return (multisig_script, p2sh_address)
def test_create_p2sh_output_tx(setup_tx_creation, nw, wallet_structures, mean_amt, sdev_amt, amount, pubs, k): wallets = make_wallets(nw, wallet_structures, mean_amt, sdev_amt) for w in wallets.values(): sync_wallet(w['wallet'], fast=True) for k, w in enumerate(wallets.values()): wallet = w['wallet'] ins_full = wallet.select_utxos(0, amount) script = bitcoin.mk_multisig_script(pubs, k) output_addr = bitcoin.script_to_address(script, vbyte=196) txid = make_sign_and_push(ins_full, wallet, amount, output_addr=output_addr) assert txid
def test_create_p2sh_output_tx(setup_tx_creation, nw, wallet_structures, mean_amt, sdev_amt, amount, pubs, k): wallets = make_wallets(nw, wallet_structures, mean_amt, sdev_amt) for w in wallets.values(): w['wallet'].sync_wallet(fast=True) for k, w in enumerate(wallets.values()): wallet_service = w['wallet'] ins_full = wallet_service.select_utxos(0, amount) script = bitcoin.mk_multisig_script(pubs, k) output_addr = bitcoin.p2sh_scriptaddr(bitcoin.safe_from_hex(script), magicbyte=196) txid = make_sign_and_push(ins_full, wallet_service, amount, output_addr=output_addr) assert txid