5
5
"""Test transaction signing using the signrawtransaction* RPCs."""
6
6
7
7
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
9
9
from test_framework .messages import sha256
10
10
from test_framework .script import CScript , OP_0
11
11
14
14
class SignRawTransactionsTest (BitcoinTestFramework ):
15
15
def set_test_params (self ):
16
16
self .setup_clean_chain = True
17
- self .num_nodes = 2
17
+ self .num_nodes = 3
18
18
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 ]
20
21
21
22
def skip_test_if_missing_module (self ):
22
23
self .skip_if_no_wallet ()
23
24
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
+
24
31
def successful_signing_test (self ):
25
32
"""Create and sign a valid raw transaction with one input.
26
33
@@ -157,7 +164,7 @@ def witness_script_test(self):
157
164
self .nodes [0 ].generate (101 )
158
165
self .nodes [0 ].sendtoaddress (p2sh_p2wsh_address ["address" ], 49.999 )
159
166
self .nodes [0 ].generate (1 )
160
- self .sync_all ()
167
+ self .sync_all ([ self . nodes [: 2 ]] )
161
168
# Find the UTXO for the transaction node[1] should have received, check witnessScript matches
162
169
unspent_output = self .nodes [1 ].listunspent (0 , 999999 , [p2sh_p2wsh_address ["address" ]])[0 ]
163
170
assert_equal (unspent_output ["witnessScript" ], p2sh_p2wsh_address ["redeemScript" ])
@@ -170,6 +177,74 @@ def witness_script_test(self):
170
177
assert 'complete' in spending_tx_signed
171
178
assert_equal (spending_tx_signed ['complete' ], True )
172
179
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
+
173
248
def run_test (self ):
174
249
self .nodes [0 ].set_deterministic_priv_key ('2Mysp7FKKe52eoC2JmU46irt1dt58TpCvhQ' , 'cTNbtVJmhx75RXomhYWSZAafuNNNKPd1cr2ZiUcAeukLNGrHWjvJ' )
175
250
self .nodes [0 ].importprivkey ("cTNbtVJmhx75RXomhYWSZAafuNNNKPd1cr2ZiUcAeukLNGrHWjvJ" )
@@ -179,6 +254,8 @@ def run_test(self):
179
254
self .witness_script_test ()
180
255
self .test_with_lock_outputs ()
181
256
257
+ self .witness_blind_pubkey_test ()
258
+
182
259
183
260
if __name__ == '__main__' :
184
261
SignRawTransactionsTest ().main ()
0 commit comments