Skip to content

Commit 3d70dd9

Browse files
committed
Move FillPSBT to be a member of CWallet
1 parent a4af324 commit 3d70dd9

File tree

10 files changed

+99
-122
lines changed

10 files changed

+99
-122
lines changed

src/Makefile.am

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ BITCOIN_CORE_H = \
241241
wallet/fees.h \
242242
wallet/ismine.h \
243243
wallet/load.h \
244-
wallet/psbtwallet.h \
245244
wallet/rpcwallet.h \
246245
wallet/scriptpubkeyman.h \
247246
wallet/wallet.h \
@@ -349,7 +348,6 @@ libbitcoin_wallet_a_SOURCES = \
349348
wallet/feebumper.cpp \
350349
wallet/fees.cpp \
351350
wallet/load.cpp \
352-
wallet/psbtwallet.cpp \
353351
wallet/rpcdump.cpp \
354352
wallet/rpcwallet.cpp \
355353
wallet/scriptpubkeyman.cpp \

src/interfaces/wallet.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <wallet/fees.h>
2020
#include <wallet/ismine.h>
2121
#include <wallet/load.h>
22-
#include <wallet/psbtwallet.h>
2322
#include <wallet/rpcwallet.h>
2423
#include <wallet/wallet.h>
2524

@@ -361,9 +360,9 @@ class WalletImpl : public Wallet
361360
bool& complete,
362361
int sighash_type = 1 /* SIGHASH_ALL */,
363362
bool sign = true,
364-
bool bip32derivs = false) override
363+
bool bip32derivs = false) const override
365364
{
366-
return FillPSBT(m_wallet.get(), psbtx, complete, sighash_type, sign, bip32derivs);
365+
return m_wallet->FillPSBT(psbtx, complete, sighash_type, sign, bip32derivs);
367366
}
368367
WalletBalances getBalances() override
369368
{

src/interfaces/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class Wallet
196196
bool& complete,
197197
int sighash_type = 1 /* SIGHASH_ALL */,
198198
bool sign = true,
199-
bool bip32derivs = false) = 0;
199+
bool bip32derivs = false) const = 0;
200200

201201
//! Get balances.
202202
virtual WalletBalances getBalances() = 0;

src/qt/sendcoinsdialog.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <ui_interface.h>
2727
#include <wallet/coincontrol.h>
2828
#include <wallet/fees.h>
29-
#include <wallet/psbtwallet.h>
3029
#include <wallet/wallet.h>
3130

3231
#include <QFontMetrics>

src/wallet/psbtwallet.cpp

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/wallet/psbtwallet.h

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/wallet/rpcwallet.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <util/vector.h>
2828
#include <wallet/coincontrol.h>
2929
#include <wallet/feebumper.h>
30-
#include <wallet/psbtwallet.h>
3130
#include <wallet/rpcwallet.h>
3231
#include <wallet/wallet.h>
3332
#include <wallet/walletdb.h>
@@ -3516,7 +3515,7 @@ static UniValue bumpfee(const JSONRPCRequest& request)
35163515
} else {
35173516
PartiallySignedTransaction psbtx(mtx);
35183517
bool complete = false;
3519-
const TransactionError err = FillPSBT(pwallet, psbtx, complete, SIGHASH_ALL, false /* sign */, true /* bip32derivs */);
3518+
const TransactionError err = pwallet->FillPSBT(psbtx, complete, SIGHASH_ALL, false /* sign */, true /* bip32derivs */);
35203519
CHECK_NONFATAL(err == TransactionError::OK);
35213520
CHECK_NONFATAL(!complete);
35223521
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
@@ -4133,7 +4132,7 @@ UniValue walletprocesspsbt(const JSONRPCRequest& request)
41334132
bool sign = request.params[1].isNull() ? true : request.params[1].get_bool();
41344133
bool bip32derivs = request.params[3].isNull() ? true : request.params[3].get_bool();
41354134
bool complete = true;
4136-
const TransactionError err = FillPSBT(pwallet, psbtx, complete, nHashType, sign, bip32derivs);
4135+
const TransactionError err = pwallet->FillPSBT(psbtx, complete, nHashType, sign, bip32derivs);
41374136
if (err != TransactionError::OK) {
41384137
throw JSONRPCTransactionError(err);
41394138
}
@@ -4256,7 +4255,7 @@ UniValue walletcreatefundedpsbt(const JSONRPCRequest& request)
42564255
// Fill transaction with out data but don't sign
42574256
bool bip32derivs = request.params[4].isNull() ? true : request.params[4].get_bool();
42584257
bool complete = true;
4259-
const TransactionError err = FillPSBT(pwallet, psbtx, complete, 1, false, bip32derivs);
4258+
const TransactionError err = pwallet->FillPSBT(psbtx, complete, 1, false, bip32derivs);
42604259
if (err != TransactionError::OK) {
42614260
throw JSONRPCTransactionError(err);
42624261
}

src/wallet/test/psbt_wallet_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <key_io.h>
66
#include <util/bip32.h>
77
#include <util/strencodings.h>
8-
#include <wallet/psbtwallet.h>
98
#include <wallet/wallet.h>
109

1110
#include <boost/test/unit_test.hpp>
@@ -61,7 +60,7 @@ BOOST_AUTO_TEST_CASE(psbt_updater_test)
6160

6261
// Fill transaction with our data
6362
bool complete = true;
64-
BOOST_REQUIRE_EQUAL(TransactionError::OK, FillPSBT(&m_wallet, psbtx, complete, SIGHASH_ALL, false, true));
63+
BOOST_REQUIRE_EQUAL(TransactionError::OK, m_wallet.FillPSBT(psbtx, complete, SIGHASH_ALL, false, true));
6564

6665
// Get the final tx
6766
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);

src/wallet/wallet.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,6 +2477,78 @@ bool CWallet::SignTransaction(CMutableTransaction& tx, const std::map<COutPoint,
24772477
return false;
24782478
}
24792479

2480+
TransactionError CWallet::FillPSBT(PartiallySignedTransaction& psbtx, bool& complete, int sighash_type, bool sign, bool bip32derivs) const
2481+
{
2482+
LOCK(cs_wallet);
2483+
// Get all of the previous transactions
2484+
complete = true;
2485+
for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
2486+
const CTxIn& txin = psbtx.tx->vin[i];
2487+
PSBTInput& input = psbtx.inputs.at(i);
2488+
2489+
if (PSBTInputSigned(input)) {
2490+
continue;
2491+
}
2492+
2493+
// Verify input looks sane. This will check that we have at most one uxto, witness or non-witness.
2494+
if (!input.IsSane()) {
2495+
return TransactionError::INVALID_PSBT;
2496+
}
2497+
2498+
// If we have no utxo, grab it from the wallet.
2499+
if (!input.non_witness_utxo && input.witness_utxo.IsNull()) {
2500+
const uint256& txhash = txin.prevout.hash;
2501+
const auto it = mapWallet.find(txhash);
2502+
if (it != mapWallet.end()) {
2503+
const CWalletTx& wtx = it->second;
2504+
// We only need the non_witness_utxo, which is a superset of the witness_utxo.
2505+
// The signing code will switch to the smaller witness_utxo if this is ok.
2506+
input.non_witness_utxo = wtx.tx;
2507+
}
2508+
}
2509+
2510+
// Get the Sighash type
2511+
if (sign && input.sighash_type > 0 && input.sighash_type != sighash_type) {
2512+
return TransactionError::SIGHASH_MISMATCH;
2513+
}
2514+
2515+
// Get the scriptPubKey to know which SigningProvider to use
2516+
CScript script;
2517+
if (!input.witness_utxo.IsNull()) {
2518+
script = input.witness_utxo.scriptPubKey;
2519+
} else if (input.non_witness_utxo) {
2520+
if (txin.prevout.n >= input.non_witness_utxo->vout.size()) {
2521+
return TransactionError::MISSING_INPUTS;
2522+
}
2523+
script = input.non_witness_utxo->vout[txin.prevout.n].scriptPubKey;
2524+
} else {
2525+
// There's no UTXO so we can just skip this now
2526+
complete = false;
2527+
continue;
2528+
}
2529+
SignatureData sigdata;
2530+
input.FillSignatureData(sigdata);
2531+
std::unique_ptr<SigningProvider> provider = GetSigningProvider(script, sigdata);
2532+
if (!provider) {
2533+
complete = false;
2534+
continue;
2535+
}
2536+
2537+
complete &= SignPSBTInput(HidingSigningProvider(provider.get(), !sign, !bip32derivs), psbtx, i, sighash_type);
2538+
}
2539+
2540+
// Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
2541+
for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
2542+
const CTxOut& out = psbtx.tx->vout.at(i);
2543+
std::unique_ptr<SigningProvider> provider = GetSigningProvider(out.scriptPubKey);
2544+
if (provider) {
2545+
UpdatePSBTOutput(HidingSigningProvider(provider.get(), true, !bip32derivs), psbtx, i);
2546+
}
2547+
}
2548+
2549+
return TransactionError::OK;
2550+
}
2551+
24802552
bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nChangePosInOut, std::string& strFailReason, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, CCoinControl coinControl)
24812553
{
24822554
std::vector<CRecipient> vecSend;

src/wallet/wallet.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <interfaces/handler.h>
1212
#include <outputtype.h>
1313
#include <policy/feerate.h>
14+
#include <psbt.h>
1415
#include <tinyformat.h>
1516
#include <ui_interface.h>
1617
#include <util/strencodings.h>
@@ -921,6 +922,25 @@ class CWallet final : public WalletStorage, private interfaces::Chain::Notificat
921922
// Sign the tx given the input coins and sighash.
922923
bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, std::string>& input_errors) const;
923924

925+
/**
926+
* Fills out a PSBT with information from the wallet. Fills in UTXOs if we have
927+
* them. Tries to sign if sign=true. Sets `complete` if the PSBT is now complete
928+
* (i.e. has all required signatures or signature-parts, and is ready to
929+
* finalize.) Sets `error` and returns false if something goes wrong.
930+
*
931+
* @param[in] psbtx PartiallySignedTransaction to fill in
932+
* @param[out] complete indicates whether the PSBT is now complete
933+
* @param[in] sighash_type the sighash type to use when signing (if PSBT does not specify)
934+
* @param[in] sign whether to sign or not
935+
* @param[in] bip32derivs whether to fill in bip32 derivation information if available
936+
* return error
937+
*/
938+
TransactionError FillPSBT(PartiallySignedTransaction& psbtx,
939+
bool& complete,
940+
int sighash_type = 1 /* SIGHASH_ALL */,
941+
bool sign = true,
942+
bool bip32derivs = true) const;
943+
924944
/**
925945
* Create a new transaction paying the recipients with a set of coins
926946
* selected by SelectCoins(); Also create the change output, when needed

0 commit comments

Comments
 (0)