diff --git a/src/base-rpc-keyring.test.ts b/src/base-rpc-keyring.test.ts new file mode 100644 index 000000000..28e2d7198 --- /dev/null +++ b/src/base-rpc-keyring.test.ts @@ -0,0 +1,58 @@ +import { JsonRpcRequest } from '@metamask/utils'; + +import { BaseRpcKeyring } from './base-rpc-keyring'; +import { GetAccountRequest } from './keyring-internal-api'; + +describe('BaseRpcKeyring', () => { + class MockRpcKeyring extends BaseRpcKeyring { + listAccounts = jest.fn(); + + getAccount = jest.fn(); + + createAccount = jest.fn(); + + filterAccountChains = jest.fn(); + + updateAccount = jest.fn(); + + deleteAccount = jest.fn(); + + listRequests = jest.fn(); + + getRequest = jest.fn(); + + submitRequest = jest.fn(); + + approveRequest = jest.fn(); + + rejectRequest = jest.fn(); + } + + const mockKeyring = new MockRpcKeyring(); + + describe('dispatch', () => { + it('should dispatch the request', async () => { + const request: JsonRpcRequest = { + jsonrpc: '2.0', + id: 'edbaab5d-c5b0-454e-8ef1-36334230f746', + method: 'keyring_getAccount', + params: { id: '29b781ba-ef79-49de-ab64-d0154231860e' }, + }; + await mockKeyring.dispatch(request); + expect(mockKeyring.getAccount).toHaveBeenCalledWith( + (request as GetAccountRequest).params.id, + ); + }); + + it('should throw a MethodNotFound error if the method does not exist', async () => { + const request: JsonRpcRequest = { + jsonrpc: '2.0', + id: '1800984b-4a02-498a-b050-34db3543b85b', + method: 'invalid_method', + }; + await expect(mockKeyring.dispatch(request)).rejects.toThrow( + 'Method not supported: invalid_method', + ); + }); + }); +}); diff --git a/src/base-rpc-keyring.ts b/src/base-rpc-keyring.ts new file mode 100644 index 000000000..0ef0381cb --- /dev/null +++ b/src/base-rpc-keyring.ts @@ -0,0 +1,51 @@ +import { Json, JsonRpcRequest } from '@metamask/utils'; + +import { + Keyring, + KeyringAccount, + KeyringRequest, + SubmitRequestResponse, +} from './keyring-api'; +import { keyringRpcDispatcher } from './keyring-rpc-dispatcher'; + +export abstract class BaseRpcKeyring implements Keyring { + /** + * Dispatch a request JSON-RPC request to the right method. + * + * @param request - The JSON-RPC request to dispatch. + * @returns A promise that resolves to the response of the request. + */ + async dispatch(request: JsonRpcRequest): Promise { + return keyringRpcDispatcher(this, request); + } + + // -------------------------------------------------------------------------- + // Methods from the `Keyring` interface. + + abstract listAccounts(): Promise; + + abstract getAccount(id: string): Promise; + + abstract createAccount( + name: string, + options?: Record | null, + ): Promise; + + abstract filterAccountChains(id: string, chains: string[]): Promise; + + abstract updateAccount(account: KeyringAccount): Promise; + + abstract deleteAccount(id: string): Promise; + + abstract listRequests(): Promise; + + abstract getRequest(id: string): Promise; + + abstract submitRequest( + request: KeyringRequest, + ): Promise; + + abstract approveRequest(id: string, result?: Json): Promise; + + abstract rejectRequest(id: string): Promise; +}