diff --git a/packages/snaps-rpc-methods/jest.config.js b/packages/snaps-rpc-methods/jest.config.js index bda41c9c0b..6cadef39ec 100644 --- a/packages/snaps-rpc-methods/jest.config.js +++ b/packages/snaps-rpc-methods/jest.config.js @@ -12,7 +12,7 @@ module.exports = deepmerge(baseConfig, { global: { branches: 95.68, functions: 98.75, - lines: 98.98, + lines: 98.99, statements: 98.69, }, }, diff --git a/packages/snaps-rpc-methods/src/restricted/manageAccounts.test.ts b/packages/snaps-rpc-methods/src/restricted/manageAccounts.test.ts index 44503f44a3..a51283f87b 100644 --- a/packages/snaps-rpc-methods/src/restricted/manageAccounts.test.ts +++ b/packages/snaps-rpc-methods/src/restricted/manageAccounts.test.ts @@ -28,6 +28,7 @@ describe('specification', () => { it('builds specification', () => { const methodHooks = { getSnapKeyring: jest.fn(), + getUnlockPromise: jest.fn(), }; expect( @@ -61,6 +62,7 @@ describe('builder', () => { manageAccountsBuilder.specificationBuilder({ methodHooks: { getSnapKeyring: jest.fn(), + getUnlockPromise: jest.fn(), }, }), ).toMatchObject({ @@ -83,9 +85,11 @@ describe('manageAccountsImplementation', () => { it('should throw params are not set', async () => { const mockKeyring = new SnapKeyringMock(); const getSnapKeyring = jest.fn().mockResolvedValue(mockKeyring); + const getUnlockPromise = jest.fn(); const manageAccounts = manageAccountsImplementation({ getSnapKeyring, + getUnlockPromise, }); await expect( @@ -105,9 +109,11 @@ describe('manageAccountsImplementation', () => { it('should throw params accountId is not set', async () => { const mockKeyring = new SnapKeyringMock(); const getSnapKeyring = jest.fn().mockResolvedValue(mockKeyring); + const getUnlockPromise = jest.fn(); const manageAccounts = manageAccountsImplementation({ getSnapKeyring, + getUnlockPromise, }); await expect( @@ -127,6 +133,7 @@ describe('manageAccountsImplementation', () => { it('should route request to snap keyring', async () => { const mockKeyring = new SnapKeyringMock(); const getSnapKeyring = jest.fn().mockResolvedValue(mockKeyring); + const getUnlockPromise = jest.fn(); const createAccountSpy = jest .spyOn(mockKeyring, 'handleKeyringSnapMessage') @@ -134,6 +141,7 @@ describe('manageAccountsImplementation', () => { const manageAccounts = manageAccountsImplementation({ getSnapKeyring, + getUnlockPromise, }); const requestResponse = await manageAccounts({ diff --git a/packages/snaps-rpc-methods/src/restricted/manageAccounts.ts b/packages/snaps-rpc-methods/src/restricted/manageAccounts.ts index 220fa25ba2..9afe4525de 100644 --- a/packages/snaps-rpc-methods/src/restricted/manageAccounts.ts +++ b/packages/snaps-rpc-methods/src/restricted/manageAccounts.ts @@ -44,6 +44,13 @@ export type ManageAccountsMethodHooks = { message: Message, ) => Promise; }>; + + /** + * Wait for the client to be unlocked. + * + * @returns A promise that resolves once the client is unlocked. + */ + getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise; }; type ManageAccountsSpecificationBuilderOptions = { @@ -89,12 +96,14 @@ export const specificationBuilder: PermissionSpecificationBuilder< * * @param hooks - The RPC method hooks. * @param hooks.getSnapKeyring - A function to get the snap keyring. + * @param hooks.getUnlockPromise - The function to get the unlock promise. * @returns The method implementation which either returns `null` for a * successful state update/deletion or returns the decrypted state. * @throws If the params are invalid. */ export function manageAccountsImplementation({ getSnapKeyring, + getUnlockPromise, }: ManageAccountsMethodHooks) { return async function manageAccounts( options: RestrictedMethodOptions, @@ -105,6 +114,9 @@ export function manageAccountsImplementation({ } = options; assert(params, SnapMessageStruct); + + await getUnlockPromise(true); + const keyring = await getSnapKeyring(origin); return await keyring.handleKeyringSnapMessage(origin, params); }; @@ -115,5 +127,6 @@ export const manageAccountsBuilder = Object.freeze({ specificationBuilder, methodHooks: { getSnapKeyring: true, + getUnlockPromise: true, }, } as const);