Skip to content

Commit bca68fe

Browse files
committed
Merge ElementsProject#906: Add signrawtransactionwithkey test with amount commitments
fbdb9f3 Add signrawtransaction test for input of amountcommitment. (k-matsuzawa) Pull request description: - This test assumes a fix for ElementsProject#904. Before correction, the test will result in an error. - I splited the node network in rpc_signrawtransaction.py to do this test. - For now, I have only created one test case. Are there any other test cases that should be added? Tree-SHA512: 842eba8a673e4e7d416de54f8ef40c63aad83b4a33f21401c4fde8c7b66008ac3fc6d87334f1bac22ffa8c0186e0b531f246f1a3a7afa45faaa0ea2336f6f865
2 parents 58bf565 + fbdb9f3 commit bca68fe

File tree

1 file changed

+81
-4
lines changed

1 file changed

+81
-4
lines changed

test/functional/rpc_signrawtransaction.py

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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, bytes_to_hex_str, hex_str_to_bytes
8+
from test_framework.util import assert_equal, assert_raises_rpc_error, bytes_to_hex_str, connect_nodes_bi, hex_str_to_bytes
99
from test_framework.messages import sha256
1010
from test_framework.script import CScript, OP_0
1111

@@ -14,13 +14,20 @@
1414
class SignRawTransactionsTest(BitcoinTestFramework):
1515
def set_test_params(self):
1616
self.setup_clean_chain = True
17-
self.num_nodes = 2
17+
self.num_nodes = 3
1818
prefix_args = ["-pubkeyprefix=111", "-scriptprefix=196", "-secretprefix=239", "-extpubkeyprefix=043587CF", "-extprvkeyprefix=04358394", "-bech32_hrp=bcrt"]
19-
self.extra_args = [["-deprecatedrpc=signrawtransaction"] + prefix_args, [] + prefix_args]
19+
elements_args = ["-blindedaddresses=1", "-initialfreecoins=2100000000000000", "-con_connect_genesis_outputs=1", "-anyonecanspendaremine=1", "-txindex=1"]
20+
self.extra_args = [["-deprecatedrpc=signrawtransaction"] + prefix_args, [] + prefix_args, elements_args]
2021

2122
def skip_test_if_missing_module(self):
2223
self.skip_if_no_wallet()
2324

25+
def setup_network(self):
26+
# Start with split network:
27+
self.setup_nodes()
28+
connect_nodes_bi(self.nodes, 0, 1)
29+
self.sync_all([self.nodes[:2], self.nodes[2:]])
30+
2431
def successful_signing_test(self):
2532
"""Create and sign a valid raw transaction with one input.
2633
@@ -157,7 +164,7 @@ def witness_script_test(self):
157164
self.nodes[0].generate(101)
158165
self.nodes[0].sendtoaddress(p2sh_p2wsh_address["address"], 49.999)
159166
self.nodes[0].generate(1)
160-
self.sync_all()
167+
self.sync_all([self.nodes[:2]])
161168
# Find the UTXO for the transaction node[1] should have received, check witnessScript matches
162169
unspent_output = self.nodes[1].listunspent(0, 999999, [p2sh_p2wsh_address["address"]])[0]
163170
assert_equal(unspent_output["witnessScript"], p2sh_p2wsh_address["redeemScript"])
@@ -170,6 +177,74 @@ def witness_script_test(self):
170177
assert 'complete' in spending_tx_signed
171178
assert_equal(spending_tx_signed['complete'], True)
172179

180+
181+
def witness_blind_pubkey_test(self):
182+
"""Create and compare signatures in multiple ways for a valid raw transaction with one input.
183+
184+
Expected results:
185+
1) The transaction has a complete set of signatures
186+
2) No script verification error occurred
187+
3) The signature of signrawtransactionwithwallet and
188+
the signature of signrawtransactionwithkey are equal.
189+
4) The signature of signrawtransactionwithwallet by inputs and
190+
the signature of signrawtransactionwithwallet by utxos are equal.
191+
5) The signed transaction can broadcast."""
192+
utxo_address = self.nodes[2].getnewaddress('', 'bech32')
193+
utxo_address_info = self.nodes[2].getaddressinfo(utxo_address)
194+
uc_addr = utxo_address_info['unconfidential']
195+
utxo_address_privkey = self.nodes[2].dumpprivkey(uc_addr)
196+
utxo_script_pk = utxo_address_info['scriptPubKey']
197+
utxo_amount = 0.1
198+
utxo_txid = self.nodes[2].sendtoaddress(utxo_address, utxo_amount)
199+
self.nodes[2].generate(1)
200+
201+
tx = self.nodes[2].getrawtransaction(utxo_txid, True)
202+
vout = [v['n'] for v in tx['vout'] if 'scriptPubKey' in v and uc_addr in v['scriptPubKey'].get('addresses',[])]
203+
assert len(vout) == 1
204+
utxo_vout = vout[0]
205+
assert 'valuecommitment' in tx['vout'][utxo_vout]
206+
utxo_amountcommitment = tx["vout"][utxo_vout]['valuecommitment']
207+
assert len(utxo_amountcommitment) == 66
208+
209+
inputs = [{'txid': utxo_txid, 'vout': utxo_vout}]
210+
outputs = [{utxo_address: 0.09998}, {'fee': 0.00002}]
211+
raw_tx = self.nodes[2].createrawtransaction(inputs, outputs)
212+
raw_blindtx = self.nodes[2].blindrawtransaction(raw_tx)
213+
214+
privkeys = [utxo_address_privkey]
215+
scripts = [
216+
{'txid': utxo_txid, 'vout': utxo_vout, 'scriptPubKey': utxo_script_pk,
217+
'amountcommitment': utxo_amountcommitment},
218+
]
219+
signed_tx = self.nodes[2].signrawtransactionwithkey(raw_blindtx, privkeys, scripts)
220+
# 1) The transaction has a complete set of signatures
221+
assert signed_tx['complete']
222+
# 2) No script verification error occurred
223+
assert 'errors' not in signed_tx
224+
225+
wallet_signed_tx = self.nodes[2].signrawtransactionwithwallet(raw_blindtx, scripts)
226+
assert wallet_signed_tx['complete']
227+
assert 'errors' not in wallet_signed_tx
228+
229+
wallet_signed_tx2 = self.nodes[2].signrawtransactionwithwallet(raw_blindtx)
230+
assert wallet_signed_tx2['complete']
231+
assert 'errors' not in wallet_signed_tx2
232+
233+
# 3) The signature of signrawtransactionwithwallet and
234+
# the signature of signrawtransactionwithkey are equal.
235+
assert signed_tx['hex'] == wallet_signed_tx['hex']
236+
# 4) The signature of signrawtransactionwithwallet by inputs and
237+
# the signature of signrawtransactionwithwallet by utxos are equal.
238+
assert wallet_signed_tx['hex'] == wallet_signed_tx2['hex']
239+
240+
# 5) The signed transaction can broadcast.
241+
txid = self.nodes[2].sendrawtransaction(signed_tx['hex'])
242+
self.nodes[2].generate(1)
243+
244+
tx = self.nodes[2].getrawtransaction(txid, True)
245+
vout = [v['n'] for v in tx['vout'] if 'scriptPubKey' in v and uc_addr in v['scriptPubKey'].get('addresses',[])]
246+
assert len(vout) == 1
247+
173248
def run_test(self):
174249
self.nodes[0].set_deterministic_priv_key('2Mysp7FKKe52eoC2JmU46irt1dt58TpCvhQ', 'cTNbtVJmhx75RXomhYWSZAafuNNNKPd1cr2ZiUcAeukLNGrHWjvJ')
175250
self.nodes[0].importprivkey("cTNbtVJmhx75RXomhYWSZAafuNNNKPd1cr2ZiUcAeukLNGrHWjvJ")
@@ -179,6 +254,8 @@ def run_test(self):
179254
self.witness_script_test()
180255
self.test_with_lock_outputs()
181256

257+
self.witness_blind_pubkey_test()
258+
182259

183260
if __name__ == '__main__':
184261
SignRawTransactionsTest().main()

0 commit comments

Comments
 (0)