Branch data Line data Source code
1 : : // Copyright (c) 2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 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 : 2186 : static RPCHelpMan verifymessage()
18 : : {
19 : 2186 : return RPCHelpMan{"verifymessage",
20 : : "Verify a signed message.",
21 : : {
22 [ + - ]: 2186 : {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the signature."},
23 [ + - ]: 2186 : {"signature", RPCArg::Type::STR, RPCArg::Optional::NO, "The signature provided by the signer in base 64 encoding (see signmessage)."},
24 [ + - ]: 2186 : {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message that was signed."},
25 : : },
26 : 0 : RPCResult{
27 : : RPCResult::Type::BOOL, "", "If the signature is verified or not."
28 [ + - + - : 4372 : },
+ - ]
29 : 2186 : RPCExamples{
30 : : "\nUnlock the wallet for 30 seconds\n"
31 [ + - + - : 4372 : + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
+ - + - ]
32 : 2186 : "\nCreate the signature\n"
33 [ + - + - : 8744 : + HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
+ - + - ]
34 : 2186 : "\nVerify the signature\n"
35 [ + - + - : 8744 : + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
+ - + - ]
36 : 2186 : "\nAs a JSON-RPC call\n"
37 [ + - + - : 8744 : + HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"signature\", \"my message\"")
+ - + - ]
38 [ + - ]: 2186 : },
39 : 13 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
40 : : {
41 : 13 : std::string strAddress = self.Arg<std::string>("address");
42 [ + - ]: 13 : std::string strSign = self.Arg<std::string>("signature");
43 [ + - ]: 13 : std::string strMessage = self.Arg<std::string>("message");
44 : :
45 [ + - + + : 13 : switch (MessageVerify(strAddress, strSign, strMessage)) {
+ + + - ]
46 : 1 : case MessageVerificationResult::ERR_INVALID_ADDRESS:
47 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
48 : 2 : case MessageVerificationResult::ERR_ADDRESS_NO_KEY:
49 [ + - + - ]: 4 : throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
50 : 1 : case MessageVerificationResult::ERR_MALFORMED_SIGNATURE:
51 [ + - + - ]: 2 : throw JSONRPCError(RPC_TYPE_ERROR, "Malformed base64 encoding");
52 : 2 : case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED:
53 : 2 : case MessageVerificationResult::ERR_NOT_SIGNED:
54 [ + - ]: 2 : return false;
55 : 7 : case MessageVerificationResult::OK:
56 [ + - ]: 7 : return true;
57 : : }
58 : :
59 [ # # ]: 0 : return false;
60 : 17 : },
61 [ + - + - : 32790 : };
+ - + - +
- + - + -
+ - + - +
- + + -
- ]
62 [ + - + - : 15302 : }
+ - + - -
- ]
63 : :
64 : 2175 : static RPCHelpMan signmessagewithprivkey()
65 : : {
66 : 2175 : return RPCHelpMan{
67 : : "signmessagewithprivkey",
68 : : "Sign a message with the private key of an address\n",
69 : : {
70 [ + - ]: 2175 : {"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key to sign the message with."},
71 [ + - ]: 2175 : {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."},
72 : : },
73 : 0 : RPCResult{
74 : : RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"
75 [ + - + - : 4350 : },
+ - ]
76 : 2175 : RPCExamples{
77 : : "\nCreate the signature\n"
78 [ + - + - : 4350 : + HelpExampleCli("signmessagewithprivkey", "\"privkey\" \"my message\"") +
+ - + - ]
79 : 2175 : "\nVerify the signature\n"
80 [ + - + - : 8700 : + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
+ - + - ]
81 : 2175 : "\nAs a JSON-RPC call\n"
82 [ + - + - : 8700 : + HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")
+ - + - ]
83 [ + - ]: 2175 : },
84 : 2 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
85 : : {
86 : 2 : std::string strPrivkey = request.params[0].get_str();
87 [ + - + - : 2 : std::string strMessage = request.params[1].get_str();
+ - ]
88 : :
89 [ + - ]: 2 : CKey key = DecodeSecret(strPrivkey);
90 [ + + ]: 2 : if (!key.IsValid()) {
91 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
92 : : }
93 : :
94 [ + - ]: 1 : std::string signature;
95 : :
96 [ + - - + ]: 1 : if (!MessageSign(key, strMessage, signature)) {
97 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");
98 : : }
99 : :
100 [ + - ]: 1 : return signature;
101 : 3 : },
102 [ + - + - : 26100 : };
+ - + - +
- + - + -
+ - + + -
- ]
103 [ + - + - : 10875 : }
+ - - - ]
104 : :
105 : 1209 : void RegisterSignMessageRPCCommands(CRPCTable& t)
106 : : {
107 : 1209 : static const CRPCCommand commands[]{
108 : : {"util", &verifymessage},
109 : : {"util", &signmessagewithprivkey},
110 [ + + + - : 1209 : };
+ - + - +
- + - -
- ]
111 [ + + ]: 3627 : for (const auto& c : commands) {
112 : 2418 : t.appendCommand(c.name, &c);
113 : : }
114 : 1209 : }
|