@xylabs/hex 4.13.23 → 4.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ export declare const HexRegExMinMax: (minBytes?: number, maxBytes?: number) => RegExp;
2
+ //# sourceMappingURL=HexRegExMinMax.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HexRegExMinMax.d.ts","sourceRoot":"","sources":["../../src/HexRegExMinMax.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,GAAI,iBAAY,EAAE,WAAU,MAAsC,WAE5F,CAAA"}
@@ -1,9 +1,16 @@
1
+ import z from 'zod';
1
2
  import type { AssertConfig } from './assert.ts';
2
3
  import type { Hex, HexConfig } from './hex/index.ts';
3
- export type Address = Exclude<Hex, 'reserved-address-value'>;
4
+ export declare const AddressRegEx: RegExp;
5
+ export declare const AddressToStringSchema: z.ZodString;
6
+ export declare const AddressFromStringSchema: z.ZodPipe<z.ZodString, z.ZodTransform<Address, string>>;
7
+ export type Address = Hex & {
8
+ readonly __address: unique symbol;
9
+ };
4
10
  export declare const ZERO_ADDRESS: Address;
5
- export declare const toAddress: (value: string | number | bigint | ArrayBufferLike, config?: HexConfig) => Lowercase<string>;
11
+ export declare const toAddress: (value: string | number | bigint | ArrayBufferLike, config?: HexConfig) => Address;
6
12
  export declare const isAddress: (value: unknown, config?: HexConfig) => value is Address;
13
+ export declare const AddressZod: z.ZodString;
7
14
  export declare function asAddress(value: unknown): Address | undefined;
8
15
  export declare function asAddress(value: unknown, assert: AssertConfig): Address;
9
16
  //# sourceMappingURL=address.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,OAAO,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAKpD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gB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,GAAG,SAAS,CAAA;AAC9D,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAA"}
@@ -1,11 +1,16 @@
1
+ import z from 'zod';
1
2
  import type { AssertConfig } from './assert.ts';
2
3
  import type { Hex } from './hex/index.ts';
4
+ export declare const HashRegEx: RegExp;
3
5
  export declare const ZERO_HASH: Hash;
4
6
  export type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096;
5
7
  export declare const HashBitLength: HashBitLength[];
6
8
  export declare const isHashBitLength: (value: unknown) => value is HashBitLength;
7
- export type Hash = Exclude<Hex, 'reserved-hash-value'>;
9
+ export type Hash = Hex & {
10
+ readonly __hash: unique symbol;
11
+ };
8
12
  export declare const isHash: (value: unknown, bitLength?: HashBitLength) => value is Hash;
13
+ export declare const HashZod: z.ZodString;
9
14
  export declare function asHash(value: unknown): Hash | undefined;
10
15
  export declare function asHash(value: unknown, assert: AssertConfig): Hash;
11
16
  //# sourceMappingURL=hash.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/hash.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAA;AAGzC,eAAO,MAAM,SAAS,EAAE,IAAkF,CAAA;AAE1G,MAAM,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAA;AAC1E,eAAO,MAAM,aAAa,EAAE,aAAa,EAA8C,CAAA;AAEvF,eAAO,MAAM,eAAe,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,aAEzD,CAAA;AAED,MAAM,MAAM,IAAI,GAAG,OAAO,CAAC,GAA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@@ -1,4 +1,9 @@
1
- export type Hex = Exclude<Lowercase<string>, 'reserved-hex-value'>;
1
+ import z from 'zod';
2
+ export type Hex = Lowercase<string> & {
3
+ readonly __hex: unique symbol;
4
+ };
5
+ export declare const HexRegEx: RegExp;
6
+ export declare const HexZod: z.ZodString;
2
7
  /** Configuration of validation and output format */
3
8
  export interface HexConfig {
4
9
  bitLength?: number;
@@ -1 +1 @@
1
- {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../../src/hex/model.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,GAAG,GAAG,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,oBAAoB,CAAC,CAAA;AAElE,oDAAoD;AACpD,MAAM,WAAW,SAAS;IACxB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB"}
1
+ {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../../src/hex/model.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAA;AAMnB,MAAM,MAAM,GAAG,GAAG,SAAS,CAAC,MAAM,CAAC,GAAG;IAAE,QAAQ,CAAC,KAAK,EAAE,OAAO,MAAM,CAAA;CAAE,CAAA;AAEvE,eAAO,MAAM,QAAQ,QAA6C,CAAA;AAElE,eAAO,MAAM,MAAM,aAKhB,CAAA;AAEH,oDAAoD;AACpD,MAAM,WAAW,SAAS;IACxB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB"}
@@ -4,5 +4,5 @@ export declare const toHex: (
4
4
  /** Supported types are string, number, bigint, and ArrayBuffer */
5
5
  value: string | number | bigint | ArrayBufferLike,
6
6
  /** Configuration of output format and validation */
7
- config?: HexConfig) => Lowercase<string>;
7
+ config?: HexConfig) => import("./model.ts").Hex;
8
8
  //# sourceMappingURL=to.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"to.d.ts","sourceRoot":"","sources":["../../../src/hex/to.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAE3C,uEAAuE;AACvE,eAAO,MAAM,KAAK;AAChB,kEAAkE;AAClE,OAAO,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,eAAe;AACjD,oDAAoD;AACpD,SAAQ,SAAc,sBAIvB,CAAA"}
1
+ {"version":3,"file":"to.d.ts","sourceRoot":"","sources":["../../../src/hex/to.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAE3C,uEAAuE;AACvE,eAAO,MAAM,KAAK;AAChB,kEAAkE;AAClE,OAAO,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,eAAe;AACjD,oDAAoD;AACpD,SAAQ,SAAc,6BAIvB,CAAA"}
@@ -1,5 +1,6 @@
1
1
  // src/address.ts
2
2
  import { isObject } from "@xylabs/typeof";
3
+ import z2 from "zod";
3
4
 
4
5
  // src/assert.ts
5
6
  import { isString, isUndefined } from "@xylabs/typeof";
@@ -120,6 +121,20 @@ var toHexLegacy = (buffer) => {
120
121
  return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
121
122
  };
122
123
 
124
+ // src/hex/model.ts
125
+ import z from "zod";
126
+
127
+ // src/HexRegExMinMax.ts
128
+ var HexRegExMinMax = (minBytes = 0, maxBytes = Number.MAX_SAFE_INTEGER / 2) => {
129
+ return new RegExp(`^[a-f0-9]{${minBytes * 2},${maxBytes * 2}}$`);
130
+ };
131
+
132
+ // src/hex/model.ts
133
+ var HexRegEx = HexRegExMinMax(1, Number.MAX_SAFE_INTEGER);
134
+ var HexZod = z.string().toLowerCase().regex(HexRegEx, { message: "Invalid hex format" }).refine(
135
+ isHex
136
+ );
137
+
123
138
  // src/hex/to.ts
124
139
  var toHex = (value, config = {}) => {
125
140
  const { prefix = false } = config;
@@ -127,6 +142,9 @@ var toHex = (value, config = {}) => {
127
142
  };
128
143
 
129
144
  // src/address.ts
145
+ var AddressRegEx = HexRegExMinMax(20, 20);
146
+ var AddressToStringSchema = z2.string().toLowerCase().regex(AddressRegEx);
147
+ var AddressFromStringSchema = z2.string().toLowerCase().regex(AddressRegEx).transform((v) => toAddress(v));
130
148
  var ZERO_ADDRESS = "0000000000000000000000000000000000000000";
131
149
  var toAddress = (value, config = {}) => {
132
150
  const { bitLength = 160, prefix = false } = config;
@@ -140,6 +158,9 @@ var isAddress = (value, config = {}) => {
140
158
  const { bitLength = 160, prefix = false } = config;
141
159
  return isHex(value, { bitLength, prefix });
142
160
  };
161
+ var AddressZod = z2.string().toLowerCase().regex(AddressRegEx, { message: "Invalid address format" }).refine(
162
+ isAddress
163
+ );
143
164
  function asAddress(value, assert) {
144
165
  try {
145
166
  let stringValue = void 0;
@@ -161,6 +182,8 @@ function asAddress(value, assert) {
161
182
 
162
183
  // src/hash.ts
163
184
  import { isUndefined as isUndefined2 } from "@xylabs/typeof";
185
+ import z3 from "zod";
186
+ var HashRegEx = HexRegExMinMax(32, 32);
164
187
  var ZERO_HASH = "0000000000000000000000000000000000000000000000000000000000000000";
165
188
  var HashBitLength = [32, 64, 128, 256, 512, 1024, 2048, 4096];
166
189
  var isHashBitLength = (value) => {
@@ -169,6 +192,9 @@ var isHashBitLength = (value) => {
169
192
  var isHash = (value, bitLength = 256) => {
170
193
  return isHex(value, { bitLength });
171
194
  };
195
+ var HashZod = z3.string().toLowerCase().regex(HashRegEx, { message: "Invalid hash format" }).refine(
196
+ isHash
197
+ );
172
198
  function asHash(value, assert) {
173
199
  let stringValue = void 0;
174
200
  switch (typeof value) {
@@ -188,7 +214,15 @@ function hexToBigInt(hex) {
188
214
  return BigInt(hexFromHexString(hex, { prefix: true }));
189
215
  }
190
216
  export {
217
+ AddressFromStringSchema,
218
+ AddressRegEx,
219
+ AddressToStringSchema,
220
+ AddressZod,
191
221
  HashBitLength,
222
+ HashRegEx,
223
+ HashZod,
224
+ HexRegEx,
225
+ HexZod,
192
226
  ZERO_ADDRESS,
193
227
  ZERO_HASH,
194
228
  asAddress,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/address.ts","../../src/assert.ts","../../src/hex/from/fromHexString.ts","../../src/hex/nibble.ts","../../src/hex/regex.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/to.ts","../../src/hash.ts","../../src/hexToBigInt.ts"],"sourcesContent":["import { isObject } from '@xylabs/typeof'\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'\n\nexport type Address = Exclude<Hex, 'reserved-address-value'>\n\nexport const ZERO_ADDRESS: Address = '0000000000000000000000000000000000000000' as const\n\nexport const toAddress = (value: string | number | bigint | ArrayBufferLike, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = false } = config\n return hexFrom(value, {\n bitLength, prefix, ...config,\n })\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","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","// 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","export const hexRegex = /^[\\da-f]+$/i\nexport const hexRegexWithPrefix = /0x[\\da-f]+$/i\n","import type { Hex, HexConfig } from './model.ts'\nimport { bitsToNibbles } from './nibble.ts'\nimport { hexRegex, hexRegexWithPrefix } from './regex.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 { 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 { isUndefined } from '@xylabs/typeof'\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'\n\nexport const ZERO_HASH: Hash = '0000000000000000000000000000000000000000000000000000000000000000' as const\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 type Hash = Exclude<Hex, 'reserved-hash-value'>\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\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;;;ACAzB,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;;;ACClB,IAAM,gBAAgB,CAAC,UAA0B;AACtD,QAAM,UAAU,SAAS;AACzB,MAAI,UAAU,WAAW,EAAG,OAAM,IAAI,MAAM,qCAAqC;AACjF,SAAO;AACT;AAGO,IAAM,gBAAgB,CAAC,UAA0B;AACtD,SAAO,SAAS;AAClB;;;ACVO,IAAM,WAAW;AACjB,IAAM,qBAAqB;;;ACG3B,IAAM,QAAQ,CAAC,OAAgB,WAA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,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;;;AClCO,SAAS,YAAY,KAAkB;AAC5C,SAAO,OAAO,iBAAiB,KAAK,EAAE,QAAQ,KAAK,CAAC,CAAC;AACvD;","names":["isString","isString","isUndefined","isUndefined"]}
1
+ {"version":3,"sources":["../../src/address.ts","../../src/assert.ts","../../src/hex/from/fromHexString.ts","../../src/hex/nibble.ts","../../src/hex/regex.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/HexRegExMinMax.ts","../../src/hex/to.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 './HexRegExMinMax.ts'\n\nexport const AddressRegEx = HexRegExMinMax(20, 20)\n\nexport const AddressToStringSchema = z.string().toLowerCase().regex(AddressRegEx)\nexport const AddressFromStringSchema = z.string().toLowerCase().regex(AddressRegEx).transform(v => toAddress(v))\n\nexport type Address = Hex & { readonly __address: unique symbol }\n\nexport const ZERO_ADDRESS = '0000000000000000000000000000000000000000' as Address\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 const AddressZod = z.string()\n .toLowerCase()\n .regex(AddressRegEx, { message: 'Invalid address format' })\n .refine(\n isAddress,\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","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","// 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","export const hexRegex = /^[\\da-f]+$/i\nexport const hexRegexWithPrefix = /0x[\\da-f]+$/i\n","import type { Hex, HexConfig } from './model.ts'\nimport { bitsToNibbles } from './nibble.ts'\nimport { hexRegex, hexRegexWithPrefix } from './regex.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 z from 'zod'\n\nimport { HexRegExMinMax } from '../HexRegExMinMax.ts'\nimport { isHex } from './is.ts'\n\n// we use Exclude to intentionally make the type not equal to string\nexport type Hex = Lowercase<string> & { readonly __hex: unique symbol }\n\nexport const HexRegEx = HexRegExMinMax(1, Number.MAX_SAFE_INTEGER)\n\nexport const HexZod = z.string()\n .toLowerCase()\n .regex(HexRegEx, { message: 'Invalid hex format' })\n .refine(\n isHex,\n )\n\n/** Configuration of validation and output format */\nexport interface HexConfig {\n bitLength?: number\n byteSize?: number\n prefix?: boolean\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","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 { 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 './HexRegExMinMax.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\nexport type Hash = Hex & { readonly __hash: unique symbol }\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;;;ACClB,IAAM,gBAAgB,CAAC,UAA0B;AACtD,QAAM,UAAU,SAAS;AACzB,MAAI,UAAU,WAAW,EAAG,OAAM,IAAI,MAAM,qCAAqC;AACjF,SAAO;AACT;AAGO,IAAM,gBAAgB,CAAC,UAA0B;AACtD,SAAO,SAAS;AAClB;;;ACVO,IAAM,WAAW;AACjB,IAAM,qBAAqB;;;ACG3B,IAAM,QAAQ,CAAC,OAAgB,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ANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/hex",
3
- "version": "4.13.23",
3
+ "version": "4.14.1",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "hex",
@@ -42,14 +42,16 @@
42
42
  "src"
43
43
  ],
44
44
  "dependencies": {
45
- "@xylabs/typeof": "^4.13.23"
45
+ "@xylabs/typeof": "~4.14.1",
46
+ "zod": "~4.0.9",
47
+ "zod-to-json-schema": "~3.24.6"
46
48
  },
47
49
  "devDependencies": {
48
- "@xylabs/ts-scripts-yarn3": "^7.0.0",
49
- "@xylabs/tsconfig": "^7.0.0",
50
- "@xylabs/vitest-extended": "^4.13.23",
51
- "typescript": "^5.8.3",
52
- "vitest": "^3.2.4"
50
+ "@xylabs/ts-scripts-yarn3": "~7.0.1",
51
+ "@xylabs/tsconfig": "~7.0.1",
52
+ "@xylabs/vitest-extended": "~4.14.1",
53
+ "typescript": "~5.8.3",
54
+ "vitest": "~3.2.4"
53
55
  },
54
56
  "engines": {
55
57
  "node": ">=18"
@@ -0,0 +1,3 @@
1
+ export const HexRegExMinMax = (minBytes = 0, maxBytes: number = (Number.MAX_SAFE_INTEGER / 2)) => {
2
+ return new RegExp(`^[a-f0-9]{${minBytes * 2},${maxBytes * 2}}$`)
3
+ }
package/src/address.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { isObject } from '@xylabs/typeof'
2
+ import z from 'zod'
2
3
 
3
4
  import type { AssertConfig } from './assert.ts'
4
5
  import { assertError } from './assert.ts'
@@ -6,16 +7,22 @@ import type { Hex, HexConfig } from './hex/index.ts'
6
7
  import {
7
8
  hexFrom, hexFromHexString, isHex,
8
9
  } from './hex/index.ts'
10
+ import { HexRegExMinMax } from './HexRegExMinMax.ts'
9
11
 
10
- export type Address = Exclude<Hex, 'reserved-address-value'>
12
+ export const AddressRegEx = HexRegExMinMax(20, 20)
11
13
 
12
- export const ZERO_ADDRESS: Address = '0000000000000000000000000000000000000000' as const
14
+ export const AddressToStringSchema = z.string().toLowerCase().regex(AddressRegEx)
15
+ export const AddressFromStringSchema = z.string().toLowerCase().regex(AddressRegEx).transform(v => toAddress(v))
13
16
 
14
- export const toAddress = (value: string | number | bigint | ArrayBufferLike, config: HexConfig = {}) => {
17
+ export type Address = Hex & { readonly __address: unique symbol }
18
+
19
+ export const ZERO_ADDRESS = '0000000000000000000000000000000000000000' as Address
20
+
21
+ export const toAddress = (value: string | number | bigint | ArrayBufferLike, config: HexConfig = {}): Address => {
15
22
  const { bitLength = 160, prefix = false } = config
16
23
  return hexFrom(value, {
17
24
  bitLength, prefix, ...config,
18
- })
25
+ }) as Address
19
26
  }
20
27
 
21
28
  export const isAddress = (value: unknown, config: HexConfig = {}): value is Address => {
@@ -23,6 +30,13 @@ export const isAddress = (value: unknown, config: HexConfig = {}): value is Addr
23
30
  return isHex(value, { bitLength, prefix })
24
31
  }
25
32
 
33
+ export const AddressZod = z.string()
34
+ .toLowerCase()
35
+ .regex(AddressRegEx, { message: 'Invalid address format' })
36
+ .refine(
37
+ isAddress,
38
+ )
39
+
26
40
  export function asAddress(value: unknown): Address | undefined
27
41
  export function asAddress(value: unknown, assert: AssertConfig): Address
28
42
  export function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {
package/src/hash.ts CHANGED
@@ -1,11 +1,15 @@
1
1
  import { isUndefined } from '@xylabs/typeof'
2
+ import z from 'zod'
2
3
 
3
4
  import type { AssertConfig } from './assert.ts'
4
5
  import { assertError } from './assert.ts'
5
6
  import type { Hex } from './hex/index.ts'
6
7
  import { hexFromHexString, isHex } from './hex/index.ts'
8
+ import { HexRegExMinMax } from './HexRegExMinMax.ts'
7
9
 
8
- export const ZERO_HASH: Hash = '0000000000000000000000000000000000000000000000000000000000000000' as const
10
+ export const HashRegEx = HexRegExMinMax(32, 32)
11
+
12
+ export const ZERO_HASH = '0000000000000000000000000000000000000000000000000000000000000000' as Hash
9
13
 
10
14
  export type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096
11
15
  export const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]
@@ -14,11 +18,18 @@ export const isHashBitLength = (value: unknown): value is HashBitLength => {
14
18
  return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)
15
19
  }
16
20
 
17
- export type Hash = Exclude<Hex, 'reserved-hash-value'>
21
+ export type Hash = Hex & { readonly __hash: unique symbol }
18
22
  export const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {
19
23
  return isHex(value, { bitLength })
20
24
  }
21
25
 
26
+ export const HashZod = z.string()
27
+ .toLowerCase()
28
+ .regex(HashRegEx, { message: 'Invalid hash format' })
29
+ .refine(
30
+ isHash,
31
+ )
32
+
22
33
  export function asHash(value: unknown): Hash | undefined
23
34
  export function asHash(value: unknown, assert: AssertConfig): Hash
24
35
  export function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {
package/src/hex/model.ts CHANGED
@@ -1,5 +1,19 @@
1
+ import z from 'zod'
2
+
3
+ import { HexRegExMinMax } from '../HexRegExMinMax.ts'
4
+ import { isHex } from './is.ts'
5
+
1
6
  // we use Exclude to intentionally make the type not equal to string
2
- export type Hex = Exclude<Lowercase<string>, 'reserved-hex-value'>
7
+ export type Hex = Lowercase<string> & { readonly __hex: unique symbol }
8
+
9
+ export const HexRegEx = HexRegExMinMax(1, Number.MAX_SAFE_INTEGER)
10
+
11
+ export const HexZod = z.string()
12
+ .toLowerCase()
13
+ .regex(HexRegEx, { message: 'Invalid hex format' })
14
+ .refine(
15
+ isHex,
16
+ )
3
17
 
4
18
  /** Configuration of validation and output format */
5
19
  export interface HexConfig {