Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
14 return RPCHelpMan{
15 "signmessage",
16 "Sign a message with the private key of an address" +
18 {
20 "The bitcoin address to use for the private key."},
22 "The message to create a signature of."},
23 },
25 "The signature of the message encoded in base 64"},
27 "\nUnlock the wallet for 30 seconds\n" +
28 HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
29 "\nCreate the signature\n" +
31 "signmessage",
32 "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
33 "\nVerify the signature\n" +
34 HelpExampleCli("verifymessage",
35 "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" "
36 "\"signature\" \"my message\"") +
37 "\nAs a JSON-RPC call\n" +
39 "signmessage",
40 "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"my message\"")},
41 [&](const RPCHelpMan &self, const Config &config,
42 const JSONRPCRequest &request) -> UniValue {
43 std::shared_ptr<CWallet> const wallet =
45 if (!wallet) {
46 return NullUniValue;
47 }
48 const CWallet *const pwallet = wallet.get();
49
50 LOCK(pwallet->cs_wallet);
51
53
54 std::string strAddress = request.params[0].get_str();
55 std::string strMessage = request.params[1].get_str();
56
57 CTxDestination dest =
58 DecodeDestination(strAddress, wallet->GetChainParams());
59 if (!IsValidDestination(dest)) {
60 throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address");
61 }
62
63 const PKHash *pkhash = std::get_if<PKHash>(&dest);
64 if (!pkhash) {
66 "Address does not refer to key");
67 }
68
69 std::string signature;
71 pwallet->SignMessage(strMessage, *pkhash, signature);
75 } else if (err != SigningResult::OK) {
77 }
78
79 return signature;
80 },
81 };
82}
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition wallet.h:254
RecursiveMutex cs_wallet
Definition wallet.h:389
SigningResult SignMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig) const
Definition wallet.cpp:2094
CTxDestination DecodeDestination(const std::string &addr, const CChainParams &params)
Definition key_io.cpp:174
std::string SigningResultString(const SigningResult res)
Definition message.cpp:74
SigningResult
Definition message.h:47
@ OK
No error.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
UniValue JSONRPCError(int code, const std::string &message)
Definition request.cpp:58
@ RPC_TYPE_ERROR
Unexpected type was passed as parameter.
Definition protocol.h:40
@ RPC_WALLET_ERROR
Wallet errors Unspecified problem with wallet (key not found etc.)
Definition protocol.h:90
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
Definition protocol.h:42
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition util.cpp:150
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition util.cpp:167
RPCHelpMan signmessage()
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition standard.cpp:260
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition standard.h:85
@ NO
Required arg.
#define LOCK(cs)
Definition sync.h:306
const UniValue NullUniValue
Definition univalue.cpp:16
void EnsureWalletIsUnlocked(const CWallet *pwallet)
Definition util.cpp:94
std::shared_ptr< CWallet > GetWalletForJSONRPCRequest(const JSONRPCRequest &request)
Figures out what wallet, if any, to use for a JSONRPCRequest.
Definition util.cpp:63
const std::string HELP_REQUIRING_PASSPHRASE
Definition util.cpp:17