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

Commit

Permalink
feat: add a BaseRpcKeyring class
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc committed Jun 26, 2023
1 parent e81fc22 commit d2634d1
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/base-rpc-keyring.test.ts
Original file line number Diff line number Diff line change
@@ -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',
);
});
});
});
51 changes: 51 additions & 0 deletions src/base-rpc-keyring.ts
Original file line number Diff line number Diff line change
@@ -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<void | Json> {
return keyringRpcDispatcher(this, request);
}

// --------------------------------------------------------------------------
// Methods from the `Keyring` interface.

abstract listAccounts(): Promise<KeyringAccount[]>;

abstract getAccount(id: string): Promise<KeyringAccount | undefined>;

abstract createAccount(
name: string,
options?: Record<string, Json> | null,
): Promise<KeyringAccount>;

abstract filterAccountChains(id: string, chains: string[]): Promise<string[]>;

abstract updateAccount(account: KeyringAccount): Promise<void>;

abstract deleteAccount(id: string): Promise<void>;

abstract listRequests(): Promise<KeyringRequest[]>;

abstract getRequest(id: string): Promise<KeyringRequest | undefined>;

abstract submitRequest(
request: KeyringRequest,
): Promise<SubmitRequestResponse>;

abstract approveRequest(id: string, result?: Json): Promise<void>;

abstract rejectRequest(id: string): Promise<void>;
}

0 comments on commit d2634d1

Please sign in to comment.