Skip to content

Commit 3135c1a

Browse files
committed
Abstract out UpdatePSBTOutput from FillPSBT
1 parent fb90ec3 commit 3135c1a

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/psbt.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,25 @@ bool PSBTInputSigned(const PSBTInput& input)
215215
return !input.final_script_sig.empty() || !input.final_script_witness.IsNull();
216216
}
217217

218+
void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index)
219+
{
220+
const CTxOut& out = psbt.tx->vout.at(index);
221+
PSBTOutput& psbt_out = psbt.outputs.at(index);
222+
223+
// Fill a SignatureData with output info
224+
SignatureData sigdata;
225+
psbt_out.FillSignatureData(sigdata);
226+
227+
// Construct a would-be spend of this output, to update sigdata with.
228+
// Note that ProduceSignature is used to fill in metadata (not actual signatures),
229+
// so provider does not need to provide any private keys (it can be a HidingSigningProvider).
230+
MutableTransactionSignatureCreator creator(psbt.tx.get_ptr(), /* index */ 0, out.nValue, SIGHASH_ALL);
231+
ProduceSignature(provider, creator, out.scriptPubKey, sigdata);
232+
233+
// Put redeem_script, witness_script, key paths, into PSBTOutput.
234+
psbt_out.FromSignatureData(sigdata);
235+
}
236+
218237
bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, int sighash, SignatureData* out_sigdata, bool use_dummy)
219238
{
220239
PSBTInput& input = psbt.inputs.at(index);

src/psbt.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,12 @@ bool PSBTInputSigned(const PSBTInput& input);
565565
/** Signs a PSBTInput, verifying that all provided data matches what is being signed. */
566566
bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, int sighash = SIGHASH_ALL, SignatureData* out_sigdata = nullptr, bool use_dummy = false);
567567

568+
/** Updates a PSBTOutput with information from provider.
569+
*
570+
* This fills in the redeem_script, witness_script, and hd_keypaths where possible.
571+
*/
572+
void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index);
573+
568574
/**
569575
* Finalizes a PSBT if possible, combining partial signatures.
570576
*

src/wallet/psbtwallet.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,7 @@ TransactionError FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& ps
4444

4545
// Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
4646
for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
47-
const CTxOut& out = psbtx.tx->vout.at(i);
48-
PSBTOutput& psbt_out = psbtx.outputs.at(i);
49-
50-
// Fill a SignatureData with output info
51-
SignatureData sigdata;
52-
psbt_out.FillSignatureData(sigdata);
53-
54-
MutableTransactionSignatureCreator creator(psbtx.tx.get_ptr(), 0, out.nValue, 1);
55-
ProduceSignature(HidingSigningProvider(pwallet, true, !bip32derivs), creator, out.scriptPubKey, sigdata);
56-
psbt_out.FromSignatureData(sigdata);
47+
UpdatePSBTOutput(HidingSigningProvider(pwallet, true, !bip32derivs), psbtx, i);
5748
}
5849

5950
return TransactionError::OK;

0 commit comments

Comments
 (0)