aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/rpc/signmessage.cpp
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2021-11-26 13:48:32 +0100
committerMarcoFalke <falke.marco@gmail.com>2021-11-26 14:50:49 +0100
commitfae239208d3676452a755f736ee5aaa17adeb493 (patch)
treec2a1d0ad1f948e4fc95baf0931aee5c4e5ebd5ea /src/wallet/rpc/signmessage.cpp
parent16d698cdcf3c4ef34bae4ccd616d0ddc4a7165d4 (diff)
downloadbitcoin-fae239208d3676452a755f736ee5aaa17adeb493.tar.xz
wallet: Split signmessage from rpcwallet
Can be reviewed with --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Diffstat (limited to 'src/wallet/rpc/signmessage.cpp')
-rw-r--r--src/wallet/rpc/signmessage.cpp68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/wallet/rpc/signmessage.cpp b/src/wallet/rpc/signmessage.cpp
new file mode 100644
index 0000000000..275eb2f1f7
--- /dev/null
+++ b/src/wallet/rpc/signmessage.cpp
@@ -0,0 +1,68 @@
+// Copyright (c) 2011-2021 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include <key_io.h>
+#include <rpc/util.h>
+#include <util/message.h>
+#include <wallet/rpcwallet.h>
+#include <wallet/wallet.h>
+
+#include <univalue.h>
+
+RPCHelpMan signmessage()
+{
+ return RPCHelpMan{"signmessage",
+ "\nSign a message with the private key of an address" +
+ HELP_REQUIRING_PASSPHRASE,
+ {
+ {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the private key."},
+ {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."},
+ },
+ RPCResult{
+ RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"
+ },
+ RPCExamples{
+ "\nUnlock the wallet for 30 seconds\n"
+ + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
+ "\nCreate the signature\n"
+ + HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
+ "\nVerify the signature\n"
+ + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
+ "\nAs a JSON-RPC call\n"
+ + HelpExampleRpc("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"my message\"")
+ },
+ [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
+ {
+ const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
+ if (!pwallet) return NullUniValue;
+
+ LOCK(pwallet->cs_wallet);
+
+ EnsureWalletIsUnlocked(*pwallet);
+
+ std::string strAddress = request.params[0].get_str();
+ std::string strMessage = request.params[1].get_str();
+
+ CTxDestination dest = DecodeDestination(strAddress);
+ if (!IsValidDestination(dest)) {
+ throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
+ }
+
+ const PKHash* pkhash = std::get_if<PKHash>(&dest);
+ if (!pkhash) {
+ throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
+ }
+
+ std::string signature;
+ SigningResult err = pwallet->SignMessage(strMessage, *pkhash, signature);
+ if (err == SigningResult::SIGNING_FAILED) {
+ throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, SigningResultString(err));
+ } else if (err != SigningResult::OK) {
+ throw JSONRPCError(RPC_WALLET_ERROR, SigningResultString(err));
+ }
+
+ return signature;
+ },
+ };
+}