mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-27 14:51:18 +02:00
Merge bitcoin/bitcoin#25512: test: remove wallet dependency and refactor rpc_signrawtransaction.py
0ee43d13e9
test: refactor rpc_signrawtransaction.py (Ayush Sharma) Pull request description: `rpc_signrawtransaction.py` currently tests the `signrawtransactionwithkey` and `signrawtransactionwithwallet` RPCs. This PR splits `rpc_signrawtransaction.py` into 1. `rpc_signrawtransactionwithkey.py`: the tests for `signrawtransactionwithkey` are moved here and this test can now be run with the wallet disabled. 2. `wallet_signrawtransactionwithwallet.py`: wallet only tests for `signrawtransactionwithwallet.py` ACKs for top commit: aureleoules: tACK0ee43d13e9
. Tree-SHA512: c7bd2ea746345c978eae231a781fc52953b9d277eb9f8abb9c3270fe1d9f678f23f3784377d7303a2aa23d7ad90097245e517d386b27b4e0016585dfddcb9d49
This commit is contained in:
140
test/functional/rpc_signrawtransactionwithkey.py
Executable file
140
test/functional/rpc_signrawtransactionwithkey.py
Executable file
@@ -0,0 +1,140 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# Copyright (c) 2015-2022 The Bitcoin Core developers
|
||||||
|
# Distributed under the MIT software license, see the accompanying
|
||||||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
"""Test transaction signing using the signrawtransactionwithkey RPC."""
|
||||||
|
|
||||||
|
from test_framework.blocktools import (
|
||||||
|
COINBASE_MATURITY,
|
||||||
|
)
|
||||||
|
from test_framework.address import (
|
||||||
|
script_to_p2sh,
|
||||||
|
)
|
||||||
|
from test_framework.key import ECKey
|
||||||
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
|
from test_framework.util import (
|
||||||
|
assert_equal,
|
||||||
|
find_vout_for_address,
|
||||||
|
)
|
||||||
|
from test_framework.script_util import (
|
||||||
|
key_to_p2pk_script,
|
||||||
|
key_to_p2pkh_script,
|
||||||
|
script_to_p2sh_p2wsh_script,
|
||||||
|
script_to_p2wsh_script,
|
||||||
|
)
|
||||||
|
from test_framework.wallet_util import (
|
||||||
|
bytes_to_wif,
|
||||||
|
)
|
||||||
|
|
||||||
|
from decimal import (
|
||||||
|
Decimal,
|
||||||
|
)
|
||||||
|
from test_framework.wallet import (
|
||||||
|
getnewdestination,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class SignRawTransactionWithKeyTest(BitcoinTestFramework):
|
||||||
|
def set_test_params(self):
|
||||||
|
self.setup_clean_chain = True
|
||||||
|
self.num_nodes = 2
|
||||||
|
|
||||||
|
def send_to_address(self, addr, amount):
|
||||||
|
input = {"txid": self.nodes[0].getblock(self.block_hash[self.blk_idx])["tx"][0], "vout": 0}
|
||||||
|
output = {addr: amount}
|
||||||
|
self.blk_idx += 1
|
||||||
|
rawtx = self.nodes[0].createrawtransaction([input], output)
|
||||||
|
txid = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransactionwithkey(rawtx, [self.nodes[0].get_deterministic_priv_key().key])["hex"], 0)
|
||||||
|
return txid
|
||||||
|
|
||||||
|
def successful_signing_test(self):
|
||||||
|
"""Create and sign a valid raw transaction with one input.
|
||||||
|
|
||||||
|
Expected results:
|
||||||
|
|
||||||
|
1) The transaction has a complete set of signatures
|
||||||
|
2) No script verification error occurred"""
|
||||||
|
self.log.info("Test valid raw transaction with one input")
|
||||||
|
privKeys = ['cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N', 'cVKpPfVKSJxKqVpE9awvXNWuLHCa5j5tiE7K6zbUSptFpTEtiFrA']
|
||||||
|
|
||||||
|
inputs = [
|
||||||
|
# Valid pay-to-pubkey scripts
|
||||||
|
{'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0,
|
||||||
|
'scriptPubKey': '76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac'},
|
||||||
|
{'txid': '83a4f6a6b73660e13ee6cb3c6063fa3759c50c9b7521d0536022961898f4fb02', 'vout': 0,
|
||||||
|
'scriptPubKey': '76a914669b857c03a5ed269d5d85a1ffac9ed5d663072788ac'},
|
||||||
|
]
|
||||||
|
|
||||||
|
outputs = {'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB': 0.1}
|
||||||
|
|
||||||
|
rawTx = self.nodes[0].createrawtransaction(inputs, outputs)
|
||||||
|
rawTxSigned = self.nodes[0].signrawtransactionwithkey(rawTx, privKeys, inputs)
|
||||||
|
|
||||||
|
# 1) The transaction has a complete set of signatures
|
||||||
|
assert rawTxSigned['complete']
|
||||||
|
|
||||||
|
# 2) No script verification error occurred
|
||||||
|
assert 'errors' not in rawTxSigned
|
||||||
|
|
||||||
|
def witness_script_test(self):
|
||||||
|
self.log.info("Test signing transaction to P2SH-P2WSH addresses without wallet")
|
||||||
|
# Create a new P2SH-P2WSH 1-of-1 multisig address:
|
||||||
|
eckey = ECKey()
|
||||||
|
eckey.generate()
|
||||||
|
embedded_privkey = bytes_to_wif(eckey.get_bytes())
|
||||||
|
embedded_pubkey = eckey.get_pubkey().get_bytes().hex()
|
||||||
|
p2sh_p2wsh_address = self.nodes[1].createmultisig(1, [embedded_pubkey], "p2sh-segwit")
|
||||||
|
# send transaction to P2SH-P2WSH 1-of-1 multisig address
|
||||||
|
self.block_hash = self.generate(self.nodes[0], COINBASE_MATURITY + 1)
|
||||||
|
self.blk_idx = 0
|
||||||
|
self.send_to_address(p2sh_p2wsh_address["address"], 49.999)
|
||||||
|
self.generate(self.nodes[0], 1)
|
||||||
|
# Get the UTXO info from scantxoutset
|
||||||
|
unspent_output = self.nodes[1].scantxoutset('start', [p2sh_p2wsh_address['descriptor']])['unspents'][0]
|
||||||
|
spk = script_to_p2sh_p2wsh_script(p2sh_p2wsh_address['redeemScript']).hex()
|
||||||
|
unspent_output['witnessScript'] = p2sh_p2wsh_address['redeemScript']
|
||||||
|
unspent_output['redeemScript'] = script_to_p2wsh_script(unspent_output['witnessScript']).hex()
|
||||||
|
assert_equal(spk, unspent_output['scriptPubKey'])
|
||||||
|
# Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys
|
||||||
|
spending_tx = self.nodes[0].createrawtransaction([unspent_output], {getnewdestination()[2]: Decimal("49.998")})
|
||||||
|
spending_tx_signed = self.nodes[0].signrawtransactionwithkey(spending_tx, [embedded_privkey], [unspent_output])
|
||||||
|
# Check the signing completed successfully
|
||||||
|
assert 'complete' in spending_tx_signed
|
||||||
|
assert_equal(spending_tx_signed['complete'], True)
|
||||||
|
|
||||||
|
# Now test with P2PKH and P2PK scripts as the witnessScript
|
||||||
|
for tx_type in ['P2PKH', 'P2PK']: # these tests are order-independent
|
||||||
|
self.verify_txn_with_witness_script(tx_type)
|
||||||
|
|
||||||
|
def verify_txn_with_witness_script(self, tx_type):
|
||||||
|
self.log.info("Test with a {} script as the witnessScript".format(tx_type))
|
||||||
|
eckey = ECKey()
|
||||||
|
eckey.generate()
|
||||||
|
embedded_privkey = bytes_to_wif(eckey.get_bytes())
|
||||||
|
embedded_pubkey = eckey.get_pubkey().get_bytes().hex()
|
||||||
|
witness_script = {
|
||||||
|
'P2PKH': key_to_p2pkh_script(embedded_pubkey).hex(),
|
||||||
|
'P2PK': key_to_p2pk_script(embedded_pubkey).hex()
|
||||||
|
}.get(tx_type, "Invalid tx_type")
|
||||||
|
redeem_script = script_to_p2wsh_script(witness_script).hex()
|
||||||
|
addr = script_to_p2sh(redeem_script)
|
||||||
|
script_pub_key = self.nodes[1].validateaddress(addr)['scriptPubKey']
|
||||||
|
# Fund that address
|
||||||
|
txid = self.send_to_address(addr, 10)
|
||||||
|
vout = find_vout_for_address(self.nodes[0], txid, addr)
|
||||||
|
self.generate(self.nodes[0], 1)
|
||||||
|
# Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys
|
||||||
|
spending_tx = self.nodes[0].createrawtransaction([{'txid': txid, 'vout': vout}], {getnewdestination()[2]: Decimal("9.999")})
|
||||||
|
spending_tx_signed = self.nodes[0].signrawtransactionwithkey(spending_tx, [embedded_privkey], [{'txid': txid, 'vout': vout, 'scriptPubKey': script_pub_key, 'redeemScript': redeem_script, 'witnessScript': witness_script, 'amount': 10}])
|
||||||
|
# Check the signing completed successfully
|
||||||
|
assert 'complete' in spending_tx_signed
|
||||||
|
assert_equal(spending_tx_signed['complete'], True)
|
||||||
|
self.nodes[0].sendrawtransaction(spending_tx_signed['hex'])
|
||||||
|
|
||||||
|
def run_test(self):
|
||||||
|
self.successful_signing_test()
|
||||||
|
self.witness_script_test()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
SignRawTransactionWithKeyTest().main()
|
@@ -182,8 +182,9 @@ BASE_SCRIPTS = [
|
|||||||
'rpc_whitelist.py',
|
'rpc_whitelist.py',
|
||||||
'feature_proxy.py',
|
'feature_proxy.py',
|
||||||
'feature_syscall_sandbox.py',
|
'feature_syscall_sandbox.py',
|
||||||
'rpc_signrawtransaction.py --legacy-wallet',
|
'wallet_signrawtransactionwithwallet.py --legacy-wallet',
|
||||||
'rpc_signrawtransaction.py --descriptors',
|
'wallet_signrawtransactionwithwallet.py --descriptors',
|
||||||
|
'rpc_signrawtransactionwithkey.py',
|
||||||
'rpc_rawtransaction.py --legacy-wallet',
|
'rpc_rawtransaction.py --legacy-wallet',
|
||||||
'wallet_groups.py --legacy-wallet',
|
'wallet_groups.py --legacy-wallet',
|
||||||
'wallet_transactiontime_rescan.py --descriptors',
|
'wallet_transactiontime_rescan.py --descriptors',
|
||||||
|
@@ -2,16 +2,14 @@
|
|||||||
# Copyright (c) 2015-2021 The Bitcoin Core developers
|
# Copyright (c) 2015-2021 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 signrawtransactionwithwallet RPC."""
|
||||||
|
|
||||||
from test_framework.blocktools import (
|
from test_framework.blocktools import (
|
||||||
COINBASE_MATURITY,
|
COINBASE_MATURITY,
|
||||||
)
|
)
|
||||||
from test_framework.address import (
|
from test_framework.address import (
|
||||||
script_to_p2sh,
|
|
||||||
script_to_p2wsh,
|
script_to_p2wsh,
|
||||||
)
|
)
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.util import (
|
from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
@@ -29,20 +27,13 @@ from test_framework.script import (
|
|||||||
OP_DROP,
|
OP_DROP,
|
||||||
OP_TRUE,
|
OP_TRUE,
|
||||||
)
|
)
|
||||||
from test_framework.script_util import (
|
|
||||||
key_to_p2pk_script,
|
|
||||||
key_to_p2pkh_script,
|
|
||||||
script_to_p2sh_p2wsh_script,
|
|
||||||
script_to_p2wsh_script,
|
|
||||||
)
|
|
||||||
from test_framework.wallet_util import bytes_to_wif
|
|
||||||
|
|
||||||
from decimal import (
|
from decimal import (
|
||||||
Decimal,
|
Decimal,
|
||||||
getcontext,
|
getcontext,
|
||||||
)
|
)
|
||||||
|
|
||||||
class SignRawTransactionsTest(BitcoinTestFramework):
|
class SignRawTransactionWithWalletTest(BitcoinTestFramework):
|
||||||
def set_test_params(self):
|
def set_test_params(self):
|
||||||
self.setup_clean_chain = True
|
self.setup_clean_chain = True
|
||||||
self.num_nodes = 2
|
self.num_nodes = 2
|
||||||
@@ -50,35 +41,6 @@ class SignRawTransactionsTest(BitcoinTestFramework):
|
|||||||
def skip_test_if_missing_module(self):
|
def skip_test_if_missing_module(self):
|
||||||
self.skip_if_no_wallet()
|
self.skip_if_no_wallet()
|
||||||
|
|
||||||
def successful_signing_test(self):
|
|
||||||
"""Create and sign a valid raw transaction with one input.
|
|
||||||
|
|
||||||
Expected results:
|
|
||||||
|
|
||||||
1) The transaction has a complete set of signatures
|
|
||||||
2) No script verification error occurred"""
|
|
||||||
self.log.info("Test valid raw transaction with one input")
|
|
||||||
privKeys = ['cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N', 'cVKpPfVKSJxKqVpE9awvXNWuLHCa5j5tiE7K6zbUSptFpTEtiFrA']
|
|
||||||
|
|
||||||
inputs = [
|
|
||||||
# Valid pay-to-pubkey scripts
|
|
||||||
{'txid': '9b907ef1e3c26fc71fe4a4b3580bc75264112f95050014157059c736f0202e71', 'vout': 0,
|
|
||||||
'scriptPubKey': '76a91460baa0f494b38ce3c940dea67f3804dc52d1fb9488ac'},
|
|
||||||
{'txid': '83a4f6a6b73660e13ee6cb3c6063fa3759c50c9b7521d0536022961898f4fb02', 'vout': 0,
|
|
||||||
'scriptPubKey': '76a914669b857c03a5ed269d5d85a1ffac9ed5d663072788ac'},
|
|
||||||
]
|
|
||||||
|
|
||||||
outputs = {'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB': 0.1}
|
|
||||||
|
|
||||||
rawTx = self.nodes[0].createrawtransaction(inputs, outputs)
|
|
||||||
rawTxSigned = self.nodes[0].signrawtransactionwithkey(rawTx, privKeys, inputs)
|
|
||||||
|
|
||||||
# 1) The transaction has a complete set of signatures
|
|
||||||
assert rawTxSigned['complete']
|
|
||||||
|
|
||||||
# 2) No script verification error occurred
|
|
||||||
assert 'errors' not in rawTxSigned
|
|
||||||
|
|
||||||
def test_with_lock_outputs(self):
|
def test_with_lock_outputs(self):
|
||||||
self.log.info("Test correct error reporting when trying to sign a locked output")
|
self.log.info("Test correct error reporting when trying to sign a locked output")
|
||||||
self.nodes[0].encryptwallet("password")
|
self.nodes[0].encryptwallet("password")
|
||||||
@@ -191,60 +153,6 @@ class SignRawTransactionsTest(BitcoinTestFramework):
|
|||||||
assert_equal(signedtx["hex"], signedtx2["hex"])
|
assert_equal(signedtx["hex"], signedtx2["hex"])
|
||||||
self.nodes[0].walletlock()
|
self.nodes[0].walletlock()
|
||||||
|
|
||||||
def witness_script_test(self):
|
|
||||||
self.log.info("Test signing transaction to P2SH-P2WSH addresses without wallet")
|
|
||||||
# Create a new P2SH-P2WSH 1-of-1 multisig address:
|
|
||||||
eckey = ECKey()
|
|
||||||
eckey.generate()
|
|
||||||
embedded_privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
embedded_pubkey = eckey.get_pubkey().get_bytes().hex()
|
|
||||||
p2sh_p2wsh_address = self.nodes[1].createmultisig(1, [embedded_pubkey], "p2sh-segwit")
|
|
||||||
# send transaction to P2SH-P2WSH 1-of-1 multisig address
|
|
||||||
self.generate(self.nodes[0], COINBASE_MATURITY + 1)
|
|
||||||
self.nodes[0].sendtoaddress(p2sh_p2wsh_address["address"], 49.999)
|
|
||||||
self.generate(self.nodes[0], 1)
|
|
||||||
# Get the UTXO info from scantxoutset
|
|
||||||
unspent_output = self.nodes[1].scantxoutset('start', [p2sh_p2wsh_address['descriptor']])['unspents'][0]
|
|
||||||
spk = script_to_p2sh_p2wsh_script(p2sh_p2wsh_address['redeemScript']).hex()
|
|
||||||
unspent_output['witnessScript'] = p2sh_p2wsh_address['redeemScript']
|
|
||||||
unspent_output['redeemScript'] = script_to_p2wsh_script(unspent_output['witnessScript']).hex()
|
|
||||||
assert_equal(spk, unspent_output['scriptPubKey'])
|
|
||||||
# Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys
|
|
||||||
spending_tx = self.nodes[0].createrawtransaction([unspent_output], {self.nodes[1].get_wallet_rpc(self.default_wallet_name).getnewaddress(): Decimal("49.998")})
|
|
||||||
spending_tx_signed = self.nodes[0].signrawtransactionwithkey(spending_tx, [embedded_privkey], [unspent_output])
|
|
||||||
# Check the signing completed successfully
|
|
||||||
assert 'complete' in spending_tx_signed
|
|
||||||
assert_equal(spending_tx_signed['complete'], True)
|
|
||||||
|
|
||||||
# Now test with P2PKH and P2PK scripts as the witnessScript
|
|
||||||
for tx_type in ['P2PKH', 'P2PK']: # these tests are order-independent
|
|
||||||
self.verify_txn_with_witness_script(tx_type)
|
|
||||||
|
|
||||||
def verify_txn_with_witness_script(self, tx_type):
|
|
||||||
self.log.info("Test with a {} script as the witnessScript".format(tx_type))
|
|
||||||
eckey = ECKey()
|
|
||||||
eckey.generate()
|
|
||||||
embedded_privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
embedded_pubkey = eckey.get_pubkey().get_bytes().hex()
|
|
||||||
witness_script = {
|
|
||||||
'P2PKH': key_to_p2pkh_script(embedded_pubkey).hex(),
|
|
||||||
'P2PK': key_to_p2pk_script(embedded_pubkey).hex()
|
|
||||||
}.get(tx_type, "Invalid tx_type")
|
|
||||||
redeem_script = script_to_p2wsh_script(witness_script).hex()
|
|
||||||
addr = script_to_p2sh(redeem_script)
|
|
||||||
script_pub_key = self.nodes[1].validateaddress(addr)['scriptPubKey']
|
|
||||||
# Fund that address
|
|
||||||
txid = self.nodes[0].sendtoaddress(addr, 10)
|
|
||||||
vout = find_vout_for_address(self.nodes[0], txid, addr)
|
|
||||||
self.generate(self.nodes[0], 1)
|
|
||||||
# Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys
|
|
||||||
spending_tx = self.nodes[0].createrawtransaction([{'txid': txid, 'vout': vout}], {self.nodes[1].getnewaddress(): Decimal("9.999")})
|
|
||||||
spending_tx_signed = self.nodes[0].signrawtransactionwithkey(spending_tx, [embedded_privkey], [{'txid': txid, 'vout': vout, 'scriptPubKey': script_pub_key, 'redeemScript': redeem_script, 'witnessScript': witness_script, 'amount': 10}])
|
|
||||||
# Check the signing completed successfully
|
|
||||||
assert 'complete' in spending_tx_signed
|
|
||||||
assert_equal(spending_tx_signed['complete'], True)
|
|
||||||
self.nodes[0].sendrawtransaction(spending_tx_signed['hex'])
|
|
||||||
|
|
||||||
def OP_1NEGATE_test(self):
|
def OP_1NEGATE_test(self):
|
||||||
self.log.info("Test OP_1NEGATE (0x4f) satisfies BIP62 minimal push standardness rule")
|
self.log.info("Test OP_1NEGATE (0x4f) satisfies BIP62 minimal push standardness rule")
|
||||||
hex_str = (
|
hex_str = (
|
||||||
@@ -385,9 +293,7 @@ class SignRawTransactionsTest(BitcoinTestFramework):
|
|||||||
])
|
])
|
||||||
|
|
||||||
def run_test(self):
|
def run_test(self):
|
||||||
self.successful_signing_test()
|
|
||||||
self.script_verification_error_test()
|
self.script_verification_error_test()
|
||||||
self.witness_script_test()
|
|
||||||
self.OP_1NEGATE_test()
|
self.OP_1NEGATE_test()
|
||||||
self.test_with_lock_outputs()
|
self.test_with_lock_outputs()
|
||||||
self.test_fully_signed_tx()
|
self.test_fully_signed_tx()
|
||||||
@@ -397,4 +303,4 @@ class SignRawTransactionsTest(BitcoinTestFramework):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
SignRawTransactionsTest().main()
|
SignRawTransactionWithWalletTest().main()
|
Reference in New Issue
Block a user