Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
wallet
rpc
signmessage.cpp
Go to the documentation of this file.
1
// Copyright (c) 2011-present 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 <
common/signmessage.h
>
6
#include <
key_io.h
>
7
#include <rpc/util.h>
8
#include <
wallet/rpc/util.h
>
9
#include <
wallet/wallet.h
>
10
11
#include <
univalue.h
>
12
13
namespace
wallet
{
14
RPCHelpMan
signmessage
()
15
{
16
return
RPCHelpMan
{
17
"signmessage"
,
18
"Sign a message with the private key of an address"
+
19
HELP_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
},
24
RPCResult
{
25
RPCResult::Type::STR
,
"signature"
,
"The signature of the message encoded in base 64"
26
},
27
RPCExamples
{
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
RPCHelpMan
& self,
const
JSONRPCRequest
& request) ->
UniValue
38
{
39
const
std::shared_ptr<const CWallet> pwallet =
GetWalletForJSONRPCRequest
(request);
40
if
(!pwallet)
return
UniValue::VNULL
;
41
42
LOCK
(pwallet->cs_wallet);
43
44
EnsureWalletIsUnlocked
(*pwallet);
45
46
std::string strAddress = request.params[0].get_str();
47
std::string strMessage = request.params[1].get_str();
48
49
CTxDestination
dest =
DecodeDestination
(strAddress);
50
if
(!
IsValidDestination
(dest)) {
51
throw
JSONRPCError
(
RPC_INVALID_ADDRESS_OR_KEY
,
"Invalid address"
);
52
}
53
54
const
PKHash
* pkhash = std::get_if<PKHash>(&dest);
55
if
(!pkhash) {
56
throw
JSONRPCError
(
RPC_TYPE_ERROR
,
"Address does not refer to key"
);
57
}
58
59
std::string signature;
60
SigningResult
err = pwallet->SignMessage(strMessage, *pkhash, signature);
61
if
(err ==
SigningResult::SIGNING_FAILED
) {
62
throw
JSONRPCError
(
RPC_INVALID_ADDRESS_OR_KEY
,
SigningResultString
(err));
63
}
else
if
(err !=
SigningResult::OK
) {
64
throw
JSONRPCError
(
RPC_WALLET_ERROR
,
SigningResultString
(err));
65
}
66
67
return
signature;
68
},
69
};
70
}
71
}
// namespace wallet
IsValidDestination
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination corresponds to one with an address.
Definition
addresstype.cpp:171
CTxDestination
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition
addresstype.h:143
JSONRPCRequest
Definition
request.h:53
RPCHelpMan
Definition
util.h:419
UniValue
Definition
univalue.h:22
UniValue::VNULL
@ VNULL
Definition
univalue.h:24
SigningResultString
std::string SigningResultString(const SigningResult res)
Definition
signmessage.cpp:81
DecodeDestination
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
Definition
key_io.cpp:299
key_io.h
wallet
Definition
wallet_balance.cpp:26
wallet::GetWalletForJSONRPCRequest
std::shared_ptr< CWallet > GetWalletForJSONRPCRequest(const JSONRPCRequest &request)
Figures out what wallet, if any, to use for a JSONRPCRequest.
Definition
util.cpp:64
wallet::EnsureWalletIsUnlocked
void EnsureWalletIsUnlocked(const CWallet &wallet)
Definition
util.cpp:88
wallet::HELP_REQUIRING_PASSPHRASE
const std::string HELP_REQUIRING_PASSPHRASE
Definition
util.cpp:20
wallet::signmessage
RPCHelpMan signmessage()
Definition
signmessage.cpp:14
JSONRPCError
UniValue JSONRPCError(int code, const std::string &message)
Definition
request.cpp:70
RPC_TYPE_ERROR
@ RPC_TYPE_ERROR
Unexpected type was passed as parameter.
Definition
protocol.h:41
RPC_WALLET_ERROR
@ RPC_WALLET_ERROR
Wallet errors.
Definition
protocol.h:71
RPC_INVALID_ADDRESS_OR_KEY
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
Definition
protocol.h:42
HelpExampleCli
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition
util.cpp:183
HelpExampleRpc
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition
util.cpp:201
signmessage.h
SigningResult
SigningResult
Definition
signmessage.h:43
SigningResult::SIGNING_FAILED
@ SIGNING_FAILED
Definition
signmessage.h:46
SigningResult::OK
@ OK
No error.
Definition
signmessage.h:44
PKHash
Definition
addresstype.h:48
RPCArg::Type::STR
@ STR
Definition
util.h:190
RPCArg::Optional::NO
@ NO
Required arg.
Definition
util.h:208
RPCExamples
Definition
util.h:408
RPCResult
Definition
util.h:296
RPCResult::Type::STR
@ STR
Definition
util.h:300
LOCK
#define LOCK(cs)
Definition
sync.h:258
univalue.h
util.h
wallet.h
Generated on
for Bitcoin Core by
1.17.0