Bitcoin Core  25.99.0
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2020-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or
5 #include <chainparams.h>
6 #include <common/args.h>
7 #include <common/system.h>
8 #include <external_signer.h>
11 #include <iostream>
12 #include <memory>
13 #include <stdexcept>
14 #include <string>
15 #include <utility>
16 #include <vector>
18 namespace wallet {
19 bool ExternalSignerScriptPubKeyMan::SetupDescriptor(std::unique_ptr<Descriptor> desc)
20 {
25  int64_t creation_time = GetTime();
27  // Make the descriptor
28  WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
29  m_wallet_descriptor = w_desc;
31  // Store the descriptor
33  if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
34  throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
35  }
37  // TopUp
38  TopUp();
41  return true;
42 }
45  const std::string command = gArgs.GetArg("-signer", "");
46  if (command == "") throw std::runtime_error(std::string(__func__) + ": restart bitcoind with -signer=<cmd>");
47  std::vector<ExternalSigner> signers;
48  ExternalSigner::Enumerate(command, signers, Params().GetChainTypeString());
49  if (signers.empty()) throw std::runtime_error(std::string(__func__) + ": No external signers found");
50  // TODO: add fingerprint argument instead of failing in case of multiple signers.
51  if (signers.size() > 1) throw std::runtime_error(std::string(__func__) + ": More than one external signer found. Please connect only one at a time.");
52  return signers[0];
53 }
55 bool ExternalSignerScriptPubKeyMan::DisplayAddress(const CScript scriptPubKey, const ExternalSigner &signer) const
56 {
57  // TODO: avoid the need to infer a descriptor from inside a descriptor wallet
58  auto provider = GetSolvingProvider(scriptPubKey);
59  auto descriptor = InferDescriptor(scriptPubKey, *provider);
61  signer.DisplayAddress(descriptor->ToString());
62  // TODO inspect result
63  return true;
64 }
66 // If sign is true, transaction must previously have been filled
67 TransactionError ExternalSignerScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type, bool sign, bool bip32derivs, int* n_signed, bool finalize) const
68 {
69  if (!sign) {
70  return DescriptorScriptPubKeyMan::FillPSBT(psbt, txdata, sighash_type, false, bip32derivs, n_signed, finalize);
71  }
73  // Already complete if every input is now signed
74  bool complete = true;
75  for (const auto& input : psbt.inputs) {
76  // TODO: for multisig wallets, we should only care if all _our_ inputs are signed
77  complete &= PSBTInputSigned(input);
78  }
79  if (complete) return TransactionError::OK;
81  std::string strFailReason;
82  if(!GetExternalSigner().SignTransaction(psbt, strFailReason)) {
83  tfm::format(std::cerr, "Failed to sign: %s\n", strFailReason);
85  }
86  if (finalize) FinalizePSBT(psbt); // This won't work in a multisig setup
87  return TransactionError::OK;
88 }
89 } // namespace wallet
ArgsManager gArgs
Definition: args.cpp:42
const auto command
const CChainParams & Params()
Return the currently selected parameters.
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:456
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
Enables interaction with an external signing device or service, such as a hardware wallet.
UniValue DisplayAddress(const std::string &descriptor) const
Display address on the device.
static bool Enumerate(const std::string &command, std::vector< ExternalSigner > &signers, const std::string chain)
Obtain a list of signers.
bool SignTransaction(CMutableTransaction &tx, const std::map< COutPoint, Coin > &coins, int sighash, std::map< int, bilingual_str > &input_errors) const override
Creates new signatures and adds them to the transaction.
bool TopUp(unsigned int size=0) override
Fills internal address pool.
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const override
TransactionError FillPSBT(PartiallySignedTransaction &psbt, const PrecomputedTransactionData &txdata, int sighash_type=SIGHASH_DEFAULT, bool sign=true, bool bip32derivs=false, int *n_signed=nullptr, bool finalize=true) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
TransactionError FillPSBT(PartiallySignedTransaction &psbt, const PrecomputedTransactionData &txdata, int sighash_type=1, bool sign=true, bool bip32derivs=false, int *n_signed=nullptr, bool finalize=true) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
bool SetupDescriptor(std::unique_ptr< Descriptor >desc)
Provide a descriptor at setup time Returns false if already setup or setup fails, true if setup is su...
bool DisplayAddress(const CScript scriptPubKey, const ExternalSigner &signer) const
WalletStorage & m_storage
Access to the wallet database.
Definition: walletdb.h:190
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:242
Descriptor with some wallet metadata.
Definition: walletutil.h:77
virtual bool IsWalletFlagSet(uint64_t) const =0
virtual WalletDatabase & GetDatabase() const =0
virtual void UnsetBlankWalletFlag(WalletBatch &)=0
std::unique_ptr< Descriptor > InferDescriptor(const CScript &script, const SigningProvider &provider)
Find a descriptor for the specified script, using information from provider where possible.
Definition: error.h:22
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1060
Indicates that the wallet needs an external signer.
Definition: walletutil.h:69
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:66
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed by checking for non-null finalized fields.
Definition: psbt.cpp:290
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
Definition: psbt.cpp:477
A version of CTransaction with the PSBT format.
Definition: psbt.h:947
std::vector< PSBTInput > inputs
Definition: psbt.h:952
#define LOCK(cs)
Definition: sync.h:258
int64_t GetTime()
Definition: time.cpp:97