What changed, and why it matters
This change tightens how a Bitcoin partial-signed transaction (PSBT) library calculates fees. Previously, if a PSBT input contained two different descriptions of the same unspent coin—one full transaction and one witness-only snapshot—the library would silently prefer the witness snapshot and ignore the other. That could let a malicious or buggy PSBT report a lower fee than the actual transaction would pay. The patch now requires both descriptions to match before using their value, and returns an error if they conflict or point to a non-existent output. The library still does not verify that the supplied coins are real or belong to the transaction, so callers must do that separately.
Review any downstream callers of GetTxFee or SumUtxoInputValues to ensure they handle the new error cases gracefully. Do not rely on this change as a substitute for validating that supplied UTXOs are real, unspent, and correctly signed; the commit explicitly leaves authentication to the caller. Consider adding integration tests for mixed witness/non-witness PSBT workflows.
Security signals we found
Inconsistent dual UTXO descriptions could cause incorrect fee reporting
Witness-only UTXO previously took precedence over full non-witness transaction data
Fee calculation now cross-validates witness and non-witness descriptions
Out-of-range previous output index now returns an error instead of panicking or being ignored
Caller is explicitly reminded that UTXO authentication remains their responsibility
Evidence from the diff
In btcd’s psbt package, SumUtxoInputValues and GetTxFee previously used in.WitnessUtxo when present and fell back to in.NonWitnessUtxo otherwise. This precedence meant a crafted WitnessUtxo could override the value/script in the full previous transaction, causing fee miscalculation. The patch reorders the logic: when NonWitnessUtxo is present it looks up the referenced output index, validates the index is in bounds, then compares that TxOut against WitnessUtxo (if also present) using the existing TxOutsEqual helper. Only if they match (or WitnessUtxo is absent) is the value added. Conflicts, missing UTXO data, and out-of-range output indices now return errors. Tests cover matching descriptions, conflicting amounts, conflicting scripts, and invalid output indexes through GetTxFee.
Changed components
psbt/utils.go: SumUtxoInputValuespsbt/psbt.go: GetTxFeepsbt/utils.go: TxOutsEqual helper reuseInspect captured patch +109 / −9
### psbt/psbt.go
@@ -536,8 +536,10 @@ func (p *Packet) SanityCheck() error {
return nil
}
-// GetTxFee returns the transaction fee. An error is returned if a transaction
-// input does not contain any UTXO information.
+// GetTxFee returns the transaction fee using the supplied UTXO information.
+// An error is returned for missing UTXO information, an invalid previous-output
+// index, or conflicting witness and non-witness output descriptions. The caller
+// remains responsible for authenticating the supplied UTXOs.
func (p *Packet) GetTxFee() (btcutil.Amount, error) {
sumInputs, err := SumUtxoInputValues(p)
if err != nil {
### psbt/utils.go
@@ -336,8 +336,9 @@ func readTransaction(txBytes []byte, noWitness bool) (*wire.MsgTx, error) {
}
// SumUtxoInputValues tries to extract the sum of all inputs specified in the
-// UTXO fields of the PSBT. An error is returned if an input is specified that
-// does not contain any UTXO information.
+// UTXO fields of the PSBT. An error is returned for missing UTXO information,
+// an invalid previous-output index, or conflicting witness and non-witness
+// output descriptions. Consistency does not authenticate the supplied UTXOs.
func SumUtxoInputValues(packet *Packet) (int64, error) {
// We take the TX ins of the unsigned TX as the truth for how many
// inputs there should be, as the fields in the extra data part of the
@@ -350,10 +351,6 @@ func SumUtxoInputValues(packet *Packet) (int64, error) {
inputSum := int64(0)
for idx, in := range packet.Inputs {
switch {
- case in.WitnessUtxo != nil:
- // Witness UTXOs only need to reference the TxOut.
- inputSum += in.WitnessUtxo.Value
-
case in.NonWitnessUtxo != nil:
// Non-witness UTXOs reference to the whole transaction
// the UTXO resides in.
@@ -368,7 +365,20 @@ func SumUtxoInputValues(packet *Packet) (int64, error) {
"TxOut field", idx)
}
- inputSum += utxOuts[txIn.PreviousOutPoint.Index].Value
+ // Reconcile descriptions so their precedence cannot
+ // change the reported fee.
+ prevOut := utxOuts[opIdx]
+ if in.WitnessUtxo != nil &&
+ !TxOutsEqual(prevOut, in.WitnessUtxo) {
+
+ return 0, fmt.Errorf("input %d has "+
+ "conflicting UTXO information", idx)
+ }
+ inputSum += prevOut.Value
+
+ case in.WitnessUtxo != nil:
+ // Witness UTXOs only need to reference the TxOut.
+ inputSum += in.WitnessUtxo.Value
default:
return 0, fmt.Errorf("input %d has no UTXO information",
### psbt/utils_test.go
@@ -7,6 +7,7 @@ import (
"github.com/btcsuite/btcd/chainhash/v2"
"github.com/btcsuite/btcd/wire/v2"
+ "github.com/stretchr/testify/require"
)
func TestSumUtxoInputValues(t *testing.T) {
@@ -73,6 +74,93 @@ func TestSumUtxoInputValues(t *testing.T) {
}
}
+// TestGetTxFeeConflictingUtxos ensures dual UTXO descriptions must agree before
+// their input value can be used to report a transaction fee.
+func TestGetTxFeeConflictingUtxos(t *testing.T) {
+ // Arrange: vary only the witness description or selected output index
+ // against a fixed 100,000-sat previous output to isolate inconsistency.
+ tests := []struct {
+ name string
+ witnessValue int64
+ witnessScript []byte
+ outputIndex uint32
+ wantErr bool
+ }{
+ {
+ name: "matching descriptions",
+ witnessValue: 100_000,
+ witnessScript: []byte{0x51},
+ outputIndex: 0,
+ wantErr: false,
+ },
+ {
+ name: "conflicting amount",
+ witnessValue: 1_000_000,
+ witnessScript: []byte{0x51},
+ outputIndex: 0,
+ wantErr: true,
+ },
+ {
+ name: "conflicting script",
+ witnessValue: 100_000,
+ witnessScript: []byte{0x00},
+ outputIndex: 0,
+ wantErr: true,
+ },
+ {
+ name: "missing referenced output",
+ witnessValue: 100_000,
+ witnessScript: []byte{0x51},
+ outputIndex: 1,
+ wantErr: true,
+ },
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ // Arrange: attach both UTXO descriptions to an input
+ // that spends the supplied previous transaction, so
+ // the public fee API must reconcile them.
+ previous := wire.NewMsgTx(2)
+ previous.AddTxIn(
+ wire.NewTxIn(&wire.OutPoint{}, nil, nil),
+ )
+ previous.AddTxOut(wire.NewTxOut(100_000, []byte{0x51}))
+ previousHash := previous.TxHash()
+ spending := wire.NewMsgTx(2)
+ spending.AddTxIn(
+ wire.NewTxIn(
+ wire.NewOutPoint(
+ &previousHash, test.outputIndex,
+ ),
+ nil, nil,
+ ),
+ )
+ spending.AddTxOut(wire.NewTxOut(90_000, []byte{0x51}))
+ packet, err := NewFromUnsignedTx(spending)
+ require.NoError(t, err)
+ packet.Inputs[0].NonWitnessUtxo = previous
+ packet.Inputs[0].WitnessUtxo = wire.NewTxOut(
+ test.witnessValue, test.witnessScript,
+ )
+
+ // Act: call the public fee API to exercise error
+ // propagation from the input-value calculation.
+ fee, err := packet.GetTxFee()
+
+ // Assert: inconsistent data must return an error
+ // and zero fee; matching descriptions yield
+ // 100,000 minus 90,000 satoshis.
+ if test.wantErr {
+ require.Error(t, err)
+ require.Zero(t, fee)
+ return
+ }
+ require.NoError(t, err)
+ require.EqualValues(t, 10_000, fee)
+ })
+ }
+}
+
func TestTxOutsEqual(t *testing.T) {
testCases := []struct {
name stringWhy this scored 59/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.