Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
feat: use objects in snap -> controller methods (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc authored Jun 22, 2023
1 parent bd7aaf6 commit 4607954
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
18 changes: 12 additions & 6 deletions src/snap-keyring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
Expand Down
12 changes: 6 additions & 6 deletions src/snap-keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ export class SnapKeyring extends EventEmitter {
message: SnapMessage,
): Promise<Json> {
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.
Expand All @@ -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;
Expand Down
18 changes: 13 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 4607954

Please sign in to comment.