Skip to content

Instantly share code, notes, and snippets.

@zilveer
Forked from badmofo/partial_msig_verify.py
Created August 4, 2017 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zilveer/3d35d2b92239b68f09f3756969315202 to your computer and use it in GitHub Desktop.
Save zilveer/3d35d2b92239b68f09f3756969315202 to your computer and use it in GitHub Desktop.
p2sh multisig verification test script
from pybitcointools import * # available via pip or https://github.com/vbuterin/pybitcointools
from json import dumps
# the tx you sent us
tx_hex = '0100000001f78c40e4a68a6efaea83dcc954550b0dc08b91c372d749cc0ba17934a6d618d801000000b40047304402206d805c798a0b0ae348332284435f4139aeed4935eb4c1322e8bce7d0d3e57efa0220303e13fa08326fa38143471a121cfb4bece404e1565340888587fb8f69914348814c69522103d6ab84e05bd2a9b36e09f08c43ff46358c0ef9f7bdd52ce6f6c1571de760ef852103c945afbe43b21fc864d933f498f362b682515753e9f89f71252ecb6088cc7a332103c14ed38495f194749c728ff3535f1e0286501d44735de87047ed5ef696ad1f4153aeffffffff0250c30000000000001976a914642421cbce0ac31e039ddead2207dfedd04443e488ac30750000000000001976a914c0f3ddcb9c495085252cccbd1b3e2e42d8907d0f88ac00000000'
# example: a 2 of 2 with a valid sig
#tx_hex = '01000000016dbce51ca14b5027bc714baed5bfd9218f30a83e8ef5df52cf8d530bd4627cce01000000910047304402200358faad7809a3571c36a4b18c5c47d9b28a42439d4e149725d8799e7993bf6d022073d2702eded20c9441d770b9a14a2d2b351e962f06c9a260c8fa87622af2ce220147522102ddb50c51cf58d71302ffd6743ec8beb3c4221ef8b166cfaf57fffe21e8ca784b2103cf6c974c0e2fa0f92a9edcbd4fe415107532183a91759954691c33e1e3e8b5c152aeffffffff0340420f00000000001976a9141fef0f5b25c084512f38ce8349bded85b30f2be488aca08601000000000017a91495cb641aad2243a418cb022f839cb01cd518bb6c8750c30000000000001976a9140ed9d7b0d291f8fa9380b90e1cee591de12e03f188ac00000000'
# example: a 2 of 3 with a valid sig (with a OP_0 sig placeholder; fyi sending a sig placeholder is optional w/ our api)
#tx_hex = '010000000149165ccce07793d366c16ead312e47b313e763a5de7c0b0fd91a234eabecbbf900000000b600483045022069ee04ca41a238730969171fe3c26e0ceb9ef4ad94cc1564807927f5b8bee994022100e16dd543bbc9cf93d12446b93402c0f0367a18782668531c43516971e53ceff401004c695221025cbde3fa061cb2123f553aa5cacc5bf929934da53e1a6ecad24c320b885069552103bd8b2ac8735be0e9d5de15b0b0e08357cd99617802de1c1da94e1cefdc1ce53721036843e3364ecbc938f078fa7338c10c721018359f759fd68599880d94e0a8ea4453aeffffffff0330750000000000001976a914c9aee929056395e63c897360e0cfbd5f78bbf3a188ac50c30000000000001976a91461257c5574b4ed8d1f819da94ef82c4ae31657e088ac102700000000000017a914a1db139cb7746437753104973f2a91e092297b8b8700000000'
tx = deserialize(tx_hex)
print 'tx:', dumps(tx)
input_i = 0
print 'validating input', input_i
script_hex = tx['ins'][input_i]['script']
script = deserialize_script(script_hex)
print 'script hex', script_hex
print 'script asm', script
sigs = [s for s in script[1:-1] if s] # first item is 0; subsequent are sigs; filter out empty placeholder sigs
print 'found sigs:', sigs
p2sh_script_hex = script[-1] # last item is the p2sh embedded script
print 'found ps2h hex', p2sh_script_hex
p2sh_script = deserialize_script(p2sh_script_hex)
print 'found ps2h asm', p2sh_script_hex
pubkeys = p2sh_script[1:-2] # first item is n, -2 is m, -1 is multisig op; we get everything else (the pubkeys)
print 'found multisig pubkeys:', pubkeys
print
print 'checking all sigs against all pubkeys'
for pubkey in pubkeys:
for sig in sigs:
print 'verifying', sig, 'against', pubkey, '=',
sig_ok = verify_tx_input(tx_hex, input_i, p2sh_script_hex, sig, pubkey)
print sig_ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment