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

feat: use objects in snap -> controller methods #28

Merged
merged 1 commit into from
Jun 22, 2023
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
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