LCOV - code coverage report
Current view: top level - src/rpc - signmessage.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 94.1 % 68 64
Test Date: 2024-11-04 04:15:01 Functions: 100.0 % 5 5
Branches: 49.1 % 216 106

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

Generated by: LCOV version 2.0-1