Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions btcutil/psbt/partial_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,11 @@ func (pi *PInput) serialize(w io.Writer) error {
}
}

for _, leafScript := range pi.TaprootLeafScript {
if leafScript == nil {
return ErrInvalidPsbtFormat
}
}
sort.Slice(pi.TaprootLeafScript, func(i, j int) bool {
return pi.TaprootLeafScript[i].SortBefore(
pi.TaprootLeafScript[j],
Expand Down
22 changes: 22 additions & 0 deletions btcutil/psbt/psbt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ import (
"github.com/stretchr/testify/require"
)

// TestSerializeNilTaprootLeafScript tests that serializing a PSBT with a nil
// entry in TaprootLeafScript returns an error instead of panicking.
// Regression test for https://github.com/btcsuite/btcd/issues/2495
func TestSerializeNilTaprootLeafScript(t *testing.T) {
tx := wire.NewMsgTx(2)
tx.AddTxIn(&wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Hash: chainhash.Hash{},
Index: 0,
},
})
tx.AddTxOut(&wire.TxOut{Value: 1000})

packet, err := NewFromUnsignedTx(tx)
require.NoError(t, err)

packet.Inputs[0].TaprootLeafScript = []*TaprootTapLeafScript{nil}

_, err = packet.B64Encode()
require.ErrorIs(t, err, ErrInvalidPsbtFormat)
}

// Test vectors from:
// // https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#test-vectors

Expand Down
3 changes: 3 additions & 0 deletions btcutil/psbt/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ func FindLeafScript(pInput *PInput,
targetLeafHash []byte) (*TaprootTapLeafScript, error) {

for _, leaf := range pInput.TaprootLeafScript {
if leaf == nil {
continue
}
leafHash := txscript.TapLeaf{
LeafVersion: leaf.LeafVersion,
Script: leaf.Script,
Expand Down