Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
rpc
signmessage.cpp
Go to the documentation of this file.
1
// Copyright (c) 2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <
common/signmessage.h
>
7
#include <
key.h
>
8
#include <
key_io.h
>
9
#include <
rpc/protocol.h
>
10
#include <
rpc/request.h
>
11
#include <
rpc/server.h
>
12
#include <rpc/util.h>
13
#include <
univalue.h
>
14
15
#include <string>
16
17
static
RPCHelpMan
verifymessage
()
18
{
19
return
RPCHelpMan
{
"verifymessage"
,
20
"Verify a signed message."
,
21
{
22
{
"address"
,
RPCArg::Type::STR
,
RPCArg::Optional::NO
,
"The bitcoin address to use for the signature."
},
23
{
"signature"
,
RPCArg::Type::STR
,
RPCArg::Optional::NO
,
"The signature provided by the signer in base 64 encoding (see signmessage)."
},
24
{
"message"
,
RPCArg::Type::STR
,
RPCArg::Optional::NO
,
"The message that was signed."
},
25
},
26
RPCResult
{
27
RPCResult::Type::BOOL
,
""
,
"If the signature is verified or not."
28
},
29
RPCExamples
{
30
"\nUnlock the wallet for 30 seconds\n"
31
+
HelpExampleCli
(
"walletpassphrase"
,
"\"mypassphrase\" 30"
) +
32
"\nCreate the signature\n"
33
+
HelpExampleCli
(
"signmessage"
,
"\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\""
) +
34
"\nVerify the signature\n"
35
+
HelpExampleCli
(
"verifymessage"
,
"\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\""
) +
36
"\nAs a JSON-RPC call\n"
37
+
HelpExampleRpc
(
"verifymessage"
,
"\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"signature\", \"my message\""
)
38
},
39
[&](
const
RPCHelpMan
& self,
const
JSONRPCRequest
& request) ->
UniValue
40
{
41
switch
(
MessageVerify
(std::string{self.
Arg
<std::string_view>(
"address"
)},
42
std::string{self.
Arg
<std::string_view>(
"signature"
)},
43
std::string{self.
Arg
<std::string_view>(
"message"
)})) {
44
case
MessageVerificationResult::ERR_INVALID_ADDRESS
:
45
throw
JSONRPCError
(
RPC_INVALID_ADDRESS_OR_KEY
,
"Invalid address"
);
46
case
MessageVerificationResult::ERR_ADDRESS_NO_KEY
:
47
throw
JSONRPCError
(
RPC_TYPE_ERROR
,
"Address does not refer to key"
);
48
case
MessageVerificationResult::ERR_MALFORMED_SIGNATURE
:
49
throw
JSONRPCError
(
RPC_TYPE_ERROR
,
"Malformed base64 encoding"
);
50
case
MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED
:
51
case
MessageVerificationResult::ERR_NOT_SIGNED
:
52
return
false
;
53
case
MessageVerificationResult::OK
:
54
return
true
;
55
}
56
57
return
false
;
58
},
59
};
60
}
61
62
static
RPCHelpMan
signmessagewithprivkey
()
63
{
64
return
RPCHelpMan
{
65
"signmessagewithprivkey"
,
66
"Sign a message with the private key of an address\n"
,
67
{
68
{
"privkey"
,
RPCArg::Type::STR
,
RPCArg::Optional::NO
,
"The private key to sign the message with."
},
69
{
"message"
,
RPCArg::Type::STR
,
RPCArg::Optional::NO
,
"The message to create a signature of."
},
70
},
71
RPCResult
{
72
RPCResult::Type::STR
,
"signature"
,
"The signature of the message encoded in base 64"
73
},
74
RPCExamples
{
75
"\nCreate the signature\n"
76
+
HelpExampleCli
(
"signmessagewithprivkey"
,
"\"privkey\" \"my message\""
) +
77
"\nVerify the signature\n"
78
+
HelpExampleCli
(
"verifymessage"
,
"\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\""
) +
79
"\nAs a JSON-RPC call\n"
80
+
HelpExampleRpc
(
"signmessagewithprivkey"
,
"\"privkey\", \"my message\""
)
81
},
82
[&](
const
RPCHelpMan
& self,
const
JSONRPCRequest
& request) ->
UniValue
83
{
84
std::string strPrivkey = request.params[0].get_str();
85
std::string strMessage = request.params[1].get_str();
86
87
CKey
key =
DecodeSecret
(strPrivkey);
88
if
(!key.
IsValid
()) {
89
throw
JSONRPCError
(
RPC_INVALID_ADDRESS_OR_KEY
,
"Invalid private key"
);
90
}
91
92
std::string signature;
93
94
if
(!
MessageSign
(key, strMessage, signature)) {
95
throw
JSONRPCError
(
RPC_INVALID_ADDRESS_OR_KEY
,
"Sign failed"
);
96
}
97
98
return
signature;
99
},
100
};
101
}
102
103
void
RegisterSignMessageRPCCommands
(
CRPCTable
&
t
)
104
{
105
static
const
CRPCCommand
commands[]{
106
{
"util"
, &
verifymessage
},
107
{
"util"
, &
signmessagewithprivkey
},
108
};
109
for
(
const
auto
& c : commands) {
110
t
.appendCommand(c.name, &c);
111
}
112
}
CKey
An encapsulated private key.
Definition
key.h:36
CKey::IsValid
bool IsValid() const
Check whether this private key is valid.
Definition
key.h:124
CRPCCommand
Definition
server.h:42
CRPCTable
RPC command dispatcher.
Definition
server.h:87
JSONRPCRequest
Definition
request.h:53
RPCHelpMan
Definition
util.h:419
RPCHelpMan::Arg
auto Arg(std::string_view key) const
Helper to get a required or default-valued request argument.
Definition
util.h:444
UniValue
Definition
univalue.h:22
MessageSign
bool MessageSign(const CKey &privkey, const std::string &message, std::string &signature)
Sign a message.
Definition
signmessage.cpp:57
MessageVerify
MessageVerificationResult MessageVerify(const std::string &address, const std::string &signature, const std::string &message)
Verify a signed message.
Definition
signmessage.cpp:26
key.h
DecodeSecret
CKey DecodeSecret(const std::string &str)
Definition
key_io.cpp:213
key_io.h
JSONRPCError
UniValue JSONRPCError(int code, const std::string &message)
Definition
request.cpp:70
request.h
protocol.h
RPC_TYPE_ERROR
@ RPC_TYPE_ERROR
Unexpected type was passed as parameter.
Definition
protocol.h:41
RPC_INVALID_ADDRESS_OR_KEY
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
Definition
protocol.h:42
RegisterSignMessageRPCCommands
void RegisterSignMessageRPCCommands(CRPCTable &t)
Definition
signmessage.cpp:103
signmessagewithprivkey
static RPCHelpMan signmessagewithprivkey()
Definition
signmessage.cpp:62
verifymessage
static RPCHelpMan verifymessage()
Definition
signmessage.cpp:17
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
server.h
signmessage.h
MessageVerificationResult::ERR_MALFORMED_SIGNATURE
@ ERR_MALFORMED_SIGNATURE
The provided signature couldn't be parsed (maybe invalid base64).
Definition
signmessage.h:31
MessageVerificationResult::ERR_INVALID_ADDRESS
@ ERR_INVALID_ADDRESS
The provided address is invalid.
Definition
signmessage.h:25
MessageVerificationResult::ERR_ADDRESS_NO_KEY
@ ERR_ADDRESS_NO_KEY
The provided address is valid but does not refer to a public key.
Definition
signmessage.h:28
MessageVerificationResult::ERR_NOT_SIGNED
@ ERR_NOT_SIGNED
The message was not signed with the private key of the provided address.
Definition
signmessage.h:37
MessageVerificationResult::OK
@ OK
The message verification was successful.
Definition
signmessage.h:40
MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED
@ ERR_PUBKEY_NOT_RECOVERED
A public key could not be recovered from the provided signature and message.
Definition
signmessage.h:34
ByteUnit::t
@ t
Definition
strencodings.h:52
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
RPCResult::Type::BOOL
@ BOOL
Definition
util.h:302
univalue.h
Generated on
for Bitcoin Core by
1.17.0