Inside Bitcoin
Opening the selected file…
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// Copyright (c) 2010 Satoshi Nakamoto2// Copyright (c) 2009-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.6#include <common/signmessage.h>7#include <rpc/register.h> // IWYU pragma: associated9#include <key.h>10#include <key_io.h>11#include <rpc/protocol.h>12#include <rpc/request.h>13#include <rpc/server.h>14#include <rpc/util.h>15#include <univalue.h>17#include <string>18#include <string_view>19#include <vector>21static RPCMethod verifymessage()22{23return RPCMethod{"verifymessage",24"Verify a signed message.",25{26{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the signature."},27{"signature", RPCArg::Type::STR, RPCArg::Optional::NO, "The signature provided by the signer in base 64 encoding (see signmessage)."},28{"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message that was signed."},29},30RPCResult{31RPCResult::Type::BOOL, "", "If the signature is verified or not."32},33RPCExamples{34"\nUnlock the wallet for 30 seconds\n"35+ HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +36"\nCreate the signature\n"37+ HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +38"\nVerify the signature\n"39+ HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +40"\nAs a JSON-RPC call\n"41+ HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"signature\", \"my message\"")42},43[](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue44{45switch (MessageVerify(std::string{self.Arg<std::string_view>("address")},46std::string{self.Arg<std::string_view>("signature")},47std::string{self.Arg<std::string_view>("message")})) {48case MessageVerificationResult::ERR_INVALID_ADDRESS:49throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");50case MessageVerificationResult::ERR_ADDRESS_NO_KEY:51throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");52case MessageVerificationResult::ERR_MALFORMED_SIGNATURE:53throw JSONRPCError(RPC_TYPE_ERROR, "Malformed base64 encoding");54case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED:55case MessageVerificationResult::ERR_NOT_SIGNED:56return false;57case MessageVerificationResult::OK:58return true;59}61return false;62},63};64}66static RPCMethod signmessagewithprivkey()67{68return RPCMethod{69"signmessagewithprivkey",70"Sign a message with the private key of an address\n",71{72{"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key to sign the message with."},73{"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."},74},75RPCResult{76RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"77},78RPCExamples{79"\nCreate the signature\n"80+ HelpExampleCli("signmessagewithprivkey", "\"privkey\" \"my message\"") +81"\nVerify the signature\n"82+ HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +83"\nAs a JSON-RPC call\n"84+ HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")85},86[](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue87{88std::string strPrivkey = request.params[0].get_str();89std::string strMessage = request.params[1].get_str();91CKey key = DecodeSecret(strPrivkey);92if (!key.IsValid()) {93throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");94}96std::string signature;98if (!MessageSign(key, strMessage, signature)) {99throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");100}102return signature;103},104};105}107void RegisterSignMessageRPCCommands(CRPCTable& t)108{109static const CRPCCommand commands[]{110{"util", &verifymessage},111{"util", &signmessagewithprivkey},112};113for (const auto& c : commands) {114t.appendCommand(c.name, &c);115}116}