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