You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decodescript.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2015-2016 The Bitcoin Core developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. """Test decoding scripts via decodescript RPC command."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. from test_framework.mininode import *
  9. from io import BytesIO
  10. class DecodeScriptTest(BitcoinTestFramework):
  11. def __init__(self):
  12. super().__init__()
  13. self.setup_clean_chain = True
  14. self.num_nodes = 1
  15. def setup_network(self, split=False):
  16. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
  17. self.is_network_split = False
  18. def decodescript_script_sig(self):
  19. signature = '304502207fa7a6d1e0ee81132a269ad84e68d695483745cde8b541e3bf630749894e342a022100c1f7ab20e13e22fb95281a870f3dcf38d782e53023ee313d741ad0cfbc0c509001'
  20. push_signature = '48' + signature
  21. public_key = '03b0da749730dc9b4b1f4a14d6902877a92541f5368778853d9c4a0cb7802dcfb2'
  22. push_public_key = '21' + public_key
  23. # below are test cases for all of the standard transaction types
  24. # 1) P2PK scriptSig
  25. # the scriptSig of a public key scriptPubKey simply pushes a signature onto the stack
  26. rpc_result = self.nodes[0].decodescript(push_signature)
  27. assert_equal(signature, rpc_result['asm'])
  28. # 2) P2PKH scriptSig
  29. rpc_result = self.nodes[0].decodescript(push_signature + push_public_key)
  30. assert_equal(signature + ' ' + public_key, rpc_result['asm'])
  31. # 3) multisig scriptSig
  32. # this also tests the leading portion of a P2SH multisig scriptSig
  33. # OP_0 <A sig> <B sig>
  34. rpc_result = self.nodes[0].decodescript('00' + push_signature + push_signature)
  35. assert_equal('0 ' + signature + ' ' + signature, rpc_result['asm'])
  36. # 4) P2SH scriptSig
  37. # an empty P2SH redeemScript is valid and makes for a very simple test case.
  38. # thus, such a spending scriptSig would just need to pass the outer redeemScript
  39. # hash test and leave true on the top of the stack.
  40. rpc_result = self.nodes[0].decodescript('5100')
  41. assert_equal('1 0', rpc_result['asm'])
  42. # 5) null data scriptSig - no such thing because null data scripts can not be spent.
  43. # thus, no test case for that standard transaction type is here.
  44. def decodescript_script_pub_key(self):
  45. public_key = '03b0da749730dc9b4b1f4a14d6902877a92541f5368778853d9c4a0cb7802dcfb2'
  46. push_public_key = '21' + public_key
  47. public_key_hash = '11695b6cd891484c2d49ec5aa738ec2b2f897777'
  48. push_public_key_hash = '14' + public_key_hash
  49. # below are test cases for all of the standard transaction types
  50. # 1) P2PK scriptPubKey
  51. # <pubkey> OP_CHECKSIG
  52. rpc_result = self.nodes[0].decodescript(push_public_key + 'ac')
  53. assert_equal(public_key + ' OP_CHECKSIG', rpc_result['asm'])
  54. # 2) P2PKH scriptPubKey
  55. # OP_DUP OP_HASH160 <PubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
  56. rpc_result = self.nodes[0].decodescript('76a9' + push_public_key_hash + '88ac')
  57. assert_equal('OP_DUP OP_HASH160 ' + public_key_hash + ' OP_EQUALVERIFY OP_CHECKSIG', rpc_result['asm'])
  58. # 3) multisig scriptPubKey
  59. # <m> <A pubkey> <B pubkey> <C pubkey> <n> OP_CHECKMULTISIG
  60. # just imagine that the pub keys used below are different.
  61. # for our purposes here it does not matter that they are the same even though it is unrealistic.
  62. rpc_result = self.nodes[0].decodescript('52' + push_public_key + push_public_key + push_public_key + '53ae')
  63. assert_equal('2 ' + public_key + ' ' + public_key + ' ' + public_key + ' 3 OP_CHECKMULTISIG', rpc_result['asm'])
  64. # 4) P2SH scriptPubKey
  65. # OP_HASH160 <Hash160(redeemScript)> OP_EQUAL.
  66. # push_public_key_hash here should actually be the hash of a redeem script.
  67. # but this works the same for purposes of this test.
  68. rpc_result = self.nodes[0].decodescript('a9' + push_public_key_hash + '87')
  69. assert_equal('OP_HASH160 ' + public_key_hash + ' OP_EQUAL', rpc_result['asm'])
  70. # 5) null data scriptPubKey
  71. # use a signature look-alike here to make sure that we do not decode random data as a signature.
  72. # this matters if/when signature sighash decoding comes along.
  73. # would want to make sure that no such decoding takes place in this case.
  74. signature_imposter = '48304502207fa7a6d1e0ee81132a269ad84e68d695483745cde8b541e3bf630749894e342a022100c1f7ab20e13e22fb95281a870f3dcf38d782e53023ee313d741ad0cfbc0c509001'
  75. # OP_RETURN <data>
  76. rpc_result = self.nodes[0].decodescript('6a' + signature_imposter)
  77. assert_equal('OP_RETURN ' + signature_imposter[2:], rpc_result['asm'])
  78. # 6) a CLTV redeem script. redeem scripts are in-effect scriptPubKey scripts, so adding a test here.
  79. # OP_NOP2 is also known as OP_CHECKLOCKTIMEVERIFY.
  80. # just imagine that the pub keys used below are different.
  81. # for our purposes here it does not matter that they are the same even though it is unrealistic.
  82. #
  83. # OP_IF
  84. # <receiver-pubkey> OP_CHECKSIGVERIFY
  85. # OP_ELSE
  86. # <lock-until> OP_CHECKLOCKTIMEVERIFY OP_DROP
  87. # OP_ENDIF
  88. # <sender-pubkey> OP_CHECKSIG
  89. #
  90. # lock until block 500,000
  91. rpc_result = self.nodes[0].decodescript('63' + push_public_key + 'ad670320a107b17568' + push_public_key + 'ac')
  92. assert_equal('OP_IF ' + public_key + ' OP_CHECKSIGVERIFY OP_ELSE 500000 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_ENDIF ' + public_key + ' OP_CHECKSIG', rpc_result['asm'])
  93. def decoderawtransaction_asm_sighashtype(self):
  94. """Test decoding scripts via RPC command "decoderawtransaction".
  95. This test is in with the "decodescript" tests because they are testing the same "asm" script decodes.
  96. """
  97. # this test case uses a random plain vanilla mainnet transaction with a single P2PKH input and output
  98. tx = '0100000001696a20784a2c70143f634e95227dbdfdf0ecd51647052e70854512235f5986ca010000008a47304402207174775824bec6c2700023309a168231ec80b82c6069282f5133e6f11cbb04460220570edc55c7c5da2ca687ebd0372d3546ebc3f810516a002350cac72dfe192dfb014104d3f898e6487787910a690410b7a917ef198905c27fb9d3b0a42da12aceae0544fc7088d239d9a48f2828a15a09e84043001f27cc80d162cb95404e1210161536ffffffff0100e1f505000000001976a914eb6c6e0cdb2d256a32d97b8df1fc75d1920d9bca88ac00000000'
  99. rpc_result = self.nodes[0].decoderawtransaction(tx)
  100. assert_equal('304402207174775824bec6c2700023309a168231ec80b82c6069282f5133e6f11cbb04460220570edc55c7c5da2ca687ebd0372d3546ebc3f810516a002350cac72dfe192dfb[ALL] 04d3f898e6487787910a690410b7a917ef198905c27fb9d3b0a42da12aceae0544fc7088d239d9a48f2828a15a09e84043001f27cc80d162cb95404e1210161536', rpc_result['vin'][0]['scriptSig']['asm'])
  101. # this test case uses a mainnet transaction that has a P2SH input and both P2PKH and P2SH outputs.
  102. # it's from James D'Angelo's awesome introductory videos about multisig: https://www.youtube.com/watch?v=zIbUSaZBJgU and https://www.youtube.com/watch?v=OSA1pwlaypc
  103. # verify that we have not altered scriptPubKey decoding.
  104. tx = '01000000018d1f5635abd06e2c7e2ddf58dc85b3de111e4ad6e0ab51bb0dcf5e84126d927300000000fdfe0000483045022100ae3b4e589dfc9d48cb82d41008dc5fa6a86f94d5c54f9935531924602730ab8002202f88cf464414c4ed9fa11b773c5ee944f66e9b05cc1e51d97abc22ce098937ea01483045022100b44883be035600e9328a01b66c7d8439b74db64187e76b99a68f7893b701d5380220225bf286493e4c4adcf928c40f785422572eb232f84a0b83b0dea823c3a19c75014c695221020743d44be989540d27b1b4bbbcfd17721c337cb6bc9af20eb8a32520b393532f2102c0120a1dda9e51a938d39ddd9fe0ebc45ea97e1d27a7cbd671d5431416d3dd87210213820eb3d5f509d7438c9eeecb4157b2f595105e7cd564b3cdbb9ead3da41eed53aeffffffff02611e0000000000001976a914dc863734a218bfe83ef770ee9d41a27f824a6e5688acee2a02000000000017a9142a5edea39971049a540474c6a99edf0aa4074c588700000000'
  105. rpc_result = self.nodes[0].decoderawtransaction(tx)
  106. assert_equal('8e3730608c3b0bb5df54f09076e196bc292a8e39a78e73b44b6ba08c78f5cbb0', rpc_result['txid'])
  107. assert_equal('0 3045022100ae3b4e589dfc9d48cb82d41008dc5fa6a86f94d5c54f9935531924602730ab8002202f88cf464414c4ed9fa11b773c5ee944f66e9b05cc1e51d97abc22ce098937ea[ALL] 3045022100b44883be035600e9328a01b66c7d8439b74db64187e76b99a68f7893b701d5380220225bf286493e4c4adcf928c40f785422572eb232f84a0b83b0dea823c3a19c75[ALL] 5221020743d44be989540d27b1b4bbbcfd17721c337cb6bc9af20eb8a32520b393532f2102c0120a1dda9e51a938d39ddd9fe0ebc45ea97e1d27a7cbd671d5431416d3dd87210213820eb3d5f509d7438c9eeecb4157b2f595105e7cd564b3cdbb9ead3da41eed53ae', rpc_result['vin'][0]['scriptSig']['asm'])
  108. assert_equal('OP_DUP OP_HASH160 dc863734a218bfe83ef770ee9d41a27f824a6e56 OP_EQUALVERIFY OP_CHECKSIG', rpc_result['vout'][0]['scriptPubKey']['asm'])
  109. assert_equal('OP_HASH160 2a5edea39971049a540474c6a99edf0aa4074c58 OP_EQUAL', rpc_result['vout'][1]['scriptPubKey']['asm'])
  110. txSave = CTransaction()
  111. txSave.deserialize(BytesIO(hex_str_to_bytes(tx)))
  112. # make sure that a specifically crafted op_return value will not pass all the IsDERSignature checks and then get decoded as a sighash type
  113. tx = '01000000015ded05872fdbda629c7d3d02b194763ce3b9b1535ea884e3c8e765d42e316724020000006b48304502204c10d4064885c42638cbff3585915b322de33762598321145ba033fc796971e2022100bb153ad3baa8b757e30a2175bd32852d2e1cb9080f84d7e32fcdfd667934ef1b012103163c0ff73511ea1743fb5b98384a2ff09dd06949488028fd819f4d83f56264efffffffff0200000000000000000b6a0930060201000201000180380100000000001976a9141cabd296e753837c086da7a45a6c2fe0d49d7b7b88ac00000000'
  114. rpc_result = self.nodes[0].decoderawtransaction(tx)
  115. assert_equal('OP_RETURN 300602010002010001', rpc_result['vout'][0]['scriptPubKey']['asm'])
  116. # verify that we have not altered scriptPubKey processing even of a specially crafted P2PKH pubkeyhash and P2SH redeem script hash that is made to pass the der signature checks
  117. tx = '01000000018d1f5635abd06e2c7e2ddf58dc85b3de111e4ad6e0ab51bb0dcf5e84126d927300000000fdfe0000483045022100ae3b4e589dfc9d48cb82d41008dc5fa6a86f94d5c54f9935531924602730ab8002202f88cf464414c4ed9fa11b773c5ee944f66e9b05cc1e51d97abc22ce098937ea01483045022100b44883be035600e9328a01b66c7d8439b74db64187e76b99a68f7893b701d5380220225bf286493e4c4adcf928c40f785422572eb232f84a0b83b0dea823c3a19c75014c695221020743d44be989540d27b1b4bbbcfd17721c337cb6bc9af20eb8a32520b393532f2102c0120a1dda9e51a938d39ddd9fe0ebc45ea97e1d27a7cbd671d5431416d3dd87210213820eb3d5f509d7438c9eeecb4157b2f595105e7cd564b3cdbb9ead3da41eed53aeffffffff02611e0000000000001976a914301102070101010101010102060101010101010188acee2a02000000000017a91430110207010101010101010206010101010101018700000000'
  118. rpc_result = self.nodes[0].decoderawtransaction(tx)
  119. assert_equal('OP_DUP OP_HASH160 3011020701010101010101020601010101010101 OP_EQUALVERIFY OP_CHECKSIG', rpc_result['vout'][0]['scriptPubKey']['asm'])
  120. assert_equal('OP_HASH160 3011020701010101010101020601010101010101 OP_EQUAL', rpc_result['vout'][1]['scriptPubKey']['asm'])
  121. # some more full transaction tests of varying specific scriptSigs. used instead of
  122. # tests in decodescript_script_sig because the decodescript RPC is specifically
  123. # for working on scriptPubKeys (argh!).
  124. push_signature = bytes_to_hex_str(txSave.vin[0].scriptSig)[2:(0x48*2+4)]
  125. signature = push_signature[2:]
  126. der_signature = signature[:-2]
  127. signature_sighash_decoded = der_signature + '[ALL]'
  128. signature_2 = der_signature + '82'
  129. push_signature_2 = '48' + signature_2
  130. signature_2_sighash_decoded = der_signature + '[NONE|ANYONECANPAY]'
  131. # 1) P2PK scriptSig
  132. txSave.vin[0].scriptSig = hex_str_to_bytes(push_signature)
  133. rpc_result = self.nodes[0].decoderawtransaction(bytes_to_hex_str(txSave.serialize()))
  134. assert_equal(signature_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm'])
  135. # make sure that the sighash decodes come out correctly for a more complex / lesser used case.
  136. txSave.vin[0].scriptSig = hex_str_to_bytes(push_signature_2)
  137. rpc_result = self.nodes[0].decoderawtransaction(bytes_to_hex_str(txSave.serialize()))
  138. assert_equal(signature_2_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm'])
  139. # 2) multisig scriptSig
  140. txSave.vin[0].scriptSig = hex_str_to_bytes('00' + push_signature + push_signature_2)
  141. rpc_result = self.nodes[0].decoderawtransaction(bytes_to_hex_str(txSave.serialize()))
  142. assert_equal('0 ' + signature_sighash_decoded + ' ' + signature_2_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm'])
  143. # 3) test a scriptSig that contains more than push operations.
  144. # in fact, it contains an OP_RETURN with data specially crafted to cause improper decode if the code does not catch it.
  145. txSave.vin[0].scriptSig = hex_str_to_bytes('6a143011020701010101010101020601010101010101')
  146. rpc_result = self.nodes[0].decoderawtransaction(bytes_to_hex_str(txSave.serialize()))
  147. assert_equal('OP_RETURN 3011020701010101010101020601010101010101', rpc_result['vin'][0]['scriptSig']['asm'])
  148. def run_test(self):
  149. self.decodescript_script_sig()
  150. self.decodescript_script_pub_key()
  151. self.decoderawtransaction_asm_sighashtype()
  152. if __name__ == '__main__':
  153. DecodeScriptTest().main()