-
-
Notifications
You must be signed in to change notification settings - Fork 276
feat(snap-account-service): add SnapAccountService
#8414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ccharly
wants to merge
3
commits into
main
Choose a base branch
from
cc/feat/snap-account-service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Changelog | ||
|
|
||
| All notable changes to this project will be documented in this file. | ||
|
|
||
| The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
| and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
|
||
| ## [Unreleased] | ||
|
|
||
| ### Added | ||
|
|
||
| - Add `SnapAccountService` ([#8414](https://github.com/MetaMask/core/pull/8414)) | ||
|
|
||
| [Unreleased]: https://github.com/MetaMask/core/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2026 MetaMask | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # `@metamask/snap-account-service` | ||
|
|
||
| Service for Account Management Snaps | ||
|
|
||
| ## Overview | ||
|
|
||
| `SnapAccountService` centralizes common operations around Snap-based account management, providing a single integration point for: | ||
|
|
||
| - **Migration** — Migrates accounts from the legacy monolithic v1 Snap keyring to per-Snap v2 keyrings. The migration runs once at initialization and is safe for concurrent callers (subsequent calls await the same promise). | ||
| - **Readiness** — Ensures a Snap is fully ready for account operations before proceeding: migration is complete, a v2 keyring exists for the Snap, and the Snap platform itself is initialized. | ||
| - **Keyring message handling** — Routes messages from Snap keyrings to the appropriate per-Snap keyring instance, including lazy keyring creation for event-driven v1 flows. | ||
|
|
||
| The service exposes its functionality through the MetaMask messenger pattern and depends on `KeyringController` and `SnapController`. | ||
|
|
||
| ## Installation | ||
|
|
||
| `yarn add @metamask/snap-account-service` | ||
|
|
||
| or | ||
|
|
||
| `npm install @metamask/snap-account-service` | ||
|
|
||
| ## Contributing | ||
|
|
||
| This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * For a detailed explanation regarding each configuration property and type check, visit: | ||
| * https://jestjs.io/docs/configuration | ||
| */ | ||
|
|
||
| const merge = require('deepmerge'); | ||
| const path = require('path'); | ||
|
|
||
| const baseConfig = require('../../jest.config.packages'); | ||
|
|
||
| const displayName = path.basename(__dirname); | ||
|
|
||
| module.exports = merge(baseConfig, { | ||
| // The display name when running multiple projects | ||
| displayName, | ||
|
|
||
| // An object that configures minimum threshold enforcement for coverage results | ||
| coverageThreshold: { | ||
| global: { | ||
| branches: 100, | ||
| functions: 100, | ||
| lines: 100, | ||
| statements: 100, | ||
| }, | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| { | ||
| "name": "@metamask/snap-account-service", | ||
| "version": "0.0.0", | ||
| "description": "Service for Account Management Snaps", | ||
| "keywords": [ | ||
| "Ethereum", | ||
| "MetaMask" | ||
| ], | ||
| "homepage": "https://github.com/MetaMask/core/tree/main/packages/snap-account-service#readme", | ||
| "bugs": { | ||
| "url": "https://github.com/MetaMask/core/issues" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/MetaMask/core.git" | ||
| }, | ||
| "license": "MIT", | ||
| "sideEffects": false, | ||
| "exports": { | ||
| ".": { | ||
| "import": { | ||
| "types": "./dist/index.d.mts", | ||
| "default": "./dist/index.mjs" | ||
| }, | ||
| "require": { | ||
| "types": "./dist/index.d.cts", | ||
| "default": "./dist/index.cjs" | ||
| } | ||
| }, | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "main": "./dist/index.cjs", | ||
| "types": "./dist/index.d.cts", | ||
| "files": [ | ||
| "dist/" | ||
| ], | ||
| "scripts": { | ||
| "build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references", | ||
| "build:all": "ts-bridge --project tsconfig.build.json --verbose --clean", | ||
| "build:docs": "typedoc", | ||
| "changelog:update": "../../scripts/update-changelog.sh @metamask/snap-account-service", | ||
| "changelog:validate": "../../scripts/validate-changelog.sh @metamask/snap-account-service", | ||
| "messenger-action-types:check": "tsx ../../packages/messenger-cli/src/cli.ts --check", | ||
| "messenger-action-types:generate": "tsx ../../packages/messenger-cli/src/cli.ts --generate", | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| "since-latest-release": "../../scripts/since-latest-release.sh", | ||
| "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", | ||
| "test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache", | ||
| "test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose", | ||
| "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch" | ||
| }, | ||
| "dependencies": { | ||
| "@metamask/messenger": "^1.1.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@metamask/auto-changelog": "^6.1.0", | ||
| "@ts-bridge/cli": "^0.6.4", | ||
| "@types/jest": "^29.5.14", | ||
| "deepmerge": "^4.2.2", | ||
| "jest": "^29.7.0", | ||
| "ts-jest": "^29.2.5", | ||
| "tsx": "^4.20.5", | ||
| "typedoc": "^0.25.13", | ||
| "typedoc-plugin-missing-exports": "^2.0.0", | ||
| "typescript": "~5.3.3" | ||
| }, | ||
| "engines": { | ||
| "node": "^18.18 || >=20" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public", | ||
| "registry": "https://registry.npmjs.org/" | ||
| } | ||
| } | ||
76 changes: 76 additions & 0 deletions
76
packages/snap-account-service/src/SnapAccountService.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { Messenger, MOCK_ANY_NAMESPACE } from '@metamask/messenger'; | ||
| import type { | ||
| MockAnyNamespace, | ||
| MessengerActions, | ||
| MessengerEvents, | ||
| } from '@metamask/messenger'; | ||
|
|
||
| import type { SnapAccountServiceMessenger } from './SnapAccountService'; | ||
| import { SnapAccountService } from './SnapAccountService'; | ||
|
|
||
| /** | ||
| * The type of the messenger populated with all external actions and events | ||
| * required by the service under test. | ||
| */ | ||
| type RootMessenger = Messenger< | ||
| MockAnyNamespace, | ||
| MessengerActions<SnapAccountServiceMessenger>, | ||
| MessengerEvents<SnapAccountServiceMessenger> | ||
| >; | ||
|
|
||
| /** | ||
| * Constructs the root messenger for the service under test. | ||
| * | ||
| * @returns The root messenger. | ||
| */ | ||
| function getRootMessenger(): RootMessenger { | ||
| return new Messenger({ namespace: MOCK_ANY_NAMESPACE }); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs the messenger for the service under test. | ||
| * | ||
| * @param rootMessenger - The root messenger. | ||
| * @returns The service-specific messenger. | ||
| */ | ||
| function getMessenger( | ||
| rootMessenger: RootMessenger, | ||
| ): SnapAccountServiceMessenger { | ||
| return new Messenger({ | ||
| namespace: 'SnapAccountService', | ||
| parent: rootMessenger, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs the service under test with sensible defaults. | ||
| * | ||
| * @param args - The arguments to this function. | ||
| * @param args.options - The options that the service constructor takes. | ||
| * @returns The new service, root messenger, and service messenger. | ||
| */ | ||
| function setup({ | ||
| options = {}, | ||
| }: { | ||
| options?: Partial<ConstructorParameters<typeof SnapAccountService>[0]>; | ||
| } = {}): { | ||
| service: SnapAccountService; | ||
| rootMessenger: RootMessenger; | ||
| messenger: SnapAccountServiceMessenger; | ||
| } { | ||
| const rootMessenger = getRootMessenger(); | ||
| const messenger = getMessenger(rootMessenger); | ||
| const service = new SnapAccountService({ messenger, ...options }); | ||
|
|
||
| return { service, rootMessenger, messenger }; | ||
| } | ||
|
|
||
| describe('SnapAccountService', () => { | ||
| describe('init', () => { | ||
| it('resolves without throwing', async () => { | ||
| const { service } = setup(); | ||
|
|
||
| expect(await service.init()).toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import type { Messenger } from '@metamask/messenger'; | ||
|
|
||
| /** | ||
| * The name of the {@link SnapAccountService}, used to namespace the service's | ||
| * actions and events. | ||
| */ | ||
| export const serviceName = 'SnapAccountService'; | ||
|
|
||
| /** | ||
| * All of the methods within {@link SnapAccountService} that are exposed via | ||
| * the messenger. | ||
| */ | ||
| const MESSENGER_EXPOSED_METHODS = [] as const; | ||
|
|
||
| /** | ||
| * Actions that {@link SnapAccountService} exposes to other consumers. | ||
| */ | ||
| export type SnapAccountServiceActions = never; | ||
|
|
||
| /** | ||
| * Actions from other messengers that {@link SnapAccountService} calls. | ||
| */ | ||
| type AllowedActions = never; | ||
|
|
||
| /** | ||
| * Events that {@link SnapAccountService} exposes to other consumers. | ||
| */ | ||
| export type SnapAccountServiceEvents = never; | ||
|
|
||
| /** | ||
| * Events from other messengers that {@link SnapAccountService} subscribes to. | ||
| */ | ||
| type AllowedEvents = never; | ||
|
|
||
| /** | ||
| * The messenger which is restricted to actions and events accessed by | ||
| * {@link SnapAccountService}. | ||
| */ | ||
| export type SnapAccountServiceMessenger = Messenger< | ||
| typeof serviceName, | ||
| SnapAccountServiceActions | AllowedActions, | ||
| SnapAccountServiceEvents | AllowedEvents | ||
| >; | ||
|
|
||
| /** | ||
| * Service responsible for managing account management snaps. | ||
| */ | ||
| export class SnapAccountService { | ||
| /** | ||
| * The name of the service. | ||
| */ | ||
| readonly name: typeof serviceName; | ||
|
|
||
| readonly #messenger: SnapAccountServiceMessenger; | ||
|
|
||
| /** | ||
| * Constructs a new {@link SnapAccountService}. | ||
| * | ||
| * @param args - The constructor arguments. | ||
| * @param args.messenger - The messenger suited for this service. | ||
| */ | ||
| constructor({ messenger }: { messenger: SnapAccountServiceMessenger }) { | ||
| this.name = serviceName; | ||
| this.#messenger = messenger; | ||
|
|
||
| this.#messenger.registerMethodActionHandlers( | ||
| this, | ||
| MESSENGER_EXPOSED_METHODS, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Initializes the snap account service. | ||
| */ | ||
| async init(): Promise<void> { | ||
| // TODO: Add initialization logic here. | ||
|
david0xd marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export { SnapAccountService } from './SnapAccountService'; | ||
| export type { | ||
| SnapAccountServiceActions, | ||
| SnapAccountServiceEvents, | ||
| SnapAccountServiceMessenger, | ||
| } from './SnapAccountService'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "extends": "../../tsconfig.packages.build.json", | ||
| "compilerOptions": { | ||
| "baseUrl": "./", | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| }, | ||
| "references": [{ "path": "../messenger/tsconfig.build.json" }], | ||
| "include": ["../../types", "./src"] | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.packages.json", | ||
| "compilerOptions": { | ||
| "baseUrl": "./" | ||
| }, | ||
| "references": [{ "path": "../messenger" }], | ||
| "include": ["../../types", "./src"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "entryPoints": ["./src/index.ts"], | ||
| "excludePrivate": true, | ||
| "hideGenerator": true, | ||
| "out": "docs", | ||
| "tsconfig": "./tsconfig.build.json" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.