Skip to content

Commit 3f59e75

Browse files
committed
feat: add KeyringController:withLocked action
1 parent 7226ac1 commit 3f59e75

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

packages/keyring-controller/jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ module.exports = merge(baseConfig, {
1717
// An object that configures minimum threshold enforcement for coverage results
1818
coverageThreshold: {
1919
global: {
20-
branches: 94.55,
20+
branches: 94.8,
2121
functions: 100,
22-
lines: 98.85,
23-
statements: 98.86,
22+
lines: 98.89,
23+
statements: 98.9,
2424
},
2525
},
2626

packages/keyring-controller/src/KeyringController.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,6 +2256,18 @@ describe('KeyringController', () => {
22562256
expect(await controller.withKeyring(selector, fn)).toBe('hello');
22572257
});
22582258
});
2259+
2260+
it('should throw an error if the keyring is not found', async () => {
2261+
await withController(async ({ controller }) => {
2262+
const selector = { type: 'foo' };
2263+
const fn = jest.fn();
2264+
2265+
await expect(controller.withKeyring(selector, fn)).rejects.toThrow(
2266+
KeyringControllerError.KeyringNotFound,
2267+
);
2268+
expect(fn).not.toHaveBeenCalled();
2269+
});
2270+
});
22592271
});
22602272

22612273
describe('when the keyring is selected by address', () => {
@@ -2283,6 +2295,20 @@ describe('KeyringController', () => {
22832295
expect(await controller.withKeyring(selector, fn)).toBe('hello');
22842296
});
22852297
});
2298+
2299+
it('should throw an error if the keyring is not found', async () => {
2300+
await withController(async ({ controller }) => {
2301+
const selector = {
2302+
address: '0x4584d2B4905087A100420AFfCe1b2d73fC69B8E4' as Hex,
2303+
};
2304+
const fn = jest.fn();
2305+
2306+
await expect(controller.withKeyring(selector, fn)).rejects.toThrow(
2307+
'KeyringController - No keyring found. Error info: There are keyrings, but none match the address',
2308+
);
2309+
expect(fn).not.toHaveBeenCalled();
2310+
});
2311+
});
22862312
});
22872313
});
22882314

@@ -2947,6 +2973,9 @@ describe('KeyringController', () => {
29472973
jest
29482974
.spyOn(KeyringController.prototype, 'signUserOperation')
29492975
.mockResolvedValue('0x1234');
2976+
jest
2977+
.spyOn(KeyringController.prototype, 'withKeyring')
2978+
.mockResolvedValue(jest.fn());
29502979
});
29512980

29522981
describe('signMessage', () => {
@@ -3059,6 +3088,19 @@ describe('KeyringController', () => {
30593088
});
30603089
});
30613090

3091+
describe('withKeyring', () => {
3092+
it('should call withKeyring', async () => {
3093+
await withController(async ({ controller, messenger }) => {
3094+
const selector = { type: KeyringTypes.hd };
3095+
const fn = async () => 'foo';
3096+
3097+
await messenger.call('KeyringController:withKeyring', selector, fn);
3098+
3099+
expect(controller.withKeyring).toHaveBeenCalledWith(selector, fn);
3100+
});
3101+
});
3102+
});
3103+
30623104
describe('prepareUserOperation', () => {
30633105
const chainId = '0x1';
30643106
const executionContext = {

packages/keyring-controller/src/KeyringController.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,21 @@ export type KeyringControllerGetEncryptionPublicKeyAction = {
124124
handler: KeyringController['getEncryptionPublicKey'];
125125
};
126126

127+
/**
128+
* @deprecated Use of this method is discouraged as actions executed directly on
129+
* keyrings are not being reflected in the KeyringController state and not
130+
* persisted in the vault. Use `KeyringController:withKeyring` instead.
131+
*/
127132
export type KeyringControllerGetKeyringsByTypeAction = {
128133
type: `${typeof name}:getKeyringsByType`;
129134
handler: KeyringController['getKeyringsByType'];
130135
};
131136

137+
/**
138+
* @deprecated Use of this method is discouraged as actions executed directly on
139+
* keyrings are not being reflected in the KeyringController state and not
140+
* persisted in the vault. Use `KeyringController:withKeyring` instead.
141+
*/
132142
export type KeyringControllerGetKeyringForAccountAction = {
133143
type: `${typeof name}:getKeyringForAccount`;
134144
handler: KeyringController['getKeyringForAccount'];
@@ -139,6 +149,10 @@ export type KeyringControllerGetAccountsAction = {
139149
handler: KeyringController['getAccounts'];
140150
};
141151

152+
/**
153+
* @deprecated This action is being phased out in favor of
154+
* `KeyringController:withKeyring`.
155+
*/
142156
export type KeyringControllerPersistAllKeyringsAction = {
143157
type: `${typeof name}:persistAllKeyrings`;
144158
handler: KeyringController['persistAllKeyrings'];
@@ -159,6 +173,11 @@ export type KeyringControllerSignUserOperationAction = {
159173
handler: KeyringController['signUserOperation'];
160174
};
161175

176+
export type KeyringControllerWithKeyringAction = {
177+
type: `${typeof name}:withKeyring`;
178+
handler: KeyringController['withKeyring'];
179+
};
180+
162181
export type KeyringControllerStateChangeEvent = {
163182
type: `${typeof name}:stateChange`;
164183
payload: [KeyringControllerState, Patch[]];
@@ -192,6 +211,7 @@ export type KeyringControllerActions =
192211
| KeyringControllerDecryptMessageAction
193212
| KeyringControllerGetEncryptionPublicKeyAction
194213
| KeyringControllerGetAccountsAction
214+
| KeyringControllerWithKeyringAction
195215
| KeyringControllerGetKeyringsByTypeAction
196216
| KeyringControllerGetKeyringForAccountAction
197217
| KeyringControllerPersistAllKeyringsAction
@@ -1380,10 +1400,10 @@ export class KeyringController extends BaseController<
13801400
}
13811401

13821402
if (!keyring) {
1383-
throw new Error(KeyringControllerError.NoKeyring);
1403+
throw new Error(KeyringControllerError.KeyringNotFound);
13841404
}
13851405

1386-
return fn(keyring as EthKeyring<Json>);
1406+
return fn(keyring);
13871407
});
13881408
}
13891409

@@ -1590,6 +1610,11 @@ export class KeyringController extends BaseController<
15901610
`${name}:signUserOperation`,
15911611
this.signUserOperation.bind(this),
15921612
);
1613+
1614+
this.messagingSystem.registerActionHandler(
1615+
`${name}:withKeyring`,
1616+
this.withKeyring.bind(this),
1617+
);
15931618
}
15941619

15951620
/**

packages/keyring-controller/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export enum KeyringControllerError {
22
NoKeyring = 'KeyringController - No keyring found',
3+
KeyringNotFound = 'KeyringController - Keyring not found.',
34
WrongPasswordType = 'KeyringController - Password must be of type string.',
45
NoFirstAccount = 'KeyringController - First Account not found.',
56
DuplicatedAccount = 'KeyringController - The account you are trying to import is a duplicate',

0 commit comments

Comments
 (0)