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 }