LCOV - code coverage report
Current view: top level - src/rpc - signmessage.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 97.3 % 73 71
Test Date: 2025-11-01 04:07:19 Functions: 100.0 % 5 5
Branches: 50.0 % 202 101

             Branch data     Line data    Source code
       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 <common/signmessage.h>
       7                 :             : #include <key.h>
       8                 :             : #include <key_io.h>
       9                 :             : #include <rpc/protocol.h>
      10                 :             : #include <rpc/request.h>
      11                 :             : #include <rpc/server.h>
      12                 :             : #include <rpc/util.h>
      13                 :             : #include <univalue.h>
      14                 :             : 
      15                 :             : #include <string>
      16                 :             : 
      17                 :         139 : static RPCHelpMan verifymessage()
      18                 :             : {
      19                 :         139 :     return RPCHelpMan{"verifymessage",
      20         [ +  - ]:         278 :         "Verify a signed message.",
      21                 :             :         {
      22   [ +  -  +  - ]:         278 :             {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address to use for the signature."},
      23   [ +  -  +  - ]:         278 :             {"signature", RPCArg::Type::STR, RPCArg::Optional::NO, "The signature provided by the signer in base 64 encoding (see signmessage)."},
      24   [ +  -  +  - ]:         278 :             {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message that was signed."},
      25                 :             :         },
      26         [ +  - ]:         278 :         RPCResult{
      27   [ +  -  +  - ]:         278 :             RPCResult::Type::BOOL, "", "If the signature is verified or not."
      28         [ +  - ]:         278 :         },
      29                 :         139 :         RPCExamples{
      30                 :             :             "\nUnlock the wallet for 30 seconds\n"
      31   [ +  -  +  -  :         278 :             + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
             +  -  +  - ]
      32                 :         139 :             "\nCreate the signature\n"
      33   [ +  -  +  -  :         556 :             + HelpExampleCli("signmessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"my message\"") +
             +  -  +  - ]
      34                 :         139 :             "\nVerify the signature\n"
      35   [ +  -  +  -  :         556 :             + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
             +  -  +  - ]
      36                 :         139 :             "\nAs a JSON-RPC call\n"
      37   [ +  -  +  -  :         556 :             + HelpExampleRpc("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\", \"signature\", \"my message\"")
             +  -  +  - ]
      38         [ +  - ]:         139 :         },
      39                 :         139 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
      40                 :             :         {
      41   [ +  -  +  -  :          72 :             switch (MessageVerify(std::string{self.Arg<std::string_view>("address")},
                   +  - ]
      42   [ +  -  +  - ]:         144 :                                   std::string{self.Arg<std::string_view>("signature")},
      43   [ +  +  +  +  :         144 :                                   std::string{self.Arg<std::string_view>("message")})) {
                   +  - ]
      44                 :          15 :             case MessageVerificationResult::ERR_INVALID_ADDRESS:
      45   [ +  -  +  - ]:          30 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
      46                 :          10 :             case MessageVerificationResult::ERR_ADDRESS_NO_KEY:
      47   [ +  -  +  - ]:          20 :                 throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
      48                 :           2 :             case MessageVerificationResult::ERR_MALFORMED_SIGNATURE:
      49   [ +  -  +  - ]:           4 :                 throw JSONRPCError(RPC_TYPE_ERROR, "Malformed base64 encoding");
      50                 :          44 :             case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED:
      51                 :          44 :             case MessageVerificationResult::ERR_NOT_SIGNED:
      52                 :          44 :                 return false;
      53                 :           1 :             case MessageVerificationResult::OK:
      54                 :           1 :                 return true;
      55                 :             :             }
      56                 :             : 
      57                 :           0 :             return false;
      58                 :             :         },
      59   [ +  -  +  -  :         973 :     };
             +  +  -  - ]
      60   [ +  -  +  -  :         834 : }
             +  -  -  - ]
      61                 :             : 
      62                 :         941 : static RPCHelpMan signmessagewithprivkey()
      63                 :             : {
      64                 :         941 :     return RPCHelpMan{
      65                 :         941 :         "signmessagewithprivkey",
      66         [ +  - ]:        1882 :         "Sign a message with the private key of an address\n",
      67                 :             :         {
      68   [ +  -  +  - ]:        1882 :             {"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key to sign the message with."},
      69   [ +  -  +  - ]:        1882 :             {"message", RPCArg::Type::STR, RPCArg::Optional::NO, "The message to create a signature of."},
      70                 :             :         },
      71         [ +  - ]:        1882 :         RPCResult{
      72   [ +  -  +  - ]:        1882 :             RPCResult::Type::STR, "signature", "The signature of the message encoded in base 64"
      73         [ +  - ]:        1882 :         },
      74                 :         941 :         RPCExamples{
      75                 :             :             "\nCreate the signature\n"
      76   [ +  -  +  -  :        1882 :             + HelpExampleCli("signmessagewithprivkey", "\"privkey\" \"my message\"") +
             +  -  +  - ]
      77                 :         941 :             "\nVerify the signature\n"
      78   [ +  -  +  -  :        3764 :             + HelpExampleCli("verifymessage", "\"1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX\" \"signature\" \"my message\"") +
             +  -  +  - ]
      79                 :         941 :             "\nAs a JSON-RPC call\n"
      80   [ +  -  +  -  :        3764 :             + HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")
             +  -  +  - ]
      81         [ +  - ]:         941 :         },
      82                 :         941 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
      83                 :             :         {
      84         [ -  + ]:         878 :             std::string strPrivkey = request.params[0].get_str();
      85   [ +  -  +  -  :         878 :             std::string strMessage = request.params[1].get_str();
                   -  + ]
      86                 :             : 
      87         [ +  - ]:         878 :             CKey key = DecodeSecret(strPrivkey);
      88         [ +  + ]:         878 :             if (!key.IsValid()) {
      89   [ +  -  +  - ]:          44 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
      90                 :             :             }
      91                 :             : 
      92         [ +  - ]:         856 :             std::string signature;
      93                 :             : 
      94   [ +  -  -  + ]:         856 :             if (!MessageSign(key, strMessage, signature)) {
      95   [ #  #  #  # ]:           0 :                 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");
      96                 :             :             }
      97                 :             : 
      98         [ +  - ]:         856 :             return signature;
      99                 :         900 :         },
     100   [ +  -  +  -  :        5646 :     };
             +  +  -  - ]
     101   [ +  -  +  -  :        3764 : }
                   -  - ]
     102                 :             : 
     103                 :          27 : void RegisterSignMessageRPCCommands(CRPCTable& t)
     104                 :             : {
     105                 :          27 :     static const CRPCCommand commands[]{
     106         [ +  - ]:          50 :         {"util", &verifymessage},
     107         [ +  - ]:          50 :         {"util", &signmessagewithprivkey},
     108   [ +  +  +  -  :          77 :     };
          +  -  +  -  -  
                      - ]
     109         [ +  + ]:          81 :     for (const auto& c : commands) {
     110                 :          54 :         t.appendCommand(c.name, &c);
     111                 :             :     }
     112                 :          27 : }
        

Generated by: LCOV version 2.0-1