Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/wallet/external_signer_scriptpubkeyman.cpp
Line
Count
Source
1
// Copyright (c) 2020-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 <chainparams.h>
6
#include <common/args.h>
7
#include <common/system.h>
8
#include <external_signer.h>
9
#include <node/types.h>
10
#include <wallet/external_signer_scriptpubkeyman.h>
11
12
#include <iostream>
13
#include <key_io.h>
14
#include <memory>
15
#include <stdexcept>
16
#include <string>
17
#include <univalue.h>
18
#include <utility>
19
#include <vector>
20
21
using common::PSBTError;
22
23
namespace wallet {
24
bool ExternalSignerScriptPubKeyMan::SetupDescriptor(WalletBatch& batch, std::unique_ptr<Descriptor> desc)
25
16
{
26
16
    LOCK(cs_desc_man);
27
16
    assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
28
16
    assert(m_storage.IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER));
29
30
16
    int64_t creation_time = GetTime();
31
32
    // Make the descriptor
33
16
    WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
34
16
    m_wallet_descriptor = w_desc;
35
36
    // Store the descriptor
37
16
    if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
38
0
        throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
39
0
    }
40
41
    // TopUp
42
16
    TopUpWithDB(batch);
43
44
16
    m_storage.UnsetBlankWalletFlag(batch);
45
16
    return true;
46
16
}
47
48
14
 util::Result<ExternalSigner> ExternalSignerScriptPubKeyMan::GetExternalSigner() {
49
14
    const std::string command = gArgs.GetArg("-signer", "");
50
14
    if (command == "") return util::Error{Untranslated("restart bitcoind with -signer=<cmd>")};
51
14
    std::vector<ExternalSigner> signers;
52
14
    ExternalSigner::Enumerate(command, signers, Params().GetChainTypeString());
53
14
    if (signers.empty()) return util::Error{Untranslated("No external signers found")};
54
    // TODO: add fingerprint argument instead of failing in case of multiple signers.
55
13
    if (signers.size() > 1) return util::Error{Untranslated("More than one external signer found. Please connect only one at a time.")};
56
12
    return signers[0];
57
13
}
58
59
util::Result<void> ExternalSignerScriptPubKeyMan::DisplayAddress(const CTxDestination& dest, const ExternalSigner &signer) const
60
4
{
61
    // TODO: avoid the need to infer a descriptor from inside a descriptor wallet
62
4
    const CScript& scriptPubKey = GetScriptForDestination(dest);
63
4
    auto provider = GetSolvingProvider(scriptPubKey);
64
4
    auto descriptor = InferDescriptor(scriptPubKey, *provider);
65
66
4
    const UniValue& result = signer.DisplayAddress(descriptor->ToString());
67
68
4
    const UniValue& error = result.find_value("error");
69
4
    if (error.isStr()) return util::Error{strprintf(_("Signer returned error: %s"), error.getValStr())};
70
71
4
    const UniValue& ret_address = result.find_value("address");
72
4
    if (!ret_address.isStr()) return util::Error{_("Signer did not echo address")};
73
74
4
    if (ret_address.getValStr() != EncodeDestination(dest)) {
75
1
        return util::Error{strprintf(_("Signer echoed unexpected address %s"), ret_address.getValStr())};
76
1
    }
77
78
3
    return util::Result<void>();
79
4
}
80
81
// If sign is true, transaction must previously have been filled
82
std::optional<PSBTError> ExternalSignerScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, const common::PSBTFillOptions& options, int* n_signed) const
83
57
{
84
57
    if (!options.sign) {
85
32
        return DescriptorScriptPubKeyMan::FillPSBT(psbt, txdata, options, n_signed);
86
32
    }
87
88
    // Already complete if every input is now signed
89
25
    bool complete = true;
90
25
    for (const auto& input : psbt.inputs) {
91
25
        complete &= PSBTInputSigned(input);
92
25
    }
93
25
    if (complete) return {};
94
95
4
    auto signer{GetExternalSigner()};
96
4
    if (!signer) {
97
1
        LogWarning("%s", util::ErrorString(signer).original);
98
1
        return PSBTError::EXTERNAL_SIGNER_NOT_FOUND;
99
1
    }
100
101
3
    std::string failure_reason;
102
3
    if(!signer->SignTransaction(psbt, failure_reason)) {
103
0
        LogWarning("Failed to sign: %s\n", failure_reason);
104
0
        return PSBTError::EXTERNAL_SIGNER_FAILED;
105
0
    }
106
3
    if (options.finalize) FinalizePSBT(psbt); // This won't work in a multisig setup
107
3
    return {};
108
3
}
109
} // namespace wallet