Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/snaps-rpc-methods/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = deepmerge(baseConfig, {
global: {
branches: 95.68,
functions: 98.75,
lines: 98.98,
lines: 98.99,
statements: 98.69,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('specification', () => {
it('builds specification', () => {
const methodHooks = {
getSnapKeyring: jest.fn(),
getUnlockPromise: jest.fn(),
};

expect(
Expand Down Expand Up @@ -61,6 +62,7 @@ describe('builder', () => {
manageAccountsBuilder.specificationBuilder({
methodHooks: {
getSnapKeyring: jest.fn(),
getUnlockPromise: jest.fn(),
},
}),
).toMatchObject({
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -127,13 +133,15 @@ 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')
.mockResolvedValue(true);

const manageAccounts = manageAccountsImplementation({
getSnapKeyring,
getUnlockPromise,
});

const requestResponse = await manageAccounts({
Expand Down
13 changes: 13 additions & 0 deletions packages/snaps-rpc-methods/src/restricted/manageAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export type ManageAccountsMethodHooks = {
message: Message,
) => Promise<Json>;
}>;

/**
* Wait for the client to be unlocked.
*
* @returns A promise that resolves once the client is unlocked.
*/
getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
};

type ManageAccountsSpecificationBuilderOptions = {
Expand Down Expand Up @@ -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<ManageAccountsParams>,
Expand All @@ -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);
};
Expand All @@ -115,5 +127,6 @@ export const manageAccountsBuilder = Object.freeze({
specificationBuilder,
methodHooks: {
getSnapKeyring: true,
getUnlockPromise: true,
},
} as const);
Loading