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// Copyright (c) 2011-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <common/signmessage.h>6#include <key_io.h>7#include <rpc/util.h>8#include <wallet/rpc/util.h>9#include <wallet/wallet.h>11#include <univalue.h>13namespace wallet {14RPCMethod signmessage()15{16return RPCMethod{17"signmessage",18"Sign a message with the private key of an address" +19HELP_REQUIRING_PASSPHRASE,20{21{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the private key."},22{"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."},23},24RPCResult{25RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"26},27RPCExamples{28"\nUnlock the wallet for 30 seconds\n"29+ HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +30"\nCreate the signature\n"31+ HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +32"\nVerify the signature\n"33+ HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +34"\nAs a JSON-RPC call\n"35+ HelpExampleRpc("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"my message\"")36},37[](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue38{39const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);40if (!pwallet) return UniValue::VNULL;42LOCK(pwallet->cs_wallet);44EnsureWalletIsUnlocked(*pwallet);46std::string strAddress = request.params[0].get_str();47std::string strMessage = request.params[1].get_str();49CTxDestination dest = DecodeDestination(strAddress);50if (!IsValidDestination(dest)) {51throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");52}54const PKHash* pkhash = std::get_if<PKHash>(&dest);55if (!pkhash) {56throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");57}59std::string signature;60SigningResult err = pwallet->SignMessage(strMessage, *pkhash, signature);61if (err == SigningResult::SIGNING_FAILED) {62throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, SigningResultString(err));63} else if (err != SigningResult::OK) {64throw JSONRPCError(RPC_WALLET_ERROR, SigningResultString(err));65}67return signature;68},69};70}71} // namespace wallet