LCOV - code coverage report
Current view: top level - src/wallet/rpc - signmessage.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 90.0 % 40 36
Test Date: 2025-06-01 06:26:32 Functions: 100.0 % 2 2
Branches: 46.1 % 128 59

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

Generated by: LCOV version 2.0-1