@metamask/snaps-utils 3.2.0 → 4.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 +35 -1
- package/dist/cjs/auxiliary-files.js +6 -19
- package/dist/cjs/auxiliary-files.js.map +1 -1
- package/dist/cjs/checksum.js +22 -5
- package/dist/cjs/checksum.js.map +1 -1
- package/dist/cjs/cronjob.js.map +1 -1
- package/dist/cjs/enum.js.map +1 -1
- package/dist/cjs/errors.js +6 -154
- package/dist/cjs/errors.js.map +1 -1
- package/dist/cjs/handler-types.js +1 -0
- package/dist/cjs/handler-types.js.map +1 -1
- package/dist/cjs/handlers.js +17 -11
- package/dist/cjs/handlers.js.map +1 -1
- package/dist/cjs/index.browser.js +2 -0
- package/dist/cjs/index.browser.js.map +1 -1
- package/dist/cjs/index.js +2 -7
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/localization.js +113 -0
- package/dist/cjs/localization.js.map +1 -0
- package/dist/cjs/manifest/manifest.js +33 -19
- package/dist/cjs/manifest/manifest.js.map +1 -1
- package/dist/cjs/manifest/validation.js +3 -2
- package/dist/cjs/manifest/validation.js.map +1 -1
- package/dist/cjs/namespace.js +5 -1
- package/dist/cjs/namespace.js.map +1 -1
- package/dist/cjs/npm.js +18 -5
- package/dist/cjs/npm.js.map +1 -1
- package/dist/cjs/snaps.js +7 -6
- package/dist/cjs/snaps.js.map +1 -1
- package/dist/cjs/structs.js.map +1 -1
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/ui.js +51 -0
- package/dist/cjs/ui.js.map +1 -0
- package/dist/cjs/validation.js +4 -2
- package/dist/cjs/validation.js.map +1 -1
- package/dist/esm/auxiliary-files.js +1 -6
- package/dist/esm/auxiliary-files.js.map +1 -1
- package/dist/esm/checksum.js +25 -6
- package/dist/esm/checksum.js.map +1 -1
- package/dist/esm/cronjob.js +1 -1
- package/dist/esm/cronjob.js.map +1 -1
- package/dist/esm/enum.js.map +1 -1
- package/dist/esm/errors.js +2 -157
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/handler-types.js +1 -0
- package/dist/esm/handler-types.js.map +1 -1
- package/dist/esm/handlers.js +13 -7
- package/dist/esm/handlers.js.map +1 -1
- package/dist/esm/index.browser.js +2 -0
- package/dist/esm/index.browser.js.map +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/localization.js +115 -0
- package/dist/esm/localization.js.map +1 -0
- package/dist/esm/manifest/manifest.js +37 -19
- package/dist/esm/manifest/manifest.js.map +1 -1
- package/dist/esm/manifest/validation.js +4 -3
- package/dist/esm/manifest/validation.js.map +1 -1
- package/dist/esm/namespace.js +3 -2
- package/dist/esm/namespace.js.map +1 -1
- package/dist/esm/npm.js +18 -5
- package/dist/esm/npm.js.map +1 -1
- package/dist/esm/snaps.js +7 -6
- package/dist/esm/snaps.js.map +1 -1
- package/dist/esm/structs.js.map +1 -1
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/ui.js +48 -0
- package/dist/esm/ui.js.map +1 -0
- package/dist/esm/validation.js +4 -2
- package/dist/esm/validation.js.map +1 -1
- package/dist/types/auxiliary-files.d.ts +1 -5
- package/dist/types/checksum.d.ts +9 -2
- package/dist/types/enum.d.ts +1 -21
- package/dist/types/errors.d.ts +1 -109
- package/dist/types/handler-types.d.ts +2 -1
- package/dist/types/handlers.d.ts +73 -152
- package/dist/types/index.browser.d.ts +2 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/localization.d.ts +143 -0
- package/dist/types/manifest/manifest.d.ts +16 -7
- package/dist/types/manifest/validation.d.ts +83 -78
- package/dist/types/namespace.d.ts +11 -10
- package/dist/types/npm.d.ts +1 -1
- package/dist/types/snaps.d.ts +13 -43
- package/dist/types/structs.d.ts +23 -0
- package/dist/types/types.d.ts +4 -2
- package/dist/types/ui.d.ts +20 -0
- package/dist/types/validation.d.ts +1 -1
- package/package.json +3 -3
package/dist/esm/errors.js
CHANGED
|
@@ -39,60 +39,10 @@ function _class_private_field_set(receiver, privateMap, value) {
|
|
|
39
39
|
return value;
|
|
40
40
|
}
|
|
41
41
|
import { errorCodes, JsonRpcError as RpcError, serializeCause } from '@metamask/rpc-errors';
|
|
42
|
-
import {
|
|
43
|
-
|
|
44
|
-
* Get the error message from an unknown error type.
|
|
45
|
-
*
|
|
46
|
-
* - If the error is an object with a `message` property, return the message.
|
|
47
|
-
* - Otherwise, return the error converted to a string.
|
|
48
|
-
*
|
|
49
|
-
* @param error - The error to get the message from.
|
|
50
|
-
* @returns The error message.
|
|
51
|
-
*/ export function getErrorMessage(error) {
|
|
52
|
-
if (isObject(error) && hasProperty(error, 'message') && typeof error.message === 'string') {
|
|
53
|
-
return error.message;
|
|
54
|
-
}
|
|
55
|
-
return String(error);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Get the error stack from an unknown error type.
|
|
59
|
-
*
|
|
60
|
-
* @param error - The error to get the stack from.
|
|
61
|
-
* @returns The error stack, or undefined if the error does not have a valid
|
|
62
|
-
* stack.
|
|
63
|
-
*/ export function getErrorStack(error) {
|
|
64
|
-
if (isObject(error) && hasProperty(error, 'stack') && typeof error.stack === 'string') {
|
|
65
|
-
return error.stack;
|
|
66
|
-
}
|
|
67
|
-
return undefined;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Get the error code from an unknown error type.
|
|
71
|
-
*
|
|
72
|
-
* @param error - The error to get the code from.
|
|
73
|
-
* @returns The error code, or `-32603` if the error does not have a valid code.
|
|
74
|
-
*/ export function getErrorCode(error) {
|
|
75
|
-
if (isObject(error) && hasProperty(error, 'code') && typeof error.code === 'number' && Number.isInteger(error.code)) {
|
|
76
|
-
return error.code;
|
|
77
|
-
}
|
|
78
|
-
return errorCodes.rpc.internal;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Get the error data from an unknown error type.
|
|
82
|
-
*
|
|
83
|
-
* @param error - The error to get the data from.
|
|
84
|
-
* @returns The error data, or an empty object if the error does not have valid
|
|
85
|
-
* data.
|
|
86
|
-
*/ export function getErrorData(error) {
|
|
87
|
-
if (isObject(error) && hasProperty(error, 'data') && typeof error.data === 'object' && error.data !== null && isValidJson(error.data) && !Array.isArray(error.data)) {
|
|
88
|
-
return error.data;
|
|
89
|
-
}
|
|
90
|
-
return {};
|
|
91
|
-
}
|
|
42
|
+
import { getErrorMessage, getErrorStack, SNAP_ERROR_CODE, SNAP_ERROR_MESSAGE } from '@metamask/snaps-sdk';
|
|
43
|
+
import { isObject, isJsonRpcError } from '@metamask/utils';
|
|
92
44
|
export const SNAP_ERROR_WRAPPER_CODE = -31001;
|
|
93
45
|
export const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';
|
|
94
|
-
export const SNAP_ERROR_CODE = -31002;
|
|
95
|
-
export const SNAP_ERROR_MESSAGE = 'Snap Error';
|
|
96
46
|
var _error = /*#__PURE__*/ new WeakMap(), _message = /*#__PURE__*/ new WeakMap(), _stack = /*#__PURE__*/ new WeakMap();
|
|
97
47
|
export class WrappedSnapError extends Error {
|
|
98
48
|
/**
|
|
@@ -162,111 +112,6 @@ export class WrappedSnapError extends Error {
|
|
|
162
112
|
_class_private_field_set(this, _stack, getErrorStack(error));
|
|
163
113
|
}
|
|
164
114
|
}
|
|
165
|
-
var _code = /*#__PURE__*/ new WeakMap(), _message1 = /*#__PURE__*/ new WeakMap(), _data = /*#__PURE__*/ new WeakMap(), _stack1 = /*#__PURE__*/ new WeakMap();
|
|
166
|
-
/**
|
|
167
|
-
* A generic error which can be thrown by a Snap, without it causing the Snap to
|
|
168
|
-
* crash.
|
|
169
|
-
*/ export class SnapError extends Error {
|
|
170
|
-
/**
|
|
171
|
-
* The error name.
|
|
172
|
-
*
|
|
173
|
-
* @returns The error name.
|
|
174
|
-
*/ get name() {
|
|
175
|
-
return 'SnapError';
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* The error code.
|
|
179
|
-
*
|
|
180
|
-
* @returns The error code.
|
|
181
|
-
*/ get code() {
|
|
182
|
-
return _class_private_field_get(this, _code);
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* The error message.
|
|
186
|
-
*
|
|
187
|
-
* @returns The error message.
|
|
188
|
-
*/ get message() {
|
|
189
|
-
return _class_private_field_get(this, _message1);
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Additional data for the error.
|
|
193
|
-
*
|
|
194
|
-
* @returns Additional data for the error.
|
|
195
|
-
*/ get data() {
|
|
196
|
-
return _class_private_field_get(this, _data);
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* The error stack.
|
|
200
|
-
*
|
|
201
|
-
* @returns The error stack.
|
|
202
|
-
*/ get stack() {
|
|
203
|
-
return _class_private_field_get(this, _stack1);
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Convert the error to a JSON object.
|
|
207
|
-
*
|
|
208
|
-
* @returns The JSON object.
|
|
209
|
-
*/ toJSON() {
|
|
210
|
-
return {
|
|
211
|
-
code: SNAP_ERROR_CODE,
|
|
212
|
-
message: SNAP_ERROR_MESSAGE,
|
|
213
|
-
data: {
|
|
214
|
-
cause: {
|
|
215
|
-
code: this.code,
|
|
216
|
-
message: this.message,
|
|
217
|
-
stack: this.stack,
|
|
218
|
-
data: this.data
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Serialize the error to a JSON object. This is called by
|
|
225
|
-
* `@metamask/rpc-errors` when serializing the error.
|
|
226
|
-
*
|
|
227
|
-
* @returns The JSON object.
|
|
228
|
-
*/ serialize() {
|
|
229
|
-
return this.toJSON();
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Create a new `SnapError`.
|
|
233
|
-
*
|
|
234
|
-
* @param error - The error to create the `SnapError` from. If this is a
|
|
235
|
-
* `string`, it will be used as the error message. If this is an `Error`, its
|
|
236
|
-
* `message` property will be used as the error message. If this is a
|
|
237
|
-
* `JsonRpcError`, its `message` property will be used as the error message
|
|
238
|
-
* and its `code` property will be used as the error code. Otherwise, the
|
|
239
|
-
* error will be converted to a string and used as the error message.
|
|
240
|
-
* @param data - Additional data to include in the error. This will be merged
|
|
241
|
-
* with the error data, if any.
|
|
242
|
-
*/ constructor(error, data = {}){
|
|
243
|
-
const message = getErrorMessage(error);
|
|
244
|
-
super(message);
|
|
245
|
-
_class_private_field_init(this, _code, {
|
|
246
|
-
writable: true,
|
|
247
|
-
value: void 0
|
|
248
|
-
});
|
|
249
|
-
_class_private_field_init(this, _message1, {
|
|
250
|
-
writable: true,
|
|
251
|
-
value: void 0
|
|
252
|
-
});
|
|
253
|
-
_class_private_field_init(this, _data, {
|
|
254
|
-
writable: true,
|
|
255
|
-
value: void 0
|
|
256
|
-
});
|
|
257
|
-
_class_private_field_init(this, _stack1, {
|
|
258
|
-
writable: true,
|
|
259
|
-
value: void 0
|
|
260
|
-
});
|
|
261
|
-
_class_private_field_set(this, _message1, message);
|
|
262
|
-
_class_private_field_set(this, _code, getErrorCode(error));
|
|
263
|
-
_class_private_field_set(this, _data, {
|
|
264
|
-
...getErrorData(error),
|
|
265
|
-
...data
|
|
266
|
-
});
|
|
267
|
-
_class_private_field_set(this, _stack1, super.stack);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
115
|
/**
|
|
271
116
|
* Check if an object is a `SnapError`.
|
|
272
117
|
*
|
package/dist/esm/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/errors.ts"],"sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport {\n hasProperty,\n isJsonRpcError,\n isObject,\n isValidJson,\n} from '@metamask/utils';\n\n/**\n * Get the error message from an unknown error type.\n *\n * - If the error is an object with a `message` property, return the message.\n * - Otherwise, return the error converted to a string.\n *\n * @param error - The error to get the message from.\n * @returns The error message.\n */\nexport function getErrorMessage(error: unknown) {\n if (\n isObject(error) &&\n hasProperty(error, 'message') &&\n typeof error.message === 'string'\n ) {\n return error.message;\n }\n\n return String(error);\n}\n\n/**\n * Get the error stack from an unknown error type.\n *\n * @param error - The error to get the stack from.\n * @returns The error stack, or undefined if the error does not have a valid\n * stack.\n */\nexport function getErrorStack(error: unknown) {\n if (\n isObject(error) &&\n hasProperty(error, 'stack') &&\n typeof error.stack === 'string'\n ) {\n return error.stack;\n }\n\n return undefined;\n}\n\n/**\n * Get the error code from an unknown error type.\n *\n * @param error - The error to get the code from.\n * @returns The error code, or `-32603` if the error does not have a valid code.\n */\nexport function getErrorCode(error: unknown) {\n if (\n isObject(error) &&\n hasProperty(error, 'code') &&\n typeof error.code === 'number' &&\n Number.isInteger(error.code)\n ) {\n return error.code;\n }\n\n return errorCodes.rpc.internal;\n}\n\n/**\n * Get the error data from an unknown error type.\n *\n * @param error - The error to get the data from.\n * @returns The error data, or an empty object if the error does not have valid\n * data.\n */\nexport function getErrorData(error: unknown) {\n if (\n isObject(error) &&\n hasProperty(error, 'data') &&\n typeof error.data === 'object' &&\n error.data !== null &&\n isValidJson(error.data) &&\n !Array.isArray(error.data)\n ) {\n return error.data;\n }\n\n return {};\n}\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport const SNAP_ERROR_CODE = -31002;\nexport const SNAP_ERROR_MESSAGE = 'Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n code: typeof SNAP_ERROR_WRAPPER_CODE;\n message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n data: {\n cause: Json;\n };\n};\n\nexport type SerializedSnapError = {\n code: typeof SNAP_ERROR_CODE;\n message: typeof SNAP_ERROR_MESSAGE;\n data: {\n cause: JsonRpcError & {\n data: Record<string, Json>;\n };\n };\n};\n\nexport class WrappedSnapError extends Error {\n readonly #error: unknown;\n\n readonly #message: string;\n\n readonly #stack?: string;\n\n /**\n * Create a new `WrappedSnapError`.\n *\n * @param error - The error to create the `WrappedSnapError` from.\n */\n constructor(error: unknown) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#error = error;\n this.#message = message;\n this.#stack = getErrorStack(error);\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'WrappedSnapError';\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapErrorWrapper {\n const cause = isSnapError(this.#error)\n ? this.#error.serialize()\n : serializeCause(this.#error);\n\n return {\n code: SNAP_ERROR_WRAPPER_CODE,\n message: SNAP_ERROR_WRAPPER_MESSAGE,\n data: {\n cause,\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * A generic error which can be thrown by a Snap, without it causing the Snap to\n * crash.\n */\nexport class SnapError extends Error {\n readonly #code: number;\n\n readonly #message: string;\n\n readonly #data: Record<string, Json>;\n\n readonly #stack?: string;\n\n /**\n * Create a new `SnapError`.\n *\n * @param error - The error to create the `SnapError` from. If this is a\n * `string`, it will be used as the error message. If this is an `Error`, its\n * `message` property will be used as the error message. If this is a\n * `JsonRpcError`, its `message` property will be used as the error message\n * and its `code` property will be used as the error code. Otherwise, the\n * error will be converted to a string and used as the error message.\n * @param data - Additional data to include in the error. This will be merged\n * with the error data, if any.\n */\n constructor(\n error: string | Error | JsonRpcError,\n data: Record<string, Json> = {},\n ) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#message = message;\n this.#code = getErrorCode(error);\n this.#data = { ...getErrorData(error), ...data };\n this.#stack = super.stack;\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'SnapError';\n }\n\n /**\n * The error code.\n *\n * @returns The error code.\n */\n get code() {\n return this.#code;\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * Additional data for the error.\n *\n * @returns Additional data for the error.\n */\n get data() {\n return this.#data;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapError {\n return {\n code: SNAP_ERROR_CODE,\n message: SNAP_ERROR_MESSAGE,\n data: {\n cause: {\n code: this.code,\n message: this.message,\n stack: this.stack,\n data: this.data,\n },\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * Check if an object is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSnapError(error: unknown): error is SnapError {\n if (\n isObject(error) &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n const serialized = error.serialize();\n return isJsonRpcError(serialized) && isSerializedSnapError(serialized);\n }\n\n return false;\n}\n\n/**\n * Check if a JSON-RPC error is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSerializedSnapError(\n error: JsonRpcError,\n): error is SerializedSnapError {\n return error.code === SNAP_ERROR_CODE && error.message === SNAP_ERROR_MESSAGE;\n}\n\n/**\n * Check if a JSON-RPC error is a `WrappedSnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `WrappedSnapError`.\n */\nexport function isWrappedSnapError(\n error: unknown,\n): error is SerializedSnapErrorWrapper {\n return (\n isJsonRpcError(error) &&\n error.code === SNAP_ERROR_WRAPPER_CODE &&\n error.message === SNAP_ERROR_WRAPPER_MESSAGE\n );\n}\n\n/**\n * Get a JSON-RPC error with the given code, message, stack, and data.\n *\n * @param code - The error code.\n * @param message - The error message.\n * @param stack - The error stack.\n * @param data - Additional data for the error.\n * @returns The JSON-RPC error.\n */\nfunction getJsonRpcError(\n code: number,\n message: string,\n stack?: string,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack;\n\n return error;\n}\n\n/**\n * Attempt to unwrap an unknown error to a `JsonRpcError`. This function will\n * try to get the error code, message, and data from the error, and return a\n * `JsonRpcError` with those properties.\n *\n * @param error - The error to unwrap.\n * @returns A tuple containing the unwrapped error and a boolean indicating\n * whether the error was handled.\n */\nexport function unwrapError(\n error: unknown,\n): [error: RpcError<DataWithOptionalCause>, isHandled: boolean] {\n // This logic is a bit complicated, but it's necessary to handle all the\n // different types of errors that can be thrown by a Snap.\n\n // If the error is a wrapped Snap error, unwrap it.\n if (isWrappedSnapError(error)) {\n // The wrapped error can be a JSON-RPC error, or an unknown error. If it's\n // a JSON-RPC error, we can unwrap it further.\n if (isJsonRpcError(error.data.cause)) {\n // If the JSON-RPC error is a wrapped Snap error, unwrap it further.\n if (isSerializedSnapError(error.data.cause)) {\n const { code, message, stack, data } = error.data.cause.data.cause;\n return [getJsonRpcError(code, message, stack, data), true];\n }\n\n // Otherwise, we use the original JSON-RPC error.\n const { code, message, stack, data } = error.data.cause;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // Otherwise, we throw an internal error with the wrapped error as the\n // message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error.data.cause),\n getErrorStack(error.data.cause),\n ),\n false,\n ];\n }\n\n // The error can be a non-wrapped JSON-RPC error, in which case we can just\n // re-throw it with the same code, message, and data.\n if (isJsonRpcError(error)) {\n const { code, message, stack, data } = error;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // If the error is not a wrapped error, we don't know how to handle it, so we\n // throw an internal error with the error as the message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error),\n getErrorStack(error),\n ),\n false,\n ];\n}\n"],"names":["errorCodes","JsonRpcError","RpcError","serializeCause","hasProperty","isJsonRpcError","isObject","isValidJson","getErrorMessage","error","message","String","getErrorStack","stack","undefined","getErrorCode","code","Number","isInteger","rpc","internal","getErrorData","data","Array","isArray","SNAP_ERROR_WRAPPER_CODE","SNAP_ERROR_WRAPPER_MESSAGE","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","WrappedSnapError","Error","name","toJSON","cause","isSnapError","serialize","constructor","SnapError","serialized","isSerializedSnapError","isWrappedSnapError","getJsonRpcError","unwrapError"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SACEA,UAAU,EACVC,gBAAgBC,QAAQ,EACxBC,cAAc,QACT,uBAAuB;AAG9B,SACEC,WAAW,EACXC,cAAc,EACdC,QAAQ,EACRC,WAAW,QACN,kBAAkB;AAEzB;;;;;;;;CAQC,GACD,OAAO,SAASC,gBAAgBC,KAAc;IAC5C,IACEH,SAASG,UACTL,YAAYK,OAAO,cACnB,OAAOA,MAAMC,OAAO,KAAK,UACzB;QACA,OAAOD,MAAMC,OAAO;IACtB;IAEA,OAAOC,OAAOF;AAChB;AAEA;;;;;;CAMC,GACD,OAAO,SAASG,cAAcH,KAAc;IAC1C,IACEH,SAASG,UACTL,YAAYK,OAAO,YACnB,OAAOA,MAAMI,KAAK,KAAK,UACvB;QACA,OAAOJ,MAAMI,KAAK;IACpB;IAEA,OAAOC;AACT;AAEA;;;;;CAKC,GACD,OAAO,SAASC,aAAaN,KAAc;IACzC,IACEH,SAASG,UACTL,YAAYK,OAAO,WACnB,OAAOA,MAAMO,IAAI,KAAK,YACtBC,OAAOC,SAAS,CAACT,MAAMO,IAAI,GAC3B;QACA,OAAOP,MAAMO,IAAI;IACnB;IAEA,OAAOhB,WAAWmB,GAAG,CAACC,QAAQ;AAChC;AAEA;;;;;;CAMC,GACD,OAA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GAChCrB,cAAcH,MAAMa,IAAI,CAACW,KAAK;YAEhC;SACD;IACH;IAEA,2EAA2E;IAC3E,qDAAqD;IACrD,IAAI5B,eAAeI,QAAQ;QACzB,MAAM,EAAEO,IAAI,EAAEN,OAAO,EAAEG,KAAK,EAAES,IAAI,EAAE,GAAGb;QACvC,OAAO;YAACgC,gBAAgBzB,MAAMN,SAASG,OAAOS;YAAO;SAAM;IAC7D;IAEA,6EAA6E;IAC7E,yDAAyD;IACzD,OAAO;QACLmB,gBACEzC,WAAWmB,GAAG,CAACC,QAAQ,EACvBZ,gBAAgBC,QAChBG,cAAcH;QAEhB;KACD;AACH"}
|
|
1
|
+
{"version":3,"sources":["../../src/errors.ts"],"sourcesContent":["import {\n errorCodes,\n JsonRpcError as RpcError,\n serializeCause,\n} from '@metamask/rpc-errors';\nimport type { DataWithOptionalCause } from '@metamask/rpc-errors';\nimport type { SerializedSnapError, SnapError } from '@metamask/snaps-sdk';\nimport {\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from '@metamask/snaps-sdk';\nimport type { Json, JsonRpcError } from '@metamask/utils';\nimport { isObject, isJsonRpcError } from '@metamask/utils';\n\nexport const SNAP_ERROR_WRAPPER_CODE = -31001;\nexport const SNAP_ERROR_WRAPPER_MESSAGE = 'Wrapped Snap Error';\n\nexport type SerializedSnapErrorWrapper = {\n code: typeof SNAP_ERROR_WRAPPER_CODE;\n message: typeof SNAP_ERROR_WRAPPER_MESSAGE;\n data: {\n cause: Json;\n };\n};\n\nexport class WrappedSnapError extends Error {\n readonly #error: unknown;\n\n readonly #message: string;\n\n readonly #stack?: string;\n\n /**\n * Create a new `WrappedSnapError`.\n *\n * @param error - The error to create the `WrappedSnapError` from.\n */\n constructor(error: unknown) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#error = error;\n this.#message = message;\n this.#stack = getErrorStack(error);\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'WrappedSnapError';\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n get message() {\n return this.#message;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapErrorWrapper {\n const cause = isSnapError(this.#error)\n ? this.#error.serialize()\n : serializeCause(this.#error);\n\n return {\n code: SNAP_ERROR_WRAPPER_CODE,\n message: SNAP_ERROR_WRAPPER_MESSAGE,\n data: {\n cause,\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * Check if an object is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSnapError(error: unknown): error is SnapError {\n if (\n isObject(error) &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n const serialized = error.serialize();\n return isJsonRpcError(serialized) && isSerializedSnapError(serialized);\n }\n\n return false;\n}\n\n/**\n * Check if a JSON-RPC error is a `SnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `SnapError`.\n */\nexport function isSerializedSnapError(\n error: JsonRpcError,\n): error is SerializedSnapError {\n return error.code === SNAP_ERROR_CODE && error.message === SNAP_ERROR_MESSAGE;\n}\n\n/**\n * Check if a JSON-RPC error is a `WrappedSnapError`.\n *\n * @param error - The object to check.\n * @returns Whether the object is a `WrappedSnapError`.\n */\nexport function isWrappedSnapError(\n error: unknown,\n): error is SerializedSnapErrorWrapper {\n return (\n isJsonRpcError(error) &&\n error.code === SNAP_ERROR_WRAPPER_CODE &&\n error.message === SNAP_ERROR_WRAPPER_MESSAGE\n );\n}\n\n/**\n * Get a JSON-RPC error with the given code, message, stack, and data.\n *\n * @param code - The error code.\n * @param message - The error message.\n * @param stack - The error stack.\n * @param data - Additional data for the error.\n * @returns The JSON-RPC error.\n */\nfunction getJsonRpcError(\n code: number,\n message: string,\n stack?: string,\n data?: Json,\n) {\n const error = new RpcError(code, message, data);\n error.stack = stack;\n\n return error;\n}\n\n/**\n * Attempt to unwrap an unknown error to a `JsonRpcError`. This function will\n * try to get the error code, message, and data from the error, and return a\n * `JsonRpcError` with those properties.\n *\n * @param error - The error to unwrap.\n * @returns A tuple containing the unwrapped error and a boolean indicating\n * whether the error was handled.\n */\nexport function unwrapError(\n error: unknown,\n): [error: RpcError<DataWithOptionalCause>, isHandled: boolean] {\n // This logic is a bit complicated, but it's necessary to handle all the\n // different types of errors that can be thrown by a Snap.\n\n // If the error is a wrapped Snap error, unwrap it.\n if (isWrappedSnapError(error)) {\n // The wrapped error can be a JSON-RPC error, or an unknown error. If it's\n // a JSON-RPC error, we can unwrap it further.\n if (isJsonRpcError(error.data.cause)) {\n // If the JSON-RPC error is a wrapped Snap error, unwrap it further.\n if (isSerializedSnapError(error.data.cause)) {\n const { code, message, stack, data } = error.data.cause.data.cause;\n return [getJsonRpcError(code, message, stack, data), true];\n }\n\n // Otherwise, we use the original JSON-RPC error.\n const { code, message, stack, data } = error.data.cause;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // Otherwise, we throw an internal error with the wrapped error as the\n // message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error.data.cause),\n getErrorStack(error.data.cause),\n ),\n false,\n ];\n }\n\n // The error can be a non-wrapped JSON-RPC error, in which case we can just\n // re-throw it with the same code, message, and data.\n if (isJsonRpcError(error)) {\n const { code, message, stack, data } = error;\n return [getJsonRpcError(code, message, stack, data), false];\n }\n\n // If the error is not a wrapped error, we don't know how to handle it, so we\n // throw an internal error with the error as the message.\n return [\n getJsonRpcError(\n errorCodes.rpc.internal,\n getErrorMessage(error),\n getErrorStack(error),\n ),\n false,\n ];\n}\n"],"names":["errorCodes","JsonRpcError","RpcError","serializeCause","getErrorMessage","getErrorStack","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","isObject","isJsonRpcError","SNAP_ERROR_WRAPPER_CODE","SNAP_ERROR_WRAPPER_MESSAGE","WrappedSnapError","Error","name","message","stack","toJSON","cause","isSnapError","error","serialize","code","data","constructor","serialized","isSerializedSnapError","isWrappedSnapError","getJsonRpcError","unwrapError","rpc","internal"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SACEA,UAAU,EACVC,gBAAgBC,QAAQ,EACxBC,cAAc,QACT,uBAAuB;AAG9B,SACEC,eAAe,EACfC,aAAa,EACbC,eAAe,EACfC,kBAAkB,QACb,sBAAsB;AAE7B,SAASC,QAAQ,EAAEC,cAAc,QAAQ,kBAAkB;AAE3D,OAAO,MAAMC,0BAA0B,CAAC,MAAM;AAC9C,OAAO,MAAMC,6BAA6B,qBAAqB;IAWpD,sCAEA,wCAEA;AALX,OAAO,MAAMC,yBAAyBC;IAqBpC;;;;GAIC,GACD,IAAIC,OAAO;QACT,OAAO;IACT;IAEA;;;;GAIC,GACD,IAAIC,UAAU;QACZ,gCAAO,IAAI,EAAEA;IACf;IAEA;;;;GAIC,GACD,IAAIC,QAAQ;QACV,gCAAO,IAAI,EAAEA;IACf;IAEA;;;;GAIC,GACDC,SAAqC;QACnC,MAAMC,QAAQC,qCAAY,IAAI,EAAEC,WAC5B,yBAAA,IAAI,EAAEA,QAAMC,SAAS,KACrBlB,wCAAe,IAAI,EAAEiB;QAEzB,OAAO;YACLE,MAAMZ;YACNK,SAASJ;YACTY,MAAM;gBACJL;YACF;QACF;IACF;IAEA;;;;;GAKC,GACDG,YAAY;QACV,OAAO,IAAI,CAACJ,MAAM;IACpB;IApEA;;;;GAIC,GACDO,YAAYJ,KAAc,CAAE;QAC1B,MAAML,UAAUX,gBAAgBgB;QAChC,KAAK,CAACL;QAbR,gCAAS;;mBAAT,KAAA;;QAEA,gCAAS;;mBAAT,KAAA;;QAEA,gCAAS;;mBAAT,KAAA;;uCAWQK,QAAQA;uCACRL,UAAUA;uCACV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E,GAAGH,MAAMG,IAAI,CAACL,KAAK;YACvD,OAAO;gBAACU,gBAAgBN,MAAMP,SAASC,OAAOO;gBAAO;aAAM;QAC7D;QAEA,sEAAsE;QACtE,WAAW;QACX,OAAO;YACLK,gBACE5B,WAAW8B,GAAG,CAACC,QAAQ,EACvB3B,gBAAgBgB,MAAMG,IAAI,CAACL,KAAK,GAChCb,cAAce,MAAMG,IAAI,CAACL,KAAK;YAEhC;SACD;IACH;IAEA,2EAA2E;IAC3E,qDAAqD;IACrD,IAAIT,eAAeW,QAAQ;QACzB,MAAM,EAAEE,IAAI,EAAEP,OAAO,EAAEC,KAAK,EAAEO,IAAI,EAAE,GAAGH;QACvC,OAAO;YAACQ,gBAAgBN,MAAMP,SAASC,OAAOO;YAAO;SAAM;IAC7D;IAEA,6EAA6E;IAC7E,yDAAyD;IACzD,OAAO;QACLK,gBACE5B,WAAW8B,GAAG,CAACC,QAAQ,EACvB3B,gBAAgBgB,QAChBf,cAAce;QAEhB;KACD;AACH"}
|
|
@@ -7,6 +7,7 @@ export var HandlerType;
|
|
|
7
7
|
HandlerType["OnUpdate"] = 'onUpdate';
|
|
8
8
|
HandlerType["OnNameLookup"] = 'onNameLookup';
|
|
9
9
|
HandlerType["OnKeyringRequest"] = 'onKeyringRequest';
|
|
10
|
+
HandlerType["OnHomePage"] = 'onHomePage';
|
|
10
11
|
})(HandlerType || (HandlerType = {}));
|
|
11
12
|
export const SNAP_EXPORT_NAMES = Object.values(HandlerType);
|
|
12
13
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/handler-types.ts"],"sourcesContent":["export enum HandlerType {\n OnRpcRequest = 'onRpcRequest',\n OnTransaction = 'onTransaction',\n OnCronjob = 'onCronjob',\n OnInstall = 'onInstall',\n OnUpdate = 'onUpdate',\n OnNameLookup = 'onNameLookup',\n OnKeyringRequest = 'onKeyringRequest',\n}\n\nexport type SnapHandler = {\n /**\n * The type of handler.\n */\n type: HandlerType;\n\n /**\n * Whether the handler is required, i.e., whether the request will fail if the\n * handler is called, but the snap does not export it.\n *\n * This is primarily used for the lifecycle handlers, which are optional.\n */\n required: boolean;\n\n /**\n * Validate the given snap export. This should return a type guard for the\n * handler type.\n *\n * @param snapExport - The export to validate.\n * @returns Whether the export is valid.\n */\n validator: (snapExport: unknown) => boolean;\n};\n\nexport const SNAP_EXPORT_NAMES = Object.values(HandlerType);\n"],"names":["HandlerType","OnRpcRequest","OnTransaction","OnCronjob","OnInstall","OnUpdate","OnNameLookup","OnKeyringRequest","SNAP_EXPORT_NAMES","Object","values"],"mappings":"WAAO;UAAKA,WAAW;IAAXA,YACVC,kBAAe;IADLD,YAEVE,mBAAgB;IAFNF,YAGVG,eAAY;IAHFH,YAIVI,eAAY;IAJFJ,YAKVK,cAAW;IALDL,YAMVM,kBAAe;IANLN,YAOVO,sBAAmB;
|
|
1
|
+
{"version":3,"sources":["../../src/handler-types.ts"],"sourcesContent":["export enum HandlerType {\n OnRpcRequest = 'onRpcRequest',\n OnTransaction = 'onTransaction',\n OnCronjob = 'onCronjob',\n OnInstall = 'onInstall',\n OnUpdate = 'onUpdate',\n OnNameLookup = 'onNameLookup',\n OnKeyringRequest = 'onKeyringRequest',\n OnHomePage = 'onHomePage',\n}\n\nexport type SnapHandler = {\n /**\n * The type of handler.\n */\n type: HandlerType;\n\n /**\n * Whether the handler is required, i.e., whether the request will fail if the\n * handler is called, but the snap does not export it.\n *\n * This is primarily used for the lifecycle handlers, which are optional.\n */\n required: boolean;\n\n /**\n * Validate the given snap export. This should return a type guard for the\n * handler type.\n *\n * @param snapExport - The export to validate.\n * @returns Whether the export is valid.\n */\n validator: (snapExport: unknown) => boolean;\n};\n\nexport const SNAP_EXPORT_NAMES = Object.values(HandlerType);\n"],"names":["HandlerType","OnRpcRequest","OnTransaction","OnCronjob","OnInstall","OnUpdate","OnNameLookup","OnKeyringRequest","OnHomePage","SNAP_EXPORT_NAMES","Object","values"],"mappings":"WAAO;UAAKA,WAAW;IAAXA,YACVC,kBAAe;IADLD,YAEVE,mBAAgB;IAFNF,YAGVG,eAAY;IAHFH,YAIVI,eAAY;IAJFJ,YAKVK,cAAW;IALDL,YAMVM,kBAAe;IANLN,YAOVO,sBAAmB;IAPTP,YAQVQ,gBAAa;GARHR,gBAAAA;AAmCZ,OAAO,MAAMS,oBAAoBC,OAAOC,MAAM,CAACX,aAAa"}
|
package/dist/esm/handlers.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ComponentStruct } from '@metamask/snaps-
|
|
2
|
-
import { literal, object, optional } from 'superstruct';
|
|
1
|
+
import { SeverityLevel, ComponentStruct } from '@metamask/snaps-sdk';
|
|
2
|
+
import { literal, nullable, object, optional } from 'superstruct';
|
|
3
3
|
import { HandlerType } from './handler-types';
|
|
4
4
|
export const SNAP_EXPORTS = {
|
|
5
5
|
[HandlerType.OnRpcRequest]: {
|
|
@@ -50,15 +50,21 @@ export const SNAP_EXPORTS = {
|
|
|
50
50
|
validator: (snapExport)=>{
|
|
51
51
|
return typeof snapExport === 'function';
|
|
52
52
|
}
|
|
53
|
+
},
|
|
54
|
+
[HandlerType.OnHomePage]: {
|
|
55
|
+
type: HandlerType.OnHomePage,
|
|
56
|
+
required: true,
|
|
57
|
+
validator: (snapExport)=>{
|
|
58
|
+
return typeof snapExport === 'function';
|
|
59
|
+
}
|
|
53
60
|
}
|
|
54
61
|
};
|
|
55
|
-
export
|
|
56
|
-
(function(SeverityLevel) {
|
|
57
|
-
SeverityLevel["Critical"] = 'critical';
|
|
58
|
-
})(SeverityLevel || (SeverityLevel = {}));
|
|
59
|
-
export const OnTransactionResponseStruct = object({
|
|
62
|
+
export const OnTransactionResponseStruct = nullable(object({
|
|
60
63
|
content: ComponentStruct,
|
|
61
64
|
severity: optional(literal(SeverityLevel.Critical))
|
|
65
|
+
}));
|
|
66
|
+
export const OnHomePageResponseStruct = object({
|
|
67
|
+
content: ComponentStruct
|
|
62
68
|
});
|
|
63
69
|
|
|
64
70
|
//# sourceMappingURL=handlers.js.map
|
package/dist/esm/handlers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/handlers.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../src/handlers.ts"],"sourcesContent":["import type {\n OnCronjobHandler,\n OnHomePageHandler,\n OnInstallHandler,\n OnKeyringRequestHandler,\n OnNameLookupHandler,\n OnRpcRequestHandler,\n OnTransactionHandler,\n OnUpdateHandler,\n} from '@metamask/snaps-sdk';\nimport { SeverityLevel, ComponentStruct } from '@metamask/snaps-sdk';\nimport { literal, nullable, object, optional } from 'superstruct';\n\nimport type { SnapHandler } from './handler-types';\nimport { HandlerType } from './handler-types';\n\nexport type SnapRpcHookArgs = {\n origin: string;\n handler: HandlerType;\n request: Record<string, unknown>;\n};\n\nexport const SNAP_EXPORTS = {\n [HandlerType.OnRpcRequest]: {\n type: HandlerType.OnRpcRequest,\n required: true,\n validator: (snapExport: unknown): snapExport is OnRpcRequestHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnTransaction]: {\n type: HandlerType.OnTransaction,\n required: true,\n validator: (snapExport: unknown): snapExport is OnTransactionHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnCronjob]: {\n type: HandlerType.OnCronjob,\n required: true,\n validator: (snapExport: unknown): snapExport is OnCronjobHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnNameLookup]: {\n type: HandlerType.OnNameLookup,\n required: true,\n validator: (snapExport: unknown): snapExport is OnNameLookupHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnInstall]: {\n type: HandlerType.OnInstall,\n required: false,\n validator: (snapExport: unknown): snapExport is OnInstallHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnUpdate]: {\n type: HandlerType.OnUpdate,\n required: false,\n validator: (snapExport: unknown): snapExport is OnUpdateHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnKeyringRequest]: {\n type: HandlerType.OnKeyringRequest,\n required: true,\n validator: (snapExport: unknown): snapExport is OnKeyringRequestHandler => {\n return typeof snapExport === 'function';\n },\n },\n [HandlerType.OnHomePage]: {\n type: HandlerType.OnHomePage,\n required: true,\n validator: (snapExport: unknown): snapExport is OnHomePageHandler => {\n return typeof snapExport === 'function';\n },\n },\n} as const;\n\nexport const OnTransactionResponseStruct = nullable(\n object({\n content: ComponentStruct,\n severity: optional(literal(SeverityLevel.Critical)),\n }),\n);\n\nexport const OnHomePageResponseStruct = object({\n content: ComponentStruct,\n});\n\n/**\n * Utility type for getting the handler function type from a handler type.\n */\nexport type HandlerFunction<Type extends SnapHandler> =\n Type['validator'] extends (snapExport: unknown) => snapExport is infer Handler\n ? Handler\n : never;\n\n/**\n * All the function-based handlers that a snap can implement.\n */\nexport type SnapFunctionExports = {\n [Key in keyof typeof SNAP_EXPORTS]?: HandlerFunction<\n (typeof SNAP_EXPORTS)[Key]\n >;\n};\n\n/**\n * All handlers that a snap can implement.\n */\nexport type SnapExports = SnapFunctionExports;\n"],"names":["SeverityLevel","ComponentStruct","literal","nullable","object","optional","HandlerType","SNAP_EXPORTS","OnRpcRequest","type","required","validator","snapExport","OnTransaction","OnCronjob","OnNameLookup","OnInstall","OnUpdate","OnKeyringRequest","OnHomePage","OnTransactionResponseStruct","content","severity","Critical","OnHomePageResponseStruct"],"mappings":"AAUA,SAASA,aAAa,EAAEC,eAAe,QAAQ,sBAAsB;AACrE,SAASC,OAAO,EAAEC,QAAQ,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,cAAc;AAGlE,SAASC,WAAW,QAAQ,kBAAkB;AAQ9C,OAAO,MAAMC,eAAe;IAC1B,CAACD,YAAYE,YAAY,CAAC,EAAE;QAC1BC,MAAMH,YAAYE,YAAY;QAC9BE,UAAU;QACVC,WAAW,CAACC;YACV,OAAO,OAAOA,eAAe;QAC/B;IACF;IACA,CAACN,YAAYO,aAAa,CAAC,EAAE;QAC3BJ,MAAMH,YAAYO,aAAa;QAC/BH,UAAU;QACVC,WAAW,CAACC;YACV,OAAO,OAAOA,eAAe;QAC/B;IACF;IACA,CAACN,YAAYQ,SAAS,CAAC,EAAE;QACvBL,MAAMH,YAAYQ,SAAS;QAC3BJ,UAAU;QACVC,WAAW,CAACC;YACV,OAAO,OAAOA,eAAe;QAC/B;IACF;IACA,CAACN,YAAYS,YAAY,CAAC,EAAE;QAC1BN,MAAMH,YAAYS,YAAY;QAC9BL,UAAU;QACVC,WAAW,CAACC;YACV,OAAO,OAAOA,eAAe;QAC/B;IACF;IACA,CAACN,YAAYU,SAAS,CAAC,EAAE;QACvBP,MAAMH,YAAYU,SAAS;QAC3BN,UAAU;QACVC,WAAW,CAACC;YACV,OAAO,OAAOA,eAAe;QAC/B;IACF;IACA,CAACN,YAAYW,QAAQ,CAAC,EAAE;QACtBR,MAAMH,YAAYW,QAAQ;QAC1BP,UAAU;QACVC,WAAW,CAACC;YACV,OAAO,OAAOA,eAAe;QAC/B;IACF;IACA,CAACN,YAAYY,gBAAgB,CAAC,EAAE;QAC9BT,MAAMH,YAAYY,gBAAgB;QAClCR,UAAU;QACVC,WAAW,CAACC;YACV,OAAO,OAAOA,eAAe;QAC/B;IACF;IACA,CAACN,YAAYa,UAAU,CAAC,EAAE;QACxBV,MAAMH,YAAYa,UAAU;QAC5BT,UAAU;QACVC,WAAW,CAACC;YACV,OAAO,OAAOA,eAAe;QAC/B;IACF;AACF,EAAW;AAEX,OAAO,MAAMQ,8BAA8BjB,SACzCC,OAAO;IACLiB,SAASpB;IACTqB,UAAUjB,SAASH,QAAQF,cAAcuB,QAAQ;AACnD,IACA;AAEF,OAAO,MAAMC,2BAA2BpB,OAAO;IAC7CiB,SAASpB;AACX,GAAG"}
|
|
@@ -13,6 +13,7 @@ export * from './handler-types';
|
|
|
13
13
|
export * from './iframe';
|
|
14
14
|
export * from './json';
|
|
15
15
|
export * from './json-rpc';
|
|
16
|
+
export * from './localization';
|
|
16
17
|
export * from './logging';
|
|
17
18
|
export * from './manifest/index.browser';
|
|
18
19
|
export * from './namespace';
|
|
@@ -21,6 +22,7 @@ export * from './snaps';
|
|
|
21
22
|
export * from './strings';
|
|
22
23
|
export * from './structs';
|
|
23
24
|
export * from './types';
|
|
25
|
+
export * from './ui';
|
|
24
26
|
export * from './validation';
|
|
25
27
|
export * from './versions';
|
|
26
28
|
export * from './virtual-file/index.browser';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.browser.ts"],"sourcesContent":["export * from './array';\nexport * from './auxiliary-files';\nexport * from './caveats';\nexport * from './checksum';\nexport * from './cronjob';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './enum';\nexport * from './errors';\nexport * from './handlers';\nexport * from './handler-types';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './logging';\nexport * from './manifest/index.browser';\nexport * from './namespace';\nexport * from './path';\nexport * from './snaps';\nexport * from './strings';\nexport * from './structs';\nexport * from './types';\nexport * from './validation';\nexport * from './versions';\nexport * from './virtual-file/index.browser';\n"],"names":[],"mappings":"AAAA,cAAc,UAAU;AACxB,cAAc,oBAAoB;AAClC,cAAc,YAAY;AAC1B,cAAc,aAAa;AAC3B,cAAc,YAAY;AAC1B,cAAc,eAAe;AAC7B,cAAc,uBAAuB;AACrC,cAAc,YAAY;AAC1B,cAAc,SAAS;AACvB,cAAc,WAAW;AACzB,cAAc,aAAa;AAC3B,cAAc,kBAAkB;AAChC,cAAc,WAAW;AACzB,cAAc,SAAS;AACvB,cAAc,aAAa;AAC3B,cAAc,YAAY;AAC1B,cAAc,2BAA2B;AACzC,cAAc,cAAc;AAC5B,cAAc,SAAS;AACvB,cAAc,UAAU;AACxB,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,UAAU;AACxB,cAAc,eAAe;AAC7B,cAAc,aAAa;AAC3B,cAAc,+BAA+B"}
|
|
1
|
+
{"version":3,"sources":["../../src/index.browser.ts"],"sourcesContent":["export * from './array';\nexport * from './auxiliary-files';\nexport * from './caveats';\nexport * from './checksum';\nexport * from './cronjob';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './enum';\nexport * from './errors';\nexport * from './handlers';\nexport * from './handler-types';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './localization';\nexport * from './logging';\nexport * from './manifest/index.browser';\nexport * from './namespace';\nexport * from './path';\nexport * from './snaps';\nexport * from './strings';\nexport * from './structs';\nexport * from './types';\nexport * from './ui';\nexport * from './validation';\nexport * from './versions';\nexport * from './virtual-file/index.browser';\n"],"names":[],"mappings":"AAAA,cAAc,UAAU;AACxB,cAAc,oBAAoB;AAClC,cAAc,YAAY;AAC1B,cAAc,aAAa;AAC3B,cAAc,YAAY;AAC1B,cAAc,eAAe;AAC7B,cAAc,uBAAuB;AACrC,cAAc,YAAY;AAC1B,cAAc,SAAS;AACvB,cAAc,WAAW;AACzB,cAAc,aAAa;AAC3B,cAAc,kBAAkB;AAChC,cAAc,WAAW;AACzB,cAAc,SAAS;AACvB,cAAc,aAAa;AAC3B,cAAc,iBAAiB;AAC/B,cAAc,YAAY;AAC1B,cAAc,2BAA2B;AACzC,cAAc,cAAc;AAC5B,cAAc,SAAS;AACvB,cAAc,UAAU;AACxB,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,UAAU;AACxB,cAAc,OAAO;AACrB,cAAc,eAAe;AAC7B,cAAc,aAAa;AAC3B,cAAc,+BAA+B"}
|
package/dist/esm/index.js
CHANGED
|
@@ -15,6 +15,7 @@ export * from './handler-types';
|
|
|
15
15
|
export * from './iframe';
|
|
16
16
|
export * from './json';
|
|
17
17
|
export * from './json-rpc';
|
|
18
|
+
export * from './localization';
|
|
18
19
|
export * from './logging';
|
|
19
20
|
export * from './manifest';
|
|
20
21
|
export * from './mock';
|
|
@@ -26,9 +27,9 @@ export * from './snaps';
|
|
|
26
27
|
export * from './strings';
|
|
27
28
|
export * from './structs';
|
|
28
29
|
export * from './types';
|
|
30
|
+
export * from './ui';
|
|
29
31
|
export * from './validation';
|
|
30
32
|
export * from './versions';
|
|
31
33
|
export * from './virtual-file';
|
|
32
|
-
export { assertLinksAreSafe } from '@metamask/snaps-ui';
|
|
33
34
|
|
|
34
35
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './array';\nexport * from './auxiliary-files';\nexport * from './caveats';\nexport * from './cronjob';\nexport * from './checksum';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './enum';\nexport * from './eval';\nexport * from './errors';\nexport * from './fs';\nexport * from './handlers';\nexport * from './handler-types';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './logging';\nexport * from './manifest';\nexport * from './mock';\nexport * from './namespace';\nexport * from './npm';\nexport * from './path';\nexport * from './post-process';\nexport * from './snaps';\nexport * from './strings';\nexport * from './structs';\nexport * from './types';\nexport * from './
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './array';\nexport * from './auxiliary-files';\nexport * from './caveats';\nexport * from './cronjob';\nexport * from './checksum';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './enum';\nexport * from './eval';\nexport * from './errors';\nexport * from './fs';\nexport * from './handlers';\nexport * from './handler-types';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './localization';\nexport * from './logging';\nexport * from './manifest';\nexport * from './mock';\nexport * from './namespace';\nexport * from './npm';\nexport * from './path';\nexport * from './post-process';\nexport * from './snaps';\nexport * from './strings';\nexport * from './structs';\nexport * from './types';\nexport * from './ui';\nexport * from './validation';\nexport * from './versions';\nexport * from './virtual-file';\n"],"names":[],"mappings":"AAAA,cAAc,UAAU;AACxB,cAAc,oBAAoB;AAClC,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,aAAa;AAC3B,cAAc,eAAe;AAC7B,cAAc,uBAAuB;AACrC,cAAc,YAAY;AAC1B,cAAc,SAAS;AACvB,cAAc,SAAS;AACvB,cAAc,WAAW;AACzB,cAAc,OAAO;AACrB,cAAc,aAAa;AAC3B,cAAc,kBAAkB;AAChC,cAAc,WAAW;AACzB,cAAc,SAAS;AACvB,cAAc,aAAa;AAC3B,cAAc,iBAAiB;AAC/B,cAAc,YAAY;AAC1B,cAAc,aAAa;AAC3B,cAAc,SAAS;AACvB,cAAc,cAAc;AAC5B,cAAc,QAAQ;AACtB,cAAc,SAAS;AACvB,cAAc,iBAAiB;AAC/B,cAAc,UAAU;AACxB,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,UAAU;AACxB,cAAc,OAAO;AACrB,cAAc,eAAe;AAC7B,cAAc,aAAa;AAC3B,cAAc,iBAAiB"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { getErrorMessage } from '@metamask/snaps-sdk';
|
|
2
|
+
import { create, object, optional, record, string, StructError } from 'superstruct';
|
|
3
|
+
import { parseJson } from './json';
|
|
4
|
+
export const LOCALIZABLE_FIELDS = [
|
|
5
|
+
'description',
|
|
6
|
+
'proposedName'
|
|
7
|
+
];
|
|
8
|
+
export const LocalizationFileStruct = object({
|
|
9
|
+
locale: string(),
|
|
10
|
+
messages: record(string(), object({
|
|
11
|
+
message: string(),
|
|
12
|
+
description: optional(string())
|
|
13
|
+
}))
|
|
14
|
+
});
|
|
15
|
+
/**
|
|
16
|
+
* Validate a list of localization files.
|
|
17
|
+
*
|
|
18
|
+
* @param localizationFiles - The localization files to validate.
|
|
19
|
+
* @returns The validated localization files.
|
|
20
|
+
* @throws If any of the files are considered invalid.
|
|
21
|
+
*/ export function getValidatedLocalizationFiles(localizationFiles) {
|
|
22
|
+
for (const file of localizationFiles){
|
|
23
|
+
try {
|
|
24
|
+
file.result = create(parseJson(file.toString()), LocalizationFileStruct);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
if (error instanceof StructError) {
|
|
27
|
+
throw new Error(`Failed to validate localization file "${file.path}": ${error.message}.`);
|
|
28
|
+
}
|
|
29
|
+
if (error instanceof SyntaxError) {
|
|
30
|
+
throw new Error(`Failed to parse localization file "${file.path}" as JSON.`);
|
|
31
|
+
}
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return localizationFiles;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Get the localization file for a given locale. If the locale is not found,
|
|
39
|
+
* the English localization file will be returned.
|
|
40
|
+
*
|
|
41
|
+
* @param locale - The locale to use.
|
|
42
|
+
* @param localizationFiles - The localization files to use.
|
|
43
|
+
* @returns The localization file, or `undefined` if no localization file was
|
|
44
|
+
* found.
|
|
45
|
+
*/ export function getLocalizationFile(locale, localizationFiles) {
|
|
46
|
+
const file = localizationFiles.find((localizationFile)=>localizationFile.locale === locale);
|
|
47
|
+
if (!file) {
|
|
48
|
+
return localizationFiles.find((localizationFile)=>localizationFile.locale === 'en');
|
|
49
|
+
}
|
|
50
|
+
return file;
|
|
51
|
+
}
|
|
52
|
+
export const TRANSLATION_REGEX = /\{\{\s?([a-zA-Z0-9-_\s]+)\s?\}\}/gu;
|
|
53
|
+
/**
|
|
54
|
+
* Translate a string using a localization file. This will replace all instances
|
|
55
|
+
* of `{{key}}` with the localized version of `key`.
|
|
56
|
+
*
|
|
57
|
+
* @param value - The string to translate.
|
|
58
|
+
* @param file - The localization file to use, or `undefined` if no localization
|
|
59
|
+
* file was found.
|
|
60
|
+
* @returns The translated string.
|
|
61
|
+
* @throws If the string contains a key that is not present in the localization
|
|
62
|
+
* file, or if no localization file was found.
|
|
63
|
+
*/ export function translate(value, file) {
|
|
64
|
+
const matches = value.matchAll(TRANSLATION_REGEX);
|
|
65
|
+
const array = Array.from(matches);
|
|
66
|
+
return array.reduce((result, [match, key])=>{
|
|
67
|
+
if (!file) {
|
|
68
|
+
throw new Error(`Failed to translate "${value}": No localization file found.`);
|
|
69
|
+
}
|
|
70
|
+
const translation = file.messages[key.trim()];
|
|
71
|
+
if (!translation) {
|
|
72
|
+
throw new Error(`Failed to translate "${value}": No translation found for "${key.trim()}" in "${file.locale}" file.`);
|
|
73
|
+
}
|
|
74
|
+
return result.replace(match, translation.message);
|
|
75
|
+
}, value);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Get the localized Snap manifest for a given locale. This will replace all
|
|
79
|
+
* localized strings in the manifest with the localized version.
|
|
80
|
+
*
|
|
81
|
+
* @param snapManifest - The Snap manifest to localize.
|
|
82
|
+
* @param locale - The locale to use.
|
|
83
|
+
* @param localizationFiles - The localization files to use.
|
|
84
|
+
* @returns The localized Snap manifest.
|
|
85
|
+
*/ export function getLocalizedSnapManifest(snapManifest, locale, localizationFiles) {
|
|
86
|
+
const file = getLocalizationFile(locale, localizationFiles);
|
|
87
|
+
return LOCALIZABLE_FIELDS.reduce((manifest, field)=>{
|
|
88
|
+
const translation = translate(manifest[field], file);
|
|
89
|
+
return {
|
|
90
|
+
...manifest,
|
|
91
|
+
[field]: translation
|
|
92
|
+
};
|
|
93
|
+
}, snapManifest);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Validate the localization files for a Snap manifest.
|
|
97
|
+
*
|
|
98
|
+
* @param snapManifest - The Snap manifest to validate.
|
|
99
|
+
* @param localizationFiles - The localization files to validate.
|
|
100
|
+
* @throws If the manifest cannot be localized.
|
|
101
|
+
*/ export function validateSnapManifestLocalizations(snapManifest, localizationFiles) {
|
|
102
|
+
try {
|
|
103
|
+
// `translate` throws if the manifest cannot be localized, so we just attempt
|
|
104
|
+
// to translate the manifest using all localization files.
|
|
105
|
+
localizationFiles.filter((file)=>file.locale !== 'en').forEach((file)=>{
|
|
106
|
+
getLocalizedSnapManifest(snapManifest, file.locale, localizationFiles);
|
|
107
|
+
});
|
|
108
|
+
// The manifest must be localizable in English.
|
|
109
|
+
getLocalizedSnapManifest(snapManifest, 'en', localizationFiles);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
throw new Error(`Failed to localize Snap manifest: ${getErrorMessage(error)}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
//# sourceMappingURL=localization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/localization.ts"],"sourcesContent":["import { getErrorMessage } from '@metamask/snaps-sdk';\nimport type { Infer } from 'superstruct';\nimport {\n create,\n object,\n optional,\n record,\n string,\n StructError,\n} from 'superstruct';\n\nimport { parseJson } from './json';\nimport type { SnapManifest } from './manifest';\nimport type { VirtualFile } from './virtual-file';\n\nexport const LOCALIZABLE_FIELDS = ['description', 'proposedName'] as const;\n\nexport const LocalizationFileStruct = object({\n locale: string(),\n messages: record(\n string(),\n object({\n message: string(),\n description: optional(string()),\n }),\n ),\n});\n\nexport type LocalizationFile = Infer<typeof LocalizationFileStruct>;\n\n/**\n * Validate a list of localization files.\n *\n * @param localizationFiles - The localization files to validate.\n * @returns The validated localization files.\n * @throws If any of the files are considered invalid.\n */\nexport function getValidatedLocalizationFiles(\n localizationFiles: VirtualFile[],\n): VirtualFile<LocalizationFile>[] {\n for (const file of localizationFiles) {\n try {\n file.result = create(parseJson(file.toString()), LocalizationFileStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(\n `Failed to validate localization file \"${file.path}\": ${error.message}.`,\n );\n }\n\n if (error instanceof SyntaxError) {\n throw new Error(\n `Failed to parse localization file \"${file.path}\" as JSON.`,\n );\n }\n\n throw error;\n }\n }\n\n return localizationFiles as VirtualFile<LocalizationFile>[];\n}\n\n/**\n * Get the localization file for a given locale. If the locale is not found,\n * the English localization file will be returned.\n *\n * @param locale - The locale to use.\n * @param localizationFiles - The localization files to use.\n * @returns The localization file, or `undefined` if no localization file was\n * found.\n */\nexport function getLocalizationFile(\n locale: string,\n localizationFiles: LocalizationFile[],\n) {\n const file = localizationFiles.find(\n (localizationFile) => localizationFile.locale === locale,\n );\n\n if (!file) {\n return localizationFiles.find(\n (localizationFile) => localizationFile.locale === 'en',\n );\n }\n\n return file;\n}\n\nexport const TRANSLATION_REGEX = /\\{\\{\\s?([a-zA-Z0-9-_\\s]+)\\s?\\}\\}/gu;\n\n/**\n * Translate a string using a localization file. This will replace all instances\n * of `{{key}}` with the localized version of `key`.\n *\n * @param value - The string to translate.\n * @param file - The localization file to use, or `undefined` if no localization\n * file was found.\n * @returns The translated string.\n * @throws If the string contains a key that is not present in the localization\n * file, or if no localization file was found.\n */\nexport function translate(value: string, file: LocalizationFile | undefined) {\n const matches = value.matchAll(TRANSLATION_REGEX);\n const array = Array.from(matches);\n\n return array.reduce<string>((result, [match, key]) => {\n if (!file) {\n throw new Error(\n `Failed to translate \"${value}\": No localization file found.`,\n );\n }\n\n const translation = file.messages[key.trim()];\n if (!translation) {\n throw new Error(\n `Failed to translate \"${value}\": No translation found for \"${key.trim()}\" in \"${\n file.locale\n }\" file.`,\n );\n }\n\n return result.replace(match, translation.message);\n }, value);\n}\n\n/**\n * Get the localized Snap manifest for a given locale. This will replace all\n * localized strings in the manifest with the localized version.\n *\n * @param snapManifest - The Snap manifest to localize.\n * @param locale - The locale to use.\n * @param localizationFiles - The localization files to use.\n * @returns The localized Snap manifest.\n */\nexport function getLocalizedSnapManifest(\n snapManifest: SnapManifest,\n locale: string,\n localizationFiles: LocalizationFile[],\n) {\n const file = getLocalizationFile(locale, localizationFiles);\n\n return LOCALIZABLE_FIELDS.reduce((manifest, field) => {\n const translation = translate(manifest[field], file);\n return {\n ...manifest,\n [field]: translation,\n };\n }, snapManifest);\n}\n\n/**\n * Validate the localization files for a Snap manifest.\n *\n * @param snapManifest - The Snap manifest to validate.\n * @param localizationFiles - The localization files to validate.\n * @throws If the manifest cannot be localized.\n */\nexport function validateSnapManifestLocalizations(\n snapManifest: SnapManifest,\n localizationFiles: LocalizationFile[],\n) {\n try {\n // `translate` throws if the manifest cannot be localized, so we just attempt\n // to translate the manifest using all localization files.\n localizationFiles\n .filter((file) => file.locale !== 'en')\n .forEach((file) => {\n getLocalizedSnapManifest(snapManifest, file.locale, localizationFiles);\n });\n\n // The manifest must be localizable in English.\n getLocalizedSnapManifest(snapManifest, 'en', localizationFiles);\n } catch (error) {\n throw new Error(\n `Failed to localize Snap manifest: ${getErrorMessage(error)}`,\n );\n }\n}\n"],"names":["getErrorMessage","create","object","optional","record","string","StructError","parseJson","LOCALIZABLE_FIELDS","LocalizationFileStruct","locale","messages","message","description","getValidatedLocalizationFiles","localizationFiles","file","result","toString","error","Error","path","SyntaxError","getLocalizationFile","find","localizationFile","TRANSLATION_REGEX","translate","value","matches","matchAll","array","Array","from","reduce","match","key","translation","trim","replace","getLocalizedSnapManifest","snapManifest","manifest","field","validateSnapManifestLocalizations","filter","forEach"],"mappings":"AAAA,SAASA,eAAe,QAAQ,sBAAsB;AAEtD,SACEC,MAAM,EACNC,MAAM,EACNC,QAAQ,EACRC,MAAM,EACNC,MAAM,EACNC,WAAW,QACN,cAAc;AAErB,SAASC,SAAS,QAAQ,SAAS;AAInC,OAAO,MAAMC,qBAAqB;IAAC;IAAe;CAAe,CAAU;AAE3E,OAAO,MAAMC,yBAAyBP,OAAO;IAC3CQ,QAAQL;IACRM,UAAUP,OACRC,UACAH,OAAO;QACLU,SAASP;QACTQ,aAAaV,SAASE;IACxB;AAEJ,GAAG;AAIH;;;;;;CAMC,GACD,OAAO,SAASS,8BACdC,iBAAgC;IAEhC,KAAK,MAAMC,QAAQD,kBAAmB;QACpC,IAAI;YACFC,KAAKC,MAAM,GAAGhB,OAAOM,UAAUS,KAAKE,QAAQ,KAAKT;QACnD,EAAE,OAAOU,OAAO;YACd,IAAIA,iBAAiBb,aAAa;gBAChC,MAAM,IAAIc,MACR,CAAC,sCAAsC,EAAEJ,KAAKK,IAAI,CAAC,GAAG,EAAEF,MAAMP,OAAO,CAAC,CAAC,CAAC;YAE5E;YAEA,IAAIO,iBAAiBG,aAAa;gBAChC,MAAM,IAAI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