Bitcoin Core  22.99.0
P2P Digital Currency
external_signer_scriptpubkeyman.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 bool ExternalSignerScriptPubKeyMan::SetupDescriptor(std::unique_ptr<Descriptor> desc)
17 {
21 
22  int64_t creation_time = GetTime();
23 
24  // Make the descriptor
25  WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
26  m_wallet_descriptor = w_desc;
27 
28  // Store the descriptor
30  if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
31  throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
32  }
33 
34  // TopUp
35  TopUp();
36 
38  return true;
39 }
40 
42  const std::string command = gArgs.GetArg("-signer", "");
43  if (command == "") throw std::runtime_error(std::string(__func__) + ": restart bitcoind with -signer=<cmd>");
44  std::vector<ExternalSigner> signers;
45  ExternalSigner::Enumerate(command, signers, Params().NetworkIDString());
46  if (signers.empty()) throw std::runtime_error(std::string(__func__) + ": No external signers found");
47  // TODO: add fingerprint argument in case of multiple signers
48  return signers[0];
49 }
50 
51 bool ExternalSignerScriptPubKeyMan::DisplayAddress(const CScript scriptPubKey, const ExternalSigner &signer) const
52 {
53  // TODO: avoid the need to infer a descriptor from inside a descriptor wallet
54  auto provider = GetSolvingProvider(scriptPubKey);
55  auto descriptor = InferDescriptor(scriptPubKey, *provider);
56 
57  signer.DisplayAddress(descriptor->ToString());
58  // TODO inspect result
59  return true;
60 }
61 
62 // If sign is true, transaction must previously have been filled
63 TransactionError ExternalSignerScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type, bool sign, bool bip32derivs, int* n_signed) const
64 {
65  if (!sign) {
66  return DescriptorScriptPubKeyMan::FillPSBT(psbt, txdata, sighash_type, false, bip32derivs, n_signed);
67  }
68 
69  // Already complete if every input is now signed
70  bool complete = true;
71  for (const auto& input : psbt.inputs) {
72  // TODO: for multisig wallets, we should only care if all _our_ inputs are signed
73  complete &= PSBTInputSigned(input);
74  }
75  if (complete) return TransactionError::OK;
76 
77  std::string strFailReason;
78  if(!GetExternalSigner().SignTransaction(psbt, strFailReason)) {
79  tfm::format(std::cerr, "Failed to sign: %s\n", strFailReason);
81  }
82  FinalizePSBT(psbt); // This won't work in a multisig setup
83  return TransactionError::OK;
84 }
ScriptPubKeyMan::m_storage
WalletStorage & m_storage
Definition: scriptpubkeyman.h:172
WalletBatch::WriteDescriptor
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:239
FinalizePSBT
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
Definition: psbt.cpp:318
ExternalSigner::DisplayAddress
UniValue DisplayAddress(const std::string &descriptor) const
Display address on the device.
Definition: external_signer.cpp:61
ExternalSignerScriptPubKeyMan::DisplayAddress
bool DisplayAddress(const CScript scriptPubKey, const ExternalSigner &signer) const
Definition: external_signer_scriptpubkeyman.cpp:51
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
DescriptorScriptPubKeyMan::GetSolvingProvider
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const override
Definition: scriptpubkeyman.cpp:2039
ExternalSignerScriptPubKeyMan::FillPSBT
TransactionError FillPSBT(PartiallySignedTransaction &psbt, const PrecomputedTransactionData &txdata, int sighash_type=1, bool sign=true, bool bip32derivs=false, int *n_signed=nullptr) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
Definition: external_signer_scriptpubkeyman.cpp:63
TransactionError::EXTERNAL_SIGNER_FAILED
@ EXTERNAL_SIGNER_FAILED
PartiallySignedTransaction::inputs
std::vector< PSBTInput > inputs
Definition: psbt.h:394
WALLET_FLAG_EXTERNAL_SIGNER
@ WALLET_FLAG_EXTERNAL_SIGNER
Indicates that the wallet needs an external signer.
Definition: walletutil.h:68
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:23
chainparams.h
TransactionError
TransactionError
Definition: error.h:22
WalletStorage::GetDatabase
virtual WalletDatabase & GetDatabase() const =0
external_signer.h
DescriptorScriptPubKeyMan::FillPSBT
TransactionError FillPSBT(PartiallySignedTransaction &psbt, const PrecomputedTransactionData &txdata, int sighash_type=1, bool sign=true, bool bip32derivs=false, int *n_signed=nullptr) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
Definition: scriptpubkeyman.cpp:2081
ArgsManager::GetArg
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:588
PrecomputedTransactionData
Definition: interpreter.h:150
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:16
WalletStorage::UnsetBlankWalletFlag
virtual void UnsetBlankWalletFlag(WalletBatch &)=0
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
WalletStorage::IsWalletFlagSet
virtual bool IsWalletFlagSet(uint64_t) const =0
ExternalSigner
Enables interaction with an external signing device or service, such as a hardware wallet.
Definition: external_signer.h:18
DescriptorScriptPubKeyMan::GetID
uint256 GetID() const override
Definition: scriptpubkeyman.cpp:2172
DescriptorScriptPubKeyMan::cs_desc_man
RecursiveMutex cs_desc_man
Definition: scriptpubkeyman.h:560
DescriptorScriptPubKeyMan::TopUp
bool TopUp(unsigned int size=0) override
Fills internal address pool.
Definition: scriptpubkeyman.cpp:1760
PartiallySignedTransaction
A version of CTransaction with the PSBT format.
Definition: psbt.h:391
LOCK
#define LOCK(cs)
Definition: sync.h:232
gArgs
ArgsManager gArgs
Definition: system.cpp:84
TransactionError::OK
@ OK
No error.
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:535
ExternalSignerScriptPubKeyMan::GetExternalSigner
static ExternalSigner GetExternalSigner()
Definition: external_signer_scriptpubkeyman.cpp:41
PSBTInputSigned
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed.
Definition: psbt.cpp:197
WalletDescriptor
Descriptor with some wallet metadata.
Definition: walletutil.h:75
DescriptorScriptPubKeyMan::SignTransaction
bool SignTransaction(CMutableTransaction &tx, const std::map< COutPoint, Coin > &coins, int sighash, std::map< int, std::string > &input_errors) const override
Creates new signatures and adds them to the transaction.
Definition: scriptpubkeyman.cpp:2049
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:1418
WalletBatch
Access to the wallet database.
Definition: walletdb.h:176
external_signer_scriptpubkeyman.h
WALLET_FLAG_DESCRIPTORS
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:65