Skip to content

Commit 928beae

Browse files
committed
Add test for P2SH-P2WSH in signrawtransactionwithkey and listunspent
1 parent 314784a commit 928beae

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

test/functional/rpc_signrawtransaction.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
"""Test transaction signing using the signrawtransaction* RPCs."""
66

77
from test_framework.test_framework import BitcoinTestFramework
8-
from test_framework.util import assert_equal, assert_raises_rpc_error
8+
from test_framework.util import assert_equal, assert_raises_rpc_error, bytes_to_hex_str, hex_str_to_bytes
9+
from test_framework.messages import sha256
10+
from test_framework.script import CScript, OP_0
911

12+
from decimal import Decimal
1013

1114
class SignRawTransactionsTest(BitcoinTestFramework):
1215
def set_test_params(self):
1316
self.setup_clean_chain = True
14-
self.num_nodes = 1
15-
self.extra_args = [["-deprecatedrpc=signrawtransaction"]]
17+
self.num_nodes = 2
18+
self.extra_args = [["-deprecatedrpc=signrawtransaction"], []]
1619

1720
def skip_test_if_missing_module(self):
1821
self.skip_if_no_wallet()
@@ -143,9 +146,33 @@ def script_verification_error_test(self):
143146
assert_equal(rawTxSigned['errors'][1]['witness'], ["304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee01", "025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee6357"])
144147
assert not rawTxSigned['errors'][0]['witness']
145148

149+
def witness_script_test(self):
150+
# Now test signing transaction to P2SH-P2WSH addresses without wallet
151+
# Create a new P2SH-P2WSH 1-of-1 multisig address:
152+
embedded_address = self.nodes[1].getaddressinfo(self.nodes[1].getnewaddress())
153+
embedded_privkey = self.nodes[1].dumpprivkey(embedded_address["address"])
154+
p2sh_p2wsh_address = self.nodes[1].addmultisigaddress(1, [embedded_address["pubkey"]], "", "p2sh-segwit")
155+
# send transaction to P2SH-P2WSH 1-of-1 multisig address
156+
self.nodes[0].generate(101)
157+
self.nodes[0].sendtoaddress(p2sh_p2wsh_address["address"], 49.999)
158+
self.nodes[0].generate(1)
159+
self.sync_all()
160+
# Find the UTXO for the transaction node[1] should have received, check witnessScript matches
161+
unspent_output = self.nodes[1].listunspent(0, 999999, [p2sh_p2wsh_address["address"]])[0]
162+
assert_equal(unspent_output["witnessScript"], p2sh_p2wsh_address["redeemScript"])
163+
p2sh_redeemScript = CScript([OP_0, sha256(hex_str_to_bytes(p2sh_p2wsh_address["redeemScript"]))])
164+
assert_equal(unspent_output["redeemScript"], bytes_to_hex_str(p2sh_redeemScript))
165+
# Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys
166+
spending_tx = self.nodes[0].createrawtransaction([unspent_output], {self.nodes[1].getnewaddress(): Decimal("49.998")})
167+
spending_tx_signed = self.nodes[0].signrawtransactionwithkey(spending_tx, [embedded_privkey], [unspent_output])
168+
# Check the signing completed successfully
169+
assert 'complete' in spending_tx_signed
170+
assert_equal(spending_tx_signed['complete'], True)
171+
146172
def run_test(self):
147173
self.successful_signing_test()
148174
self.script_verification_error_test()
175+
self.witness_script_test()
149176
self.test_with_lock_outputs()
150177

151178

0 commit comments

Comments
 (0)