Skip to content

feat(btc-provider): accept P2TR accounts and add testnet4#8548

Open
marcopeereboom wants to merge 2 commits into
MetaMask:mainfrom
marcopeereboom:feat/btc-provider-p2tr-testnet4
Open

feat(btc-provider): accept P2TR accounts and add testnet4#8548
marcopeereboom wants to merge 2 commits into
MetaMask:mainfrom
marcopeereboom:feat/btc-provider-p2tr-testnet4

Conversation

@marcopeereboom
Copy link
Copy Markdown

@marcopeereboom marcopeereboom commented Apr 22, 2026

Explanation

BtcAccountProvider currently restricts Bitcoin account compatibility
to P2WPKH only and limits capabilities and discovery to mainnet and
testnet3. This blocks taproot (P2TR) accounts from being tracked by
the provider and prevents testnet4 usage.

This PR makes three changes to BtcAccountProvider:

  • isAccountCompatible: accept P2TR accounts alongside P2WPKH so that
    taproot accounts created through discovery or future UI options are
    correctly managed by the provider
  • capabilities.scopes: add BtcScope.Testnet4 so the provider
    advertises testnet4 support
  • discoverAccounts: include BtcScope.Testnet4 in discovery scopes
    so existing testnet4 accounts are found during wallet setup

Default account creation via createAccountV1 remains P2WPKH — no
behavioral change for existing callers. The snap already has full P2TR
and testnet4 support; this change enables the provider to work with
accounts the snap can now create.

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

Note

Medium Risk
Medium risk because it changes Bitcoin account compatibility, advertised discovery scopes, and overrides resyncAccounts to delete/re-create Snap accounts—mistakes could lead to missing or duplicated accounts if scope/type inference is wrong.

Overview
Bitcoin provider enhancements. BtcAccountProvider now treats P2TR accounts as compatible, advertises BtcScope.Testnet4, and runs discovery across Mainnet, Testnet, and Testnet4 to match its capabilities.

Resync behavior change. Overrides resyncAccounts to re-create missing Snap accounts using the original account.type and first account.scopes entry (instead of defaulting to P2WPKH/Mainnet), adds guards for empty scopes, and reports/propagates re-creation failures after keyring removal to avoid silent inconsistent state.

Tests were updated/expanded to cover P2TR compatibility, multi-scope discovery, and the new resync scenarios.

Reviewed by Cursor Bugbot for commit 2adb7c4. Bugbot is set up for automated code reviews on this repo. Configure here.

@marcopeereboom marcopeereboom requested review from a team as code owners April 22, 2026 12:35
@marcopeereboom
Copy link
Copy Markdown
Author

FWIW, I tested all of this code using metamask flask and I was able to send and move ordinals.

Comment thread packages/multichain-account-service/src/providers/BtcAccountProvider.ts Outdated
@marcopeereboom marcopeereboom force-pushed the feat/btc-provider-p2tr-testnet4 branch from c03182b to 0fbd4e0 Compare May 12, 2026 06:52
@marcopeereboom
Copy link
Copy Markdown
Author

Addressed the Copilot/Bugbot feedback — it was right.

Root cause: SnapAccountProvider.resyncAccounts() re-creates missing snap accounts by calling createAccountV1, which in BtcAccountProvider hardcodes addressType: P2WPKH and scope: Mainnet. With P2TR and testnet4 accounts now managed by this provider (via isAccountCompatible), a desync would silently re-create them with wrong derivation parameters — different address, original funds unreachable.

Fix: Override resyncAccounts in BtcAccountProvider to pass the original account.type and account.scopes[0] directly to keyring.createAccount() instead of funneling through createAccountV1. The delete-extras path is preserved as-is from the base class.

Also rebased onto upstream/main to clean out the merge commits.

Tests: 3 new (P2TR type preservation, testnet4 scope preservation, P2WPKH/Mainnet identity), full package suite passes (13 suites, 322 tests).

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 0fbd4e0. Configure here.

scope: account.scopes[0],
}),
this.config.createAccounts.timeoutMs,
);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Resync override doesn't track re-created account IDs

Medium Severity

The resyncAccounts override calls keyring.createAccount(...) directly and discards the returned KeyringAccount. The base class's resync path goes through this.createAccounts()createBip44Accounts(), which calls this.accounts.add(snapAccount.id) to register the newly created account in the provider's internal tracking set. Since the override skips this, re-created accounts won't appear in getAccounts() or be found by getAccount() after resync completes.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0fbd4e0. Configure here.

@marcopeereboom
Copy link
Copy Markdown
Author

BtcAccountProvider overrides the entire resyncAccounts method from SnapAccountProvider to fix a single concern: the base implementation re-creates missing snap accounts via createAccounts()createAccountV1(), which hardcodes addressType: BtcAccountType.P2wpkh and scope: BtcScope.Mainnet. This silently converts P2TR or testnet4 accounts, making the original taproot addresses unreachable.

The override copy-pastes ~60 lines of the snap-has-more deletion path and the MetaMask-has-more re-creation path from the base class, changing only the re-creation call to use account.type and account.scopes[0] instead of the hardcoded defaults.

If SnapAccountProvider.resyncAccounts evolves — new error handling, different deletion strategy, additional config knobs, observability hooks — BtcAccountProvider's override silently diverges. There's no compiler or test signal that the base changed. The two implementations will drift, and future bugs in the base fix won't propagate to BTC accounts.

This also sets a precedent: every new provider that supports multiple address types or network scopes will need its own full override with the same copy-paste pattern (Solana, future account types).

Here is an idea on how to fix it; make the base class's re-creation step a virtual method that subclasses can override surgically:

// In SnapAccountProvider:
protected recreateAccount(
  keyring: RestrictedSnapKeyring,
  account: Bip44Account<InternalAccount>,
  entropySource: EntropySourceId,
  groupIndex: number,
): Promise<void> {
  return this.createAccounts({
    type: AccountCreationType.Bip44DeriveIndex,
    entropySource,
    groupIndex,
  });
}

Then resyncAccounts calls this.recreateAccount(...) instead of this.createAccounts(...) directly. BtcAccountProvider overrides only recreateAccount:

// In BtcAccountProvider:
protected override async recreateAccount(
  keyring: RestrictedSnapKeyring,
  account: Bip44Account<InternalAccount>,
  entropySource: EntropySourceId,
  groupIndex: number,
): Promise<void> {
  const scope = account.scopes?.[0];
  if (!scope) {
    throw new Error(
      `Account ${account.id} has no scopes, cannot determine target network`,
    );
  }
  await withTimeout(
    () =>
      keyring.createAccount({
        entropySource,
        index: groupIndex,
        addressType: account.type,
        scope,
      }),
    this.config.createAccounts.timeoutMs,
  );
}

This keeps all sync logic (deletion, size comparison, error reporting, the removeAccount-before-create dance) in one place, and subclasses only define how a single account gets re-created. The recreateAccount hook is a small refactor in SnapAccountProvider and eliminates the copy-paste for all current and future providers.

@marcopeereboom marcopeereboom force-pushed the feat/btc-provider-p2tr-testnet4 branch from 0fbd4e0 to c29622e Compare May 12, 2026 09:04
marcopeereboom and others added 2 commits May 12, 2026 10:06
Widen BtcAccountProvider to recognize P2TR (taproot) accounts as
compatible Bitcoin accounts alongside P2WPKH. Add BtcScope.Testnet4
to the provider capabilities and account discovery scopes.

Default account creation remains P2WPKH — no behavioral change for
existing callers. P2TR accounts created through discovery or future
UI options are now correctly tracked by the provider.

Depends on MetaMask/snap-bitcoin-wallet P2TR support for the snap
to serve P2TR accounts via keyring_createAccount.
Override resyncAccounts to use the original account type and scope
when re-creating missing Snap accounts instead of hardcoded P2WPKH
and Mainnet defaults.

Guard against accounts with empty scopes to prevent undefined scope
in re-creation. Propagate re-creation failure after keyring removal
to surface inconsistent state. Align discovery scopes with advertised
capabilities by including BtcScope.Testnet. Remove redundant
Object.values check in isAccountCompatible.
@marcopeereboom marcopeereboom force-pushed the feat/btc-provider-p2tr-testnet4 branch from c29622e to 2adb7c4 Compare May 12, 2026 09:06
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