utils: function to get dust limit for a pkscript.

This commit is contained in:
Slyghtning 2025-07-17 15:08:11 +02:00
parent bce9b5d45d
commit 94168c3fe5
No known key found for this signature in database
GPG key ID: F82D456EA023C9BF
2 changed files with 47 additions and 0 deletions

15
utils/dust_limit.go Normal file
View file

@ -0,0 +1,15 @@
package utils
import (
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/mempool"
"github.com/btcsuite/btcd/wire"
)
// DustLimitForPkScript returns the dust limit for a given pkScript. An output
// must be greater or equal to this value.
func DustLimitForPkScript(pkscript []byte) btcutil.Amount {
return btcutil.Amount(mempool.GetDustThreshold(&wire.TxOut{
PkScript: pkscript,
}))
}

32
utils/dust_limit_test.go Normal file
View file

@ -0,0 +1,32 @@
package utils
import (
"testing"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/stretchr/testify/require"
)
type pkScriptGetter func([]byte) ([]byte, error)
// TestDustLimitForPkScript checks that the dust limit for a given script size
// matches the calculation in lnwallet.DustLimitForSize.
func TestDustLimitForPkScript(t *testing.T) {
getScripts := map[int]pkScriptGetter{
input.P2WPKHSize: input.WitnessPubKeyHash,
input.P2WSHSize: input.WitnessScriptHash,
input.P2SHSize: input.GenerateP2SH,
input.P2PKHSize: input.GenerateP2PKH,
}
for scriptSize, getPkScript := range getScripts {
pkScript, err := getPkScript([]byte{})
require.NoError(t, err, "failed to generate pkScript")
require.Equal(
t, lnwallet.DustLimitForSize(scriptSize),
DustLimitForPkScript(pkScript),
)
}
}