LCOV - code coverage report
Current view: top level - src/rpc - signmessage.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 65.8 % 73 48
Test Date: 2026-10-11 07:59:32 Functions: 100.0 % 5 5
Branches: 35.6 % 202 72

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

Generated by: LCOV version 2.0-1