From b0ab66fe02d4099a2304b0d5c726879cb28f5ad5 Mon Sep 17 00:00:00 2001 From: Boris Nagaev Date: Thu, 2 Jul 2026 01:14:22 -0500 Subject: [PATCH] psbt: compact witness utxo scripts after parsing wire.ReadTxOut returns PkScript slices backed by its internal 4 MiB script slab. PSBT inputs keep parsed WitnessUtxo values, so small scripts could otherwise keep the whole slab live. Copy the script before storing the TxOut and assert the parsed witness script has compact capacity. --- psbt/strict_tx_values_test.go | 4 +++- psbt/utils.go | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/psbt/strict_tx_values_test.go b/psbt/strict_tx_values_test.go index 83ed6bdd..e31b8be2 100644 --- a/psbt/strict_tx_values_test.go +++ b/psbt/strict_tx_values_test.go @@ -240,7 +240,9 @@ func TestParsesWitnessUtxoTxOutStrictly(t *testing.T) { strictnessPSBTWithWitnessUtxo(t, txOutBytes), ), false) require.NoError(t, err) - require.Equal(t, pkScript, packet.Inputs[0].WitnessUtxo.PkScript) + script := packet.Inputs[0].WitnessUtxo.PkScript + require.Equal(t, pkScript, script) + require.Equal(t, len(script), cap(script)) malformedTxOut := append(append([]byte{}, txOutBytes...), 0x00) _, err = NewFromRawBytes(bytes.NewReader( diff --git a/psbt/utils.go b/psbt/utils.go index baf75583..9bad7722 100644 --- a/psbt/utils.go +++ b/psbt/utils.go @@ -315,6 +315,13 @@ func readTxOut(txout []byte) (*wire.TxOut, error) { return nil, err } + // wire.ReadTxOut stores PkScript in an internal 4 MiB script slab. + // Compact it before storing the TxOut in the PSBT so tiny witness + // scripts do not retain the full slab. + script := make([]byte, len(txOut.PkScript)) + copy(script, txOut.PkScript) + txOut.PkScript = script + return txOut, nil }