44# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55"""Test RPC commands for signing messages with private key."""
66
7+ from test_framework .descriptors import (
8+ descsum_create ,
9+ )
710from test_framework .test_framework import BitcoinTestFramework
811from test_framework .util import (
912 assert_equal ,
1013 assert_raises_rpc_error ,
1114)
1215
16+
1317class SignMessagesWithPrivTest (BitcoinTestFramework ):
1418 def set_test_params (self ):
1519 self .setup_clean_chain = True
1620 self .num_nodes = 1
1721
22+ def addresses_from_privkey (self , priv_key ):
23+ '''Return addresses for a given WIF private key in legacy (P2PKH),
24+ and script-to-hash (P2SH) forman.'''
25+ descriptors = f'pkh({ priv_key } )' , f'sh(pkh({ priv_key } ))'
26+ return [self .nodes [0 ].deriveaddresses (descsum_create (desc ))[0 ] for desc in descriptors ]
27+
1828 def run_test (self ):
1929 message = 'This is just a test message'
2030
2131 self .log .info ('test signing with priv_key' )
2232 priv_key = 'cU4zhap7nPJAWeMFu4j6jLrfPmqakDAzy8zn8Fhb3oEevdm4e5Lc'
23- address = 'yeMpGzMj3rhtnz48XsfpB8itPHhHtgxLc3'
2433 expected_signature = 'ICzMhjIUmmXcPWy2+9nw01zQMawo+s5FIy6F7VMkL+TmIeNq1j3AMEuw075os29kh5KYLbysKkDlDD+EAqERBd4='
2534 signature = self .nodes [0 ].signmessagewithprivkey (priv_key , message )
2635 assert_equal (expected_signature , signature )
27- assert self .nodes [0 ].verifymessage (address , signature , message )
36+
37+ self .log .info ('test that verifying with P2PKH address succeeds' )
38+ addresses = self .addresses_from_privkey (priv_key )
39+ assert_equal (addresses [0 ], 'yeMpGzMj3rhtnz48XsfpB8itPHhHtgxLc3' )
40+ assert self .nodes [0 ].verifymessage (addresses [0 ], signature , message )
41+
42+ self .log .info ('test that verifying with non-P2PKH addresses throws error' )
43+ for non_p2pkh_address in addresses [1 :]:
44+ assert_raises_rpc_error (- 3 , "Address does not refer to key" , self .nodes [0 ].verifymessage , non_p2pkh_address , signature , message )
2845
2946 self .log .info ('test parameter validity and error codes' )
3047 # signmessagewithprivkey has two required parameters
@@ -42,5 +59,6 @@ def run_test(self):
4259
4360 assert_raises_rpc_error (- 3 , "Malformed base64 encoding" , self .nodes [0 ].verifymessage , 'yeMpGzMj3rhtnz48XsfpB8itPHhHtgxLc3' , "invalid_sig" , message )
4461
62+
4563if __name__ == '__main__' :
4664 SignMessagesWithPrivTest ().main ()
0 commit comments