Skip to content

Commit c87bd5d

Browse files
committed
utils: function to get dust limit for a pkscript.
1 parent 6b71582 commit c87bd5d

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

utils/dust_limit.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package utils
2+
3+
import (
4+
"github.com/btcsuite/btcd/btcutil"
5+
"github.com/btcsuite/btcd/mempool"
6+
"github.com/btcsuite/btcd/wire"
7+
)
8+
9+
// DustLimitForPkScript returns the dust limit for a given pkScript. An output
10+
// must be greater or equal to this value.
11+
func DustLimitForPkScript(pkscript []byte) btcutil.Amount {
12+
return btcutil.Amount(mempool.GetDustThreshold(&wire.TxOut{
13+
PkScript: pkscript,
14+
}))
15+
}

utils/dust_limit_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/lightningnetwork/lnd/input"
7+
"github.com/lightningnetwork/lnd/lnwallet"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
type pkScriptGetter func([]byte) ([]byte, error)
12+
13+
// TestDustLimitForPkScript checks that the dust limit for a given script size
14+
// matches the calculation in lnwallet.DustLimitForSize.
15+
func TestDustLimitForPkScript(t *testing.T) {
16+
getScripts := map[int]pkScriptGetter{
17+
input.P2WPKHSize: input.WitnessPubKeyHash,
18+
input.P2WSHSize: input.WitnessScriptHash,
19+
input.P2SHSize: input.GenerateP2SH,
20+
input.P2PKHSize: input.GenerateP2PKH,
21+
}
22+
23+
for scriptSize, getPkScript := range getScripts {
24+
pkScript, err := getPkScript([]byte{})
25+
require.NoError(t, err, "failed to generate pkScript")
26+
27+
require.Equal(
28+
t, lnwallet.DustLimitForSize(scriptSize),
29+
DustLimitForPkScript(pkScript),
30+
)
31+
}
32+
}

0 commit comments

Comments
 (0)