MetaMask / MetaMask/internal-snaps
[bitcoin-wallet-snap] refactor: replace hardcoded dust limits with bdk-wasm's is_dust()
- Dominant language
- TypeScript
- Stars
- 1
- Forks
- 2
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 105
Description
## Summary
The dust validation in `packages/snap/src/handlers/validation.ts` currently hardcodes dust thresholds per address type:
```ts
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](https://github.com/bitcoindevkit/bdk-wasm/pull/41), 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](https://github.com/bitcoindevkit/bdk-wasm/pull/13) and delegate to BDK / Bitcoin Core's actual dust relay policy logic.
## Suggested change
```ts
// 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).
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in packages/snap/src/handlers/validation.ts and inspect the current dust validation and address script data. Check the @metamask/bitcoindevkit version and its available dust APIs, then run the existing dust limit cases in RpcHandler.test.ts. Done means validation delegates to the BDK dust check, the hardcoded thresholds are removed, and the existing tests pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- blockchain
- Issue type
- Refactor
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100