From d1f8ca2fff250090510aa9b2ac5f15dd5a109c3d Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Fri, 14 Feb 2025 14:42:42 +0100 Subject: [PATCH 1/2] feat: add `KeyringController:withKeyring` action --- .../src/KeyringController.test.ts | 22 +++++++++++++++++++ .../src/KeyringController.ts | 13 ++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/keyring-controller/src/KeyringController.test.ts b/packages/keyring-controller/src/KeyringController.test.ts index b9fab0c14a..19daf6e49e 100644 --- a/packages/keyring-controller/src/KeyringController.test.ts +++ b/packages/keyring-controller/src/KeyringController.test.ts @@ -3838,6 +3838,28 @@ describe('KeyringController', () => { }); }); }); + + describe('withKeyring', () => { + it('should call withKeyring', async () => { + await withController( + { keyringBuilders: [keyringBuilderFactory(MockKeyring)] }, + async ({ controller, messenger }) => { + await controller.addNewKeyring(MockKeyring.type); + + const actionReturnValue = await messenger.call( + 'KeyringController:withKeyring', + { type: MockKeyring.type }, + async (keyring) => { + expect(keyring.type).toBe(MockKeyring.type); + return keyring.type; + }, + ); + + expect(actionReturnValue).toBe(MockKeyring.type); + }, + ); + }); + }); }); describe('run conditions', () => { diff --git a/packages/keyring-controller/src/KeyringController.ts b/packages/keyring-controller/src/KeyringController.ts index d28b07ce93..55f3acad09 100644 --- a/packages/keyring-controller/src/KeyringController.ts +++ b/packages/keyring-controller/src/KeyringController.ts @@ -177,6 +177,11 @@ export type KeyringControllerAddNewAccountAction = { handler: KeyringController['addNewAccount']; }; +export type KeyringControllerWithKeyringAction = { + type: `${typeof name}:withKeyring`; + handler: KeyringController['withKeyring']; +}; + export type KeyringControllerStateChangeEvent = { type: `${typeof name}:stateChange`; payload: [KeyringControllerState, Patch[]]; @@ -216,7 +221,8 @@ export type KeyringControllerActions = | KeyringControllerPrepareUserOperationAction | KeyringControllerPatchUserOperationAction | KeyringControllerSignUserOperationAction - | KeyringControllerAddNewAccountAction; + | KeyringControllerAddNewAccountAction + | KeyringControllerWithKeyringAction; export type KeyringControllerEvents = | KeyringControllerStateChangeEvent @@ -1796,6 +1802,11 @@ export class KeyringController extends BaseController< `${name}:addNewAccount`, this.addNewAccount.bind(this), ); + + this.messagingSystem.registerActionHandler( + `${name}:withKeyring`, + this.withKeyring.bind(this), + ); } /** From fab93d7d89f737b0b8d4809de8da7e03a144fbf8 Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Fri, 14 Feb 2025 14:55:54 +0100 Subject: [PATCH 2/2] add changelog entry --- packages/keyring-controller/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/keyring-controller/CHANGELOG.md b/packages/keyring-controller/CHANGELOG.md index 1390ffdb22..59c0a21205 100644 --- a/packages/keyring-controller/CHANGELOG.md +++ b/packages/keyring-controller/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `KeyringController:withKeyring` action ([#5332](https://github.com/MetaMask/core/pull/5332)) + - The action can be used to consume the `withKeyring` method of the `KeyringController` class + ## [19.1.0] ### Added