diff --git a/src/node/psbt.cpp b/src/node/psbt.cpp index e43e3b4aef..04ce6cbc9d 100644 --- a/src/node/psbt.cpp +++ b/src/node/psbt.cpp @@ -31,7 +31,7 @@ PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx) // Check for a UTXO CTxOut utxo; - if (psbtx.GetInputUTXO(utxo, i)) { + if (input.GetUTXO(utxo)) { //TODO(gwillen) do PSBT inputs always have explicit assets & amounts? if (!MoneyRange(utxo.nValue.GetAmount()) || !MoneyRange(in_amts[utxo.nAsset.GetAsset()] + utxo.nValue.GetAmount())) { result.SetInvalid(strprintf("PSBT is not valid. Input %u has invalid value", i)); @@ -127,7 +127,7 @@ PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx) PSBTInput& input = psbtx.inputs[i]; Coin newcoin; - if (!SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, 1, nullptr, true) || !psbtx.GetInputUTXO(newcoin.out, i)) { + if (!SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, 1, nullptr, true) || !input.GetUTXO(newcoin.out)) { success = false; break; } else { diff --git a/src/psbt.cpp b/src/psbt.cpp index 9931c9ef10..13e166fe1f 100644 --- a/src/psbt.cpp +++ b/src/psbt.cpp @@ -65,17 +65,15 @@ bool PartiallySignedTransaction::AddOutput(const CTxOut& txout, const PSBTOutput return true; } -bool PartiallySignedTransaction::GetInputUTXO(CTxOut& utxo, int input_index) const +bool PSBTInput::GetUTXO(CTxOut& utxo) const { - PSBTInput input = inputs[input_index]; - uint32_t prevout_index = tx->vin[input_index].prevout.n; - if (input.non_witness_utxo) { - if (prevout_index >= input.non_witness_utxo->vout.size()) { + if (non_witness_utxo) { + if (*prev_out >= non_witness_utxo->vout.size()) { return false; } - utxo = input.non_witness_utxo->vout[prevout_index]; - } else if (!input.witness_utxo.IsNull()) { - utxo = input.witness_utxo; + utxo = non_witness_utxo->vout[*prev_out]; + } else if (!witness_utxo.IsNull()) { + utxo = witness_utxo; } else { return false; } diff --git a/src/psbt.h b/src/psbt.h index 487f45cec1..cae568d435 100644 --- a/src/psbt.h +++ b/src/psbt.h @@ -196,6 +196,7 @@ struct PSBTInput void FillSignatureData(SignatureData& sigdata) const; void FromSignatureData(const SignatureData& sigdata); void Merge(const PSBTInput& input); + bool GetUTXO(CTxOut& utxo) const; PSBTInput(uint32_t version) : m_psbt_version(version) {} template @@ -742,14 +743,6 @@ struct PartiallySignedTransaction void CacheUnsignedTxPieces(); PartiallySignedTransaction() {} explicit PartiallySignedTransaction(const CMutableTransaction& tx); - /** - * Finds the UTXO for a given input index - * - * @param[out] utxo The UTXO of the input if found - * @param[in] input_index Index of the input to retrieve the UTXO of - * @return Whether the UTXO for the specified input was found - */ - bool GetInputUTXO(CTxOut& utxo, int input_index) const; template inline void Serialize(Stream& s) const { diff --git a/src/test/fuzz/psbt.cpp b/src/test/fuzz/psbt.cpp index a3dbf14da2..40e6fb1738 100644 --- a/src/test/fuzz/psbt.cpp +++ b/src/test/fuzz/psbt.cpp @@ -59,7 +59,7 @@ void test_one_input(const std::vector& buffer) for (size_t i = 0; i < psbt.tx->vin.size(); ++i) { CTxOut tx_out; - if (psbt.GetInputUTXO(tx_out, i)) { + if (psbt.inputs.at(i).GetUTXO(tx_out)) { (void)tx_out.IsNull(); (void)tx_out.ToString(); }