|
1 | 1 | import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine'; |
2 | | -import { PermissionConstraint, RequestedPermissions } from '@metamask/permission-controller'; |
| 2 | +import type { |
| 3 | + PermissionConstraint, |
| 4 | + RequestedPermissions, |
| 5 | +} from '@metamask/permission-controller'; |
| 6 | +import { isEqual } from '@metamask/snaps-utils'; |
3 | 7 | import { hasProperty, type Json, type JsonRpcParams } from '@metamask/utils'; |
| 8 | + |
4 | 9 | import { SnapEndowments } from '../endowments'; |
5 | | -import { isEqual } from '@metamask/snaps-utils'; |
6 | 10 |
|
7 | | -export interface PreinstalledSnapsMiddlewareHooks { |
8 | | - getPermittedEvmAccounts: () => string[]; |
9 | | - getAllEvmAccounts: () => string[]; |
10 | | - getPermissions: () => Record<string, PermissionConstraint> | undefined; |
11 | | - grantPermissions: (permissions: RequestedPermissions) => void; |
12 | | -} |
| 11 | +export type PreinstalledSnapsMiddlewareHooks = { |
| 12 | + getPermittedEvmAccounts: () => string[]; |
| 13 | + getAllEvmAccounts: () => string[]; |
| 14 | + getPermissions: () => Record<string, PermissionConstraint> | undefined; |
| 15 | + grantPermissions: (permissions: RequestedPermissions) => void; |
| 16 | +}; |
13 | 17 |
|
14 | 18 | /** |
15 | 19 | * Creates a middleware that automatically grants permissions to preinstalled Snaps |
16 | 20 | * that want to use the Ethereum provider endowment. |
17 | 21 | * |
18 | 22 | * @param hooks - The hooks used by the middleware. |
| 23 | + * @param hooks.getPermittedEvmAccounts |
| 24 | + * @param hooks.getAllEvmAccounts |
| 25 | + * @param hooks.getPermissions |
| 26 | + * @param hooks.grantPermissions |
19 | 27 | * @returns The middleware. |
20 | 28 | */ |
21 | | -export function createPreinstalledSnapsMiddleware( |
22 | | - { getPermittedEvmAccounts, getAllEvmAccounts, getPermissions, grantPermissions }: PreinstalledSnapsMiddlewareHooks, |
23 | | -): JsonRpcMiddleware<JsonRpcParams, Json> { |
| 29 | +export function createPreinstalledSnapsMiddleware({ |
| 30 | + getPermittedEvmAccounts, |
| 31 | + getAllEvmAccounts, |
| 32 | + getPermissions, |
| 33 | + grantPermissions, |
| 34 | +}: PreinstalledSnapsMiddlewareHooks): JsonRpcMiddleware<JsonRpcParams, Json> { |
24 | 35 | return function methodMiddleware(request, _response, next, _end) { |
25 | | - if (!request.method.startsWith("wallet") && !request.method.startsWith("eth")) { |
26 | | - return next(); |
| 36 | + if ( |
| 37 | + !request.method.startsWith('wallet') && |
| 38 | + !request.method.startsWith('eth') |
| 39 | + ) { |
| 40 | + return next(); |
27 | 41 | } |
28 | 42 |
|
29 | 43 | const permissions = getPermissions(); |
30 | 44 |
|
31 | | - if (!permissions || !hasProperty(permissions, SnapEndowments.EthereumProvider)) { |
32 | | - return next(); |
| 45 | + if ( |
| 46 | + !permissions || |
| 47 | + !hasProperty(permissions, SnapEndowments.EthereumProvider) |
| 48 | + ) { |
| 49 | + return next(); |
33 | 50 | } |
34 | 51 |
|
35 | 52 | const evmAccounts = getAllEvmAccounts(); |
36 | 53 | const existingEvmAccounts = getPermittedEvmAccounts(); |
37 | 54 |
|
38 | 55 | if (isEqual(evmAccounts, existingEvmAccounts)) { |
39 | | - return next(); |
| 56 | + return next(); |
40 | 57 | } |
41 | 58 |
|
42 | 59 | grantPermissions({ |
43 | | - 'endowment:caip25': { |
44 | | - caveats: [ |
45 | | - { |
46 | | - type: 'authorizedScopes', |
47 | | - value: { |
48 | | - requiredScopes: {}, |
49 | | - optionalScopes: { |
50 | | - 'wallet:eip155': { |
51 | | - accounts: evmAccounts.map(account => `wallet:eip155:${account}`), |
52 | | - }, |
53 | | - }, |
54 | | - sessionProperties: {}, |
55 | | - isMultichainOrigin: false, |
| 60 | + 'endowment:caip25': { |
| 61 | + caveats: [ |
| 62 | + { |
| 63 | + type: 'authorizedScopes', |
| 64 | + value: { |
| 65 | + requiredScopes: {}, |
| 66 | + optionalScopes: { |
| 67 | + 'wallet:eip155': { |
| 68 | + accounts: evmAccounts.map( |
| 69 | + (account) => `wallet:eip155:${account}`, |
| 70 | + ), |
56 | 71 | }, |
57 | 72 | }, |
58 | | - ], |
| 73 | + sessionProperties: {}, |
| 74 | + isMultichainOrigin: false, |
| 75 | + }, |
59 | 76 | }, |
| 77 | + ], |
| 78 | + }, |
60 | 79 | }); |
61 | 80 |
|
62 | 81 | return next(); |
|
0 commit comments