Skip to content

Commit e7ca8af

Browse files
committed
Merge bitcoin/bitcoin#25782: test: check that verifymessage RPC fails for non-P2PKH addresses
68006c1 test: check that `verifymessage` RPC fails for non-P2PKH addresses (Sebastian Falbesoner) Pull request description: This PR adds missing test coverage for the `verifymessage` RPC, for the case that a non-P2PKH (but otherwise valid) address is passed: https://github.com/bitcoin/bitcoin/blob/e09ad284c762a79d59417389e9056c18e25d9770/src/util/message.cpp#L38-L40 https://github.com/bitcoin/bitcoin/blob/e09ad284c762a79d59417389e9056c18e25d9770/src/rpc/signmessage.cpp#L48-L49 The passed addresses to trigger the error are of the types nested segwit (P2SH-P2WPKH) and native segwit (P2WPKH) and are created with a helper function `addresses_from_privkey` using descriptors and the `deriveaddresses` RPC. At some point in the future, if we have BIP322 support, all those will likely succeed and can then be moved from error-throwing to the succedding assert loop. ACKs for top commit: achow101: ACK 68006c1 w0xlt: ACK bitcoin/bitcoin@68006c1 Tree-SHA512: fec4ed97460787c2ef3d04e3fce89c9365c87207c8358b59c41890f3738355c002e64f289ab4aef794ef4dfd5c867be8b67d736fb620489204f2c6bfb8d3363c
2 parents 9ff6adc + 68006c1 commit e7ca8af

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

test/functional/rpc_signmessagewithprivkey.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,44 @@
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+
)
710
from test_framework.test_framework import BitcoinTestFramework
811
from test_framework.util import (
912
assert_equal,
1013
assert_raises_rpc_error,
1114
)
1215

16+
1317
class 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+
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+
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 = 'cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N'
23-
address = 'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB'
2433
expected_signature = 'INbVnW4e6PeRmsv2Qgu8NuopvrVjkcxob+sX8OcZG0SALhWybUjzMLPdAsXI46YZGb0KQTRii+wWIQzRpG/U+S0='
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], '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)
2845

2946
self.log.info('test parameter validity and error codes')
3047
# signmessagewithprivkey has two required parameters
@@ -41,5 +58,6 @@ def run_test(self):
4158
# malformed signature provided
4259
assert_raises_rpc_error(-3, "Malformed base64 encoding", self.nodes[0].verifymessage, 'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB', "invalid_sig", message)
4360

61+
4462
if __name__ == '__main__':
4563
SignMessagesWithPrivTest().main()

0 commit comments

Comments
 (0)