Skip to content

Commit ad94a30

Browse files
committed
merge bitcoin#25013: Remove cs_main from verifymessage, move msg utils to new file
1 parent 8e42b12 commit ad94a30

File tree

4 files changed

+118
-97
lines changed

4 files changed

+118
-97
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ libbitcoin_server_a_SOURCES = \
540540
rpc/rawtransaction.cpp \
541541
rpc/server.cpp \
542542
rpc/server_util.cpp \
543+
rpc/signmessage.cpp \
543544
rpc/txoutproof.cpp \
544545
script/sigcache.cpp \
545546
shutdown.cpp \

src/rpc/misc.cpp

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020
#include <key_io.h>
2121
#include <net.h>
2222
#include <node/context.h>
23-
#include <rpc/blockchain.h>
2423
#include <rpc/index_util.h>
2524
#include <rpc/server.h>
2625
#include <rpc/server_util.h>
2726
#include <rpc/util.h>
2827
#include <scheduler.h>
2928
#include <script/descriptor.h>
3029
#include <txmempool.h>
30+
#include <univalue.h>
3131
#include <util/check.h>
32-
#include <util/message.h> // For MessageSign(), MessageVerify()
3332
#include <util/strencodings.h>
3433
#include <util/system.h>
3534
#include <validation.h>
@@ -42,8 +41,6 @@
4241
#include <malloc.h>
4342
#endif
4443

45-
#include <univalue.h>
46-
4744
static RPCHelpMan debug()
4845
{
4946
return RPCHelpMan{"debug",
@@ -475,97 +472,6 @@ static RPCHelpMan deriveaddresses()
475472
};
476473
}
477474

478-
static RPCHelpMan verifymessage()
479-
{
480-
return RPCHelpMan{"verifymessage",
481-
"\nVerify a signed message\n",
482-
{
483-
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The Dash address to use for the signature."},
484-
{"signature", RPCArg::Type::STR, RPCArg::Optional::NO, "The signature provided by the signer in base 64 encoding (see signmessage)."},
485-
{"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message that was signed."},
486-
},
487-
RPCResult{
488-
RPCResult::Type::BOOL, "", "If the signature is verified or not."
489-
},
490-
RPCExamples{
491-
"\nUnlock the wallet for 30 seconds\n"
492-
+ HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
493-
"\nCreate the signature\n"
494-
+ HelpExampleCli("signmessage", "\"" + EXAMPLE_ADDRESS[0] + "\" \"my message\"") +
495-
"\nVerify the signature\n"
496-
+ HelpExampleCli("verifymessage", "\"" + EXAMPLE_ADDRESS[0] + "\" \"signature\" \"my message\"") +
497-
"\nAs a JSON-RPC call\n"
498-
+ HelpExampleRpc("verifymessage", "\"" + EXAMPLE_ADDRESS[0] + "\", \"signature\", \"my message\"")
499-
},
500-
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
501-
{
502-
503-
LOCK(cs_main);
504-
505-
std::string strAddress = request.params[0].get_str();
506-
std::string strSign = request.params[1].get_str();
507-
std::string strMessage = request.params[2].get_str();
508-
509-
switch (MessageVerify(strAddress, strSign, strMessage)) {
510-
case MessageVerificationResult::ERR_INVALID_ADDRESS:
511-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
512-
case MessageVerificationResult::ERR_ADDRESS_NO_KEY:
513-
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
514-
case MessageVerificationResult::ERR_MALFORMED_SIGNATURE:
515-
throw JSONRPCError(RPC_TYPE_ERROR, "Malformed base64 encoding");
516-
case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED:
517-
case MessageVerificationResult::ERR_NOT_SIGNED:
518-
return false;
519-
case MessageVerificationResult::OK:
520-
return true;
521-
}
522-
523-
return false;
524-
},
525-
};
526-
}
527-
528-
static RPCHelpMan signmessagewithprivkey()
529-
{
530-
return RPCHelpMan{"signmessagewithprivkey",
531-
"\nSign a message with the private key of an address\n",
532-
{
533-
{"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key to sign the message with."},
534-
{"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."},
535-
},
536-
RPCResult{
537-
RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"
538-
},
539-
RPCExamples{
540-
"\nCreate the signature\n"
541-
+ HelpExampleCli("signmessagewithprivkey", "\"privkey\" \"my message\"") +
542-
"\nVerify the signature\n"
543-
+ HelpExampleCli("verifymessage", "\"" + EXAMPLE_ADDRESS[0] + "\" \"signature\" \"my message\"") +
544-
"\nAs a JSON-RPC call\n"
545-
+ HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")
546-
},
547-
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
548-
{
549-
550-
std::string strPrivkey = request.params[0].get_str();
551-
std::string strMessage = request.params[1].get_str();
552-
553-
CKey key = DecodeSecret(strPrivkey);
554-
if (!key.IsValid()) {
555-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
556-
}
557-
558-
std::string signature;
559-
560-
if (!MessageSign(key, strMessage, signature)) {
561-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");
562-
}
563-
564-
return signature;
565-
},
566-
};
567-
}
568-
569475
static RPCHelpMan setmocktime()
570476
{
571477
return RPCHelpMan{"setmocktime",
@@ -1512,8 +1418,6 @@ static const CRPCCommand commands[] =
15121418
{ "util", &createmultisig, },
15131419
{ "util", &deriveaddresses, },
15141420
{ "util", &getdescriptorinfo, },
1515-
{ "util", &verifymessage, },
1516-
{ "util", &signmessagewithprivkey, },
15171421
{ "util", &getindexinfo, },
15181422
{ "blockchain", &getspentinfo, },
15191423

src/rpc/register.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ void RegisterNetRPCCommands(CRPCTable &tableRPC);
1616
void RegisterMiscRPCCommands(CRPCTable &tableRPC);
1717
void RegisterMiningRPCCommands(CRPCTable &tableRPC);
1818
void RegisterRawTransactionRPCCommands(CRPCTable &tableRPC);
19+
void RegisterSignMessageRPCCommands(CRPCTable&);
1920
void RegisterMasternodeRPCCommands(CRPCTable &tableRPC);
2021
void RegisterCoinJoinRPCCommands(CRPCTable &tableRPC);
2122
void RegisterGovernanceRPCCommands(CRPCTable &tableRPC);
@@ -31,6 +32,7 @@ static inline void RegisterAllCoreRPCCommands(CRPCTable &t)
3132
RegisterMiscRPCCommands(t);
3233
RegisterMiningRPCCommands(t);
3334
RegisterRawTransactionRPCCommands(t);
35+
RegisterSignMessageRPCCommands(t);
3436
RegisterMasternodeRPCCommands(t);
3537
RegisterCoinJoinRPCCommands(t);
3638
RegisterGovernanceRPCCommands(t);

src/rpc/signmessage.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
17+
static RPCHelpMan verifymessage()
18+
{
19+
return RPCHelpMan{"verifymessage",
20+
"\nVerify a signed message\n",
21+
{
22+
{"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The Dash 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", "\"" + EXAMPLE_ADDRESS[0] + "\" \"my message\"") +
34+
"\nVerify the signature\n"
35+
+ HelpExampleCli("verifymessage", "\"" + EXAMPLE_ADDRESS[0] + "\" \"signature\" \"my message\"") +
36+
"\nAs a JSON-RPC call\n"
37+
+ HelpExampleRpc("verifymessage", "\"" + EXAMPLE_ADDRESS[0] + "\", \"signature\", \"my message\"")
38+
},
39+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
40+
{
41+
std::string strAddress = request.params[0].get_str();
42+
std::string strSign = request.params[1].get_str();
43+
std::string strMessage = request.params[2].get_str();
44+
45+
switch (MessageVerify(strAddress, strSign, strMessage)) {
46+
case MessageVerificationResult::ERR_INVALID_ADDRESS:
47+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
48+
case MessageVerificationResult::ERR_ADDRESS_NO_KEY:
49+
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
50+
case MessageVerificationResult::ERR_MALFORMED_SIGNATURE:
51+
throw JSONRPCError(RPC_TYPE_ERROR, "Malformed base64 encoding");
52+
case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED:
53+
case MessageVerificationResult::ERR_NOT_SIGNED:
54+
return false;
55+
case MessageVerificationResult::OK:
56+
return true;
57+
}
58+
59+
return false;
60+
},
61+
};
62+
}
63+
64+
static RPCHelpMan signmessagewithprivkey()
65+
{
66+
return RPCHelpMan{"signmessagewithprivkey",
67+
"\nSign a message with the private key of an address\n",
68+
{
69+
{"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key to sign the message with."},
70+
{"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."},
71+
},
72+
RPCResult{
73+
RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"
74+
},
75+
RPCExamples{
76+
"\nCreate the signature\n"
77+
+ HelpExampleCli("signmessagewithprivkey", "\"privkey\" \"my message\"") +
78+
"\nVerify the signature\n"
79+
+ HelpExampleCli("verifymessage", "\"" + EXAMPLE_ADDRESS[0] + "\" \"signature\" \"my message\"") +
80+
"\nAs a JSON-RPC call\n"
81+
+ HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")
82+
},
83+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
84+
{
85+
86+
std::string strPrivkey = request.params[0].get_str();
87+
std::string strMessage = request.params[1].get_str();
88+
89+
CKey key = DecodeSecret(strPrivkey);
90+
if (!key.IsValid()) {
91+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
92+
}
93+
94+
std::string signature;
95+
96+
if (!MessageSign(key, strMessage, signature)) {
97+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");
98+
}
99+
100+
return signature;
101+
},
102+
};
103+
}
104+
105+
void RegisterSignMessageRPCCommands(CRPCTable& t)
106+
{
107+
static const CRPCCommand commands[]{
108+
{"util", &verifymessage},
109+
{"util", &signmessagewithprivkey},
110+
};
111+
for (const auto& c : commands) {
112+
t.appendCommand(c.name, &c);
113+
}
114+
}

0 commit comments

Comments
 (0)