Skip to content

fix(bitcoin-wallet-snap): reveal own output scripts when signing a PSBT - #225

Open
jeremytsng wants to merge 3 commits into
fix/btc-snap-stopgap-scanfrom
fix/btc-snap-reveal-at-sign
Open

fix(bitcoin-wallet-snap): reveal own output scripts when signing a PSBT#225
jeremytsng wants to merge 3 commits into
fix/btc-snap-stopgap-scanfrom
fix/btc-snap-reveal-at-sign

Conversation

@jeremytsng

Copy link
Copy Markdown
Contributor

Explanation

Second of three PRs splitting #201 per review feedback. Stacked on #224.

When the snap fills and signs a partner-supplied PSBT (bridge/swap), the template's change output pays one of our own scripts — but that script may sit beyond the revealed set. BDK's lookahead lets it pass isMine, so we sign it, while routine sync (start_sync_with_revealed_spks) never queries it. Change we signed away can then be missing from the balance, which is the shape of both support cases behind this stack (0.09 BTC and 6.9 BTC).

The rule this PR adds: if we sign it, we watch it.

  • BitcoinAccount.revealToScript(script): resolves the script's keychain and derivation index (derivation_of_spk) and reveals up to it (reveal_addresses_to) when it lies beyond the last revealed index. Verified against the pinned @metamask/bitcoindevkit build: derivation_index returns the last revealed index, and lookahead scripts resolve.
  • signPsbt reveals every own output script of the PSBT after a successful signature and persists the wallet before broadcast — explicitly, so broadcast: false flows are covered too.
  • Security shape: the reveal runs only on the signing path, which sits behind the user confirmation dialog. fillPsbt and computeFee (callable without confirmation) reveal nothing, so an unconfirmed request cannot advance the revealed index; per signature the reveal distance is bounded by BDK's lookahead.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Partner PSBT templates may pay wallet scripts beyond the revealed set;
BDK's lookahead lets them pass isMine while routine sync, which queries
revealed scripts only, never watches them. Signing now reveals every own
output script and persists the wallet before broadcast, so signed-away
change is always covered by sync. The reveal runs only on the signing
path: signing sits behind the user confirmation dialog, while fillPsbt
and computeFee stay reveal-free so an unconfirmed request cannot advance
the revealed index.
@jeremytsng
jeremytsng force-pushed the fix/btc-snap-reveal-at-sign branch from 276f370 to cdd693d Compare August 28, 2026 05:09

export type AccountCapability =
(typeof AccountCapability)[keyof typeof AccountCapability];
export enum AccountCapability {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR #216 has introduced the ban of enum, need to use const now.

Bitcoin: 0,
Testnet: 1,
} as const;
export enum Purpose {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

export type Slip44 = (typeof Slip44)[keyof typeof Slip44];
export enum Slip44 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

2 participants