Skip to content

Instantly share code, notes, and snippets.

@APTy
Created January 30, 2016 01:46
Show Gist options
  • Save APTy/3b3a17fbe17aef9a2005 to your computer and use it in GitHub Desktop.
Save APTy/3b3a17fbe17aef9a2005 to your computer and use it in GitHub Desktop.
two1 multisig
import two1.lib.bitcoin as bitcoin
from two1.lib.wallet import Wallet
wallet = Wallet()
# Collect public keys
p1_pubkey = wallet.get_payout_public_key()
p2_pubkey = wallet.get_change_public_key()
# Build multisig redeem script
public_keys = [p1_pubkey.compressed_bytes, p2_pubkey.compressed_bytes]
redeem_script = bitcoin.Script.build_multisig_redeem(2, public_keys)
# Build deposit transaction (pays to the redeem script above)
deposit_tx = wallet.build_signed_transaction({redeem_script.address(): 25000}, fees=5000)[0]
print("Deposit: {}\n".format(deposit_tx.to_hex()))
# Build payment transaction
deposit_utxo_index = deposit_tx.output_index_for_address(redeem_script.hash160())
# Build unsigned payment transaction with a placeholder script
script_sig = bitcoin.Script()
inputs = [bitcoin.TransactionInput(deposit_tx.hash, deposit_utxo_index, script_sig, 0xffffffff)]
outputs = [bitcoin.TransactionOutput(15000, bitcoin.Script.build_p2pkh(p1_pubkey.hash160()))]
payment_tx = bitcoin.Transaction(bitcoin.Transaction.DEFAULT_TRANSACTION_VERSION, inputs, outputs, 0x0)
# Sign payment transaction with pubkey 1
public_key = bitcoin.PublicKey.from_bytes(redeem_script.extract_multisig_redeem_info()['public_keys'][0])
private_key = wallet.get_private_for_public(public_key)
payment_tx.sign_input(0, bitcoin.Transaction.SIG_HASH_ALL, private_key, redeem_script)
# Sign payment transaction with pubkey 2
public_key = bitcoin.PublicKey.from_bytes(redeem_script.extract_multisig_redeem_info()['public_keys'][1])
private_key = wallet.get_private_for_public(public_key)
payment_tx.sign_input(0, bitcoin.Transaction.SIG_HASH_ALL, private_key, redeem_script)
print("Payment: {}".format(payment_tx.to_hex()))
Testnet Example Output
----------------------
$ python multisig.py
Deposit: 0100000001c381f52d55afb1382485ed022fd4005debbbdb37fedbccc62a805478a83bcd3e010000006a473044022070c9b8317875c48ba60d71a33c8fac7f85147500a82b78b4af50ca4ea39fb06e02205dafcd5eb7b1aadec765495e2a244aa33a644e9a9ce9b959128675e43777f2d8012103bb8800b3357148eceeea2d2d74d17d04c369a03fcfc6520057f8006935eba975ffffffff02a86100000000000017a914fbaa82d64466f55b14524c5771e38c51d6787dc087aa270802000000001976a9142af20cfdf5af656852b4b7ad6c09f36b99165e3e88ac00000000
Payment: 01000000015ed912393a55c9a0f06a011d7fd5fce8ff58b4ab22c4c7601e994972407c4da400000000db00483045022100921f387893fa36d4b60ecf785e1e8a59d164085750be31301ea22cdea121707602205294c0a01b078eed8afd6fec7a8de96d60125488c3fe476a4c0078ac344b4b8601483045022100a83a97308d0b0235a245e1f7dea4b36e23d907ed3a58c4d3063fdef7fc5c067e022000855617aa4598b2bb7eb492f6f4cd0c0370e7805d8682e5b6f526334fc0a1d30147522103ee3685640bfdc4cddb7cec5336108fb9ad3e10e25c8731c2ee000080d788ed2321035f386354840f31343e20bdef33abe4af14501da44c903a469feec986f3f3be0c52aeffffffff01983a0000000000001976a9144afc1f6d85872b24935a36a3d016bcb172c03d0e88ac00000000
Blockexplorer Links
-------------------
Address: https://testnet.blockexplorer.com/address/2NGBurkWpP6BrCm3D7jPHmQfjtd21WFuF59
Deposit: https://testnet.blockexplorer.com/tx/a44d7c407249991e60c7c422abb458ffe8fcd57f1d016af0a0c9553a3912d95e
Payment: https://testnet.blockexplorer.com/tx/f0a89ac9e790bef6977a8333857ac60af5658b13aca077ce99f4361f1703784b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment