diff --git a/src/psbt.h b/src/psbt.h --- a/src/psbt.h +++ b/src/psbt.h @@ -505,6 +505,15 @@ SignatureData *out_sigdata = nullptr, bool use_dummy = false); +/** + * Updates a PSBTOutput with information from provider. + * + * This fills in the redeem_script, witness_script, and hd_keypaths where + * possible. + */ +void UpdatePSBTOutput(const SigningProvider &provider, + PartiallySignedTransaction &psbt, int index); + /** * Finalizes a PSBT if possible, combining partial signatures. * diff --git a/src/psbt.cpp b/src/psbt.cpp --- a/src/psbt.cpp +++ b/src/psbt.cpp @@ -5,7 +5,6 @@ #include #include - PartiallySignedTransaction::PartiallySignedTransaction( const CMutableTransaction &txIn) : tx(txIn) { @@ -173,6 +172,28 @@ return !input.final_script_sig.empty(); } +void UpdatePSBTOutput(const SigningProvider &provider, + PartiallySignedTransaction &psbt, int index) { + const CTxOut &out = psbt.tx->vout.at(index); + PSBTOutput &psbt_out = psbt.outputs.at(index); + + // Fill a SignatureData with output info + SignatureData sigdata; + psbt_out.FillSignatureData(sigdata); + + // Construct a would-be spend of this output, to update sigdata with. + // Note that ProduceSignature is used to fill in metadata (not actual + // signatures), so provider does not need to provide any private keys (it + // can be a HidingSigningProvider). + MutableTransactionSignatureCreator creator(psbt.tx.get_ptr(), /* index */ 0, + out.nValue, + SigHashType().withForkId()); + ProduceSignature(provider, creator, out.scriptPubKey, sigdata); + + // Put redeem_script and key paths, into PSBTOutput. + psbt_out.FromSignatureData(sigdata); +} + bool SignPSBTInput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index, SigHashType sighash, SignatureData *out_sigdata, diff --git a/src/wallet/psbtwallet.cpp b/src/wallet/psbtwallet.cpp --- a/src/wallet/psbtwallet.cpp +++ b/src/wallet/psbtwallet.cpp @@ -51,18 +51,8 @@ // Fill in the bip32 keypaths and redeemscripts for the outputs so that // hardware wallets can identify change for (size_t i = 0; i < psbtx.tx->vout.size(); ++i) { - const CTxOut &out = psbtx.tx->vout.at(i); - PSBTOutput &psbt_out = psbtx.outputs.at(i); - - // Fill a SignatureData with output info - SignatureData sigdata; - psbt_out.FillSignatureData(sigdata); - - MutableTransactionSignatureCreator creator( - psbtx.tx.get_ptr(), 0, out.nValue, SigHashType().withForkId()); - ProduceSignature(HidingSigningProvider(pwallet, true, !bip32derivs), - creator, out.scriptPubKey, sigdata); - psbt_out.FromSignatureData(sigdata); + UpdatePSBTOutput(HidingSigningProvider(pwallet, true, !bip32derivs), + psbtx, i); } return TransactionError::OK;