Skip to content

Commit be8d9d9

Browse files
authored
Unlock client before calling getEntropySources in snap_listEntropySources method (#3194)
`snap_listEntropySources` was missing a call to `getUnlockPromise`, so if the client is locked, it would just return an empty array.
1 parent 4832fd7 commit be8d9d9

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

packages/snaps-rpc-methods/src/permitted/listEntropySources.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('snap_listEntropySources', () => {
1616
hookNames: {
1717
hasPermission: true,
1818
getEntropySources: true,
19+
getUnlockPromise: true,
1920
},
2021
});
2122
});
@@ -25,6 +26,7 @@ describe('snap_listEntropySources', () => {
2526
it('returns the result from the `getEntropySources` hook', async () => {
2627
const { implementation } = listEntropySourcesHandler;
2728

29+
const getUnlockPromise = jest.fn();
2830
const getEntropySources = jest.fn().mockReturnValue([
2931
{
3032
name: 'Secret recovery phrase 1',
@@ -49,6 +51,7 @@ describe('snap_listEntropySources', () => {
4951
const hooks = {
5052
hasPermission: jest.fn().mockReturnValue(true),
5153
getEntropySources,
54+
getUnlockPromise,
5255
};
5356

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

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

107+
const getUnlockPromise = jest.fn();
103108
const hooks = {
104109
hasPermission: jest.fn().mockReturnValue(false),
105110
getEntropySources: jest.fn(),
111+
getUnlockPromise,
106112
};
107113

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

134+
expect(getUnlockPromise).not.toHaveBeenCalled();
128135
expect(response).toStrictEqual({
129136
jsonrpc: '2.0',
130137
id: 1,

packages/snaps-rpc-methods/src/permitted/listEntropySources.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const REQUIRED_PERMISSIONS = [
2929
const hookNames: MethodHooksObject<ListEntropySourcesHooks> = {
3030
hasPermission: true,
3131
getEntropySources: true,
32+
getUnlockPromise: true,
3233
};
3334

3435
export type ListEntropySourcesHooks = {
@@ -46,6 +47,13 @@ export type ListEntropySourcesHooks = {
4647
* @returns The entropy sources.
4748
*/
4849
getEntropySources: () => EntropySource[];
50+
51+
/**
52+
* Wait for the extension to be unlocked.
53+
*
54+
* @returns A promise that resolves once the extension is unlocked.
55+
*/
56+
getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
4957
};
5058

5159
export const listEntropySourcesHandler: PermittedHandlerExport<
@@ -59,7 +67,7 @@ export const listEntropySourcesHandler: PermittedHandlerExport<
5967
};
6068

6169
/**
62-
* The `snap_getInterfaceContext` method implementation.
70+
* The `snap_listEntropySources` method implementation.
6371
*
6472
* @param _request - The JSON-RPC request object. Not used by this function.
6573
* @param response - The JSON-RPC response object.
@@ -70,20 +78,27 @@ export const listEntropySourcesHandler: PermittedHandlerExport<
7078
* @param hooks.hasPermission - The function to check if the origin has a
7179
* permission.
7280
* @param hooks.getEntropySources - The function to get the entropy sources.
81+
* @param hooks.getUnlockPromise - The function to get the unlock promise.
7382
* @returns Noting.
7483
*/
75-
function listEntropySourcesImplementation(
84+
async function listEntropySourcesImplementation(
7685
_request: JsonRpcRequest<ListEntropySourcesParams>,
7786
response: PendingJsonRpcResponse<ListEntropySourcesResult>,
7887
_next: unknown,
7988
end: JsonRpcEngineEndCallback,
80-
{ hasPermission, getEntropySources }: ListEntropySourcesHooks,
81-
): void {
89+
{
90+
hasPermission,
91+
getEntropySources,
92+
getUnlockPromise,
93+
}: ListEntropySourcesHooks,
94+
): Promise<void> {
8295
const isPermitted = REQUIRED_PERMISSIONS.some(hasPermission);
8396
if (!isPermitted) {
8497
return end(providerErrors.unauthorized());
8598
}
8699

100+
await getUnlockPromise(true);
101+
87102
response.result = getEntropySources();
88103
return end();
89104
}

0 commit comments

Comments
 (0)