-
Notifications
You must be signed in to change notification settings - Fork 542
/
validation.ts
209 lines (181 loc) · 5.22 KB
/
validation.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { ChainIdStruct, HandlerType } from '@metamask/snap-utils';
import { SnapKeyring } from '@metamask/snap-types';
import {
array,
assign,
enums,
Infer,
is,
literal,
object,
omit,
optional,
record,
string,
tuple,
union,
unknown,
} from 'superstruct';
import {
assertStruct,
Json,
JsonRpcIdStruct,
JsonRpcRequestStruct,
JsonRpcSuccess,
JsonRpcSuccessStruct,
JsonStruct,
} from '@metamask/utils';
const VALIDATION_FUNCTIONS = {
[HandlerType.OnRpcRequest]: validateFunctionExport,
[HandlerType.OnTransaction]: validateFunctionExport,
[HandlerType.SnapKeyring]: validateKeyringExport,
[HandlerType.OnCronjob]: validateFunctionExport,
};
/**
* Validates a function export.
*
* @param snapExport - The export itself.
* @returns True if the export matches the expected shape, false otherwise.
*/
function validateFunctionExport(
snapExport: unknown,
): snapExport is (...args: unknown[]) => unknown {
return typeof snapExport === 'function';
}
/**
* Validates a keyring export.
*
* @param snapExport - The export itself.
* @returns True if the export matches the expected shape, false otherwise.
*/
function validateKeyringExport(snapExport: unknown): snapExport is SnapKeyring {
// TODO Decide whether we want more validation
return typeof snapExport === 'object';
}
/**
* Validates a given snap export.
*
* @param type - The type of export expected.
* @param snapExport - The export itself.
* @returns True if the export matches the expected shape, false otherwise.
*/
export function validateExport(type: HandlerType, snapExport: unknown) {
const validationFunction = VALIDATION_FUNCTIONS[type];
return validationFunction?.(snapExport) ?? false;
}
export const JsonRpcRequestWithoutIdStruct = assign(
omit(JsonRpcRequestStruct, ['id']),
object({
id: optional(JsonRpcIdStruct),
}),
);
export type JsonRpcRequestWithoutId = Infer<
typeof JsonRpcRequestWithoutIdStruct
>;
export const EndowmentStruct = string();
export type Endowment = Infer<typeof EndowmentStruct>;
/**
* Check if the given value is an endowment.
*
* @param value - The value to check.
* @returns Whether the value is an endowment.
*/
export function isEndowment(value: unknown): value is Endowment {
return is(value, EndowmentStruct);
}
/**
* Check if the given value is an array of endowments.
*
* @param value - The value to check.
* @returns Whether the value is an array of endowments.
*/
export function isEndowmentsArray(value: unknown): value is Endowment[] {
return Array.isArray(value) && value.every(isEndowment);
}
const OkStruct = literal('OK');
export const PingRequestArgumentsStruct = optional(
union([literal(undefined), array()]),
);
export const TerminateRequestArgumentsStruct = union([
literal(undefined),
array(),
]);
export const ExecuteSnapRequestArgumentsStruct = tuple([
string(),
string(),
optional(array(EndowmentStruct)),
]);
export const SnapRpcRequestArgumentsStruct = tuple([
string(),
enums(Object.values(HandlerType)),
string(),
assign(
JsonRpcRequestWithoutIdStruct,
object({
params: optional(record(string(), unknown())),
}),
),
]);
export type PingRequestArguments = Infer<typeof PingRequestArgumentsStruct>;
export type TerminateRequestArguments = Infer<
typeof TerminateRequestArgumentsStruct
>;
export type ExecuteSnapRequestArguments = Infer<
typeof ExecuteSnapRequestArgumentsStruct
>;
export type SnapRpcRequestArguments = Infer<
typeof SnapRpcRequestArgumentsStruct
>;
export type RequestArguments =
| PingRequestArguments
| TerminateRequestArguments
| ExecuteSnapRequestArguments
| SnapRpcRequestArguments;
export const OnTransactionRequestArgumentsStruct = object({
// TODO: Improve `transaction` type.
transaction: record(string(), JsonStruct),
chainId: ChainIdStruct,
});
export type OnTransactionRequestArguments = Infer<
typeof OnTransactionRequestArgumentsStruct
>;
/**
* Asserts that the given value is a valid {@link OnTransactionRequestArguments}
* object.
*
* @param value - The value to validate.
* @throws If the value is not a valid {@link OnTransactionRequestArguments}
* object.
*/
export function assertIsOnTransactionRequestArguments(
value: unknown,
): asserts value is OnTransactionRequestArguments {
assertStruct(
value,
OnTransactionRequestArgumentsStruct,
'Invalid request params',
);
}
const OkResponseStruct = assign(
JsonRpcSuccessStruct,
object({
result: OkStruct,
}),
);
const SnapRpcResponse = JsonRpcSuccessStruct;
export type OkResponse = Infer<typeof OkResponseStruct>;
export type SnapRpcResponse = Infer<typeof SnapRpcResponse>;
export type Response = OkResponse | SnapRpcResponse;
type RequestParams<Params extends unknown[] | undefined> =
Params extends undefined ? [] : Params;
type RequestFunction<
Args extends RequestArguments,
ResponseType extends JsonRpcSuccess<Json>,
> = (...args: RequestParams<Args>) => Promise<ResponseType['result']>;
export type Ping = RequestFunction<PingRequestArguments, OkResponse>;
export type Terminate = RequestFunction<TerminateRequestArguments, OkResponse>;
export type ExecuteSnap = RequestFunction<
ExecuteSnapRequestArguments,
OkResponse
>;
export type SnapRpc = RequestFunction<SnapRpcRequestArguments, SnapRpcResponse>;