Bitcoin Core  22.99.0
P2P Digital Currency
signmessage.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <key_io.h>
6 #include <rpc/util.h>
7 #include <util/message.h>
8 #include <wallet/rpc/util.h>
9 #include <wallet/wallet.h>
10 
11 #include <univalue.h>
12 
13 namespace wallet {
15 {
16  return RPCHelpMan{"signmessage",
17  "\nSign a message with the private key of an address" +
19  {
20  {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the private key."},
21  {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."},
22  },
23  RPCResult{
24  RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"
25  },
27  "\nUnlock the wallet for 30 seconds\n"
28  + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
29  "\nCreate the signature\n"
30  + HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
31  "\nVerify the signature\n"
32  + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
33  "\nAs a JSON-RPC call\n"
34  + HelpExampleRpc("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"my message\"")
35  },
36  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
37  {
38  const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
39  if (!pwallet) return NullUniValue;
40 
41  LOCK(pwallet->cs_wallet);
42 
43  EnsureWalletIsUnlocked(*pwallet);
44 
45  std::string strAddress = request.params[0].get_str();
46  std::string strMessage = request.params[1].get_str();
47 
48  CTxDestination dest = DecodeDestination(strAddress);
49  if (!IsValidDestination(dest)) {
50  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
51  }
52 
53  const PKHash* pkhash = std::get_if<PKHash>(&dest);
54  if (!pkhash) {
55  throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
56  }
57 
58  std::string signature;
59  SigningResult err = pwallet->SignMessage(strMessage, *pkhash, signature);
60  if (err == SigningResult::SIGNING_FAILED) {
62  } else if (err != SigningResult::OK) {
64  }
65 
66  return signature;
67  },
68  };
69 }
70 } // namespace wallet
HelpExampleCli
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:156
SigningResult::OK
@ OK
No error.
wallet.h
key_io.h
RPCHelpMan
Definition: util.h:346
NullUniValue
const UniValue NullUniValue
Definition: univalue.cpp:13
RPCArg::Optional::NO
@ NO
Required arg.
RPCArg::Type::STR
@ STR
wallet::GetWalletForJSONRPCRequest
std::shared_ptr< CWallet > GetWalletForJSONRPCRequest(const JSONRPCRequest &request)
Figures out what wallet, if any, to use for a JSONRPCRequest.
Definition: util.cpp:55
SigningResultString
std::string SigningResultString(const SigningResult res)
Definition: message.cpp:80
wallet
Definition: node.h:38
SigningResult
SigningResult
Definition: message.h:42
UniValue
Definition: univalue.h:17
wallet::HELP_REQUIRING_PASSPHRASE
const std::string HELP_REQUIRING_PASSPHRASE
Definition: util.cpp:17
util.h
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
IsValidDestination
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:332
message.h
univalue.h
RPCExamples
Definition: util.h:336
RPC_WALLET_ERROR
@ RPC_WALLET_ERROR
Wallet errors.
Definition: protocol.h:71
RPCResult::Type::STR
@ STR
HelpExampleRpc
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:174
RPC_INVALID_ADDRESS_OR_KEY
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
Definition: protocol.h:41
PKHash
Definition: standard.h:79
JSONRPCError
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:56
LOCK
#define LOCK(cs)
Definition: sync.h:226
DecodeDestination
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
Definition: key_io.cpp:281
SigningResult::SIGNING_FAILED
@ SIGNING_FAILED
JSONRPCRequest
Definition: request.h:28
RPCResult
Definition: util.h:231
RPC_TYPE_ERROR
@ RPC_TYPE_ERROR
Unexpected type was passed as parameter.
Definition: protocol.h:40
wallet::signmessage
RPCHelpMan signmessage()
Definition: signmessage.cpp:14
wallet::EnsureWalletIsUnlocked
void EnsureWalletIsUnlocked(const CWallet &wallet)
Definition: util.cpp:80