Skip to content

refactor: replace hardcoded dust limits with bdk-wasm's is_dust() #593

@darioAnongba

Description

@darioAnongba

Summary

The dust validation in packages/snap/src/handlers/validation.ts currently hardcodes dust thresholds per address type:

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;
  }
}

With bitcoindevkit/bdk-wasm v0.3.0, the following dust check methods are now available:

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

These were added in bitcoindevkit/bdk-wasm#13 and delegate to BDK / Bitcoin Core's actual dust relay policy logic.

Suggested change

// Before: hardcoded switch + manual comparison
const sats = Amount.from_btc(Number(amountInBtc)).to_sat();
const min = getDustLimitSats(account.addressType);
if (sats < min) { ... }

// After: delegate to BDK
const amount = Amount.from_btc(Number(amountInBtc));
if (amount.is_dust(account.publicAddress.script_pubkey)) { ... }

Benefits

  • Correct by construction — matches Bitcoin Core's exact dust relay policy
  • Future-proof — automatically handles new script types without code changes
  • Less maintenance — no hardcoded constants to keep in sync
  • Simpler — removes ~25 lines of code

Dependency

Requires upgrading @metamask/bitcoindevkit to a version built from bitcoindevkit/bdk-wasm v0.3.0+.

Existing dust limit tests in RpcHandler.test.ts should continue to pass since BDK computes the same thresholds (e.g. 294 sats for p2wpkh).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions