-
Notifications
You must be signed in to change notification settings - Fork 542
/
ExecutionService.ts
99 lines (83 loc) · 2.26 KB
/
ExecutionService.ts
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
import { RestrictedControllerMessenger } from '@metamask/base-controller';
import { SnapId, SnapRpcHookArgs } from '@metamask/snaps-utils';
import { Json } from '@metamask/types';
type TerminateSnap = (snapId: string) => Promise<void>;
type TerminateAll = () => Promise<void>;
type ExecuteSnap = (snapData: SnapExecutionData) => Promise<unknown>;
type HandleRpcRequest = (
snapId: string,
options: SnapRpcHookArgs,
) => Promise<unknown>;
export interface ExecutionService {
terminateSnap: TerminateSnap;
terminateAllSnaps: TerminateAll;
executeSnap: ExecuteSnap;
handleRpcRequest: HandleRpcRequest;
}
export type SnapExecutionData = {
snapId: string;
sourceCode: string;
endowments?: Json;
};
export type SnapErrorJson = {
message: string;
code: number;
data?: Json;
};
const controllerName = 'ExecutionService';
export type ErrorMessageEvent = {
type: 'ExecutionService:unhandledError';
payload: [SnapId, SnapErrorJson];
};
export type OutboundRequest = {
type: 'ExecutionService:outboundRequest';
payload: [SnapId];
};
export type OutboundResponse = {
type: 'ExecutionService:outboundResponse';
payload: [SnapId];
};
export type ExecutionServiceEvents =
| ErrorMessageEvent
| OutboundRequest
| OutboundResponse;
/**
* Handles RPC request.
*/
export type HandleRpcRequestAction = {
type: `${typeof controllerName}:handleRpcRequest`;
handler: ExecutionService['handleRpcRequest'];
};
/**
* Executes a given snap.
*/
export type ExecuteSnapAction = {
type: `${typeof controllerName}:executeSnap`;
handler: ExecutionService['executeSnap'];
};
/**
* Terminates a given snap.
*/
export type TerminateSnapAction = {
type: `${typeof controllerName}:terminateSnap`;
handler: ExecutionService['terminateSnap'];
};
/**
* Terminates all snaps.
*/
export type TerminateAllSnapsAction = {
type: `${typeof controllerName}:terminateAllSnaps`;
handler: ExecutionService['terminateAllSnaps'];
};
export type ExecutionServiceActions =
| HandleRpcRequestAction
| ExecuteSnapAction
| TerminateSnapAction
| TerminateAllSnapsAction;
export type ExecutionServiceMessenger = RestrictedControllerMessenger<
'ExecutionService',
ExecutionServiceActions,
ExecutionServiceEvents,
ExecutionServiceActions['type'],
ExecutionServiceEvents['type']
>;