Skip to content

Commit 40f5e8d

Browse files
committed
Merge pull request #5937
a71ab10 QA: add RPC tests for error reporting of "signrawtransaction" (dexX7) 8ac2a4e RPC: show script verification errors in "signrawtransaction" result (dexX7)
2 parents 31c0bf1 + a71ab10 commit 40f5e8d

File tree

3 files changed

+149
-8
lines changed

3 files changed

+149
-8
lines changed

qa/pull-tester/rpc-tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ testScripts=(
2222
'txn_doublespend.py'
2323
'txn_doublespend.py --mineblock'
2424
'getchaintips.py'
25+
'rawtransactions.py'
2526
'rest.py'
2627
'mempool_spendcoinbase.py'
2728
'mempool_coinbase_spends.py'
2829
'httpbasics.py'
2930
'zapwallettxes.py'
3031
'proxy_test.py'
3132
'merkle_blocks.py'
32-
'rawtransactions.py'
33+
'signrawtransactions.py'
3334
# 'forknotify.py'
3435
'maxblocksinflight.py'
3536
'invalidblockrequest.py'

qa/rpc-tests/signrawtransactions.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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()

src/rpcrawtransaction.cpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2014 The Bitcoin Core developers
2+
// Copyright (c) 2009-2015 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -13,6 +13,7 @@
1313
#include "net.h"
1414
#include "rpcserver.h"
1515
#include "script/script.h"
16+
#include "script/script_error.h"
1617
#include "script/sign.h"
1718
#include "script/standard.h"
1819
#include "uint256.h"
@@ -491,6 +492,18 @@ Value decodescript(const Array& params, bool fHelp)
491492
return r;
492493
}
493494

495+
/** Pushes a JSON object for script verification or signing errors to vErrorsRet. */
496+
static void TxInErrorToJSON(const CTxIn& txin, Array& vErrorsRet, const std::string& strMessage)
497+
{
498+
Object entry;
499+
entry.push_back(Pair("txid", txin.prevout.hash.ToString()));
500+
entry.push_back(Pair("vout", (uint64_t)txin.prevout.n));
501+
entry.push_back(Pair("scriptSig", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
502+
entry.push_back(Pair("sequence", (uint64_t)txin.nSequence));
503+
entry.push_back(Pair("error", strMessage));
504+
vErrorsRet.push_back(entry);
505+
}
506+
494507
Value signrawtransaction(const Array& params, bool fHelp)
495508
{
496509
if (fHelp || params.size() < 1 || params.size() > 4)
@@ -532,8 +545,18 @@ Value signrawtransaction(const Array& params, bool fHelp)
532545

533546
"\nResult:\n"
534547
"{\n"
535-
" \"hex\": \"value\", (string) The raw transaction with signature(s) (hex-encoded string)\n"
536-
" \"complete\": true|false (boolean) if transaction has a complete set of signature\n"
548+
" \"hex\" : \"value\", (string) The hex-encoded raw transaction with signature(s)\n"
549+
" \"complete\" : true|false, (boolean) If the transaction has a complete set of signatures\n"
550+
" \"errors\" : [ (json array of objects) Script verification errors (if there are any)\n"
551+
" {\n"
552+
" \"txid\" : \"hash\", (string) The hash of the referenced, previous transaction\n"
553+
" \"vout\" : n, (numeric) The index of the output to spent and used as input\n"
554+
" \"scriptSig\" : \"hex\", (string) The hex-encoded signature script\n"
555+
" \"sequence\" : n, (numeric) Script sequence number\n"
556+
" \"error\" : \"text\" (string) Verification or signing error related to the input\n"
557+
" }\n"
558+
" ,...\n"
559+
" ]\n"
537560
"}\n"
538561

539562
"\nExamples:\n"
@@ -568,7 +591,6 @@ Value signrawtransaction(const Array& params, bool fHelp)
568591
// mergedTx will end up with all the signatures; it
569592
// starts as a clone of the rawtx:
570593
CMutableTransaction mergedTx(txVariants[0]);
571-
bool fComplete = true;
572594

573595
// Fetch previous transactions (inputs):
574596
CCoinsView viewDummy;
@@ -683,12 +705,15 @@ Value signrawtransaction(const Array& params, bool fHelp)
683705

684706
bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
685707

708+
// Script verification errors
709+
Array vErrors;
710+
686711
// Sign what we can:
687712
for (unsigned int i = 0; i < mergedTx.vin.size(); i++) {
688713
CTxIn& txin = mergedTx.vin[i];
689714
const CCoins* coins = view.AccessCoins(txin.prevout.hash);
690715
if (coins == NULL || !coins->IsAvailable(txin.prevout.n)) {
691-
fComplete = false;
716+
TxInErrorToJSON(txin, vErrors, "Input not found or already spent");
692717
continue;
693718
}
694719
const CScript& prevPubKey = coins->vout[txin.prevout.n].scriptPubKey;
@@ -702,13 +727,19 @@ Value signrawtransaction(const Array& params, bool fHelp)
702727
BOOST_FOREACH(const CMutableTransaction& txv, txVariants) {
703728
txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
704729
}
705-
if (!VerifyScript(txin.scriptSig, prevPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker(&mergedTx, i)))
706-
fComplete = false;
730+
ScriptError serror = SCRIPT_ERR_OK;
731+
if (!VerifyScript(txin.scriptSig, prevPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker(&mergedTx, i), &serror)) {
732+
TxInErrorToJSON(txin, vErrors, ScriptErrorString(serror));
733+
}
707734
}
735+
bool fComplete = vErrors.empty();
708736

709737
Object result;
710738
result.push_back(Pair("hex", EncodeHexTx(mergedTx)));
711739
result.push_back(Pair("complete", fComplete));
740+
if (!vErrors.empty()) {
741+
result.push_back(Pair("errors", vErrors));
742+
}
712743

713744
return result;
714745
}

0 commit comments

Comments
 (0)