external_signer.cpp raw

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