@metamask/utils 6.2.0 → 7.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -1
- package/dist/cjs/assert.js +116 -0
- package/dist/cjs/assert.js.map +1 -0
- package/dist/cjs/base64.js +32 -0
- package/dist/cjs/base64.js.map +1 -0
- package/dist/cjs/bytes.js +255 -0
- package/dist/cjs/bytes.js.map +1 -0
- package/dist/cjs/caip-types.js +115 -0
- package/dist/cjs/caip-types.js.map +1 -0
- package/dist/cjs/checksum.js +17 -0
- package/dist/cjs/checksum.js.map +1 -0
- package/dist/cjs/coercers.js +96 -0
- package/dist/cjs/coercers.js.map +1 -0
- package/dist/cjs/collections.js +157 -0
- package/dist/cjs/collections.js.map +1 -0
- package/dist/cjs/encryption-types.js +6 -0
- package/dist/cjs/encryption-types.js.map +1 -0
- package/dist/cjs/hex.js +107 -0
- package/dist/cjs/hex.js.map +1 -0
- package/dist/cjs/index.js +37 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/json.js +263 -0
- package/dist/cjs/json.js.map +1 -0
- package/dist/cjs/keyring.js +6 -0
- package/dist/cjs/keyring.js.map +1 -0
- package/dist/cjs/logging.js +33 -0
- package/dist/cjs/logging.js.map +1 -0
- package/dist/cjs/misc.js +111 -0
- package/dist/cjs/misc.js.map +1 -0
- package/dist/cjs/number.js +52 -0
- package/dist/cjs/number.js.map +1 -0
- package/dist/cjs/opaque.js +8 -0
- package/dist/cjs/opaque.js.map +1 -0
- package/dist/cjs/time.js +63 -0
- package/dist/cjs/time.js.map +1 -0
- package/dist/cjs/transaction-types.js +6 -0
- package/dist/cjs/transaction-types.js.map +1 -0
- package/dist/cjs/versions.js +79 -0
- package/dist/cjs/versions.js.map +1 -0
- package/dist/{assert.js → esm/assert.js} +30 -35
- package/dist/esm/assert.js.map +1 -0
- package/dist/{base64.js → esm/base64.js} +10 -16
- package/dist/esm/base64.js.map +1 -0
- package/dist/{bytes.js → esm/bytes.js} +50 -93
- package/dist/esm/bytes.js.map +1 -0
- package/dist/esm/caip-types.js +99 -0
- package/dist/esm/caip-types.js.map +1 -0
- package/dist/esm/checksum.js +7 -0
- package/dist/esm/checksum.js.map +1 -0
- package/dist/{coercers.js → esm/coercers.js} +43 -52
- package/dist/esm/coercers.js.map +1 -0
- package/dist/esm/collections.js +140 -0
- package/dist/esm/collections.js.map +1 -0
- package/dist/esm/encryption-types.js +3 -0
- package/dist/esm/encryption-types.js.map +1 -0
- package/dist/esm/hex.js +108 -0
- package/dist/esm/hex.js.map +1 -0
- package/dist/{index.d.ts → esm/index.js} +3 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/json.js +301 -0
- package/dist/esm/json.js.map +1 -0
- package/dist/esm/keyring.js +3 -0
- package/dist/esm/keyring.js.map +1 -0
- package/dist/{logging.js → esm/logging.js} +5 -14
- package/dist/esm/logging.js.map +1 -0
- package/dist/{misc.js → esm/misc.js} +37 -42
- package/dist/esm/misc.js.map +1 -0
- package/dist/{number.js → esm/number.js} +18 -28
- package/dist/esm/number.js.map +1 -0
- package/dist/esm/opaque.js +5 -0
- package/dist/esm/opaque.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/time.js +53 -0
- package/dist/esm/time.js.map +1 -0
- package/dist/esm/transaction-types.js +3 -0
- package/dist/esm/transaction-types.js.map +1 -0
- package/dist/esm/versions.js +78 -0
- package/dist/esm/versions.js.map +1 -0
- package/dist/{assert.d.ts → types/assert.d.ts} +3 -2
- package/dist/types/assert.d.ts.map +1 -0
- package/dist/{base64.d.ts → types/base64.d.ts} +3 -2
- package/dist/types/base64.d.ts.map +1 -0
- package/dist/{bytes.d.ts → types/bytes.d.ts} +2 -1
- package/dist/types/bytes.d.ts.map +1 -0
- package/dist/types/caip-types.d.ts +93 -0
- package/dist/types/caip-types.d.ts.map +1 -0
- package/dist/types/caip-types.test-d.d.ts +2 -0
- package/dist/types/caip-types.test-d.d.ts.map +1 -0
- package/dist/{checksum.d.ts → types/checksum.d.ts} +1 -0
- package/dist/types/checksum.d.ts.map +1 -0
- package/dist/{coercers.d.ts → types/coercers.d.ts} +3 -2
- package/dist/types/coercers.d.ts.map +1 -0
- package/dist/{collections.d.ts → types/collections.d.ts} +1 -0
- package/dist/types/collections.d.ts.map +1 -0
- package/dist/{encryption-types.d.ts → types/encryption-types.d.ts} +1 -0
- package/dist/types/encryption-types.d.ts.map +1 -0
- package/dist/{hex.d.ts → types/hex.d.ts} +2 -1
- package/dist/types/hex.d.ts.map +1 -0
- package/dist/types/hex.test-d.d.ts +2 -0
- package/dist/types/hex.test-d.d.ts.map +1 -0
- package/dist/types/index.d.ts +19 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/{json.d.ts → types/json.d.ts} +3 -2
- package/dist/types/json.d.ts.map +1 -0
- package/dist/types/json.test-d.d.ts +2 -0
- package/dist/types/json.test-d.d.ts.map +1 -0
- package/dist/{keyring.d.ts → types/keyring.d.ts} +3 -2
- package/dist/types/keyring.d.ts.map +1 -0
- package/dist/{logging.d.ts → types/logging.d.ts} +2 -1
- package/dist/types/logging.d.ts.map +1 -0
- package/dist/{misc.d.ts → types/misc.d.ts} +13 -0
- package/dist/types/misc.d.ts.map +1 -0
- package/dist/types/misc.test-d.d.ts +2 -0
- package/dist/types/misc.test-d.d.ts.map +1 -0
- package/dist/{number.d.ts → types/number.d.ts} +1 -0
- package/dist/types/number.d.ts.map +1 -0
- package/dist/{opaque.d.ts → types/opaque.d.ts} +1 -0
- package/dist/types/opaque.d.ts.map +1 -0
- package/dist/{time.d.ts → types/time.d.ts} +1 -0
- package/dist/types/time.d.ts.map +1 -0
- package/dist/{transaction-types.d.ts → types/transaction-types.d.ts} +3 -2
- package/dist/types/transaction-types.d.ts.map +1 -0
- package/dist/{versions.d.ts → types/versions.d.ts} +3 -2
- package/dist/types/versions.d.ts.map +1 -0
- package/package.json +40 -20
- package/dist/__fixtures__/bytes.d.ts +0 -25
- package/dist/__fixtures__/bytes.js +0 -234
- package/dist/__fixtures__/bytes.js.map +0 -1
- package/dist/__fixtures__/coercions.d.ts +0 -5
- package/dist/__fixtures__/coercions.js +0 -22
- package/dist/__fixtures__/coercions.js.map +0 -1
- package/dist/__fixtures__/index.d.ts +0 -4
- package/dist/__fixtures__/index.js +0 -21
- package/dist/__fixtures__/index.js.map +0 -1
- package/dist/__fixtures__/json.d.ts +0 -1226
- package/dist/__fixtures__/json.js +0 -1491
- package/dist/__fixtures__/json.js.map +0 -1
- package/dist/__fixtures__/numbers.d.ts +0 -5
- package/dist/__fixtures__/numbers.js +0 -56
- package/dist/__fixtures__/numbers.js.map +0 -1
- package/dist/assert.js.map +0 -1
- package/dist/base64.js.map +0 -1
- package/dist/bytes.js.map +0 -1
- package/dist/checksum.js +0 -7
- package/dist/checksum.js.map +0 -1
- package/dist/coercers.js.map +0 -1
- package/dist/collections.js +0 -109
- package/dist/collections.js.map +0 -1
- package/dist/encryption-types.js +0 -3
- package/dist/encryption-types.js.map +0 -1
- package/dist/hex.js +0 -134
- package/dist/hex.js.map +0 -1
- package/dist/hex.test-d.d.ts +0 -1
- package/dist/hex.test-d.js +0 -16
- package/dist/hex.test-d.js.map +0 -1
- package/dist/index.js +0 -34
- package/dist/index.js.map +0 -1
- package/dist/json.js +0 -346
- package/dist/json.js.map +0 -1
- package/dist/json.test-d.d.ts +0 -1
- package/dist/json.test-d.js +0 -62
- package/dist/json.test-d.js.map +0 -1
- package/dist/keyring.js +0 -3
- package/dist/keyring.js.map +0 -1
- package/dist/logging.js.map +0 -1
- package/dist/misc.js.map +0 -1
- package/dist/misc.test-d.d.ts +0 -1
- package/dist/misc.test-d.js +0 -97
- package/dist/misc.test-d.js.map +0 -1
- package/dist/number.js.map +0 -1
- package/dist/opaque.js +0 -3
- package/dist/opaque.js.map +0 -1
- package/dist/time.js +0 -67
- package/dist/time.js.map +0 -1
- package/dist/transaction-types.js +0 -3
- package/dist/transaction-types.js.map +0 -1
- package/dist/versions.js +0 -95
- package/dist/versions.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/bytes.ts"],"sourcesContent":["import { assert } from './assert';\nimport type { Hex } from './hex';\nimport { add0x, assertIsHexString, remove0x } from './hex';\n\n// '0'.charCodeAt(0) === 48\nconst HEX_MINIMUM_NUMBER_CHARACTER = 48;\n\n// '9'.charCodeAt(0) === 57\nconst HEX_MAXIMUM_NUMBER_CHARACTER = 58;\nconst HEX_CHARACTER_OFFSET = 87;\n\nexport type Bytes = bigint | number | string | Uint8Array;\n\n/**\n * Memoized function that returns an array to be used as a lookup table for\n * converting bytes to hexadecimal values.\n *\n * The array is created lazily and then cached for future use. The benefit of\n * this approach is that the performance of converting bytes to hex is much\n * better than if we were to call `toString(16)` on each byte.\n *\n * The downside is that the array is created once and then never garbage\n * collected. This is not a problem in practice because the array is only 256\n * elements long.\n *\n * @returns A function that returns the lookup table.\n */\nfunction getPrecomputedHexValuesBuilder(): () => string[] {\n // To avoid issues with tree shaking, we need to use a function to return the\n // array. This is because the array is only used in the `bytesToHex` function\n // and if we were to use a global variable, the array might be removed by the\n // tree shaker.\n const lookupTable: string[] = [];\n\n return () => {\n if (lookupTable.length === 0) {\n for (let i = 0; i < 256; i++) {\n lookupTable.push(i.toString(16).padStart(2, '0'));\n }\n }\n\n return lookupTable;\n };\n}\n\n/**\n * Function implementation of the {@link getPrecomputedHexValuesBuilder}\n * function.\n */\nconst getPrecomputedHexValues = getPrecomputedHexValuesBuilder();\n\n/**\n * Check if a value is a `Uint8Array`.\n *\n * @param value - The value to check.\n * @returns Whether the value is a `Uint8Array`.\n */\nexport function isBytes(value: unknown): value is Uint8Array {\n return value instanceof Uint8Array;\n}\n\n/**\n * Assert that a value is a `Uint8Array`.\n *\n * @param value - The value to check.\n * @throws If the value is not a `Uint8Array`.\n */\nexport function assertIsBytes(value: unknown): asserts value is Uint8Array {\n assert(isBytes(value), 'Value must be a Uint8Array.');\n}\n\n/**\n * Convert a `Uint8Array` to a hexadecimal string.\n *\n * @param bytes - The bytes to convert to a hexadecimal string.\n * @returns The hexadecimal string.\n */\nexport function bytesToHex(bytes: Uint8Array): Hex {\n assertIsBytes(bytes);\n\n if (bytes.length === 0) {\n return '0x';\n }\n\n const lookupTable = getPrecomputedHexValues();\n const hexadecimal = new Array(bytes.length);\n\n for (let i = 0; i < bytes.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n hexadecimal[i] = lookupTable[bytes[i]!];\n }\n\n return add0x(hexadecimal.join(''));\n}\n\n/**\n * Convert a `Uint8Array` to a `bigint`.\n *\n * To convert a `Uint8Array` to a `number` instead, use {@link bytesToNumber}.\n * To convert a two's complement encoded `Uint8Array` to a `bigint`, use\n * {@link bytesToSignedBigInt}.\n *\n * @param bytes - The bytes to convert to a `bigint`.\n * @returns The `bigint`.\n */\nexport function bytesToBigInt(bytes: Uint8Array): bigint {\n assertIsBytes(bytes);\n\n const hexadecimal = bytesToHex(bytes);\n return BigInt(hexadecimal);\n}\n\n/**\n * Convert a `Uint8Array` to a signed `bigint`. This assumes that the bytes are\n * encoded in two's complement.\n *\n * To convert a `Uint8Array` to an unsigned `bigint` instead, use\n * {@link bytesToBigInt}.\n *\n * @see https://en.wikipedia.org/wiki/Two%27s_complement\n * @param bytes - The bytes to convert to a signed `bigint`.\n * @returns The signed `bigint`.\n */\nexport function bytesToSignedBigInt(bytes: Uint8Array): bigint {\n assertIsBytes(bytes);\n\n let value = BigInt(0);\n for (const byte of bytes) {\n // eslint-disable-next-line no-bitwise\n value = (value << BigInt(8)) + BigInt(byte);\n }\n\n return BigInt.asIntN(bytes.length * 8, value);\n}\n\n/**\n * Convert a `Uint8Array` to a `number`.\n *\n * To convert a `Uint8Array` to a `bigint` instead, use {@link bytesToBigInt}.\n *\n * @param bytes - The bytes to convert to a number.\n * @returns The number.\n * @throws If the resulting number is not a safe integer.\n */\nexport function bytesToNumber(bytes: Uint8Array): number {\n assertIsBytes(bytes);\n\n const bigint = bytesToBigInt(bytes);\n\n assert(\n bigint <= BigInt(Number.MAX_SAFE_INTEGER),\n 'Number is not a safe integer. Use `bytesToBigInt` instead.',\n );\n\n return Number(bigint);\n}\n\n/**\n * Convert a UTF-8 encoded `Uint8Array` to a `string`.\n *\n * @param bytes - The bytes to convert to a string.\n * @returns The string.\n */\nexport function bytesToString(bytes: Uint8Array): string {\n assertIsBytes(bytes);\n\n return new TextDecoder().decode(bytes);\n}\n\n/**\n * Convert a hexadecimal string to a `Uint8Array`. The string can optionally be\n * prefixed with `0x`. It accepts even and odd length strings.\n *\n * If the value is \"0x\", an empty `Uint8Array` is returned.\n *\n * @param value - The hexadecimal string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function hexToBytes(value: string): Uint8Array {\n // \"0x\" is often used as empty byte array.\n if (value?.toLowerCase?.() === '0x') {\n return new Uint8Array();\n }\n\n assertIsHexString(value);\n\n // Remove the `0x` prefix if it exists, and pad the string to have an even\n // number of characters.\n const strippedValue = remove0x(value).toLowerCase();\n const normalizedValue =\n strippedValue.length % 2 === 0 ? strippedValue : `0${strippedValue}`;\n const bytes = new Uint8Array(normalizedValue.length / 2);\n\n for (let i = 0; i < bytes.length; i++) {\n // While this is not the prettiest way to convert a hexadecimal string to a\n // `Uint8Array`, it is a lot faster than using `parseInt` to convert each\n // character.\n const c1 = normalizedValue.charCodeAt(i * 2);\n const c2 = normalizedValue.charCodeAt(i * 2 + 1);\n const n1 =\n c1 -\n (c1 < HEX_MAXIMUM_NUMBER_CHARACTER\n ? HEX_MINIMUM_NUMBER_CHARACTER\n : HEX_CHARACTER_OFFSET);\n const n2 =\n c2 -\n (c2 < HEX_MAXIMUM_NUMBER_CHARACTER\n ? HEX_MINIMUM_NUMBER_CHARACTER\n : HEX_CHARACTER_OFFSET);\n\n bytes[i] = n1 * 16 + n2;\n }\n\n return bytes;\n}\n\n/**\n * Convert a `bigint` to a `Uint8Array`.\n *\n * This assumes that the `bigint` is an unsigned integer. To convert a signed\n * `bigint` instead, use {@link signedBigIntToBytes}.\n *\n * @param value - The bigint to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function bigIntToBytes(value: bigint): Uint8Array {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(value >= BigInt(0), 'Value must be a non-negative bigint.');\n\n const hexadecimal = value.toString(16);\n return hexToBytes(hexadecimal);\n}\n\n/**\n * Check if a `bigint` fits in a certain number of bytes.\n *\n * @param value - The `bigint` to check.\n * @param bytes - The number of bytes.\n * @returns Whether the `bigint` fits in the number of bytes.\n */\nfunction bigIntFits(value: bigint, bytes: number): boolean {\n assert(bytes > 0);\n\n /* eslint-disable no-bitwise */\n const mask = value >> BigInt(31);\n return !(((~value & mask) + (value & ~mask)) >> BigInt(bytes * 8 + ~0));\n /* eslint-enable no-bitwise */\n}\n\n/**\n * Convert a signed `bigint` to a `Uint8Array`. This uses two's complement\n * encoding to represent negative numbers.\n *\n * To convert an unsigned `bigint` to a `Uint8Array` instead, use\n * {@link bigIntToBytes}.\n *\n * @see https://en.wikipedia.org/wiki/Two%27s_complement\n * @param value - The number to convert to bytes.\n * @param byteLength - The length of the resulting `Uint8Array`. If the number\n * is larger than the maximum value that can be represented by the given length,\n * an error is thrown.\n * @returns The bytes as `Uint8Array`.\n */\nexport function signedBigIntToBytes(\n value: bigint,\n byteLength: number,\n): Uint8Array {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(typeof byteLength === 'number', 'Byte length must be a number.');\n assert(byteLength > 0, 'Byte length must be greater than 0.');\n assert(\n bigIntFits(value, byteLength),\n 'Byte length is too small to represent the given value.',\n );\n\n // ESLint doesn't like mutating function parameters, so to avoid having to\n // disable the rule, we create a new variable.\n let numberValue = value;\n const bytes = new Uint8Array(byteLength);\n\n for (let i = 0; i < bytes.length; i++) {\n bytes[i] = Number(BigInt.asUintN(8, numberValue));\n // eslint-disable-next-line no-bitwise\n numberValue >>= BigInt(8);\n }\n\n return bytes.reverse();\n}\n\n/**\n * Convert a `number` to a `Uint8Array`.\n *\n * @param value - The number to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n * @throws If the number is not a safe integer.\n */\nexport function numberToBytes(value: number): Uint8Array {\n assert(typeof value === 'number', 'Value must be a number.');\n assert(value >= 0, 'Value must be a non-negative number.');\n assert(\n Number.isSafeInteger(value),\n 'Value is not a safe integer. Use `bigIntToBytes` instead.',\n );\n\n const hexadecimal = value.toString(16);\n return hexToBytes(hexadecimal);\n}\n\n/**\n * Convert a `string` to a UTF-8 encoded `Uint8Array`.\n *\n * @param value - The string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function stringToBytes(value: string): Uint8Array {\n assert(typeof value === 'string', 'Value must be a string.');\n\n return new TextEncoder().encode(value);\n}\n\n/**\n * Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,\n * a `bigint`, a `number`, or a `string`.\n *\n * This will attempt to guess the type of the value based on its type and\n * contents. For more control over the conversion, use the more specific\n * conversion functions, such as {@link hexToBytes} or {@link stringToBytes}.\n *\n * If the value is a `string`, and it is prefixed with `0x`, it will be\n * interpreted as a hexadecimal string. Otherwise, it will be interpreted as a\n * UTF-8 string. To convert a hexadecimal string to bytes without interpreting\n * it as a UTF-8 string, use {@link hexToBytes} instead.\n *\n * If the value is a `bigint`, it is assumed to be unsigned. To convert a signed\n * `bigint` to bytes, use {@link signedBigIntToBytes} instead.\n *\n * If the value is a `Uint8Array`, it will be returned as-is.\n *\n * @param value - The value to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function valueToBytes(value: Bytes): Uint8Array {\n if (typeof value === 'bigint') {\n return bigIntToBytes(value);\n }\n\n if (typeof value === 'number') {\n return numberToBytes(value);\n }\n\n if (typeof value === 'string') {\n if (value.startsWith('0x')) {\n return hexToBytes(value);\n }\n\n return stringToBytes(value);\n }\n\n if (isBytes(value)) {\n return value;\n }\n\n throw new TypeError(`Unsupported value type: \"${typeof value}\".`);\n}\n\n/**\n * Concatenate multiple byte-like values into a single `Uint8Array`. The values\n * can be `Uint8Array`, `bigint`, `number`, or `string`. This uses\n * {@link valueToBytes} under the hood to convert each value to bytes. Refer to\n * the documentation of that function for more information.\n *\n * @param values - The values to concatenate.\n * @returns The concatenated bytes as `Uint8Array`.\n */\nexport function concatBytes(values: Bytes[]): Uint8Array {\n const normalizedValues = new Array(values.length);\n let byteLength = 0;\n\n for (let i = 0; i < values.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const value = valueToBytes(values[i]!);\n\n normalizedValues[i] = value;\n byteLength += value.length;\n }\n\n const bytes = new Uint8Array(byteLength);\n for (let i = 0, offset = 0; i < normalizedValues.length; i++) {\n // While we could simply spread the values into an array and use\n // `Uint8Array.from`, that is a lot slower than using `Uint8Array.set`.\n bytes.set(normalizedValues[i], offset);\n offset += normalizedValues[i].length;\n }\n\n return bytes;\n}\n\n/**\n * Create a {@link DataView} from a {@link Uint8Array}. This is a convenience\n * function that avoids having to create a {@link DataView} manually, which\n * requires passing the `byteOffset` and `byteLength` parameters every time.\n *\n * Not passing the `byteOffset` and `byteLength` parameters can result in\n * unexpected behavior when the {@link Uint8Array} is a view of a larger\n * {@link ArrayBuffer}, e.g., when using {@link Uint8Array.subarray}.\n *\n * This function also supports Node.js {@link Buffer}s.\n *\n * @example\n * ```typescript\n * const bytes = new Uint8Array([1, 2, 3]);\n *\n * // This is equivalent to:\n * // const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n * const dataView = createDataView(bytes);\n * ```\n * @param bytes - The bytes to create the {@link DataView} from.\n * @returns The {@link DataView}.\n */\nexport function createDataView(bytes: Uint8Array): DataView {\n // To maintain compatibility with Node.js, we need to check if the bytes are\n // a Buffer. If so, we need to slice the buffer to get the underlying\n // ArrayBuffer.\n // eslint-disable-next-line no-restricted-globals\n if (typeof Buffer !== 'undefined' && bytes instanceof Buffer) {\n const buffer = bytes.buffer.slice(\n bytes.byteOffset,\n bytes.byteOffset + bytes.byteLength,\n );\n\n return new DataView(buffer);\n }\n\n return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n}\n"],"names":["assert","add0x","assertIsHexString","remove0x","HEX_MINIMUM_NUMBER_CHARACTER","HEX_MAXIMUM_NUMBER_CHARACTER","HEX_CHARACTER_OFFSET","getPrecomputedHexValuesBuilder","lookupTable","length","i","push","toString","padStart","getPrecomputedHexValues","isBytes","value","Uint8Array","assertIsBytes","bytesToHex","bytes","hexadecimal","Array","join","bytesToBigInt","BigInt","bytesToSignedBigInt","byte","asIntN","bytesToNumber","bigint","Number","MAX_SAFE_INTEGER","bytesToString","TextDecoder","decode","hexToBytes","toLowerCase","strippedValue","normalizedValue","c1","charCodeAt","c2","n1","n2","bigIntToBytes","bigIntFits","mask","signedBigIntToBytes","byteLength","numberValue","asUintN","reverse","numberToBytes","isSafeInteger","stringToBytes","TextEncoder","encode","valueToBytes","startsWith","TypeError","concatBytes","values","normalizedValues","offset","set","createDataView","Buffer","buffer","slice","byteOffset","DataView"],"mappings":"AAAA,SAASA,MAAM,QAAQ,WAAW;AAElC,SAASC,KAAK,EAAEC,iBAAiB,EAAEC,QAAQ,QAAQ,QAAQ;AAE3D,2BAA2B;AAC3B,MAAMC,+BAA+B;AAErC,2BAA2B;AAC3B,MAAMC,+BAA+B;AACrC,MAAMC,uBAAuB;AAI7B;;;;;;;;;;;;;CAaC,GACD,SAASC;IACP,6EAA6E;IAC7E,6EAA6E;IAC7E,6EAA6E;IAC7E,eAAe;IACf,MAAMC,cAAwB,EAAE;IAEhC,OAAO;QACL,IAAIA,YAAYC,MAAM,KAAK,GAAG;YAC5B,IAAK,IAAIC,IAAI,GAAGA,IAAI,KAAKA,IAAK;gBAC5BF,YAAYG,IAAI,CAACD,EAAEE,QAAQ,CAAC,IAAIC,QAAQ,CAAC,GAAG;YAC9C;QACF;QAEA,OAAOL;IACT;AACF;AAEA;;;CAGC,GACD,MAAMM,0BAA0BP;AAEhC;;;;;CAKC,GACD,OAAO,SAASQ,QAAQC,KAAc;IACpC,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X = RegExp("^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$", "u");
|
|
3
|
+
export const CAIP_NAMESPACE_REGEX = /^[-a-z0-9]{3,8}$/u;
|
|
4
|
+
export const CAIP_REFERENCE_REGEX = /^[-_a-zA-Z0-9]{1,32}$/u;
|
|
5
|
+
export const CAIP_ACCOUNT_ID_REGEX = RegExp("^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})):(?<accountAddress>[-.%a-zA-Z0-9]{1,128})$", "u");
|
|
6
|
+
export const CAIP_ACCOUNT_ADDRESS_REGEX = /^[-.%a-zA-Z0-9]{1,128}$/u;
|
|
7
|
+
/**
|
|
8
|
+
* A CAIP-2 chain ID, i.e., a human-readable namespace and reference.
|
|
9
|
+
*/ export const CaipChainIdStruct = pattern(string(), CAIP_CHAIN_ID_REGEX);
|
|
10
|
+
/**
|
|
11
|
+
* A CAIP-2 namespace, i.e., the first part of a CAIP chain ID.
|
|
12
|
+
*/ export const CaipNamespaceStruct = pattern(string(), CAIP_NAMESPACE_REGEX);
|
|
13
|
+
/**
|
|
14
|
+
* A CAIP-2 reference, i.e., the second part of a CAIP chain ID.
|
|
15
|
+
*/ export const CaipReferenceStruct = pattern(string(), CAIP_REFERENCE_REGEX);
|
|
16
|
+
/**
|
|
17
|
+
* A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.
|
|
18
|
+
*/ export const CaipAccountIdStruct = pattern(string(), CAIP_ACCOUNT_ID_REGEX);
|
|
19
|
+
/**
|
|
20
|
+
* A CAIP-10 account address, i.e., the third part of the CAIP account ID.
|
|
21
|
+
*/ export const CaipAccountAddressStruct = pattern(string(), CAIP_ACCOUNT_ADDRESS_REGEX);
|
|
22
|
+
/**
|
|
23
|
+
* Check if the given value is a {@link CaipChainId}.
|
|
24
|
+
*
|
|
25
|
+
* @param value - The value to check.
|
|
26
|
+
* @returns Whether the value is a {@link CaipChainId}.
|
|
27
|
+
*/ export function isCaipChainId(value) {
|
|
28
|
+
return is(value, CaipChainIdStruct);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Check if the given value is a {@link CaipNamespace}.
|
|
32
|
+
*
|
|
33
|
+
* @param value - The value to check.
|
|
34
|
+
* @returns Whether the value is a {@link CaipNamespace}.
|
|
35
|
+
*/ export function isCaipNamespace(value) {
|
|
36
|
+
return is(value, CaipNamespaceStruct);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Check if the given value is a {@link CaipReference}.
|
|
40
|
+
*
|
|
41
|
+
* @param value - The value to check.
|
|
42
|
+
* @returns Whether the value is a {@link CaipReference}.
|
|
43
|
+
*/ export function isCaipReference(value) {
|
|
44
|
+
return is(value, CaipReferenceStruct);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Check if the given value is a {@link CaipAccountId}.
|
|
48
|
+
*
|
|
49
|
+
* @param value - The value to check.
|
|
50
|
+
* @returns Whether the value is a {@link CaipAccountId}.
|
|
51
|
+
*/ export function isCaipAccountId(value) {
|
|
52
|
+
return is(value, CaipAccountIdStruct);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Check if a value is a {@link CaipAccountAddress}.
|
|
56
|
+
*
|
|
57
|
+
* @param value - The value to validate.
|
|
58
|
+
* @returns True if the value is a valid {@link CaipAccountAddress}.
|
|
59
|
+
*/ export function isCaipAccountAddress(value) {
|
|
60
|
+
return is(value, CaipAccountAddressStruct);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Parse a CAIP-2 chain ID to an object containing the namespace and reference.
|
|
64
|
+
* This validates the CAIP-2 chain ID before parsing it.
|
|
65
|
+
*
|
|
66
|
+
* @param caipChainId - The CAIP-2 chain ID to validate and parse.
|
|
67
|
+
* @returns The parsed CAIP-2 chain ID.
|
|
68
|
+
*/ export function parseCaipChainId(caipChainId) {
|
|
69
|
+
const match = CAIP_CHAIN_ID_REGEX.exec(caipChainId);
|
|
70
|
+
if (!match?.groups) {
|
|
71
|
+
throw new Error('Invalid CAIP chain ID.');
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
namespace: match.groups.namespace,
|
|
75
|
+
reference: match.groups.reference
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Parse an CAIP-10 account ID to an object containing the chain ID, parsed chain ID, and account address.
|
|
80
|
+
* This validates the CAIP-10 account ID before parsing it.
|
|
81
|
+
*
|
|
82
|
+
* @param caipAccountId - The CAIP-10 account ID to validate and parse.
|
|
83
|
+
* @returns The parsed CAIP-10 account ID.
|
|
84
|
+
*/ export function parseCaipAccountId(caipAccountId) {
|
|
85
|
+
const match = CAIP_ACCOUNT_ID_REGEX.exec(caipAccountId);
|
|
86
|
+
if (!match?.groups) {
|
|
87
|
+
throw new Error('Invalid CAIP account ID.');
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
address: match.groups.accountAddress,
|
|
91
|
+
chainId: match.groups.chainId,
|
|
92
|
+
chain: {
|
|
93
|
+
namespace: match.groups.namespace,
|
|
94
|
+
reference: match.groups.reference
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
//# sourceMappingURL=caip-types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/caip-types.ts"],"sourcesContent":["import type { Infer } from 'superstruct';\nimport { is, pattern, string } from 'superstruct';\n\nexport const CAIP_CHAIN_ID_REGEX =\n /^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$/u;\n\nexport const CAIP_NAMESPACE_REGEX = /^[-a-z0-9]{3,8}$/u;\n\nexport const CAIP_REFERENCE_REGEX = /^[-_a-zA-Z0-9]{1,32}$/u;\n\nexport const CAIP_ACCOUNT_ID_REGEX =\n /^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})):(?<accountAddress>[-.%a-zA-Z0-9]{1,128})$/u;\n\nexport const CAIP_ACCOUNT_ADDRESS_REGEX = /^[-.%a-zA-Z0-9]{1,128}$/u;\n\n/**\n * A CAIP-2 chain ID, i.e., a human-readable namespace and reference.\n */\nexport const CaipChainIdStruct = pattern(string(), CAIP_CHAIN_ID_REGEX);\nexport type CaipChainId = `${string}:${string}`;\n\n/**\n * A CAIP-2 namespace, i.e., the first part of a CAIP chain ID.\n */\nexport const CaipNamespaceStruct = pattern(string(), CAIP_NAMESPACE_REGEX);\nexport type CaipNamespace = Infer<typeof CaipNamespaceStruct>;\n\n/**\n * A CAIP-2 reference, i.e., the second part of a CAIP chain ID.\n */\nexport const CaipReferenceStruct = pattern(string(), CAIP_REFERENCE_REGEX);\nexport type CaipReference = Infer<typeof CaipReferenceStruct>;\n\n/**\n * A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.\n */\nexport const CaipAccountIdStruct = pattern(string(), CAIP_ACCOUNT_ID_REGEX);\nexport type CaipAccountId = `${string}:${string}:${string}`;\n\n/**\n * A CAIP-10 account address, i.e., the third part of the CAIP account ID.\n */\nexport const CaipAccountAddressStruct = pattern(\n string(),\n CAIP_ACCOUNT_ADDRESS_REGEX,\n);\nexport type CaipAccountAddress = Infer<typeof CaipAccountAddressStruct>;\n\n/**\n * Check if the given value is a {@link CaipChainId}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipChainId}.\n */\nexport function isCaipChainId(value: unknown): value is CaipChainId {\n return is(value, CaipChainIdStruct);\n}\n\n/**\n * Check if the given value is a {@link CaipNamespace}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipNamespace}.\n */\nexport function isCaipNamespace(value: unknown): value is CaipNamespace {\n return is(value, CaipNamespaceStruct);\n}\n\n/**\n * Check if the given value is a {@link CaipReference}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipReference}.\n */\nexport function isCaipReference(value: unknown): value is CaipReference {\n return is(value, CaipReferenceStruct);\n}\n\n/**\n * Check if the given value is a {@link CaipAccountId}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipAccountId}.\n */\nexport function isCaipAccountId(value: unknown): value is CaipAccountId {\n return is(value, CaipAccountIdStruct);\n}\n\n/**\n * Check if a value is a {@link CaipAccountAddress}.\n *\n * @param value - The value to validate.\n * @returns True if the value is a valid {@link CaipAccountAddress}.\n */\nexport function isCaipAccountAddress(\n value: unknown,\n): value is CaipAccountAddress {\n return is(value, CaipAccountAddressStruct);\n}\n\n/**\n * Parse a CAIP-2 chain ID to an object containing the namespace and reference.\n * This validates the CAIP-2 chain ID before parsing it.\n *\n * @param caipChainId - The CAIP-2 chain ID to validate and parse.\n * @returns The parsed CAIP-2 chain ID.\n */\nexport function parseCaipChainId(caipChainId: CaipChainId): {\n namespace: CaipNamespace;\n reference: CaipReference;\n} {\n const match = CAIP_CHAIN_ID_REGEX.exec(caipChainId);\n if (!match?.groups) {\n throw new Error('Invalid CAIP chain ID.');\n }\n\n return {\n namespace: match.groups.namespace as CaipNamespace,\n reference: match.groups.reference as CaipReference,\n };\n}\n\n/**\n * Parse an CAIP-10 account ID to an object containing the chain ID, parsed chain ID, and account address.\n * This validates the CAIP-10 account ID before parsing it.\n *\n * @param caipAccountId - The CAIP-10 account ID to validate and parse.\n * @returns The parsed CAIP-10 account ID.\n */\nexport function parseCaipAccountId(caipAccountId: CaipAccountId): {\n address: CaipAccountAddress;\n chainId: CaipChainId;\n chain: { namespace: CaipNamespace; reference: CaipReference };\n} {\n const match = CAIP_ACCOUNT_ID_REGEX.exec(caipAccountId);\n if (!match?.groups) {\n throw new Error('Invalid CAIP account ID.');\n }\n\n return {\n address: match.groups.accountAddress as CaipAccountAddress,\n chainId: match.groups.chainId as CaipChainId,\n chain: {\n namespace: match.groups.namespace as CaipNamespace,\n reference: match.groups.reference as CaipReference,\n },\n };\n}\n"],"names":["is","pattern","string","CAIP_CHAIN_ID_REGEX","CAIP_NAMESPACE_REGEX","CAIP_REFERENCE_REGEX","CAIP_ACCOUNT_ID_REGEX","CAIP_ACCOUNT_ADDRESS_REGEX","CaipChainIdStruct","CaipNamespaceStruct","CaipReferenceStruct","CaipAccountIdStruct","CaipAccountAddressStruct","isCaipChainId","value","isCaipNamespace","isCaipReference","isCaipAccountId","isCaipAccountAddress","parseCaipChainId","caipChainId","match","exec","groups","Error","namespace","reference","parseCaipAccountId","caipAccountId","address","accountAddress","chainId","chain"],"mappings":"AACA,SAASA,EAAE,EAAEC,OAAO,EAAEC,MAAM,QAAQ,cAAc;AAElD,OAAO,MAAMC,sBACX,gFAAoE;AAEtE,OAAO,MAAMC,uBAAuB,oBAAoB;AAExD,OAAO,MAAMC,uBAAuB,yBAAyB;AAE7D,OAAO,MAAMC,wBACX,qIAAyH;AAE3H,OAAO,MAAMC,6BAA6B,2BAA2B;AAErE;;CAEC,GACD,OAAO,MAAMC,oBAAoBP,QAAQC,UAAUC,qBAAqB;AAGxE;;CAEC,GACD,OAAO,MAAMM,sBAAsBR,QAAQC,UAAUE,sBAAsB;AAG3E;;CAEC,GACD,OAAO,MAAMM,sBAAsBT,QAAQC,UAAUG,sBAAsB;AAG3E;;CAEC,GACD,OAAO,MAAMM,sBAAsBV,QAAQC,UAAUI,uBAAuB;AAG5E;;CAEC,GACD,OAAO,MAAMM,2BAA2BX,QACtCC,UACAK,4BACA;AAGF;;;;;CAKC,GACD,OAAO,SAASM,cAAcC,KAAc;IAC1C,OAAOd,GAAGc,OAAON;AACnB;AAEA;;;;;CAKC,GACD,OAAO,SAASO,gBAAgBD,KAAc;IAC5C,OAAOd,GAAGc,OAAOL;AACnB;AAEA;;;;;CAKC,GACD,OAAO,SAASO,gBAAgBF,KAAc;IAC5C,OAAOd,GAAGc,OAAOJ;AACnB;AAEA;;;;;CAKC,GACD,OAAO,SAASO,gBAAgBH,KAAc;IAC5C,OAAOd,GAAGc,OAAOH;AACnB;AAEA;;;;;CAKC,GACD,OAAO,SAASO,qBACdJ,KAAc;IAEd,OAAOd,GAAGc,OAAOF;AACnB;AAEA;;;;;;CAMC,GACD,OAAO,SAASO,iBAAiBC,WAAwB;IAIvD,MAAMC,QAAQlB,oBAAoBmB,IAAI,CAACF;IACvC,IAAI,CAACC,OAAOE,QAAQ;QAClB,MAAM,IAAIC,MAAM;IAClB;IAEA,OAAO;QACLC,WAAWJ,MAAME,MAAM,CAACE,SAAS;QACjCC,WAAWL,MAAME,MAAM,CAACG,SAAS;IACnC;AACF;AAEA;;;;;;CAMC,GACD,OAAO,SAASC,mBAAmBC,aAA4B;IAK7D,MAAMP,QAAQf,sBAAsBgB,IAAI,CAACM;IACzC,IAAI,CAACP,OAAOE,QAAQ;QAClB,MAAM,IAAIC,MAAM;IAClB;IAEA,OAAO;QACLK,SAASR,MAAME,MAAM,CAACO,cAAc;QACpCC,SAASV,MAAME,MAAM,CAACQ,OAAO;QAC7BC,OAAO;YACLP,WAAWJ,MAAME,MAAM,CAACE,SAAS;YACjCC,WAAWL,MAAME,MAAM,CAACG,SAAS;QACnC;IACF;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/checksum.ts"],"sourcesContent":["import { size, string } from 'superstruct';\n\nimport { base64 } from './base64';\n\nexport const ChecksumStruct = size(\n base64(string(), { paddingRequired: true }),\n 44,\n 44,\n);\n"],"names":["size","string","base64","ChecksumStruct","paddingRequired"],"mappings":"AAAA,SAASA,IAAI,EAAEC,MAAM,QAAQ,cAAc;AAE3C,SAASC,MAAM,QAAQ,WAAW;AAElC,OAAO,MAAMC,iBAAiBH,KAC5BE,OAAOD,UAAU;IAAEG,iBAAiB;AAAK,IACzC,IACA,IACA"}
|
|
@@ -1,16 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
1
|
+
import { bigint, coerce, create, instance, number, string, StructError, union } from 'superstruct';
|
|
2
|
+
import { assert } from './assert';
|
|
3
|
+
import { bytesToHex, hexToBytes } from './bytes';
|
|
4
|
+
import { StrictHexStruct } from './hex';
|
|
5
|
+
const NumberLikeStruct = union([
|
|
6
|
+
number(),
|
|
7
|
+
bigint(),
|
|
8
|
+
string(),
|
|
9
|
+
StrictHexStruct
|
|
10
|
+
]);
|
|
11
|
+
const NumberCoercer = coerce(number(), NumberLikeStruct, Number);
|
|
12
|
+
const BigIntCoercer = coerce(bigint(), NumberLikeStruct, BigInt);
|
|
13
|
+
const BytesLikeStruct = union([
|
|
14
|
+
StrictHexStruct,
|
|
15
|
+
instance(Uint8Array)
|
|
16
|
+
]);
|
|
17
|
+
const BytesCoercer = coerce(instance(Uint8Array), union([
|
|
18
|
+
StrictHexStruct
|
|
19
|
+
]), hexToBytes);
|
|
20
|
+
const HexCoercer = coerce(StrictHexStruct, instance(Uint8Array), bytesToHex);
|
|
14
21
|
/**
|
|
15
22
|
* Create a number from a number-like value.
|
|
16
23
|
*
|
|
@@ -35,22 +42,18 @@ const HexCoercer = (0, superstruct_1.coerce)(hex_1.StrictHexStruct, (0, superstr
|
|
|
35
42
|
* @returns The created number.
|
|
36
43
|
* @throws If the value is not a number-like value, or if the resulting number
|
|
37
44
|
* is `NaN` or `Infinity`.
|
|
38
|
-
*/
|
|
39
|
-
function createNumber(value) {
|
|
45
|
+
*/ export function createNumber(value) {
|
|
40
46
|
try {
|
|
41
|
-
const result =
|
|
42
|
-
|
|
47
|
+
const result = create(value, NumberCoercer);
|
|
48
|
+
assert(Number.isFinite(result), `Expected a number-like value, got "${value}".`);
|
|
43
49
|
return result;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (error instanceof superstruct_1.StructError) {
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof StructError) {
|
|
47
52
|
throw new Error(`Expected a number-like value, got "${value}".`);
|
|
48
53
|
}
|
|
49
|
-
/* istanbul ignore next */
|
|
50
|
-
throw error;
|
|
54
|
+
/* istanbul ignore next */ throw error;
|
|
51
55
|
}
|
|
52
56
|
}
|
|
53
|
-
exports.createNumber = createNumber;
|
|
54
57
|
/**
|
|
55
58
|
* Create a `bigint` from a number-like value.
|
|
56
59
|
*
|
|
@@ -72,22 +75,18 @@ exports.createNumber = createNumber;
|
|
|
72
75
|
* @param value - The value to create the bigint from.
|
|
73
76
|
* @returns The created bigint.
|
|
74
77
|
* @throws If the value is not a number-like value.
|
|
75
|
-
*/
|
|
76
|
-
function createBigInt(value) {
|
|
78
|
+
*/ export function createBigInt(value) {
|
|
77
79
|
try {
|
|
78
80
|
// The `BigInt` constructor throws if the value is not a number-like value.
|
|
79
81
|
// There is no need to validate the value manually.
|
|
80
|
-
return
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (error instanceof superstruct_1.StructError) {
|
|
82
|
+
return create(value, BigIntCoercer);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (error instanceof StructError) {
|
|
84
85
|
throw new Error(`Expected a number-like value, got "${String(error.value)}".`);
|
|
85
86
|
}
|
|
86
|
-
/* istanbul ignore next */
|
|
87
|
-
throw error;
|
|
87
|
+
/* istanbul ignore next */ throw error;
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
-
exports.createBigInt = createBigInt;
|
|
91
90
|
/**
|
|
92
91
|
* Create a byte array from a bytes-like value.
|
|
93
92
|
*
|
|
@@ -106,23 +105,19 @@ exports.createBigInt = createBigInt;
|
|
|
106
105
|
* @param value - The value to create the byte array from.
|
|
107
106
|
* @returns The created byte array.
|
|
108
107
|
* @throws If the value is not a bytes-like value.
|
|
109
|
-
*/
|
|
110
|
-
function createBytes(value) {
|
|
108
|
+
*/ export function createBytes(value) {
|
|
111
109
|
if (typeof value === 'string' && value.toLowerCase() === '0x') {
|
|
112
110
|
return new Uint8Array();
|
|
113
111
|
}
|
|
114
112
|
try {
|
|
115
|
-
return
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (error instanceof superstruct_1.StructError) {
|
|
113
|
+
return create(value, BytesCoercer);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (error instanceof StructError) {
|
|
119
116
|
throw new Error(`Expected a bytes-like value, got "${String(error.value)}".`);
|
|
120
117
|
}
|
|
121
|
-
/* istanbul ignore next */
|
|
122
|
-
throw error;
|
|
118
|
+
/* istanbul ignore next */ throw error;
|
|
123
119
|
}
|
|
124
120
|
}
|
|
125
|
-
exports.createBytes = createBytes;
|
|
126
121
|
/**
|
|
127
122
|
* Create a hexadecimal string from a bytes-like value.
|
|
128
123
|
*
|
|
@@ -141,22 +136,18 @@ exports.createBytes = createBytes;
|
|
|
141
136
|
* @param value - The value to create the hex string from.
|
|
142
137
|
* @returns The created hex string.
|
|
143
138
|
* @throws If the value is not a bytes-like value.
|
|
144
|
-
*/
|
|
145
|
-
|
|
146
|
-
if ((value instanceof Uint8Array && value.length === 0) ||
|
|
147
|
-
(typeof value === 'string' && value.toLowerCase() === '0x')) {
|
|
139
|
+
*/ export function createHex(value) {
|
|
140
|
+
if (value instanceof Uint8Array && value.length === 0 || typeof value === 'string' && value.toLowerCase() === '0x') {
|
|
148
141
|
return '0x';
|
|
149
142
|
}
|
|
150
143
|
try {
|
|
151
|
-
return
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (error instanceof superstruct_1.StructError) {
|
|
144
|
+
return create(value, HexCoercer);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (error instanceof StructError) {
|
|
155
147
|
throw new Error(`Expected a bytes-like value, got "${String(error.value)}".`);
|
|
156
148
|
}
|
|
157
|
-
/* istanbul ignore next */
|
|
158
|
-
throw error;
|
|
149
|
+
/* istanbul ignore next */ throw error;
|
|
159
150
|
}
|
|
160
151
|
}
|
|
161
|
-
|
|
152
|
+
|
|
162
153
|
//# sourceMappingURL=coercers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/coercers.ts"],"sourcesContent":["import type { Infer } from 'superstruct';\nimport {\n bigint,\n coerce,\n create,\n instance,\n number,\n string,\n StructError,\n union,\n} from 'superstruct';\n\nimport { assert } from './assert';\nimport { bytesToHex, hexToBytes } from './bytes';\nimport type { Hex } from './hex';\nimport { StrictHexStruct } from './hex';\n\nconst NumberLikeStruct = union([number(), bigint(), string(), StrictHexStruct]);\nconst NumberCoercer = coerce(number(), NumberLikeStruct, Number);\nconst BigIntCoercer = coerce(bigint(), NumberLikeStruct, BigInt);\n\nconst BytesLikeStruct = union([StrictHexStruct, instance(Uint8Array)]);\nconst BytesCoercer = coerce(\n instance(Uint8Array),\n union([StrictHexStruct]),\n hexToBytes,\n);\n\nconst HexCoercer = coerce(StrictHexStruct, instance(Uint8Array), bytesToHex);\n\nexport type NumberLike = Infer<typeof NumberLikeStruct>;\nexport type BytesLike = Infer<typeof BytesLikeStruct>;\n\n/**\n * Create a number from a number-like value.\n *\n * - If the value is a number, it is returned as-is.\n * - If the value is a `bigint`, it is converted to a number.\n * - If the value is a string, it is interpreted as a decimal number.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is\n * interpreted as a hexadecimal number.\n *\n * This validates that the value is a number-like value, and that the resulting\n * number is not `NaN` or `Infinity`.\n *\n * @example\n * ```typescript\n * const value = createNumber('0x010203');\n * console.log(value); // 66051\n *\n * const otherValue = createNumber(123n);\n * console.log(otherValue); // 123\n * ```\n * @param value - The value to create the number from.\n * @returns The created number.\n * @throws If the value is not a number-like value, or if the resulting number\n * is `NaN` or `Infinity`.\n */\nexport function createNumber(value: NumberLike): number {\n try {\n const result = create(value, NumberCoercer);\n\n assert(\n Number.isFinite(result),\n `Expected a number-like value, got \"${value}\".`,\n );\n\n return result;\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(`Expected a number-like value, got \"${value}\".`);\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a `bigint` from a number-like value.\n *\n * - If the value is a number, it is converted to a `bigint`.\n * - If the value is a `bigint`, it is returned as-is.\n * - If the value is a string, it is interpreted as a decimal number and\n * converted to a `bigint`.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is\n * interpreted as a hexadecimal number and converted to a `bigint`.\n *\n * @example\n * ```typescript\n * const value = createBigInt('0x010203');\n * console.log(value); // 16909060n\n *\n * const otherValue = createBigInt(123);\n * console.log(otherValue); // 123n\n * ```\n * @param value - The value to create the bigint from.\n * @returns The created bigint.\n * @throws If the value is not a number-like value.\n */\nexport function createBigInt(value: NumberLike): bigint {\n try {\n // The `BigInt` constructor throws if the value is not a number-like value.\n // There is no need to validate the value manually.\n return create(value, BigIntCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(\n `Expected a number-like value, got \"${String(error.value)}\".`,\n );\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a byte array from a bytes-like value.\n *\n * - If the value is a byte array, it is returned as-is.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is interpreted\n * as a hexadecimal number and converted to a byte array.\n *\n * @example\n * ```typescript\n * const value = createBytes('0x010203');\n * console.log(value); // Uint8Array [ 1, 2, 3 ]\n *\n * const otherValue = createBytes('0x010203');\n * console.log(otherValue); // Uint8Array [ 1, 2, 3 ]\n * ```\n * @param value - The value to create the byte array from.\n * @returns The created byte array.\n * @throws If the value is not a bytes-like value.\n */\nexport function createBytes(value: BytesLike): Uint8Array {\n if (typeof value === 'string' && value.toLowerCase() === '0x') {\n return new Uint8Array();\n }\n\n try {\n return create(value, BytesCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(\n `Expected a bytes-like value, got \"${String(error.value)}\".`,\n );\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a hexadecimal string from a bytes-like value.\n *\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is returned\n * as-is.\n * - If the value is a `Uint8Array`, it is converted to a hex string.\n *\n * @example\n * ```typescript\n * const value = createHex(new Uint8Array([1, 2, 3]));\n * console.log(value); // '0x010203'\n *\n * const otherValue = createHex('0x010203');\n * console.log(otherValue); // '0x010203'\n * ```\n * @param value - The value to create the hex string from.\n * @returns The created hex string.\n * @throws If the value is not a bytes-like value.\n */\nexport function createHex(value: BytesLike): Hex {\n if (\n (value instanceof Uint8Array && value.length === 0) ||\n (typeof value === 'string' && value.toLowerCase() === '0x')\n ) {\n return '0x';\n }\n\n try {\n return create(value, HexCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(\n `Expected a bytes-like value, got \"${String(error.value)}\".`,\n );\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n"],"names":["bigint","coerce","create","instance","number","string","StructError","union","assert","bytesToHex","hexToBytes","StrictHexStruct","NumberLikeStruct","NumberCoercer","Number","BigIntCoercer","BigInt","BytesLikeStruct","Uint8Array","BytesCoercer","HexCoercer","createNumber","value","result","isFinite","error","Error","createBigInt","String","createBytes","toLowerCase","createHex","length"],"mappings":"AACA,SACEA,MAAM,EACNC,MAAM,EACNC,MAAM,EACNC,QAAQ,EACRC,MAAM,EACNC,MAAM,EACNC,WAAW,EACXC,KAAK,QACA,cAAc;AAErB,SAASC,MAAM,QAAQ,WAAW;AAClC,SAASC,UAAU,EAAEC,UAAU,QAAQ,UAAU;AAEjD,SAASC,eAAe,QAAQ,QAAQ;AAExC,MAAMC,mBAAmBL,MAAM;IAACH;IAAUJ;IAAUK;IAAUM;CAAgB;AAC9E,MAAME,gBAAgBZ,OAAOG,UAAUQ,kBAAkBE;AACzD,MAAMC,gBAAgBd,OAAOD,UAAUY,kBAAkBI;AAEzD,MAAMC,kBAAkBV,MAAM;IAACI;IAAiBR,SAASe;CAAY;AACrE,MAAMC,eAAelB,OACnBE,SAASe,aACTX,MAAM;IAACI;CAAgB,GACvBD;AAGF,MAAMU,aAAanB,OAAOU,iBAAiBR,SAASe,aAAaT;AAKjE;;;;;;;;;;;;;;;;;;;;;;;;CAwBC,GACD,OAAO,SAASY,aAAaC,KAAiB;IAC5C,IAAI;QACF,MAAMC,SAASrB,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,IAAIA,iBAAiBnB,aAAa;YAChC,MAAM,IAAIoB,MACR,CAAC,kCAAkC,EAAEE,OAAOH,MAAMH,KAAK,EAAE,EAAE,CAAC;QAEhE;QAEA,wBAAwB,GACxB,MAAMG;IACR;AACF"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A {@link ReadonlyMap} that cannot be modified after instantiation.
|
|
3
|
+
* The implementation uses an inner map hidden via a private field, and the
|
|
4
|
+
* immutability guarantee relies on it being impossible to get a reference
|
|
5
|
+
* to this map.
|
|
6
|
+
*/ function _check_private_redeclaration(obj, privateCollection) {
|
|
7
|
+
if (privateCollection.has(obj)) {
|
|
8
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function _class_apply_descriptor_get(receiver, descriptor) {
|
|
12
|
+
if (descriptor.get) {
|
|
13
|
+
return descriptor.get.call(receiver);
|
|
14
|
+
}
|
|
15
|
+
return descriptor.value;
|
|
16
|
+
}
|
|
17
|
+
function _class_apply_descriptor_set(receiver, descriptor, value) {
|
|
18
|
+
if (descriptor.set) {
|
|
19
|
+
descriptor.set.call(receiver, value);
|
|
20
|
+
} else {
|
|
21
|
+
if (!descriptor.writable) {
|
|
22
|
+
throw new TypeError("attempted to set read only private field");
|
|
23
|
+
}
|
|
24
|
+
descriptor.value = value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function _class_extract_field_descriptor(receiver, privateMap, action) {
|
|
28
|
+
if (!privateMap.has(receiver)) {
|
|
29
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
30
|
+
}
|
|
31
|
+
return privateMap.get(receiver);
|
|
32
|
+
}
|
|
33
|
+
function _class_private_field_get(receiver, privateMap) {
|
|
34
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
|
|
35
|
+
return _class_apply_descriptor_get(receiver, descriptor);
|
|
36
|
+
}
|
|
37
|
+
function _class_private_field_init(obj, privateMap, value) {
|
|
38
|
+
_check_private_redeclaration(obj, privateMap);
|
|
39
|
+
privateMap.set(obj, value);
|
|
40
|
+
}
|
|
41
|
+
function _class_private_field_set(receiver, privateMap, value) {
|
|
42
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
|
|
43
|
+
_class_apply_descriptor_set(receiver, descriptor, value);
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
var _map = /*#__PURE__*/ new WeakMap();
|
|
47
|
+
let _Symbol_iterator = Symbol.iterator;
|
|
48
|
+
class FrozenMap {
|
|
49
|
+
get size() {
|
|
50
|
+
return _class_private_field_get(this, _map).size;
|
|
51
|
+
}
|
|
52
|
+
[_Symbol_iterator]() {
|
|
53
|
+
return _class_private_field_get(this, _map)[Symbol.iterator]();
|
|
54
|
+
}
|
|
55
|
+
entries() {
|
|
56
|
+
return _class_private_field_get(this, _map).entries();
|
|
57
|
+
}
|
|
58
|
+
forEach(callbackfn, thisArg) {
|
|
59
|
+
// We have to wrap the specified callback in order to prevent it from
|
|
60
|
+
// receiving a reference to the inner map.
|
|
61
|
+
return _class_private_field_get(this, _map).forEach((value, key, _map)=>callbackfn.call(thisArg, value, key, this));
|
|
62
|
+
}
|
|
63
|
+
get(key) {
|
|
64
|
+
return _class_private_field_get(this, _map).get(key);
|
|
65
|
+
}
|
|
66
|
+
has(key) {
|
|
67
|
+
return _class_private_field_get(this, _map).has(key);
|
|
68
|
+
}
|
|
69
|
+
keys() {
|
|
70
|
+
return _class_private_field_get(this, _map).keys();
|
|
71
|
+
}
|
|
72
|
+
values() {
|
|
73
|
+
return _class_private_field_get(this, _map).values();
|
|
74
|
+
}
|
|
75
|
+
toString() {
|
|
76
|
+
return `FrozenMap(${this.size}) {${this.size > 0 ? ` ${[
|
|
77
|
+
...this.entries()
|
|
78
|
+
].map(([key, value])=>`${String(key)} => ${String(value)}`).join(', ')} ` : ''}}`;
|
|
79
|
+
}
|
|
80
|
+
constructor(entries){
|
|
81
|
+
_class_private_field_init(this, _map, {
|
|
82
|
+
writable: true,
|
|
83
|
+
value: void 0
|
|
84
|
+
});
|
|
85
|
+
_class_private_field_set(this, _map, new Map(entries));
|
|
86
|
+
Object.freeze(this);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
var _set = /*#__PURE__*/ new WeakMap();
|
|
90
|
+
let _Symbol_iterator1 = Symbol.iterator;
|
|
91
|
+
/**
|
|
92
|
+
* A {@link ReadonlySet} that cannot be modified after instantiation.
|
|
93
|
+
* The implementation uses an inner set hidden via a private field, and the
|
|
94
|
+
* immutability guarantee relies on it being impossible to get a reference
|
|
95
|
+
* to this set.
|
|
96
|
+
*/ class FrozenSet {
|
|
97
|
+
get size() {
|
|
98
|
+
return _class_private_field_get(this, _set).size;
|
|
99
|
+
}
|
|
100
|
+
[_Symbol_iterator1]() {
|
|
101
|
+
return _class_private_field_get(this, _set)[Symbol.iterator]();
|
|
102
|
+
}
|
|
103
|
+
entries() {
|
|
104
|
+
return _class_private_field_get(this, _set).entries();
|
|
105
|
+
}
|
|
106
|
+
forEach(callbackfn, thisArg) {
|
|
107
|
+
// We have to wrap the specified callback in order to prevent it from
|
|
108
|
+
// receiving a reference to the inner set.
|
|
109
|
+
return _class_private_field_get(this, _set).forEach((value, value2, _set)=>callbackfn.call(thisArg, value, value2, this));
|
|
110
|
+
}
|
|
111
|
+
has(value) {
|
|
112
|
+
return _class_private_field_get(this, _set).has(value);
|
|
113
|
+
}
|
|
114
|
+
keys() {
|
|
115
|
+
return _class_private_field_get(this, _set).keys();
|
|
116
|
+
}
|
|
117
|
+
values() {
|
|
118
|
+
return _class_private_field_get(this, _set).values();
|
|
119
|
+
}
|
|
120
|
+
toString() {
|
|
121
|
+
return `FrozenSet(${this.size}) {${this.size > 0 ? ` ${[
|
|
122
|
+
...this.values()
|
|
123
|
+
].map((member)=>String(member)).join(', ')} ` : ''}}`;
|
|
124
|
+
}
|
|
125
|
+
constructor(values){
|
|
126
|
+
_class_private_field_init(this, _set, {
|
|
127
|
+
writable: true,
|
|
128
|
+
value: void 0
|
|
129
|
+
});
|
|
130
|
+
_class_private_field_set(this, _set, new Set(values));
|
|
131
|
+
Object.freeze(this);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
Object.freeze(FrozenMap);
|
|
135
|
+
Object.freeze(FrozenMap.prototype);
|
|
136
|
+
Object.freeze(FrozenSet);
|
|
137
|
+
Object.freeze(FrozenSet.prototype);
|
|
138
|
+
export { FrozenMap, FrozenSet };
|
|
139
|
+
|
|
140
|
+
//# sourceMappingURL=collections.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/collections.ts"],"sourcesContent":["/**\n * A {@link ReadonlyMap} that cannot be modified after instantiation.\n * The implementation uses an inner map hidden via a private field, and the\n * immutability guarantee relies on it being impossible to get a reference\n * to this map.\n */\nclass FrozenMap<Key, Value> implements ReadonlyMap<Key, Value> {\n readonly #map: Map<Key, Value>;\n\n public get size() {\n return this.#map.size;\n }\n\n public [Symbol.iterator]() {\n return this.#map[Symbol.iterator]();\n }\n\n constructor(entries?: readonly (readonly [Key, Value])[] | null) {\n this.#map = new Map<Key, Value>(entries);\n Object.freeze(this);\n }\n\n public entries() {\n return this.#map.entries();\n }\n\n public forEach(\n callbackfn: (value: Value, key: Key, map: this) => void,\n thisArg?: any,\n ): void {\n // We have to wrap the specified callback in order to prevent it from\n // receiving a reference to the inner map.\n return this.#map.forEach((value: Value, key: Key, _map: unknown) =>\n callbackfn.call(thisArg, value, key, this),\n );\n }\n\n public get(key: Key) {\n return this.#map.get(key);\n }\n\n public has(key: Key) {\n return this.#map.has(key);\n }\n\n public keys() {\n return this.#map.keys();\n }\n\n public values() {\n return this.#map.values();\n }\n\n public toString(): string {\n return `FrozenMap(${this.size}) {${\n this.size > 0\n ? ` ${[...this.entries()]\n .map(([key, value]) => `${String(key)} => ${String(value)}`)\n .join(', ')} `\n : ''\n }}`;\n }\n}\n\n/**\n * A {@link ReadonlySet} that cannot be modified after instantiation.\n * The implementation uses an inner set hidden via a private field, and the\n * immutability guarantee relies on it being impossible to get a reference\n * to this set.\n */\nclass FrozenSet<Value> implements ReadonlySet<Value> {\n readonly #set: Set<Value>;\n\n public get size() {\n return this.#set.size;\n }\n\n public [Symbol.iterator]() {\n return this.#set[Symbol.iterator]();\n }\n\n constructor(values?: readonly Value[] | null) {\n this.#set = new Set<Value>(values);\n Object.freeze(this);\n }\n\n public entries() {\n return this.#set.entries();\n }\n\n public forEach(\n callbackfn: (value: Value, value2: Value, set: this) => void,\n thisArg?: any,\n ): void {\n // We have to wrap the specified callback in order to prevent it from\n // receiving a reference to the inner set.\n return this.#set.forEach((value: Value, value2: Value, _set: unknown) =>\n callbackfn.call(thisArg, value, value2, this),\n );\n }\n\n public has(value: Value) {\n return this.#set.has(value);\n }\n\n public keys() {\n return this.#set.keys();\n }\n\n public values() {\n return this.#set.values();\n }\n\n public toString(): string {\n return `FrozenSet(${this.size}) {${\n this.size > 0\n ? ` ${[...this.values()].map((member) => String(member)).join(', ')} `\n : ''\n }}`;\n }\n}\n\nObject.freeze(FrozenMap);\nObject.freeze(FrozenMap.prototype);\n\nObject.freeze(FrozenSet);\nObject.freeze(FrozenSet.prototype);\n\nexport { FrozenMap, FrozenSet };\n"],"names":["Symbol","iterator","FrozenMap","size","map","entries","forEach","callbackfn","thisArg","value","key","_map","call","get","has","keys","values","toString","String","join","constructor","Map","Object","freeze","FrozenSet","set","value2","_set","member","Set","prototype"],"mappings":"AAAA;;;;;CAKC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAEU;IAMDA,mBAAAA,OAAOC,QAAQ;AAPzB,MAAMC;IAGJ,IAAWC,OAAO;QAChB,OAAO,yBAAA,IAAI,EAAEC,MAAID,IAAI;IACvB;IAEO,CAACH,iBAAgB,GAAG;QACzB,OAAO,yBAAA,IAAI,EAAEI,KAAG,CAACJ,OAAOC,QAAQ,CAAC;IACnC;IAOOI,UAAU;QACf,OAAO,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,OAAOC,QAAQ,CAAC;IACnC;IAOOI,UAAU;QACf,OAAO,yBAAA,IAAI,EAAEoB,MAAIpB,OAAO;IAC1B;IAEOC,QACLC,UAA4D,EAC5DC,OAAa,EACP;QACN,qEAAqE;QACrE,0CAA0C;QAC1C,OAAO,yBAAA,IAAI,EAAEiB,MAAInB,OAAO,CAAC,CAACG,OAAciB,QAAeC,OACrDpB,WAAWK,IAAI,CAACJ,SAASC,OAAOiB,QAAQ,IAAI;IAEhD;IAEOZ,IAAIL,KAAY,EAAE;QACvB,OAAO,yBAAA,IAAI,EAAEgB,MAAIX,GAAG,CAACL;IACvB;IAEOM,OAAO;QACZ,OAAO,yBAAA,IAAI,EAAEU,MAAIV,IAAI;IACvB;IAEOC,SAAS;QACd,OAAO,yBAAA,IAAI,EAAES,MAAIT,MAAM;IACzB;IAEOC,WAAmB;QACxB,OAAO,CAAC,UAAU,EAAE,IAAI,CAACd,IAAI,CAAC,GAAG,EAC/B,IAAI,CAACA,IAAI,GAAG,IACR,CAAC,CAAC,EAAE;eAAI,IAAI,CAACa,MAAM;SAAG,CAACZ,GAAG,CAAC,CAACwB,SAAWV,OAAOU,SAAST,IAAI,CAAC,MAAM,CAAC,CAAC,GACpE,GACL,CAAC,CAAC;IACL;IAtCAC,YAAYJ,MAAgC,CAAE;QAV9C,gCAAS;;mBAAT,KAAA;;uCAWQS,MAAM,IAAII,IAAWb;QAC3BM,OAAOC,MAAM,CAAC,IAAI;IACpB;AAoCF;AAEAD,OAAOC,MAAM,CAACrB;AACdoB,OAAOC,MAAM,CAACrB,UAAU4B,SAAS;AAEjCR,OAAOC,MAAM,CAACC;AACdF,OAAOC,MAAM,CAACC,UAAUM,SAAS;AAEjC,SAAS5B,SAAS,EAAEsB,SAAS,GAAG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/encryption-types.ts"],"sourcesContent":["export type Eip1024EncryptedData = {\n version: string;\n nonce: string;\n ephemPublicKey: string;\n ciphertext: string;\n};\n"],"names":[],"mappings":"AAAA,WAKE"}
|
package/dist/esm/hex.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { keccak_256 as keccak256 } from '@noble/hashes/sha3';
|
|
2
|
+
import { is, pattern, string } from 'superstruct';
|
|
3
|
+
import { assert } from './assert';
|
|
4
|
+
import { bytesToHex } from './bytes';
|
|
5
|
+
export const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu);
|
|
6
|
+
export const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu);
|
|
7
|
+
export const HexAddressStruct = pattern(string(), /^0x[0-9a-f]{40}$/u);
|
|
8
|
+
export const HexChecksumAddressStruct = pattern(string(), /^0x[0-9a-fA-F]{40}$/u);
|
|
9
|
+
/**
|
|
10
|
+
* Check if a string is a valid hex string.
|
|
11
|
+
*
|
|
12
|
+
* @param value - The value to check.
|
|
13
|
+
* @returns Whether the value is a valid hex string.
|
|
14
|
+
*/ export function isHexString(value) {
|
|
15
|
+
return is(value, HexStruct);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Strictly check if a string is a valid hex string. A valid hex string must
|
|
19
|
+
* start with the "0x"-prefix.
|
|
20
|
+
*
|
|
21
|
+
* @param value - The value to check.
|
|
22
|
+
* @returns Whether the value is a valid hex string.
|
|
23
|
+
*/ export function isStrictHexString(value) {
|
|
24
|
+
return is(value, StrictHexStruct);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Assert that a value is a valid hex string.
|
|
28
|
+
*
|
|
29
|
+
* @param value - The value to check.
|
|
30
|
+
* @throws If the value is not a valid hex string.
|
|
31
|
+
*/ export function assertIsHexString(value) {
|
|
32
|
+
assert(isHexString(value), 'Value must be a hexadecimal string.');
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Assert that a value is a valid hex string. A valid hex string must start with
|
|
36
|
+
* the "0x"-prefix.
|
|
37
|
+
*
|
|
38
|
+
* @param value - The value to check.
|
|
39
|
+
* @throws If the value is not a valid hex string.
|
|
40
|
+
*/ export function assertIsStrictHexString(value) {
|
|
41
|
+
assert(isStrictHexString(value), 'Value must be a hexadecimal string, starting with "0x".');
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Validate that the passed prefixed hex string is an all-lowercase
|
|
45
|
+
* hex address, or a valid mixed-case checksum address.
|
|
46
|
+
*
|
|
47
|
+
* @param possibleAddress - Input parameter to check against.
|
|
48
|
+
* @returns Whether or not the input is a valid hex address.
|
|
49
|
+
*/ export function isValidHexAddress(possibleAddress) {
|
|
50
|
+
return is(possibleAddress, HexAddressStruct) || isValidChecksumAddress(possibleAddress);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Encode a passed hex string as an ERC-55 mixed-case checksum address.
|
|
54
|
+
*
|
|
55
|
+
* @param address - The hex address to encode.
|
|
56
|
+
* @returns The address encoded according to ERC-55.
|
|
57
|
+
* @see https://eips.ethereum.org/EIPS/eip-55
|
|
58
|
+
*/ export function getChecksumAddress(address) {
|
|
59
|
+
assert(is(address, HexChecksumAddressStruct), 'Invalid hex address.');
|
|
60
|
+
const unPrefixed = remove0x(address.toLowerCase());
|
|
61
|
+
const unPrefixedHash = remove0x(bytesToHex(keccak256(unPrefixed)));
|
|
62
|
+
return `0x${unPrefixed.split('').map((character, nibbleIndex)=>{
|
|
63
|
+
const hashCharacter = unPrefixedHash[nibbleIndex];
|
|
64
|
+
assert(is(hashCharacter, string()), 'Hash shorter than address.');
|
|
65
|
+
return parseInt(hashCharacter, 16) > 7 ? character.toUpperCase() : character;
|
|
66
|
+
}).join('')}`;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Validate that the passed hex string is a valid ERC-55 mixed-case
|
|
70
|
+
* checksum address.
|
|
71
|
+
*
|
|
72
|
+
* @param possibleChecksum - The hex address to check.
|
|
73
|
+
* @returns True if the address is a checksum address.
|
|
74
|
+
*/ export function isValidChecksumAddress(possibleChecksum) {
|
|
75
|
+
if (!is(possibleChecksum, HexChecksumAddressStruct)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return getChecksumAddress(possibleChecksum) === possibleChecksum;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Add the `0x`-prefix to a hexadecimal string. If the string already has the
|
|
82
|
+
* prefix, it is returned as-is.
|
|
83
|
+
*
|
|
84
|
+
* @param hexadecimal - The hexadecimal string to add the prefix to.
|
|
85
|
+
* @returns The prefixed hexadecimal string.
|
|
86
|
+
*/ export function add0x(hexadecimal) {
|
|
87
|
+
if (hexadecimal.startsWith('0x')) {
|
|
88
|
+
return hexadecimal;
|
|
89
|
+
}
|
|
90
|
+
if (hexadecimal.startsWith('0X')) {
|
|
91
|
+
return `0x${hexadecimal.substring(2)}`;
|
|
92
|
+
}
|
|
93
|
+
return `0x${hexadecimal}`;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have
|
|
97
|
+
* the prefix, it is returned as-is.
|
|
98
|
+
*
|
|
99
|
+
* @param hexadecimal - The hexadecimal string to remove the prefix from.
|
|
100
|
+
* @returns The un-prefixed hexadecimal string.
|
|
101
|
+
*/ export function remove0x(hexadecimal) {
|
|
102
|
+
if (hexadecimal.startsWith('0x') || hexadecimal.startsWith('0X')) {
|
|
103
|
+
return hexadecimal.substring(2);
|
|
104
|
+
}
|
|
105
|
+
return hexadecimal;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
//# sourceMappingURL=hex.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hex.ts"],"sourcesContent":["import { keccak_256 as keccak256 } from '@noble/hashes/sha3';\nimport type { Struct } from 'superstruct';\nimport { is, pattern, string } from 'superstruct';\n\nimport { assert } from './assert';\nimport { bytesToHex } from './bytes';\n\nexport type Hex = `0x${string}`;\n\nexport const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu);\nexport const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu) as Struct<\n Hex,\n null\n>;\nexport const HexAddressStruct = pattern(\n string(),\n /^0x[0-9a-f]{40}$/u,\n) as Struct<Hex, null>;\nexport const HexChecksumAddressStruct = pattern(\n string(),\n /^0x[0-9a-fA-F]{40}$/u,\n) as Struct<Hex, null>;\n\n/**\n * Check if a string is a valid hex string.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isHexString(value: unknown): value is string {\n return is(value, HexStruct);\n}\n\n/**\n * Strictly check if a string is a valid hex string. A valid hex string must\n * start with the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isStrictHexString(value: unknown): value is Hex {\n return is(value, StrictHexStruct);\n}\n\n/**\n * Assert that a value is a valid hex string.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsHexString(value: unknown): asserts value is string {\n assert(isHexString(value), 'Value must be a hexadecimal string.');\n}\n\n/**\n * Assert that a value is a valid hex string. A valid hex string must start with\n * the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsStrictHexString(value: unknown): asserts value is Hex {\n assert(\n isStrictHexString(value),\n 'Value must be a hexadecimal string, starting with \"0x\".',\n );\n}\n\n/**\n * Validate that the passed prefixed hex string is an all-lowercase\n * hex address, or a valid mixed-case checksum address.\n *\n * @param possibleAddress - Input parameter to check against.\n * @returns Whether or not the input is a valid hex address.\n */\nexport function isValidHexAddress(possibleAddress: Hex) {\n return (\n is(possibleAddress, HexAddressStruct) ||\n isValidChecksumAddress(possibleAddress)\n );\n}\n\n/**\n * Encode a passed hex string as an ERC-55 mixed-case checksum address.\n *\n * @param address - The hex address to encode.\n * @returns The address encoded according to ERC-55.\n * @see https://eips.ethereum.org/EIPS/eip-55\n */\nexport function getChecksumAddress(address: Hex) {\n assert(is(address, HexChecksumAddressStruct), 'Invalid hex address.');\n const unPrefixed = remove0x(address.toLowerCase());\n const unPrefixedHash = remove0x(bytesToHex(keccak256(unPrefixed)));\n return `0x${unPrefixed\n .split('')\n .map((character, nibbleIndex) => {\n const hashCharacter = unPrefixedHash[nibbleIndex];\n assert(is(hashCharacter, string()), 'Hash shorter than address.');\n return parseInt(hashCharacter, 16) > 7\n ? character.toUpperCase()\n : character;\n })\n .join('')}`;\n}\n\n/**\n * Validate that the passed hex string is a valid ERC-55 mixed-case\n * checksum address.\n *\n * @param possibleChecksum - The hex address to check.\n * @returns True if the address is a checksum address.\n */\nexport function isValidChecksumAddress(possibleChecksum: Hex) {\n if (!is(possibleChecksum, HexChecksumAddressStruct)) {\n return false;\n }\n\n return getChecksumAddress(possibleChecksum) === possibleChecksum;\n}\n\n/**\n * Add the `0x`-prefix to a hexadecimal string. If the string already has the\n * prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to add the prefix to.\n * @returns The prefixed hexadecimal string.\n */\nexport function add0x(hexadecimal: string): Hex {\n if (hexadecimal.startsWith('0x')) {\n return hexadecimal as Hex;\n }\n\n if (hexadecimal.startsWith('0X')) {\n return `0x${hexadecimal.substring(2)}`;\n }\n\n return `0x${hexadecimal}`;\n}\n\n/**\n * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have\n * the prefix, it is returned as-is.\n *\n * @param hexadecimal - The hexadecimal string to remove the prefix from.\n * @returns The un-prefixed hexadecimal string.\n */\nexport function remove0x(hexadecimal: string): string {\n if (hexadecimal.startsWith('0x') || hexadecimal.startsWith('0X')) {\n return hexadecimal.substring(2);\n }\n\n return hexadecimal;\n}\n"],"names":["keccak_256","keccak256","is","pattern","string","assert","bytesToHex","HexStruct","StrictHexStruct","HexAddressStruct","HexChecksumAddressStruct","isHexString","value","isStrictHexString","assertIsHexString","assertIsStrictHexString","isValidHexAddress","possibleAddress","isValidChecksumAddress","getChecksumAddress","address","unPrefixed","remove0x","toLowerCase","unPrefixedHash","split","map","character","nibbleIndex","hashCharacter","parseInt","toU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