Skip to content

feat(accounts-controller): add support for Snap keyring v2#8513

Merged
ccharly merged 12 commits intomainfrom
cc/feat/accounts-controller-snap-keyring-v2
Apr 30, 2026
Merged

feat(accounts-controller): add support for Snap keyring v2#8513
ccharly merged 12 commits intomainfrom
cc/feat/accounts-controller-snap-keyring-v2

Conversation

@ccharly
Copy link
Copy Markdown
Contributor

@ccharly ccharly commented Apr 17, 2026

Explanation

Adding support for Snap keyring v2 for this controller. For now, those keyrings are not in use, but we will need this to be able to the accounts associated with those new keyrings.

References

N/A

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
Touches account synchronization logic and adds a new path for constructing internal accounts from Snap v2 keyrings; incorrect matching or metadata injection could cause missing/incorrect accounts during keyring state updates.

Overview
Adds support for SnapKeyring v2 accounts so the AccountsController can materialize Snap v2 keyring addresses into internal accounts during KeyringController:stateChange and updateAccounts.

This introduces Snap v2 type detection (KeyringType.Snap), looks up accounts via SnapKeyringV2.lookupByAddress across per-snap keyring instances, and injects minimal metadata (including metadata.snap.id) when converting those results into internal accounts. Utilities and tests are updated to recognize/label the v2 keyring type and cover edge cases where the v2 keyring instance/account cannot be found, and the changelog documents the new support.

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

@ccharly ccharly force-pushed the cc/feat/accounts-controller-snap-keyring-v2 branch from 2851f32 to 235c68e Compare April 17, 2026 16:37
Comment on lines -890 to -909
const generatePatch = (): StatePatch => {
return {
previous: {},
added: [],
updated: [],
removed: [],
};
};
const patches = {
snap: generatePatch(),
normal: generatePatch(),
};

// Gets the patch object based on the keyring type (since Snap accounts and other accounts
// are handled differently).
const patchOf = (type: string): StatePatch => {
if (isSnapKeyringType(type)) {
return patches.snap;
}
return patches.normal;
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.

Just removed all this since we don't need that separation at all!

Comment on lines +877 to +889
metadata: {
name: '',
importTime: Date.now(),
lastSelected: 0,
keyring: {
type: KeyringType.Snap,
},
snap: {
name: keyring.snapId,
enabled: true,
id: keyring.snapId,
},
},
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.

The new Snap keyring v2 only use KeyringAccount, thus we don't have any metadata for those!

@ccharly ccharly marked this pull request as ready for review April 17, 2026 17:41
@ccharly ccharly requested review from a team as code owners April 17, 2026 17:41
Comment thread packages/accounts-controller/src/AccountsController.ts
Comment thread packages/accounts-controller/src/utils.ts
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 c9d2191. Configure here.

Comment thread packages/accounts-controller/src/AccountsController.ts
@ccharly ccharly enabled auto-merge April 23, 2026 20:52
Comment on lines +878 to +880
// Snap keyring v2 are "per-Snaps", so we need to iterate over all of them to find the account.
for (const keyring of keyrings) {
if (keyring instanceof SnapKeyringV2) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems inefficient, can we keep a map in memory for faster lookups?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe thats an overkill given that there won't be hundreds of snap keyrings

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.

No actually I added that a safe-guard (with a small runtime cost yep) but :getKeyringsByType should """guarantee""" us that we get SnapKeyringV2 instances.

Also, I couldn't use :withKeyring* here because this code has to be synchronous 😬 Ultimately, account storage will get delegated to the KeyringController that will keep all KeyringAccounts in memory (for synchronous access).

We won't need that trick anymore once everything will be migrated to v2!

Copy link
Copy Markdown
Member

@gantunesr gantunesr left a comment

Choose a reason for hiding this comment

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

lgtm

@ccharly ccharly added this pull request to the merge queue Apr 30, 2026
Merged via the queue into main with commit 247f06c Apr 30, 2026
366 checks passed
@ccharly ccharly deleted the cc/feat/accounts-controller-snap-keyring-v2 branch April 30, 2026 02:33
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