2016-03-19 20:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-07-27 00:36:45 +02:00
|
|
|
# Copyright (c) 2015-2018 The Bitcoin Core developers
|
2015-03-23 18:16:07 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-06-13 00:02:49 +02:00
|
|
|
"""Test transaction signing using the signrawtransaction* RPCs."""
|
2015-03-23 18:16:07 +01:00
|
|
|
|
2015-05-02 13:58:10 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2018-07-07 00:10:35 +02:00
|
|
|
from test_framework.util import assert_equal, assert_raises_rpc_error
|
2015-03-23 18:16:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SignRawTransactionsTest(BitcoinTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2016-05-14 13:01:31 +02:00
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 1
|
2017-06-13 00:02:49 +02:00
|
|
|
self.extra_args = [["-deprecatedrpc=signrawtransaction"]]
|
2015-03-23 18:16:07 +01:00
|
|
|
|
2018-09-09 19:32:37 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2015-03-23 18:16:07 +01:00
|
|
|
def successful_signing_test(self):
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Create and sign a valid raw transaction with one input.
|
2015-03-23 18:16:07 +01:00
|
|
|
|
|
|
|
Expected results:
|
|
|
|
|
|
|
|
1) The transaction has a complete set of signatures
|
|
|
|
2) No script verification error occurred"""
|
2017-02-02 22:24:31 +01:00
|
|
|
privKeys = ['cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N', 'cVKpPfVKSJxKqVpE9awvXNWuLHCa5j5tiE7K6zbUSptFpTEtiFrA']
|
2015-03-23 18:16:07 +01:00
|
|
|
|
|
|
|
inputs = [
|
2017-02-02 22:24:31 +01:00
|
|
|
# Valid pay-to-pubkey scripts
|
2015-03-23 18:16:07 +01:00
|
|
|
{'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0,
|
2017-02-02 22:24:31 +01:00
|
|
|
'scriptPubKey': '76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac'},
|
|
|
|
{'txid': '83a4f6a6b73660e13ee6cb3c6063fa3759c50c9b7521d0536022961898f4fb02', 'vout': 0,
|
|
|
|
'scriptPubKey': '76a914669b857c03a5ed269d5d85a1ffac9ed5d663072788ac'},
|
2015-03-23 18:16:07 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
outputs = {'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB': 0.1}
|
|
|
|
|
|
|
|
rawTx = self.nodes[0].createrawtransaction(inputs, outputs)
|
2017-06-12 21:23:02 +02:00
|
|
|
rawTxSigned = self.nodes[0].signrawtransactionwithkey(rawTx, privKeys, inputs)
|
2015-03-23 18:16:07 +01:00
|
|
|
|
|
|
|
# 1) The transaction has a complete set of signatures
|
2017-06-13 00:02:49 +02:00
|
|
|
assert rawTxSigned['complete']
|
2015-03-23 18:16:07 +01:00
|
|
|
|
|
|
|
# 2) No script verification error occurred
|
|
|
|
assert 'errors' not in rawTxSigned
|
|
|
|
|
|
|
|
def script_verification_error_test(self):
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Create and sign a raw transaction with valid (vin 0), invalid (vin 1) and one missing (vin 2) input script.
|
2015-03-23 18:16:07 +01:00
|
|
|
|
|
|
|
Expected results:
|
|
|
|
|
|
|
|
3) The transaction has no complete set of signatures
|
|
|
|
4) Two script verification errors occurred
|
|
|
|
5) Script verification errors have certain properties ("txid", "vout", "scriptSig", "sequence", "error")
|
|
|
|
6) The verification errors refer to the invalid (vin 1) and missing input (vin 2)"""
|
|
|
|
privKeys = ['cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N']
|
|
|
|
|
|
|
|
inputs = [
|
|
|
|
# Valid pay-to-pubkey script
|
|
|
|
{'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0},
|
|
|
|
# Invalid script
|
|
|
|
{'txid': '5b8673686910442c644b1f4993d8f7753c7c8fcb5c87ee40d56eaeef25204547', 'vout': 7},
|
|
|
|
# Missing scriptPubKey
|
|
|
|
{'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 1},
|
|
|
|
]
|
|
|
|
|
|
|
|
scripts = [
|
|
|
|
# Valid pay-to-pubkey script
|
|
|
|
{'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0,
|
|
|
|
'scriptPubKey': '76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac'},
|
|
|
|
# Invalid script
|
|
|
|
{'txid': '5b8673686910442c644b1f4993d8f7753c7c8fcb5c87ee40d56eaeef25204547', 'vout': 7,
|
|
|
|
'scriptPubKey': 'badbadbadbad'}
|
|
|
|
]
|
|
|
|
|
|
|
|
outputs = {'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB': 0.1}
|
|
|
|
|
|
|
|
rawTx = self.nodes[0].createrawtransaction(inputs, outputs)
|
2017-02-02 21:11:28 +01:00
|
|
|
|
|
|
|
# Make sure decoderawtransaction is at least marginally sane
|
|
|
|
decodedRawTx = self.nodes[0].decoderawtransaction(rawTx)
|
|
|
|
for i, inp in enumerate(inputs):
|
|
|
|
assert_equal(decodedRawTx["vin"][i]["txid"], inp["txid"])
|
|
|
|
assert_equal(decodedRawTx["vin"][i]["vout"], inp["vout"])
|
|
|
|
|
|
|
|
# Make sure decoderawtransaction throws if there is extra data
|
2017-07-12 16:33:46 +02:00
|
|
|
assert_raises_rpc_error(-22, "TX decode failed", self.nodes[0].decoderawtransaction, rawTx + "00")
|
2017-02-02 21:11:28 +01:00
|
|
|
|
2017-06-12 21:23:02 +02:00
|
|
|
rawTxSigned = self.nodes[0].signrawtransactionwithkey(rawTx, privKeys, scripts)
|
2015-03-23 18:16:07 +01:00
|
|
|
|
|
|
|
# 3) The transaction has no complete set of signatures
|
2017-06-13 00:02:49 +02:00
|
|
|
assert not rawTxSigned['complete']
|
2015-03-23 18:16:07 +01:00
|
|
|
|
|
|
|
# 4) Two script verification errors occurred
|
|
|
|
assert 'errors' in rawTxSigned
|
|
|
|
assert_equal(len(rawTxSigned['errors']), 2)
|
|
|
|
|
|
|
|
# 5) Script verification errors have certain properties
|
|
|
|
assert 'txid' in rawTxSigned['errors'][0]
|
|
|
|
assert 'vout' in rawTxSigned['errors'][0]
|
2016-08-03 15:04:50 +02:00
|
|
|
assert 'witness' in rawTxSigned['errors'][0]
|
2015-03-23 18:16:07 +01:00
|
|
|
assert 'scriptSig' in rawTxSigned['errors'][0]
|
|
|
|
assert 'sequence' in rawTxSigned['errors'][0]
|
|
|
|
assert 'error' in rawTxSigned['errors'][0]
|
|
|
|
|
|
|
|
# 6) The verification errors refer to the invalid (vin 1) and missing input (vin 2)
|
|
|
|
assert_equal(rawTxSigned['errors'][0]['txid'], inputs[1]['txid'])
|
|
|
|
assert_equal(rawTxSigned['errors'][0]['vout'], inputs[1]['vout'])
|
|
|
|
assert_equal(rawTxSigned['errors'][1]['txid'], inputs[2]['txid'])
|
|
|
|
assert_equal(rawTxSigned['errors'][1]['vout'], inputs[2]['vout'])
|
2016-08-03 15:04:50 +02:00
|
|
|
assert not rawTxSigned['errors'][0]['witness']
|
|
|
|
|
|
|
|
# Now test signing failure for transaction with input witnesses
|
|
|
|
p2wpkh_raw_tx = "01000000000102fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f00000000494830450221008b9d1dc26ba6a9cb62127b02742fa9d754cd3bebf337f7a55d114c8e5cdd30be022040529b194ba3f9281a99f2b1c0a19c0489bc22ede944ccf4ecbab4cc618ef3ed01eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a0100000000ffffffff02202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac000247304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee0121025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee635711000000"
|
|
|
|
|
2017-06-12 21:23:02 +02:00
|
|
|
rawTxSigned = self.nodes[0].signrawtransactionwithwallet(p2wpkh_raw_tx)
|
2016-08-03 15:04:50 +02:00
|
|
|
|
|
|
|
# 7) The transaction has no complete set of signatures
|
2017-06-13 00:02:49 +02:00
|
|
|
assert not rawTxSigned['complete']
|
2016-08-03 15:04:50 +02:00
|
|
|
|
|
|
|
# 8) Two script verification errors occurred
|
|
|
|
assert 'errors' in rawTxSigned
|
|
|
|
assert_equal(len(rawTxSigned['errors']), 2)
|
|
|
|
|
|
|
|
# 9) Script verification errors have certain properties
|
|
|
|
assert 'txid' in rawTxSigned['errors'][0]
|
|
|
|
assert 'vout' in rawTxSigned['errors'][0]
|
|
|
|
assert 'witness' in rawTxSigned['errors'][0]
|
|
|
|
assert 'scriptSig' in rawTxSigned['errors'][0]
|
|
|
|
assert 'sequence' in rawTxSigned['errors'][0]
|
|
|
|
assert 'error' in rawTxSigned['errors'][0]
|
|
|
|
|
|
|
|
# Non-empty witness checked here
|
|
|
|
assert_equal(rawTxSigned['errors'][1]['witness'], ["304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee01", "025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee6357"])
|
|
|
|
assert not rawTxSigned['errors'][0]['witness']
|
2015-03-23 18:16:07 +01:00
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
self.successful_signing_test()
|
|
|
|
self.script_verification_error_test()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
SignRawTransactionsTest().main()
|