@metamask/utils 10.0.1 → 11.0.1
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 +11 -1
- package/dist/json.cjs +7 -10
- package/dist/json.cjs.map +1 -1
- package/dist/json.d.cts.map +1 -1
- package/dist/json.d.mts.map +1 -1
- package/dist/json.mjs +8 -11
- package/dist/json.mjs.map +1 -1
- package/dist/keyring.cjs.map +1 -1
- package/dist/keyring.d.cts +3 -1
- package/dist/keyring.d.cts.map +1 -1
- package/dist/keyring.d.mts +3 -1
- package/dist/keyring.d.mts.map +1 -1
- package/dist/keyring.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [11.0.1]
|
|
10
|
+
### Fixed
|
|
11
|
+
- Improve error message for invalid JSON values ([#224](https://github.com/MetaMask/utils/pull/224))
|
|
12
|
+
|
|
13
|
+
## [11.0.0]
|
|
14
|
+
### Changed
|
|
15
|
+
- **BREAKING:** `generateRandomMnemonic` now returns `Promise<void>` instead of `void` ([#222](https://github.com/MetaMask/utils/pull/222))
|
|
16
|
+
|
|
9
17
|
## [10.0.1]
|
|
10
18
|
### Added
|
|
11
19
|
- Add Solana CAIP namespace ([#219](https://github.com/MetaMask/utils/pull/219))
|
|
@@ -273,7 +281,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
273
281
|
### Added
|
|
274
282
|
- Initial release
|
|
275
283
|
|
|
276
|
-
[Unreleased]: https://github.com/MetaMask/utils/compare/
|
|
284
|
+
[Unreleased]: https://github.com/MetaMask/utils/compare/v11.0.1...HEAD
|
|
285
|
+
[11.0.1]: https://github.com/MetaMask/utils/compare/v11.0.0...v11.0.1
|
|
286
|
+
[11.0.0]: https://github.com/MetaMask/utils/compare/v10.0.1...v11.0.0
|
|
277
287
|
[10.0.1]: https://github.com/MetaMask/utils/compare/v10.0.0...v10.0.1
|
|
278
288
|
[10.0.0]: https://github.com/MetaMask/utils/compare/v9.3.0...v10.0.0
|
|
279
289
|
[9.3.0]: https://github.com/MetaMask/utils/compare/v9.2.1...v9.3.0
|
package/dist/json.cjs
CHANGED
|
@@ -122,16 +122,13 @@ exports.UnsafeJsonStruct = (0, superstruct_1.define)('JSON', (json) => validateJ
|
|
|
122
122
|
* This struct sanitizes the value before validating it, so that it is safe to
|
|
123
123
|
* use with untrusted input.
|
|
124
124
|
*/
|
|
125
|
-
exports.JsonStruct = (0, superstruct_1.coerce)(exports.UnsafeJsonStruct, (0, superstruct_1.any)(), (value) => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
return propValue;
|
|
133
|
-
}));
|
|
134
|
-
});
|
|
125
|
+
exports.JsonStruct = (0, superstruct_1.coerce)(exports.UnsafeJsonStruct, (0, superstruct_1.refine)((0, superstruct_1.any)(), 'JSON', (value) => (0, superstruct_1.is)(value, exports.UnsafeJsonStruct)), (value) => JSON.parse(JSON.stringify(value, (propKey, propValue) => {
|
|
126
|
+
// Strip __proto__ and constructor properties to prevent prototype pollution.
|
|
127
|
+
if (propKey === '__proto__' || propKey === 'constructor') {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
return propValue;
|
|
131
|
+
})));
|
|
135
132
|
/**
|
|
136
133
|
* Check if the given value is a valid {@link Json} value, i.e., a value that is
|
|
137
134
|
* serializable to JSON.
|
package/dist/json.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.cjs","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":";;;AAAA,uDAkB+B;AAU/B,yCAAwC;AACxC,qCAAqC;AAgDrC;;;;;;GAMG;AACI,MAAM,MAAM,GAAG,CACpB,MAAc,E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,iBAAiB,EAAE,IAAI;QACvB,eAAe,EAAE,KAAK;QACtB,UAAU,EAAE,IAAI;QAChB,GAAG,OAAO;KACX,CAAC;IAEF;;;;;;OAMG;IACH,MAAM,gBAAgB,GAAG,CAAC,EAAW,EAAmB,EAAE;QACxD,OAAO,OAAO,CACZ,CAAC,OAAO,EAAE,KAAK,QAAQ,IAAI,CAAC,eAAe,IAAI,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;YACnE,CAAC,OAAO,EAAE,KAAK,QAAQ,IAAI,CAAC,iBAAiB,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YAChE,CAAC,UAAU,IAAI,EAAE,KAAK,IAAI,CAAC,CAC9B,CAAC;IACJ,CAAC,CAAC;IAEF,OAAO,gBAAgB,CAAC;AAC1B,CAAC;AAxBD,sDAwBC","sourcesContent":["import {\n any,\n array,\n coerce,\n create,\n define,\n integer,\n is,\n literal,\n nullable,\n number,\n object as superstructObject,\n optional,\n record,\n string,\n union,\n unknown,\n Struct,\n} from '@metamask/superstruct';\nimport type {\n Context,\n Infer,\n ObjectSchema,\n Simplify,\n Optionalize,\n} from '@metamask/superstruct';\n\nimport type { AssertionErrorConstructor } from './assert';\nimport { assertStruct } from './assert';\nimport { hasProperty } from './misc';\n\n/**\n * Any JSON-compatible value.\n */\nexport type Json =\n | null\n | boolean\n | number\n | string\n | Json[]\n | { [prop: string]: Json };\n\n/**\n * A helper type to make properties with `undefined` in their type optional, but\n * not `undefined` itself.\n *\n * @example\n * ```ts\n * type Foo = ObjectOptional<{ foo: string | undefined }>;\n * // Foo is equivalent to { foo?: string }\n * ```\n */\nexport type ObjectOptional<Schema extends Record<string, unknown>> = {\n [Key in keyof Schema as Schema[Key] extends ExactOptionalGuard\n ? Key\n : never]?: Schema[Key] extends ExactOptionalGuard & infer Original\n ? Original\n : never;\n} & {\n [Key in keyof Schema as Schema[Key] extends ExactOptionalGuard\n ? never\n : Key]: Schema[Key];\n};\n\n/**\n * An object type with support for exact optionals. This is used by the `object`\n * struct. This uses the {@link ObjectOptional} helper to make properties with\n * `undefined` in their type optional, but not `undefined` itself.\n */\nexport type ObjectType<Schema extends ObjectSchema> = Simplify<\n ObjectOptional<\n Optionalize<{\n [Key in keyof Schema]: Infer<Schema[Key]>;\n }>\n >\n>;\n\n/**\n * A struct to check if the given value is a valid object, with support for\n * {@link exactOptional} types.\n *\n * @param schema - The schema of the object.\n * @returns A struct to check if the given value is an object.\n */\nexport const object = <Schema extends ObjectSchema>(\n schema: Schema,\n): Struct<ObjectType<Schema>> =>\n // The type is slightly different from a regular object struct, because we\n // want to make properties with `undefined` in their type optional, but not\n // `undefined` itself. This means that we need a type cast.\n superstructObject(schema) as unknown as Struct<ObjectType<Schema>>;\n\ndeclare const exactOptionalSymbol: unique symbol;\ntype ExactOptionalGuard = {\n _exactOptionalGuard?: typeof exactOptionalSymbol;\n};\n\n/**\n * Check the last field of a path is present.\n *\n * @param context - The context to check.\n * @param context.path - The path to check.\n * @param context.branch - The branch to check.\n * @returns Whether the last field of a path is present.\n */\nfunction hasOptional({ path, branch }: Context): boolean {\n const field = path[path.length - 1];\n return hasProperty(branch[branch.length - 2], field);\n}\n\n/**\n * A struct which allows the property of an object to be absent, or to be present\n * as long as it's valid and not set to `undefined`.\n *\n * This struct should be used in conjunction with the {@link object} from this\n * library, to get proper type inference.\n *\n * @param struct - The struct to check the value against, if present.\n * @returns A struct to check if the given value is valid, or not present.\n * @example\n * ```ts\n * const struct = object({\n * foo: exactOptional(string()),\n * bar: exactOptional(number()),\n * baz: optional(boolean()),\n * qux: unknown(),\n * });\n *\n * type Type = Infer<typeof struct>;\n * // Type is equivalent to:\n * // {\n * // foo?: string;\n * // bar?: number;\n * // baz?: boolean | undefined;\n * // qux: unknown;\n * // }\n * ```\n */\nexport function exactOptional<Type, Schema>(\n struct: Struct<Type, Schema>,\n): Struct<Type & ExactOptionalGuard, Schema> {\n return new Struct<Type & ExactOptionalGuard, Schema>({\n ...struct,\n\n type: `optional ${struct.type}`,\n validator: (value, context) =>\n !hasOptional(context) || struct.validator(value, context),\n\n refiner: (value, context) =>\n !hasOptional(context) || struct.refiner(value as Type, context),\n });\n}\n\n/**\n * Validate an unknown input to be valid JSON.\n *\n * Useful for constructing JSON structs.\n *\n * @param json - An unknown value.\n * @returns True if the value is valid JSON, otherwise false.\n */\nfunction validateJson(json: unknown): boolean {\n if (json === null || typeof json === 'boolean' || typeof json === 'string') {\n return true;\n }\n\n if (typeof json === 'number' && Number.isFinite(json)) {\n return true;\n }\n\n if (typeof json === 'object') {\n let every = true;\n if (Array.isArray(json)) {\n // Ignoring linting error since for-of is significantly slower than a normal for-loop\n // and performance is important in this specific function.\n // eslint-disable-next-line @typescript-eslint/prefer-for-of\n for (let i = 0; i < json.length; i++) {\n if (!validateJson(json[i])) {\n every = false;\n break;\n }\n }\n return every;\n }\n\n const entries = Object.entries(json);\n // Ignoring linting errors since for-of is significantly slower than a normal for-loop\n // and performance is important in this specific function.\n // eslint-disable-next-line @typescript-eslint/prefer-for-of\n for (let i = 0; i < entries.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n if (typeof entries[i]![0] !== 'string' || !validateJson(entries[i]![1])) {\n every = false;\n break;\n }\n }\n return every;\n }\n\n return false;\n}\n\n/**\n * A struct to check if the given value is a valid JSON-serializable value.\n *\n * Note that this struct is unsafe. For safe validation, use {@link JsonStruct}.\n */\nexport const UnsafeJsonStruct: Struct<Json> = define('JSON', (json) =>\n validateJson(json),\n);\n\n/**\n * A struct to check if the given value is a valid JSON-serializable value.\n *\n * This struct sanitizes the value before validating it, so that it is safe to\n * use with untrusted input.\n */\nexport const JsonStruct = coerce(UnsafeJsonStruct, any(), (value) => {\n assertStruct(value, UnsafeJsonStruct);\n return JSON.parse(\n JSON.stringify(value, (propKey, propValue) => {\n // Strip __proto__ and constructor properties to prevent prototype pollution.\n if (propKey === '__proto__' || propKey === 'constructor') {\n return undefined;\n }\n return propValue;\n }),\n );\n});\n\n/**\n * Check if the given value is a valid {@link Json} value, i.e., a value that is\n * serializable to JSON.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link Json} value.\n */\nexport function isValidJson(value: unknown): value is Json {\n try {\n getSafeJson(value);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Validate and return sanitized JSON.\n *\n * Note:\n * This function uses sanitized JsonStruct for validation\n * that applies stringify and then parse of a value provided\n * to ensure that there are no getters which can have side effects\n * that can cause security issues.\n *\n * @param value - JSON structure to be processed.\n * @returns Sanitized JSON structure.\n */\nexport function getSafeJson<Type extends Json = Json>(value: unknown): Type {\n return create(value, JsonStruct) as Type;\n}\n\n/**\n * Get the size of a JSON value in bytes. This also validates the value.\n *\n * @param value - The JSON value to get the size of.\n * @returns The size of the JSON value in bytes.\n */\nexport function getJsonSize(value: unknown): number {\n assertStruct(value, JsonStruct, 'Invalid JSON value');\n\n const json = JSON.stringify(value);\n return new TextEncoder().encode(json).byteLength;\n}\n\n/**\n * The string '2.0'.\n */\nexport const jsonrpc2 = '2.0' as const;\nexport const JsonRpcVersionStruct = literal(jsonrpc2);\n\n/**\n * A String specifying the version of the JSON-RPC protocol.\n * MUST be exactly \"2.0\".\n */\nexport type JsonRpcVersion2 = typeof jsonrpc2;\n\nexport const JsonRpcIdStruct = nullable(union([number(), string()]));\n\n/**\n * An identifier established by the Client that MUST contain a String, Number,\n * or NULL value if included. If it is not included it is assumed to be a\n * notification. The value SHOULD normally not be Null and Numbers SHOULD\n * NOT contain fractional parts.\n */\nexport type JsonRpcId = Infer<typeof JsonRpcIdStruct>;\n\nexport const JsonRpcErrorStruct = object({\n code: integer(),\n message: string(),\n data: exactOptional(JsonStruct),\n stack: exactOptional(string()),\n});\n\n/**\n * Mark a certain key of a type as optional.\n */\nexport type OptionalField<\n Type extends Record<string, unknown>,\n Key extends keyof Type,\n> = Omit<Type, Key> & Partial<Pick<Type, Key>>;\n\n/**\n * A JSON-RPC error object.\n *\n * Note that TypeScript infers `unknown | undefined` as `unknown`, meaning that\n * the `data` field is not optional. To make it optional, we use the\n * `OptionalField` helper, to explicitly make it optional.\n */\nexport type JsonRpcError = OptionalField<\n Infer<typeof JsonRpcErrorStruct>,\n 'data'\n>;\n\nexport const JsonRpcParamsStruct: Struct<Json[] | Record<string, Json>, null> =\n union([record(string(), JsonStruct), array(JsonStruct)]);\n\nexport type JsonRpcParams = Json[] | Record<string, Json>;\n\nexport const JsonRpcRequestStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n method: string(),\n params: exactOptional(JsonRpcParamsStruct),\n});\n\nexport type InferWithParams<\n Type extends Struct<any>,\n Params extends JsonRpcParams,\n> = Infer<Type> & {\n params?: Params;\n};\n\n/**\n * A JSON-RPC request object.\n */\nexport type JsonRpcRequest<Params extends JsonRpcParams = JsonRpcParams> =\n InferWithParams<typeof JsonRpcRequestStruct, Params>;\n\nexport const JsonRpcNotificationStruct = object({\n jsonrpc: JsonRpcVersionStruct,\n method: string(),\n params: exactOptional(JsonRpcParamsStruct),\n});\n\n/**\n * A JSON-RPC notification object.\n */\nexport type JsonRpcNotification<Params extends JsonRpcParams = JsonRpcParams> =\n InferWithParams<typeof JsonRpcNotificationStruct, Params>;\n\n/**\n * Check if the given value is a valid {@link JsonRpcNotification} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcNotification}\n * object.\n */\nexport function isJsonRpcNotification(\n value: unknown,\n): value is JsonRpcNotification {\n return is(value, JsonRpcNotificationStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcNotification} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcNotification} object.\n */\nexport function assertIsJsonRpcNotification(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcNotification {\n assertStruct(\n value,\n JsonRpcNotificationStruct,\n 'Invalid JSON-RPC notification',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcRequest} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcRequest} object.\n */\nexport function isJsonRpcRequest(value: unknown): value is JsonRpcRequest {\n return is(value, JsonRpcRequestStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcRequest} object.\n *\n * @param value - The JSON-RPC request or notification to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcRequest} object.\n */\nexport function assertIsJsonRpcRequest(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcRequest {\n assertStruct(\n value,\n JsonRpcRequestStruct,\n 'Invalid JSON-RPC request',\n ErrorWrapper,\n );\n}\n\nexport const PendingJsonRpcResponseStruct = superstructObject({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n result: optional(unknown()),\n error: optional(JsonRpcErrorStruct),\n});\n\n/**\n * A JSON-RPC response object that has not yet been resolved.\n */\nexport type PendingJsonRpcResponse<Result extends Json> = Omit<\n Infer<typeof PendingJsonRpcResponseStruct>,\n 'result'\n> & {\n result?: Result;\n};\n\nexport const JsonRpcSuccessStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n result: JsonStruct,\n});\n\n/**\n * A successful JSON-RPC response object.\n */\nexport type JsonRpcSuccess<Result extends Json> = Omit<\n Infer<typeof JsonRpcSuccessStruct>,\n 'result'\n> & {\n result: Result;\n};\n\nexport const JsonRpcFailureStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n error: JsonRpcErrorStruct as Struct<JsonRpcError>,\n});\n\n/**\n * A failed JSON-RPC response object.\n */\nexport type JsonRpcFailure = Infer<typeof JsonRpcFailureStruct>;\n\nexport const JsonRpcResponseStruct = union([\n JsonRpcSuccessStruct,\n JsonRpcFailureStruct,\n]);\n\n/**\n * A JSON-RPC response object. Must be checked to determine whether it's a\n * success or failure.\n *\n * @template Result - The type of the result.\n */\nexport type JsonRpcResponse<Result extends Json> =\n | JsonRpcSuccess<Result>\n | JsonRpcFailure;\n\n/**\n * Type guard to check whether specified JSON-RPC response is a\n * {@link PendingJsonRpcResponse}.\n *\n * @param response - The JSON-RPC response to check.\n * @returns Whether the specified JSON-RPC response is pending.\n */\nexport function isPendingJsonRpcResponse(\n response: unknown,\n): response is PendingJsonRpcResponse<Json> {\n return is(response, PendingJsonRpcResponseStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link PendingJsonRpcResponse} object.\n *\n * @param response - The JSON-RPC response to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link PendingJsonRpcResponse}\n * object.\n */\nexport function assertIsPendingJsonRpcResponse(\n response: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts response is PendingJsonRpcResponse<Json> {\n assertStruct(\n response,\n PendingJsonRpcResponseStruct,\n 'Invalid pending JSON-RPC response',\n ErrorWrapper,\n );\n}\n\n/**\n * Type guard to check if a value is a {@link JsonRpcResponse}.\n *\n * @param response - The object to check.\n * @returns Whether the object is a JsonRpcResponse.\n */\nexport function isJsonRpcResponse(\n response: unknown,\n): response is JsonRpcResponse<Json> {\n return is(response, JsonRpcResponseStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcResponse} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcResponse} object.\n */\nexport function assertIsJsonRpcResponse(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcResponse<Json> {\n assertStruct(\n value,\n JsonRpcResponseStruct,\n 'Invalid JSON-RPC response',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcSuccess} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcSuccess} object.\n */\nexport function isJsonRpcSuccess(\n value: unknown,\n): value is JsonRpcSuccess<Json> {\n return is(value, JsonRpcSuccessStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcSuccess} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcSuccess} object.\n */\nexport function assertIsJsonRpcSuccess(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcSuccess<Json> {\n assertStruct(\n value,\n JsonRpcSuccessStruct,\n 'Invalid JSON-RPC success response',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcFailure} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcFailure} object.\n */\nexport function isJsonRpcFailure(value: unknown): value is JsonRpcFailure {\n return is(value, JsonRpcFailureStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcFailure} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcFailure} object.\n */\nexport function assertIsJsonRpcFailure(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcFailure {\n assertStruct(\n value,\n JsonRpcFailureStruct,\n 'Invalid JSON-RPC failure response',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcError} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcError} object.\n */\nexport function isJsonRpcError(value: unknown): value is JsonRpcError {\n return is(value, JsonRpcErrorStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcError} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcError} object.\n */\nexport function assertIsJsonRpcError(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcError {\n assertStruct(\n value,\n JsonRpcErrorStruct,\n 'Invalid JSON-RPC error',\n ErrorWrapper,\n );\n}\n\ntype JsonRpcValidatorOptions = {\n permitEmptyString?: boolean;\n permitFractions?: boolean;\n permitNull?: boolean;\n};\n\n/**\n * Gets a function for validating JSON-RPC request / response `id` values.\n *\n * By manipulating the options of this factory, you can control the behavior\n * of the resulting validator for some edge cases. This is useful because e.g.\n * `null` should sometimes but not always be permitted.\n *\n * Note that the empty string (`''`) is always permitted by the JSON-RPC\n * specification, but that kind of sucks and you may want to forbid it in some\n * instances anyway.\n *\n * For more details, see the\n * [JSON-RPC Specification](https://www.jsonrpc.org/specification).\n *\n * @param options - An options object.\n * @param options.permitEmptyString - Whether the empty string (i.e. `''`)\n * should be treated as a valid ID. Default: `true`\n * @param options.permitFractions - Whether fractional numbers (e.g. `1.2`)\n * should be treated as valid IDs. Default: `false`\n * @param options.permitNull - Whether `null` should be treated as a valid ID.\n * Default: `true`\n * @returns The JSON-RPC ID validator function.\n */\nexport function getJsonRpcIdValidator(options?: JsonRpcValidatorOptions) {\n const { permitEmptyString, permitFractions, permitNull } = {\n permitEmptyString: true,\n permitFractions: false,\n permitNull: true,\n ...options,\n };\n\n /**\n * Type guard for {@link JsonRpcId}.\n *\n * @param id - The JSON-RPC ID value to check.\n * @returns Whether the given ID is valid per the options given to the\n * factory.\n */\n const isValidJsonRpcId = (id: unknown): id is JsonRpcId => {\n return Boolean(\n (typeof id === 'number' && (permitFractions || Number.isInteger(id))) ||\n (typeof id === 'string' && (permitEmptyString || id.length > 0)) ||\n (permitNull && id === null),\n );\n };\n\n return isValidJsonRpcId;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"json.cjs","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":";;;AAAA,uDAmB+B;AAU/B,yCAAwC;AACxC,qCAAqC;AAgDrC;;;;;;GAMG;AACI,MAAM,MAAM,GAAG,CACpB,MAAc,EACc,EAAE;AAC9B,0EAA0E;AAC1E,2EAA2E;AAC3E,2DAA2D;AAC3D,IAAA,oBAAiB,EAAC,MAAM,CAA0C,CAAC;AANxD,QAAA,MAAM,UAMkD;AAOrE;;;;;;;GAOG;AACH,SAAS,WAAW,CAAC,EAAE,IAAI,EAAE,MAAM,EAAW;IAC5C,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACpC,OAAO,IAAA,kBAAW,EAAC,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AACvD,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,SAAgB,aAAa,CAC3B,MAA4B;IAE5B,OAAO,IAAI,oBAAM,CAAoC;QACnD,GAAG,MAAM;QAET,IAAI,EAAE,YAAY,MAAM,CAAC,IAAI,EAAE;QAC/B,SAAS,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,CAC5B,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC;QAE3D,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,CAC1B,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAa,EAAE,OAAO,CAAC;KAClE,CAAC,CAAC;AACL,CAAC;AAbD,sCAaC;AAED;;;;;;;GAOG;AACH,SAAS,YAAY,CAAC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;AAED;;;;;;;GAOG;AACH,SAAgB,oBAAoB,CAClC,KAAc;AACd,gEAAgE;AAChE,YAAwC;IAExC,IAAA,qBAAY,EACV,KAAK,EACL,0BAAkB,EAClB,wBAAwB,EACxB,YAAY,CACb,CAAC;AACJ,CAAC;AAXD,oDAWC;AAQD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,SAAgB,qBAAqB,CAAC,OAAiC;IACrE,MAAM,EAAE,iBAAiB,EAAE,eAAe,EAAE,UAAU,EAAE,GAAG;QACzD,iBAAiB,EAAE,IAAI;QACvB,eAAe,EAAE,KAAK;QACtB,UAAU,EAAE,IAAI;QAChB,GAAG,OAAO;KACX,CAAC;IAEF;;;;;;OAMG;IACH,MAAM,gBAAgB,GAAG,CAAC,EAAW,EAAmB,EAAE;QACxD,OAAO,OAAO,CACZ,CAAC,OAAO,EAAE,KAAK,QAAQ,IAAI,CAAC,eAAe,IAAI,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;YACnE,CAAC,OAAO,EAAE,KAAK,QAAQ,IAAI,CAAC,iBAAiB,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YAChE,CAAC,UAAU,IAAI,EAAE,KAAK,IAAI,CAAC,CAC9B,CAAC;IACJ,CAAC,CAAC;IAEF,OAAO,gBAAgB,CAAC;AAC1B,CAAC;AAxBD,sDAwBC","sourcesContent":["import {\n any,\n array,\n coerce,\n create,\n define,\n integer,\n is,\n literal,\n nullable,\n number,\n object as superstructObject,\n optional,\n record,\n string,\n union,\n unknown,\n Struct,\n refine,\n} from '@metamask/superstruct';\nimport type {\n Context,\n Infer,\n ObjectSchema,\n Simplify,\n Optionalize,\n} from '@metamask/superstruct';\n\nimport type { AssertionErrorConstructor } from './assert';\nimport { assertStruct } from './assert';\nimport { hasProperty } from './misc';\n\n/**\n * Any JSON-compatible value.\n */\nexport type Json =\n | null\n | boolean\n | number\n | string\n | Json[]\n | { [prop: string]: Json };\n\n/**\n * A helper type to make properties with `undefined` in their type optional, but\n * not `undefined` itself.\n *\n * @example\n * ```ts\n * type Foo = ObjectOptional<{ foo: string | undefined }>;\n * // Foo is equivalent to { foo?: string }\n * ```\n */\nexport type ObjectOptional<Schema extends Record<string, unknown>> = {\n [Key in keyof Schema as Schema[Key] extends ExactOptionalGuard\n ? Key\n : never]?: Schema[Key] extends ExactOptionalGuard & infer Original\n ? Original\n : never;\n} & {\n [Key in keyof Schema as Schema[Key] extends ExactOptionalGuard\n ? never\n : Key]: Schema[Key];\n};\n\n/**\n * An object type with support for exact optionals. This is used by the `object`\n * struct. This uses the {@link ObjectOptional} helper to make properties with\n * `undefined` in their type optional, but not `undefined` itself.\n */\nexport type ObjectType<Schema extends ObjectSchema> = Simplify<\n ObjectOptional<\n Optionalize<{\n [Key in keyof Schema]: Infer<Schema[Key]>;\n }>\n >\n>;\n\n/**\n * A struct to check if the given value is a valid object, with support for\n * {@link exactOptional} types.\n *\n * @param schema - The schema of the object.\n * @returns A struct to check if the given value is an object.\n */\nexport const object = <Schema extends ObjectSchema>(\n schema: Schema,\n): Struct<ObjectType<Schema>> =>\n // The type is slightly different from a regular object struct, because we\n // want to make properties with `undefined` in their type optional, but not\n // `undefined` itself. This means that we need a type cast.\n superstructObject(schema) as unknown as Struct<ObjectType<Schema>>;\n\ndeclare const exactOptionalSymbol: unique symbol;\ntype ExactOptionalGuard = {\n _exactOptionalGuard?: typeof exactOptionalSymbol;\n};\n\n/**\n * Check the last field of a path is present.\n *\n * @param context - The context to check.\n * @param context.path - The path to check.\n * @param context.branch - The branch to check.\n * @returns Whether the last field of a path is present.\n */\nfunction hasOptional({ path, branch }: Context): boolean {\n const field = path[path.length - 1];\n return hasProperty(branch[branch.length - 2], field);\n}\n\n/**\n * A struct which allows the property of an object to be absent, or to be present\n * as long as it's valid and not set to `undefined`.\n *\n * This struct should be used in conjunction with the {@link object} from this\n * library, to get proper type inference.\n *\n * @param struct - The struct to check the value against, if present.\n * @returns A struct to check if the given value is valid, or not present.\n * @example\n * ```ts\n * const struct = object({\n * foo: exactOptional(string()),\n * bar: exactOptional(number()),\n * baz: optional(boolean()),\n * qux: unknown(),\n * });\n *\n * type Type = Infer<typeof struct>;\n * // Type is equivalent to:\n * // {\n * // foo?: string;\n * // bar?: number;\n * // baz?: boolean | undefined;\n * // qux: unknown;\n * // }\n * ```\n */\nexport function exactOptional<Type, Schema>(\n struct: Struct<Type, Schema>,\n): Struct<Type & ExactOptionalGuard, Schema> {\n return new Struct<Type & ExactOptionalGuard, Schema>({\n ...struct,\n\n type: `optional ${struct.type}`,\n validator: (value, context) =>\n !hasOptional(context) || struct.validator(value, context),\n\n refiner: (value, context) =>\n !hasOptional(context) || struct.refiner(value as Type, context),\n });\n}\n\n/**\n * Validate an unknown input to be valid JSON.\n *\n * Useful for constructing JSON structs.\n *\n * @param json - An unknown value.\n * @returns True if the value is valid JSON, otherwise false.\n */\nfunction validateJson(json: unknown): boolean {\n if (json === null || typeof json === 'boolean' || typeof json === 'string') {\n return true;\n }\n\n if (typeof json === 'number' && Number.isFinite(json)) {\n return true;\n }\n\n if (typeof json === 'object') {\n let every = true;\n if (Array.isArray(json)) {\n // Ignoring linting error since for-of is significantly slower than a normal for-loop\n // and performance is important in this specific function.\n // eslint-disable-next-line @typescript-eslint/prefer-for-of\n for (let i = 0; i < json.length; i++) {\n if (!validateJson(json[i])) {\n every = false;\n break;\n }\n }\n return every;\n }\n\n const entries = Object.entries(json);\n // Ignoring linting errors since for-of is significantly slower than a normal for-loop\n // and performance is important in this specific function.\n // eslint-disable-next-line @typescript-eslint/prefer-for-of\n for (let i = 0; i < entries.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n if (typeof entries[i]![0] !== 'string' || !validateJson(entries[i]![1])) {\n every = false;\n break;\n }\n }\n return every;\n }\n\n return false;\n}\n\n/**\n * A struct to check if the given value is a valid JSON-serializable value.\n *\n * Note that this struct is unsafe. For safe validation, use {@link JsonStruct}.\n */\nexport const UnsafeJsonStruct: Struct<Json> = define('JSON', (json) =>\n validateJson(json),\n);\n\n/**\n * A struct to check if the given value is a valid JSON-serializable value.\n *\n * This struct sanitizes the value before validating it, so that it is safe to\n * use with untrusted input.\n */\nexport const JsonStruct = coerce(\n UnsafeJsonStruct,\n refine(any(), 'JSON', (value) => is(value, UnsafeJsonStruct)),\n (value) =>\n JSON.parse(\n JSON.stringify(value, (propKey, propValue) => {\n // Strip __proto__ and constructor properties to prevent prototype pollution.\n if (propKey === '__proto__' || propKey === 'constructor') {\n return undefined;\n }\n return propValue;\n }),\n ),\n);\n\n/**\n * Check if the given value is a valid {@link Json} value, i.e., a value that is\n * serializable to JSON.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link Json} value.\n */\nexport function isValidJson(value: unknown): value is Json {\n try {\n getSafeJson(value);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Validate and return sanitized JSON.\n *\n * Note:\n * This function uses sanitized JsonStruct for validation\n * that applies stringify and then parse of a value provided\n * to ensure that there are no getters which can have side effects\n * that can cause security issues.\n *\n * @param value - JSON structure to be processed.\n * @returns Sanitized JSON structure.\n */\nexport function getSafeJson<Type extends Json = Json>(value: unknown): Type {\n return create(value, JsonStruct) as Type;\n}\n\n/**\n * Get the size of a JSON value in bytes. This also validates the value.\n *\n * @param value - The JSON value to get the size of.\n * @returns The size of the JSON value in bytes.\n */\nexport function getJsonSize(value: unknown): number {\n assertStruct(value, JsonStruct, 'Invalid JSON value');\n\n const json = JSON.stringify(value);\n return new TextEncoder().encode(json).byteLength;\n}\n\n/**\n * The string '2.0'.\n */\nexport const jsonrpc2 = '2.0' as const;\nexport const JsonRpcVersionStruct = literal(jsonrpc2);\n\n/**\n * A String specifying the version of the JSON-RPC protocol.\n * MUST be exactly \"2.0\".\n */\nexport type JsonRpcVersion2 = typeof jsonrpc2;\n\nexport const JsonRpcIdStruct = nullable(union([number(), string()]));\n\n/**\n * An identifier established by the Client that MUST contain a String, Number,\n * or NULL value if included. If it is not included it is assumed to be a\n * notification. The value SHOULD normally not be Null and Numbers SHOULD\n * NOT contain fractional parts.\n */\nexport type JsonRpcId = Infer<typeof JsonRpcIdStruct>;\n\nexport const JsonRpcErrorStruct = object({\n code: integer(),\n message: string(),\n data: exactOptional(JsonStruct),\n stack: exactOptional(string()),\n});\n\n/**\n * Mark a certain key of a type as optional.\n */\nexport type OptionalField<\n Type extends Record<string, unknown>,\n Key extends keyof Type,\n> = Omit<Type, Key> & Partial<Pick<Type, Key>>;\n\n/**\n * A JSON-RPC error object.\n *\n * Note that TypeScript infers `unknown | undefined` as `unknown`, meaning that\n * the `data` field is not optional. To make it optional, we use the\n * `OptionalField` helper, to explicitly make it optional.\n */\nexport type JsonRpcError = OptionalField<\n Infer<typeof JsonRpcErrorStruct>,\n 'data'\n>;\n\nexport const JsonRpcParamsStruct: Struct<Json[] | Record<string, Json>, null> =\n union([record(string(), JsonStruct), array(JsonStruct)]);\n\nexport type JsonRpcParams = Json[] | Record<string, Json>;\n\nexport const JsonRpcRequestStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n method: string(),\n params: exactOptional(JsonRpcParamsStruct),\n});\n\nexport type InferWithParams<\n Type extends Struct<any>,\n Params extends JsonRpcParams,\n> = Infer<Type> & {\n params?: Params;\n};\n\n/**\n * A JSON-RPC request object.\n */\nexport type JsonRpcRequest<Params extends JsonRpcParams = JsonRpcParams> =\n InferWithParams<typeof JsonRpcRequestStruct, Params>;\n\nexport const JsonRpcNotificationStruct = object({\n jsonrpc: JsonRpcVersionStruct,\n method: string(),\n params: exactOptional(JsonRpcParamsStruct),\n});\n\n/**\n * A JSON-RPC notification object.\n */\nexport type JsonRpcNotification<Params extends JsonRpcParams = JsonRpcParams> =\n InferWithParams<typeof JsonRpcNotificationStruct, Params>;\n\n/**\n * Check if the given value is a valid {@link JsonRpcNotification} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcNotification}\n * object.\n */\nexport function isJsonRpcNotification(\n value: unknown,\n): value is JsonRpcNotification {\n return is(value, JsonRpcNotificationStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcNotification} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcNotification} object.\n */\nexport function assertIsJsonRpcNotification(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcNotification {\n assertStruct(\n value,\n JsonRpcNotificationStruct,\n 'Invalid JSON-RPC notification',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcRequest} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcRequest} object.\n */\nexport function isJsonRpcRequest(value: unknown): value is JsonRpcRequest {\n return is(value, JsonRpcRequestStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcRequest} object.\n *\n * @param value - The JSON-RPC request or notification to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcRequest} object.\n */\nexport function assertIsJsonRpcRequest(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcRequest {\n assertStruct(\n value,\n JsonRpcRequestStruct,\n 'Invalid JSON-RPC request',\n ErrorWrapper,\n );\n}\n\nexport const PendingJsonRpcResponseStruct = superstructObject({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n result: optional(unknown()),\n error: optional(JsonRpcErrorStruct),\n});\n\n/**\n * A JSON-RPC response object that has not yet been resolved.\n */\nexport type PendingJsonRpcResponse<Result extends Json> = Omit<\n Infer<typeof PendingJsonRpcResponseStruct>,\n 'result'\n> & {\n result?: Result;\n};\n\nexport const JsonRpcSuccessStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n result: JsonStruct,\n});\n\n/**\n * A successful JSON-RPC response object.\n */\nexport type JsonRpcSuccess<Result extends Json> = Omit<\n Infer<typeof JsonRpcSuccessStruct>,\n 'result'\n> & {\n result: Result;\n};\n\nexport const JsonRpcFailureStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n error: JsonRpcErrorStruct as Struct<JsonRpcError>,\n});\n\n/**\n * A failed JSON-RPC response object.\n */\nexport type JsonRpcFailure = Infer<typeof JsonRpcFailureStruct>;\n\nexport const JsonRpcResponseStruct = union([\n JsonRpcSuccessStruct,\n JsonRpcFailureStruct,\n]);\n\n/**\n * A JSON-RPC response object. Must be checked to determine whether it's a\n * success or failure.\n *\n * @template Result - The type of the result.\n */\nexport type JsonRpcResponse<Result extends Json> =\n | JsonRpcSuccess<Result>\n | JsonRpcFailure;\n\n/**\n * Type guard to check whether specified JSON-RPC response is a\n * {@link PendingJsonRpcResponse}.\n *\n * @param response - The JSON-RPC response to check.\n * @returns Whether the specified JSON-RPC response is pending.\n */\nexport function isPendingJsonRpcResponse(\n response: unknown,\n): response is PendingJsonRpcResponse<Json> {\n return is(response, PendingJsonRpcResponseStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link PendingJsonRpcResponse} object.\n *\n * @param response - The JSON-RPC response to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link PendingJsonRpcResponse}\n * object.\n */\nexport function assertIsPendingJsonRpcResponse(\n response: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts response is PendingJsonRpcResponse<Json> {\n assertStruct(\n response,\n PendingJsonRpcResponseStruct,\n 'Invalid pending JSON-RPC response',\n ErrorWrapper,\n );\n}\n\n/**\n * Type guard to check if a value is a {@link JsonRpcResponse}.\n *\n * @param response - The object to check.\n * @returns Whether the object is a JsonRpcResponse.\n */\nexport function isJsonRpcResponse(\n response: unknown,\n): response is JsonRpcResponse<Json> {\n return is(response, JsonRpcResponseStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcResponse} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcResponse} object.\n */\nexport function assertIsJsonRpcResponse(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcResponse<Json> {\n assertStruct(\n value,\n JsonRpcResponseStruct,\n 'Invalid JSON-RPC response',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcSuccess} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcSuccess} object.\n */\nexport function isJsonRpcSuccess(\n value: unknown,\n): value is JsonRpcSuccess<Json> {\n return is(value, JsonRpcSuccessStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcSuccess} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcSuccess} object.\n */\nexport function assertIsJsonRpcSuccess(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcSuccess<Json> {\n assertStruct(\n value,\n JsonRpcSuccessStruct,\n 'Invalid JSON-RPC success response',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcFailure} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcFailure} object.\n */\nexport function isJsonRpcFailure(value: unknown): value is JsonRpcFailure {\n return is(value, JsonRpcFailureStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcFailure} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcFailure} object.\n */\nexport function assertIsJsonRpcFailure(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcFailure {\n assertStruct(\n value,\n JsonRpcFailureStruct,\n 'Invalid JSON-RPC failure response',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcError} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcError} object.\n */\nexport function isJsonRpcError(value: unknown): value is JsonRpcError {\n return is(value, JsonRpcErrorStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcError} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcError} object.\n */\nexport function assertIsJsonRpcError(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcError {\n assertStruct(\n value,\n JsonRpcErrorStruct,\n 'Invalid JSON-RPC error',\n ErrorWrapper,\n );\n}\n\ntype JsonRpcValidatorOptions = {\n permitEmptyString?: boolean;\n permitFractions?: boolean;\n permitNull?: boolean;\n};\n\n/**\n * Gets a function for validating JSON-RPC request / response `id` values.\n *\n * By manipulating the options of this factory, you can control the behavior\n * of the resulting validator for some edge cases. This is useful because e.g.\n * `null` should sometimes but not always be permitted.\n *\n * Note that the empty string (`''`) is always permitted by the JSON-RPC\n * specification, but that kind of sucks and you may want to forbid it in some\n * instances anyway.\n *\n * For more details, see the\n * [JSON-RPC Specification](https://www.jsonrpc.org/specification).\n *\n * @param options - An options object.\n * @param options.permitEmptyString - Whether the empty string (i.e. `''`)\n * should be treated as a valid ID. Default: `true`\n * @param options.permitFractions - Whether fractional numbers (e.g. `1.2`)\n * should be treated as valid IDs. Default: `false`\n * @param options.permitNull - Whether `null` should be treated as a valid ID.\n * Default: `true`\n * @returns The JSON-RPC ID validator function.\n */\nexport function getJsonRpcIdValidator(options?: JsonRpcValidatorOptions) {\n const { permitEmptyString, permitFractions, permitNull } = {\n permitEmptyString: true,\n permitFractions: false,\n permitNull: true,\n ...options,\n };\n\n /**\n * Type guard for {@link JsonRpcId}.\n *\n * @param id - The JSON-RPC ID value to check.\n * @returns Whether the given ID is valid per the options given to the\n * factory.\n */\n const isValidJsonRpcId = (id: unknown): id is JsonRpcId => {\n return Boolean(\n (typeof id === 'number' && (permitFractions || Number.isInteger(id))) ||\n (typeof id === 'string' && (permitEmptyString || id.length > 0)) ||\n (permitNull && id === null),\n );\n };\n\n return isValidJsonRpcId;\n}\n"]}
|
package/dist/json.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.d.cts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAiBL,MAAM,
|
|
1
|
+
{"version":3,"file":"json.d.cts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAiBL,MAAM,EAEP,8BAA8B;AAC/B,OAAO,KAAK,EAEV,KAAK,EACL,YAAY,EACZ,QAAQ,EACR,WAAW,EACZ,8BAA8B;AAE/B,OAAO,KAAK,EAAE,yBAAyB,EAAE,qBAAiB;AAI1D;;GAEG;AACH,MAAM,MAAM,IAAI,GACZ,IAAI,GACJ,OAAO,GACP,MAAM,GACN,MAAM,GACN,IAAI,EAAE,GACN;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC;AAE7B;;;;;;;;;GASG;AACH,MAAM,MAAM,cAAc,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI;KAClE,GAAG,IAAI,MAAM,MAAM,IAAI,MAAM,CAAC,GAAG,CAAC,SAAS,kBAAkB,GAC1D,GAAG,GACH,KAAK,CAAC,CAAC,EAAE,MAAM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C,KAAK,IAAI,cAAc,CAOjC;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,YAAY,CAEpE;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,OAAO,EAEd,YAAY,CAAC,EAAE,yBAAyB,GACvC,OAAO,CAAC,KAAK,IAAI,YAAY,CAO/B;AAED,KAAK,uBAAuB,GAAG;IAC7B,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,CAAC,EAAE,uBAAuB,QAevC,OAAO,kCAStC"}
|
package/dist/json.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.d.mts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAiBL,MAAM,
|
|
1
|
+
{"version":3,"file":"json.d.mts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAiBL,MAAM,EAEP,8BAA8B;AAC/B,OAAO,KAAK,EAEV,KAAK,EACL,YAAY,EACZ,QAAQ,EACR,WAAW,EACZ,8BAA8B;AAE/B,OAAO,KAAK,EAAE,yBAAyB,EAAE,qBAAiB;AAI1D;;GAEG;AACH,MAAM,MAAM,IAAI,GACZ,IAAI,GACJ,OAAO,GACP,MAAM,GACN,MAAM,GACN,IAAI,EAAE,GACN;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC;AAE7B;;;;;;;;;GASG;AACH,MAAM,MAAM,cAAc,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI;KAClE,GAAG,IAAI,MAAM,MAAM,IAAI,MAAM,CAAC,GAAG,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,KAAK,EAAE,OAAO,EAEd,YAAY,CAAC,EAAE,yBAAyB,GACvC,OAAO,CAAC,KAAK,IAAI,cAAc,CAOjC;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,YAAY,CAEpE;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,OAAO,EAEd,YAAY,CAAC,EAAE,yBAAyB,GACvC,OAAO,CAAC,KAAK,IAAI,YAAY,CAO/B;AAED,KAAK,uBAAuB,GAAG;IAC7B,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,CAAC,EAAE,uBAAuB,QAevC,OAAO,kCAStC"}
|
package/dist/json.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { any, array, coerce, create, define, integer, is, literal, nullable, number, object as superstructObject, optional, record, string, union, unknown, Struct } from "@metamask/superstruct";
|
|
1
|
+
import { any, array, coerce, create, define, integer, is, literal, nullable, number, object as superstructObject, optional, record, string, union, unknown, Struct, refine } from "@metamask/superstruct";
|
|
2
2
|
import { assertStruct } from "./assert.mjs";
|
|
3
3
|
import { hasProperty } from "./misc.mjs";
|
|
4
4
|
/**
|
|
@@ -117,16 +117,13 @@ export const UnsafeJsonStruct = define('JSON', (json) => validateJson(json));
|
|
|
117
117
|
* This struct sanitizes the value before validating it, so that it is safe to
|
|
118
118
|
* use with untrusted input.
|
|
119
119
|
*/
|
|
120
|
-
export const JsonStruct = coerce(UnsafeJsonStruct, any(), (value) => {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return propValue;
|
|
128
|
-
}));
|
|
129
|
-
});
|
|
120
|
+
export const JsonStruct = coerce(UnsafeJsonStruct, refine(any(), 'JSON', (value) => is(value, UnsafeJsonStruct)), (value) => JSON.parse(JSON.stringify(value, (propKey, propValue) => {
|
|
121
|
+
// Strip __proto__ and constructor properties to prevent prototype pollution.
|
|
122
|
+
if (propKey === '__proto__' || propKey === 'constructor') {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
return propValue;
|
|
126
|
+
})));
|
|
130
127
|
/**
|
|
131
128
|
* Check if the given value is a valid {@link Json} value, i.e., a value that is
|
|
132
129
|
* serializable to JSON.
|
package/dist/json.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.mjs","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,KAAK,EACL,MAAM,EACN,MAAM,EACN,MAAM,EACN,OAAO,EACP,EAAE,EACF,OAAO,EACP,QAAQ,EACR,MAAM,EACN,MAAM,IAAI,iBAAiB,EAC3B,QAAQ,EACR,MAAM,EACN,MAAM,EACN,KAAK,EACL,OAAO,EACP,MAAM,EACP,8BAA8B;AAU/B,OAAO,EAAE,YAAY,EAAE,qBAAiB;AACxC,OAAO,EAAE,WAAW,EAAE,mBAAe;AAgDrC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CACpB,MAAc,EACc,EAAE;AAC9B,0EAA0E;AAC1E,2EAA2E;AAC3E,2DAA2D;AAC3D,iBAAiB,CAAC,MAAM,CAA0C,CAAC;AAOrE;;;;;;;GAOG;AACH,SAAS,WAAW,CAAC,EAAE,IAAI,EAAE,MAAM,EAAW;IAC5C,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACpC,OAAO,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AACvD,CAAC;AAED;;;;;;;;;;;;;;;;;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EACL,oBAAoB,EACpB,mCAAmC,EACnC,YAAY,CACb,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,OAAO,EAAE,CAAC,KAAK,EAAE,kBAAkB,CAAC,CAAC;AACvC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAClC,KAAc;AACd,gEAAgE;AAChE,YAAwC;IAExC,YAAY,CACV,KAAK,EACL,kBAAkB,EAClB,wBAAwB,EACxB,YAAY,CACb,CAAC;AACJ,CAAC;AAQD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,qBAAqB,CAAC,OAAiC;IACrE,MAAM,EAAE,iBAAiB,EAAE,eAAe,EAAE,UAAU,EAAE,GAAG;QACzD,iBAAiB,EAAE,IAAI;QACvB,eAAe,EAAE,KAAK;QACtB,UAAU,EAAE,IAAI;QAChB,GAAG,OAAO;KACX,CAAC;IAEF;;;;;;OAMG;IACH,MAAM,gBAAgB,GAAG,CAAC,EAAW,EAAmB,EAAE;QACxD,OAAO,OAAO,CACZ,CAAC,OAAO,EAAE,KAAK,QAAQ,IAAI,CAAC,eAAe,IAAI,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;YACnE,CAAC,OAAO,EAAE,KAAK,QAAQ,IAAI,CAAC,iBAAiB,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YAChE,CAAC,UAAU,IAAI,EAAE,KAAK,IAAI,CAAC,CAC9B,CAAC;IACJ,CAAC,CAAC;IAEF,OAAO,gBAAgB,CAAC;AAC1B,CAAC","sourcesContent":["import {\n any,\n array,\n coerce,\n create,\n define,\n integer,\n is,\n literal,\n nullable,\n number,\n object as superstructObject,\n optional,\n record,\n string,\n union,\n unknown,\n Struct,\n} from '@metamask/superstruct';\nimport type {\n Context,\n Infer,\n ObjectSchema,\n Simplify,\n Optionalize,\n} from '@metamask/superstruct';\n\nimport type { AssertionErrorConstructor } from './assert';\nimport { assertStruct } from './assert';\nimport { hasProperty } from './misc';\n\n/**\n * Any JSON-compatible value.\n */\nexport type Json =\n | null\n | boolean\n | number\n | string\n | Json[]\n | { [prop: string]: Json };\n\n/**\n * A helper type to make properties with `undefined` in their type optional, but\n * not `undefined` itself.\n *\n * @example\n * ```ts\n * type Foo = ObjectOptional<{ foo: string | undefined }>;\n * // Foo is equivalent to { foo?: string }\n * ```\n */\nexport type ObjectOptional<Schema extends Record<string, unknown>> = {\n [Key in keyof Schema as Schema[Key] extends ExactOptionalGuard\n ? Key\n : never]?: Schema[Key] extends ExactOptionalGuard & infer Original\n ? Original\n : never;\n} & {\n [Key in keyof Schema as Schema[Key] extends ExactOptionalGuard\n ? never\n : Key]: Schema[Key];\n};\n\n/**\n * An object type with support for exact optionals. This is used by the `object`\n * struct. This uses the {@link ObjectOptional} helper to make properties with\n * `undefined` in their type optional, but not `undefined` itself.\n */\nexport type ObjectType<Schema extends ObjectSchema> = Simplify<\n ObjectOptional<\n Optionalize<{\n [Key in keyof Schema]: Infer<Schema[Key]>;\n }>\n >\n>;\n\n/**\n * A struct to check if the given value is a valid object, with support for\n * {@link exactOptional} types.\n *\n * @param schema - The schema of the object.\n * @returns A struct to check if the given value is an object.\n */\nexport const object = <Schema extends ObjectSchema>(\n schema: Schema,\n): Struct<ObjectType<Schema>> =>\n // The type is slightly different from a regular object struct, because we\n // want to make properties with `undefined` in their type optional, but not\n // `undefined` itself. This means that we need a type cast.\n superstructObject(schema) as unknown as Struct<ObjectType<Schema>>;\n\ndeclare const exactOptionalSymbol: unique symbol;\ntype ExactOptionalGuard = {\n _exactOptionalGuard?: typeof exactOptionalSymbol;\n};\n\n/**\n * Check the last field of a path is present.\n *\n * @param context - The context to check.\n * @param context.path - The path to check.\n * @param context.branch - The branch to check.\n * @returns Whether the last field of a path is present.\n */\nfunction hasOptional({ path, branch }: Context): boolean {\n const field = path[path.length - 1];\n return hasProperty(branch[branch.length - 2], field);\n}\n\n/**\n * A struct which allows the property of an object to be absent, or to be present\n * as long as it's valid and not set to `undefined`.\n *\n * This struct should be used in conjunction with the {@link object} from this\n * library, to get proper type inference.\n *\n * @param struct - The struct to check the value against, if present.\n * @returns A struct to check if the given value is valid, or not present.\n * @example\n * ```ts\n * const struct = object({\n * foo: exactOptional(string()),\n * bar: exactOptional(number()),\n * baz: optional(boolean()),\n * qux: unknown(),\n * });\n *\n * type Type = Infer<typeof struct>;\n * // Type is equivalent to:\n * // {\n * // foo?: string;\n * // bar?: number;\n * // baz?: boolean | undefined;\n * // qux: unknown;\n * // }\n * ```\n */\nexport function exactOptional<Type, Schema>(\n struct: Struct<Type, Schema>,\n): Struct<Type & ExactOptionalGuard, Schema> {\n return new Struct<Type & ExactOptionalGuard, Schema>({\n ...struct,\n\n type: `optional ${struct.type}`,\n validator: (value, context) =>\n !hasOptional(context) || struct.validator(value, context),\n\n refiner: (value, context) =>\n !hasOptional(context) || struct.refiner(value as Type, context),\n });\n}\n\n/**\n * Validate an unknown input to be valid JSON.\n *\n * Useful for constructing JSON structs.\n *\n * @param json - An unknown value.\n * @returns True if the value is valid JSON, otherwise false.\n */\nfunction validateJson(json: unknown): boolean {\n if (json === null || typeof json === 'boolean' || typeof json === 'string') {\n return true;\n }\n\n if (typeof json === 'number' && Number.isFinite(json)) {\n return true;\n }\n\n if (typeof json === 'object') {\n let every = true;\n if (Array.isArray(json)) {\n // Ignoring linting error since for-of is significantly slower than a normal for-loop\n // and performance is important in this specific function.\n // eslint-disable-next-line @typescript-eslint/prefer-for-of\n for (let i = 0; i < json.length; i++) {\n if (!validateJson(json[i])) {\n every = false;\n break;\n }\n }\n return every;\n }\n\n const entries = Object.entries(json);\n // Ignoring linting errors since for-of is significantly slower than a normal for-loop\n // and performance is important in this specific function.\n // eslint-disable-next-line @typescript-eslint/prefer-for-of\n for (let i = 0; i < entries.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n if (typeof entries[i]![0] !== 'string' || !validateJson(entries[i]![1])) {\n every = false;\n break;\n }\n }\n return every;\n }\n\n return false;\n}\n\n/**\n * A struct to check if the given value is a valid JSON-serializable value.\n *\n * Note that this struct is unsafe. For safe validation, use {@link JsonStruct}.\n */\nexport const UnsafeJsonStruct: Struct<Json> = define('JSON', (json) =>\n validateJson(json),\n);\n\n/**\n * A struct to check if the given value is a valid JSON-serializable value.\n *\n * This struct sanitizes the value before validating it, so that it is safe to\n * use with untrusted input.\n */\nexport const JsonStruct = coerce(UnsafeJsonStruct, any(), (value) => {\n assertStruct(value, UnsafeJsonStruct);\n return JSON.parse(\n JSON.stringify(value, (propKey, propValue) => {\n // Strip __proto__ and constructor properties to prevent prototype pollution.\n if (propKey === '__proto__' || propKey === 'constructor') {\n return undefined;\n }\n return propValue;\n }),\n );\n});\n\n/**\n * Check if the given value is a valid {@link Json} value, i.e., a value that is\n * serializable to JSON.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link Json} value.\n */\nexport function isValidJson(value: unknown): value is Json {\n try {\n getSafeJson(value);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Validate and return sanitized JSON.\n *\n * Note:\n * This function uses sanitized JsonStruct for validation\n * that applies stringify and then parse of a value provided\n * to ensure that there are no getters which can have side effects\n * that can cause security issues.\n *\n * @param value - JSON structure to be processed.\n * @returns Sanitized JSON structure.\n */\nexport function getSafeJson<Type extends Json = Json>(value: unknown): Type {\n return create(value, JsonStruct) as Type;\n}\n\n/**\n * Get the size of a JSON value in bytes. This also validates the value.\n *\n * @param value - The JSON value to get the size of.\n * @returns The size of the JSON value in bytes.\n */\nexport function getJsonSize(value: unknown): number {\n assertStruct(value, JsonStruct, 'Invalid JSON value');\n\n const json = JSON.stringify(value);\n return new TextEncoder().encode(json).byteLength;\n}\n\n/**\n * The string '2.0'.\n */\nexport const jsonrpc2 = '2.0' as const;\nexport const JsonRpcVersionStruct = literal(jsonrpc2);\n\n/**\n * A String specifying the version of the JSON-RPC protocol.\n * MUST be exactly \"2.0\".\n */\nexport type JsonRpcVersion2 = typeof jsonrpc2;\n\nexport const JsonRpcIdStruct = nullable(union([number(), string()]));\n\n/**\n * An identifier established by the Client that MUST contain a String, Number,\n * or NULL value if included. If it is not included it is assumed to be a\n * notification. The value SHOULD normally not be Null and Numbers SHOULD\n * NOT contain fractional parts.\n */\nexport type JsonRpcId = Infer<typeof JsonRpcIdStruct>;\n\nexport const JsonRpcErrorStruct = object({\n code: integer(),\n message: string(),\n data: exactOptional(JsonStruct),\n stack: exactOptional(string()),\n});\n\n/**\n * Mark a certain key of a type as optional.\n */\nexport type OptionalField<\n Type extends Record<string, unknown>,\n Key extends keyof Type,\n> = Omit<Type, Key> & Partial<Pick<Type, Key>>;\n\n/**\n * A JSON-RPC error object.\n *\n * Note that TypeScript infers `unknown | undefined` as `unknown`, meaning that\n * the `data` field is not optional. To make it optional, we use the\n * `OptionalField` helper, to explicitly make it optional.\n */\nexport type JsonRpcError = OptionalField<\n Infer<typeof JsonRpcErrorStruct>,\n 'data'\n>;\n\nexport const JsonRpcParamsStruct: Struct<Json[] | Record<string, Json>, null> =\n union([record(string(), JsonStruct), array(JsonStruct)]);\n\nexport type JsonRpcParams = Json[] | Record<string, Json>;\n\nexport const JsonRpcRequestStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n method: string(),\n params: exactOptional(JsonRpcParamsStruct),\n});\n\nexport type InferWithParams<\n Type extends Struct<any>,\n Params extends JsonRpcParams,\n> = Infer<Type> & {\n params?: Params;\n};\n\n/**\n * A JSON-RPC request object.\n */\nexport type JsonRpcRequest<Params extends JsonRpcParams = JsonRpcParams> =\n InferWithParams<typeof JsonRpcRequestStruct, Params>;\n\nexport const JsonRpcNotificationStruct = object({\n jsonrpc: JsonRpcVersionStruct,\n method: string(),\n params: exactOptional(JsonRpcParamsStruct),\n});\n\n/**\n * A JSON-RPC notification object.\n */\nexport type JsonRpcNotification<Params extends JsonRpcParams = JsonRpcParams> =\n InferWithParams<typeof JsonRpcNotificationStruct, Params>;\n\n/**\n * Check if the given value is a valid {@link JsonRpcNotification} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcNotification}\n * object.\n */\nexport function isJsonRpcNotification(\n value: unknown,\n): value is JsonRpcNotification {\n return is(value, JsonRpcNotificationStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcNotification} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcNotification} object.\n */\nexport function assertIsJsonRpcNotification(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcNotification {\n assertStruct(\n value,\n JsonRpcNotificationStruct,\n 'Invalid JSON-RPC notification',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcRequest} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcRequest} object.\n */\nexport function isJsonRpcRequest(value: unknown): value is JsonRpcRequest {\n return is(value, JsonRpcRequestStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcRequest} object.\n *\n * @param value - The JSON-RPC request or notification to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcRequest} object.\n */\nexport function assertIsJsonRpcRequest(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcRequest {\n assertStruct(\n value,\n JsonRpcRequestStruct,\n 'Invalid JSON-RPC request',\n ErrorWrapper,\n );\n}\n\nexport const PendingJsonRpcResponseStruct = superstructObject({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n result: optional(unknown()),\n error: optional(JsonRpcErrorStruct),\n});\n\n/**\n * A JSON-RPC response object that has not yet been resolved.\n */\nexport type PendingJsonRpcResponse<Result extends Json> = Omit<\n Infer<typeof PendingJsonRpcResponseStruct>,\n 'result'\n> & {\n result?: Result;\n};\n\nexport const JsonRpcSuccessStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n result: JsonStruct,\n});\n\n/**\n * A successful JSON-RPC response object.\n */\nexport type JsonRpcSuccess<Result extends Json> = Omit<\n Infer<typeof JsonRpcSuccessStruct>,\n 'result'\n> & {\n result: Result;\n};\n\nexport const JsonRpcFailureStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n error: JsonRpcErrorStruct as Struct<JsonRpcError>,\n});\n\n/**\n * A failed JSON-RPC response object.\n */\nexport type JsonRpcFailure = Infer<typeof JsonRpcFailureStruct>;\n\nexport const JsonRpcResponseStruct = union([\n JsonRpcSuccessStruct,\n JsonRpcFailureStruct,\n]);\n\n/**\n * A JSON-RPC response object. Must be checked to determine whether it's a\n * success or failure.\n *\n * @template Result - The type of the result.\n */\nexport type JsonRpcResponse<Result extends Json> =\n | JsonRpcSuccess<Result>\n | JsonRpcFailure;\n\n/**\n * Type guard to check whether specified JSON-RPC response is a\n * {@link PendingJsonRpcResponse}.\n *\n * @param response - The JSON-RPC response to check.\n * @returns Whether the specified JSON-RPC response is pending.\n */\nexport function isPendingJsonRpcResponse(\n response: unknown,\n): response is PendingJsonRpcResponse<Json> {\n return is(response, PendingJsonRpcResponseStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link PendingJsonRpcResponse} object.\n *\n * @param response - The JSON-RPC response to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link PendingJsonRpcResponse}\n * object.\n */\nexport function assertIsPendingJsonRpcResponse(\n response: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts response is PendingJsonRpcResponse<Json> {\n assertStruct(\n response,\n PendingJsonRpcResponseStruct,\n 'Invalid pending JSON-RPC response',\n ErrorWrapper,\n );\n}\n\n/**\n * Type guard to check if a value is a {@link JsonRpcResponse}.\n *\n * @param response - The object to check.\n * @returns Whether the object is a JsonRpcResponse.\n */\nexport function isJsonRpcResponse(\n response: unknown,\n): response is JsonRpcResponse<Json> {\n return is(response, JsonRpcResponseStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcResponse} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcResponse} object.\n */\nexport function assertIsJsonRpcResponse(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcResponse<Json> {\n assertStruct(\n value,\n JsonRpcResponseStruct,\n 'Invalid JSON-RPC response',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcSuccess} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcSuccess} object.\n */\nexport function isJsonRpcSuccess(\n value: unknown,\n): value is JsonRpcSuccess<Json> {\n return is(value, JsonRpcSuccessStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcSuccess} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcSuccess} object.\n */\nexport function assertIsJsonRpcSuccess(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcSuccess<Json> {\n assertStruct(\n value,\n JsonRpcSuccessStruct,\n 'Invalid JSON-RPC success response',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcFailure} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcFailure} object.\n */\nexport function isJsonRpcFailure(value: unknown): value is JsonRpcFailure {\n return is(value, JsonRpcFailureStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcFailure} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcFailure} object.\n */\nexport function assertIsJsonRpcFailure(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcFailure {\n assertStruct(\n value,\n JsonRpcFailureStruct,\n 'Invalid JSON-RPC failure response',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcError} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcError} object.\n */\nexport function isJsonRpcError(value: unknown): value is JsonRpcError {\n return is(value, JsonRpcErrorStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcError} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcError} object.\n */\nexport function assertIsJsonRpcError(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcError {\n assertStruct(\n value,\n JsonRpcErrorStruct,\n 'Invalid JSON-RPC error',\n ErrorWrapper,\n );\n}\n\ntype JsonRpcValidatorOptions = {\n permitEmptyString?: boolean;\n permitFractions?: boolean;\n permitNull?: boolean;\n};\n\n/**\n * Gets a function for validating JSON-RPC request / response `id` values.\n *\n * By manipulating the options of this factory, you can control the behavior\n * of the resulting validator for some edge cases. This is useful because e.g.\n * `null` should sometimes but not always be permitted.\n *\n * Note that the empty string (`''`) is always permitted by the JSON-RPC\n * specification, but that kind of sucks and you may want to forbid it in some\n * instances anyway.\n *\n * For more details, see the\n * [JSON-RPC Specification](https://www.jsonrpc.org/specification).\n *\n * @param options - An options object.\n * @param options.permitEmptyString - Whether the empty string (i.e. `''`)\n * should be treated as a valid ID. Default: `true`\n * @param options.permitFractions - Whether fractional numbers (e.g. `1.2`)\n * should be treated as valid IDs. Default: `false`\n * @param options.permitNull - Whether `null` should be treated as a valid ID.\n * Default: `true`\n * @returns The JSON-RPC ID validator function.\n */\nexport function getJsonRpcIdValidator(options?: JsonRpcValidatorOptions) {\n const { permitEmptyString, permitFractions, permitNull } = {\n permitEmptyString: true,\n permitFractions: false,\n permitNull: true,\n ...options,\n };\n\n /**\n * Type guard for {@link JsonRpcId}.\n *\n * @param id - The JSON-RPC ID value to check.\n * @returns Whether the given ID is valid per the options given to the\n * factory.\n */\n const isValidJsonRpcId = (id: unknown): id is JsonRpcId => {\n return Boolean(\n (typeof id === 'number' && (permitFractions || Number.isInteger(id))) ||\n (typeof id === 'string' && (permitEmptyString || id.length > 0)) ||\n (permitNull && id === null),\n );\n };\n\n return isValidJsonRpcId;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"json.mjs","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,KAAK,EACL,MAAM,EACN,MAAM,EACN,MAAM,EACN,OAAO,EACP,EAAE,EACF,OAAO,EACP,QAAQ,EACR,MAAM,EACN,MAAM,IAAI,iBAAiB,EAC3B,QAAQ,EACR,MAAM,EACN,MAAM,EACN,KAAK,EACL,OAAO,EACP,MAAM,EACN,MAAM,EACP,8BAA8B;AAU/B,OAAO,EAAE,YAAY,EAAE,qBAAiB;AACxC,OAAO,EAAE,WAAW,EAAE,mBAAe;AAgDrC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CACpB,MAAc,EACc,EAAE;AAC9B,0EAA0E;AAC1E,2EAA2E;AAC3E,2DAA2D;AAC3D,iBAAiB,CAAC,MAAM,CAA0C,CAAC;AAOrE;;;;;;;GAOG;AACH,SAAS,WAAW,CAAC,EAAE,IAAI,EAAE,MAAM,EAAW;IAC5C,MAAM,KAA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B,CAAC","sourcesContent":["import {\n any,\n array,\n coerce,\n create,\n define,\n integer,\n is,\n literal,\n nullable,\n number,\n object as superstructObject,\n optional,\n record,\n string,\n union,\n unknown,\n Struct,\n refine,\n} from '@metamask/superstruct';\nimport type {\n Context,\n Infer,\n ObjectSchema,\n Simplify,\n Optionalize,\n} from '@metamask/superstruct';\n\nimport type { AssertionErrorConstructor } from './assert';\nimport { assertStruct } from './assert';\nimport { hasProperty } from './misc';\n\n/**\n * Any JSON-compatible value.\n */\nexport type Json =\n | null\n | boolean\n | number\n | string\n | Json[]\n | { [prop: string]: Json };\n\n/**\n * A helper type to make properties with `undefined` in their type optional, but\n * not `undefined` itself.\n *\n * @example\n * ```ts\n * type Foo = ObjectOptional<{ foo: string | undefined }>;\n * // Foo is equivalent to { foo?: string }\n * ```\n */\nexport type ObjectOptional<Schema extends Record<string, unknown>> = {\n [Key in keyof Schema as Schema[Key] extends ExactOptionalGuard\n ? Key\n : never]?: Schema[Key] extends ExactOptionalGuard & infer Original\n ? Original\n : never;\n} & {\n [Key in keyof Schema as Schema[Key] extends ExactOptionalGuard\n ? never\n : Key]: Schema[Key];\n};\n\n/**\n * An object type with support for exact optionals. This is used by the `object`\n * struct. This uses the {@link ObjectOptional} helper to make properties with\n * `undefined` in their type optional, but not `undefined` itself.\n */\nexport type ObjectType<Schema extends ObjectSchema> = Simplify<\n ObjectOptional<\n Optionalize<{\n [Key in keyof Schema]: Infer<Schema[Key]>;\n }>\n >\n>;\n\n/**\n * A struct to check if the given value is a valid object, with support for\n * {@link exactOptional} types.\n *\n * @param schema - The schema of the object.\n * @returns A struct to check if the given value is an object.\n */\nexport const object = <Schema extends ObjectSchema>(\n schema: Schema,\n): Struct<ObjectType<Schema>> =>\n // The type is slightly different from a regular object struct, because we\n // want to make properties with `undefined` in their type optional, but not\n // `undefined` itself. This means that we need a type cast.\n superstructObject(schema) as unknown as Struct<ObjectType<Schema>>;\n\ndeclare const exactOptionalSymbol: unique symbol;\ntype ExactOptionalGuard = {\n _exactOptionalGuard?: typeof exactOptionalSymbol;\n};\n\n/**\n * Check the last field of a path is present.\n *\n * @param context - The context to check.\n * @param context.path - The path to check.\n * @param context.branch - The branch to check.\n * @returns Whether the last field of a path is present.\n */\nfunction hasOptional({ path, branch }: Context): boolean {\n const field = path[path.length - 1];\n return hasProperty(branch[branch.length - 2], field);\n}\n\n/**\n * A struct which allows the property of an object to be absent, or to be present\n * as long as it's valid and not set to `undefined`.\n *\n * This struct should be used in conjunction with the {@link object} from this\n * library, to get proper type inference.\n *\n * @param struct - The struct to check the value against, if present.\n * @returns A struct to check if the given value is valid, or not present.\n * @example\n * ```ts\n * const struct = object({\n * foo: exactOptional(string()),\n * bar: exactOptional(number()),\n * baz: optional(boolean()),\n * qux: unknown(),\n * });\n *\n * type Type = Infer<typeof struct>;\n * // Type is equivalent to:\n * // {\n * // foo?: string;\n * // bar?: number;\n * // baz?: boolean | undefined;\n * // qux: unknown;\n * // }\n * ```\n */\nexport function exactOptional<Type, Schema>(\n struct: Struct<Type, Schema>,\n): Struct<Type & ExactOptionalGuard, Schema> {\n return new Struct<Type & ExactOptionalGuard, Schema>({\n ...struct,\n\n type: `optional ${struct.type}`,\n validator: (value, context) =>\n !hasOptional(context) || struct.validator(value, context),\n\n refiner: (value, context) =>\n !hasOptional(context) || struct.refiner(value as Type, context),\n });\n}\n\n/**\n * Validate an unknown input to be valid JSON.\n *\n * Useful for constructing JSON structs.\n *\n * @param json - An unknown value.\n * @returns True if the value is valid JSON, otherwise false.\n */\nfunction validateJson(json: unknown): boolean {\n if (json === null || typeof json === 'boolean' || typeof json === 'string') {\n return true;\n }\n\n if (typeof json === 'number' && Number.isFinite(json)) {\n return true;\n }\n\n if (typeof json === 'object') {\n let every = true;\n if (Array.isArray(json)) {\n // Ignoring linting error since for-of is significantly slower than a normal for-loop\n // and performance is important in this specific function.\n // eslint-disable-next-line @typescript-eslint/prefer-for-of\n for (let i = 0; i < json.length; i++) {\n if (!validateJson(json[i])) {\n every = false;\n break;\n }\n }\n return every;\n }\n\n const entries = Object.entries(json);\n // Ignoring linting errors since for-of is significantly slower than a normal for-loop\n // and performance is important in this specific function.\n // eslint-disable-next-line @typescript-eslint/prefer-for-of\n for (let i = 0; i < entries.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n if (typeof entries[i]![0] !== 'string' || !validateJson(entries[i]![1])) {\n every = false;\n break;\n }\n }\n return every;\n }\n\n return false;\n}\n\n/**\n * A struct to check if the given value is a valid JSON-serializable value.\n *\n * Note that this struct is unsafe. For safe validation, use {@link JsonStruct}.\n */\nexport const UnsafeJsonStruct: Struct<Json> = define('JSON', (json) =>\n validateJson(json),\n);\n\n/**\n * A struct to check if the given value is a valid JSON-serializable value.\n *\n * This struct sanitizes the value before validating it, so that it is safe to\n * use with untrusted input.\n */\nexport const JsonStruct = coerce(\n UnsafeJsonStruct,\n refine(any(), 'JSON', (value) => is(value, UnsafeJsonStruct)),\n (value) =>\n JSON.parse(\n JSON.stringify(value, (propKey, propValue) => {\n // Strip __proto__ and constructor properties to prevent prototype pollution.\n if (propKey === '__proto__' || propKey === 'constructor') {\n return undefined;\n }\n return propValue;\n }),\n ),\n);\n\n/**\n * Check if the given value is a valid {@link Json} value, i.e., a value that is\n * serializable to JSON.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link Json} value.\n */\nexport function isValidJson(value: unknown): value is Json {\n try {\n getSafeJson(value);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Validate and return sanitized JSON.\n *\n * Note:\n * This function uses sanitized JsonStruct for validation\n * that applies stringify and then parse of a value provided\n * to ensure that there are no getters which can have side effects\n * that can cause security issues.\n *\n * @param value - JSON structure to be processed.\n * @returns Sanitized JSON structure.\n */\nexport function getSafeJson<Type extends Json = Json>(value: unknown): Type {\n return create(value, JsonStruct) as Type;\n}\n\n/**\n * Get the size of a JSON value in bytes. This also validates the value.\n *\n * @param value - The JSON value to get the size of.\n * @returns The size of the JSON value in bytes.\n */\nexport function getJsonSize(value: unknown): number {\n assertStruct(value, JsonStruct, 'Invalid JSON value');\n\n const json = JSON.stringify(value);\n return new TextEncoder().encode(json).byteLength;\n}\n\n/**\n * The string '2.0'.\n */\nexport const jsonrpc2 = '2.0' as const;\nexport const JsonRpcVersionStruct = literal(jsonrpc2);\n\n/**\n * A String specifying the version of the JSON-RPC protocol.\n * MUST be exactly \"2.0\".\n */\nexport type JsonRpcVersion2 = typeof jsonrpc2;\n\nexport const JsonRpcIdStruct = nullable(union([number(), string()]));\n\n/**\n * An identifier established by the Client that MUST contain a String, Number,\n * or NULL value if included. If it is not included it is assumed to be a\n * notification. The value SHOULD normally not be Null and Numbers SHOULD\n * NOT contain fractional parts.\n */\nexport type JsonRpcId = Infer<typeof JsonRpcIdStruct>;\n\nexport const JsonRpcErrorStruct = object({\n code: integer(),\n message: string(),\n data: exactOptional(JsonStruct),\n stack: exactOptional(string()),\n});\n\n/**\n * Mark a certain key of a type as optional.\n */\nexport type OptionalField<\n Type extends Record<string, unknown>,\n Key extends keyof Type,\n> = Omit<Type, Key> & Partial<Pick<Type, Key>>;\n\n/**\n * A JSON-RPC error object.\n *\n * Note that TypeScript infers `unknown | undefined` as `unknown`, meaning that\n * the `data` field is not optional. To make it optional, we use the\n * `OptionalField` helper, to explicitly make it optional.\n */\nexport type JsonRpcError = OptionalField<\n Infer<typeof JsonRpcErrorStruct>,\n 'data'\n>;\n\nexport const JsonRpcParamsStruct: Struct<Json[] | Record<string, Json>, null> =\n union([record(string(), JsonStruct), array(JsonStruct)]);\n\nexport type JsonRpcParams = Json[] | Record<string, Json>;\n\nexport const JsonRpcRequestStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n method: string(),\n params: exactOptional(JsonRpcParamsStruct),\n});\n\nexport type InferWithParams<\n Type extends Struct<any>,\n Params extends JsonRpcParams,\n> = Infer<Type> & {\n params?: Params;\n};\n\n/**\n * A JSON-RPC request object.\n */\nexport type JsonRpcRequest<Params extends JsonRpcParams = JsonRpcParams> =\n InferWithParams<typeof JsonRpcRequestStruct, Params>;\n\nexport const JsonRpcNotificationStruct = object({\n jsonrpc: JsonRpcVersionStruct,\n method: string(),\n params: exactOptional(JsonRpcParamsStruct),\n});\n\n/**\n * A JSON-RPC notification object.\n */\nexport type JsonRpcNotification<Params extends JsonRpcParams = JsonRpcParams> =\n InferWithParams<typeof JsonRpcNotificationStruct, Params>;\n\n/**\n * Check if the given value is a valid {@link JsonRpcNotification} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcNotification}\n * object.\n */\nexport function isJsonRpcNotification(\n value: unknown,\n): value is JsonRpcNotification {\n return is(value, JsonRpcNotificationStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcNotification} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcNotification} object.\n */\nexport function assertIsJsonRpcNotification(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcNotification {\n assertStruct(\n value,\n JsonRpcNotificationStruct,\n 'Invalid JSON-RPC notification',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcRequest} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcRequest} object.\n */\nexport function isJsonRpcRequest(value: unknown): value is JsonRpcRequest {\n return is(value, JsonRpcRequestStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcRequest} object.\n *\n * @param value - The JSON-RPC request or notification to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcRequest} object.\n */\nexport function assertIsJsonRpcRequest(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcRequest {\n assertStruct(\n value,\n JsonRpcRequestStruct,\n 'Invalid JSON-RPC request',\n ErrorWrapper,\n );\n}\n\nexport const PendingJsonRpcResponseStruct = superstructObject({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n result: optional(unknown()),\n error: optional(JsonRpcErrorStruct),\n});\n\n/**\n * A JSON-RPC response object that has not yet been resolved.\n */\nexport type PendingJsonRpcResponse<Result extends Json> = Omit<\n Infer<typeof PendingJsonRpcResponseStruct>,\n 'result'\n> & {\n result?: Result;\n};\n\nexport const JsonRpcSuccessStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n result: JsonStruct,\n});\n\n/**\n * A successful JSON-RPC response object.\n */\nexport type JsonRpcSuccess<Result extends Json> = Omit<\n Infer<typeof JsonRpcSuccessStruct>,\n 'result'\n> & {\n result: Result;\n};\n\nexport const JsonRpcFailureStruct = object({\n id: JsonRpcIdStruct,\n jsonrpc: JsonRpcVersionStruct,\n error: JsonRpcErrorStruct as Struct<JsonRpcError>,\n});\n\n/**\n * A failed JSON-RPC response object.\n */\nexport type JsonRpcFailure = Infer<typeof JsonRpcFailureStruct>;\n\nexport const JsonRpcResponseStruct = union([\n JsonRpcSuccessStruct,\n JsonRpcFailureStruct,\n]);\n\n/**\n * A JSON-RPC response object. Must be checked to determine whether it's a\n * success or failure.\n *\n * @template Result - The type of the result.\n */\nexport type JsonRpcResponse<Result extends Json> =\n | JsonRpcSuccess<Result>\n | JsonRpcFailure;\n\n/**\n * Type guard to check whether specified JSON-RPC response is a\n * {@link PendingJsonRpcResponse}.\n *\n * @param response - The JSON-RPC response to check.\n * @returns Whether the specified JSON-RPC response is pending.\n */\nexport function isPendingJsonRpcResponse(\n response: unknown,\n): response is PendingJsonRpcResponse<Json> {\n return is(response, PendingJsonRpcResponseStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link PendingJsonRpcResponse} object.\n *\n * @param response - The JSON-RPC response to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link PendingJsonRpcResponse}\n * object.\n */\nexport function assertIsPendingJsonRpcResponse(\n response: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts response is PendingJsonRpcResponse<Json> {\n assertStruct(\n response,\n PendingJsonRpcResponseStruct,\n 'Invalid pending JSON-RPC response',\n ErrorWrapper,\n );\n}\n\n/**\n * Type guard to check if a value is a {@link JsonRpcResponse}.\n *\n * @param response - The object to check.\n * @returns Whether the object is a JsonRpcResponse.\n */\nexport function isJsonRpcResponse(\n response: unknown,\n): response is JsonRpcResponse<Json> {\n return is(response, JsonRpcResponseStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcResponse} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcResponse} object.\n */\nexport function assertIsJsonRpcResponse(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcResponse<Json> {\n assertStruct(\n value,\n JsonRpcResponseStruct,\n 'Invalid JSON-RPC response',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcSuccess} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcSuccess} object.\n */\nexport function isJsonRpcSuccess(\n value: unknown,\n): value is JsonRpcSuccess<Json> {\n return is(value, JsonRpcSuccessStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcSuccess} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcSuccess} object.\n */\nexport function assertIsJsonRpcSuccess(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcSuccess<Json> {\n assertStruct(\n value,\n JsonRpcSuccessStruct,\n 'Invalid JSON-RPC success response',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcFailure} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcFailure} object.\n */\nexport function isJsonRpcFailure(value: unknown): value is JsonRpcFailure {\n return is(value, JsonRpcFailureStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcFailure} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcFailure} object.\n */\nexport function assertIsJsonRpcFailure(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcFailure {\n assertStruct(\n value,\n JsonRpcFailureStruct,\n 'Invalid JSON-RPC failure response',\n ErrorWrapper,\n );\n}\n\n/**\n * Check if the given value is a valid {@link JsonRpcError} object.\n *\n * @param value - The value to check.\n * @returns Whether the given value is a valid {@link JsonRpcError} object.\n */\nexport function isJsonRpcError(value: unknown): value is JsonRpcError {\n return is(value, JsonRpcErrorStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link JsonRpcError} object.\n *\n * @param value - The value to check.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the given value is not a valid {@link JsonRpcError} object.\n */\nexport function assertIsJsonRpcError(\n value: unknown,\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper?: AssertionErrorConstructor,\n): asserts value is JsonRpcError {\n assertStruct(\n value,\n JsonRpcErrorStruct,\n 'Invalid JSON-RPC error',\n ErrorWrapper,\n );\n}\n\ntype JsonRpcValidatorOptions = {\n permitEmptyString?: boolean;\n permitFractions?: boolean;\n permitNull?: boolean;\n};\n\n/**\n * Gets a function for validating JSON-RPC request / response `id` values.\n *\n * By manipulating the options of this factory, you can control the behavior\n * of the resulting validator for some edge cases. This is useful because e.g.\n * `null` should sometimes but not always be permitted.\n *\n * Note that the empty string (`''`) is always permitted by the JSON-RPC\n * specification, but that kind of sucks and you may want to forbid it in some\n * instances anyway.\n *\n * For more details, see the\n * [JSON-RPC Specification](https://www.jsonrpc.org/specification).\n *\n * @param options - An options object.\n * @param options.permitEmptyString - Whether the empty string (i.e. `''`)\n * should be treated as a valid ID. Default: `true`\n * @param options.permitFractions - Whether fractional numbers (e.g. `1.2`)\n * should be treated as valid IDs. Default: `false`\n * @param options.permitNull - Whether `null` should be treated as a valid ID.\n * Default: `true`\n * @returns The JSON-RPC ID validator function.\n */\nexport function getJsonRpcIdValidator(options?: JsonRpcValidatorOptions) {\n const { permitEmptyString, permitFractions, permitNull } = {\n permitEmptyString: true,\n permitFractions: false,\n permitNull: true,\n ...options,\n };\n\n /**\n * Type guard for {@link JsonRpcId}.\n *\n * @param id - The JSON-RPC ID value to check.\n * @returns Whether the given ID is valid per the options given to the\n * factory.\n */\n const isValidJsonRpcId = (id: unknown): id is JsonRpcId => {\n return Boolean(\n (typeof id === 'number' && (permitFractions || Number.isInteger(id))) ||\n (typeof id === 'string' && (permitEmptyString || id.length > 0)) ||\n (permitNull && id === null),\n );\n };\n\n return isValidJsonRpcId;\n}\n"]}
|
package/dist/keyring.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyring.cjs","sourceRoot":"","sources":["../src/keyring.ts"],"names":[],"mappings":"","sourcesContent":["import type { TypedTransaction, TxData } from '@ethereumjs/tx';\n\nimport type { Eip1024EncryptedData } from './encryption-types';\nimport type { Hex } from './hex';\nimport type { Json } from './json';\n\n/**\n * A Keyring class.\n *\n * This type is used to validate the constructor signature and the `type`\n * static property on Keyring classes. See the {@link Keyring} type for more\n * information.\n */\nexport type KeyringClass<State extends Json> = {\n /**\n * The Keyring constructor. Takes a single parameter, an \"options\" object.\n * See the documentation for the specific keyring for more information about\n * what these options are.\n *\n * @param options - The constructor options. Differs between keyring\n * implementations.\n */\n new (options?: Record<string, unknown>): Keyring<State>;\n\n /**\n * The name of this type of keyring. This must uniquely identify the\n * keyring type.\n */\n type: string;\n};\n\n/**\n * A keyring is something that can sign messages. Keyrings are used to add new\n * signing strategies; each strategy is a new keyring.\n *\n * Each keyring manages a collection of key pairs, which we call \"accounts\".\n * Each account is referred to by its \"address\", which is a unique identifier\n * derived from the public key. The address is always a \"0x\"-prefixed\n * hexidecimal string.\n *\n * The keyring might store the private key for each account as well, but it's\n * not guaranteed. Some keyrings delegate signing, so they don't need the\n * private key directly. The keyring (and in particular the keyring state)\n * should be treated with care though, just in case it does contain sensitive\n * material such as a private key.\n */\nexport type Keyring<State extends Json> = {\n /**\n * The name of this type of keyring. This must match the `type` property of\n * the keyring class.\n */\n type: string;\n\n /**\n * Get the addresses for all accounts in this keyring.\n *\n * @returns A list of the account addresses for this keyring\n */\n getAccounts(): Promise<Hex[]>;\n\n /**\n * Add an account to the keyring.\n *\n * @param number - The number of accounts to add. Usually defaults to 1.\n * @returns A list of the newly added account addresses.\n */\n addAccounts(number: number): Promise<Hex[]>;\n\n /**\n * Serialize the keyring state as a JSON-serializable object.\n *\n * @returns A JSON-serializable representation of the keyring state.\n */\n serialize(): Promise<State>;\n\n /**\n * Deserialize the given keyring state, overwriting any existing state with\n * the serialized state provided.\n *\n * @param state - A JSON-serializable representation of the keyring state.\n */\n deserialize(state: State): Promise<void>;\n\n /**\n * Method to include asynchronous configuration.\n */\n init?(): Promise<void>;\n\n /**\n * Remove an account from the keyring.\n *\n * @param address - The address of the account to remove.\n */\n removeAccount?(address: Hex): void;\n\n /**\n * Export the private key for one of the keyring accounts.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter is used to allow exporting a\n * private key that is derived from the given account, rather than exporting\n * that account's private key directly.\n *\n * @param address - The address of the account to export.\n * @param options - Export options; differs between keyrings.\n * @returns The non-prefixed, hex-encoded private key that was requested.\n */\n exportAccount?(\n address: Hex,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Get the \"app key\" address for the given account and origin. An app key is\n * an application-specific key pair. See {@link https://eips.ethereum.org/EIPS/eip-1775|EIP-1775}\n * for more information. The {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin|origin}\n * is used as the unique identifier for the application, and it's used as\n * part of the key derivation process.\n *\n * @param address - The address of the account the app key is derived from.\n * @param origin - The origin of the application.\n * @returns The address of the app key for the given account and origin.\n */\n getAppKeyAddress?(address: Hex, origin: string): Promise<Hex>;\n\n /**\n * Sign a transaction. This is equivalent to the `eth_signTransaction`\n * Ethereum JSON-RPC method. See the Ethereum JSON-RPC API documentation for\n * more details.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param transaction - The transaction to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed transaction.\n */\n signTransaction?(\n address: Hex,\n transaction: TypedTransaction,\n options?: Record<string, unknown>,\n ): Promise<TxData>;\n\n /**\n * Sign a message. This is equivalent to an older version of the the\n * `eth_sign` Ethereum JSON-RPC method. The message is signed using ECDSA,\n * using the curve secp256k1 the Keccak-256 hash function.\n *\n * For more information about this method and why we still support it, see\n * the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signMessage?(\n address: Hex,\n message: string,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Sign a message. This is equivalent to the `eth_sign` Ethereum JSON-RPC\n * method, which is exposed by MetaMask as the method `personal_sign`. See\n * the Ethereum JSON-RPC API documentation for more details.\n *\n * For more information about this method and why we call it `personal_sign`,\n * see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signPersonalMessage?(\n address: Hex,\n message: Hex,\n options?: { version?: string } & Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Sign a message. This is equivalent to the `eth_signTypedData` Ethereum\n * JSON-RPC method. See {@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md|EIP-712}\n * for more details.\n *\n * The \"version\" option dictates which version of `eth_signTypedData` is\n * used. The latest version reflects the specification most closely, whereas\n * earlier versions reflect earlier drafts of the specification that are\n * still supported for backwards-compatibility reasons. For more information\n * about why we support multiple versions, see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept additional options as well. See the documentation for\n * the specific keyring for more information about what these options are.\n * For some keyrings, the options parameter can even change which key is used\n * for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param typedData - The data to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signTypedData?(\n address: Hex,\n typedData: Record<string, unknown>,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Get a public key to use for encryption. This is equivalent to the\n * ` eth_getEncryptionPublicKey` JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-getencryptionpublickey|MetaMask Docs}\n * for more information.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used (e.g. encrypting with app keys).\n *\n * @param account - The address of the account you want the encryption key for.\n * @param options - Options; differs between keyrings.\n */\n getEncryptionPublicKey?(\n account: Hex,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Decrypt an encrypted message. This is equivalent to the ` eth_decrypt`\n * JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-decrypt|MetaMask Docs}\n * for more information.\n *\n * @param account - The address of the account you want to use to decrypt\n * the message.\n * @param encryptedData - The encrypted data that you want to decrypt.\n * @returns The decrypted data.\n */\n decryptMessage?(\n account: Hex,\n encryptedData: Eip1024EncryptedData,\n ): Promise<string>;\n\n /**\n * Generates the properties for the keyring based on the given\n * BIP39-compliant mnemonic.\n */\n generateRandomMnemonic?(): void;\n\n /**\n * Destroy the keyring.\n */\n destroy?(): Promise<void>;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"keyring.cjs","sourceRoot":"","sources":["../src/keyring.ts"],"names":[],"mappings":"","sourcesContent":["import type { TypedTransaction, TxData } from '@ethereumjs/tx';\n\nimport type { Eip1024EncryptedData } from './encryption-types';\nimport type { Hex } from './hex';\nimport type { Json } from './json';\n\n/**\n * A Keyring class.\n *\n * This type is used to validate the constructor signature and the `type`\n * static property on Keyring classes. See the {@link Keyring} type for more\n * information.\n */\nexport type KeyringClass<State extends Json> = {\n /**\n * The Keyring constructor. Takes a single parameter, an \"options\" object.\n * See the documentation for the specific keyring for more information about\n * what these options are.\n *\n * @param options - The constructor options. Differs between keyring\n * implementations.\n */\n new (options?: Record<string, unknown>): Keyring<State>;\n\n /**\n * The name of this type of keyring. This must uniquely identify the\n * keyring type.\n */\n type: string;\n};\n\n/**\n * A keyring is something that can sign messages. Keyrings are used to add new\n * signing strategies; each strategy is a new keyring.\n *\n * Each keyring manages a collection of key pairs, which we call \"accounts\".\n * Each account is referred to by its \"address\", which is a unique identifier\n * derived from the public key. The address is always a \"0x\"-prefixed\n * hexidecimal string.\n *\n * The keyring might store the private key for each account as well, but it's\n * not guaranteed. Some keyrings delegate signing, so they don't need the\n * private key directly. The keyring (and in particular the keyring state)\n * should be treated with care though, just in case it does contain sensitive\n * material such as a private key.\n */\nexport type Keyring<State extends Json> = {\n /**\n * The name of this type of keyring. This must match the `type` property of\n * the keyring class.\n */\n type: string;\n\n /**\n * Get the addresses for all accounts in this keyring.\n *\n * @returns A list of the account addresses for this keyring\n */\n getAccounts(): Promise<Hex[]>;\n\n /**\n * Add an account to the keyring.\n *\n * @param number - The number of accounts to add. Usually defaults to 1.\n * @returns A list of the newly added account addresses.\n */\n addAccounts(number: number): Promise<Hex[]>;\n\n /**\n * Serialize the keyring state as a JSON-serializable object.\n *\n * @returns A JSON-serializable representation of the keyring state.\n */\n serialize(): Promise<State>;\n\n /**\n * Deserialize the given keyring state, overwriting any existing state with\n * the serialized state provided.\n *\n * @param state - A JSON-serializable representation of the keyring state.\n */\n deserialize(state: State): Promise<void>;\n\n /**\n * Method to include asynchronous configuration.\n */\n init?(): Promise<void>;\n\n /**\n * Remove an account from the keyring.\n *\n * @param address - The address of the account to remove.\n */\n removeAccount?(address: Hex): void;\n\n /**\n * Export the private key for one of the keyring accounts.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter is used to allow exporting a\n * private key that is derived from the given account, rather than exporting\n * that account's private key directly.\n *\n * @param address - The address of the account to export.\n * @param options - Export options; differs between keyrings.\n * @returns The non-prefixed, hex-encoded private key that was requested.\n */\n exportAccount?(\n address: Hex,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Get the \"app key\" address for the given account and origin. An app key is\n * an application-specific key pair. See {@link https://eips.ethereum.org/EIPS/eip-1775|EIP-1775}\n * for more information. The {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin|origin}\n * is used as the unique identifier for the application, and it's used as\n * part of the key derivation process.\n *\n * @param address - The address of the account the app key is derived from.\n * @param origin - The origin of the application.\n * @returns The address of the app key for the given account and origin.\n */\n getAppKeyAddress?(address: Hex, origin: string): Promise<Hex>;\n\n /**\n * Sign a transaction. This is equivalent to the `eth_signTransaction`\n * Ethereum JSON-RPC method. See the Ethereum JSON-RPC API documentation for\n * more details.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param transaction - The transaction to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed transaction.\n */\n signTransaction?(\n address: Hex,\n transaction: TypedTransaction,\n options?: Record<string, unknown>,\n ): Promise<TxData>;\n\n /**\n * Sign a message. This is equivalent to an older version of the the\n * `eth_sign` Ethereum JSON-RPC method. The message is signed using ECDSA,\n * using the curve secp256k1 the Keccak-256 hash function.\n *\n * For more information about this method and why we still support it, see\n * the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signMessage?(\n address: Hex,\n message: string,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Sign a message. This is equivalent to the `eth_sign` Ethereum JSON-RPC\n * method, which is exposed by MetaMask as the method `personal_sign`. See\n * the Ethereum JSON-RPC API documentation for more details.\n *\n * For more information about this method and why we call it `personal_sign`,\n * see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signPersonalMessage?(\n address: Hex,\n message: Hex,\n options?: { version?: string } & Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Sign a message. This is equivalent to the `eth_signTypedData` Ethereum\n * JSON-RPC method. See {@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md|EIP-712}\n * for more details.\n *\n * The \"version\" option dictates which version of `eth_signTypedData` is\n * used. The latest version reflects the specification most closely, whereas\n * earlier versions reflect earlier drafts of the specification that are\n * still supported for backwards-compatibility reasons. For more information\n * about why we support multiple versions, see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept additional options as well. See the documentation for\n * the specific keyring for more information about what these options are.\n * For some keyrings, the options parameter can even change which key is used\n * for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param typedData - The data to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signTypedData?(\n address: Hex,\n typedData: Record<string, unknown>,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Get a public key to use for encryption. This is equivalent to the\n * ` eth_getEncryptionPublicKey` JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-getencryptionpublickey|MetaMask Docs}\n * for more information.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used (e.g. encrypting with app keys).\n *\n * @param account - The address of the account you want the encryption key for.\n * @param options - Options; differs between keyrings.\n */\n getEncryptionPublicKey?(\n account: Hex,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Decrypt an encrypted message. This is equivalent to the ` eth_decrypt`\n * JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-decrypt|MetaMask Docs}\n * for more information.\n *\n * @param account - The address of the account you want to use to decrypt\n * the message.\n * @param encryptedData - The encrypted data that you want to decrypt.\n * @returns The decrypted data.\n */\n decryptMessage?(\n account: Hex,\n encryptedData: Eip1024EncryptedData,\n ): Promise<string>;\n\n /**\n * Generates the properties for the keyring based on the given\n * BIP39-compliant mnemonic.\n *\n * @returns A promise resolving when the keyring has generated the properties.\n */\n generateRandomMnemonic?(): Promise<void>;\n\n /**\n * Destroy the keyring.\n */\n destroy?(): Promise<void>;\n};\n"]}
|
package/dist/keyring.d.cts
CHANGED
|
@@ -214,8 +214,10 @@ export type Keyring<State extends Json> = {
|
|
|
214
214
|
/**
|
|
215
215
|
* Generates the properties for the keyring based on the given
|
|
216
216
|
* BIP39-compliant mnemonic.
|
|
217
|
+
*
|
|
218
|
+
* @returns A promise resolving when the keyring has generated the properties.
|
|
217
219
|
*/
|
|
218
|
-
generateRandomMnemonic?(): void
|
|
220
|
+
generateRandomMnemonic?(): Promise<void>;
|
|
219
221
|
/**
|
|
220
222
|
* Destroy the keyring.
|
|
221
223
|
*/
|
package/dist/keyring.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyring.d.cts","sourceRoot":"","sources":["../src/keyring.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,EAAE,uBAAuB;AAE/D,OAAO,KAAK,EAAE,oBAAoB,EAAE,+BAA2B;AAC/D,OAAO,KAAK,EAAE,GAAG,EAAE,kBAAc;AACjC,OAAO,KAAK,EAAE,IAAI,EAAE,mBAAe;AAEnC;;;;;;GAMG;AACH,MAAM,MAAM,YAAY,CAAC,KAAK,SAAS,IAAI,IAAI;IAC7C;;;;;;;OAOG;IACH,KAAK,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;IAExD;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,OAAO,CAAC,KAAK,SAAS,IAAI,IAAI;IACxC;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;;OAIG;IACH,WAAW,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;IAE9B;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AO,EAAE,GAAG,EACZ,OAAO,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACvD,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnB;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,aAAa,CAAC,CACZ,OAAO,EAAE,GAAG,EACZ,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAClC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnB;;;;;;;;;;;;OAYG;IACH,sBAAsB,CAAC,CACrB,OAAO,EAAE,GAAG,EACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnB;;;;;;;;;OASG;IACH,cAAc,CAAC,CACb,OAAO,EAAE,GAAG,EACZ,aAAa,EAAE,oBAAoB,GAClC,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnB;;;;;OAKG;IACH,sBAAsB,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzC;;OAEG;IACH,OAAO,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B,CAAC"}
|
package/dist/keyring.d.mts
CHANGED
|
@@ -214,8 +214,10 @@ export type Keyring<State extends Json> = {
|
|
|
214
214
|
/**
|
|
215
215
|
* Generates the properties for the keyring based on the given
|
|
216
216
|
* BIP39-compliant mnemonic.
|
|
217
|
+
*
|
|
218
|
+
* @returns A promise resolving when the keyring has generated the properties.
|
|
217
219
|
*/
|
|
218
|
-
generateRandomMnemonic?(): void
|
|
220
|
+
generateRandomMnemonic?(): Promise<void>;
|
|
219
221
|
/**
|
|
220
222
|
* Destroy the keyring.
|
|
221
223
|
*/
|
package/dist/keyring.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1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GAChC,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnB;;;;;;;;;OASG;IACH,cAAc,CAAC,CACb,OAAO,EAAE,GAAG,EACZ,aAAa,EAAE,oBAAoB,GAClC,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnB;;;;;OAKG;IACH,sBAAsB,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzC;;OAEG;IACH,OAAO,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B,CAAC"}
|
package/dist/keyring.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyring.mjs","sourceRoot":"","sources":["../src/keyring.ts"],"names":[],"mappings":"","sourcesContent":["import type { TypedTransaction, TxData } from '@ethereumjs/tx';\n\nimport type { Eip1024EncryptedData } from './encryption-types';\nimport type { Hex } from './hex';\nimport type { Json } from './json';\n\n/**\n * A Keyring class.\n *\n * This type is used to validate the constructor signature and the `type`\n * static property on Keyring classes. See the {@link Keyring} type for more\n * information.\n */\nexport type KeyringClass<State extends Json> = {\n /**\n * The Keyring constructor. Takes a single parameter, an \"options\" object.\n * See the documentation for the specific keyring for more information about\n * what these options are.\n *\n * @param options - The constructor options. Differs between keyring\n * implementations.\n */\n new (options?: Record<string, unknown>): Keyring<State>;\n\n /**\n * The name of this type of keyring. This must uniquely identify the\n * keyring type.\n */\n type: string;\n};\n\n/**\n * A keyring is something that can sign messages. Keyrings are used to add new\n * signing strategies; each strategy is a new keyring.\n *\n * Each keyring manages a collection of key pairs, which we call \"accounts\".\n * Each account is referred to by its \"address\", which is a unique identifier\n * derived from the public key. The address is always a \"0x\"-prefixed\n * hexidecimal string.\n *\n * The keyring might store the private key for each account as well, but it's\n * not guaranteed. Some keyrings delegate signing, so they don't need the\n * private key directly. The keyring (and in particular the keyring state)\n * should be treated with care though, just in case it does contain sensitive\n * material such as a private key.\n */\nexport type Keyring<State extends Json> = {\n /**\n * The name of this type of keyring. This must match the `type` property of\n * the keyring class.\n */\n type: string;\n\n /**\n * Get the addresses for all accounts in this keyring.\n *\n * @returns A list of the account addresses for this keyring\n */\n getAccounts(): Promise<Hex[]>;\n\n /**\n * Add an account to the keyring.\n *\n * @param number - The number of accounts to add. Usually defaults to 1.\n * @returns A list of the newly added account addresses.\n */\n addAccounts(number: number): Promise<Hex[]>;\n\n /**\n * Serialize the keyring state as a JSON-serializable object.\n *\n * @returns A JSON-serializable representation of the keyring state.\n */\n serialize(): Promise<State>;\n\n /**\n * Deserialize the given keyring state, overwriting any existing state with\n * the serialized state provided.\n *\n * @param state - A JSON-serializable representation of the keyring state.\n */\n deserialize(state: State): Promise<void>;\n\n /**\n * Method to include asynchronous configuration.\n */\n init?(): Promise<void>;\n\n /**\n * Remove an account from the keyring.\n *\n * @param address - The address of the account to remove.\n */\n removeAccount?(address: Hex): void;\n\n /**\n * Export the private key for one of the keyring accounts.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter is used to allow exporting a\n * private key that is derived from the given account, rather than exporting\n * that account's private key directly.\n *\n * @param address - The address of the account to export.\n * @param options - Export options; differs between keyrings.\n * @returns The non-prefixed, hex-encoded private key that was requested.\n */\n exportAccount?(\n address: Hex,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Get the \"app key\" address for the given account and origin. An app key is\n * an application-specific key pair. See {@link https://eips.ethereum.org/EIPS/eip-1775|EIP-1775}\n * for more information. The {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin|origin}\n * is used as the unique identifier for the application, and it's used as\n * part of the key derivation process.\n *\n * @param address - The address of the account the app key is derived from.\n * @param origin - The origin of the application.\n * @returns The address of the app key for the given account and origin.\n */\n getAppKeyAddress?(address: Hex, origin: string): Promise<Hex>;\n\n /**\n * Sign a transaction. This is equivalent to the `eth_signTransaction`\n * Ethereum JSON-RPC method. See the Ethereum JSON-RPC API documentation for\n * more details.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param transaction - The transaction to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed transaction.\n */\n signTransaction?(\n address: Hex,\n transaction: TypedTransaction,\n options?: Record<string, unknown>,\n ): Promise<TxData>;\n\n /**\n * Sign a message. This is equivalent to an older version of the the\n * `eth_sign` Ethereum JSON-RPC method. The message is signed using ECDSA,\n * using the curve secp256k1 the Keccak-256 hash function.\n *\n * For more information about this method and why we still support it, see\n * the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signMessage?(\n address: Hex,\n message: string,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Sign a message. This is equivalent to the `eth_sign` Ethereum JSON-RPC\n * method, which is exposed by MetaMask as the method `personal_sign`. See\n * the Ethereum JSON-RPC API documentation for more details.\n *\n * For more information about this method and why we call it `personal_sign`,\n * see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signPersonalMessage?(\n address: Hex,\n message: Hex,\n options?: { version?: string } & Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Sign a message. This is equivalent to the `eth_signTypedData` Ethereum\n * JSON-RPC method. See {@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md|EIP-712}\n * for more details.\n *\n * The \"version\" option dictates which version of `eth_signTypedData` is\n * used. The latest version reflects the specification most closely, whereas\n * earlier versions reflect earlier drafts of the specification that are\n * still supported for backwards-compatibility reasons. For more information\n * about why we support multiple versions, see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept additional options as well. See the documentation for\n * the specific keyring for more information about what these options are.\n * For some keyrings, the options parameter can even change which key is used\n * for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param typedData - The data to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signTypedData?(\n address: Hex,\n typedData: Record<string, unknown>,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Get a public key to use for encryption. This is equivalent to the\n * ` eth_getEncryptionPublicKey` JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-getencryptionpublickey|MetaMask Docs}\n * for more information.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used (e.g. encrypting with app keys).\n *\n * @param account - The address of the account you want the encryption key for.\n * @param options - Options; differs between keyrings.\n */\n getEncryptionPublicKey?(\n account: Hex,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Decrypt an encrypted message. This is equivalent to the ` eth_decrypt`\n * JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-decrypt|MetaMask Docs}\n * for more information.\n *\n * @param account - The address of the account you want to use to decrypt\n * the message.\n * @param encryptedData - The encrypted data that you want to decrypt.\n * @returns The decrypted data.\n */\n decryptMessage?(\n account: Hex,\n encryptedData: Eip1024EncryptedData,\n ): Promise<string>;\n\n /**\n * Generates the properties for the keyring based on the given\n * BIP39-compliant mnemonic.\n */\n generateRandomMnemonic?(): void;\n\n /**\n * Destroy the keyring.\n */\n destroy?(): Promise<void>;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"keyring.mjs","sourceRoot":"","sources":["../src/keyring.ts"],"names":[],"mappings":"","sourcesContent":["import type { TypedTransaction, TxData } from '@ethereumjs/tx';\n\nimport type { Eip1024EncryptedData } from './encryption-types';\nimport type { Hex } from './hex';\nimport type { Json } from './json';\n\n/**\n * A Keyring class.\n *\n * This type is used to validate the constructor signature and the `type`\n * static property on Keyring classes. See the {@link Keyring} type for more\n * information.\n */\nexport type KeyringClass<State extends Json> = {\n /**\n * The Keyring constructor. Takes a single parameter, an \"options\" object.\n * See the documentation for the specific keyring for more information about\n * what these options are.\n *\n * @param options - The constructor options. Differs between keyring\n * implementations.\n */\n new (options?: Record<string, unknown>): Keyring<State>;\n\n /**\n * The name of this type of keyring. This must uniquely identify the\n * keyring type.\n */\n type: string;\n};\n\n/**\n * A keyring is something that can sign messages. Keyrings are used to add new\n * signing strategies; each strategy is a new keyring.\n *\n * Each keyring manages a collection of key pairs, which we call \"accounts\".\n * Each account is referred to by its \"address\", which is a unique identifier\n * derived from the public key. The address is always a \"0x\"-prefixed\n * hexidecimal string.\n *\n * The keyring might store the private key for each account as well, but it's\n * not guaranteed. Some keyrings delegate signing, so they don't need the\n * private key directly. The keyring (and in particular the keyring state)\n * should be treated with care though, just in case it does contain sensitive\n * material such as a private key.\n */\nexport type Keyring<State extends Json> = {\n /**\n * The name of this type of keyring. This must match the `type` property of\n * the keyring class.\n */\n type: string;\n\n /**\n * Get the addresses for all accounts in this keyring.\n *\n * @returns A list of the account addresses for this keyring\n */\n getAccounts(): Promise<Hex[]>;\n\n /**\n * Add an account to the keyring.\n *\n * @param number - The number of accounts to add. Usually defaults to 1.\n * @returns A list of the newly added account addresses.\n */\n addAccounts(number: number): Promise<Hex[]>;\n\n /**\n * Serialize the keyring state as a JSON-serializable object.\n *\n * @returns A JSON-serializable representation of the keyring state.\n */\n serialize(): Promise<State>;\n\n /**\n * Deserialize the given keyring state, overwriting any existing state with\n * the serialized state provided.\n *\n * @param state - A JSON-serializable representation of the keyring state.\n */\n deserialize(state: State): Promise<void>;\n\n /**\n * Method to include asynchronous configuration.\n */\n init?(): Promise<void>;\n\n /**\n * Remove an account from the keyring.\n *\n * @param address - The address of the account to remove.\n */\n removeAccount?(address: Hex): void;\n\n /**\n * Export the private key for one of the keyring accounts.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter is used to allow exporting a\n * private key that is derived from the given account, rather than exporting\n * that account's private key directly.\n *\n * @param address - The address of the account to export.\n * @param options - Export options; differs between keyrings.\n * @returns The non-prefixed, hex-encoded private key that was requested.\n */\n exportAccount?(\n address: Hex,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Get the \"app key\" address for the given account and origin. An app key is\n * an application-specific key pair. See {@link https://eips.ethereum.org/EIPS/eip-1775|EIP-1775}\n * for more information. The {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin|origin}\n * is used as the unique identifier for the application, and it's used as\n * part of the key derivation process.\n *\n * @param address - The address of the account the app key is derived from.\n * @param origin - The origin of the application.\n * @returns The address of the app key for the given account and origin.\n */\n getAppKeyAddress?(address: Hex, origin: string): Promise<Hex>;\n\n /**\n * Sign a transaction. This is equivalent to the `eth_signTransaction`\n * Ethereum JSON-RPC method. See the Ethereum JSON-RPC API documentation for\n * more details.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param transaction - The transaction to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed transaction.\n */\n signTransaction?(\n address: Hex,\n transaction: TypedTransaction,\n options?: Record<string, unknown>,\n ): Promise<TxData>;\n\n /**\n * Sign a message. This is equivalent to an older version of the the\n * `eth_sign` Ethereum JSON-RPC method. The message is signed using ECDSA,\n * using the curve secp256k1 the Keccak-256 hash function.\n *\n * For more information about this method and why we still support it, see\n * the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signMessage?(\n address: Hex,\n message: string,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Sign a message. This is equivalent to the `eth_sign` Ethereum JSON-RPC\n * method, which is exposed by MetaMask as the method `personal_sign`. See\n * the Ethereum JSON-RPC API documentation for more details.\n *\n * For more information about this method and why we call it `personal_sign`,\n * see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param message - The message to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signPersonalMessage?(\n address: Hex,\n message: Hex,\n options?: { version?: string } & Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Sign a message. This is equivalent to the `eth_signTypedData` Ethereum\n * JSON-RPC method. See {@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md|EIP-712}\n * for more details.\n *\n * The \"version\" option dictates which version of `eth_signTypedData` is\n * used. The latest version reflects the specification most closely, whereas\n * earlier versions reflect earlier drafts of the specification that are\n * still supported for backwards-compatibility reasons. For more information\n * about why we support multiple versions, see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}.\n *\n * Some keyrings accept additional options as well. See the documentation for\n * the specific keyring for more information about what these options are.\n * For some keyrings, the options parameter can even change which key is used\n * for signing (e.g. signing with app keys).\n *\n * @param address - The address of the account to use for signing.\n * @param typedData - The data to sign.\n * @param options - Signing options; differs between keyrings.\n * @returns The signed message.\n */\n signTypedData?(\n address: Hex,\n typedData: Record<string, unknown>,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Get a public key to use for encryption. This is equivalent to the\n * ` eth_getEncryptionPublicKey` JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-getencryptionpublickey|MetaMask Docs}\n * for more information.\n *\n * Some keyrings accept an \"options\" parameter as well. See the documentation\n * for the specific keyring for more information about what these options\n * are. For some keyrings, the options parameter can even change which key is\n * used (e.g. encrypting with app keys).\n *\n * @param account - The address of the account you want the encryption key for.\n * @param options - Options; differs between keyrings.\n */\n getEncryptionPublicKey?(\n account: Hex,\n options?: Record<string, unknown>,\n ): Promise<string>;\n\n /**\n * Decrypt an encrypted message. This is equivalent to the ` eth_decrypt`\n * JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-decrypt|MetaMask Docs}\n * for more information.\n *\n * @param account - The address of the account you want to use to decrypt\n * the message.\n * @param encryptedData - The encrypted data that you want to decrypt.\n * @returns The decrypted data.\n */\n decryptMessage?(\n account: Hex,\n encryptedData: Eip1024EncryptedData,\n ): Promise<string>;\n\n /**\n * Generates the properties for the keyring based on the given\n * BIP39-compliant mnemonic.\n *\n * @returns A promise resolving when the keyring has generated the properties.\n */\n generateRandomMnemonic?(): Promise<void>;\n\n /**\n * Destroy the keyring.\n */\n destroy?(): Promise<void>;\n};\n"]}
|
package/package.json
CHANGED