Skip to content

feat(keyring-controller): add withController for atomic operations over multiple keyrings#8416

Open
ccharly wants to merge 19 commits intocc/chore/bump-accounts-depsfrom
cc/feat/with-keyrings
Open

feat(keyring-controller): add withController for atomic operations over multiple keyrings#8416
ccharly wants to merge 19 commits intocc/chore/bump-accounts-depsfrom
cc/feat/with-keyrings

Conversation

@ccharly
Copy link
Copy Markdown
Contributor

@ccharly ccharly commented Apr 9, 2026

Explanation

Today there's no way to make multiple operations in an "atomic" (read transactional) way.

A good example of this is if you want to use a keyring using withKeyring that's not existing yet (I'm omitting the createIfMissing variants, as we wanted to move away from this pattern).

To do this in a safe way, you usually have to use your own lock to make sure you can get-or-create the keyring and prevent concurrent keyring creations.

This new withController is based on the withKeyring but with an access to a "restricted" state and methods of the controller. This way, you can interact with multiple keyring at once while being guarded (to prevent race-conditions) by the controller's global lock.

The former problem can then be written that way now:

const account = await keyringController.withController(async (controller) => {
  // Here, `controller.keyrings` is a "view" on the existing keyrings (instances), only valid
  // for this block.
  let keyring: MyKeyring | undefined = controller.keyrings.find(isMyKeyring);
  if (!keyring) {
    const { keyring: myKeyring } = await controller.addNewKeyring({ type: 'My Keyring', data: { ... }});
    keyring = myKeyring;
  }
  
  const [account] = await keyring.createAccounts(...);
  return account;
});

This will also be used to write the migration from the existing SnapKeyring (1 for ALL Snaps) to multiple SnapKeyring (1 PER Snap) in a safe way like:

await keyringController.withController(async (controller) => {
  const accounts: Map<SnapId, KeyringAccount[]> = new Map();

  // Get existing Snap accounts from the single Snap keyring instance we have today.
  const keyring: SnapKeyring | undefined = controller.keyrings.find(isSnapKeyring);
  if (keyring) {
    for (const account of keyring.listAccounts()) {
      accounts[account.metadata.snap.id] ??= [];
      accounts[account.metadata.snap.id].push(account);
    }
  }
  
  // Re-create all new Snap keyrings, 1 per Snap.
  for (const [snapId, snapAccounts] of accounts.entries()) {
    await controller.addNewKeyring({ type: 'Snap keyring', data: snapAccounts });
  }
  
  // We can safely remove the existing Snap keyring now.
  await controller.removeKeyring(...);
});

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
Adds a new transactional API that can create/remove keyrings and changes how keyring instances are managed/persisted, plus broad keyring dependency upgrades; failures could impact vault persistence or keyring lifecycle.

Overview
Introduces KeyringController.withController, a new atomic, controller-wide transaction API that exposes a restricted view of keyrings and stages addNewKeyring/removeKeyring changes with automatic persist-or-rollback behavior and lifecycle cleanup.

Updates messenger action types and tests to support the new KeyringController:withController action, tightens unsafe direct keyring instance leakage checks, and adjusts keyring entry typing to include optional v2 instances.

Separately, advances the keyring v2 transition by mapping @metamask/*/v2 imports in Jest, switching some types/imports to @metamask/keyring-api/v2, adding Stellar account sort-order support in account-tree-controller, and bumping multiple packages to newer @metamask/keyring-* (and related) versions with corresponding yarn.lock updates.

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

@ccharly ccharly force-pushed the cc/feat/with-keyrings branch 2 times, most recently from e1b0a6b to 001d092 Compare April 13, 2026 15:19
@ccharly ccharly changed the title feat(keyring-controller): add withController for atomic operations over multiple keyrings feat(keyring-controller): add withController for atomic operations over multiple keyrings Apr 13, 2026
@ccharly ccharly marked this pull request as ready for review April 13, 2026 15:33
@ccharly ccharly requested review from a team as code owners April 13, 2026 15:33
Comment thread packages/keyring-controller/src/KeyringController.ts Outdated
@ccharly ccharly force-pushed the cc/feat/with-keyrings branch from b74e2c3 to 02a9fac Compare April 13, 2026 16:10
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 3 potential issues.

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 02a9fac. Configure here.

Comment thread packages/keyring-controller/src/KeyringController-method-action-types.ts Outdated
Comment thread packages/keyring-controller/src/KeyringController.ts
Comment thread packages/keyring-controller/src/KeyringController.ts Outdated
@ccharly ccharly force-pushed the cc/feat/with-keyrings branch from 21276f6 to 0f03c1f Compare April 15, 2026 12:41
@ccharly ccharly requested a review from a team as a code owner April 15, 2026 12:41
@ccharly ccharly requested review from a team as code owners April 15, 2026 12:41
@ccharly ccharly changed the base branch from main to cc/chore/bump-accounts-deps April 15, 2026 12:42
@ccharly ccharly removed request for a team April 15, 2026 12:42
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