Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2018-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <external_signer.h>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>14#include <algorithm>15#include <stdexcept>16#include <string>17#include <vector>19ExternalSigner::ExternalSigner(std::vector<std::string> command, std::string chain, std::string fingerprint, std::string name)20: m_command{std::move(command)}, m_chain{std::move(chain)}, m_fingerprint{std::move(fingerprint)}, m_name{std::move(name)} {}22std::vector<std::string> ExternalSigner::NetworkArg() const23{24return {"--chain", m_chain};25}27bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, const std::string& chain)28{29// Call <command> enumerate30std::vector<std::string> cmd_args = Cat(subprocess::util::split(command), {"enumerate"});32const UniValue result = RunCommandParseJSON(cmd_args, "");33if (!result.isArray()) {34throw std::runtime_error(strprintf("'%s' received invalid response, expected array of signers", command));35}36for (const UniValue& signer : result.getValues()) {37// Check for error38const UniValue& error = signer.find_value("error");39if (!error.isNull()) {40if (!error.isStr()) {41throw std::runtime_error(strprintf("'%s' error", command));42}43throw std::runtime_error(strprintf("'%s' error: %s", command, error.getValStr()));44}45// Check if fingerprint is present46const UniValue& fingerprint = signer.find_value("fingerprint");47if (fingerprint.isNull()) {48throw std::runtime_error(strprintf("'%s' received invalid response, missing signer fingerprint", command));49}50const std::string& fingerprintStr{fingerprint.get_str()};51if (fingerprintStr.size() != 8 || !IsHex(fingerprintStr)) {52throw std::runtime_error(strprintf("'%s' received invalid fingerprint, must be 8 hex characters", command));53}54// Skip duplicate signer55bool duplicate = false;56for (const ExternalSigner& signer : signers) {57if (signer.m_fingerprint.compare(fingerprintStr) == 0) duplicate = true;58}59if (duplicate) continue;60std::string name;61const UniValue& model_field = signer.find_value("model");62if (model_field.isStr() && model_field.getValStr() != "") {63name += model_field.getValStr();64}65signers.emplace_back(subprocess::util::split(command), chain, fingerprintStr, name);66}67return true;68}70UniValue ExternalSigner::DisplayAddress(const std::string& descriptor) const71{72return RunCommandParseJSON(Cat(m_command, Cat(Cat({"--fingerprint", m_fingerprint}, NetworkArg()), {"displayaddress", "--desc", descriptor})), "");73}75UniValue ExternalSigner::GetDescriptors(const int account)76{77return RunCommandParseJSON(Cat(m_command, Cat(Cat({"--fingerprint", m_fingerprint}, NetworkArg()), {"getdescriptors", "--account", strprintf("%d", account)})), "");78}80bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::string& error)81{82// Serialize the PSBT83DataStream ssTx{};84ssTx << psbtx;85// parse ExternalSigner master fingerprint86std::vector<unsigned char> parsed_m_fingerprint = ParseHex(m_fingerprint);87// Check if signer fingerprint matches any input master key fingerprint88auto matches_signer_fingerprint = [&](const PSBTInput& input) {89for (const auto& entry : input.hd_keypaths) {90if (std::ranges::equal(parsed_m_fingerprint, entry.second.fingerprint)) return true;91}92for (const auto& entry : input.m_tap_bip32_paths) {93if (std::ranges::equal(parsed_m_fingerprint, entry.second.second.fingerprint)) return true;94}95return false;96};98if (!std::any_of(psbtx.inputs.begin(), psbtx.inputs.end(), matches_signer_fingerprint)) {99error = "Signer fingerprint " + m_fingerprint + " does not match any of the inputs:\n" + EncodeBase64(ssTx.str());100return false;101}103const std::vector<std::string> command = Cat(m_command, Cat({"--stdin", "--fingerprint", m_fingerprint}, NetworkArg()));104const std::string stdinStr = "signtx " + EncodeBase64(ssTx.str());106const UniValue signer_result = RunCommandParseJSON(command, stdinStr);108if (signer_result.find_value("error").isStr()) {109error = signer_result.find_value("error").get_str();110return false;111}113if (!signer_result.find_value("psbt").isStr()) {114error = "Unexpected result from signer";115return false;116}118util::Result<PartiallySignedTransaction> signer_psbtx = DecodeBase64PSBT(signer_result.find_value("psbt").get_str());119if (!signer_psbtx) {120error = strprintf("TX decode failed %s", util::ErrorString(signer_psbtx).original);121return false;122}124psbtx = *signer_psbtx;126return true;127}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.