@metamask/snaps-execution-environments 9.0.0 → 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -1
- package/dist/common/commands.cjs +13 -2
- package/dist/common/commands.cjs.map +1 -1
- package/dist/common/commands.d.cts.map +1 -1
- package/dist/common/commands.d.mts.map +1 -1
- package/dist/common/commands.mjs +14 -3
- package/dist/common/commands.mjs.map +1 -1
- package/dist/common/validation.cjs +67 -2
- package/dist/common/validation.cjs.map +1 -1
- package/dist/common/validation.d.cts +131 -8
- package/dist/common/validation.d.cts.map +1 -1
- package/dist/common/validation.d.mts +131 -8
- package/dist/common/validation.d.mts.map +1 -1
- package/dist/common/validation.mjs +66 -3
- package/dist/common/validation.mjs.map +1 -1
- package/dist/webpack/iframe/bundle.js +1 -1
- package/dist/webpack/iframe/index.html +104 -37
- package/dist/webpack/node-process/bundle.js +3987 -3670
- package/dist/webpack/node-process/bundle.js.LICENSE.txt +3986 -3669
- package/dist/webpack/node-thread/bundle.js +3987 -3670
- package/dist/webpack/node-thread/bundle.js.LICENSE.txt +3986 -3669
- package/dist/webpack/webview/index.html +105 -38
- package/package.json +9 -12
|
@@ -120,6 +120,33 @@ export type OnAssetHistoricalPriceRequestArguments = Infer<typeof OnAssetHistori
|
|
|
120
120
|
* object.
|
|
121
121
|
*/
|
|
122
122
|
export declare function assertIsOnAssetHistoricalPriceRequestArguments(value: unknown): asserts value is OnAssetHistoricalPriceRequestArguments;
|
|
123
|
+
export declare const OnAssetsMarketDataRequestArgumentsStruct: Struct<{
|
|
124
|
+
assets: {
|
|
125
|
+
asset: `${string}:${string}/${string}:${string}`;
|
|
126
|
+
unit: `${string}:${string}/${string}:${string}`;
|
|
127
|
+
}[];
|
|
128
|
+
}, {
|
|
129
|
+
assets: Struct<{
|
|
130
|
+
asset: `${string}:${string}/${string}:${string}`;
|
|
131
|
+
unit: `${string}:${string}/${string}:${string}`;
|
|
132
|
+
}[], Struct<{
|
|
133
|
+
asset: `${string}:${string}/${string}:${string}`;
|
|
134
|
+
unit: `${string}:${string}/${string}:${string}`;
|
|
135
|
+
}, {
|
|
136
|
+
asset: Struct<`${string}:${string}/${string}:${string}`, null>;
|
|
137
|
+
unit: Struct<`${string}:${string}/${string}:${string}`, null>;
|
|
138
|
+
}>>;
|
|
139
|
+
}>;
|
|
140
|
+
export type OnAssetsMarketDataRequestArguments = Infer<typeof OnAssetsMarketDataRequestArgumentsStruct>;
|
|
141
|
+
/**
|
|
142
|
+
* Asserts that the given value is a valid {@link OnAssetsMarketDataRequestArguments}
|
|
143
|
+
* object.
|
|
144
|
+
*
|
|
145
|
+
* @param value - The value to validate.
|
|
146
|
+
* @throws If the value is not a valid {@link OnAssetsMarketDataRequestArguments}
|
|
147
|
+
* object.
|
|
148
|
+
*/
|
|
149
|
+
export declare function assertIsOnAssetsMarketDataRequestArguments(value: unknown): asserts value is OnAssetsMarketDataRequestArguments;
|
|
123
150
|
export declare const OnAssetsLookupRequestArgumentsStruct: Struct<{
|
|
124
151
|
assets: `${string}:${string}/${string}:${string}`[];
|
|
125
152
|
}, {
|
|
@@ -140,7 +167,6 @@ export declare const OnAssetsConversionRequestArgumentsStruct: Struct<{
|
|
|
140
167
|
from: `${string}:${string}/${string}:${string}`;
|
|
141
168
|
to: `${string}:${string}/${string}:${string}`;
|
|
142
169
|
}[];
|
|
143
|
-
includeMarketData?: boolean | undefined;
|
|
144
170
|
}, {
|
|
145
171
|
conversions: Struct<{
|
|
146
172
|
from: `${string}:${string}/${string}:${string}`;
|
|
@@ -152,7 +178,6 @@ export declare const OnAssetsConversionRequestArgumentsStruct: Struct<{
|
|
|
152
178
|
from: Struct<`${string}:${string}/${string}:${string}`, null>;
|
|
153
179
|
to: Struct<`${string}:${string}/${string}:${string}`, null>;
|
|
154
180
|
}>>;
|
|
155
|
-
includeMarketData: Struct<boolean | undefined, null>;
|
|
156
181
|
}>;
|
|
157
182
|
export type OnAssetsConversionRequestArguments = Infer<typeof OnAssetsConversionRequestArgumentsStruct>;
|
|
158
183
|
/**
|
|
@@ -183,8 +208,8 @@ export declare const OnUserInputArgumentsStruct: Struct<{
|
|
|
183
208
|
name: string;
|
|
184
209
|
asset: `${string}:${string}/${string}:${string}`;
|
|
185
210
|
} | null>;
|
|
186
|
-
name: string;
|
|
187
211
|
type: import("@metamask/snaps-sdk").UserInputEventType.FormSubmitEvent;
|
|
212
|
+
name: string;
|
|
188
213
|
} | {
|
|
189
214
|
value: string | boolean | {
|
|
190
215
|
addresses: `${string}:${string}:${string}`[];
|
|
@@ -194,11 +219,19 @@ export declare const OnUserInputArgumentsStruct: Struct<{
|
|
|
194
219
|
name: string;
|
|
195
220
|
asset: `${string}:${string}/${string}:${string}`;
|
|
196
221
|
};
|
|
197
|
-
|
|
222
|
+
/**
|
|
223
|
+
* Asserts that the given value is a valid {@link OnUserInputArguments}
|
|
224
|
+
* object.
|
|
225
|
+
*
|
|
226
|
+
* @param value - The value to validate.
|
|
227
|
+
* @throws If the value is not a valid {@link OnUserInputArguments}
|
|
228
|
+
* object.
|
|
229
|
+
*/
|
|
198
230
|
type: import("@metamask/snaps-sdk").UserInputEventType.InputChangeEvent;
|
|
199
|
-
} | {
|
|
200
231
|
name: string;
|
|
232
|
+
} | {
|
|
201
233
|
type: import("@metamask/snaps-sdk").UserInputEventType.FileUploadEvent;
|
|
234
|
+
name: string;
|
|
202
235
|
file: {
|
|
203
236
|
name: string;
|
|
204
237
|
size: number;
|
|
@@ -226,8 +259,8 @@ export declare const OnUserInputArgumentsStruct: Struct<{
|
|
|
226
259
|
name: string;
|
|
227
260
|
asset: `${string}:${string}/${string}:${string}`;
|
|
228
261
|
} | null>;
|
|
229
|
-
name: string;
|
|
230
262
|
type: import("@metamask/snaps-sdk").UserInputEventType.FormSubmitEvent;
|
|
263
|
+
name: string;
|
|
231
264
|
} | {
|
|
232
265
|
value: string | boolean | {
|
|
233
266
|
addresses: `${string}:${string}:${string}`[];
|
|
@@ -237,11 +270,19 @@ export declare const OnUserInputArgumentsStruct: Struct<{
|
|
|
237
270
|
name: string;
|
|
238
271
|
asset: `${string}:${string}/${string}:${string}`;
|
|
239
272
|
};
|
|
240
|
-
|
|
273
|
+
/**
|
|
274
|
+
* Asserts that the given value is a valid {@link OnUserInputArguments}
|
|
275
|
+
* object.
|
|
276
|
+
*
|
|
277
|
+
* @param value - The value to validate.
|
|
278
|
+
* @throws If the value is not a valid {@link OnUserInputArguments}
|
|
279
|
+
* object.
|
|
280
|
+
*/
|
|
241
281
|
type: import("@metamask/snaps-sdk").UserInputEventType.InputChangeEvent;
|
|
242
|
-
} | {
|
|
243
282
|
name: string;
|
|
283
|
+
} | {
|
|
244
284
|
type: import("@metamask/snaps-sdk").UserInputEventType.FileUploadEvent;
|
|
285
|
+
name: string;
|
|
245
286
|
file: {
|
|
246
287
|
name: string;
|
|
247
288
|
size: number;
|
|
@@ -275,6 +316,88 @@ export type OnProtocolRequestArguments = Infer<typeof OnProtocolRequestArguments
|
|
|
275
316
|
* object.
|
|
276
317
|
*/
|
|
277
318
|
export declare function assertIsOnProtocolRequestArguments(value: unknown): asserts value is OnProtocolRequestArguments;
|
|
319
|
+
export declare const WebSocketEventStruct: Struct<{
|
|
320
|
+
id: string;
|
|
321
|
+
type: "open";
|
|
322
|
+
origin: string;
|
|
323
|
+
} | {
|
|
324
|
+
id: string;
|
|
325
|
+
type: "close";
|
|
326
|
+
origin: string;
|
|
327
|
+
code: number;
|
|
328
|
+
reason: string | null;
|
|
329
|
+
wasClean: boolean | null;
|
|
330
|
+
} | {
|
|
331
|
+
data: {
|
|
332
|
+
type: "text";
|
|
333
|
+
message: string;
|
|
334
|
+
} | {
|
|
335
|
+
type: "binary";
|
|
336
|
+
message: number[];
|
|
337
|
+
};
|
|
338
|
+
id: string;
|
|
339
|
+
type: "message";
|
|
340
|
+
origin: string;
|
|
341
|
+
}, null>;
|
|
342
|
+
export declare const OnWebSocketEventArgumentsStruct: Struct<{
|
|
343
|
+
event: {
|
|
344
|
+
id: string;
|
|
345
|
+
type: "open";
|
|
346
|
+
origin: string;
|
|
347
|
+
} | {
|
|
348
|
+
id: string;
|
|
349
|
+
type: "close";
|
|
350
|
+
origin: string;
|
|
351
|
+
code: number;
|
|
352
|
+
reason: string | null;
|
|
353
|
+
wasClean: boolean | null;
|
|
354
|
+
} | {
|
|
355
|
+
data: {
|
|
356
|
+
type: "text";
|
|
357
|
+
message: string;
|
|
358
|
+
} | {
|
|
359
|
+
type: "binary";
|
|
360
|
+
message: number[];
|
|
361
|
+
};
|
|
362
|
+
id: string;
|
|
363
|
+
type: "message";
|
|
364
|
+
origin: string;
|
|
365
|
+
};
|
|
366
|
+
}, {
|
|
367
|
+
event: Struct<{
|
|
368
|
+
id: string;
|
|
369
|
+
type: "open";
|
|
370
|
+
origin: string;
|
|
371
|
+
} | {
|
|
372
|
+
id: string;
|
|
373
|
+
type: "close";
|
|
374
|
+
origin: string;
|
|
375
|
+
code: number;
|
|
376
|
+
reason: string | null;
|
|
377
|
+
wasClean: boolean | null;
|
|
378
|
+
} | {
|
|
379
|
+
data: {
|
|
380
|
+
type: "text";
|
|
381
|
+
message: string;
|
|
382
|
+
} | {
|
|
383
|
+
type: "binary";
|
|
384
|
+
message: number[];
|
|
385
|
+
};
|
|
386
|
+
id: string;
|
|
387
|
+
type: "message";
|
|
388
|
+
origin: string;
|
|
389
|
+
}, null>;
|
|
390
|
+
}>;
|
|
391
|
+
export type OnWebSocketEventArguments = Infer<typeof OnWebSocketEventArgumentsStruct>;
|
|
392
|
+
/**
|
|
393
|
+
* Asserts that the given value is a valid {@link OnWebSocketEventArguments}
|
|
394
|
+
* object.
|
|
395
|
+
*
|
|
396
|
+
* @param value - The value to validate.
|
|
397
|
+
* @throws If the value is not a valid {@link OnWebSocketEventArguments}
|
|
398
|
+
* object.
|
|
399
|
+
*/
|
|
400
|
+
export declare function assertIsOnWebSocketEventArguments(value: unknown): asserts value is OnWebSocketEventArguments;
|
|
278
401
|
declare const OkResponseStruct: Struct<{
|
|
279
402
|
jsonrpc: "2.0";
|
|
280
403
|
id: string | number | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.mts","sourceRoot":"","sources":["../../src/common/validation.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validation.d.mts","sourceRoot":"","sources":["../../src/common/validation.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,WAAW,EAAE,8BAA8B;AACpD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAmB3D,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,cAAc,EACf,wBAAwB;AAazB,eAAO,MAAM,6BAA6B;;;;;;;;;;EAKxC,CAAC;AAEH,MAAM,MAAM,uBAAuB,GAAG,KAAK,CACzC,OAAO,6BAA6B,CACrC,CAAC;AAEF,eAAO,MAAM,eAAe,sBAAW,CAAC;AACxC,MAAM,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AAEtD;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAE9D;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,EAAE,CAEtE;AAID,eAAO,MAAM,0BAA0B,qCAEtC,CAAC;AAEF,eAAO,MAAM,+BAA+B,qCAG1C,CAAC;AAEH,eAAO,MAAM,iCAAiC,0CAI5C,CAAC;AAEH,eAAO,MAAM,6BAA6B;;;;;SAaxC,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,KAAK,CAAC,OAAO,0BAA0B,CAAC,CAAC;AAC5E,MAAM,MAAM,yBAAyB,GAAG,KAAK,CAC3C,OAAO,+BAA+B,CACvC,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG,KAAK,CAC7C,OAAO,iCAAiC,CACzC,CAAC;AAEF,MAAM,MAAM,uBAAuB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OAAO,GAAG,eAAe,CAAC,uBAAuB,EAAE,eAAe,CAAC,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { rpcErrors } from "@metamask/rpc-errors";
|
|
2
|
-
import { InterfaceContextStruct, UserInputEventStruct } from "@metamask/snaps-sdk";
|
|
2
|
+
import { InterfaceContextStruct, literal as customLiteral, typedUnion, UserInputEventStruct } from "@metamask/snaps-sdk";
|
|
3
3
|
import { HandlerType } from "@metamask/snaps-utils";
|
|
4
|
-
import { any, array, assign, boolean, enums, is,
|
|
4
|
+
import { any, array, assign, boolean, enums, is, nullable, number, object, optional, record, size, string, tuple, union, literal } from "@metamask/superstruct";
|
|
5
5
|
import { assertStruct, CaipAssetTypeStruct, CaipChainIdStruct, JsonRpcIdStruct, JsonRpcParamsStruct, JsonRpcRequestStruct, JsonRpcSuccessStruct, JsonRpcVersionStruct, JsonStruct } from "@metamask/utils";
|
|
6
6
|
export const JsonRpcRequestWithoutIdStruct = object({
|
|
7
7
|
jsonrpc: optional(JsonRpcVersionStruct),
|
|
@@ -131,6 +131,23 @@ export const OnAssetHistoricalPriceRequestArgumentsStruct = object({
|
|
|
131
131
|
export function assertIsOnAssetHistoricalPriceRequestArguments(value) {
|
|
132
132
|
assertRequestArguments(value, OnAssetHistoricalPriceRequestArgumentsStruct);
|
|
133
133
|
}
|
|
134
|
+
export const OnAssetsMarketDataRequestArgumentsStruct = object({
|
|
135
|
+
assets: size(array(object({
|
|
136
|
+
asset: CaipAssetTypeStruct,
|
|
137
|
+
unit: CaipAssetTypeStruct,
|
|
138
|
+
})), 1, Infinity),
|
|
139
|
+
});
|
|
140
|
+
/**
|
|
141
|
+
* Asserts that the given value is a valid {@link OnAssetsMarketDataRequestArguments}
|
|
142
|
+
* object.
|
|
143
|
+
*
|
|
144
|
+
* @param value - The value to validate.
|
|
145
|
+
* @throws If the value is not a valid {@link OnAssetsMarketDataRequestArguments}
|
|
146
|
+
* object.
|
|
147
|
+
*/
|
|
148
|
+
export function assertIsOnAssetsMarketDataRequestArguments(value) {
|
|
149
|
+
assertRequestArguments(value, OnAssetsMarketDataRequestArgumentsStruct);
|
|
150
|
+
}
|
|
134
151
|
export const OnAssetsLookupRequestArgumentsStruct = object({
|
|
135
152
|
assets: size(array(CaipAssetTypeStruct), 1, Infinity),
|
|
136
153
|
});
|
|
@@ -150,7 +167,6 @@ export const OnAssetsConversionRequestArgumentsStruct = object({
|
|
|
150
167
|
from: CaipAssetTypeStruct,
|
|
151
168
|
to: CaipAssetTypeStruct,
|
|
152
169
|
})), 1, Infinity),
|
|
153
|
-
includeMarketData: optional(boolean()),
|
|
154
170
|
});
|
|
155
171
|
/**
|
|
156
172
|
* Asserts that the given value is a valid {@link OnAssetsConversionRequestArguments}
|
|
@@ -194,6 +210,53 @@ export const OnProtocolRequestArgumentsStruct = object({
|
|
|
194
210
|
export function assertIsOnProtocolRequestArguments(value) {
|
|
195
211
|
assertRequestArguments(value, OnProtocolRequestArgumentsStruct);
|
|
196
212
|
}
|
|
213
|
+
const WebSocketOpenEventStruct = object({
|
|
214
|
+
type: customLiteral('open'),
|
|
215
|
+
id: string(),
|
|
216
|
+
origin: string(),
|
|
217
|
+
});
|
|
218
|
+
const WebSocketCloseEventStruct = object({
|
|
219
|
+
type: customLiteral('close'),
|
|
220
|
+
id: string(),
|
|
221
|
+
origin: string(),
|
|
222
|
+
code: number(),
|
|
223
|
+
reason: nullable(string()),
|
|
224
|
+
wasClean: nullable(boolean()),
|
|
225
|
+
});
|
|
226
|
+
const WebSocketMessageEventStruct = object({
|
|
227
|
+
type: customLiteral('message'),
|
|
228
|
+
id: string(),
|
|
229
|
+
origin: string(),
|
|
230
|
+
data: typedUnion([
|
|
231
|
+
object({
|
|
232
|
+
type: customLiteral('text'),
|
|
233
|
+
message: string(),
|
|
234
|
+
}),
|
|
235
|
+
object({
|
|
236
|
+
type: customLiteral('binary'),
|
|
237
|
+
message: array(number()),
|
|
238
|
+
}),
|
|
239
|
+
]),
|
|
240
|
+
});
|
|
241
|
+
export const WebSocketEventStruct = typedUnion([
|
|
242
|
+
WebSocketOpenEventStruct,
|
|
243
|
+
WebSocketCloseEventStruct,
|
|
244
|
+
WebSocketMessageEventStruct,
|
|
245
|
+
]);
|
|
246
|
+
export const OnWebSocketEventArgumentsStruct = object({
|
|
247
|
+
event: WebSocketEventStruct,
|
|
248
|
+
});
|
|
249
|
+
/**
|
|
250
|
+
* Asserts that the given value is a valid {@link OnWebSocketEventArguments}
|
|
251
|
+
* object.
|
|
252
|
+
*
|
|
253
|
+
* @param value - The value to validate.
|
|
254
|
+
* @throws If the value is not a valid {@link OnWebSocketEventArguments}
|
|
255
|
+
* object.
|
|
256
|
+
*/
|
|
257
|
+
export function assertIsOnWebSocketEventArguments(value) {
|
|
258
|
+
assertRequestArguments(value, OnWebSocketEventArgumentsStruct);
|
|
259
|
+
}
|
|
197
260
|
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
198
261
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
199
262
|
const OkResponseStruct = object({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.mjs","sourceRoot":"","sources":["../../src/common/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACrB,4BAA4B;AAC7B,OAAO,EAAE,WAAW,EAAE,8BAA8B;AAEpD,OAAO,EACL,GAAG,EACH,KAAK,EACL,MAAM,EACN,OAAO,EACP,KAAK,EACL,EAAE,EACF,OAAO,EACP,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,MAAM,EACN,IAAI,EACJ,MAAM,EACN,KAAK,EACL,KAAK,EACN,8BAA8B;AAM/B,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,iBAAiB,EACjB,eAAe,EACf,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,UAAU,EACX,wBAAwB;AAEzB,MAA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CAAC,QAAQ,CAAC,sBAAsB,CAAC,CAAC;CACpD,CAAC,CAAC;AAIH;;;;;;;GAOG;AACH,MAAM,UAAU,mCAAmC,CACjD,KAAc;IAEd,sBAAsB,CAAC,KAAK,EAAE,0BAA0B,CAAC,CAAC;AAC5D,CAAC;AAED,MAAM,CAAC,MAAM,gCAAgC,GAAG,MAAM,CAAC;IACrD,KAAK,EAAE,iBAAiB;IACxB,OAAO,EAAE,oBAAoB;CAC9B,CAA6E,CAAC;AAM/E;;;;;;;GAOG;AACH,MAAM,UAAU,kCAAkC,CAChD,KAAc;IAEd,sBAAsB,CAAC,KAAK,EAAE,gCAAgC,CAAC,CAAC;AAClE,CAAC;AAED,gFAAgF;AAChF,6DAA6D;AAC7D,MAAM,gBAAgB,GAAG,MAAM,CAAC;IAC9B,EAAE,EAAE,eAAe;IACnB,OAAO,EAAE,oBAAoB;IAC7B,MAAM,EAAE,QAAQ;CACjB,CAAC,CAAC;AAEH,MAAM,eAAe,GAAG,oBAAoB,CAAC","sourcesContent":["import { rpcErrors } from '@metamask/rpc-errors';\nimport {\n InterfaceContextStruct,\n UserInputEventStruct,\n} from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type { Infer, Struct } from '@metamask/superstruct';\nimport {\n any,\n array,\n assign,\n boolean,\n enums,\n is,\n literal,\n nullable,\n object,\n optional,\n record,\n size,\n string,\n tuple,\n union,\n} from '@metamask/superstruct';\nimport type {\n CaipChainId,\n JsonRpcRequest,\n JsonRpcSuccess,\n} from '@metamask/utils';\nimport {\n assertStruct,\n CaipAssetTypeStruct,\n CaipChainIdStruct,\n JsonRpcIdStruct,\n JsonRpcParamsStruct,\n JsonRpcRequestStruct,\n JsonRpcSuccessStruct,\n JsonRpcVersionStruct,\n JsonStruct,\n} from '@metamask/utils';\n\nexport const JsonRpcRequestWithoutIdStruct = object({\n jsonrpc: optional(JsonRpcVersionStruct),\n id: optional(JsonRpcIdStruct),\n method: string(),\n params: optional(JsonRpcParamsStruct),\n});\n\nexport type JsonRpcRequestWithoutId = Infer<\n typeof JsonRpcRequestWithoutIdStruct\n>;\n\nexport const EndowmentStruct = string();\nexport type Endowment = Infer<typeof EndowmentStruct>;\n\n/**\n * Check if the given value is an endowment.\n *\n * @param value - The value to check.\n * @returns Whether the value is an endowment.\n */\nexport function isEndowment(value: unknown): value is Endowment {\n return is(value, EndowmentStruct);\n}\n\n/**\n * Check if the given value is an array of endowments.\n *\n * @param value - The value to check.\n * @returns Whether the value is an array of endowments.\n */\nexport function isEndowmentsArray(value: unknown): value is Endowment[] {\n return Array.isArray(value) && value.every(isEndowment);\n}\n\nconst OkStruct = literal('OK');\n\nexport const PingRequestArgumentsStruct = optional(\n union([literal(undefined), array()]),\n);\n\nexport const TerminateRequestArgumentsStruct = union([\n literal(undefined),\n array(),\n]);\n\nexport const ExecuteSnapRequestArgumentsStruct = tuple([\n string(),\n string(),\n array(EndowmentStruct),\n]);\n\nexport const SnapRpcRequestArgumentsStruct = tuple([\n string(),\n enums(Object.values(HandlerType)),\n string(),\n assign(\n JsonRpcRequestWithoutIdStruct,\n object({\n // Previously this would validate that the parameters were valid JSON.\n // This is already validated for all messages received by the executor.\n // If that assumption changes, this should once again validate JSON.\n params: optional(record(string(), any())),\n }),\n ),\n]);\n\nexport type PingRequestArguments = Infer<typeof PingRequestArgumentsStruct>;\nexport type TerminateRequestArguments = Infer<\n typeof TerminateRequestArgumentsStruct\n>;\n\nexport type ExecuteSnapRequestArguments = Infer<\n typeof ExecuteSnapRequestArgumentsStruct\n>;\n\nexport type SnapRpcRequestArguments = Infer<\n typeof SnapRpcRequestArgumentsStruct\n>;\n\nexport type RequestArguments =\n | PingRequestArguments\n | TerminateRequestArguments\n | ExecuteSnapRequestArguments\n | SnapRpcRequestArguments;\n\n/**\n * Asserts that the given value is a valid request arguments object.\n *\n * @param value - The value to validate.\n * @param struct - The struct to validate the value against.\n * @throws If the value is not a valid request arguments object.\n */\nfunction assertRequestArguments<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n): asserts value is Struct<Type, Schema> {\n assertStruct(\n value,\n struct,\n 'Invalid request params',\n rpcErrors.invalidParams,\n );\n}\n\nexport const OnTransactionRequestArgumentsStruct = object({\n // TODO: Improve `transaction` type.\n transaction: record(string(), JsonStruct),\n chainId: CaipChainIdStruct,\n transactionOrigin: nullable(string()),\n});\n\nexport type OnTransactionRequestArguments = Infer<\n typeof OnTransactionRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnTransactionRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnTransactionRequestArguments}\n * object.\n */\nexport function assertIsOnTransactionRequestArguments(\n value: unknown,\n): asserts value is OnTransactionRequestArguments {\n assertRequestArguments(value, OnTransactionRequestArgumentsStruct);\n}\n\nexport const OnSignatureRequestArgumentsStruct = object({\n signature: record(string(), JsonStruct),\n signatureOrigin: nullable(string()),\n});\n\nexport type OnSignatureRequestArguments = Infer<\n typeof OnSignatureRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnSignatureRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnSignatureRequestArguments}\n * object.\n */\nexport function assertIsOnSignatureRequestArguments(\n value: unknown,\n): asserts value is OnSignatureRequestArguments {\n assertRequestArguments(value, OnSignatureRequestArgumentsStruct);\n}\n\nconst baseNameLookupArgs = { chainId: CaipChainIdStruct };\nconst domainRequestStruct = object({\n ...baseNameLookupArgs,\n address: string(),\n});\nconst addressRequestStruct = object({\n ...baseNameLookupArgs,\n domain: string(),\n});\n\nexport const OnNameLookupRequestArgumentsStruct = union([\n domainRequestStruct,\n addressRequestStruct,\n]);\n\nexport type OnNameLookupRequestArguments = Infer<\n typeof OnNameLookupRequestArgumentsStruct\n>;\n\nexport type PossibleLookupRequestArgs = typeof baseNameLookupArgs & {\n address?: string;\n domain?: string;\n};\n\n/**\n * Asserts that the given value is a valid {@link OnNameLookupRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnNameLookupRequestArguments}\n * object.\n */\nexport function assertIsOnNameLookupRequestArguments(\n value: unknown,\n): asserts value is OnNameLookupRequestArguments {\n assertRequestArguments(value, OnNameLookupRequestArgumentsStruct);\n}\n\nexport const OnAssetHistoricalPriceRequestArgumentsStruct = object({\n from: CaipAssetTypeStruct,\n to: CaipAssetTypeStruct,\n});\n\nexport type OnAssetHistoricalPriceRequestArguments = Infer<\n typeof OnAssetHistoricalPriceRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnAssetHistoricalPriceRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnAssetHistoricalPriceRequestArguments}\n * object.\n */\nexport function assertIsOnAssetHistoricalPriceRequestArguments(\n value: unknown,\n): asserts value is OnAssetHistoricalPriceRequestArguments {\n assertRequestArguments(value, OnAssetHistoricalPriceRequestArgumentsStruct);\n}\n\nexport const OnAssetsLookupRequestArgumentsStruct = object({\n assets: size(array(CaipAssetTypeStruct), 1, Infinity),\n});\n\nexport type OnAssetsLookupRequestArguments = Infer<\n typeof OnAssetsLookupRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnAssetsLookupRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnAssetsLookupRequestArguments}\n * object.\n */\nexport function assertIsOnAssetsLookupRequestArguments(\n value: unknown,\n): asserts value is OnAssetsLookupRequestArguments {\n assertRequestArguments(value, OnAssetsLookupRequestArgumentsStruct);\n}\n\nexport const OnAssetsConversionRequestArgumentsStruct = object({\n conversions: size(\n array(\n object({\n from: CaipAssetTypeStruct,\n to: CaipAssetTypeStruct,\n }),\n ),\n 1,\n Infinity,\n ),\n includeMarketData: optional(boolean()),\n});\n\nexport type OnAssetsConversionRequestArguments = Infer<\n typeof OnAssetsConversionRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnAssetsConversionRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnNameLookupRequestArguments}\n * object.\n */\nexport function assertIsOnAssetsConversionRequestArguments(\n value: unknown,\n): asserts value is OnAssetsConversionRequestArguments {\n assertRequestArguments(value, OnAssetsConversionRequestArgumentsStruct);\n}\n\nexport const OnUserInputArgumentsStruct = object({\n id: string(),\n event: UserInputEventStruct,\n context: optional(nullable(InterfaceContextStruct)),\n});\n\nexport type OnUserInputArguments = Infer<typeof OnUserInputArgumentsStruct>;\n\n/**\n * Asserts that the given value is a valid {@link OnUserInputArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnUserInputArguments}\n * object.\n */\nexport function assertIsOnUserInputRequestArguments(\n value: unknown,\n): asserts value is OnUserInputArguments {\n assertRequestArguments(value, OnUserInputArgumentsStruct);\n}\n\nexport const OnProtocolRequestArgumentsStruct = object({\n scope: CaipChainIdStruct,\n request: JsonRpcRequestStruct,\n}) as unknown as Struct<{ scope: CaipChainId; request: JsonRpcRequest }, null>;\n\nexport type OnProtocolRequestArguments = Infer<\n typeof OnProtocolRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnProtocolRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnProtocolRequestArguments}\n * object.\n */\nexport function assertIsOnProtocolRequestArguments(\n value: unknown,\n): asserts value is OnProtocolRequestArguments {\n assertRequestArguments(value, OnProtocolRequestArgumentsStruct);\n}\n\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nconst OkResponseStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n result: OkStruct,\n});\n\nconst SnapRpcResponse = JsonRpcSuccessStruct;\n\nexport type OkResponse = Infer<typeof OkResponseStruct>;\nexport type SnapRpcResponse = Infer<typeof SnapRpcResponse>;\n\nexport type Response = OkResponse | SnapRpcResponse;\n\ntype RequestParams<Params extends unknown[] | undefined> =\n Params extends undefined ? [] : Params;\n\ntype RequestFunction<\n Args extends RequestArguments,\n ResponseType extends JsonRpcSuccess,\n> = (...args: RequestParams<Args>) => Promise<ResponseType['result']>;\n\nexport type Ping = RequestFunction<PingRequestArguments, OkResponse>;\nexport type Terminate = RequestFunction<TerminateRequestArguments, OkResponse>;\nexport type ExecuteSnap = RequestFunction<\n ExecuteSnapRequestArguments,\n OkResponse\n>;\nexport type SnapRpc = RequestFunction<SnapRpcRequestArguments, SnapRpcResponse>;\n"]}
|
|
1
|
+
{"version":3,"file":"validation.mjs","sourceRoot":"","sources":["../../src/common/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EACL,sBAAsB,EACtB,OAAO,IAAI,aAAa,EACxB,UAAU,EACV,oBAAoB,EACrB,4BAA4B;AAC7B,OAAO,EAAE,WAAW,EAAE,8BAA8B;AAEpD,OAAO,EACL,GAAG,EACH,KAAK,EACL,MAAM,EACN,OAAO,EACP,KAAK,EACL,EAAE,EACF,QAAQ,EACR,MAAM,EACN,MAAM,EACN,QAAQ,EACR,MAAM,EACN,IAAI,EACJ,MAAM,EACN,KAAK,EACL,KAAK,EACL,OAAO,EACR,8BAA8B;AAM/B,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,iBAAiB,EACjB,eAAe,EACf,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,UAAU,EACX,wBAAwB;AAEzB,MAAM,CAAC,MAAM,6BAA6B,GAAG,MAAM,CAAC;IAClD,OAAO,EAAE,QAAQ,CAAC,oBAAoB,CAAC;IACvC,EAAE,EAAE,QAAQ,CAAC,eAAe,CAAC;IAC7B,MAAM,EAAE,MAAM,EAAE;IAChB,MAAM,EAAE,QAAQ,CAAC,mBAAmB,CAAC;CACtC,CAAC,CAAC;AAMH,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,EAAE,C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AO,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC;SACzB,CAAC;KACH,CAAC;CACH,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,oBAAoB,GAAG,UAAU,CAAC;IAC7C,wBAAwB;IACxB,yBAAyB;IACzB,2BAA2B;CAC5B,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,+BAA+B,GAAG,MAAM,CAAC;IACpD,KAAK,EAAE,oBAAoB;CAC5B,CAAC,CAAC;AAMH;;;;;;;GAOG;AACH,MAAM,UAAU,iCAAiC,CAC/C,KAAc;IAEd,sBAAsB,CAAC,KAAK,EAAE,+BAA+B,CAAC,CAAC;AACjE,CAAC;AAED,gFAAgF;AAChF,6DAA6D;AAC7D,MAAM,gBAAgB,GAAG,MAAM,CAAC;IAC9B,EAAE,EAAE,eAAe;IACnB,OAAO,EAAE,oBAAoB;IAC7B,MAAM,EAAE,QAAQ;CACjB,CAAC,CAAC;AAEH,MAAM,eAAe,GAAG,oBAAoB,CAAC","sourcesContent":["import { rpcErrors } from '@metamask/rpc-errors';\nimport {\n InterfaceContextStruct,\n literal as customLiteral,\n typedUnion,\n UserInputEventStruct,\n} from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type { Infer, Struct } from '@metamask/superstruct';\nimport {\n any,\n array,\n assign,\n boolean,\n enums,\n is,\n nullable,\n number,\n object,\n optional,\n record,\n size,\n string,\n tuple,\n union,\n literal,\n} from '@metamask/superstruct';\nimport type {\n CaipChainId,\n JsonRpcRequest,\n JsonRpcSuccess,\n} from '@metamask/utils';\nimport {\n assertStruct,\n CaipAssetTypeStruct,\n CaipChainIdStruct,\n JsonRpcIdStruct,\n JsonRpcParamsStruct,\n JsonRpcRequestStruct,\n JsonRpcSuccessStruct,\n JsonRpcVersionStruct,\n JsonStruct,\n} from '@metamask/utils';\n\nexport const JsonRpcRequestWithoutIdStruct = object({\n jsonrpc: optional(JsonRpcVersionStruct),\n id: optional(JsonRpcIdStruct),\n method: string(),\n params: optional(JsonRpcParamsStruct),\n});\n\nexport type JsonRpcRequestWithoutId = Infer<\n typeof JsonRpcRequestWithoutIdStruct\n>;\n\nexport const EndowmentStruct = string();\nexport type Endowment = Infer<typeof EndowmentStruct>;\n\n/**\n * Check if the given value is an endowment.\n *\n * @param value - The value to check.\n * @returns Whether the value is an endowment.\n */\nexport function isEndowment(value: unknown): value is Endowment {\n return is(value, EndowmentStruct);\n}\n\n/**\n * Check if the given value is an array of endowments.\n *\n * @param value - The value to check.\n * @returns Whether the value is an array of endowments.\n */\nexport function isEndowmentsArray(value: unknown): value is Endowment[] {\n return Array.isArray(value) && value.every(isEndowment);\n}\n\nconst OkStruct = literal('OK');\n\nexport const PingRequestArgumentsStruct = optional(\n union([literal(undefined), array()]),\n);\n\nexport const TerminateRequestArgumentsStruct = union([\n literal(undefined),\n array(),\n]);\n\nexport const ExecuteSnapRequestArgumentsStruct = tuple([\n string(),\n string(),\n array(EndowmentStruct),\n]);\n\nexport const SnapRpcRequestArgumentsStruct = tuple([\n string(),\n enums(Object.values(HandlerType)),\n string(),\n assign(\n JsonRpcRequestWithoutIdStruct,\n object({\n // Previously this would validate that the parameters were valid JSON.\n // This is already validated for all messages received by the executor.\n // If that assumption changes, this should once again validate JSON.\n params: optional(record(string(), any())),\n }),\n ),\n]);\n\nexport type PingRequestArguments = Infer<typeof PingRequestArgumentsStruct>;\nexport type TerminateRequestArguments = Infer<\n typeof TerminateRequestArgumentsStruct\n>;\n\nexport type ExecuteSnapRequestArguments = Infer<\n typeof ExecuteSnapRequestArgumentsStruct\n>;\n\nexport type SnapRpcRequestArguments = Infer<\n typeof SnapRpcRequestArgumentsStruct\n>;\n\nexport type RequestArguments =\n | PingRequestArguments\n | TerminateRequestArguments\n | ExecuteSnapRequestArguments\n | SnapRpcRequestArguments;\n\n/**\n * Asserts that the given value is a valid request arguments object.\n *\n * @param value - The value to validate.\n * @param struct - The struct to validate the value against.\n * @throws If the value is not a valid request arguments object.\n */\nfunction assertRequestArguments<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n): asserts value is Struct<Type, Schema> {\n assertStruct(\n value,\n struct,\n 'Invalid request params',\n rpcErrors.invalidParams,\n );\n}\n\nexport const OnTransactionRequestArgumentsStruct = object({\n // TODO: Improve `transaction` type.\n transaction: record(string(), JsonStruct),\n chainId: CaipChainIdStruct,\n transactionOrigin: nullable(string()),\n});\n\nexport type OnTransactionRequestArguments = Infer<\n typeof OnTransactionRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnTransactionRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnTransactionRequestArguments}\n * object.\n */\nexport function assertIsOnTransactionRequestArguments(\n value: unknown,\n): asserts value is OnTransactionRequestArguments {\n assertRequestArguments(value, OnTransactionRequestArgumentsStruct);\n}\n\nexport const OnSignatureRequestArgumentsStruct = object({\n signature: record(string(), JsonStruct),\n signatureOrigin: nullable(string()),\n});\n\nexport type OnSignatureRequestArguments = Infer<\n typeof OnSignatureRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnSignatureRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnSignatureRequestArguments}\n * object.\n */\nexport function assertIsOnSignatureRequestArguments(\n value: unknown,\n): asserts value is OnSignatureRequestArguments {\n assertRequestArguments(value, OnSignatureRequestArgumentsStruct);\n}\n\nconst baseNameLookupArgs = { chainId: CaipChainIdStruct };\nconst domainRequestStruct = object({\n ...baseNameLookupArgs,\n address: string(),\n});\nconst addressRequestStruct = object({\n ...baseNameLookupArgs,\n domain: string(),\n});\n\nexport const OnNameLookupRequestArgumentsStruct = union([\n domainRequestStruct,\n addressRequestStruct,\n]);\n\nexport type OnNameLookupRequestArguments = Infer<\n typeof OnNameLookupRequestArgumentsStruct\n>;\n\nexport type PossibleLookupRequestArgs = typeof baseNameLookupArgs & {\n address?: string;\n domain?: string;\n};\n\n/**\n * Asserts that the given value is a valid {@link OnNameLookupRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnNameLookupRequestArguments}\n * object.\n */\nexport function assertIsOnNameLookupRequestArguments(\n value: unknown,\n): asserts value is OnNameLookupRequestArguments {\n assertRequestArguments(value, OnNameLookupRequestArgumentsStruct);\n}\n\nexport const OnAssetHistoricalPriceRequestArgumentsStruct = object({\n from: CaipAssetTypeStruct,\n to: CaipAssetTypeStruct,\n});\n\nexport type OnAssetHistoricalPriceRequestArguments = Infer<\n typeof OnAssetHistoricalPriceRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnAssetHistoricalPriceRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnAssetHistoricalPriceRequestArguments}\n * object.\n */\nexport function assertIsOnAssetHistoricalPriceRequestArguments(\n value: unknown,\n): asserts value is OnAssetHistoricalPriceRequestArguments {\n assertRequestArguments(value, OnAssetHistoricalPriceRequestArgumentsStruct);\n}\n\nexport const OnAssetsMarketDataRequestArgumentsStruct = object({\n assets: size(\n array(\n object({\n asset: CaipAssetTypeStruct,\n unit: CaipAssetTypeStruct,\n }),\n ),\n 1,\n Infinity,\n ),\n});\n\nexport type OnAssetsMarketDataRequestArguments = Infer<\n typeof OnAssetsMarketDataRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnAssetsMarketDataRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnAssetsMarketDataRequestArguments}\n * object.\n */\nexport function assertIsOnAssetsMarketDataRequestArguments(\n value: unknown,\n): asserts value is OnAssetsMarketDataRequestArguments {\n assertRequestArguments(value, OnAssetsMarketDataRequestArgumentsStruct);\n}\n\nexport const OnAssetsLookupRequestArgumentsStruct = object({\n assets: size(array(CaipAssetTypeStruct), 1, Infinity),\n});\n\nexport type OnAssetsLookupRequestArguments = Infer<\n typeof OnAssetsLookupRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnAssetsLookupRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnAssetsLookupRequestArguments}\n * object.\n */\nexport function assertIsOnAssetsLookupRequestArguments(\n value: unknown,\n): asserts value is OnAssetsLookupRequestArguments {\n assertRequestArguments(value, OnAssetsLookupRequestArgumentsStruct);\n}\n\nexport const OnAssetsConversionRequestArgumentsStruct = object({\n conversions: size(\n array(\n object({\n from: CaipAssetTypeStruct,\n to: CaipAssetTypeStruct,\n }),\n ),\n 1,\n Infinity,\n ),\n});\n\nexport type OnAssetsConversionRequestArguments = Infer<\n typeof OnAssetsConversionRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnAssetsConversionRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnNameLookupRequestArguments}\n * object.\n */\nexport function assertIsOnAssetsConversionRequestArguments(\n value: unknown,\n): asserts value is OnAssetsConversionRequestArguments {\n assertRequestArguments(value, OnAssetsConversionRequestArgumentsStruct);\n}\n\nexport const OnUserInputArgumentsStruct = object({\n id: string(),\n event: UserInputEventStruct,\n context: optional(nullable(InterfaceContextStruct)),\n});\n\nexport type OnUserInputArguments = Infer<typeof OnUserInputArgumentsStruct>;\n\n/**\n * Asserts that the given value is a valid {@link OnUserInputArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnUserInputArguments}\n * object.\n */\nexport function assertIsOnUserInputRequestArguments(\n value: unknown,\n): asserts value is OnUserInputArguments {\n assertRequestArguments(value, OnUserInputArgumentsStruct);\n}\n\nexport const OnProtocolRequestArgumentsStruct = object({\n scope: CaipChainIdStruct,\n request: JsonRpcRequestStruct,\n}) as unknown as Struct<{ scope: CaipChainId; request: JsonRpcRequest }, null>;\n\nexport type OnProtocolRequestArguments = Infer<\n typeof OnProtocolRequestArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnProtocolRequestArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnProtocolRequestArguments}\n * object.\n */\nexport function assertIsOnProtocolRequestArguments(\n value: unknown,\n): asserts value is OnProtocolRequestArguments {\n assertRequestArguments(value, OnProtocolRequestArgumentsStruct);\n}\n\nconst WebSocketOpenEventStruct = object({\n type: customLiteral('open'),\n id: string(),\n origin: string(),\n});\n\nconst WebSocketCloseEventStruct = object({\n type: customLiteral('close'),\n id: string(),\n origin: string(),\n code: number(),\n reason: nullable(string()),\n wasClean: nullable(boolean()),\n});\n\nconst WebSocketMessageEventStruct = object({\n type: customLiteral('message'),\n id: string(),\n origin: string(),\n data: typedUnion([\n object({\n type: customLiteral('text'),\n message: string(),\n }),\n object({\n type: customLiteral('binary'),\n message: array(number()),\n }),\n ]),\n});\n\nexport const WebSocketEventStruct = typedUnion([\n WebSocketOpenEventStruct,\n WebSocketCloseEventStruct,\n WebSocketMessageEventStruct,\n]);\n\nexport const OnWebSocketEventArgumentsStruct = object({\n event: WebSocketEventStruct,\n});\n\nexport type OnWebSocketEventArguments = Infer<\n typeof OnWebSocketEventArgumentsStruct\n>;\n\n/**\n * Asserts that the given value is a valid {@link OnWebSocketEventArguments}\n * object.\n *\n * @param value - The value to validate.\n * @throws If the value is not a valid {@link OnWebSocketEventArguments}\n * object.\n */\nexport function assertIsOnWebSocketEventArguments(\n value: unknown,\n): asserts value is OnWebSocketEventArguments {\n assertRequestArguments(value, OnWebSocketEventArgumentsStruct);\n}\n\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nconst OkResponseStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n result: OkStruct,\n});\n\nconst SnapRpcResponse = JsonRpcSuccessStruct;\n\nexport type OkResponse = Infer<typeof OkResponseStruct>;\nexport type SnapRpcResponse = Infer<typeof SnapRpcResponse>;\n\nexport type Response = OkResponse | SnapRpcResponse;\n\ntype RequestParams<Params extends unknown[] | undefined> =\n Params extends undefined ? [] : Params;\n\ntype RequestFunction<\n Args extends RequestArguments,\n ResponseType extends JsonRpcSuccess,\n> = (...args: RequestParams<Args>) => Promise<ResponseType['result']>;\n\nexport type Ping = RequestFunction<PingRequestArguments, OkResponse>;\nexport type Terminate = RequestFunction<TerminateRequestArguments, OkResponse>;\nexport type ExecuteSnap = RequestFunction<\n ExecuteSnapRequestArguments,\n OkResponse\n>;\nexport type SnapRpc = RequestFunction<SnapRpcRequestArguments, SnapRpcResponse>;\n"]}
|