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

Generated by: LCOV version 2.0-1