@xylabs/hex 5.0.4 → 5.0.6

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.
Files changed (87) hide show
  1. package/dist/neutral/address/AddressTransformZod.d.ts +8 -0
  2. package/dist/neutral/address/AddressTransformZod.d.ts.map +1 -0
  3. package/dist/neutral/address/AddressValidationZod.d.ts +8 -0
  4. package/dist/neutral/address/AddressValidationZod.d.ts.map +1 -0
  5. package/dist/neutral/address/address.d.ts +7 -22
  6. package/dist/neutral/address/address.d.ts.map +1 -1
  7. package/dist/neutral/address/addressDeprecated.d.ts +18 -0
  8. package/dist/neutral/address/addressDeprecated.d.ts.map +1 -0
  9. package/dist/neutral/address/as.d.ts +7 -0
  10. package/dist/neutral/address/as.d.ts.map +1 -0
  11. package/dist/neutral/address/index.d.ts +6 -2
  12. package/dist/neutral/address/index.d.ts.map +1 -1
  13. package/dist/neutral/address/is.d.ts +6 -0
  14. package/dist/neutral/address/is.d.ts.map +1 -0
  15. package/dist/neutral/address/to.d.ts +6 -0
  16. package/dist/neutral/address/to.d.ts.map +1 -0
  17. package/dist/neutral/ethAddress.d.ts +4 -0
  18. package/dist/neutral/ethAddress.d.ts.map +1 -1
  19. package/dist/neutral/hash/as.d.ts +5 -0
  20. package/dist/neutral/hash/as.d.ts.map +1 -0
  21. package/dist/neutral/hash/hash.d.ts +14 -0
  22. package/dist/neutral/hash/hash.d.ts.map +1 -0
  23. package/dist/neutral/hash/index.d.ts +4 -0
  24. package/dist/neutral/hash/index.d.ts.map +1 -0
  25. package/dist/neutral/hash/is.d.ts +3 -0
  26. package/dist/neutral/hash/is.d.ts.map +1 -0
  27. package/dist/neutral/hex/as.d.ts +1 -1
  28. package/dist/neutral/hex/as.d.ts.map +1 -1
  29. package/dist/neutral/hex/from/from.d.ts +1 -1
  30. package/dist/neutral/hex/from/from.d.ts.map +1 -1
  31. package/dist/neutral/hex/from/fromArrayBuffer.d.ts +1 -1
  32. package/dist/neutral/hex/from/fromArrayBuffer.d.ts.map +1 -1
  33. package/dist/neutral/hex/from/fromBigInt.d.ts +1 -1
  34. package/dist/neutral/hex/from/fromBigInt.d.ts.map +1 -1
  35. package/dist/neutral/hex/from/fromHexString.d.ts +1 -1
  36. package/dist/neutral/hex/from/fromHexString.d.ts.map +1 -1
  37. package/dist/neutral/hex/from/fromNumber.d.ts +1 -1
  38. package/dist/neutral/hex/from/fromNumber.d.ts.map +1 -1
  39. package/dist/neutral/hex/{model.d.ts → hex.d.ts} +4 -5
  40. package/dist/neutral/hex/hex.d.ts.map +1 -0
  41. package/dist/neutral/hex/index.d.ts +1 -1
  42. package/dist/neutral/hex/index.d.ts.map +1 -1
  43. package/dist/neutral/hex/is.d.ts +1 -1
  44. package/dist/neutral/hex/is.d.ts.map +1 -1
  45. package/dist/neutral/hex/to.d.ts +4 -2
  46. package/dist/neutral/hex/to.d.ts.map +1 -1
  47. package/dist/neutral/index-internal.d.ts +1 -1
  48. package/dist/neutral/index-internal.d.ts.map +1 -1
  49. package/dist/neutral/index-internal.mjs +94 -62
  50. package/dist/neutral/index-internal.mjs.map +1 -1
  51. package/dist/neutral/index.mjs +94 -62
  52. package/dist/neutral/index.mjs.map +1 -1
  53. package/package.json +4 -4
  54. package/src/address/AddressTransformZod.ts +26 -0
  55. package/src/address/{AddressZodValidation.ts → AddressValidationZod.ts} +2 -2
  56. package/src/address/address.ts +6 -76
  57. package/src/address/addressDeprecated.ts +18 -0
  58. package/src/address/as.ts +37 -0
  59. package/src/address/index.ts +6 -2
  60. package/src/address/is.ts +14 -0
  61. package/src/address/to.ts +18 -0
  62. package/src/ethAddress.ts +9 -2
  63. package/src/hash/as.ts +24 -0
  64. package/src/hash/hash.ts +23 -0
  65. package/src/hash/index.ts +3 -0
  66. package/src/hash/is.ts +6 -0
  67. package/src/hex/as.ts +1 -1
  68. package/src/hex/from/from.ts +1 -1
  69. package/src/hex/from/fromArrayBuffer.ts +1 -1
  70. package/src/hex/from/fromBigInt.ts +1 -1
  71. package/src/hex/from/fromHexString.ts +1 -1
  72. package/src/hex/from/fromNumber.ts +1 -1
  73. package/src/hex/{model.ts → hex.ts} +3 -6
  74. package/src/hex/index.ts +1 -1
  75. package/src/hex/is.ts +1 -1
  76. package/src/hex/to.ts +1 -1
  77. package/src/index-internal.ts +1 -1
  78. package/src/spec/hash.spec.ts +1 -1
  79. package/dist/neutral/address/AddressZodTransform.d.ts +0 -5
  80. package/dist/neutral/address/AddressZodTransform.d.ts.map +0 -1
  81. package/dist/neutral/address/AddressZodValidation.d.ts +0 -5
  82. package/dist/neutral/address/AddressZodValidation.d.ts.map +0 -1
  83. package/dist/neutral/hash.d.ts +0 -17
  84. package/dist/neutral/hash.d.ts.map +0 -1
  85. package/dist/neutral/hex/model.d.ts.map +0 -1
  86. package/src/address/AddressZodTransform.ts +0 -19
  87. package/src/hash.ts +0 -51
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/address/address.ts","../../src/assert.ts","../../src/hex/from/fromHexString.ts","../../src/HexRegEx.ts","../../src/hex/nibble.ts","../../src/hex/is.ts","../../src/hex/from/fromArrayBuffer.ts","../../src/hex/from/fromBigInt.ts","../../src/hex/from/fromNumber.ts","../../src/hex/from/from.ts","../../src/hex/as.ts","../../src/hex/isHexZero.ts","../../src/hex/legacy.ts","../../src/hex/model.ts","../../src/hex/to.ts","../../src/address/AddressZodTransform.ts","../../src/address/AddressZodValidation.ts","../../src/ethAddress.ts","../../src/hash.ts","../../src/hexToBigInt.ts"],"sourcesContent":["import { isObject } from '@xylabs/typeof'\nimport z from 'zod'\n\nimport type { AssertConfig } from '../assert.ts'\nimport { assertError } from '../assert.ts'\nimport type { Hex, HexConfig } from '../hex/index.ts'\nimport {\n hexFrom, hexFromHexString, isHex,\n} from '../hex/index.ts'\nimport { HexRegExMinMax } from '../HexRegEx.ts'\nimport { AddressZodTransform } from './AddressZodTransform.ts'\nimport { AddressZodValidation } from './AddressZodValidation.ts'\n\n// using true instead of unique symbol to avoid conflicts with other versions of library\nexport type Address = Hex & {\n readonly __address: true\n}\n\nexport const AddressRegEx = HexRegExMinMax(20, 20)\n\nexport const AddressToStringZod = z.string().regex(AddressRegEx)\n\n/** @deprecated use AddressToStringZod */\nexport const AddressToStringSchema = AddressToStringZod\n\nexport const AddressFromStringZod = z.string().toLowerCase().regex(AddressRegEx).transform(v => toAddress(v))\n\n/** @deprecated use AddressFromStringZod */\nexport const AddressFromStringSchema = AddressFromStringZod\n\nexport const ZERO_ADDRESS = '0000000000000000000000000000000000000000' as Address\nexport const ADDRESS_LENGTH = 40 as const\n\nexport const toAddress = (value: string | number | bigint | ArrayBufferLike, config: HexConfig = {}): Address => {\n const { bitLength = 160, prefix = false } = config\n return hexFrom(value, {\n bitLength, prefix, ...config,\n }) as Address\n}\n\nexport const isAddress = (value: unknown, config: HexConfig = {}): value is Address => {\n const { bitLength = 160, prefix = false } = config\n return isHex(value, { bitLength, prefix })\n}\n\nexport function asAddress(value: unknown): Address | undefined\nexport function asAddress(value: unknown, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {\n try {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value, { prefix: false })\n break\n }\n default: {\n return isObject(assert) ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n } catch (ex) {\n const error = ex as Error\n return assertError(undefined, assert, error.message)\n }\n}\n\n/** @alpha */\nexport function isAddressV2(value: unknown): value is Address {\n return AddressZodValidation.safeParse(value).success\n}\n\n/** @alpha */\nexport function asAddressV2(value: unknown, assert: boolean = false): Address | undefined {\n return assert\n ? AddressZodValidation.parse(value)\n : AddressZodValidation.safeParse(value).data\n}\n\n/** @alpha */\nexport function toAddressV2(value: unknown, assert: boolean = false): Address | undefined {\n return assert\n ? AddressZodTransform.parse(value)\n : AddressZodTransform.safeParse(value).data\n}\n","import { isString, isUndefined } from '@xylabs/typeof'\n\ntype AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (!isUndefined(assert)) {\n const assertString\n = typeof assert === 'string'\n ? assert\n : typeof assert === 'boolean'\n ? defaultMessage\n : assert(value, defaultMessage)\n if (isString(assertString) || assertString === true) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isNumber } from '@xylabs/typeof'\n\nimport { isHex } from '../is.ts'\nimport type { Hex, HexConfig } from '../model.ts'\nimport { bitsToNibbles } from '../nibble.ts'\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): Hex => {\n const {\n prefix = false, byteSize = 8, bitLength,\n } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unEvened = (value.startsWith('0x') ? value.slice(2) : value).toLowerCase()\n if (isHex(unEvened)) {\n const evenCharacters = unEvened.padStart(unEvened.length + (unEvened.length % nibbleBoundary), '0')\n const padded = isNumber(bitLength) ? evenCharacters.padStart(bitLength / 4, '0') : evenCharacters\n return (prefix ? `0x${padded}` : padded).toLowerCase() as Hex\n } else {\n throw new Error('Received string is not a value hex')\n }\n}\n","export const HexRegExMinMax = (minBytes = 0, maxBytes: number = (Number.MAX_SAFE_INTEGER / 2)) => {\n return new RegExp(`^[a-f0-9]{${minBytes * 2},${maxBytes * 2}}$`)\n}\n\nexport const HexRegExMinMaxMixedCaseWithPrefix = (minBytes = 0, maxBytes: number = (Number.MAX_SAFE_INTEGER / 2)) => {\n return new RegExp(`^0x[a-fA-F0-9]{${minBytes * 2},${maxBytes * 2}}$`)\n}\n\nexport const HexRegEx = /^[0-9a-f]+$/\nexport const HexRegExWithPrefix = /^0x[0-9a-f]+$/\n","// determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n// determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","import { HexRegEx, HexRegExWithPrefix } from '../HexRegEx.ts'\nimport type { Hex, HexConfig } from './model.ts'\nimport { bitsToNibbles } from './nibble.ts'\n\nexport const isHex = (value: unknown, config?: HexConfig): value is Hex => {\n // Is it a string?\n if (typeof value !== 'string') return false\n\n const valueCharLength = config?.prefix ? value.length - 2 : value.length\n\n // If a bitLength specified, does it conform?\n if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false\n\n // Does it only has hex values?\n return config?.prefix ? HexRegExWithPrefix.test(value) : HexRegEx.test(value)\n}\n","import type { Hex, HexConfig } from '../model.ts'\nimport { hexFromHexString } from './fromHexString.ts'\n\n/** Convert an ArrayBuffer to a hex string */\nexport const hexFromArrayBuffer = (\n /** The buffer to be converted */\n buffer: ArrayBufferLike,\n /** Configuration of output format and validation */\n config?: HexConfig,\n): Hex => {\n const unPadded = [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join('')\n return hexFromHexString(unPadded, config)\n}\n","import type { Hex, HexConfig } from '../model.ts'\nimport { hexFromHexString } from './fromHexString.ts'\n\n/** Convert a bigint to a hex string */\nexport const hexFromBigInt = (\n /** The bigint to be converted */\n value: bigint,\n /** Configuration of output format and validation */\n config: HexConfig = {},\n): Hex => {\n const unPadded = value.toString(16)\n return hexFromHexString(unPadded, config)\n}\n","import type { Hex, HexConfig } from '../model.ts'\nimport { hexFromBigInt } from './fromBigInt.ts'\n\nexport const hexFromNumber = (value: number, config?: HexConfig): Hex => {\n return hexFromBigInt(BigInt(value), config)\n}\n","import type { Hex, HexConfig } from '../model.ts'\nimport { hexFromArrayBuffer } from './fromArrayBuffer.ts'\nimport { hexFromBigInt } from './fromBigInt.ts'\nimport { hexFromHexString } from './fromHexString.ts'\nimport { hexFromNumber } from './fromNumber.ts'\n\n/** Takes unknown value and tries our best to convert it to a hex string */\nexport const hexFrom = (\n /** Supported types are string, number, bigint, and ArrayBuffer */\n value: string | number | bigint | ArrayBufferLike,\n /** Configuration of output format and validation */\n config?: HexConfig,\n): Hex => {\n switch (typeof value) {\n case 'string': {\n return hexFromHexString(value, config)\n }\n case 'bigint': {\n return hexFromBigInt(value, config)\n }\n case 'number': {\n return hexFromNumber(value, config)\n }\n case 'object': {\n return hexFromArrayBuffer(value, config)\n }\n default: {\n throw new Error(`Invalid type: ${typeof value}`)\n }\n }\n}\n","import type { AssertConfig } from '../assert.ts'\nimport { assertError } from '../assert.ts'\nimport { hexFromHexString } from './from/index.ts'\nimport { isHex } from './is.ts'\nimport type { Hex } from './model.ts'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","import { isString } from '@xylabs/typeof'\n\nimport { hexFromHexString } from './from/index.ts'\n\nexport const isHexZero = (value?: string) => {\n return isString(value) ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : undefined\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join('')\n}\n","import type { Brand } from '@xylabs/typeof'\nimport z from 'zod'\n\nimport { HexRegEx } from '../HexRegEx.ts'\n\n// using true instead of unique symbol to avoid conflicts with other versions of library\nexport type Hex = Brand<Lowercase<string>, { readonly __hex: true }>\n\nexport const HexZod = z.string()\n .regex(HexRegEx, { message: 'Invalid hex format' }).transform(x => x as Hex)\n\n/** Configuration of validation and output format */\nexport interface HexConfig {\n bitLength?: number\n byteSize?: number\n prefix?: boolean\n}\n\nexport type HexZodType = z.infer<typeof HexZod>\n","import { hexFrom } from './from/index.ts'\nimport type { HexConfig } from './model.ts'\n\n/** takes any value and tries our best to convert it to a hex string */\nexport const toHex = (\n /** Supported types are string, number, bigint, and ArrayBuffer */\n value: string | number | bigint | ArrayBufferLike,\n /** Configuration of output format and validation */\n config: HexConfig = {},\n) => {\n const { prefix = false } = config\n return hexFrom(value, { prefix, ...config })\n}\n","import z from 'zod'\n\nimport type { Address } from './address.ts'\nimport { AddressZodValidation } from './AddressZodValidation.ts'\n\nexport const AddressZodTransform = z.union([z.string(), z.bigint()])\n .transform((value) => {\n if (typeof value === 'bigint') {\n return value.toString(16).padStart(40, '0')\n }\n if (value.startsWith('0x')) {\n return value.slice(2)\n }\n return value\n })\n .refine(x => AddressZodValidation.safeParse(x).data)\n .transform(x => x as Address)\n\nexport type AddressZodTransformType = z.infer<typeof AddressZodTransform>\n","import z from 'zod'\n\nimport { HexZod } from '../hex/index.ts'\nimport type { Address } from './address.ts'\nimport { ADDRESS_LENGTH } from './address.ts'\n\nexport const AddressZodValidation = z.string()\n .refine(x => HexZod.safeParse(x))\n .refine(x => x.length === ADDRESS_LENGTH, { error: e => new Error(`Address must have 40 characters [${e.input}]`) })\n .transform(v => v as Address)\n\nexport type AddressZodValidationType = z.infer<typeof AddressZodValidation>\n","import type { Brand } from '@xylabs/typeof'\nimport z from 'zod'\n\nimport type { AssertConfig } from './assert.ts'\nimport { assertError } from './assert.ts'\nimport type { HexConfig } from './hex/index.ts'\nimport {\n hexFrom, hexFromHexString, isHex,\n} from './hex/index.ts'\nimport { HexRegExMinMaxMixedCaseWithPrefix } from './HexRegEx.ts'\n\nexport const EthAddressRegEx = HexRegExMinMaxMixedCaseWithPrefix(20, 20)\n\nexport const EthAddressToStringSchema = z.string().regex(EthAddressRegEx)\nexport const EthAddressFromStringSchema = z.string().regex(EthAddressRegEx).transform(v => toEthAddress(v))\n\n// using true instead of unique symbol to avoid conflicts with other versions of library\nexport type EthAddress = Brand<string, { readonly __eth_address: true }>\n\nexport const ETH_ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' as EthAddress\n\nexport const toEthAddress = (value: string | number | bigint | ArrayBufferLike, config: HexConfig = {}): EthAddress => {\n const { bitLength = 160, prefix = false } = config\n return `0x${hexFrom(value, {\n bitLength, prefix, ...config,\n })}` as EthAddress\n}\n\nexport const isEthAddress = (value: unknown, config: HexConfig = {}): value is EthAddress => {\n const { bitLength = 160, prefix = true } = config\n return isHex(value, { bitLength, prefix })\n}\n\nexport const EthAddressZod = z.string()\n .regex(EthAddressRegEx, { message: 'Invalid address format' })\n .refine(\n isEthAddress,\n )\n\nexport function asEthAddress(value: unknown): EthAddress | undefined\nexport function asEthAddress(value: unknown, assert: AssertConfig): EthAddress\nexport function asEthAddress(value: unknown, assert?: AssertConfig): EthAddress | undefined {\n try {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value, { prefix: true })\n break\n }\n default: {\n if (value !== undefined) {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n }\n return isEthAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an EthAddress [${value}]`)\n } catch (ex) {\n const error = ex as Error\n return assertError(undefined, assert, error.message)\n }\n}\n","import type { Brand } from '@xylabs/typeof'\nimport { isUndefined } from '@xylabs/typeof'\nimport z from 'zod'\n\nimport type { AssertConfig } from './assert.ts'\nimport { assertError } from './assert.ts'\nimport type { Hex } from './hex/index.ts'\nimport { hexFromHexString, isHex } from './hex/index.ts'\nimport { HexRegExMinMax } from './HexRegEx.ts'\n\nexport const HashRegEx = HexRegExMinMax(32, 32)\n\nexport const ZERO_HASH = '0000000000000000000000000000000000000000000000000000000000000000' as Hash\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\n// using true instead of unique symbol to avoid conflicts with other versions of library\nexport type Hash = Brand<Hex, { readonly __hash: true }>\n\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\n}\n\nexport const HashZod = z.string()\n .toLowerCase()\n .regex(HashRegEx, { message: 'Invalid hash format' })\n .refine(\n isHash,\n )\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return isUndefined(assert) ? undefined : assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n","import { type Hex, hexFromHexString } from './hex/index.ts'\n\nexport function hexToBigInt(hex: Hex): bigint {\n return BigInt(hexFromHexString(hex, { prefix: true }))\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,OAAOA,QAAO;;;ACDd,SAAS,UAAU,mBAAmB;AAM/B,IAAM,cAAc,CAAC,OAAgB,QAAkC,mBAA2B;AACvG,MAAI,CAAC,YAAY,MAAM,GAAG;AACxB,UAAM,eACF,OAAO,WAAW,WAChB,SACA,OAAO,WAAW,YAChB,iBACA,OAAO,OAAO,cAAc;AACpC,QAAI,SAAS,YAAY,KAAK,iBAAiB,MAAM;AACnD,YAAM,IAAI,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACvE;AAAA,EACF;AAEA,SAAO;AACT;;;ACpBA,SAAS,gBAAgB;;;ACAlB,IAAM,iBAAiB,CAAC,WAAW,GAAG,WAAoB,OAAO,mBAAmB,MAAO;AAChG,SAAO,IAAI,OAAO,aAAa,WAAW,CAAC,IAAI,WAAW,CAAC,IAAI;AACjE;AAEO,IAAM,oCAAoC,CAAC,WAA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AY,QAAW,QAAQ,MAAM,OAAO;AAAA,EACrD;AACF;;;AC5DA,SAAS,eAAAC,oBAAmB;AAC5B,OAAOC,QAAO;AAQP,IAAM,YAAY,eAAe,IAAI,EAAE;AAEvC,IAAM,YAAY;AAGlB,IAAM,gBAAiC,CAAC,IAAI,IAAI,KAAK,KAAK,KAAK,MAAM,MAAM,IAAI;AAE/E,IAAM,kBAAkB,CAAC,UAA2C;AACzE,SAAO,OAAO,UAAU,YAAY,cAAc,SAAS,KAAsB;AACnF;AAKO,IAAM,SAAS,CAAC,OAAgB,YAA2B,QAAuB;AACvF,SAAO,MAAM,OAAO,EAAE,UAAU,CAAC;AACnC;AAEO,IAAM,UAAUC,GAAE,OAAO,EAC7B,YAAY,EACZ,MAAM,WAAW,EAAE,SAAS,sBAAsB,CAAC,EACnD;AAAA,EACC;AACF;AAIK,SAAS,OAAO,OAAgB,QAAyC;AAC9E,MAAI,cAAkC;AAEtC,UAAQ,OAAO,OAAO;AAAA,IACpB,KAAK,UAAU;AACb,oBAAc,iBAAiB,KAAK;AACpC;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAOC,aAAY,MAAM,IAAI,SAAY,YAAY,OAAO,QAAQ,qBAAqB,OAAO,KAAK,GAAG;AAAA,IAC1G;AAAA,EACF;AACA,SAAO,OAAO,WAAW,IAAI,cAAc,YAAY,OAAO,QAAQ,wBAAwB,KAAK,GAAG;AACxG;;;AChDO,SAAS,YAAY,KAAkB;AAC5C,SAAO,OAAO,iBAAiB,KAAK,EAAE,QAAQ,KAAK,CAAC,CAAC;AACvD;","names":["z","isString","isString","z","z","z","z","z","z","z","isUndefined","z","z","isUndefined"]}
1
+ {"version":3,"sources":["../../src/address/address.ts","../../src/HexRegEx.ts","../../src/address/addressDeprecated.ts","../../src/assert.ts","../../src/hex/from/fromHexString.ts","../../src/hex/nibble.ts","../../src/hex/is.ts","../../src/hex/from/fromArrayBuffer.ts","../../src/hex/from/fromBigInt.ts","../../src/hex/from/fromNumber.ts","../../src/hex/from/from.ts","../../src/hex/as.ts","../../src/hex/hex.ts","../../src/hex/isHexZero.ts","../../src/hex/legacy.ts","../../src/hex/to.ts","../../src/address/AddressTransformZod.ts","../../src/address/AddressValidationZod.ts","../../src/address/to.ts","../../src/address/as.ts","../../src/address/is.ts","../../src/ethAddress.ts","../../src/hash/as.ts","../../src/hash/is.ts","../../src/hash/hash.ts","../../src/hexToBigInt.ts"],"sourcesContent":["import type { Brand } from '@xylabs/typeof'\nimport z from 'zod'\n\nimport type { Hex } from '../hex/index.ts'\nimport { HexRegExMinMax } from '../HexRegEx.ts'\n\nexport const ZERO_ADDRESS = '0000000000000000000000000000000000000000' as Address\nexport const ADDRESS_LENGTH = 40 as const\n\nexport const AddressRegEx = HexRegExMinMax(ADDRESS_LENGTH / 2, ADDRESS_LENGTH / 2)\n\nexport const AddressZod = z.string()\n .regex(AddressRegEx, { message: 'Invalid hex format' }) as unknown as z.ZodType<Brand<Hex, { readonly __address: true }>>\n\nexport type Address = z.infer<typeof AddressZod>\n","export const HexRegExMinMax = (minBytes = 0, maxBytes: number = (Number.MAX_SAFE_INTEGER / 2)) => {\n return new RegExp(`^[a-f0-9]{${minBytes * 2},${maxBytes * 2}}$`)\n}\n\nexport const HexRegExMinMaxMixedCaseWithPrefix = (minBytes = 0, maxBytes: number = (Number.MAX_SAFE_INTEGER / 2)) => {\n return new RegExp(`^0x[a-fA-F0-9]{${minBytes * 2},${maxBytes * 2}}$`)\n}\n\nexport const HexRegEx = /^[0-9a-f]+$/\nexport const HexRegExWithPrefix = /^0x[0-9a-f]+$/\n","import z from 'zod'\n\nimport { AddressRegEx } from './address.ts'\nimport { toAddress } from './to.ts'\n\n/** @deprecated use AddressZod */\nexport const AddressToStringZod = z.string().regex(AddressRegEx)\n\n/** @deprecated use AddressZod */\n// eslint-disable-next-line sonarjs/deprecation\nexport const AddressToStringSchema = AddressToStringZod\n\n/** @deprecated use AddressZod */\nexport const AddressFromStringZod = z.string().toLowerCase().regex(AddressRegEx).transform(v => toAddress(v))\n\n/** @deprecated use AddressZod */\n// eslint-disable-next-line sonarjs/deprecation\nexport const AddressFromStringSchema = AddressFromStringZod\n","import { isString, isUndefined } from '@xylabs/typeof'\n\ntype AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (!isUndefined(assert)) {\n const assertString\n = typeof assert === 'string'\n ? assert\n : typeof assert === 'boolean'\n ? defaultMessage\n : assert(value, defaultMessage)\n if (isString(assertString) || assertString === true) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isNumber } from '@xylabs/typeof'\n\nimport type { Hex, HexConfig } from '../hex.ts'\nimport { isHex } from '../is.ts'\nimport { bitsToNibbles } from '../nibble.ts'\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): Hex => {\n const {\n prefix = false, byteSize = 8, bitLength,\n } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unEvened = (value.startsWith('0x') ? value.slice(2) : value).toLowerCase()\n if (isHex(unEvened)) {\n const evenCharacters = unEvened.padStart(unEvened.length + (unEvened.length % nibbleBoundary), '0')\n const padded = isNumber(bitLength) ? evenCharacters.padStart(bitLength / 4, '0') : evenCharacters\n return (prefix ? `0x${padded}` : padded).toLowerCase() as Hex\n } else {\n throw new Error('Received string is not a value hex')\n }\n}\n","// determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n// determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","import { HexRegEx, HexRegExWithPrefix } from '../HexRegEx.ts'\nimport type { Hex, HexConfig } from './hex.ts'\nimport { bitsToNibbles } from './nibble.ts'\n\nexport const isHex = (value: unknown, config?: HexConfig): value is Hex => {\n // Is it a string?\n if (typeof value !== 'string') return false\n\n const valueCharLength = config?.prefix ? value.length - 2 : value.length\n\n // If a bitLength specified, does it conform?\n if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false\n\n // Does it only has hex values?\n return config?.prefix ? HexRegExWithPrefix.test(value) : HexRegEx.test(value)\n}\n","import type { Hex, HexConfig } from '../hex.ts'\nimport { hexFromHexString } from './fromHexString.ts'\n\n/** Convert an ArrayBuffer to a hex string */\nexport const hexFromArrayBuffer = (\n /** The buffer to be converted */\n buffer: ArrayBufferLike,\n /** Configuration of output format and validation */\n config?: HexConfig,\n): Hex => {\n const unPadded = [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join('')\n return hexFromHexString(unPadded, config)\n}\n","import type { Hex, HexConfig } from '../hex.ts'\nimport { hexFromHexString } from './fromHexString.ts'\n\n/** Convert a bigint to a hex string */\nexport const hexFromBigInt = (\n /** The bigint to be converted */\n value: bigint,\n /** Configuration of output format and validation */\n config: HexConfig = {},\n): Hex => {\n const unPadded = value.toString(16)\n return hexFromHexString(unPadded, config)\n}\n","import type { Hex, HexConfig } from '../hex.ts'\nimport { hexFromBigInt } from './fromBigInt.ts'\n\nexport const hexFromNumber = (value: number, config?: HexConfig): Hex => {\n return hexFromBigInt(BigInt(value), config)\n}\n","import type { Hex, HexConfig } from '../hex.ts'\nimport { hexFromArrayBuffer } from './fromArrayBuffer.ts'\nimport { hexFromBigInt } from './fromBigInt.ts'\nimport { hexFromHexString } from './fromHexString.ts'\nimport { hexFromNumber } from './fromNumber.ts'\n\n/** Takes unknown value and tries our best to convert it to a hex string */\nexport const hexFrom = (\n /** Supported types are string, number, bigint, and ArrayBuffer */\n value: string | number | bigint | ArrayBufferLike,\n /** Configuration of output format and validation */\n config?: HexConfig,\n): Hex => {\n switch (typeof value) {\n case 'string': {\n return hexFromHexString(value, config)\n }\n case 'bigint': {\n return hexFromBigInt(value, config)\n }\n case 'number': {\n return hexFromNumber(value, config)\n }\n case 'object': {\n return hexFromArrayBuffer(value, config)\n }\n default: {\n throw new Error(`Invalid type: ${typeof value}`)\n }\n }\n}\n","import type { AssertConfig } from '../assert.ts'\nimport { assertError } from '../assert.ts'\nimport { hexFromHexString } from './from/index.ts'\nimport type { Hex } from './hex.ts'\nimport { isHex } from './is.ts'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","import type { Brand } from '@xylabs/typeof'\nimport z from 'zod'\n\nimport { HexRegEx } from '../HexRegEx.ts'\n\nexport const HexZod = z.string()\n .regex(HexRegEx, { message: 'Invalid hex format' }) as unknown as z.ZodType<Brand<Lowercase<string>, { readonly __hex: true }>>\n\nexport type Hex = z.infer<typeof HexZod>\n\n/** Configuration of validation and output format */\nexport interface HexConfig {\n bitLength?: number\n byteSize?: number\n prefix?: boolean\n}\n","import { isString } from '@xylabs/typeof'\n\nimport { hexFromHexString } from './from/index.ts'\n\nexport const isHexZero = (value?: string) => {\n return isString(value) ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : undefined\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join('')\n}\n","import { hexFrom } from './from/index.ts'\nimport type { HexConfig } from './hex.ts'\n\n/** takes any value and tries our best to convert it to a hex string */\nexport const toHex = (\n /** Supported types are string, number, bigint, and ArrayBuffer */\n value: string | number | bigint | ArrayBufferLike,\n /** Configuration of output format and validation */\n config: HexConfig = {},\n) => {\n const { prefix = false } = config\n return hexFrom(value, { prefix, ...config })\n}\n","import z from 'zod'\n\nimport { type Address, ADDRESS_LENGTH } from './address.ts'\nimport { AddressValidationZod } from './AddressValidationZod.ts'\n\nexport const AddressTransformZod = z.union([z.string(), z.bigint(), z.number()])\n .transform((value) => {\n switch (typeof value) {\n case 'bigint': {\n return value.toString(16).padStart(ADDRESS_LENGTH, '0')\n }\n case 'string': {\n if (value.startsWith('0x')) {\n return value.slice(2)\n }\n return value\n }\n case 'number': {\n return BigInt(value).toString(16).padStart(ADDRESS_LENGTH, '0')\n }\n }\n })\n .refine(x => AddressValidationZod.safeParse(x).data)\n .transform(x => x as Address)\n\nexport type AddressTransformZodType = z.infer<typeof AddressTransformZod>\n","import z from 'zod'\n\nimport { HexZod } from '../hex/index.ts'\nimport type { Address } from './address.ts'\nimport { ADDRESS_LENGTH } from './address.ts'\n\nexport const AddressValidationZod = z.string()\n .refine(x => HexZod.safeParse(x))\n .refine(x => x.length === ADDRESS_LENGTH, { error: e => new Error(`Address must have 40 characters [${e.input}]`) })\n .transform(v => v as Address)\n\nexport type AddressValidationZodType = z.infer<typeof AddressValidationZod>\n","import type { HexConfig } from '../hex/index.ts'\nimport { hexFrom } from '../hex/index.ts'\nimport type { Address } from './address.ts'\nimport { AddressTransformZod } from './AddressTransformZod.ts'\n\nexport const toAddress = (value: string | number | bigint | ArrayBufferLike, config: HexConfig = {}): Address => {\n const { bitLength = 160, prefix = false } = config\n return hexFrom(value, {\n bitLength, prefix, ...config,\n }) as unknown as Address\n}\n\n/** @alpha */\nexport function toAddressV2(value: unknown, assert: boolean = false): Address | undefined {\n return assert\n ? AddressTransformZod.parse(value)\n : AddressTransformZod.safeParse(value).data\n}\n","import { isObject } from '@xylabs/typeof'\n\nimport type { AssertConfig } from '../assert.ts'\nimport { assertError } from '../assert.ts'\nimport { hexFromHexString } from '../hex/index.ts'\nimport type { Address } from './address.ts'\nimport { AddressValidationZod } from './AddressValidationZod.ts'\nimport { isAddress } from './is.ts'\n\nexport function asAddress(value: unknown): Address | undefined\nexport function asAddress(value: unknown, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {\n try {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value, { prefix: false })\n break\n }\n default: {\n return isObject(assert) ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n } catch (ex) {\n const error = ex as Error\n return assertError(undefined, assert, error.message)\n }\n}\n\n/** @alpha */\nexport function asAddressV2(value: unknown, assert: boolean = false): Address | undefined {\n return assert\n ? AddressValidationZod.parse(value)\n : AddressValidationZod.safeParse(value).data\n}\n","import type { HexConfig } from '../hex/index.ts'\nimport { isHex } from '../hex/index.ts'\nimport type { Address } from './address.ts'\nimport { AddressValidationZod } from './AddressValidationZod.ts'\n\nexport const isAddress = (value: unknown, config: HexConfig = {}): value is Address => {\n const { bitLength = 160, prefix = false } = config\n return isHex(value, { bitLength, prefix })\n}\n\n/** @alpha */\nexport function isAddressV2(value: unknown): value is Address {\n return AddressValidationZod.safeParse(value).success\n}\n","import type { Brand } from '@xylabs/typeof'\nimport z from 'zod'\n\nimport type { AssertConfig } from './assert.ts'\nimport { assertError } from './assert.ts'\nimport type { HexConfig } from './hex/index.ts'\nimport {\n hexFrom, hexFromHexString, isHex,\n} from './hex/index.ts'\nimport { HexRegExMinMaxMixedCaseWithPrefix } from './HexRegEx.ts'\n\nexport const EthAddressRegEx = HexRegExMinMaxMixedCaseWithPrefix(20, 20)\n\nexport const EthAddressToStringZod = z.string().regex(EthAddressRegEx)\n\n/** @deprecated use EthAddressToStringZod */\nexport const EthAddressToStringSchema = EthAddressToStringZod\n\nexport const EthAddressFromStringZod = z.string().regex(EthAddressRegEx).transform(v => toEthAddress(v))\n\n/** @deprecated use EthAddressFromStringZod */\nexport const EthAddressFromStringSchema = EthAddressFromStringZod\n\n// using true instead of unique symbol to avoid conflicts with other versions of library\nexport type EthAddress = Brand<string, { readonly __eth_address: true }>\n\nexport const ETH_ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' as EthAddress\n\nexport const toEthAddress = (value: string | number | bigint | ArrayBufferLike, config: HexConfig = {}): EthAddress => {\n const { bitLength = 160, prefix = false } = config\n return `0x${hexFrom(value, {\n bitLength, prefix, ...config,\n })}` as EthAddress\n}\n\nexport const isEthAddress = (value: unknown, config: HexConfig = {}): value is EthAddress => {\n const { bitLength = 160, prefix = true } = config\n return isHex(value, { bitLength, prefix })\n}\n\nexport const EthAddressZod = z.string()\n .regex(EthAddressRegEx, { message: 'Invalid address format' })\n .refine(\n isEthAddress,\n )\n\nexport function asEthAddress(value: unknown): EthAddress | undefined\nexport function asEthAddress(value: unknown, assert: AssertConfig): EthAddress\nexport function asEthAddress(value: unknown, assert?: AssertConfig): EthAddress | undefined {\n try {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value, { prefix: true })\n break\n }\n default: {\n if (value !== undefined) {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n }\n return isEthAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an EthAddress [${value}]`)\n } catch (ex) {\n const error = ex as Error\n return assertError(undefined, assert, error.message)\n }\n}\n","import { isUndefined } from '@xylabs/typeof'\n\nimport type { AssertConfig } from '../assert.ts'\nimport { assertError } from '../assert.ts'\nimport { hexFromHexString } from '../hex/index.ts'\nimport { type Hash } from './hash.ts'\nimport { isHash } from './is.ts'\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return isUndefined(assert) ? undefined : assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n","import { isHex } from '../hex/index.ts'\nimport type { Hash, HashBitLength } from './hash.ts'\n\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\n}\n","import type { Brand } from '@xylabs/typeof'\nimport z from 'zod'\n\nimport type { Hex } from '../hex/index.ts'\nimport { HexRegExMinMax } from '../HexRegEx.ts'\n\nexport const HASH_LENGTH = 32 as const\n\nexport const HashRegEx = HexRegExMinMax(HASH_LENGTH, HASH_LENGTH)\n\nexport const ZERO_HASH = '0000000000000000000000000000000000000000000000000000000000000000' as Hash\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport const HashZod = z.string()\n .regex(HashRegEx, { message: 'Invalid hex format' }) as unknown as z.ZodType<Brand<Hex, { readonly __hash: true }>>\n\nexport type Hash = z.infer<typeof HashZod>\n","import { type Hex, hexFromHexString } from './hex/index.ts'\n\nexport function hexToBigInt(hex: Hex): bigint {\n return BigInt(hexFromHexString(hex, { prefix: true }))\n}\n"],"mappings":";AACA,OAAO,OAAO;;;ACDP,IAAM,iBAAiB,CAAC,WAAW,GAAG,WAAoB,OAAO,mBAAmB,MAAO;AAChG,SAAO,IAAI,OAAO,aAAa,WAAW,CAAC,IAAI,WAAW,CAAC,IAAI;AACjE;AAEO,IAAM,oCAAoC,CAAC,WAAW,GAAG,WAAoB,OAAO,mBAAmB,MAAO;AACnH,SAAO,IAAI,OAAO,kBAAkB,WAAW,CAAC,IAAI,WAAW,CAAC,IAAI;AACtE;AAEO,IAAM,WAAW;AACjB,IAAM,qBAAqB;;;ADH3B,IAAM,eAAe;AACrB,IAAM,iBAAiB;AAEvB,IAAM,eAAe,eAAe,iBAAiB,GAAG,iBAAiB,CAAC;AAE1E,IAAM,aAAa,EAAE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;AAEO,IAAM,UAAUC,GAAE,OAAO,EAC7B,MAAM,WAAW,EAAE,SAAS,qBAAqB,CAAC;;;AClB9C,SAAS,YAAY,KAAkB;AAC5C,SAAO,OAAO,iBAAiB,KAAK,EAAE,QAAQ,KAAK,CAAC,CAAC;AACvD;","names":["z","z","z","isString","isString","z","z","z","z","z","z","z","isUndefined","isUndefined","z","z"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/hex",
3
- "version": "5.0.4",
3
+ "version": "5.0.6",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "hex",
@@ -47,13 +47,13 @@
47
47
  "src"
48
48
  ],
49
49
  "dependencies": {
50
- "@xylabs/typeof": "~5.0.4",
51
- "zod": "~4.0.15"
50
+ "@xylabs/typeof": "~5.0.6",
51
+ "zod": "~4.0.17"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@xylabs/ts-scripts-yarn3": "~7.1.0",
55
55
  "@xylabs/tsconfig": "~7.1.0",
56
- "@xylabs/vitest-extended": "~5.0.4",
56
+ "@xylabs/vitest-extended": "~5.0.6",
57
57
  "typescript": "~5.9.2",
58
58
  "vitest": "~3.2.4"
59
59
  },
@@ -0,0 +1,26 @@
1
+ import z from 'zod'
2
+
3
+ import { type Address, ADDRESS_LENGTH } from './address.ts'
4
+ import { AddressValidationZod } from './AddressValidationZod.ts'
5
+
6
+ export const AddressTransformZod = z.union([z.string(), z.bigint(), z.number()])
7
+ .transform((value) => {
8
+ switch (typeof value) {
9
+ case 'bigint': {
10
+ return value.toString(16).padStart(ADDRESS_LENGTH, '0')
11
+ }
12
+ case 'string': {
13
+ if (value.startsWith('0x')) {
14
+ return value.slice(2)
15
+ }
16
+ return value
17
+ }
18
+ case 'number': {
19
+ return BigInt(value).toString(16).padStart(ADDRESS_LENGTH, '0')
20
+ }
21
+ }
22
+ })
23
+ .refine(x => AddressValidationZod.safeParse(x).data)
24
+ .transform(x => x as Address)
25
+
26
+ export type AddressTransformZodType = z.infer<typeof AddressTransformZod>
@@ -4,9 +4,9 @@ import { HexZod } from '../hex/index.ts'
4
4
  import type { Address } from './address.ts'
5
5
  import { ADDRESS_LENGTH } from './address.ts'
6
6
 
7
- export const AddressZodValidation = z.string()
7
+ export const AddressValidationZod = z.string()
8
8
  .refine(x => HexZod.safeParse(x))
9
9
  .refine(x => x.length === ADDRESS_LENGTH, { error: e => new Error(`Address must have 40 characters [${e.input}]`) })
10
10
  .transform(v => v as Address)
11
11
 
12
- export type AddressZodValidationType = z.infer<typeof AddressZodValidation>
12
+ export type AddressValidationZodType = z.infer<typeof AddressValidationZod>
@@ -1,85 +1,15 @@
1
- import { isObject } from '@xylabs/typeof'
1
+ import type { Brand } from '@xylabs/typeof'
2
2
  import z from 'zod'
3
3
 
4
- import type { AssertConfig } from '../assert.ts'
5
- import { assertError } from '../assert.ts'
6
- import type { Hex, HexConfig } from '../hex/index.ts'
7
- import {
8
- hexFrom, hexFromHexString, isHex,
9
- } from '../hex/index.ts'
4
+ import type { Hex } from '../hex/index.ts'
10
5
  import { HexRegExMinMax } from '../HexRegEx.ts'
11
- import { AddressZodTransform } from './AddressZodTransform.ts'
12
- import { AddressZodValidation } from './AddressZodValidation.ts'
13
-
14
- // using true instead of unique symbol to avoid conflicts with other versions of library
15
- export type Address = Hex & {
16
- readonly __address: true
17
- }
18
-
19
- export const AddressRegEx = HexRegExMinMax(20, 20)
20
-
21
- export const AddressToStringZod = z.string().regex(AddressRegEx)
22
-
23
- /** @deprecated use AddressToStringZod */
24
- export const AddressToStringSchema = AddressToStringZod
25
-
26
- export const AddressFromStringZod = z.string().toLowerCase().regex(AddressRegEx).transform(v => toAddress(v))
27
-
28
- /** @deprecated use AddressFromStringZod */
29
- export const AddressFromStringSchema = AddressFromStringZod
30
6
 
31
7
  export const ZERO_ADDRESS = '0000000000000000000000000000000000000000' as Address
32
8
  export const ADDRESS_LENGTH = 40 as const
33
9
 
34
- export const toAddress = (value: string | number | bigint | ArrayBufferLike, config: HexConfig = {}): Address => {
35
- const { bitLength = 160, prefix = false } = config
36
- return hexFrom(value, {
37
- bitLength, prefix, ...config,
38
- }) as Address
39
- }
40
-
41
- export const isAddress = (value: unknown, config: HexConfig = {}): value is Address => {
42
- const { bitLength = 160, prefix = false } = config
43
- return isHex(value, { bitLength, prefix })
44
- }
45
-
46
- export function asAddress(value: unknown): Address | undefined
47
- export function asAddress(value: unknown, assert: AssertConfig): Address
48
- export function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {
49
- try {
50
- let stringValue: string | undefined = undefined
51
-
52
- switch (typeof value) {
53
- case 'string': {
54
- stringValue = hexFromHexString(value, { prefix: false })
55
- break
56
- }
57
- default: {
58
- return isObject(assert) ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined
59
- }
60
- }
61
- return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)
62
- } catch (ex) {
63
- const error = ex as Error
64
- return assertError(undefined, assert, error.message)
65
- }
66
- }
67
-
68
- /** @alpha */
69
- export function isAddressV2(value: unknown): value is Address {
70
- return AddressZodValidation.safeParse(value).success
71
- }
10
+ export const AddressRegEx = HexRegExMinMax(ADDRESS_LENGTH / 2, ADDRESS_LENGTH / 2)
72
11
 
73
- /** @alpha */
74
- export function asAddressV2(value: unknown, assert: boolean = false): Address | undefined {
75
- return assert
76
- ? AddressZodValidation.parse(value)
77
- : AddressZodValidation.safeParse(value).data
78
- }
12
+ export const AddressZod = z.string()
13
+ .regex(AddressRegEx, { message: 'Invalid hex format' }) as unknown as z.ZodType<Brand<Hex, { readonly __address: true }>>
79
14
 
80
- /** @alpha */
81
- export function toAddressV2(value: unknown, assert: boolean = false): Address | undefined {
82
- return assert
83
- ? AddressZodTransform.parse(value)
84
- : AddressZodTransform.safeParse(value).data
85
- }
15
+ export type Address = z.infer<typeof AddressZod>
@@ -0,0 +1,18 @@
1
+ import z from 'zod'
2
+
3
+ import { AddressRegEx } from './address.ts'
4
+ import { toAddress } from './to.ts'
5
+
6
+ /** @deprecated use AddressZod */
7
+ export const AddressToStringZod = z.string().regex(AddressRegEx)
8
+
9
+ /** @deprecated use AddressZod */
10
+ // eslint-disable-next-line sonarjs/deprecation
11
+ export const AddressToStringSchema = AddressToStringZod
12
+
13
+ /** @deprecated use AddressZod */
14
+ export const AddressFromStringZod = z.string().toLowerCase().regex(AddressRegEx).transform(v => toAddress(v))
15
+
16
+ /** @deprecated use AddressZod */
17
+ // eslint-disable-next-line sonarjs/deprecation
18
+ export const AddressFromStringSchema = AddressFromStringZod
@@ -0,0 +1,37 @@
1
+ import { isObject } from '@xylabs/typeof'
2
+
3
+ import type { AssertConfig } from '../assert.ts'
4
+ import { assertError } from '../assert.ts'
5
+ import { hexFromHexString } from '../hex/index.ts'
6
+ import type { Address } from './address.ts'
7
+ import { AddressValidationZod } from './AddressValidationZod.ts'
8
+ import { isAddress } from './is.ts'
9
+
10
+ export function asAddress(value: unknown): Address | undefined
11
+ export function asAddress(value: unknown, assert: AssertConfig): Address
12
+ export function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {
13
+ try {
14
+ let stringValue: string | undefined = undefined
15
+
16
+ switch (typeof value) {
17
+ case 'string': {
18
+ stringValue = hexFromHexString(value, { prefix: false })
19
+ break
20
+ }
21
+ default: {
22
+ return isObject(assert) ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined
23
+ }
24
+ }
25
+ return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)
26
+ } catch (ex) {
27
+ const error = ex as Error
28
+ return assertError(undefined, assert, error.message)
29
+ }
30
+ }
31
+
32
+ /** @alpha */
33
+ export function asAddressV2(value: unknown, assert: boolean = false): Address | undefined {
34
+ return assert
35
+ ? AddressValidationZod.parse(value)
36
+ : AddressValidationZod.safeParse(value).data
37
+ }
@@ -1,3 +1,7 @@
1
1
  export * from './address.ts'
2
- export * from './AddressZodTransform.ts'
3
- export * from './AddressZodValidation.ts'
2
+ export * from './addressDeprecated.ts'
3
+ export * from './AddressTransformZod.ts'
4
+ export * from './AddressValidationZod.ts'
5
+ export * from './as.ts'
6
+ export * from './is.ts'
7
+ export * from './to.ts'
@@ -0,0 +1,14 @@
1
+ import type { HexConfig } from '../hex/index.ts'
2
+ import { isHex } from '../hex/index.ts'
3
+ import type { Address } from './address.ts'
4
+ import { AddressValidationZod } from './AddressValidationZod.ts'
5
+
6
+ export const isAddress = (value: unknown, config: HexConfig = {}): value is Address => {
7
+ const { bitLength = 160, prefix = false } = config
8
+ return isHex(value, { bitLength, prefix })
9
+ }
10
+
11
+ /** @alpha */
12
+ export function isAddressV2(value: unknown): value is Address {
13
+ return AddressValidationZod.safeParse(value).success
14
+ }
@@ -0,0 +1,18 @@
1
+ import type { HexConfig } from '../hex/index.ts'
2
+ import { hexFrom } from '../hex/index.ts'
3
+ import type { Address } from './address.ts'
4
+ import { AddressTransformZod } from './AddressTransformZod.ts'
5
+
6
+ export const toAddress = (value: string | number | bigint | ArrayBufferLike, config: HexConfig = {}): Address => {
7
+ const { bitLength = 160, prefix = false } = config
8
+ return hexFrom(value, {
9
+ bitLength, prefix, ...config,
10
+ }) as unknown as Address
11
+ }
12
+
13
+ /** @alpha */
14
+ export function toAddressV2(value: unknown, assert: boolean = false): Address | undefined {
15
+ return assert
16
+ ? AddressTransformZod.parse(value)
17
+ : AddressTransformZod.safeParse(value).data
18
+ }
package/src/ethAddress.ts CHANGED
@@ -11,8 +11,15 @@ import { HexRegExMinMaxMixedCaseWithPrefix } from './HexRegEx.ts'
11
11
 
12
12
  export const EthAddressRegEx = HexRegExMinMaxMixedCaseWithPrefix(20, 20)
13
13
 
14
- export const EthAddressToStringSchema = z.string().regex(EthAddressRegEx)
15
- export const EthAddressFromStringSchema = z.string().regex(EthAddressRegEx).transform(v => toEthAddress(v))
14
+ export const EthAddressToStringZod = z.string().regex(EthAddressRegEx)
15
+
16
+ /** @deprecated use EthAddressToStringZod */
17
+ export const EthAddressToStringSchema = EthAddressToStringZod
18
+
19
+ export const EthAddressFromStringZod = z.string().regex(EthAddressRegEx).transform(v => toEthAddress(v))
20
+
21
+ /** @deprecated use EthAddressFromStringZod */
22
+ export const EthAddressFromStringSchema = EthAddressFromStringZod
16
23
 
17
24
  // using true instead of unique symbol to avoid conflicts with other versions of library
18
25
  export type EthAddress = Brand<string, { readonly __eth_address: true }>
package/src/hash/as.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { isUndefined } from '@xylabs/typeof'
2
+
3
+ import type { AssertConfig } from '../assert.ts'
4
+ import { assertError } from '../assert.ts'
5
+ import { hexFromHexString } from '../hex/index.ts'
6
+ import { type Hash } from './hash.ts'
7
+ import { isHash } from './is.ts'
8
+
9
+ export function asHash(value: unknown): Hash | undefined
10
+ export function asHash(value: unknown, assert: AssertConfig): Hash
11
+ export function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {
12
+ let stringValue: string | undefined = undefined
13
+
14
+ switch (typeof value) {
15
+ case 'string': {
16
+ stringValue = hexFromHexString(value)
17
+ break
18
+ }
19
+ default: {
20
+ return isUndefined(assert) ? undefined : assertError(value, assert, `Unsupported type [${typeof value}]`)
21
+ }
22
+ }
23
+ return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)
24
+ }
@@ -0,0 +1,23 @@
1
+ import type { Brand } from '@xylabs/typeof'
2
+ import z from 'zod'
3
+
4
+ import type { Hex } from '../hex/index.ts'
5
+ import { HexRegExMinMax } from '../HexRegEx.ts'
6
+
7
+ export const HASH_LENGTH = 32 as const
8
+
9
+ export const HashRegEx = HexRegExMinMax(HASH_LENGTH, HASH_LENGTH)
10
+
11
+ export const ZERO_HASH = '0000000000000000000000000000000000000000000000000000000000000000' as Hash
12
+
13
+ export type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096
14
+ export const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]
15
+
16
+ export const isHashBitLength = (value: unknown): value is HashBitLength => {
17
+ return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)
18
+ }
19
+
20
+ export const HashZod = z.string()
21
+ .regex(HashRegEx, { message: 'Invalid hex format' }) as unknown as z.ZodType<Brand<Hex, { readonly __hash: true }>>
22
+
23
+ export type Hash = z.infer<typeof HashZod>
@@ -0,0 +1,3 @@
1
+ export * from './as.ts'
2
+ export * from './hash.ts'
3
+ export * from './is.ts'
package/src/hash/is.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { isHex } from '../hex/index.ts'
2
+ import type { Hash, HashBitLength } from './hash.ts'
3
+
4
+ export const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {
5
+ return isHex(value, { bitLength })
6
+ }
package/src/hex/as.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import type { AssertConfig } from '../assert.ts'
2
2
  import { assertError } from '../assert.ts'
3
3
  import { hexFromHexString } from './from/index.ts'
4
+ import type { Hex } from './hex.ts'
4
5
  import { isHex } from './is.ts'
5
- import type { Hex } from './model.ts'
6
6
 
7
7
  export function asHex(value: unknown): Hex | undefined
8
8
  export function asHex(value: unknown, assert: AssertConfig): Hex
@@ -1,4 +1,4 @@
1
- import type { Hex, HexConfig } from '../model.ts'
1
+ import type { Hex, HexConfig } from '../hex.ts'
2
2
  import { hexFromArrayBuffer } from './fromArrayBuffer.ts'
3
3
  import { hexFromBigInt } from './fromBigInt.ts'
4
4
  import { hexFromHexString } from './fromHexString.ts'
@@ -1,4 +1,4 @@
1
- import type { Hex, HexConfig } from '../model.ts'
1
+ import type { Hex, HexConfig } from '../hex.ts'
2
2
  import { hexFromHexString } from './fromHexString.ts'
3
3
 
4
4
  /** Convert an ArrayBuffer to a hex string */
@@ -1,4 +1,4 @@
1
- import type { Hex, HexConfig } from '../model.ts'
1
+ import type { Hex, HexConfig } from '../hex.ts'
2
2
  import { hexFromHexString } from './fromHexString.ts'
3
3
 
4
4
  /** Convert a bigint to a hex string */
@@ -1,7 +1,7 @@
1
1
  import { isNumber } from '@xylabs/typeof'
2
2
 
3
+ import type { Hex, HexConfig } from '../hex.ts'
3
4
  import { isHex } from '../is.ts'
4
- import type { Hex, HexConfig } from '../model.ts'
5
5
  import { bitsToNibbles } from '../nibble.ts'
6
6
 
7
7
  export const hexFromHexString = (value: string, config: HexConfig = {}): Hex => {
@@ -1,4 +1,4 @@
1
- import type { Hex, HexConfig } from '../model.ts'
1
+ import type { Hex, HexConfig } from '../hex.ts'
2
2
  import { hexFromBigInt } from './fromBigInt.ts'
3
3
 
4
4
  export const hexFromNumber = (value: number, config?: HexConfig): Hex => {
@@ -3,11 +3,10 @@ import z from 'zod'
3
3
 
4
4
  import { HexRegEx } from '../HexRegEx.ts'
5
5
 
6
- // using true instead of unique symbol to avoid conflicts with other versions of library
7
- export type Hex = Brand<Lowercase<string>, { readonly __hex: true }>
8
-
9
6
  export const HexZod = z.string()
10
- .regex(HexRegEx, { message: 'Invalid hex format' }).transform(x => x as Hex)
7
+ .regex(HexRegEx, { message: 'Invalid hex format' }) as unknown as z.ZodType<Brand<Lowercase<string>, { readonly __hex: true }>>
8
+
9
+ export type Hex = z.infer<typeof HexZod>
11
10
 
12
11
  /** Configuration of validation and output format */
13
12
  export interface HexConfig {
@@ -15,5 +14,3 @@ export interface HexConfig {
15
14
  byteSize?: number
16
15
  prefix?: boolean
17
16
  }
18
-
19
- export type HexZodType = z.infer<typeof HexZod>
package/src/hex/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export * from './as.ts'
2
2
  export * from './from/index.ts'
3
+ export * from './hex.ts'
3
4
  export * from './is.ts'
4
5
  export * from './isHexZero.ts'
5
6
  export * from './legacy.ts'
6
- export * from './model.ts'
7
7
  export * from './nibble.ts'
8
8
  export * from './to.ts'
package/src/hex/is.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { HexRegEx, HexRegExWithPrefix } from '../HexRegEx.ts'
2
- import type { Hex, HexConfig } from './model.ts'
2
+ import type { Hex, HexConfig } from './hex.ts'
3
3
  import { bitsToNibbles } from './nibble.ts'
4
4
 
5
5
  export const isHex = (value: unknown, config?: HexConfig): value is Hex => {
package/src/hex/to.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { hexFrom } from './from/index.ts'
2
- import type { HexConfig } from './model.ts'
2
+ import type { HexConfig } from './hex.ts'
3
3
 
4
4
  /** takes any value and tries our best to convert it to a hex string */
5
5
  export const toHex = (
@@ -1,7 +1,7 @@
1
1
  export * from './address/index.ts'
2
2
  export * from './assert.ts'
3
3
  export * from './ethAddress.ts'
4
- export * from './hash.ts'
4
+ export * from './hash/index.ts'
5
5
  export * from './hex/index.ts'
6
6
  export * from './HexRegEx.ts'
7
7
  export * from './hexToBigInt.ts'
@@ -4,7 +4,7 @@ import {
4
4
  describe, expect, test,
5
5
  } from 'vitest'
6
6
 
7
- import { isHash } from '../hash.ts'
7
+ import { isHash } from '../hash/index.ts'
8
8
  import { hexFromNumber } from '../hex/index.ts'
9
9
 
10
10
  describe('hash', () => {
@@ -1,5 +0,0 @@
1
- import z from 'zod';
2
- import type { Address } from './address.ts';
3
- export declare const AddressZodTransform: z.ZodPipe<z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodBigInt]>, z.ZodTransform<string, string | bigint>>, z.ZodTransform<Address, string>>;
4
- export type AddressZodTransformType = z.infer<typeof AddressZodTransform>;
5
- //# sourceMappingURL=AddressZodTransform.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AddressZodTransform.d.ts","sourceRoot":"","sources":["../../../src/address/AddressZodTransform.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAA;AAEnB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAG3C,eAAO,MAAM,mBAAmB,iJAWD,CAAA;AAE/B,MAAM,MAAM,uBAAuB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAA"}
@@ -1,5 +0,0 @@
1
- import z from 'zod';
2
- import type { Address } from './address.ts';
3
- export declare const AddressZodValidation: z.ZodPipe<z.ZodString, z.ZodTransform<Address, string>>;
4
- export type AddressZodValidationType = z.infer<typeof AddressZodValidation>;
5
- //# sourceMappingURL=AddressZodValidation.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AddressZodValidation.d.ts","sourceRoot":"","sources":["../../../src/address/AddressZodValidation.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAA;AAGnB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAG3C,eAAO,MAAM,oBAAoB,yDAGF,CAAA;AAE/B,MAAM,MAAM,wBAAwB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAA"}
@@ -1,17 +0,0 @@
1
- import type { Brand } from '@xylabs/typeof';
2
- import z from 'zod';
3
- import type { AssertConfig } from './assert.ts';
4
- import type { Hex } from './hex/index.ts';
5
- export declare const HashRegEx: RegExp;
6
- export declare const ZERO_HASH: Hash;
7
- export type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096;
8
- export declare const HashBitLength: HashBitLength[];
9
- export declare const isHashBitLength: (value: unknown) => value is HashBitLength;
10
- export type Hash = Brand<Hex, {
11
- readonly __hash: true;
12
- }>;
13
- export declare const isHash: (value: unknown, bitLength?: HashBitLength) => value is Hash;
14
- export declare const HashZod: z.ZodString;
15
- export declare function asHash(value: unknown): Hash | undefined;
16
- export declare function asHash(value: unknown, assert: AssertConfig): Hash;
17
- //# sourceMappingURL=hash.d.ts.map