-
Notifications
You must be signed in to change notification settings - Fork 658
Expand file tree
/
Copy pathgetSnaps.ts
More file actions
75 lines (69 loc) · 2.09 KB
/
Copy pathgetSnaps.ts
File metadata and controls
75 lines (69 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';
import type { PermittedHandlerExport } from '@metamask/permission-controller';
import type { GetSnapsResult } from '@metamask/snaps-sdk';
import type { JsonRpcParams, PendingJsonRpcResponse } from '@metamask/utils';
import type { MethodHooksObject } from '../utils';
const methodName = 'wallet_getSnaps';
const hookNames: MethodHooksObject<GetSnapsHooks> = {
getSnaps: true,
};
/**
* Get permitted and installed Snaps for the requesting origin.
*
* @example
* ```ts
* const snaps = await snap.request({
* method: 'wallet_getSnaps',
* });
* console.log(snaps);
* // {
* // 'npm:example-snap': {
* // id: 'npm:example-snap',
* // version: '1.0.0',
* // initialPermissions: { ... },
* // blocked: false,
* // enabled: true,
* // },
* // ...,
* // }
* ```
*/
export const getSnapsHandler = {
methodNames: [methodName] as const,
implementation: getSnapsImplementation,
hookNames,
} satisfies PermittedHandlerExport<
GetSnapsHooks,
JsonRpcParams,
GetSnapsResult
>;
export type GetSnapsHooks = {
/**
* @returns The permitted and installed snaps for the requesting origin.
*/
getSnaps: () => Promise<GetSnapsResult>;
};
/**
* The `wallet_getSnaps` method implementation.
* Fetches available snaps for the requesting origin and adds them to the JSON-RPC response.
*
* @param _req - The JSON-RPC request object. Not used by this function.
* @param res - The JSON-RPC response object.
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
* function.
* @param end - The `json-rpc-engine` "end" callback.
* @param hooks - The RPC method hooks.
* @param hooks.getSnaps - A function that returns the snaps available for the requesting origin.
* @returns Nothing.
*/
async function getSnapsImplementation(
_req: unknown,
res: PendingJsonRpcResponse<GetSnapsResult>,
_next: unknown,
end: JsonRpcEngineEndCallback,
{ getSnaps }: GetSnapsHooks,
): Promise<void> {
// getSnaps is already bound to the origin
res.result = await getSnaps();
return end();
}