diff --git a/src/snap-keyring.test.ts b/src/snap-keyring.test.ts index 41b8d0d5..5a7954f5 100644 --- a/src/snap-keyring.test.ts +++ b/src/snap-keyring.test.ts @@ -35,22 +35,28 @@ describe('SnapKeyring', () => { keyring = new SnapKeyring(mockSnapController as unknown as SnapController); for (const account of accounts) { mockSnapController.handleRequest.mockResolvedValueOnce(accounts); - await keyring.handleKeyringSnapMessage(snapId, [ - 'create', - account.address, - ]); + await keyring.handleKeyringSnapMessage(snapId, { + method: 'createAccount', + params: { + id: account.id, + }, + }); } }); describe('handleKeyringSnapMessage', () => { it('should return the list of accounts', async () => { - const result = await keyring.handleKeyringSnapMessage(snapId, ['read']); + const result = await keyring.handleKeyringSnapMessage(snapId, { + method: 'listAccounts', + }); expect(result).toStrictEqual(accounts); }); it('should fail if the method is not supported', async () => { await expect( - keyring.handleKeyringSnapMessage(snapId, ['invalid']), + keyring.handleKeyringSnapMessage(snapId, { + method: 'invalid', + }), ).rejects.toThrow('Method not supported: invalid'); }); }); diff --git a/src/snap-keyring.ts b/src/snap-keyring.ts index 7f8f436c..c58a8312 100644 --- a/src/snap-keyring.ts +++ b/src/snap-keyring.ts @@ -83,16 +83,16 @@ export class SnapKeyring extends EventEmitter { message: SnapMessage, ): Promise { assert(message, SnapMessageStruct); - const [method, params] = message; + const { method, params } = message; switch (method) { - case 'update': - case 'delete': - case 'create': { + case 'updateAccount': + case 'deleteAccount': + case 'createAccount': { await this.#syncAccounts(snapId); return null; } - case 'read': { + case 'listAccounts': { // Don't call the snap back to list the accounts. The main use case for // this method is to allow the snap to verify if the keyring's state is // in sync with the snap's state. @@ -101,7 +101,7 @@ export class SnapKeyring extends EventEmitter { ); } - case 'submit': { + case 'submitResponse': { const { id, result } = params as any; // FIXME: add a struct for this this.#resolveRequest(id, result); return true; diff --git a/src/types.ts b/src/types.ts index 4a543467..f16c455a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,10 +1,18 @@ import { JsonStruct } from '@metamask/utils'; -import { Infer, string, tuple, union } from 'superstruct'; +import { + Infer, + array, + object, + optional, + record, + string, + union, +} from 'superstruct'; -export const SnapMessageStruct = union([ - tuple([string()]), - tuple([string(), JsonStruct]), -]); +export const SnapMessageStruct = object({ + method: string(), + params: optional(union([array(JsonStruct), record(string(), JsonStruct)])), +}); /** * Message sent by the snap to manage accounts and requests.