Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('snap_listEntropySources', () => {
hookNames: {
hasPermission: true,
getEntropySources: true,
getUnlockPromise: true,
},
});
});
Expand All @@ -25,6 +26,7 @@ describe('snap_listEntropySources', () => {
it('returns the result from the `getEntropySources` hook', async () => {
const { implementation } = listEntropySourcesHandler;

const getUnlockPromise = jest.fn();
const getEntropySources = jest.fn().mockReturnValue([
{
name: 'Secret recovery phrase 1',
Expand All @@ -49,6 +51,7 @@ describe('snap_listEntropySources', () => {
const hooks = {
hasPermission: jest.fn().mockReturnValue(true),
getEntropySources,
getUnlockPromise,
};

const engine = new JsonRpcEngine();
Expand All @@ -71,6 +74,7 @@ describe('snap_listEntropySources', () => {
method: 'snap_listEntropySources',
});

expect(getUnlockPromise).toHaveBeenCalledWith(true);
expect(response).toStrictEqual({
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -100,9 +104,11 @@ describe('snap_listEntropySources', () => {
it('returns an unauthorized error if the requesting origin does not have the required permission', async () => {
const { implementation } = listEntropySourcesHandler;

const getUnlockPromise = jest.fn();
const hooks = {
hasPermission: jest.fn().mockReturnValue(false),
getEntropySources: jest.fn(),
getUnlockPromise,
};

const engine = new JsonRpcEngine();
Expand All @@ -125,6 +131,7 @@ describe('snap_listEntropySources', () => {
method: 'snap_listEntropySources',
});

expect(getUnlockPromise).not.toHaveBeenCalled();
expect(response).toStrictEqual({
jsonrpc: '2.0',
id: 1,
Expand Down
23 changes: 19 additions & 4 deletions packages/snaps-rpc-methods/src/permitted/listEntropySources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const REQUIRED_PERMISSIONS = [
const hookNames: MethodHooksObject<ListEntropySourcesHooks> = {
hasPermission: true,
getEntropySources: true,
getUnlockPromise: true,
};

export type ListEntropySourcesHooks = {
Expand All @@ -46,6 +47,13 @@ export type ListEntropySourcesHooks = {
* @returns The entropy sources.
*/
getEntropySources: () => EntropySource[];

/**
* Wait for the extension to be unlocked.
*
* @returns A promise that resolves once the extension is unlocked.
*/
getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
};

export const listEntropySourcesHandler: PermittedHandlerExport<
Expand All @@ -59,7 +67,7 @@ export const listEntropySourcesHandler: PermittedHandlerExport<
};

/**
* The `snap_getInterfaceContext` method implementation.
* The `snap_listEntropySources` method implementation.
*
* @param _request - The JSON-RPC request object. Not used by this function.
* @param response - The JSON-RPC response object.
Expand All @@ -70,20 +78,27 @@ export const listEntropySourcesHandler: PermittedHandlerExport<
* @param hooks.hasPermission - The function to check if the origin has a
* permission.
* @param hooks.getEntropySources - The function to get the entropy sources.
* @param hooks.getUnlockPromise - The function to get the unlock promise.
* @returns Noting.
*/
function listEntropySourcesImplementation(
async function listEntropySourcesImplementation(
_request: JsonRpcRequest<ListEntropySourcesParams>,
response: PendingJsonRpcResponse<ListEntropySourcesResult>,
_next: unknown,
end: JsonRpcEngineEndCallback,
{ hasPermission, getEntropySources }: ListEntropySourcesHooks,
): void {
{
hasPermission,
getEntropySources,
getUnlockPromise,
}: ListEntropySourcesHooks,
): Promise<void> {
const isPermitted = REQUIRED_PERMISSIONS.some(hasPermission);
if (!isPermitted) {
return end(providerErrors.unauthorized());
}

await getUnlockPromise(true);

response.result = getEntropySources();
return end();
}
Loading