Skip to content

refactor: replace hardcoded dust limits with BDK's is_dust()#592

Closed
darioAnongba wants to merge 1 commit intoMetaMask:mainfrom
darioAnongba:refactor/use-bdk-dust-checks
Closed

refactor: replace hardcoded dust limits with BDK's is_dust()#592
darioAnongba wants to merge 1 commit intoMetaMask:mainfrom
darioAnongba:refactor/use-bdk-dust-checks

Conversation

@darioAnongba
Copy link
Contributor

@darioAnongba darioAnongba commented Mar 16, 2026

Summary

Replace the hardcoded getDustLimitSats switch statement in validation.ts with BDK's Amount.is_dust(script) method, which computes dust thresholds from the actual script — matching Bitcoin Core's relay policy exactly.

Before

function getDustLimitSats(addressType: AddressType): bigint {
  switch (addressType) {
    case 'p2wpkh': return 294n;
    case 'p2pkh':  return 546n;
    case 'p2sh':   return 540n;
    case 'p2wsh':  return 330n;
    case 'p2tr':   return 330n;
    default:       return 546n;
  }
}

After

const amount = Amount.from_btc(Number(amountInBtc));
if (amount.is_dust(account.publicAddress.script_pubkey)) { ... }

Benefits

  • Correct by construction — delegates to BDK / bitcoin-core's dust relay logic
  • Future-proof — automatically handles new script types without code changes
  • Less maintenance — no hardcoded constants to keep in sync
  • Simpler — removes ~25 lines, replaces with 2

Dependency

This requires @metamask/bitcoindevkit to be built from bitcoindevkit/bdk-wasm v0.3.0+ (release PR: bitcoindevkit/bdk-wasm#41), which exposes:

  • Amount.is_dust(script) — check if amount is below dust for a script
  • ScriptBuf.minimal_non_dust() — get the minimum non-dust amount for a script
  • ScriptBuf.minimal_non_dust_custom(fee_rate) — same with custom dust relay fee

These were added in bitcoindevkit/bdk-wasm#13.

Testing

Existing dust limit tests in RpcHandler.test.ts (lines 605-642) should continue to pass since the BDK computation produces the same thresholds (294 sats for p2wpkh) as the previously hardcoded values.


Note

Medium Risk
Changes send-amount validation behavior by delegating dust checks to BDK’s Amount.is_dust(script_pubkey), which could alter edge-case acceptance/rejection depending on the underlying BDK version and script type handling.

Overview
validateDustLimit now uses BDK’s Amount.is_dust(account.publicAddress.script_pubkey) to enforce dust limits, removing the hardcoded per-AddressType dust threshold switch and the AddressType import.

This makes dust validation depend on the actual output script (matching Bitcoin Core relay policy) rather than a fixed table of constants.

Written by Cursor Bugbot for commit 8a12eed. This will update automatically on new commits. Configure here.

Remove the getDustLimitSats switch statement that hardcodes dust thresholds
per address type (294 for p2wpkh, 546 for p2pkh, etc.) and replace it with
BDK's Amount.is_dust(script) method.

This delegates dust calculation to bitcoin-core's relay policy logic via BDK,
which computes the threshold from the actual script rather than matching on
address types. Benefits:
- Automatically handles all current and future script types
- Matches Bitcoin Core's exact dust relay policy
- Removes maintenance burden of keeping hardcoded values in sync

Requires @metamask/bitcoindevkit to be upgraded to a version built from
bitcoindevkit/bdk-wasm v0.3.0+, which exposes Amount.is_dust() and
ScriptBuf.minimal_non_dust().
@darioAnongba darioAnongba requested a review from a team as a code owner March 16, 2026 16:37
@darioAnongba darioAnongba deleted the refactor/use-bdk-dust-checks branch March 16, 2026 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant