LCOV - code coverage report
Current view: top level - src/wallet/rpc - signmessage.cpp (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 14 35 40.0 %
Date: 2026-06-25 07:23:51 Functions: 4 5 80.0 %

          Line data    Source code
       1             : // Copyright (c) 2011-2021 The Bitcoin Core developers
       2             : // Distributed under the MIT software license, see the accompanying
       3             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4             : 
       5             : #include <key_io.h>
       6             : #include <rpc/util.h>
       7             : #include <util/message.h>
       8             : #include <wallet/rpc/util.h>
       9             : #include <wallet/wallet.h>
      10             : 
      11             : #include <univalue.h>
      12             : 
      13             : namespace wallet {
      14           8 : RPCHelpMan signmessage()
      15             : {
      16          16 :     return RPCHelpMan{"signmessage",
      17           8 :         "\nSign a message with the private key of an address" +
      18             :           HELP_REQUIRING_PASSPHRASE,
      19          24 :         {
      20           8 :             {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The Dash address to use for the private key."},
      21           8 :             {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."},
      22             :         },
      23           8 :         RPCResult{
      24           8 :             RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"
      25             :         },
      26           8 :         RPCExamples{
      27             :             "\nUnlock the wallet for 30 seconds\n"
      28           8 :             + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
      29             :             "\nCreate the signature\n"
      30           8 :             + HelpExampleCli("signmessage", "\"" + EXAMPLE_ADDRESS[0] + "\" \"my message\"") +
      31             :             "\nVerify the signature\n"
      32           8 :             + HelpExampleCli("verifymessage", "\"" + EXAMPLE_ADDRESS[0] + "\" \"signature\" \"my message\"") +
      33             :             "\nAs a JSON-RPC call\n"
      34           8 :             + HelpExampleRpc("signmessage", "\"" + EXAMPLE_ADDRESS[0] + "\", \"my message\"")
      35             :         },
      36           8 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
      37             :         {
      38           0 :             const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
      39           0 :             if (!pwallet) return UniValue::VNULL;
      40             : 
      41           0 :             LOCK(pwallet->cs_wallet);
      42             : 
      43           0 :             EnsureWalletIsUnlocked(*pwallet);
      44             : 
      45           0 :             std::string strAddress = request.params[0].get_str();
      46           0 :             std::string strMessage = request.params[1].get_str();
      47             : 
      48           0 :             CTxDestination dest = DecodeDestination(strAddress);
      49           0 :             if (!IsValidDestination(dest)) {
      50           0 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
      51             :             }
      52             : 
      53           0 :             const PKHash* pkhash = std::get_if<PKHash>(&dest);
      54           0 :             if (!pkhash) {
      55           0 :                 throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
      56             :             }
      57             : 
      58           0 :             std::string signature;
      59           0 :             SigningResult err = pwallet->SignMessage(strMessage, *pkhash, signature);
      60           0 :             if (err == SigningResult::SIGNING_FAILED) {
      61           0 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, SigningResultString(err));
      62           0 :             } else if (err != SigningResult::OK) {
      63           0 :                 throw JSONRPCError(RPC_WALLET_ERROR, SigningResultString(err));
      64             :             }
      65             : 
      66           0 :             return signature;
      67           0 :         },
      68             :     };
      69           0 : }
      70             : } // namespace wallet

Generated by: LCOV version 1.16