Changeset View
Changeset View
Standalone View
Standalone View
test/functional/rpc_signrawtransaction.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2016 The Bitcoin Core developers | # Copyright (c) 2015-2016 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test transaction signing using the signrawtransaction* RPCs.""" | """Test transaction signing using the signrawtransaction* RPCs.""" | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import assert_equal, assert_raises_rpc_error | from test_framework.util import assert_equal, assert_raises_rpc_error | ||||
RPC_WALLET_NOT_SPECIFIED = "Wallet file not specified (must request wallet " + \ | |||||
"RPC through /wallet/<filename> uri-path)." | |||||
class SignRawTransactionsTest(BitcoinTestFramework): | class SignRawTransactionsTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.num_nodes = 1 | self.num_nodes = 2 | ||||
self.extra_args = [["-deprecatedrpc=signrawtransaction"]] | self.extra_args = [["-deprecatedrpc=signrawtransaction"], | ||||
["-deprecatedrpc=signrawtransaction", "-wallet=w1", | |||||
"-wallet=w2"]] | |||||
def successful_signing_test(self): | def successful_signing_test(self): | ||||
"""Creates and signs a valid raw transaction with one input. | """Creates and signs a valid raw transaction with one input. | ||||
Expected results: | Expected results: | ||||
1) The transaction has a complete set of signatures | 1) The transaction has a complete set of signatures | ||||
2) No script verification error occurred""" | 2) No script verification error occurred""" | ||||
▲ Show 20 Lines • Show All 164 Lines • ▼ Show 20 Lines | def test_sighashes(self): | ||||
rawTx, privKeys, inputs, sighash) | rawTx, privKeys, inputs, sighash) | ||||
# 3) If the sighash is invalid the RPC throws an error | # 3) If the sighash is invalid the RPC throws an error | ||||
for sighash in invalid_sighashes: | for sighash in invalid_sighashes: | ||||
assert_raises_rpc_error(-8, "Invalid sighash param", | assert_raises_rpc_error(-8, "Invalid sighash param", | ||||
self.nodes[0].signrawtransactionwithkey, | self.nodes[0].signrawtransactionwithkey, | ||||
rawTx, privKeys, inputs, sighash) | rawTx, privKeys, inputs, sighash) | ||||
def multiwallet_signing_test(self): | |||||
"""Creates and signs a raw transaction with a multiwallet node. | |||||
Expected results: | |||||
1) The transaction is not signed if no wallet is specified | |||||
2) The transaction is signed if the correct wallet URI is given""" | |||||
inputs = [ | |||||
# Valid pay-to-pubkey scripts | |||||
{'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', | |||||
'vout': 0, 'amount': 3.14159, | |||||
'scriptPubKey': '76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac'}, | |||||
] | |||||
outputs = {'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB': 0.1} | |||||
multiwallet_node = self.nodes[1] | |||||
rawTx = multiwallet_node.createrawtransaction(inputs, outputs) | |||||
# The multiwallet node cannot sign the transaction if no wallet is | |||||
# specified | |||||
assert_raises_rpc_error(-19, RPC_WALLET_NOT_SPECIFIED, | |||||
multiwallet_node.signrawtransactionwithwallet, | |||||
rawTx) | |||||
# The multiwallet node can sign the transaction using w1 | |||||
w1 = multiwallet_node.get_wallet_rpc('w1') | |||||
w1.generate(101) | |||||
utxo = w1.listunspent()[0] | |||||
inputs = [{ | |||||
'txid': utxo['txid'], | |||||
'vout': utxo['vout'], | |||||
}] | |||||
rawTx_w1 = w1.createrawtransaction(inputs, outputs) | |||||
rawTxSigned_w1 = w1.signrawtransactionwithwallet(rawTx_w1) | |||||
assert rawTxSigned_w1['complete'] | |||||
assert 'errors' not in rawTxSigned_w1 | |||||
# Perform the same test on signrawtransaction | |||||
rawTxSigned_w1 = w1.signrawtransaction(rawTx_w1) | |||||
assert rawTxSigned_w1['complete'] | |||||
assert 'errors' not in rawTxSigned_w1 | |||||
def run_test(self): | def run_test(self): | ||||
self.successful_signing_test() | self.successful_signing_test() | ||||
self.script_verification_error_test() | self.script_verification_error_test() | ||||
self.test_sighashes() | self.test_sighashes() | ||||
self.multiwallet_signing_test() | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
SignRawTransactionsTest().main() | SignRawTransactionsTest().main() |