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

Commit

Permalink
fix: allow the request ID to be a string, number or null (#156)
Browse files Browse the repository at this point in the history
* fix: allow the request ID to be a string, number or null

* fix: import `UuidStruct`
  • Loading branch information
danroc authored Oct 18, 2023
1 parent 1eacc2b commit be49659
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/internal/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { JsonStruct } from '@metamask/utils';
import type { Infer } from 'superstruct';
import { array, literal, object, record, string } from 'superstruct';
import {
array,
literal,
number,
object,
record,
string,
union,
} from 'superstruct';

import {
KeyringAccountDataStruct,
Expand All @@ -12,7 +20,7 @@ import { UuidStruct } from '../utils';

const CommonHeader = {
jsonrpc: literal('2.0'),
id: UuidStruct,
id: union([string(), number(), literal(null)]),
};

// ----------------------------------------------------------------------------
Expand Down
40 changes: 37 additions & 3 deletions src/rpc-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,49 @@ describe('keyringRpcDispatcher', () => {
);
});

it('should fail to list requests with a non-UUIDv4 request ID', async () => {
it('calls the keyring with a non-UUIDv4 string request ID', async () => {
const request: JsonRpcRequest = {
jsonrpc: '2.0',
id: 'invalid-id-string',
id: 'request-id',
method: 'keyring_listRequests',
};

keyring.listRequests.mockResolvedValue([]);
expect(await handleKeyringRequest(keyring, request)).toStrictEqual([]);
});

it('calls the keyring with a number request ID', async () => {
const request: JsonRpcRequest = {
jsonrpc: '2.0',
id: 1,
method: 'keyring_listRequests',
};

keyring.listRequests.mockResolvedValue([]);
expect(await handleKeyringRequest(keyring, request)).toStrictEqual([]);
});

it('calls the keyring with a null request ID', async () => {
const request: JsonRpcRequest = {
jsonrpc: '2.0',
id: null,
method: 'keyring_listRequests',
};

keyring.listRequests.mockResolvedValue([]);
expect(await handleKeyringRequest(keyring, request)).toStrictEqual([]);
});

it('fails to call the keyring with a boolean request ID', async () => {
const request: JsonRpcRequest = {
jsonrpc: '2.0',
id: true as any,
method: 'keyring_listRequests',
};

keyring.listRequests.mockResolvedValue([]);
await expect(handleKeyringRequest(keyring, request)).rejects.toThrow(
'At path: id -- Expected a string matching',
'At path: id -- Expected the value to satisfy a union of `string | number | literal`, but received: true',
);
});

Expand Down

0 comments on commit be49659

Please sign in to comment.