This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |