4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
"""Test RPC commands for signing messages with private key."""
6
6
7
+ from test_framework .descriptors import (
8
+ descsum_create ,
9
+ )
7
10
from test_framework .test_framework import BitcoinTestFramework
8
11
from test_framework .util import (
9
12
assert_equal ,
10
13
assert_raises_rpc_error ,
11
14
)
12
15
16
+
13
17
class SignMessagesWithPrivTest (BitcoinTestFramework ):
14
18
def set_test_params (self ):
15
19
self .setup_clean_chain = True
16
20
self .num_nodes = 1
17
21
22
+ def addresses_from_privkey (self , priv_key ):
23
+ '''Return addresses for a given WIF private key in legacy (P2PKH),
24
+ nested segwit (P2SH-P2WPKH) and native segwit (P2WPKH) formats.'''
25
+ descriptors = f'pkh({ priv_key } )' , f'sh(wpkh({ priv_key } ))' , f'wpkh({ priv_key } )'
26
+ return [self .nodes [0 ].deriveaddresses (descsum_create (desc ))[0 ] for desc in descriptors ]
27
+
18
28
def run_test (self ):
19
29
message = 'This is just a test message'
20
30
21
31
self .log .info ('test signing with priv_key' )
22
32
priv_key = 'cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N'
23
- address = 'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB'
24
33
expected_signature = 'INbVnW4e6PeRmsv2Qgu8NuopvrVjkcxob+sX8OcZG0SALhWybUjzMLPdAsXI46YZGb0KQTRii+wWIQzRpG/U+S0='
25
34
signature = self .nodes [0 ].signmessagewithprivkey (priv_key , message )
26
35
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 ], 'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB' )
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 )
28
45
29
46
self .log .info ('test parameter validity and error codes' )
30
47
# signmessagewithprivkey has two required parameters
@@ -41,5 +58,6 @@ def run_test(self):
41
58
# malformed signature provided
42
59
assert_raises_rpc_error (- 3 , "Malformed base64 encoding" , self .nodes [0 ].verifymessage , 'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB' , "invalid_sig" , message )
43
60
61
+
44
62
if __name__ == '__main__' :
45
63
SignMessagesWithPrivTest ().main ()
0 commit comments