Skip to content

Commit 5dd0c03

Browse files
committed
FillPSBT: report number of inputs signed (or would sign)
In FillPSBT, optionally report the number of inputs we successfully signed, as an out parameter. If "sign" is false, instead report the number of inputs for which GetSigningProvider does not return nullptr. (This is a potentially overbroad estimate of inputs we could sign.)
1 parent 9e7b23b commit 5dd0c03

File tree

8 files changed

+46
-13
lines changed

8 files changed

+46
-13
lines changed

src/interfaces/wallet.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,10 @@ class WalletImpl : public Wallet
335335
bool sign,
336336
bool bip32derivs,
337337
PartiallySignedTransaction& psbtx,
338-
bool& complete) override
338+
bool& complete,
339+
size_t* n_signed) override
339340
{
340-
return m_wallet->FillPSBT(psbtx, complete, sighash_type, sign, bip32derivs);
341+
return m_wallet->FillPSBT(psbtx, complete, sighash_type, sign, bip32derivs, n_signed);
341342
}
342343
WalletBalances getBalances() override
343344
{

src/interfaces/wallet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ class Wallet
197197
bool sign,
198198
bool bip32derivs,
199199
PartiallySignedTransaction& psbtx,
200-
bool& complete) = 0;
200+
bool& complete,
201+
size_t* n_signed) = 0;
201202

202203
//! Get balances.
203204
virtual WalletBalances getBalances() = 0;

src/qt/sendcoinsdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ void SendCoinsDialog::on_sendButton_clicked()
392392
CMutableTransaction mtx = CMutableTransaction{*(m_current_transaction->getWtx())};
393393
PartiallySignedTransaction psbtx(mtx);
394394
bool complete = false;
395-
const TransactionError err = model->wallet().fillPSBT(SIGHASH_ALL, false /* sign */, true /* bip32derivs */, psbtx, complete);
395+
const TransactionError err = model->wallet().fillPSBT(SIGHASH_ALL, false /* sign */, true /* bip32derivs */, psbtx, complete, nullptr);
396396
assert(!complete);
397397
assert(err == TransactionError::OK);
398398
// Serialize the PSBT

src/qt/walletmodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ bool WalletModel::bumpFee(uint256 hash, uint256& new_hash)
536536
if (create_psbt) {
537537
PartiallySignedTransaction psbtx(mtx);
538538
bool complete = false;
539-
const TransactionError err = wallet().fillPSBT(SIGHASH_ALL, false /* sign */, true /* bip32derivs */, psbtx, complete);
539+
const TransactionError err = wallet().fillPSBT(SIGHASH_ALL, false /* sign */, true /* bip32derivs */, psbtx, complete, nullptr);
540540
if (err != TransactionError::OK || complete) {
541541
QMessageBox::critical(nullptr, tr("Fee bump error"), tr("Can't draft transaction."));
542542
return false;

src/wallet/scriptpubkeyman.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,11 @@ SigningResult LegacyScriptPubKeyMan::SignMessage(const std::string& message, con
585585
return SigningResult::SIGNING_FAILED;
586586
}
587587

588-
TransactionError LegacyScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbtx, int sighash_type, bool sign, bool bip32derivs) const
588+
TransactionError LegacyScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbtx, int sighash_type, bool sign, bool bip32derivs, int* n_signed) const
589589
{
590+
if (n_signed) {
591+
*n_signed = 0;
592+
}
590593
for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
591594
const CTxIn& txin = psbtx.tx->vin[i];
592595
PSBTInput& input = psbtx.inputs.at(i);
@@ -617,6 +620,14 @@ TransactionError LegacyScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psb
617620
SignatureData sigdata;
618621
input.FillSignatureData(sigdata);
619622
SignPSBTInput(HidingSigningProvider(this, !sign, !bip32derivs), psbtx, i, sighash_type);
623+
624+
bool signed_one = PSBTInputSigned(input);
625+
if (n_signed && (signed_one || !sign)) {
626+
// If sign is false, we assume that we _could_ sign if we get here. This
627+
// will never have false negatives; it is hard to tell under what i
628+
// circumstances it could have false positives.
629+
(*n_signed)++;
630+
}
620631
}
621632

622633
// Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
@@ -2064,8 +2075,11 @@ SigningResult DescriptorScriptPubKeyMan::SignMessage(const std::string& message,
20642075
return SigningResult::OK;
20652076
}
20662077

2067-
TransactionError DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbtx, int sighash_type, bool sign, bool bip32derivs) const
2078+
TransactionError DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbtx, int sighash_type, bool sign, bool bip32derivs, int* n_signed) const
20682079
{
2080+
if (n_signed) {
2081+
*n_signed = 0;
2082+
}
20692083
for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
20702084
const CTxIn& txin = psbtx.tx->vin[i];
20712085
PSBTInput& input = psbtx.inputs.at(i);
@@ -2117,6 +2131,14 @@ TransactionError DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction&
21172131
}
21182132

21192133
SignPSBTInput(HidingSigningProvider(keys.get(), !sign, !bip32derivs), psbtx, i, sighash_type);
2134+
2135+
bool signed_one = PSBTInputSigned(input);
2136+
if (n_signed && (signed_one || !sign)) {
2137+
// If sign is false, we assume that we _could_ sign if we get here. This
2138+
// will never have false negatives; it is hard to tell under what i
2139+
// circumstances it could have false positives.
2140+
(*n_signed)++;
2141+
}
21202142
}
21212143

21222144
// Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change

src/wallet/scriptpubkeyman.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class ScriptPubKeyMan
234234
/** Sign a message with the given script */
235235
virtual SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const { return SigningResult::SIGNING_FAILED; };
236236
/** Adds script and derivation path information to a PSBT, and optionally signs it. */
237-
virtual TransactionError FillPSBT(PartiallySignedTransaction& psbt, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false) const { return TransactionError::INVALID_PSBT; }
237+
virtual TransactionError FillPSBT(PartiallySignedTransaction& psbt, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr) const { return TransactionError::INVALID_PSBT; }
238238

239239
virtual uint256 GetID() const { return uint256(); }
240240

@@ -393,7 +393,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
393393

394394
bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, std::string>& input_errors) const override;
395395
SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const override;
396-
TransactionError FillPSBT(PartiallySignedTransaction& psbt, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false) const override;
396+
TransactionError FillPSBT(PartiallySignedTransaction& psbt, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr) const override;
397397

398398
uint256 GetID() const override;
399399

@@ -596,7 +596,7 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
596596

597597
bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, std::string>& input_errors) const override;
598598
SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const override;
599-
TransactionError FillPSBT(PartiallySignedTransaction& psbt, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false) const override;
599+
TransactionError FillPSBT(PartiallySignedTransaction& psbt, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr) const override;
600600

601601
uint256 GetID() const override;
602602

src/wallet/wallet.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,8 +2471,11 @@ bool CWallet::SignTransaction(CMutableTransaction& tx, const std::map<COutPoint,
24712471
return false;
24722472
}
24732473

2474-
TransactionError CWallet::FillPSBT(PartiallySignedTransaction& psbtx, bool& complete, int sighash_type, bool sign, bool bip32derivs) const
2474+
TransactionError CWallet::FillPSBT(PartiallySignedTransaction& psbtx, bool& complete, int sighash_type, bool sign, bool bip32derivs, size_t * n_signed) const
24752475
{
2476+
if (n_signed) {
2477+
*n_signed = 0;
2478+
}
24762479
LOCK(cs_wallet);
24772480
// Get all of the previous transactions
24782481
for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
@@ -2503,10 +2506,15 @@ TransactionError CWallet::FillPSBT(PartiallySignedTransaction& psbtx, bool& comp
25032506

25042507
// Fill in information from ScriptPubKeyMans
25052508
for (ScriptPubKeyMan* spk_man : GetAllScriptPubKeyMans()) {
2506-
TransactionError res = spk_man->FillPSBT(psbtx, sighash_type, sign, bip32derivs);
2509+
int n_signed_this_spkm = 0;
2510+
TransactionError res = spk_man->FillPSBT(psbtx, sighash_type, sign, bip32derivs, &n_signed_this_spkm);
25072511
if (res != TransactionError::OK) {
25082512
return res;
25092513
}
2514+
2515+
if (n_signed) {
2516+
(*n_signed) += n_signed_this_spkm;
2517+
}
25102518
}
25112519

25122520
// Complete if every input is now signed

src/wallet/wallet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
964964
bool& complete,
965965
int sighash_type = 1 /* SIGHASH_ALL */,
966966
bool sign = true,
967-
bool bip32derivs = true) const;
967+
bool bip32derivs = true,
968+
size_t* n_signed = nullptr) const;
968969

969970
/**
970971
* Create a new transaction paying the recipients with a set of coins

0 commit comments

Comments
 (0)