Bitcoin Core 31.99.0
P2P Digital Currency
external_signer_scriptpubkeyman.cpp
Go to the documentation of this file.
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>
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
22
23namespace wallet {
24std::unique_ptr<ExternalSignerScriptPubKeyMan> ExternalSignerScriptPubKeyMan::LoadFromStorage(WalletStorage& storage, const uint256& id, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys)
25{
26 return std::unique_ptr<ExternalSignerScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(storage, id, descriptor, keypool_size, keys, ckeys));
27}
28
29std::unique_ptr<ExternalSignerScriptPubKeyMan> ExternalSignerScriptPubKeyMan::CreateNew(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, std::unique_ptr<Descriptor> desc)
30{
31 int64_t creation_time = GetTime();
32
33 // Make the descriptor
34 WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
35
36 auto spkm = std::unique_ptr<ExternalSignerScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(storage, w_desc, keypool_size));
37
38 LOCK(spkm->cs_desc_man);
41
42 // Store the descriptor
43 if (!batch.WriteDescriptor(spkm->GetID(), spkm->m_wallet_descriptor)) {
44 throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
45 }
46
47 // TopUp
48 spkm->TopUpWithDB(batch);
49
50 storage.UnsetBlankWalletFlag(batch);
51 return spkm;
52}
53
55 const std::string command = gArgs.GetArg("-signer", "");
56 if (command == "") return util::Error{Untranslated("restart bitcoind with -signer=<cmd>")};
57 std::vector<ExternalSigner> signers;
58 ExternalSigner::Enumerate(command, signers, Params().GetChainTypeString());
59 if (signers.empty()) return util::Error{Untranslated("No external signers found")};
60 // TODO: add fingerprint argument instead of failing in case of multiple signers.
61 if (signers.size() > 1) return util::Error{Untranslated("More than one external signer found. Please connect only one at a time.")};
62 return signers[0];
63}
64
66{
67 // TODO: avoid the need to infer a descriptor from inside a descriptor wallet
68 const CScript& scriptPubKey = GetScriptForDestination(dest);
69 auto provider = GetSolvingProvider(scriptPubKey);
70 auto descriptor = InferDescriptor(scriptPubKey, *provider);
71
72 const UniValue& result = signer.DisplayAddress(descriptor->ToString());
73
74 const UniValue& error = result.find_value("error");
75 if (error.isStr()) return util::Error{strprintf(_("Signer returned error: %s"), error.getValStr())};
76
77 const UniValue& ret_address = result.find_value("address");
78 if (!ret_address.isStr()) return util::Error{_("Signer did not echo address")};
79
80 if (ret_address.getValStr() != EncodeDestination(dest)) {
81 return util::Error{strprintf(_("Signer echoed unexpected address %s"), ret_address.getValStr())};
82 }
83
84 return util::Result<void>();
85}
86
87// If sign is true, transaction must previously have been filled
88std::optional<PSBTError> ExternalSignerScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, const common::PSBTFillOptions& options, int* n_signed) const
89{
90 if (!options.sign) {
91 return DescriptorScriptPubKeyMan::FillPSBT(psbt, txdata, options, n_signed);
92 }
93
94 // Already complete if every input is now signed
95 bool complete = true;
96 for (const auto& input : psbt.inputs) {
97 complete &= PSBTInputSigned(input);
98 }
99 if (complete) return {};
100
102 if (!signer) {
103 LogWarning("%s", util::ErrorString(signer).original);
104 return PSBTError::EXTERNAL_SIGNER_NOT_FOUND;
105 }
106
107 std::string failure_reason;
108 if(!signer->SignTransaction(psbt, failure_reason)) {
109 LogWarning("Failed to sign: %s\n", failure_reason);
110 return PSBTError::EXTERNAL_SIGNER_FAILED;
111 }
112 if (options.finalize) FinalizePSBT(psbt); // This won't work in a multisig setup
113 return {};
114}
115} // namespace wallet
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
ArgsManager gArgs
Definition: args.cpp:38
const auto command
const CChainParams & Params()
Return the currently selected parameters.
std::string GetArg(const std::string &strArg, const std::string &strDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return string argument or default value.
Definition: args.cpp:517
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
Enables interaction with an external signing device or service, such as a hardware wallet.
static bool Enumerate(const std::string &command, std::vector< ExternalSigner > &signers, const std::string &chain)
Obtain a list of signers.
A version of CTransaction with the PSBT format.
Definition: psbt.h:1239
std::vector< PSBTInput > inputs
Definition: psbt.h:1248
const UniValue & find_value(std::string_view key) const
Definition: univalue.cpp:232
const std::string & getValStr() const
Definition: univalue.h:68
bool isStr() const
Definition: univalue.h:85
256-bit opaque blob.
Definition: uint256.h:196
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const override
std::optional< common::PSBTError > FillPSBT(PartiallySignedTransaction &psbt, const PrecomputedTransactionData &txdata, const common::PSBTFillOptions &options, int *n_signed=nullptr) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
static std::unique_ptr< ExternalSignerScriptPubKeyMan > CreateNew(WalletStorage &storage, WalletBatch &batch, int64_t keypool_size, std::unique_ptr< Descriptor > desc)
static std::unique_ptr< ExternalSignerScriptPubKeyMan > LoadFromStorage(WalletStorage &storage, const uint256 &id, WalletDescriptor &descriptor, int64_t keypool_size, const KeyMap &keys, const CryptedKeyMap &ckeys)
static util::Result< ExternalSigner > GetExternalSigner()
std::optional< common::PSBTError > FillPSBT(PartiallySignedTransaction &psbt, const PrecomputedTransactionData &txdata, const common::PSBTFillOptions &options, int *n_signed=nullptr) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
util::Result< void > DisplayAddress(const CTxDestination &dest, const ExternalSigner &signer) const
Display address on the device and verify that the returned value matches.
Access to the wallet database.
Definition: walletdb.h:197
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:239
Descriptor with some wallet metadata.
Definition: walletutil.h:64
virtual bool IsWalletFlagSet(uint64_t) const =0
virtual void UnsetBlankWalletFlag(WalletBatch &)=0
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:295
#define LogWarning(...)
Definition: log.h:126
PSBTError
Definition: types.h:19
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:93
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char > > > CryptedKeyMap
@ WALLET_FLAG_EXTERNAL_SIGNER
Indicates that the wallet needs an external signer.
Definition: walletutil.h:56
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:53
std::map< CKeyID, CKey > KeyMap
is a home for public enum and struct type definitions that are used internally by node code,...
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed by checking for non-null finalized fields.
Definition: psbt.cpp:547
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
Definition: psbt.cpp:797
std::unique_ptr< Descriptor > InferDescriptor(const CScript &script, const SigningProvider &provider)
Find a descriptor for the specified script, using information from provider where possible.
Instructions for how a PSBT should be signed or filled with information.
Definition: types.h:31
bool finalize
Whether to create the final scriptSig or scriptWitness if possible.
Definition: types.h:45
bool sign
Whether to sign or not.
Definition: types.h:35
Definition: musig.c:31
#define LOCK(cs)
Definition: sync.h:268
std::vector< uint16_t > keys
Definition: dbwrapper.cpp:376
FuzzedDataProvider provider
Definition: dbwrapper.cpp:366
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:82
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:88
assert(!tx.IsCoinBase())