Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2016-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Test RPC commands for signing messages with private key."""7from test_framework.descriptors import (8descsum_create,9)10from test_framework.test_framework import BitcoinTestFramework11from test_framework.util import (12assert_equal,13assert_raises_rpc_error,14)17class SignMessagesWithPrivTest(BitcoinTestFramework):18def set_test_params(self):19self.setup_clean_chain = True20self.num_nodes = 122def addresses_from_privkey(self, priv_key):23'''Return addresses for a given WIF private key in legacy (P2PKH),24nested segwit (P2SH-P2WPKH) and native segwit (P2WPKH) formats.'''25descriptors = f'pkh({priv_key})', f'sh(wpkh({priv_key}))', f'wpkh({priv_key})'26return [self.nodes[0].deriveaddresses(descsum_create(desc))[0] for desc in descriptors]28def run_test(self):29message = 'This is just a test message'31self.log.info('test signing with priv_key')32priv_key = 'cUeKHd5orzT3mz8P9pxyREHfsWtVfgsfDjiZZBcjUBAaGk1BTj7N'33expected_signature = 'INbVnW4e6PeRmsv2Qgu8NuopvrVjkcxob+sX8OcZG0SALhWybUjzMLPdAsXI46YZGb0KQTRii+wWIQzRpG/U+S0='34signature = self.nodes[0].signmessagewithprivkey(priv_key, message)35assert_equal(expected_signature, signature)37self.log.info('test that verifying with P2PKH address succeeds')38addresses = self.addresses_from_privkey(priv_key)39assert_equal(addresses[0], 'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB')40assert self.nodes[0].verifymessage(addresses[0], signature, message)42self.log.info('test that verifying with non-P2PKH addresses throws error')43for non_p2pkh_address in addresses[1:]:44assert_raises_rpc_error(-3, "Address does not refer to key", self.nodes[0].verifymessage, non_p2pkh_address, signature, message)46self.log.info('test parameter validity and error codes')47# signmessagewithprivkey has two required parameters48for num_params in [0, 1, 3, 4, 5]:49param_list = ["dummy"]*num_params50assert_raises_rpc_error(-1, "signmessagewithprivkey", self.nodes[0].signmessagewithprivkey, *param_list)51# verifymessage has three required parameters52for num_params in [0, 1, 2, 4, 5]:53param_list = ["dummy"]*num_params54assert_raises_rpc_error(-1, "verifymessage", self.nodes[0].verifymessage, *param_list)55# invalid key or address provided56assert_raises_rpc_error(-5, "Invalid private key", self.nodes[0].signmessagewithprivkey, "invalid_key", message)57assert_raises_rpc_error(-5, "Invalid address", self.nodes[0].verifymessage, "invalid_addr", signature, message)58# malformed signature provided59assert_raises_rpc_error(-3, "Malformed base64 encoding", self.nodes[0].verifymessage, 'mpLQjfK79b7CCV4VMJWEWAj5Mpx8Up5zxB', "invalid_sig", message)62if __name__ == '__main__':63SignMessagesWithPrivTest(__file__).main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.