Bitcoin Core  0.19.99
P2P Digital Currency
psbtwallet.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2019 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 <wallet/psbtwallet.h>
6 
7 TransactionError FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, bool& complete, int sighash_type, bool sign, bool bip32derivs)
8 {
9  LOCK(pwallet->cs_wallet);
10  // Get all of the previous transactions
11  complete = true;
12  for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
13  const CTxIn& txin = psbtx.tx->vin[i];
14  PSBTInput& input = psbtx.inputs.at(i);
15 
16  if (PSBTInputSigned(input)) {
17  continue;
18  }
19 
20  // Verify input looks sane. This will check that we have at most one uxto, witness or non-witness.
21  if (!input.IsSane()) {
23  }
24 
25  // If we have no utxo, grab it from the wallet.
26  if (!input.non_witness_utxo && input.witness_utxo.IsNull()) {
27  const uint256& txhash = txin.prevout.hash;
28  const auto it = pwallet->mapWallet.find(txhash);
29  if (it != pwallet->mapWallet.end()) {
30  const CWalletTx& wtx = it->second;
31  // We only need the non_witness_utxo, which is a superset of the witness_utxo.
32  // The signing code will switch to the smaller witness_utxo if this is ok.
33  input.non_witness_utxo = wtx.tx;
34  }
35  }
36 
37  // Get the Sighash type
38  if (sign && input.sighash_type > 0 && input.sighash_type != sighash_type) {
40  }
41 
42  // Get the scriptPubKey to know which SigningProvider to use
43  CScript script;
44  if (!input.witness_utxo.IsNull()) {
45  script = input.witness_utxo.scriptPubKey;
46  } else if (input.non_witness_utxo) {
47  if (txin.prevout.n >= input.non_witness_utxo->vout.size()) {
49  }
50  script = input.non_witness_utxo->vout[txin.prevout.n].scriptPubKey;
51  } else {
52  // There's no UTXO so we can just skip this now
53  complete = false;
54  continue;
55  }
56  SignatureData sigdata;
57  input.FillSignatureData(sigdata);
58  std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(script, sigdata);
59  if (!provider) {
60  complete = false;
61  continue;
62  }
63 
64  complete &= SignPSBTInput(HidingSigningProvider(provider.get(), !sign, !bip32derivs), psbtx, i, sighash_type);
65  }
66 
67  // Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
68  for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
69  const CTxOut& out = psbtx.tx->vout.at(i);
70  std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(out.scriptPubKey);
71  if (provider) {
72  UpdatePSBTOutput(HidingSigningProvider(provider.get(), true, !bip32derivs), psbtx, i);
73  }
74  }
75 
76  return TransactionError::OK;
77 }
CScript scriptPubKey
Definition: transaction.h:137
Optional< CMutableTransaction > tx
Definition: psbt.h:390
bool SignPSBTInput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index, int sighash, SignatureData *out_sigdata, bool use_dummy)
Signs a PSBTInput, verifying that all provided data matches what is being signed. ...
Definition: psbt.cpp:236
std::unique_ptr< SigningProvider > GetSigningProvider(const CScript &script) const
Get the SigningProvider for a script.
Definition: wallet.cpp:4177
A version of CTransaction with the PSBT format.
Definition: psbt.h:388
CTxOut witness_utxo
Definition: psbt.h:47
bool IsNull() const
Definition: transaction.h:160
TransactionError FillPSBT(const CWallet *pwallet, PartiallySignedTransaction &psbtx, bool &complete, int sighash_type, bool sign, bool bip32derivs)
Fills out a PSBT with information from the wallet.
Definition: psbtwallet.cpp:7
An input of a transaction.
Definition: transaction.h:63
#define LOCK(cs)
Definition: sync.h:218
uint32_t n
Definition: transaction.h:22
A structure for PSBTs which contain per-input information.
Definition: psbt.h:44
An output of a transaction.
Definition: transaction.h:133
void UpdatePSBTOutput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index)
Updates a PSBTOutput with information from provider.
Definition: psbt.cpp:217
std::vector< PSBTInput > inputs
Definition: psbt.h:391
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:253
int sighash_type
Definition: psbt.h:55
256-bit opaque blob.
Definition: uint256.h:120
bool IsSane() const
Definition: psbt.cpp:161
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
CTransactionRef non_witness_utxo
Definition: psbt.h:46
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:606
TransactionError
Definition: error.h:22
RecursiveMutex cs_wallet
Definition: wallet.h:717
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed.
Definition: psbt.cpp:212
void FillSignatureData(SignatureData &sigdata) const
Definition: psbt.cpp:88
auto it
Definition: validation.cpp:361
COutPoint prevout
Definition: transaction.h:66
CTransactionRef tx
Definition: wallet.h:349
uint256 hash
Definition: transaction.h:21