Skip to content

Commit cea053b

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

File tree

2 files changed

+46
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)