Skip to content

feat: snap keyring v2#501

Open
hmalik88 wants to merge 17 commits intomainfrom
hm/snap-keyring-v2
Open

feat: snap keyring v2#501
hmalik88 wants to merge 17 commits intomainfrom
hm/snap-keyring-v2

Conversation

@hmalik88
Copy link
Copy Markdown
Contributor

@hmalik88 hmalik88 commented Apr 6, 2026

Summary

Introduces SnapKeyringV2 as a per-snap keyring wrapper that implements the KeyringV2 interface, establishing a 1:1 relationship between account management snaps and their keyrings.

Architecture Diagrams

Architecture

Onboarding

Deserialize

Batch Create

What changed

  • SnapKeyringV2 implements KeyringV2 with createAccount, createAccounts, deleteAccount, submitRequest, getAccount, and getAccounts
  • Snap communication and parent coordination (state persistence, validation, locking) are injected via callbacks from SnapKeyring
  • SnapKeyring becomes a thin router by delegating createAccount, createAccounts, and #deleteAccount to the per-snap wrapper
  • normalizeAccountAddress extracted to shared util (was duplicated)
  • #accountIndex reverse map enables O(1) "which snap owns this account?" lookups
  • External serialization format is unchanged, existing accounts deserialize into per-snap wrappers automatically, no migration needed

Integration

Version bump in the clients. The public API of SnapKeyring is unchanged.

What's next

  • Per-snap locking (independent mutexes per wrapper)
  • Expose SnapKeyringV2 instances directly to KeyringController

Note

Medium Risk
Refactors core Snap account storage/routing and create/delete flows to go through new per-snap wrappers and a global account-id index, which could affect account ownership resolution and persistence if edge cases are missed. External serialized format is preserved, but deserialize/serialize logic is rewritten and should be validated against existing state.

Overview
Introduces SnapKeyringV2, a per-snap wrapper implementing KeyringV2 (account CRUD + submitRequest) backed by @metamask/keyring-sdk’s registry, and exports it publicly.

Refactors SnapKeyring to replace the flat #accounts map with per-snap wrappers plus a reverse #accountIndex for O(1) account-id → snap ownership routing; createAccount, createAccounts, and deletion now delegate to the wrapper, while serialize/deserialize re-group and rebuild wrappers but keep the external persisted format.

Moves normalizeAccountAddress into util, adds @metamask/keyring-sdk as a dependency/tsconfig reference, and updates tests (new SnapKeyringV2 suite plus adjusted expectations and an unknown-snap case for getAccountsBySnapId).

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

@hmalik88 hmalik88 changed the title feat: Snap Keyring V2 feat: snap keyring v2 Apr 6, 2026
@hmalik88
Copy link
Copy Markdown
Contributor Author

hmalik88 commented Apr 7, 2026

@metamaskbot publish-previews

@hmalik88
Copy link
Copy Markdown
Contributor Author

hmalik88 commented Apr 7, 2026

@metamaskbot publish-previews

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

Preview builds have been published. See these instructions (from the core monorepo) for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/account-api": "1.0.1-018fb62",
  "@metamask-previews/hw-wallet-sdk": "0.8.0-018fb62",
  "@metamask-previews/keyring-api": "22.0.0-018fb62",
  "@metamask-previews/eth-hd-keyring": "13.1.0-018fb62",
  "@metamask-previews/eth-ledger-bridge-keyring": "11.3.0-018fb62",
  "@metamask-previews/eth-money-keyring": "2.0.0-018fb62",
  "@metamask-previews/eth-qr-keyring": "1.1.0-018fb62",
  "@metamask-previews/eth-simple-keyring": "11.1.1-018fb62",
  "@metamask-previews/eth-trezor-keyring": "9.1.0-018fb62",
  "@metamask-previews/keyring-internal-api": "10.0.1-018fb62",
  "@metamask-previews/keyring-internal-snap-client": "9.0.1-018fb62",
  "@metamask-previews/keyring-sdk": "1.1.0-018fb62",
  "@metamask-previews/eth-snap-keyring": "20.0.0-018fb62",
  "@metamask-previews/keyring-snap-client": "8.2.1-018fb62",
  "@metamask-previews/keyring-snap-sdk": "8.0.0-018fb62",
  "@metamask-previews/keyring-utils": "3.2.0-018fb62"
}

// Default capabilities — parent updates this when snap metadata is available.
// We cast here because KeyringCapabilities requires a non-empty scopes array,
// but we don't have the snap metadata at construction time (e.g. during deserialization).
this.capabilities = { scopes: [] } as unknown as KeyringCapabilities;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will update when there's a new release of snaps-rpc-methods so I can use getKeyringCaveatCapabilities. Safe to leave as it is for now since we're not using capabilities at the moment.

@hmalik88
Copy link
Copy Markdown
Contributor Author

hmalik88 commented Apr 8, 2026

@metamaskbot publish-previews

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

Preview builds have been published. See these instructions (from the core monorepo) for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/account-api": "1.0.1-d23a1b9",
  "@metamask-previews/hw-wallet-sdk": "0.8.0-d23a1b9",
  "@metamask-previews/keyring-api": "22.0.0-d23a1b9",
  "@metamask-previews/eth-hd-keyring": "13.1.0-d23a1b9",
  "@metamask-previews/eth-ledger-bridge-keyring": "11.3.0-d23a1b9",
  "@metamask-previews/eth-money-keyring": "2.0.0-d23a1b9",
  "@metamask-previews/eth-qr-keyring": "1.1.0-d23a1b9",
  "@metamask-previews/eth-simple-keyring": "11.1.1-d23a1b9",
  "@metamask-previews/eth-trezor-keyring": "9.1.0-d23a1b9",
  "@metamask-previews/keyring-internal-api": "10.0.1-d23a1b9",
  "@metamask-previews/keyring-internal-snap-client": "9.0.1-d23a1b9",
  "@metamask-previews/keyring-sdk": "1.1.0-d23a1b9",
  "@metamask-previews/eth-snap-keyring": "20.0.0-d23a1b9",
  "@metamask-previews/keyring-snap-client": "8.2.1-d23a1b9",
  "@metamask-previews/keyring-snap-sdk": "8.0.0-d23a1b9",
  "@metamask-previews/keyring-utils": "3.2.0-d23a1b9"
}

@hmalik88 hmalik88 marked this pull request as ready for review April 8, 2026 10:20
@hmalik88 hmalik88 requested a review from a team as a code owner April 8, 2026 10:20
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

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit d23a1b9. Configure here.

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