Skip to content

Commit

Permalink
feat: add KeyringController:withKeyring action (#5332)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito authored Feb 14, 2025
1 parent f8c8a90 commit 731da56
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/keyring-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
13 changes: 12 additions & 1 deletion packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]];
Expand Down Expand Up @@ -216,7 +221,8 @@ export type KeyringControllerActions =
| KeyringControllerPrepareUserOperationAction
| KeyringControllerPatchUserOperationAction
| KeyringControllerSignUserOperationAction
| KeyringControllerAddNewAccountAction;
| KeyringControllerAddNewAccountAction
| KeyringControllerWithKeyringAction;

export type KeyringControllerEvents =
| KeyringControllerStateChangeEvent
Expand Down Expand Up @@ -1796,6 +1802,11 @@ export class KeyringController extends BaseController<
`${name}:addNewAccount`,
this.addNewAccount.bind(this),
);

this.messagingSystem.registerActionHandler(
`${name}:withKeyring`,
this.withKeyring.bind(this),
);
}

/**
Expand Down

0 comments on commit 731da56

Please sign in to comment.