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
16bool 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
51bool 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
63TransactionError 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
84}
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: system.cpp:590
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
bool TopUp(unsigned int size=0) override
Fills internal address pool.
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const override
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.
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.
uint256 GetID() const override
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 DisplayAddress(const CScript scriptPubKey, const ExternalSigner &signer) const
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...
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.
WalletStorage & m_storage
Access to the wallet database.
Definition: walletdb.h:179
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:240
Descriptor with some wallet metadata.
Definition: walletutil.h:76
virtual bool IsWalletFlagSet(uint64_t) const =0
virtual void UnsetBlankWalletFlag(WalletBatch &)=0
virtual WalletDatabase & GetDatabase() const =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.
TransactionError
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:1062
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed.
Definition: psbt.cpp:197
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
Definition: psbt.cpp:318
A version of CTransaction with the PSBT format.
Definition: psbt.h:392
std::vector< PSBTInput > inputs
Definition: psbt.h:394
#define LOCK(cs)
Definition: sync.h:226
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:26
ArgsManager gArgs
Definition: system.cpp:85
assert(!tx.IsCoinBase())
@ WALLET_FLAG_EXTERNAL_SIGNER
Indicates that the wallet needs an external signer.
Definition: walletutil.h:68
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:65