def tx_from_json_dict(r): version = r.get("version") lock_time = r.get("locktime") txs_in = [] for vin in r.get("vin"): if "coinbase" in vin: previous_hash = b'\0' * 32 script = h2b(vin.get("coinbase")) previous_index = 4294967295 else: previous_hash = h2b_rev(vin.get("txid")) scriptSig = vin.get("scriptSig") if "hex" in scriptSig: script = h2b(scriptSig.get("hex")) else: script = tools.compile(scriptSig.get("asm")) previous_index = vin.get("vout") sequence = vin.get("sequence") txs_in.append(TxIn(previous_hash, previous_index, script, sequence)) txs_out = [] for vout in r.get("vout"): coin_value = btc_to_satoshi(decimal.Decimal(vout.get("value"))) script = tools.compile(vout.get("scriptPubKey").get("asm")) txs_out.append(TxOut(coin_value, script)) tx = Tx(version, txs_in, txs_out, lock_time) bh = r.get("blockhash") if bh: bh = h2b_rev(bh) tx.confirmation_block_hash = bh return tx
def make_script_test(script_in, script_out, flags_string, comment, expected, coin_value, script_witness): script_in_bin = compile(script_in) script_out_bin = compile(script_out) script_witness_bin = [h2b(w) for w in script_witness] flags = parse_flags(flags_string) def f(self): try: credit_tx = build_credit_tx(script_out_bin, coin_value) spend_tx = build_spending_tx(script_in_bin, credit_tx) spend_tx.txs_in[0].witness = script_witness_bin msg = '' check_solution(spend_tx, tx_in_idx=0, flags=flags) r = 0 except ScriptError as se: r = se.error_code() msg = se.args[0] except: r = -1 # for now, just deal with 0 versus nonzero expect_error = getattr(errno, expected) if r != expect_error: dump_failure_info(spend_tx, script_in, script_out, flags, flags_string, expected, r, msg, comment) self.assertEqual(r, expect_error) return f
def dump_failure_info(spend_tx, script_in, script_out, flags, flags_string, expected, actual, message, comment): return print() print(flags_string) print("EXPECTED: %s" % expected) print("ACTUAL: %s" % actual) print("MESSAGE: %s" % message) print(comment) print(disassemble(compile(script_in))) print(disassemble(compile(script_out))) def tbf(*args): pc, opcode, data, stack, altstack, is_signature, is_condition = args from pycoin_grs.tx.script.tools import disassemble_for_opcode_data opd = disassemble_for_opcode_data(opcode, data) if len(altstack) == 0: altstack = '' print("%s %s\n %3x %s" % (stack, altstack, pc, opd)) import pdb pdb.set_trace() print("test failed: '%s' '%s' : %s %s" % (script_in, script_out, comment, flags_string)) try: check_solution(spend_tx, tx_in_idx=0, traceback_f=tbf, flags=flags) except Exception as ex: print(ex) try: check_solution(spend_tx, tx_in_idx=0, traceback_f=tbf, flags=flags) except Exception as ex: print(ex) import pdb pdb.set_trace()
def test_issue_225(self): script = tools.compile("OP_RETURN 'foobar'") tx_out = TxOut(1, script) address = tx_out.bitcoin_address(netcode="XTN") self.assertEqual(address, "(nulldata 666f6f626172)") address = tx_out.bitcoin_address(netcode="BTC") self.assertEqual(address, "(nulldata 666f6f626172)")
def test_nulldata(self): OP_RETURN = tools.compile("OP_RETURN") # note that because chr() is used samples with length > 255 will not work for sample in [ b'test', b'me', b'a', b'39qEwuwyb2cAX38MFtrNzvq3KV9hSNov3q', b'', b'0' * 80 ]: sample_script = OP_RETURN + tools.bin_script([sample]) nd = ScriptNulldata(sample) self.assertEqual(nd.nulldata, sample) self.assertEqual(nd.script(), sample_script) nd2 = ScriptNulldata.from_script(sample_script) self.assertEqual(nd.nulldata, nd2.nulldata) out = TxOut(1, nd.script()) # ensure we can create a tx Tx(0, [], [out]) # convert between asm and back to ensure no bugs with compilation self.assertEqual(nd.script(), tools.compile(tools.disassemble(nd.script())))
def txs_from_json(path): """ Read tests from ./data/tx_??valid.json Format is an array of arrays Inner arrays are either [ "comment" ] or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...], serializedTransaction, verifyFlags] ... where all scripts are stringified scripts. verifyFlags is a comma separated list of script verification flags to apply, or "NONE" """ comments = None with open(path, 'r') as f: for tvec in json.load(f): if len(tvec) == 1: comments = tvec[0] continue assert len(tvec) == 3 prevouts = tvec[0] for prevout in prevouts: assert len(prevout) in (3, 4) tx_hex = tvec[1] flag_mask = parse_flags(tvec[2]) try: tx = Tx.from_hex(tx_hex) except: print("Cannot parse tx_hex: %s" % tx_hex) raise spendable_db = {} blank_spendable = Spendable(0, b'', b'\0' * 32, 0) for prevout in prevouts: coin_value = 1000000 if len(prevout) == 4: coin_value = prevout[3] spendable = Spendable(coin_value=coin_value, script=compile(prevout[2]), tx_hash=h2b_rev(prevout[0]), tx_out_index=prevout[1]) spendable_db[(spendable.tx_hash, spendable.tx_out_index)] = spendable unspents = [ spendable_db.get((tx_in.previous_hash, tx_in.previous_index), blank_spendable) for tx_in in tx.txs_in ] tx.set_unspents(unspents) yield (tx, flag_mask, comments)
def check(s): b1 = compile(s) s1 = disassemble(b1) b2 = compile(s1) self.assertEqual(s, s1) self.assertEqual(b1, b2)
def test_bip143_txs(self): # these five examples are from BIP 143 at # https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki tx_u1, tx_s1 = self.check_bip143_tx( "0100000002fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad" "969f0000000000eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9" "b2b55d57b90ec68a0100000000ffffffff02202cb206000000001976a9148280b37df3" "78db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143bde42dbee7e" "4dbe6a21b2d50ce2f0167faa815988ac11000000", "01000000000102fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4" "e4ad969f00000000494830450221008b9d1dc26ba6a9cb62127b02742fa9d754cd3beb" "f337f7a55d114c8e5cdd30be022040529b194ba3f9281a99f2b1c0a19c0489bc22ede9" "44ccf4ecbab4cc618ef3ed01eeffffffef51e1b804cc89d182d279655c3aa89e815b1b" "309fe287d9b2b55d57b90ec68a0100000000ffffffff02202cb206000000001976a914" "8280b37df378db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143b" "de42dbee7e4dbe6a21b2d50ce2f0167faa815988ac000247304402203609e17b84f6a7" "d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c45183315" "61406f90300e8f3358f51928d43c212a8caed02de67eebee0121025476c2e83188368d" "a1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee635711000000", [(6.25, "2103c9f4836b9a4f77fc0d81f7bcb01b7f1b35916864b9476c241ce9fc198bd25432ac" ), (6, "00141d0f172a0ecb48aee1be1f2687d2963ae33f71a1")], 2, 2, 1, 17) self.assertEqual( b2h(tx_s1.hash_prevouts(SIGHASH_ALL)), "96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37") self.assertEqual( b2h(tx_s1.hash_sequence(SIGHASH_ALL)), "52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b") self.assertEqual( b2h(tx_s1.hash_outputs(SIGHASH_ALL, 0)), "863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5") script = tools.compile( "OP_DUP OP_HASH160 %s OP_EQUALVERIFY OP_CHECKSIG" % b2h(tx_s1.unspents[1].script[2:])) self.assertEqual( b2h( tx_s1.segwit_signature_preimage(script=script, tx_in_idx=1, hash_type=SIGHASH_ALL)), "0100000096b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd" "3752b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3bef51" "e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a0100000019" "76a9141d0f172a0ecb48aee1be1f2687d2963ae33f71a188ac0046c32300000000ffff" "ffff863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e511" "00000001000000") self.assertEqual( b2h(to_bytes_32(tx_s1.signature_for_hash_type_segwit(script, 1, 1))), "c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670") self.check_tx_can_be_signed(tx_u1, tx_s1, [ 0xbbc27228ddcb9209d7fd6f36b02f7dfa6252af40bb2f1cbc7a557da8027ff866, 0x619c335025c7f4012e556c2a58b2506e30b8511b53ade95ea316fd8c3286feb9 ]) tx_u2, tx_s2 = self.check_bip143_tx( "0100000001db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a" "54770100000000feffffff02b8b4eb0b000000001976a914a457b684d7f0d539a46a45" "bbc043f35b59d0d96388ac0008af2f000000001976a914fd270b1ee6abcaea97fea7ad" "0402e8bd8ad6d77c88ac92040000", "01000000000101db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3c" "eb1a5477010000001716001479091972186c449eb1ded22b78e40d009bdf0089feffff" "ff02b8b4eb0b000000001976a914a457b684d7f0d539a46a45bbc043f35b59d0d96388" "ac0008af2f000000001976a914fd270b1ee6abcaea97fea7ad0402e8bd8ad6d77c88ac" "02473044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794d1" "2d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe" "9dcb012103ad1d8e89212f0b92c74d23bb710c00662ad1470198ac48c43f7d6f93a2a2" "687392040000", [(10, "a9144733f37cf4db86fbc2efed2500b4f4e49f31202387")], 1, 2, 1, 1170) self.check_tx_can_be_signed(tx_u2, tx_s2, [ 0xeb696a065ef48a2192da5b28b694f87544b30fae8327c4510137a922f32c6dcf ], ["001479091972186c449eb1ded22b78e40d009bdf0089"]) tx_u3, tx_s3 = self.check_bip143_tx( "0100000002fe3dc9208094f3ffd12645477b3dc56f60ec4fa8e6f5d67c565d1c6b9216" "b36e0000000000ffffffff0815cf020f013ed6cf91d29f4202e8a58726b1ac6c79da47" "c23d1bee0a6925f80000000000ffffffff0100f2052a010000001976a914a30741f814" "5e5acadf23f751864167f32e0963f788ac00000000", "01000000000102fe3dc9208094f3ffd12645477b3dc56f60ec4fa8e6f5d67c565d1c6b" "9216b36e000000004847304402200af4e47c9b9629dbecc21f73af989bdaa911f7e6f6" "c2e9394588a3aa68f81e9902204f3fcf6ade7e5abb1295b6774c8e0abd94ae62217367" "096bc02ee5e435b67da201ffffffff0815cf020f013ed6cf91d29f4202e8a58726b1ac" "6c79da47c23d1bee0a6925f80000000000ffffffff0100f2052a010000001976a914a3" "0741f8145e5acadf23f751864167f32e0963f788ac000347304402200de66acf452778" "9bfda55fc5459e214fa6083f936b430a762c629656216805ac0220396f550692cd3471" "71cbc1ef1f51e15282e837bb2b30860dc77c8f78bc8501e503473044022027dc95ad6b" "740fe5129e7e62a75dd00f291a2aeb1200b84b09d9e3789406b6c002201a9ecd315dd6" "a0e632ab20bbb98948bc0c6fb204f2c286963bb48517a7058e27034721026dccc749ad" "c2a9d0d89497ac511f760f45c47dc5ed9cf352a58ac706453880aeadab210255a9626a" "ebf5e29c0e6538428ba0d1dcf6ca98ffdf086aa8ced5e0d0215ea465ac00000000", [(1.5625, "21036d5c20fa14fb2f635474c1dc4ef5909d4568e5569b79fc94d3448486e14685f8ac" ), (49, "00205d1b56b63d714eebe542309525f484b7e9d6f686b3781b6f61ef925d66d6f6a0" )], 2, 1, 1, 0) tx_u4, tx_s4 = self.check_bip143_tx( "0100000002e9b542c5176808107ff1df906f46bb1f2583b16112b95ee5380665ba7fcf" "c0010000000000ffffffff80e68831516392fcd100d186b3c2c7b95c80b53c77e77c35" "ba03a66b429a2a1b0000000000ffffffff0280969800000000001976a914de4b231626" "ef508c9a74a8517e6783c0546d6b2888ac80969800000000001976a9146648a8cd4531" "e1ec47f35916de8e259237294d1e88ac00000000", "01000000000102e9b542c5176808107ff1df906f46bb1f2583b16112b95ee5380665ba" "7fcfc0010000000000ffffffff80e68831516392fcd100d186b3c2c7b95c80b53c77e7" "7c35ba03a66b429a2a1b0000000000ffffffff0280969800000000001976a914de4b23" "1626ef508c9a74a8517e6783c0546d6b2888ac80969800000000001976a9146648a8cd" "4531e1ec47f35916de8e259237294d1e88ac02483045022100f6a10b8604e6dc910194" "b79ccfc93e1bc0ec7c03453caaa8987f7d6c3413566002206216229ede9b4d6ec2d325" "be245c5b508ff0339bf1794078e20bfe0babc7ffe683270063ab68210392972e2eb617" "b2388771abe27235fd5ac44af8e61693261550447a4c3e39da98ac0247304402200325" "21802a76ad7bf74d0e2c218b72cf0cbc867066e2e53db905ba37f130397e02207709e2" "188ed7f08f4c952d9d13986da504502b8c3be59617e043552f506c46ff83275163ab68" "210392972e2eb617b2388771abe27235fd5ac44af8e61693261550447a4c3e39da98ac" "00000000", [ (0.16777215, "0020ba468eea561b26301e4cf69fa34bde4ad60c81e70f059f045ca9a79931004a4d" ), (0.16777215, "0020d9bbfbe56af7c4b7f960a70d7ea107156913d9e5a26b0a71429df5e097ca6537" ), ], 2, 2, 1, 0) tx_u5, tx_s5 = self.check_bip143_tx( "010000000136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787" "b96e0100000000ffffffff0200e9a435000000001976a914389ffce9cd9ae88dcc0631" "e88a821ffdbe9bfe2688acc0832f05000000001976a9147480a33f950689af511e6e84" "c138dbbd3c3ee41588ac00000000", "0100000000010136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca2" "9787b96e0100000023220020a16b5755f7f6f96dbd65f5f0d6ab9418b89af4b1f14a1b" "b8a09062c35f0dcb54ffffffff0200e9a435000000001976a914389ffce9cd9ae88dcc" "0631e88a821ffdbe9bfe2688acc0832f05000000001976a9147480a33f950689af511e" "6e84c138dbbd3c3ee41588ac080047304402206ac44d672dac41f9b00e28f4df20c52e" "eb087207e8d758d76d92c6fab3b73e2b0220367750dbbe19290069cba53d096f44530e" "4f98acaa594810388cf7409a1870ce01473044022068c7946a43232757cbdf9176f009" "a928e1cd9a1a8c212f15c1e11ac9f2925d9002205b75f937ff2f9f3c1246e547e54f62" "e027f64eefa2695578cc6432cdabce271502473044022059ebf56d98010a932cf8ecfe" "c54c48e6139ed6adb0728c09cbe1e4fa0915302e022007cd986c8fa870ff5d2b3a8913" "9c9fe7e499259875357e20fcbb15571c76795403483045022100fbefd94bd0a488d50b" "79102b5dad4ab6ced30c4069f1eaa69a4b5a763414067e02203156c6a5c9cf88f91265" "f5a942e96213afae16d83321c8b31bb342142a14d16381483045022100a5263ea0553b" "a89221984bd7f0b13613db16e7a70c549a86de0cc0444141a407022005c360ef0ae5a5" "d4f9f2f87a56c1546cc8268cab08c73501d6b3be2e1e1a8a08824730440220525406a1" "482936d5a21888260dc165497a90a15669636d8edca6b9fe490d309c022032af0c646a" "34a44d1f4576bf6a4a74b67940f8faa84c7df9abe12a01a11e2b4783cf56210307b8ae" "49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28" "bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703" "413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb8" "33092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94b" "a04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2" "f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56ae00000000", [(9.87654321, "a9149993a429037b5d912407a71c252019287b8d27a587")], 1, 2, 1, 0) tx_u5prime = self.unsigned_copy(tx_s5) tx_s_hex = tx_s5.as_hex() tx_u5prime.set_unspents(tx_s5.unspents) ss = [ "56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba3" "2103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21" "034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a2103" "3400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6" "d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b6" "61b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56ae", "0020a16b5755f7f6f96dbd65f5f0d6ab9418b89af4b1f14a1bb8a09062c35f0dcb54" ] p2sh_lookup = build_p2sh_lookup([h2b(x) for x in ss]) for se, sighash_type in [ (0x730fff80e1413068a05b57d6a58261f07551163369787f349438ea38ca80fac6, SIGHASH_ALL), (0x11fa3d25a17cbc22b29c44a484ba552b5a53149d106d3d853e22fdd05a2d8bb3, SIGHASH_NONE), (0x77bf4141a87d55bdd7f3cd0bdccf6e9e642935fec45f2f30047be7b799120661, SIGHASH_SINGLE), (0x14af36970f5025ea3e8b5542c0f8ebe7763e674838d08808896b63c3351ffe49, SIGHASH_ANYONECANPAY | SIGHASH_ALL), (0xfe9a95c19eef81dde2b95c1284ef39be497d128e2aa46916fb02d552485e0323, SIGHASH_ANYONECANPAY | SIGHASH_NONE), (0x428a7aee9f0c2af0cd19af3cf1c78149951ea528726989b2e83e4778d2c3f890, SIGHASH_ANYONECANPAY | SIGHASH_SINGLE), ]: tx_u5prime.sign(hash_type=sighash_type, hash160_lookup=build_hash160_lookup([se]), p2sh_lookup=p2sh_lookup) self.check_signed(tx_u5prime) tx_hex = tx_u5prime.as_hex() self.assertEqual(tx_hex, tx_s_hex) self.assertEqual( b2h(tx_s5.hash_prevouts(SIGHASH_ALL)), "74afdc312af5183c4198a40ca3c1a275b485496dd3929bca388c4b5e31f7aaa0") self.assertEqual( b2h(tx_s5.hash_sequence(SIGHASH_ALL)), "3bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e70665044") self.assertEqual( b2h(tx_s5.hash_outputs(SIGHASH_ALL, 0)), "bc4d309071414bed932f98832b27b4d76dad7e6c1346f487a8fdbb8eb90307cc") self.assertEqual( b2h(tx_s5.hash_outputs(SIGHASH_SINGLE, 0)), "9efe0c13a6b16c14a41b04ebe6a63f419bdacb2f8705b494a43063ca3cd4f708") script = tx_s5.txs_in[0].witness[-1] self.assertEqual( b2h( tx_s5.segwit_signature_preimage(script=script, tx_in_idx=0, hash_type=SIGHASH_ALL)), "0100000074afdc312af5183c4198a40ca3c1a275b485496dd3929bca388c4b5e31f7aa" "a03bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e706650443664" "1869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e01000000cf" "56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba3" "2103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21" "034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a2103" "3400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6" "d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b6" "61b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56aeb168de" "3a00000000ffffffffbc4d309071414bed932f98832b27b4d76dad7e6c1346f487a8fd" "bb8eb90307cc0000000001000000") self.assertEqual( b2h( tx_s5.segwit_signature_preimage(script=script, tx_in_idx=0, hash_type=SIGHASH_NONE)), "0100000074afdc312af5183c4198a40ca3c1a275b485496dd3929bca388c4b5e31f7aa" "a000000000000000000000000000000000000000000000000000000000000000003664" "1869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e01000000cf" "56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba3" "2103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21" "034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a2103" "3400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6" "d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b6" "61b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56aeb168de" "3a00000000ffffffff0000000000000000000000000000000000000000000000000000" "0000000000000000000002000000") self.assertEqual( b2h( tx_s5.segwit_signature_preimage(script=script, tx_in_idx=0, hash_type=SIGHASH_SINGLE)), "0100000074afdc312af5183c4198a40ca3c1a275b485496dd3929bca388c4b5e31f7aa" "a000000000000000000000000000000000000000000000000000000000000000003664" "1869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e01000000cf" "56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba3" "2103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21" "034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a2103" "3400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6" "d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b6" "61b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56aeb168de" "3a00000000ffffffff9efe0c13a6b16c14a41b04ebe6a63f419bdacb2f8705b494a430" "63ca3cd4f7080000000003000000") self.assertEqual( b2h( tx_s5.segwit_signature_preimage(script=script, tx_in_idx=0, hash_type=SIGHASH_ALL | SIGHASH_ANYONECANPAY)), "0100000000000000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000000003664" "1869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e01000000cf" "56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba3" "2103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21" "034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a2103" "3400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6" "d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b6" "61b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56aeb168de" "3a00000000ffffffffbc4d309071414bed932f98832b27b4d76dad7e6c1346f487a8fd" "bb8eb90307cc0000000081000000") self.assertEqual( b2h( tx_s5.segwit_signature_preimage(script=script, tx_in_idx=0, hash_type=SIGHASH_NONE | SIGHASH_ANYONECANPAY)), "0100000000000000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000000003664" "1869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e01000000cf" "56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba3" "2103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21" "034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a2103" "3400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6" "d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b6" "61b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56aeb168de" "3a00000000ffffffff0000000000000000000000000000000000000000000000000000" "0000000000000000000082000000") self.assertEqual( b2h( tx_s5.segwit_signature_preimage(script=script, tx_in_idx=0, hash_type=SIGHASH_SINGLE | SIGHASH_ANYONECANPAY)), "0100000000000000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000000003664" "1869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e01000000cf" "56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba3" "2103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21" "034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a2103" "3400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6" "d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b6" "61b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56aeb168de" "3a00000000ffffffff9efe0c13a6b16c14a41b04ebe6a63f419bdacb2f8705b494a430" "63ca3cd4f7080000000083000000") tx = Tx.from_hex( "010000000169c12106097dc2e0526493ef67f21269fe888ef05c7a3a5dacab38e1ac83" "87f14c1d000000ffffffff0101000000000000000000000000") tx.set_witness(0, [ h2b(x) for x in [ "30450220487fb382c4974de3f7d834c1b617fe15860828c7f96454490edd6d891556dc" "c9022100baf95feb48f845d5bfc9882eb6aeefa1bc3790e39f59eaa46ff7f15ae626c5" "3e01", "02a9781d66b61fb5a7ef00ac5ad5bc6ffc78be7b44a566e3c87870e1079368df4c", "ad4830450220487fb382c4974de3f7d834c1b617fe15860828c7f96454490edd6d8915" "56dcc9022100baf95feb48f845d5bfc9882eb6aeefa1bc3790e39f59eaa46ff7f15ae6" "26c53e01" ] ]) tx = Tx.from_hex( "0100000000010169c12106097dc2e0526493ef67f21269fe888ef05c7a3a5dacab38e1" "ac8387f14c1d000000ffffffff01010000000000000000034830450220487fb382c497" "4de3f7d834c1b617fe15860828c7f96454490edd6d891556dcc9022100baf95feb48f8" "45d5bfc9882eb6aeefa1bc3790e39f59eaa46ff7f15ae626c53e012102a9781d66b61f" "b5a7ef00ac5ad5bc6ffc78be7b44a566e3c87870e1079368df4c4aad4830450220487f" "b382c4974de3f7d834c1b617fe15860828c7f96454490edd6d891556dcc9022100baf9" "5feb48f845d5bfc9882eb6aeefa1bc3790e39f59eaa46ff7f15ae626c53e0100000000" ) tx_hex = tx.as_hex() print(tx) print(tx_hex) tx = Tx.from_hex( "010000000169c12106097dc2e0526493ef67f21269fe888ef05c7a3a5dacab38e1ac83" "87f14c1d000000ffffffff0101000000000000000000000000") self.assertEqual( tx_hex, "0100000000010169c12106097dc2e0526493ef67f21269fe888ef05c7a3a5dacab38e1" "ac8387f14c1d000000ffffffff01010000000000000000034830450220487fb382c497" "4de3f7d834c1b617fe15860828c7f96454490edd6d891556dcc9022100baf95feb48f8" "45d5bfc9882eb6aeefa1bc3790e39f59eaa46ff7f15ae626c53e012102a9781d66b61f" "b5a7ef00ac5ad5bc6ffc78be7b44a566e3c87870e1079368df4c4aad4830450220487f" "b382c4974de3f7d834c1b617fe15860828c7f96454490edd6d891556dcc9022100baf9" "5feb48f845d5bfc9882eb6aeefa1bc3790e39f59eaa46ff7f15ae626c53e0100000000" ) tx_u6, tx_s6 = self.check_bip143_tx( "010000000169c12106097dc2e0526493ef67f21269fe888ef05c7a3a5dacab38e1ac83" "87f14c1d000000ffffffff0101000000000000000000000000", "0100000000010169c12106097dc2e0526493ef67f21269fe888ef05c7a3a5dacab38e1" "ac8387f14c1d000000ffffffff01010000000000000000034830450220487fb382c497" "4de3f7d834c1b617fe15860828c7f96454490edd6d891556dcc9022100baf95feb48f8" "45d5bfc9882eb6aeefa1bc3790e39f59eaa46ff7f15ae626c53e012102a9781d66b61f" "b5a7ef00ac5ad5bc6ffc78be7b44a566e3c87870e1079368df4c4aad4830450220487f" "b382c4974de3f7d834c1b617fe15860828c7f96454490edd6d891556dcc9022100baf9" "5feb48f845d5bfc9882eb6aeefa1bc3790e39f59eaa46ff7f15ae626c53e0100000000", [(0.002, "00209e1be07558ea5cc8e02ed1d80c0911048afad949affa36d5c3951e3159dbea19" )], 1, 1, 1, 0) tx_u7, tx_s7 = self.check_bip143_tx( "01000000019275cb8d4a485ce95741c013f7c0d28722160008021bb469a11982d47a66" "28964c1d000000ffffffff0101000000000000000000000000", "010000000001019275cb8d4a485ce95741c013f7c0d28722160008021bb469a11982d4" "7a6628964c1d000000ffffffff0101000000000000000007004830450220487fb382c4" "974de3f7d834c1b617fe15860828c7f96454490edd6d891556dcc9022100baf95feb48" "f845d5bfc9882eb6aeefa1bc3790e39f59eaa46ff7f15ae626c53e0148304502205286" "f726690b2e9b0207f0345711e63fa7012045b9eb0f19c2458ce1db90cf43022100e89f" "17f86abc5b149eba4115d4f128bcf45d77fb3ecdd34f594091340c0395960101022102" "966f109c54e85d3aee8321301136cedeb9fc710fdef58a9de8a73942f8e567c021034f" "fc99dd9a79dd3cb31e2ab3e0b09e0e67db41ac068c625cd1f491576016c84e9552af48" "30450220487fb382c4974de3f7d834c1b617fe15860828c7f96454490edd6d891556dc" "c9022100baf95feb48f845d5bfc9882eb6aeefa1bc3790e39f59eaa46ff7f15ae626c5" "3e0148304502205286f726690b2e9b0207f0345711e63fa7012045b9eb0f19c2458ce1" "db90cf43022100e89f17f86abc5b149eba4115d4f128bcf45d77fb3ecdd34f59409134" "0c039596017500000000", [(0.002, "00209b66c15b4e0b4eb49fa877982cafded24859fe5b0e2dbfbe4f0df1de7743fd52" )], 1, 1, 1, 0) print(tx_s7.txs_in[0])
def test_validate(self): # block 80971 block_80971_cs = '00000000001126456C67A1F5F0FF0268F53B4F22E0531DC70C7B69746AF69DAC'.lower( ) block_80971_data = h2b( "01000000950A1631FB9FAC411DFB173487B9E18018B7C6F7147E78C062584100000000" "00A881352F97F14BF191B54915AE124E051B8FE6C3922C5082B34EAD503000FC34D891" "974CED66471B4016850A04010000000100000000000000000000000000000000000000" "00000000000000000000000000FFFFFFFF0804ED66471B02C301FFFFFFFF0100F2052A" "01000000434104CB6B6B4EADC96C7D08B21B29D0ADA5F29F9378978CABDB602B8B65DA" "08C8A93CAAB46F5ABD59889BAC704925942DD77A2116D10E0274CAD944C71D3D1A6705" "70AC0000000001000000018C55ED829F16A4E43902940D3D33005264606D5F7D555B5F" "67EE4C033390C2EB010000008A47304402202D1BF606648EDCDB124C1254930852D991" "88E1231715031CBEAEA80CCFD2B39A02201FA9D6EE7A1763580E342474FC1AEF59B046" "8F98479953437F525063E25675DE014104A01F763CFBF5E518C628939158AF3DC0CAAC" "35C4BA7BC1CE8B7E634E8CDC44E15F0296B250282BD649BAA8398D199F2424FCDCD88D" "3A9ED186E4FD3CB9BF57CFFFFFFFFF02404B4C00000000001976A9148156FF75BEF24B" "35ACCE3C05289A2411E1B0E57988AC00AA38DF010000001976A914BC7E692A5FFE95A5" "96712F5ED83393B3002E452E88AC0000000001000000019C97AFDF6C9A31FFA86D71EA" "79A079001E2B59EE408FD418498219400639AC0A010000008B4830450220363CFFAE09" "599397B21E6D8A8073FB1DFBE06B6ACDD0F2F7D3FEA86CA9C3F605022100FA255A6ED2" "3FD825C759EF1A885A31CAD0989606CA8A3A16657D50FE3CEF5828014104FF444BAC08" "308B9EC97F56A652AD8866E0BA804DA97868909999566CB377F4A2C8F1000E83B49686" "8F3A282E1A34DF78565B65C15C3FA21A0763FD81A3DFBBB6FFFFFFFF02C05EECDE0100" "00001976A914588554E6CC64E7343D77117DA7E01357A6111B7988AC404B4C00000000" "001976A914CA6EB218592F289999F13916EE32829AD587DBC588AC0000000001000000" "01BEF5C9225CB9FE3DEF929423FA36AAD9980B9D6F8F3070001ACF3A5FB389A69F0000" "00004A493046022100FB23B1E2F2FB8B96E04D220D385346290A9349F89BBBC5C225D5" "A56D931F8A8E022100F298EB28294B90C1BAF319DAB713E7CA721AAADD8FCC15F849DE" "7B0A6CF5412101FFFFFFFF0100F2052A010000001976A9146DDEA8071439951115469D" "0D2E2B80ECBCDD48DB88AC00000000") # block 80974 block_80974_cs = '0000000000089F7910F6755C10EA2795EC368A29B435D80770AD78493A6FECF1'.lower( ) block_80974_data = h2b( "010000007480150B299A16BBCE5CCDB1D1BBC65CFC5893B01E6619107C552000000000" "007900A2B203D24C69710AB6A94BEB937E1B1ADD64C2327E268D8C3E5F8B41DBED8796" "974CED66471B204C324703010000000100000000000000000000000000000000000000" "00000000000000000000000000FFFFFFFF0804ED66471B024001FFFFFFFF0100F2052A" "010000004341045FEE68BAB9915C4EDCA4C680420ED28BBC369ED84D48AC178E1F5F7E" "EAC455BBE270DABA06802145854B5E29F0A7F816E2DF906E0FE4F6D5B4C9B92940E4F0" "EDAC000000000100000001F7B30415D1A7BF6DB91CB2A272767C6799D721A4178AA328" "E0D77C199CB3B57F010000008A4730440220556F61B84F16E637836D2E74B8CB784DE4" "0C28FE3EF93CCB7406504EE9C7CAA5022043BD4749D4F3F7F831AC696748AD8D8E79AE" "B4A1C539E742AA3256910FC88E170141049A414D94345712893A828DE57B4C2054E2F5" "96CDCA9D0B4451BA1CA5F8847830B9BE6E196450E6ABB21C540EA31BE310271AA00A49" "ED0BA930743D1ED465BAD0FFFFFFFF0200E1F505000000001976A914529A63393D63E9" "80ACE6FA885C5A89E4F27AA08988ACC0ADA41A000000001976A9145D17976537F30886" "5ED533CCCFDD76558CA3C8F088AC00000000010000000165148D894D3922EF5FFDA962" "BE26016635C933D470C8B0AB7618E869E3F70E3C000000008B48304502207F5779EBF4" "834FEAEFF4D250898324EB5C0833B16D7AF4C1CB0F66F50FCF6E85022100B78A65377F" "D018281E77285EFC31E5B9BA7CB7E20E015CF6B7FA3E4A466DD195014104072AD79E0A" "A38C05FA33DD185F84C17F611E58A8658CE996D8B04395B99C7BE36529CAB7606900A0" "CD5A7AEBC6B233EA8E0FE60943054C63620E05E5B85F0426FFFFFFFF02404B4C000000" "00001976A914D4CAA8447532CA8EE4C80A1AE1D230A01E22BFDB88AC8013A0DE010000" "001976A9149661A79AE1F6D487AF3420C13E649D6DF3747FC288AC00000000") block_80971 = Block.parse(io.BytesIO(block_80971_data)) self.assertEqual(block_80971.id(), block_80971_cs) block_80974 = Block.parse(io.BytesIO(block_80974_data)) self.assertEqual(block_80974.id(), block_80974_cs) tx_db = {tx.hash(): tx for tx in block_80971.txs} tx_to_validate = block_80974.txs[2] self.assertEqual( "OP_DUP OP_HASH160 [d4caa8447532ca8ee4c80a1ae1d230a01e22bfdb] OP_EQUALVERIFY OP_CHECKSIG", tools.disassemble(tx_to_validate.txs_out[0].script)) self.assertEqual( tx_to_validate.id(), "7c4f5385050c18aa8df2ba50da566bbab68635999cc99b75124863da1594195b") tx_to_validate.unspents_from_db(tx_db) self.assertEqual(tx_to_validate.bad_signature_count(), 0) # now, let's corrupt the Tx and see what happens tx_out = tx_to_validate.txs_out[1] disassembly = tools.disassemble(tx_out.script) tx_out.script = tools.compile(disassembly) self.assertEqual(tx_to_validate.bad_signature_count(), 0) disassembly = disassembly.replace( "9661a79ae1f6d487af3420c13e649d6df3747fc2", "9661a79ae1f6d487af3420c13e649d6df3747fc3") tx_out.script = tools.compile(disassembly) self.assertEqual(tx_to_validate.bad_signature_count(), 1) self.assertFalse(tx_to_validate.is_signature_ok(0))
def test_validate_two_inputs(self): def tx_from_b64(h): f = io.BytesIO(binascii.a2b_base64(h.encode("utf8"))) return Tx.parse(f) # c9989d984c97128b03b9f118481c631c584f7aa42b578dbea6194148701b053d # This is the one we're going to validate. It has inputs from # tx_1 = b52201c2741d410b70688335afebba0d58f8675fa9b6c8c54becb0d7c0a75983 # and tx_2 = 72151f65db1d8594df90778639a4c0c17c1e303af01de0d04af8fac13854bbfd TX_0_HEX = ( "AQAAAAKDWafA17DsS8XItqlfZ/hYDbrrrzWDaHALQR10wgEitQAAAACLSDBFAiAnyvQ1P7" "b8+84JbBUbE1Xtgrd0KNpD4eyVTNU/burbtgIhAOS8T1TrhXkGXQTGbLSEJy5uvZMGEzOj" "ITxO+DrykiPlAUEE3yJcIB5OCpaDjrop+N3bm8h9PKw8bF/YB4v3yD+VeQf4fXdUZ9hJJS" "nFeJ+QeJrC7q3Y23QSYeYbW/AfA3D5G//////9u1Q4wfr4StDgHfA6MB58wcCkOYZ3kN+U" "hR3bZR8VcgAAAACLSDBFAiAN6ZQr+9HTgmF57EsPyXIhQ6J5M4lgwlj/tJTShZ+toQIhAL" "0U1i9yiCEm75uCEp8uRaySqS7P4x7A+L2Vr5kS+7ANAUEEkSqVI6gw1scM0GuJWgMh4jpW" "KJA0yOl03uQaV/jHURn+HswOIORzvsG9qQY1/9BZgDPaMuI5U5JlyA3WkhLxgf////8Ctk" "SUzxAAAAAZdqkULXTu3lp2t/wMSuvqbifOSj9/kvmIrAAoa+4AAAAAGXapFF3ySpVdjz9V" "8fRKvzDqXQRcmowSiKwAAAAA") TX_1_HEX = ( "AQAAAAEL3YmFDcZpf4SH7uN1IBmMoBd4OhmTp4EAQ8A0ZQ3tiwAAAACKRzBEAiA4Fkl8lk" "JSeLtWHsp1j0h7y0KKFmqxhDR0CK0HnmZWBQIgDSTDenor3zbNqTs+FApeDl8DKCz1xGQC" "JQN0/sp00VABQQQzSNc33wdDXA/F9y9/hAR88q6Se6vRCHEC7dYgbIp1pgxqGzrWXQroGk" "QLhnAbn/fDhUoVbCgM/UHXYmjXlhdO/////wI3HGlfEQAAABl2qRRM+dhUVUjeAlb0jEsH" "JrFClGGSZ4isMAYVCgAAAAAZdqkUgnSLXoYTeOKFFRdtLYxWcGZ2Ht2IrAAAAAA=") TX_2_HEX = ( "AQAAAAFDjBbw61AYUWMx+3moZ2vb9dvLKydOSFIwcfBTjG0QSgEAAACKRzBEAiA5WWKhR4" "8OI60ZDCXnOru/FH6NvuTGhRLggjbpJB2dhgIgKp0FFL0ClSCxxqGjYneDinvgROGSw6Dt" "Vtvflrhaom8BQQR50YjAg1e5qRkP4ER29ec5jKfzk3DHJhS7Si0sEbvNIJMfjjbZfZWtJi" "15wHZhuHh4e3G6SWMdJLHH5pgbseFh/////wLPE5deAAAAABl2qRSmRdbMvv5fEbgFD1Yk" "taBU9zQTW4iswJ7mBQAAAAAZdqkU4E5+Is4tr+8bPU6ELYHSvz/Ng0eIrAAAAAA=") tx_0 = tx_from_b64(TX_0_HEX) self.assertEqual( tx_0.id(), "c9989d984c97128b03b9f118481c631c584f7aa42b578dbea6194148701b053d") tx_1 = tx_from_b64(TX_1_HEX) self.assertEqual( tx_1.id(), "b52201c2741d410b70688335afebba0d58f8675fa9b6c8c54becb0d7c0a75983") tx_2 = tx_from_b64(TX_2_HEX) self.assertEqual( tx_2.id(), "72151f65db1d8594df90778639a4c0c17c1e303af01de0d04af8fac13854bbfd") TX_DB = {tx.hash(): tx for tx in [tx_0, tx_1, tx_2]} tx_to_validate = tx_0 self.assertEqual( "OP_DUP OP_HASH160 [2d74eede5a76b7fc0c4aebea6e27ce4a3f7f92f9] OP_EQUALVERIFY OP_CHECKSIG", tools.disassemble(tx_to_validate.txs_out[0].script)) self.assertEqual( tx_to_validate.id(), "c9989d984c97128b03b9f118481c631c584f7aa42b578dbea6194148701b053d") tx_to_validate.unspents_from_db(TX_DB) self.assertEqual(tx_to_validate.bad_signature_count(), 0) # now let's mess with signatures disassembly = tools.disassemble(tx_to_validate.txs_in[0].script) tx_to_validate.txs_in[0].script = tools.compile(disassembly) self.assertEqual(tx_to_validate.bad_signature_count(), 0) disassembly = disassembly.replace("353fb6fcfbce09", "353fb6fcfbce19") tx_to_validate.txs_in[0].script = tools.compile(disassembly) self.assertEqual(tx_to_validate.bad_signature_count(), 1) self.assertFalse(tx_to_validate.is_signature_ok(0)) tx_to_validate = tx_from_b64(TX_0_HEX) tx_to_validate.unspents_from_db(TX_DB) self.assertEqual(tx_to_validate.bad_signature_count(), 0) disassembly = tools.disassemble(tx_to_validate.txs_in[1].script) disassembly = disassembly.replace("960c258ffb494d2859f", "960d258ffb494d2859f") tx_to_validate.txs_in[1].script = tools.compile(disassembly) self.assertEqual(tx_to_validate.bad_signature_count(), 1) self.assertFalse(tx_to_validate.is_signature_ok(1)) # futz with signature on tx_1 tx_to_validate = tx_from_b64(TX_0_HEX) original_tx_hash = tx_1.hash() disassembly = tools.disassemble(tx_1.txs_out[0].script) disassembly = disassembly.replace( "4cf9d8545548de0256f48c4b0726b14294619267", "4cf9d8545548de1256f48c4b0726b14294619267") tx_1.txs_out[0].script = tools.compile(disassembly) TX_DB[original_tx_hash] = tx_1 tx_to_validate.unspents_from_db(TX_DB, ignore_missing=True) self.assertEqual(tx_to_validate.bad_signature_count(), 1) self.assertFalse(tx_to_validate.is_signature_ok(0, )) # fix it up again TX_DB[original_tx_hash] = tx_from_b64(TX_1_HEX) tx_to_validate.unspents_from_db(TX_DB) self.assertEqual(tx_to_validate.bad_signature_count(), 0) # futz with signature on tx_2 tx_to_validate = tx_from_b64(TX_0_HEX) original_tx_hash = tx_2.hash() disassembly = tools.disassemble(tx_2.txs_out[0].script) disassembly = disassembly.replace( "a645d6ccbefe5f11b8050f5624b5a054f734135b", "a665d6ccbefe5f11b8050f5624b5a054f734135b") tx_2.txs_out[0].script = tools.compile(disassembly) TX_DB[original_tx_hash] = tx_2 tx_to_validate.unspents_from_db(TX_DB, ignore_missing=True) self.assertEqual(tx_to_validate.bad_signature_count(), 1) self.assertFalse(tx_to_validate.is_signature_ok(1)) # fix it up again TX_DB[original_tx_hash] = tx_from_b64(TX_2_HEX) tx_to_validate.unspents_from_db(TX_DB) self.assertEqual(tx_to_validate.bad_signature_count(), 0)