|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# Copyright (c) 2015 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 | + |
| 6 | +from test_framework import BitcoinTestFramework |
| 7 | +from util import * |
| 8 | + |
| 9 | + |
| 10 | +class SignRawTransactionsTest(BitcoinTestFramework): |
| 11 | + """Tests transaction signing via RPC command "signrawtransaction".""" |
| 12 | + |
| 13 | + def setup_chain(self): |
| 14 | + print('Initializing test directory ' + self.options.tmpdir) |
| 15 | + initialize_chain_clean(self.options.tmpdir, 1) |
| 16 | + |
| 17 | + def setup_network(self, split=False): |
| 18 | + self.nodes = start_nodes(1, self.options.tmpdir) |
| 19 | + self.is_network_split = False |
| 20 | + |
| 21 | + def successful_signing_test(self): |
| 22 | + """Creates and signs a valid raw transaction with one input. |
| 23 | +
|
| 24 | + Expected results: |
| 25 | +
|
| 26 | + 1) The transaction has a complete set of signatures |
| 27 | + 2) No script verification error occurred""" |
| 28 | + privKeys = ['cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N'] |
| 29 | + |
| 30 | + inputs = [ |
| 31 | + # Valid pay-to-pubkey script |
| 32 | + {'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0, |
| 33 | + 'scriptPubKey': '76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac'} |
| 34 | + ] |
| 35 | + |
| 36 | + outputs = {'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB': 0.1} |
| 37 | + |
| 38 | + rawTx = self.nodes[0].createrawtransaction(inputs, outputs) |
| 39 | + rawTxSigned = self.nodes[0].signrawtransaction(rawTx, inputs, privKeys) |
| 40 | + |
| 41 | + # 1) The transaction has a complete set of signatures |
| 42 | + assert 'complete' in rawTxSigned |
| 43 | + assert_equal(rawTxSigned['complete'], True) |
| 44 | + |
| 45 | + # 2) No script verification error occurred |
| 46 | + assert 'errors' not in rawTxSigned |
| 47 | + |
| 48 | + def script_verification_error_test(self): |
| 49 | + """Creates and signs a raw transaction with valid (vin 0), invalid (vin 1) and one missing (vin 2) input script. |
| 50 | +
|
| 51 | + Expected results: |
| 52 | +
|
| 53 | + 3) The transaction has no complete set of signatures |
| 54 | + 4) Two script verification errors occurred |
| 55 | + 5) Script verification errors have certain properties ("txid", "vout", "scriptSig", "sequence", "error") |
| 56 | + 6) The verification errors refer to the invalid (vin 1) and missing input (vin 2)""" |
| 57 | + privKeys = ['cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N'] |
| 58 | + |
| 59 | + inputs = [ |
| 60 | + # Valid pay-to-pubkey script |
| 61 | + {'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0}, |
| 62 | + # Invalid script |
| 63 | + {'txid': '5b8673686910442c644b1f4993d8f7753c7c8fcb5c87ee40d56eaeef25204547', 'vout': 7}, |
| 64 | + # Missing scriptPubKey |
| 65 | + {'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 1}, |
| 66 | + ] |
| 67 | + |
| 68 | + scripts = [ |
| 69 | + # Valid pay-to-pubkey script |
| 70 | + {'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0, |
| 71 | + 'scriptPubKey': '76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac'}, |
| 72 | + # Invalid script |
| 73 | + {'txid': '5b8673686910442c644b1f4993d8f7753c7c8fcb5c87ee40d56eaeef25204547', 'vout': 7, |
| 74 | + 'scriptPubKey': 'badbadbadbad'} |
| 75 | + ] |
| 76 | + |
| 77 | + outputs = {'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB': 0.1} |
| 78 | + |
| 79 | + rawTx = self.nodes[0].createrawtransaction(inputs, outputs) |
| 80 | + rawTxSigned = self.nodes[0].signrawtransaction(rawTx, scripts, privKeys) |
| 81 | + |
| 82 | + # 3) The transaction has no complete set of signatures |
| 83 | + assert 'complete' in rawTxSigned |
| 84 | + assert_equal(rawTxSigned['complete'], False) |
| 85 | + |
| 86 | + # 4) Two script verification errors occurred |
| 87 | + assert 'errors' in rawTxSigned |
| 88 | + assert_equal(len(rawTxSigned['errors']), 2) |
| 89 | + |
| 90 | + # 5) Script verification errors have certain properties |
| 91 | + assert 'txid' in rawTxSigned['errors'][0] |
| 92 | + assert 'vout' in rawTxSigned['errors'][0] |
| 93 | + assert 'scriptSig' in rawTxSigned['errors'][0] |
| 94 | + assert 'sequence' in rawTxSigned['errors'][0] |
| 95 | + assert 'error' in rawTxSigned['errors'][0] |
| 96 | + |
| 97 | + # 6) The verification errors refer to the invalid (vin 1) and missing input (vin 2) |
| 98 | + assert_equal(rawTxSigned['errors'][0]['txid'], inputs[1]['txid']) |
| 99 | + assert_equal(rawTxSigned['errors'][0]['vout'], inputs[1]['vout']) |
| 100 | + assert_equal(rawTxSigned['errors'][1]['txid'], inputs[2]['txid']) |
| 101 | + assert_equal(rawTxSigned['errors'][1]['vout'], inputs[2]['vout']) |
| 102 | + |
| 103 | + def run_test(self): |
| 104 | + self.successful_signing_test() |
| 105 | + self.script_verification_error_test() |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == '__main__': |
| 109 | + SignRawTransactionsTest().main() |
0 commit comments