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.

signrawtransactions.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 transaction signing using the signrawtransaction RPC."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. class SignRawTransactionsTest(BitcoinTestFramework):
  9. def __init__(self):
  10. super().__init__()
  11. self.setup_clean_chain = True
  12. self.num_nodes = 1
  13. def setup_network(self, split=False):
  14. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
  15. self.is_network_split = False
  16. def successful_signing_test(self):
  17. """Create and sign a valid raw transaction with one input.
  18. Expected results:
  19. 1) The transaction has a complete set of signatures
  20. 2) No script verification error occurred"""
  21. privKeys = ['cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N', 'cVKpPfVKSJxKqVpE9awvXNWuLHCa5j5tiE7K6zbUSptFpTEtiFrA']
  22. inputs = [
  23. # Valid pay-to-pubkey scripts
  24. {'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0,
  25. 'scriptPubKey': '76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac'},
  26. {'txid': '83a4f6a6b73660e13ee6cb3c6063fa3759c50c9b7521d0536022961898f4fb02', 'vout': 0,
  27. 'scriptPubKey': '76a914669b857c03a5ed269d5d85a1ffac9ed5d663072788ac'},
  28. ]
  29. outputs = {'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB': 0.1}
  30. rawTx = self.nodes[0].createrawtransaction(inputs, outputs)
  31. rawTxSigned = self.nodes[0].signrawtransaction(rawTx, inputs, privKeys)
  32. # 1) The transaction has a complete set of signatures
  33. assert 'complete' in rawTxSigned
  34. assert_equal(rawTxSigned['complete'], True)
  35. # 2) No script verification error occurred
  36. assert 'errors' not in rawTxSigned
  37. # Check that signrawtransaction doesn't blow up on garbage merge attempts
  38. dummyTxInconsistent = self.nodes[0].createrawtransaction([inputs[0]], outputs)
  39. rawTxUnsigned = self.nodes[0].signrawtransaction(rawTx + dummyTxInconsistent, inputs)
  40. assert 'complete' in rawTxUnsigned
  41. assert_equal(rawTxUnsigned['complete'], False)
  42. # Check that signrawtransaction properly merges unsigned and signed txn, even with garbage in the middle
  43. rawTxSigned2 = self.nodes[0].signrawtransaction(rawTxUnsigned["hex"] + dummyTxInconsistent + rawTxSigned["hex"], inputs)
  44. assert 'complete' in rawTxSigned2
  45. assert_equal(rawTxSigned2['complete'], True)
  46. assert 'errors' not in rawTxSigned2
  47. def script_verification_error_test(self):
  48. """Create and sign a raw transaction with valid (vin 0), invalid (vin 1) and one missing (vin 2) input script.
  49. Expected results:
  50. 3) The transaction has no complete set of signatures
  51. 4) Two script verification errors occurred
  52. 5) Script verification errors have certain properties ("txid", "vout", "scriptSig", "sequence", "error")
  53. 6) The verification errors refer to the invalid (vin 1) and missing input (vin 2)"""
  54. privKeys = ['cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N']
  55. inputs = [
  56. # Valid pay-to-pubkey script
  57. {'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0},
  58. # Invalid script
  59. {'txid': '5b8673686910442c644b1f4993d8f7753c7c8fcb5c87ee40d56eaeef25204547', 'vout': 7},
  60. # Missing scriptPubKey
  61. {'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 1},
  62. ]
  63. scripts = [
  64. # Valid pay-to-pubkey script
  65. {'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0,
  66. 'scriptPubKey': '76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac'},
  67. # Invalid script
  68. {'txid': '5b8673686910442c644b1f4993d8f7753c7c8fcb5c87ee40d56eaeef25204547', 'vout': 7,
  69. 'scriptPubKey': 'badbadbadbad'}
  70. ]
  71. outputs = {'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB': 0.1}
  72. rawTx = self.nodes[0].createrawtransaction(inputs, outputs)
  73. # Make sure decoderawtransaction is at least marginally sane
  74. decodedRawTx = self.nodes[0].decoderawtransaction(rawTx)
  75. for i, inp in enumerate(inputs):
  76. assert_equal(decodedRawTx["vin"][i]["txid"], inp["txid"])
  77. assert_equal(decodedRawTx["vin"][i]["vout"], inp["vout"])
  78. # Make sure decoderawtransaction throws if there is extra data
  79. assert_raises(JSONRPCException, self.nodes[0].decoderawtransaction, rawTx + "00")
  80. rawTxSigned = self.nodes[0].signrawtransaction(rawTx, scripts, privKeys)
  81. # 3) The transaction has no complete set of signatures
  82. assert 'complete' in rawTxSigned
  83. assert_equal(rawTxSigned['complete'], False)
  84. # 4) Two script verification errors occurred
  85. assert 'errors' in rawTxSigned
  86. assert_equal(len(rawTxSigned['errors']), 2)
  87. # 5) Script verification errors have certain properties
  88. assert 'txid' in rawTxSigned['errors'][0]
  89. assert 'vout' in rawTxSigned['errors'][0]
  90. assert 'scriptSig' in rawTxSigned['errors'][0]
  91. assert 'sequence' in rawTxSigned['errors'][0]
  92. assert 'error' in rawTxSigned['errors'][0]
  93. # 6) The verification errors refer to the invalid (vin 1) and missing input (vin 2)
  94. assert_equal(rawTxSigned['errors'][0]['txid'], inputs[1]['txid'])
  95. assert_equal(rawTxSigned['errors'][0]['vout'], inputs[1]['vout'])
  96. assert_equal(rawTxSigned['errors'][1]['txid'], inputs[2]['txid'])
  97. assert_equal(rawTxSigned['errors'][1]['vout'], inputs[2]['vout'])
  98. def run_test(self):
  99. self.successful_signing_test()
  100. self.script_verification_error_test()
  101. if __name__ == '__main__':
  102. SignRawTransactionsTest().main()