LCOV - code coverage report
Current view: top level - src - external_signer.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 86.2 % 65 56
Test Date: 2026-02-22 05:32:45 Functions: 100.0 % 7 7
Branches: 42.5 % 240 102

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2018-present 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 <external_signer.h>
       6                 :             : 
       7                 :             : #include <chainparams.h>
       8                 :             : #include <common/run_command.h>
       9                 :             : #include <core_io.h>
      10                 :             : #include <psbt.h>
      11                 :             : #include <util/strencodings.h>
      12                 :             : #include <util/subprocess.h>
      13                 :             : 
      14                 :             : #include <algorithm>
      15                 :             : #include <stdexcept>
      16                 :             : #include <string>
      17                 :             : #include <vector>
      18                 :             : 
      19                 :          13 : ExternalSigner::ExternalSigner(std::vector<std::string> command, std::string chain, std::string fingerprint, std::string name)
      20                 :          13 :     : m_command{std::move(command)}, m_chain{std::move(chain)}, m_fingerprint{std::move(fingerprint)}, m_name{std::move(name)} {}
      21                 :             : 
      22                 :          10 : std::vector<std::string> ExternalSigner::NetworkArg() const
      23                 :             : {
      24   [ -  +  +  +  :          40 :     return {"--chain", m_chain};
                   -  - ]
      25   [ +  -  -  +  :          20 : }
                   -  - ]
      26                 :             : 
      27                 :          18 : bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, const std::string& chain)
      28                 :             : {
      29                 :             :     // Call <command> enumerate
      30   [ +  -  +  -  :          18 :     std::vector<std::string> cmd_args = Cat(subprocess::util::split(command), {"enumerate"});
             +  -  +  - ]
      31                 :             : 
      32   [ +  -  +  + ]:          22 :     const UniValue result = RunCommandParseJSON(cmd_args, "");
      33         [ -  + ]:          14 :     if (!result.isArray()) {
      34   [ #  #  #  # ]:           0 :         throw std::runtime_error(strprintf("'%s' received invalid response, expected array of signers", command));
      35                 :             :     }
      36   [ +  -  +  + ]:          27 :     for (const UniValue& signer : result.getValues()) {
      37                 :             :         // Check for error
      38         [ +  - ]:          14 :         const UniValue& error = signer.find_value("error");
      39         [ +  + ]:          14 :         if (!error.isNull()) {
      40         [ -  + ]:           1 :             if (!error.isStr()) {
      41   [ #  #  #  # ]:           0 :                 throw std::runtime_error(strprintf("'%s' error", command));
      42                 :             :             }
      43   [ +  -  +  - ]:           2 :             throw std::runtime_error(strprintf("'%s' error: %s", command, error.getValStr()));
      44                 :             :         }
      45                 :             :         // Check if fingerprint is present
      46         [ +  - ]:          13 :         const UniValue& fingerprint = signer.find_value("fingerprint");
      47         [ -  + ]:          13 :         if (fingerprint.isNull()) {
      48   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("'%s' received invalid response, missing signer fingerprint", command));
      49                 :             :         }
      50         [ +  - ]:          13 :         const std::string& fingerprintStr{fingerprint.get_str()};
      51                 :             :         // Skip duplicate signer
      52                 :          13 :         bool duplicate = false;
      53         [ +  + ]:          14 :         for (const ExternalSigner& signer : signers) {
      54         [ -  + ]:           1 :             if (signer.m_fingerprint.compare(fingerprintStr) == 0) duplicate = true;
      55                 :             :         }
      56         [ +  - ]:          13 :         if (duplicate) break;
      57         [ +  - ]:          13 :         std::string name;
      58         [ +  - ]:          13 :         const UniValue& model_field = signer.find_value("model");
      59   [ +  -  +  - ]:          13 :         if (model_field.isStr() && model_field.getValStr() != "") {
      60         [ -  + ]:          13 :             name += model_field.getValStr();
      61                 :             :         }
      62   [ +  -  +  -  :          13 :         signers.emplace_back(subprocess::util::split(command), chain, fingerprintStr, name);
                   +  - ]
      63                 :          13 :     }
      64                 :          13 :     return true;
      65                 :          19 : }
      66                 :             : 
      67                 :           4 : UniValue ExternalSigner::DisplayAddress(const std::string& descriptor) const
      68                 :             : {
      69   [ +  -  -  +  :          40 :     return RunCommandParseJSON(Cat(m_command, Cat(Cat({"--fingerprint", m_fingerprint}, NetworkArg()), {"displayaddress", "--desc", descriptor})), "");
          +  -  +  -  +  
          -  +  -  +  -  
          +  +  +  +  -  
                -  -  - ]
      70   [ +  -  +  -  :          16 : }
          -  +  +  -  +  
          -  +  -  -  -  
                   -  - ]
      71                 :             : 
      72                 :           3 : UniValue ExternalSigner::GetDescriptors(const int account)
      73                 :             : {
      74   [ +  -  +  -  :          30 :     return RunCommandParseJSON(Cat(m_command, Cat(Cat({"--fingerprint", m_fingerprint}, NetworkArg()), {"getdescriptors", "--account", strprintf("%d", account)})), "");
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  +  +  +  
             +  -  -  -  
                      - ]
      75   [ +  -  +  -  :           9 : }
          +  -  +  -  +  
             -  -  -  -  
                      - ]
      76                 :             : 
      77                 :           3 : bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::string& error)
      78                 :             : {
      79                 :             :     // Serialize the PSBT
      80                 :           3 :     DataStream ssTx{};
      81         [ +  - ]:           3 :     ssTx << psbtx;
      82                 :             :     // parse ExternalSigner master fingerprint
      83   [ -  +  +  - ]:           3 :     std::vector<unsigned char> parsed_m_fingerprint = ParseHex(m_fingerprint);
      84                 :             :     // Check if signer fingerprint matches any input master key fingerprint
      85                 :           6 :     auto matches_signer_fingerprint = [&](const PSBTInput& input) {
      86         [ -  + ]:           3 :         for (const auto& entry : input.hd_keypaths) {
      87         [ #  # ]:           0 :             if (std::ranges::equal(parsed_m_fingerprint, entry.second.fingerprint)) return true;
      88                 :             :         }
      89         [ +  - ]:           3 :         for (const auto& entry : input.m_tap_bip32_paths) {
      90         [ -  + ]:           3 :             if (std::ranges::equal(parsed_m_fingerprint, entry.second.second.fingerprint)) return true;
      91                 :             :         }
      92                 :             :         return false;
      93                 :           3 :     };
      94                 :             : 
      95         [ -  + ]:           3 :     if (!std::any_of(psbtx.inputs.begin(), psbtx.inputs.end(), matches_signer_fingerprint)) {
      96   [ #  #  #  #  :           0 :         error = "Signer fingerprint " + m_fingerprint + " does not match any of the inputs:\n" + EncodeBase64(ssTx.str());
             #  #  #  # ]
      97                 :           0 :         return false;
      98                 :             :     }
      99                 :             : 
     100   [ +  -  -  +  :          18 :     const std::vector<std::string> command = Cat(m_command, Cat({"--stdin", "--fingerprint", m_fingerprint}, NetworkArg()));
          +  -  +  -  +  
             -  +  +  -  
                      - ]
     101   [ +  -  +  -  :           9 :     const std::string stdinStr = "signtx " + EncodeBase64(ssTx.str());
                   +  - ]
     102                 :             : 
     103         [ +  - ]:           3 :     const UniValue signer_result = RunCommandParseJSON(command, stdinStr);
     104                 :             : 
     105   [ +  -  -  + ]:           3 :     if (signer_result.find_value("error").isStr()) {
     106   [ #  #  #  #  :           0 :         error = signer_result.find_value("error").get_str();
                   #  # ]
     107                 :             :         return false;
     108                 :             :     }
     109                 :             : 
     110   [ +  -  -  + ]:           3 :     if (!signer_result.find_value("psbt").isStr()) {
     111         [ -  - ]:           3 :         error = "Unexpected result from signer";
     112                 :             :         return false;
     113                 :             :     }
     114                 :             : 
     115                 :           3 :     PartiallySignedTransaction signer_psbtx;
     116         [ +  - ]:           3 :     std::string signer_psbt_error;
     117   [ +  -  +  -  :           3 :     if (!DecodeBase64PSBT(signer_psbtx, signer_result.find_value("psbt").get_str(), signer_psbt_error)) {
             +  -  -  + ]
     118         [ #  # ]:           0 :         error = strprintf("TX decode failed %s", signer_psbt_error);
     119                 :           0 :         return false;
     120                 :             :     }
     121                 :             : 
     122         [ +  - ]:           3 :     psbtx = signer_psbtx;
     123                 :             : 
     124                 :             :     return true;
     125   [ +  -  +  -  :          12 : }
             +  -  -  - ]
        

Generated by: LCOV version 2.0-1