Bitcoin Core  22.99.0
P2P Digital Currency
external_signer_scriptpubkeyman.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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 <external_signer.h>
8 
9 #include <iostream>
10 #include <memory>
11 #include <stdexcept>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 
16 namespace wallet {
17 bool ExternalSignerScriptPubKeyMan::SetupDescriptor(std::unique_ptr<Descriptor> desc)
18 {
22 
23  int64_t creation_time = GetTime();
24 
25  // Make the descriptor
26  WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
27  m_wallet_descriptor = w_desc;
28 
29  // Store the descriptor
31  if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
32  throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
33  }
34 
35  // TopUp
36  TopUp();
37 
39  return true;
40 }
41 
43  const std::string command = gArgs.GetArg("-signer", "");
44  if (command == "") throw std::runtime_error(std::string(__func__) + ": restart bitcoind with -signer=<cmd>");
45  std::vector<ExternalSigner> signers;
46  ExternalSigner::Enumerate(command, signers, Params().NetworkIDString());
47  if (signers.empty()) throw std::runtime_error(std::string(__func__) + ": No external signers found");
48  // TODO: add fingerprint argument in case of multiple signers
49  return signers[0];
50 }
51 
52 bool ExternalSignerScriptPubKeyMan::DisplayAddress(const CScript scriptPubKey, const ExternalSigner &signer) const
53 {
54  // TODO: avoid the need to infer a descriptor from inside a descriptor wallet
55  auto provider = GetSolvingProvider(scriptPubKey);
56  auto descriptor = InferDescriptor(scriptPubKey, *provider);
57 
58  signer.DisplayAddress(descriptor->ToString());
59  // TODO inspect result
60  return true;
61 }
62 
63 // If sign is true, transaction must previously have been filled
64 TransactionError ExternalSignerScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type, bool sign, bool bip32derivs, int* n_signed, bool finalize) const
65 {
66  if (!sign) {
67  return DescriptorScriptPubKeyMan::FillPSBT(psbt, txdata, sighash_type, false, bip32derivs, n_signed, finalize);
68  }
69 
70  // Already complete if every input is now signed
71  bool complete = true;
72  for (const auto& input : psbt.inputs) {
73  // TODO: for multisig wallets, we should only care if all _our_ inputs are signed
74  complete &= PSBTInputSigned(input);
75  }
76  if (complete) return TransactionError::OK;
77 
78  std::string strFailReason;
79  if(!GetExternalSigner().SignTransaction(psbt, strFailReason)) {
80  tfm::format(std::cerr, "Failed to sign: %s\n", strFailReason);
82  }
83  if (finalize) FinalizePSBT(psbt); // This won't work in a multisig setup
84  return TransactionError::OK;
85 }
86 } // namespace wallet
FinalizePSBT
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
Definition: psbt.cpp:333
ExternalSigner::DisplayAddress
UniValue DisplayAddress(const std::string &descriptor) const
Display address on the device.
Definition: external_signer.cpp:62
wallet::ScriptPubKeyMan::m_storage
WalletStorage & m_storage
Definition: scriptpubkeyman.h:169
assert
assert(!tx.IsCoinBase())
tinyformat::format
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:1062
wallet::DescriptorScriptPubKeyMan::cs_desc_man
RecursiveMutex cs_desc_man
Definition: scriptpubkeyman.h:567
wallet::DescriptorScriptPubKeyMan::FillPSBT
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.
Definition: scriptpubkeyman.cpp:2104
TransactionError::EXTERNAL_SIGNER_FAILED
@ EXTERNAL_SIGNER_FAILED
wallet::ExternalSignerScriptPubKeyMan::SetupDescriptor
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...
Definition: external_signer_scriptpubkeyman.cpp:17
PartiallySignedTransaction::inputs
std::vector< PSBTInput > inputs
Definition: psbt.h:674
wallet::WALLET_FLAG_DESCRIPTORS
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:66
GetTime
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:26
ExternalSigner::Enumerate
static bool Enumerate(const std::string &command, std::vector< ExternalSigner > &signers, const std::string chain)
Obtain a list of signers.
Definition: external_signer.cpp:24
wallet::ExternalSignerScriptPubKeyMan::FillPSBT
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.
Definition: external_signer_scriptpubkeyman.cpp:64
wallet
Definition: node.h:38
wallet::WalletBatch::WriteDescriptor
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:241
chainparams.h
wallet::WalletStorage::GetDatabase
virtual WalletDatabase & GetDatabase() const =0
wallet::WALLET_FLAG_EXTERNAL_SIGNER
@ WALLET_FLAG_EXTERNAL_SIGNER
Indicates that the wallet needs an external signer.
Definition: walletutil.h:69
TransactionError
TransactionError
Definition: error.h:22
external_signer.h
ArgsManager::GetArg
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:588
wallet::WalletBatch
Access to the wallet database.
Definition: walletdb.h:180
PrecomputedTransactionData
Definition: interpreter.h:151
wallet::ExternalSignerScriptPubKeyMan::DisplayAddress
bool DisplayAddress(const CScript scriptPubKey, const ExternalSigner &signer) const
Definition: external_signer_scriptpubkeyman.cpp:52
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
gArgs
ArgsManager gArgs
Definition: system.cpp:87
ExternalSigner
Enables interaction with an external signing device or service, such as a hardware wallet.
Definition: external_signer.h:18
wallet::DescriptorScriptPubKeyMan::GetSolvingProvider
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const override
Definition: scriptpubkeyman.cpp:2062
wallet::DescriptorScriptPubKeyMan::TopUp
bool TopUp(unsigned int size=0) override
Fills internal address pool.
Definition: scriptpubkeyman.cpp:1774
wallet::DescriptorScriptPubKeyMan::SignTransaction
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.
Definition: scriptpubkeyman.cpp:2072
PartiallySignedTransaction
A version of CTransaction with the PSBT format.
Definition: psbt.h:668
LOCK
#define LOCK(cs)
Definition: sync.h:226
TransactionError::OK
@ OK
No error.
wallet::DescriptorScriptPubKeyMan::GetID
uint256 GetID() const override
Definition: scriptpubkeyman.cpp:2195
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:561
PSBTInputSigned
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed.
Definition: psbt.cpp:208
wallet::ExternalSignerScriptPubKeyMan::GetExternalSigner
static ExternalSigner GetExternalSigner()
Definition: external_signer_scriptpubkeyman.cpp:42
wallet::WalletDescriptor
Descriptor with some wallet metadata.
Definition: walletutil.h:76
wallet::WalletStorage::IsWalletFlagSet
virtual bool IsWalletFlagSet(uint64_t) const =0
wallet::WalletStorage::UnsetBlankWalletFlag
virtual void UnsetBlankWalletFlag(WalletBatch &)=0
InferDescriptor
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: descriptor.cpp:1413
external_signer_scriptpubkeyman.h