-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrpcHandler.ts
More file actions
114 lines (99 loc) · 3.3 KB
/
Copy pathrpcHandler.ts
File metadata and controls
114 lines (99 loc) · 3.3 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { logger } from '@metamask/7715-permissions-shared/utils';
import type { Json } from '@metamask/snaps-sdk';
import type { PermissionHandlerFactory } from '../core/permissionHandlerFactory';
import { DEFAULT_GATOR_PERMISSION_TO_OFFER } from '../permissions/permissionOffers';
import type { ProfileSyncManager } from '../profileSync';
import { validatePermissionRequestParam } from '../utils/validate';
/**
* Type for the RPC handler methods.
*/
export type RpcHandler = {
/**
* Handles grant permission requests.
*
* @param params - The parameters for the grant permission request.
* @returns The result of the grant permission request.
*/
grantPermission(params?: Json): Promise<Json>;
/**
* Handles get permission offers requests.
*
* @returns The permission offers.
*/
getPermissionOffers(): Promise<Json>;
/**
* Handles get granted permissions requests.
*
* @returns The granted permissions.
*/
getGrantedPermissions(): Promise<Json>;
};
/**
* Creates an RPC handler with methods for handling permission-related RPC requests.
*
* @param config - The parameters for creating the RPC handler.
* @param config.permissionHandlerFactory - The factory for creating permission handlers.
* @param config.profileSyncManager - The profile sync manager.
* @returns An object with RPC handler methods.
*/
export function createRpcHandler(config: {
permissionHandlerFactory: PermissionHandlerFactory;
profileSyncManager: ProfileSyncManager;
}): RpcHandler {
const { permissionHandlerFactory, profileSyncManager } = config;
/**
* Handles grant permission requests.
*
* @param params - The parameters for the grant permission request.
* @returns The result of the grant permission request.
*/
const grantPermission = async (params?: Json): Promise<Json> => {
logger.debug('grantPermissions()', params);
const { permissionsRequest, siteOrigin } =
validatePermissionRequestParam(params);
const responses = await Promise.all(
permissionsRequest.map(async (request) => {
const handler =
permissionHandlerFactory.createPermissionHandler(request);
const permissionResponse =
await handler.handlePermissionRequest(siteOrigin);
if (!permissionResponse.approved) {
throw new Error(permissionResponse.reason);
}
if (permissionResponse.response) {
await profileSyncManager.storeGrantedPermission({
permissionResponse: permissionResponse.response,
siteOrigin,
});
}
return permissionResponse.response;
}),
);
return responses as Json[];
};
/**
* Handles get permission offers requests.
*
* @returns The permission offers.
*/
const getPermissionOffers = async (): Promise<Json> => {
logger.debug('getPermissionOffers()');
return DEFAULT_GATOR_PERMISSION_TO_OFFER;
};
/**
* Handles get granted permissions requests.
*
* @returns The granted permissions.
*/
const getGrantedPermissions = async (): Promise<Json> => {
logger.debug('getGrantedPermissions()');
const grantedPermission =
await profileSyncManager.getAllGrantedPermissions();
return grantedPermission as Json[];
};
return {
grantPermission,
getPermissionOffers,
getGrantedPermissions,
};
}