Bitcoin Core  21.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>
8 
9 #ifdef ENABLE_EXTERNAL_SIGNER
10 
11 bool ExternalSignerScriptPubKeyMan::SetupDescriptor(std::unique_ptr<Descriptor> desc)
12 {
16 
17  int64_t creation_time = GetTime();
18 
19  // Make the descriptor
20  WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
21  m_wallet_descriptor = w_desc;
22 
23  // Store the descriptor
25  if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
26  throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
27  }
28 
29  // TopUp
30  TopUp();
31 
33  return true;
34 }
35 
37  const std::string command = gArgs.GetArg("-signer", "");
38  if (command == "") throw std::runtime_error(std::string(__func__) + ": restart bitcoind with -signer=<cmd>");
39  std::vector<ExternalSigner> signers;
40  ExternalSigner::Enumerate(command, signers, Params().NetworkIDString());
41  if (signers.empty()) throw std::runtime_error(std::string(__func__) + ": No external signers found");
42  // TODO: add fingerprint argument in case of multiple signers
43  return signers[0];
44 }
45 
46 bool ExternalSignerScriptPubKeyMan::DisplayAddress(const CScript scriptPubKey, const ExternalSigner &signer) const
47 {
48  // TODO: avoid the need to infer a descriptor from inside a descriptor wallet
49  auto provider = GetSolvingProvider(scriptPubKey);
50  auto descriptor = InferDescriptor(scriptPubKey, *provider);
51 
52  signer.DisplayAddress(descriptor->ToString());
53  // TODO inspect result
54  return true;
55 }
56 
57 // If sign is true, transaction must previously have been filled
58 TransactionError ExternalSignerScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbt, int sighash_type, bool sign, bool bip32derivs, int* n_signed) const
59 {
60  if (!sign) {
61  return DescriptorScriptPubKeyMan::FillPSBT(psbt, sighash_type, false, bip32derivs, n_signed);
62  }
63 
64  // Already complete if every input is now signed
65  bool complete = true;
66  for (const auto& input : psbt.inputs) {
67  // TODO: for multisig wallets, we should only care if all _our_ inputs are signed
68  complete &= PSBTInputSigned(input);
69  }
70  if (complete) return TransactionError::OK;
71 
72  std::string strFailReason;
73  if(!GetExternalSigner().SignTransaction(psbt, strFailReason)) {
74  tfm::format(std::cerr, "Failed to sign: %s\n", strFailReason);
76  }
77  FinalizePSBT(psbt); // This won't work in a multisig setup
78  return TransactionError::OK;
79 }
80 
81 #endif
ScriptPubKeyMan::m_storage
WalletStorage & m_storage
Definition: scriptpubkeyman.h:172
WalletBatch::WriteDescriptor
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:232
FinalizePSBT
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
Definition: psbt.cpp:298
ExternalSigner::DisplayAddress
UniValue DisplayAddress(const std::string &descriptor) const
Display address on the device.
Definition: external_signer.cpp:62
ExternalSignerScriptPubKeyMan::DisplayAddress
bool DisplayAddress(const CScript scriptPubKey, const ExternalSigner &signer) const
Definition: external_signer_scriptpubkeyman.cpp:46
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:2044
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:65
GetTime
int64_t GetTime()
DEPRECATED Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:26
chainparams.h
TransactionError
TransactionError
Definition: error.h:22
WalletStorage::GetDatabase
virtual WalletDatabase & GetDatabase() const =0
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:503
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:11
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:22
DescriptorScriptPubKeyMan::GetID
uint256 GetID() const override
Definition: scriptpubkeyman.cpp:2177
DescriptorScriptPubKeyMan::cs_desc_man
RecursiveMutex cs_desc_man
Definition: scriptpubkeyman.h:561
ExternalSigner::Enumerate
static bool Enumerate(const std::string &command, std::vector< ExternalSigner > &signers, std::string chain, bool ignore_errors=false)
Obtain a list of signers.
Definition: external_signer.cpp:21
DescriptorScriptPubKeyMan::TopUp
bool TopUp(unsigned int size=0) override
Fills internal address pool.
Definition: scriptpubkeyman.cpp:1747
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:79
TransactionError::OK
@ OK
No error.
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:523
ExternalSignerScriptPubKeyMan::GetExternalSigner
static ExternalSigner GetExternalSigner()
Definition: external_signer_scriptpubkeyman.cpp:36
PSBTInputSigned
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed.
Definition: psbt.cpp:194
WalletDescriptor
Descriptor with some wallet metadata.
Definition: walletutil.h:72
DescriptorScriptPubKeyMan::FillPSBT
TransactionError FillPSBT(PartiallySignedTransaction &psbt, 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:2086
ExternalSignerScriptPubKeyMan::FillPSBT
TransactionError FillPSBT(PartiallySignedTransaction &psbt, 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:58
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:2054
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:1191
WalletBatch
Access to the wallet database.
Definition: walletdb.h:176
external_signer_scriptpubkeyman.h
assert
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
WALLET_FLAG_DESCRIPTORS
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:62