@xylabs/hex 5.0.1 → 5.0.3

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 (39) hide show
  1. package/dist/neutral/Brand.d.ts +3 -3
  2. package/dist/neutral/Brand.d.ts.map +1 -1
  3. package/dist/neutral/address/AddressZodTransform.d.ts +1 -1
  4. package/dist/neutral/address/AddressZodTransform.d.ts.map +1 -1
  5. package/dist/neutral/address/AddressZodValidation.d.ts +1 -1
  6. package/dist/neutral/address/AddressZodValidation.d.ts.map +1 -1
  7. package/dist/neutral/address/address.d.ts +10 -2
  8. package/dist/neutral/address/address.d.ts.map +1 -1
  9. package/dist/neutral/address/index.d.ts +0 -1
  10. package/dist/neutral/address/index.d.ts.map +1 -1
  11. package/dist/neutral/ethAddress.d.ts +1 -1
  12. package/dist/neutral/ethAddress.d.ts.map +1 -1
  13. package/dist/neutral/hash.d.ts +1 -1
  14. package/dist/neutral/hash.d.ts.map +1 -1
  15. package/dist/neutral/hex/model.d.ts +1 -1
  16. package/dist/neutral/hex/model.d.ts.map +1 -1
  17. package/dist/neutral/index-internal.d.ts +8 -0
  18. package/dist/neutral/index-internal.d.ts.map +1 -0
  19. package/dist/neutral/index-internal.mjs +332 -0
  20. package/dist/neutral/index-internal.mjs.map +1 -0
  21. package/dist/neutral/index.d.ts +1 -7
  22. package/dist/neutral/index.d.ts.map +1 -1
  23. package/dist/neutral/index.mjs +33 -31
  24. package/dist/neutral/index.mjs.map +1 -1
  25. package/package.json +11 -7
  26. package/src/Brand.ts +4 -1
  27. package/src/address/AddressZodTransform.ts +1 -1
  28. package/src/address/AddressZodValidation.ts +1 -1
  29. package/src/address/address.ts +27 -2
  30. package/src/address/index.ts +0 -1
  31. package/src/ethAddress.ts +1 -1
  32. package/src/hash.ts +1 -1
  33. package/src/hex/model.ts +1 -1
  34. package/src/index-internal.ts +7 -0
  35. package/src/index.ts +1 -7
  36. package/src/spec/address.spec.ts +24 -22
  37. package/dist/neutral/address/AddressNamespace.d.ts +0 -11
  38. package/dist/neutral/address/AddressNamespace.d.ts.map +0 -1
  39. package/src/address/AddressNamespace.ts +0 -18
@@ -1,4 +1,4 @@
1
- export type Brand<T, B> = T & {
2
- [K in keyof B]: B[K];
3
- };
1
+ import type { Brand as TypeOfBrand } from '@xylabs/typeof';
2
+ /** @deprecated use from @xylabs/typeof instead */
3
+ export type Brand<T, B> = TypeOfBrand<T, B>;
4
4
  //# sourceMappingURL=Brand.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Brand.d.ts","sourceRoot":"","sources":["../../src/Brand.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,CAAA"}
1
+ {"version":3,"file":"Brand.d.ts","sourceRoot":"","sources":["../../src/Brand.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE1D,kDAAkD;AAClD,MAAM,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC,IAAI,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA"}
@@ -1,5 +1,5 @@
1
1
  import z from 'zod';
2
- import type { Address } from './AddressNamespace.ts';
2
+ import type { Address } from './address.ts';
3
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
4
  export type AddressZodTransformType = z.infer<typeof AddressZodTransform>;
5
5
  //# sourceMappingURL=AddressZodTransform.d.ts.map
@@ -1 +1 @@
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,uBAAuB,CAAA;AAGpD,eAAO,MAAM,mBAAmB,iJAWD,CAAA;AAE/B,MAAM,MAAM,uBAAuB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAA"}
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 +1,5 @@
1
1
  import z from 'zod';
2
- import type { Address } from './AddressNamespace.ts';
2
+ import type { Address } from './address.ts';
3
3
  export declare const AddressZodValidation: z.ZodPipe<z.ZodString, z.ZodTransform<Address, string>>;
4
4
  export type AddressZodValidationType = z.infer<typeof AddressZodValidation>;
5
5
  //# sourceMappingURL=AddressZodValidation.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"AddressZodValidation.d.ts","sourceRoot":"","sources":["../../../src/address/AddressZodValidation.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAA;AAInB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AAEpD,eAAO,MAAM,oBAAoB,yDAGF,CAAA;AAE/B,MAAM,MAAM,wBAAwB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAA"}
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,7 +1,9 @@
1
1
  import z from 'zod';
2
2
  import type { AssertConfig } from '../assert.ts';
3
- import type { HexConfig } from '../hex/index.ts';
4
- import type { Address } from './AddressNamespace.ts';
3
+ import type { Hex, HexConfig } from '../hex/index.ts';
4
+ export type Address = Hex & {
5
+ readonly __address: true;
6
+ };
5
7
  export declare const AddressRegEx: RegExp;
6
8
  export declare const AddressToStringZod: z.ZodString;
7
9
  /** @deprecated use AddressToStringZod */
@@ -15,4 +17,10 @@ export declare const toAddress: (value: string | number | bigint | ArrayBufferLi
15
17
  export declare const isAddress: (value: unknown, config?: HexConfig) => value is Address;
16
18
  export declare function asAddress(value: unknown): Address | undefined;
17
19
  export declare function asAddress(value: unknown, assert: AssertConfig): Address;
20
+ /** @alpha */
21
+ export declare function isAddressV2(value: unknown): value is Address;
22
+ /** @alpha */
23
+ export declare function asAddressV2(value: unknown, assert?: boolean): Address | undefined;
24
+ /** @alpha */
25
+ export declare function toAddressV2(value: unknown, assert?: boolean): Address | undefined;
18
26
  //# sourceMappingURL=address.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../../src/address/address.ts"],"names":[],"mappings":"AACA,OAAO,CAAC,MAAM,KAAK,CAAA;AAEnB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAEhD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAKhD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AAEpD,eAAO,MAAM,YAAY,QAAyB,CAAA;AAElD,eAAO,MAAM,kBAAkB,aAAiC,CAAA;AAEhE,yCAAyC;AACzC,eAAO,MAAM,qBAAqB,aAAqB,CAAA;AAEvD,eAAO,MAAM,oBAAoB,yDAA4E,CAAA;AAE7G,2CAA2C;A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SAAQ,SAAc,KAAG,OAKrG,CAAA;AAED,eAAO,MAAM,SAAS,GAAI,OAAO,OAAO,EAAE,SAAQ,SAAc,KAAG,KAAK,IAAI,OAG3E,CAAA;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,GAAG,SAAS,CAAA;AAC9D,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAA;AAqBxE,aAAa;AACb,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,OAAO,CAE5D;AAED,aAAa;AACb,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,GAAE,OAAe,GAAG,OAAO,GAAG,SAAS,CAIxF;AAED,aAAa;AACb,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,GAAE,OAAe,GAAG,OAAO,GAAG,SAAS,CAIxF"}
@@ -1,5 +1,4 @@
1
1
  export * from './address.ts';
2
- export * from './AddressNamespace.ts';
3
2
  export * from './AddressZodTransform.ts';
4
3
  export * from './AddressZodValidation.ts';
5
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/address/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,uBAAuB,CAAA;AACrC,cAAc,0BAA0B,CAAA;AACxC,cAAc,2BAA2B,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/address/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,0BAA0B,CAAA;AACxC,cAAc,2BAA2B,CAAA"}
@@ -1,6 +1,6 @@
1
+ import type { Brand } from '@xylabs/typeof';
1
2
  import z from 'zod';
2
3
  import type { AssertConfig } from './assert.ts';
3
- import type { Brand } from './Brand.ts';
4
4
  import type { HexConfig } from './hex/index.ts';
5
5
  export declare const EthAddressRegEx: RegExp;
6
6
  export declare const EthAddressToStringSchema: z.ZodString;
@@ -1 +1 @@
1
- {"version":3,"file":"ethAddress.d.ts","sourceRoot":"","sources":["../../src/ethAddress.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAA;AAEnB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAM/C,eAAO,MAAM,eAAe,QAA4C,CAAA;AAExE,eAAO,MAAM,wBAAwB,aAAoC,CAAA;AACzE,eAAO,MAAM,0BAA0B,4DAAoE,CAAA;AAG3G,MAAM,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,EAAE;IAAE,QAAQ,CAAC,aAAa,EAAE,IAAI,CAAA;CAAE,CAAC,CAAA;AAExE,eAAO,MAAM,gBAAgB,EAAmD,UAAU,CAAA;AAE1F,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,eAAe,EAAE,SAAQ,SAAc,KAAG,UAKxG,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,OAAO,OAAO,EAAE,SAAQ,SAAc,KAAG,KAAK,IAAI,UAG9E,CAAA;AAED,eAAO,MAAM,aAAa,aAIvB,CAAA;AAEH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,CAAA;AACpE,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,UAAU,CAAA"}
1
+ {"version":3,"file":"ethAddress.d.ts","sourceRoot":"","sources":["../../src/ethAddress.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,CAAC,MAAM,KAAK,CAAA;AAEnB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAM/C,eAAO,MAAM,eAAe,QAA4C,CAAA;AAExE,eAAO,MAAM,wBAAwB,aAAoC,CAAA;AACzE,eAAO,MAAM,0BAA0B,4DAAoE,CAAA;AAG3G,MAAM,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,EAAE;IAAE,QAAQ,CAAC,aAAa,EAAE,IAAI,CAAA;CAAE,CAAC,CAAA;AAExE,eAAO,MAAM,gBAAgB,EAAmD,UAAU,CAAA;AAE1F,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,eAAe,EAAE,SAAQ,SAAc,KAAG,UAKxG,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,OAAO,OAAO,EAAE,SAAQ,SAAc,KAAG,KAAK,IAAI,UAG9E,CAAA;AAED,eAAO,MAAM,aAAa,aAIvB,CAAA;AAEH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,CAAA;AACpE,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,UAAU,CAAA"}
@@ -1,6 +1,6 @@
1
+ import type { Brand } from '@xylabs/typeof';
1
2
  import z from 'zod';
2
3
  import type { AssertConfig } from './assert.ts';
3
- import type { Brand } from './Brand.ts';
4
4
  import type { Hex } from './hex/index.ts';
5
5
  export declare const HashRegEx: RegExp;
6
6
  export declare const ZERO_HASH: Hash;
@@ -1 +1 @@
1
- {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/hash.ts"],"names":[],"mappings":"AACA,OAAO,CAAC,MAAM,KAAK,CAAA;AAEnB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,KAAK,EA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AI,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;AAGD,MAAM,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,EAAE;IAAE,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAA;CAAE,CAAC,CAAA;AAExD,eAAO,MAAM,MAAM,GAAI,OAAO,OAAO,EAAE,YAAW,aAAmB,KAAG,KAAK,IAAI,IAEhF,CAAA;AAED,eAAO,MAAM,OAAO,aAKjB,CAAA;AAEH,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,CAAA;AACxD,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA"}
@@ -1,5 +1,5 @@
1
+ import type { Brand } from '@xylabs/typeof';
1
2
  import z from 'zod';
2
- import type { Brand } from '../Brand.ts';
3
3
  export type Hex = Brand<Lowercase<string>, {
4
4
  readonly __hex: true;
5
5
  }>;
@@ -1 +1 @@
1
- {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../../src/hex/model.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAA;AAEnB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAIxC,MAAM,MAAM,GAAG,GAAG,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE;IAAE,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAA;CAAE,CAAC,CAAA;AAEpE,eAAO,MAAM,MAAM,qDAC2D,CAAA;AAE9E,oDAAoD;AACpD,MAAM,WAAW,SAAS;IACxB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB;AAED,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,MAAM,CAAC,CAAA"}
1
+ {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../../src/hex/model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,CAAC,MAAM,KAAK,CAAA;AAKnB,MAAM,MAAM,GAAG,GAAG,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE;IAAE,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAA;CAAE,CAAC,CAAA;AAEpE,eAAO,MAAM,MAAM,qDAC2D,CAAA;AAE9E,oDAAoD;AACpD,MAAM,WAAW,SAAS;IACxB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB;AAED,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,MAAM,CAAC,CAAA"}
@@ -0,0 +1,8 @@
1
+ export * from './address/index.ts';
2
+ export * from './assert.ts';
3
+ export * from './ethAddress.ts';
4
+ export * from './hash.ts';
5
+ export * from './hex/index.ts';
6
+ export * from './HexRegEx.ts';
7
+ export * from './hexToBigInt.ts';
8
+ //# sourceMappingURL=index-internal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-internal.d.ts","sourceRoot":"","sources":["../../src/index-internal.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,aAAa,CAAA;AAC3B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,WAAW,CAAA;AACzB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,kBAAkB,CAAA"}
@@ -0,0 +1,332 @@
1
+ // src/address/address.ts
2
+ import { isObject } from "@xylabs/typeof";
3
+ import z4 from "zod";
4
+
5
+ // src/assert.ts
6
+ import { isString, isUndefined } from "@xylabs/typeof";
7
+ var assertError = (value, assert, defaultMessage) => {
8
+ if (!isUndefined(assert)) {
9
+ const assertString = typeof assert === "string" ? assert : typeof assert === "boolean" ? defaultMessage : assert(value, defaultMessage);
10
+ if (isString(assertString) || assertString === true) {
11
+ throw new Error(assertString === true ? defaultMessage : assertString);
12
+ }
13
+ }
14
+ return void 0;
15
+ };
16
+
17
+ // src/hex/from/fromHexString.ts
18
+ import { isNumber } from "@xylabs/typeof";
19
+
20
+ // src/HexRegEx.ts
21
+ var HexRegExMinMax = (minBytes = 0, maxBytes = Number.MAX_SAFE_INTEGER / 2) => {
22
+ return new RegExp(`^[a-f0-9]{${minBytes * 2},${maxBytes * 2}}$`);
23
+ };
24
+ var HexRegExMinMaxMixedCaseWithPrefix = (minBytes = 0, maxBytes = Number.MAX_SAFE_INTEGER / 2) => {
25
+ return new RegExp(`^0x[a-fA-F0-9]{${minBytes * 2},${maxBytes * 2}}$`);
26
+ };
27
+ var HexRegEx = /^[0-9a-f]+$/;
28
+ var HexRegExWithPrefix = /^0x[0-9a-f]+$/;
29
+
30
+ // src/hex/nibble.ts
31
+ var bitsToNibbles = (value) => {
32
+ const nibbles = value >> 2;
33
+ if (value !== nibbles << 2) throw new Error("Bits for nibbles must multiple of 4");
34
+ return nibbles;
35
+ };
36
+ var nibblesToBits = (value) => {
37
+ return value << 2;
38
+ };
39
+
40
+ // src/hex/is.ts
41
+ var isHex = (value, config) => {
42
+ if (typeof value !== "string") return false;
43
+ const valueCharLength = config?.prefix ? value.length - 2 : value.length;
44
+ if (config?.bitLength !== void 0 && valueCharLength !== bitsToNibbles(config?.bitLength)) return false;
45
+ return config?.prefix ? HexRegExWithPrefix.test(value) : HexRegEx.test(value);
46
+ };
47
+
48
+ // src/hex/from/fromHexString.ts
49
+ var hexFromHexString = (value, config = {}) => {
50
+ const {
51
+ prefix = false,
52
+ byteSize = 8,
53
+ bitLength
54
+ } = config;
55
+ const nibbleBoundary = bitsToNibbles(byteSize);
56
+ const unEvened = (value.startsWith("0x") ? value.slice(2) : value).toLowerCase();
57
+ if (isHex(unEvened)) {
58
+ const evenCharacters = unEvened.padStart(unEvened.length + unEvened.length % nibbleBoundary, "0");
59
+ const padded = isNumber(bitLength) ? evenCharacters.padStart(bitLength / 4, "0") : evenCharacters;
60
+ return (prefix ? `0x${padded}` : padded).toLowerCase();
61
+ } else {
62
+ throw new Error("Received string is not a value hex");
63
+ }
64
+ };
65
+
66
+ // src/hex/from/fromArrayBuffer.ts
67
+ var hexFromArrayBuffer = (buffer, config) => {
68
+ const unPadded = [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
69
+ return hexFromHexString(unPadded, config);
70
+ };
71
+
72
+ // src/hex/from/fromBigInt.ts
73
+ var hexFromBigInt = (value, config = {}) => {
74
+ const unPadded = value.toString(16);
75
+ return hexFromHexString(unPadded, config);
76
+ };
77
+
78
+ // src/hex/from/fromNumber.ts
79
+ var hexFromNumber = (value, config) => {
80
+ return hexFromBigInt(BigInt(value), config);
81
+ };
82
+
83
+ // src/hex/from/from.ts
84
+ var hexFrom = (value, config) => {
85
+ switch (typeof value) {
86
+ case "string": {
87
+ return hexFromHexString(value, config);
88
+ }
89
+ case "bigint": {
90
+ return hexFromBigInt(value, config);
91
+ }
92
+ case "number": {
93
+ return hexFromNumber(value, config);
94
+ }
95
+ case "object": {
96
+ return hexFromArrayBuffer(value, config);
97
+ }
98
+ default: {
99
+ throw new Error(`Invalid type: ${typeof value}`);
100
+ }
101
+ }
102
+ };
103
+
104
+ // src/hex/as.ts
105
+ function asHex(value, assert) {
106
+ let stringValue = void 0;
107
+ switch (typeof value) {
108
+ case "string": {
109
+ stringValue = hexFromHexString(value);
110
+ break;
111
+ }
112
+ default: {
113
+ return assertError(value, assert, `Unsupported type [${typeof value}]`);
114
+ }
115
+ }
116
+ return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`);
117
+ }
118
+
119
+ // src/hex/isHexZero.ts
120
+ import { isString as isString2 } from "@xylabs/typeof";
121
+ var isHexZero = (value) => {
122
+ return isString2(value) ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : void 0;
123
+ };
124
+
125
+ // src/hex/legacy.ts
126
+ var toHexLegacy = (buffer) => {
127
+ return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
128
+ };
129
+
130
+ // src/hex/model.ts
131
+ import z from "zod";
132
+ var HexZod = z.string().regex(HexRegEx, { message: "Invalid hex format" }).transform((x) => x);
133
+
134
+ // src/hex/to.ts
135
+ var toHex = (value, config = {}) => {
136
+ const { prefix = false } = config;
137
+ return hexFrom(value, { prefix, ...config });
138
+ };
139
+
140
+ // src/address/AddressZodTransform.ts
141
+ import z3 from "zod";
142
+
143
+ // src/address/AddressZodValidation.ts
144
+ import z2 from "zod";
145
+ var AddressZodValidation = z2.string().refine((x) => HexZod.safeParse(x)).refine((x) => x.length === ADDRESS_LENGTH, { error: (e) => new Error(`Address must have 40 characters [${e.input}]`) }).transform((v) => v);
146
+
147
+ // src/address/AddressZodTransform.ts
148
+ var AddressZodTransform = z3.union([z3.string(), z3.bigint()]).transform((value) => {
149
+ if (typeof value === "bigint") {
150
+ return value.toString(16).padStart(40, "0");
151
+ }
152
+ if (value.startsWith("0x")) {
153
+ return value.slice(2);
154
+ }
155
+ return value;
156
+ }).refine((x) => AddressZodValidation.safeParse(x).data).transform((x) => x);
157
+
158
+ // src/address/address.ts
159
+ var AddressRegEx = HexRegExMinMax(20, 20);
160
+ var AddressToStringZod = z4.string().regex(AddressRegEx);
161
+ var AddressToStringSchema = AddressToStringZod;
162
+ var AddressFromStringZod = z4.string().toLowerCase().regex(AddressRegEx).transform((v) => toAddress(v));
163
+ var AddressFromStringSchema = AddressFromStringZod;
164
+ var ZERO_ADDRESS = "0000000000000000000000000000000000000000";
165
+ var ADDRESS_LENGTH = 40;
166
+ var toAddress = (value, config = {}) => {
167
+ const { bitLength = 160, prefix = false } = config;
168
+ return hexFrom(value, {
169
+ bitLength,
170
+ prefix,
171
+ ...config
172
+ });
173
+ };
174
+ var isAddress = (value, config = {}) => {
175
+ const { bitLength = 160, prefix = false } = config;
176
+ return isHex(value, { bitLength, prefix });
177
+ };
178
+ function asAddress(value, assert) {
179
+ try {
180
+ let stringValue = void 0;
181
+ switch (typeof value) {
182
+ case "string": {
183
+ stringValue = hexFromHexString(value, { prefix: false });
184
+ break;
185
+ }
186
+ default: {
187
+ return isObject(assert) ? assertError(value, assert, `Unsupported type [${typeof value}]`) : void 0;
188
+ }
189
+ }
190
+ return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`);
191
+ } catch (ex) {
192
+ const error = ex;
193
+ return assertError(void 0, assert, error.message);
194
+ }
195
+ }
196
+ function isAddressV2(value) {
197
+ return AddressZodValidation.safeParse(value).success;
198
+ }
199
+ function asAddressV2(value, assert = false) {
200
+ return assert ? AddressZodValidation.parse(value) : AddressZodValidation.safeParse(value).data;
201
+ }
202
+ function toAddressV2(value, assert = false) {
203
+ return assert ? AddressZodTransform.parse(value) : AddressZodTransform.safeParse(value).data;
204
+ }
205
+
206
+ // src/ethAddress.ts
207
+ import z5 from "zod";
208
+ var EthAddressRegEx = HexRegExMinMaxMixedCaseWithPrefix(20, 20);
209
+ var EthAddressToStringSchema = z5.string().regex(EthAddressRegEx);
210
+ var EthAddressFromStringSchema = z5.string().regex(EthAddressRegEx).transform((v) => toEthAddress(v));
211
+ var ETH_ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
212
+ var toEthAddress = (value, config = {}) => {
213
+ const { bitLength = 160, prefix = false } = config;
214
+ return `0x${hexFrom(value, {
215
+ bitLength,
216
+ prefix,
217
+ ...config
218
+ })}`;
219
+ };
220
+ var isEthAddress = (value, config = {}) => {
221
+ const { bitLength = 160, prefix = true } = config;
222
+ return isHex(value, { bitLength, prefix });
223
+ };
224
+ var EthAddressZod = z5.string().regex(EthAddressRegEx, { message: "Invalid address format" }).refine(
225
+ isEthAddress
226
+ );
227
+ function asEthAddress(value, assert) {
228
+ try {
229
+ let stringValue = void 0;
230
+ switch (typeof value) {
231
+ case "string": {
232
+ stringValue = hexFromHexString(value, { prefix: true });
233
+ break;
234
+ }
235
+ default: {
236
+ if (value !== void 0) {
237
+ return assertError(value, assert, `Unsupported type [${typeof value}]`);
238
+ }
239
+ }
240
+ }
241
+ return isEthAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an EthAddress [${value}]`);
242
+ } catch (ex) {
243
+ const error = ex;
244
+ return assertError(void 0, assert, error.message);
245
+ }
246
+ }
247
+
248
+ // src/hash.ts
249
+ import { isUndefined as isUndefined2 } from "@xylabs/typeof";
250
+ import z6 from "zod";
251
+ var HashRegEx = HexRegExMinMax(32, 32);
252
+ var ZERO_HASH = "0000000000000000000000000000000000000000000000000000000000000000";
253
+ var HashBitLength = [32, 64, 128, 256, 512, 1024, 2048, 4096];
254
+ var isHashBitLength = (value) => {
255
+ return typeof value === "number" && HashBitLength.includes(value);
256
+ };
257
+ var isHash = (value, bitLength = 256) => {
258
+ return isHex(value, { bitLength });
259
+ };
260
+ var HashZod = z6.string().toLowerCase().regex(HashRegEx, { message: "Invalid hash format" }).refine(
261
+ isHash
262
+ );
263
+ function asHash(value, assert) {
264
+ let stringValue = void 0;
265
+ switch (typeof value) {
266
+ case "string": {
267
+ stringValue = hexFromHexString(value);
268
+ break;
269
+ }
270
+ default: {
271
+ return isUndefined2(assert) ? void 0 : assertError(value, assert, `Unsupported type [${typeof value}]`);
272
+ }
273
+ }
274
+ return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`);
275
+ }
276
+
277
+ // src/hexToBigInt.ts
278
+ function hexToBigInt(hex) {
279
+ return BigInt(hexFromHexString(hex, { prefix: true }));
280
+ }
281
+ export {
282
+ ADDRESS_LENGTH,
283
+ AddressFromStringSchema,
284
+ AddressFromStringZod,
285
+ AddressRegEx,
286
+ AddressToStringSchema,
287
+ AddressToStringZod,
288
+ AddressZodTransform,
289
+ AddressZodValidation,
290
+ ETH_ZERO_ADDRESS,
291
+ EthAddressFromStringSchema,
292
+ EthAddressRegEx,
293
+ EthAddressToStringSchema,
294
+ EthAddressZod,
295
+ HashBitLength,
296
+ HashRegEx,
297
+ HashZod,
298
+ HexRegEx,
299
+ HexRegExMinMax,
300
+ HexRegExMinMaxMixedCaseWithPrefix,
301
+ HexRegExWithPrefix,
302
+ HexZod,
303
+ ZERO_ADDRESS,
304
+ ZERO_HASH,
305
+ asAddress,
306
+ asAddressV2,
307
+ asEthAddress,
308
+ asHash,
309
+ asHex,
310
+ assertError,
311
+ bitsToNibbles,
312
+ hexFrom,
313
+ hexFromArrayBuffer,
314
+ hexFromBigInt,
315
+ hexFromHexString,
316
+ hexFromNumber,
317
+ hexToBigInt,
318
+ isAddress,
319
+ isAddressV2,
320
+ isEthAddress,
321
+ isHash,
322
+ isHashBitLength,
323
+ isHex,
324
+ isHexZero,
325
+ nibblesToBits,
326
+ toAddress,
327
+ toAddressV2,
328
+ toEthAddress,
329
+ toHex,
330
+ toHexLegacy
331
+ };
332
+ //# sourceMappingURL=index-internal.mjs.map
@@ -0,0 +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,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A,MACF;AAAA,IACF;AACA,WAAO,aAAa,WAAW,IAAI,cAAc,YAAY,OAAO,QAAQ,+BAA+B,KAAK,GAAG;AAAA,EACrH,SAAS,IAAI;AACX,UAAM,QAAQ;AACd,WAAO,YAAY,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,9 +1,3 @@
1
- export * from './address/index.ts';
2
- export * from './assert.ts';
3
1
  export * from './Brand.ts';
4
- export * from './ethAddress.ts';
5
- export * from './hash.ts';
6
- export * from './hex/index.ts';
7
- export * from './HexRegEx.ts';
8
- export * from './hexToBigInt.ts';
2
+ export * from './index-internal.ts';
9
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,WAAW,CAAA;AACzB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,kBAAkB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,qBAAqB,CAAA"}
@@ -1,6 +1,6 @@
1
1
  // src/address/address.ts
2
2
  import { isObject } from "@xylabs/typeof";
3
- import z2 from "zod";
3
+ import z4 from "zod";
4
4
 
5
5
  // src/assert.ts
6
6
  import { isString, isUndefined } from "@xylabs/typeof";
@@ -137,11 +137,29 @@ var toHex = (value, config = {}) => {
137
137
  return hexFrom(value, { prefix, ...config });
138
138
  };
139
139
 
140
+ // src/address/AddressZodTransform.ts
141
+ import z3 from "zod";
142
+
143
+ // src/address/AddressZodValidation.ts
144
+ import z2 from "zod";
145
+ var AddressZodValidation = z2.string().refine((x) => HexZod.safeParse(x)).refine((x) => x.length === ADDRESS_LENGTH, { error: (e) => new Error(`Address must have 40 characters [${e.input}]`) }).transform((v) => v);
146
+
147
+ // src/address/AddressZodTransform.ts
148
+ var AddressZodTransform = z3.union([z3.string(), z3.bigint()]).transform((value) => {
149
+ if (typeof value === "bigint") {
150
+ return value.toString(16).padStart(40, "0");
151
+ }
152
+ if (value.startsWith("0x")) {
153
+ return value.slice(2);
154
+ }
155
+ return value;
156
+ }).refine((x) => AddressZodValidation.safeParse(x).data).transform((x) => x);
157
+
140
158
  // src/address/address.ts
141
159
  var AddressRegEx = HexRegExMinMax(20, 20);
142
- var AddressToStringZod = z2.string().regex(AddressRegEx);
160
+ var AddressToStringZod = z4.string().regex(AddressRegEx);
143
161
  var AddressToStringSchema = AddressToStringZod;
144
- var AddressFromStringZod = z2.string().toLowerCase().regex(AddressRegEx).transform((v) => toAddress(v));
162
+ var AddressFromStringZod = z4.string().toLowerCase().regex(AddressRegEx).transform((v) => toAddress(v));
145
163
  var AddressFromStringSchema = AddressFromStringZod;
146
164
  var ZERO_ADDRESS = "0000000000000000000000000000000000000000";
147
165
  var ADDRESS_LENGTH = 40;
@@ -175,33 +193,15 @@ function asAddress(value, assert) {
175
193
  return assertError(void 0, assert, error.message);
176
194
  }
177
195
  }
178
-
179
- // src/address/AddressZodTransform.ts
180
- import z4 from "zod";
181
-
182
- // src/address/AddressZodValidation.ts
183
- import z3 from "zod";
184
- var AddressZodValidation = z3.string().refine((x) => HexZod.safeParse(x)).refine((x) => x.length === ADDRESS_LENGTH, { error: (e) => new Error(`Address must have 40 characters [${e.input}]`) }).transform((v) => v);
185
-
186
- // src/address/AddressZodTransform.ts
187
- var AddressZodTransform = z4.union([z4.string(), z4.bigint()]).transform((value) => {
188
- if (typeof value === "bigint") {
189
- return value.toString(16).padStart(40, "0");
190
- }
191
- if (value.startsWith("0x")) {
192
- return value.slice(2);
193
- }
194
- return value;
195
- }).refine((x) => AddressZodValidation.safeParse(x).data).transform((x) => x);
196
-
197
- // src/address/AddressNamespace.ts
198
- var Address;
199
- ((Address2) => {
200
- Address2.identify = (value) => AddressZodValidation.safeParse(value).success;
201
- Address2.cast = (value, assert = false) => assert ? AddressZodValidation.parse(value) : AddressZodValidation.safeParse(value).data;
202
- Address2.from = (value, assert = false) => assert ? AddressZodTransform.parse(value) : AddressZodTransform.safeParse(value).data;
203
- Address2.toString = (value) => value;
204
- })(Address || (Address = {}));
196
+ function isAddressV2(value) {
197
+ return AddressZodValidation.safeParse(value).success;
198
+ }
199
+ function asAddressV2(value, assert = false) {
200
+ return assert ? AddressZodValidation.parse(value) : AddressZodValidation.safeParse(value).data;
201
+ }
202
+ function toAddressV2(value, assert = false) {
203
+ return assert ? AddressZodTransform.parse(value) : AddressZodTransform.safeParse(value).data;
204
+ }
205
205
 
206
206
  // src/ethAddress.ts
207
207
  import z5 from "zod";
@@ -280,7 +280,6 @@ function hexToBigInt(hex) {
280
280
  }
281
281
  export {
282
282
  ADDRESS_LENGTH,
283
- Address,
284
283
  AddressFromStringSchema,
285
284
  AddressFromStringZod,
286
285
  AddressRegEx,
@@ -304,6 +303,7 @@ export {
304
303
  ZERO_ADDRESS,
305
304
  ZERO_HASH,
306
305
  asAddress,
306
+ asAddressV2,
307
307
  asEthAddress,
308
308
  asHash,
309
309
  asHex,
@@ -316,6 +316,7 @@ export {
316
316
  hexFromNumber,
317
317
  hexToBigInt,
318
318
  isAddress,
319
+ isAddressV2,
319
320
  isEthAddress,
320
321
  isHash,
321
322
  isHashBitLength,
@@ -323,6 +324,7 @@ export {
323
324
  isHexZero,
324
325
  nibblesToBits,
325
326
  toAddress,
327
+ toAddressV2,
326
328
  toEthAddress,
327
329
  toHex,
328
330
  toHexLegacy
@@ -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/address/AddressNamespace.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 { HexConfig } from '../hex/index.ts'\nimport {\n hexFrom, hexFromHexString, isHex,\n} from '../hex/index.ts'\nimport { HexRegExMinMax } from '../HexRegEx.ts'\nimport type { Address } from './AddressNamespace.ts'\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","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 z from 'zod'\n\nimport type { Brand } from '../Brand.ts'\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 './AddressNamespace.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 { ADDRESS_LENGTH } from './address.ts'\nimport type { Address } from './AddressNamespace.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 { Hex } from '../hex/index.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 & { readonly __address: true }\n\n// eslint-disable-next-line @typescript-eslint/no-namespace\nexport namespace Address {\n export const identify = (value: unknown): value is Address => AddressZodValidation.safeParse(value).success\n export const cast = (value: unknown, assert: boolean = false): Address | undefined => assert\n ? AddressZodValidation.parse(value)\n : AddressZodValidation.safeParse(value).data\n export const from = (value: unknown, assert: boolean = false): Address | undefined => assert\n ? AddressZodTransform.parse(value)\n : AddressZodTransform.safeParse(value).data\n export const toString = (value: Address): string => value as string\n}\n","import z from 'zod'\n\nimport type { AssertConfig } from './assert.ts'\nimport { assertError } from './assert.ts'\nimport type { Brand } from './Brand.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 { isUndefined } from '@xylabs/typeof'\nimport z from 'zod'\n\nimport type { AssertConfig } from './assert.ts'\nimport { assertError } from './assert.ts'\nimport type { Brand } from './Brand.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,iBAAi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AAG;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","Address","z","z","isUndefined","z","z","isUndefined"]}
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,WAAW,GAAG,WAAoB,OAAO,mBAAmB,MAAO;AACnH,SAAO,IAAI,OAAO,kBAAkB,WAAW,CAAC,IAAI,WAAW,CAAC,IAAI;AACtE;AAEO,IAAM,WAAW;AACjB,IAAM,qBAAqB;;;ACR3B,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;;;ACNO,IAAM,QAAQ,CAAC,OAAgB,WAAqC;A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AO,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/hex",
3
- "version": "5.0.1",
3
+ "version": "5.0.3",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "hex",
@@ -32,6 +32,11 @@
32
32
  "source": "./src/index.ts",
33
33
  "default": "./dist/neutral/index.mjs"
34
34
  },
35
+ "./internal": {
36
+ "types": "./dist/neutral/index-internal.d.ts",
37
+ "source": "./src/index-internal.ts",
38
+ "default": "./dist/neutral/index-internal.mjs"
39
+ },
35
40
  "./package.json": "./package.json"
36
41
  },
37
42
  "module": "./dist/neutral/index.mjs",
@@ -42,14 +47,13 @@
42
47
  "src"
43
48
  ],
44
49
  "dependencies": {
45
- "@xylabs/typeof": "~5.0.1",
46
- "zod": "~4.0.14",
47
- "zod-to-json-schema": "~3.24.6"
50
+ "@xylabs/typeof": "~5.0.3",
51
+ "zod": "~4.0.15"
48
52
  },
49
53
  "devDependencies": {
50
- "@xylabs/ts-scripts-yarn3": "~7.0.3",
51
- "@xylabs/tsconfig": "~7.0.3",
52
- "@xylabs/vitest-extended": "~5.0.1",
54
+ "@xylabs/ts-scripts-yarn3": "~7.1.0",
55
+ "@xylabs/tsconfig": "~7.1.0",
56
+ "@xylabs/vitest-extended": "~5.0.3",
53
57
  "typescript": "~5.9.2",
54
58
  "vitest": "~3.2.4"
55
59
  },
package/src/Brand.ts CHANGED
@@ -1 +1,4 @@
1
- export type Brand<T, B> = T & { [K in keyof B]: B[K] }
1
+ import type { Brand as TypeOfBrand } from '@xylabs/typeof'
2
+
3
+ /** @deprecated use from @xylabs/typeof instead */
4
+ export type Brand<T, B> = TypeOfBrand<T, B>
@@ -1,6 +1,6 @@
1
1
  import z from 'zod'
2
2
 
3
- import type { Address } from './AddressNamespace.ts'
3
+ import type { Address } from './address.ts'
4
4
  import { AddressZodValidation } from './AddressZodValidation.ts'
5
5
 
6
6
  export const AddressZodTransform = z.union([z.string(), z.bigint()])
@@ -1,8 +1,8 @@
1
1
  import z from 'zod'
2
2
 
3
3
  import { HexZod } from '../hex/index.ts'
4
+ import type { Address } from './address.ts'
4
5
  import { ADDRESS_LENGTH } from './address.ts'
5
- import type { Address } from './AddressNamespace.ts'
6
6
 
7
7
  export const AddressZodValidation = z.string()
8
8
  .refine(x => HexZod.safeParse(x))
@@ -3,12 +3,18 @@ import z from 'zod'
3
3
 
4
4
  import type { AssertConfig } from '../assert.ts'
5
5
  import { assertError } from '../assert.ts'
6
- import type { HexConfig } from '../hex/index.ts'
6
+ import type { Hex, HexConfig } from '../hex/index.ts'
7
7
  import {
8
8
  hexFrom, hexFromHexString, isHex,
9
9
  } from '../hex/index.ts'
10
10
  import { HexRegExMinMax } from '../HexRegEx.ts'
11
- import type { Address } from './AddressNamespace.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
+ }
12
18
 
13
19
  export const AddressRegEx = HexRegExMinMax(20, 20)
14
20
 
@@ -58,3 +64,22 @@ export function asAddress(value: unknown, assert?: AssertConfig): Address | unde
58
64
  return assertError(undefined, assert, error.message)
59
65
  }
60
66
  }
67
+
68
+ /** @alpha */
69
+ export function isAddressV2(value: unknown): value is Address {
70
+ return AddressZodValidation.safeParse(value).success
71
+ }
72
+
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
+ }
79
+
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
+ }
@@ -1,4 +1,3 @@
1
1
  export * from './address.ts'
2
- export * from './AddressNamespace.ts'
3
2
  export * from './AddressZodTransform.ts'
4
3
  export * from './AddressZodValidation.ts'
package/src/ethAddress.ts CHANGED
@@ -1,8 +1,8 @@
1
+ import type { Brand } from '@xylabs/typeof'
1
2
  import z from 'zod'
2
3
 
3
4
  import type { AssertConfig } from './assert.ts'
4
5
  import { assertError } from './assert.ts'
5
- import type { Brand } from './Brand.ts'
6
6
  import type { HexConfig } from './hex/index.ts'
7
7
  import {
8
8
  hexFrom, hexFromHexString, isHex,
package/src/hash.ts CHANGED
@@ -1,9 +1,9 @@
1
+ import type { Brand } from '@xylabs/typeof'
1
2
  import { isUndefined } from '@xylabs/typeof'
2
3
  import z from 'zod'
3
4
 
4
5
  import type { AssertConfig } from './assert.ts'
5
6
  import { assertError } from './assert.ts'
6
- import type { Brand } from './Brand.ts'
7
7
  import type { Hex } from './hex/index.ts'
8
8
  import { hexFromHexString, isHex } from './hex/index.ts'
9
9
  import { HexRegExMinMax } from './HexRegEx.ts'
package/src/hex/model.ts CHANGED
@@ -1,6 +1,6 @@
1
+ import type { Brand } from '@xylabs/typeof'
1
2
  import z from 'zod'
2
3
 
3
- import type { Brand } from '../Brand.ts'
4
4
  import { HexRegEx } from '../HexRegEx.ts'
5
5
 
6
6
  // using true instead of unique symbol to avoid conflicts with other versions of library
@@ -0,0 +1,7 @@
1
+ export * from './address/index.ts'
2
+ export * from './assert.ts'
3
+ export * from './ethAddress.ts'
4
+ export * from './hash.ts'
5
+ export * from './hex/index.ts'
6
+ export * from './HexRegEx.ts'
7
+ export * from './hexToBigInt.ts'
package/src/index.ts CHANGED
@@ -1,8 +1,2 @@
1
- export * from './address/index.ts'
2
- export * from './assert.ts'
3
1
  export * from './Brand.ts'
4
- export * from './ethAddress.ts'
5
- export * from './hash.ts'
6
- export * from './hex/index.ts'
7
- export * from './HexRegEx.ts'
8
- export * from './hexToBigInt.ts'
2
+ export * from './index-internal.ts'
@@ -5,7 +5,9 @@ import {
5
5
  } from 'vitest'
6
6
 
7
7
  import {
8
- Address, asAddress, isAddress,
8
+ asAddress, asAddressV2, isAddress,
9
+ isAddressV2,
10
+ toAddressV2,
9
11
  } from '../address/index.ts'
10
12
 
11
13
  describe('address', () => {
@@ -28,34 +30,34 @@ describe('address', () => {
28
30
 
29
31
  describe('address [zod]', () => {
30
32
  test('identify', () => {
31
- expect(Address.identify(true)).toBeFalse()
32
- expect(Address.identify({})).toBeFalse()
33
- expect(Address.identify('NotHex')).toBeFalse()
34
- expect(Address.identify('deadbeef')).toBeFalse()
35
- expect(Address.identify('0xdeadbeef')).toBeFalse()
36
- expect(Address.identify('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')).toBeTrue()
37
- expect(Address.identify('0xdeadbeefdeadbeefdeadxxxxdeadbeefdeadbeef')).toBeFalse()
33
+ expect(isAddressV2(true)).toBeFalse()
34
+ expect(isAddressV2({})).toBeFalse()
35
+ expect(isAddressV2('NotHex')).toBeFalse()
36
+ expect(isAddressV2('deadbeef')).toBeFalse()
37
+ expect(isAddressV2('0xdeadbeef')).toBeFalse()
38
+ expect(isAddressV2('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')).toBeTrue()
39
+ expect(isAddressV2('0xdeadbeefdeadbeefdeadxxxxdeadbeefdeadbeef')).toBeFalse()
38
40
  })
39
41
  test('cast', () => {
40
- expect(Address.cast('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef')).toBe(undefined)
41
- expect(Address.cast('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
42
- expect(Address.cast('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', false)).toBe(undefined)
43
- expect(Address.cast('000000000000000000000000000000000000000a', true)).toBe('000000000000000000000000000000000000000a')
44
- expect(Address.cast('0x000000000000000000000000000000000000000a', false)).toBe(undefined)
42
+ expect(asAddressV2('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef')).toBe(undefined)
43
+ expect(asAddressV2('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
44
+ expect(asAddressV2('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', false)).toBe(undefined)
45
+ expect(asAddressV2('000000000000000000000000000000000000000a', true)).toBe('000000000000000000000000000000000000000a')
46
+ expect(asAddressV2('0x000000000000000000000000000000000000000a', false)).toBe(undefined)
45
47
  })
46
48
 
47
49
  test('from', () => {
48
- expect(Address.from('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef')).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
49
- expect(Address.from('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
50
- expect(Address.from('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', true)).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
51
- expect(Address.from('000000000000000000000000000000000000000a', true)).toBe('000000000000000000000000000000000000000a')
52
- expect(Address.from('0x000000000000000000000000000000000000000a', true)).toBe('000000000000000000000000000000000000000a')
53
- expect(Address.from(10n, true)).toBe('000000000000000000000000000000000000000a')
54
- expect(Address.from(
50
+ expect(toAddressV2('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef')).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
51
+ expect(toAddressV2('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
52
+ expect(toAddressV2('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', true)).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
53
+ expect(toAddressV2('000000000000000000000000000000000000000a', true)).toBe('000000000000000000000000000000000000000a')
54
+ expect(toAddressV2('0x000000000000000000000000000000000000000a', true)).toBe('000000000000000000000000000000000000000a')
55
+ expect(toAddressV2(10n, true)).toBe('000000000000000000000000000000000000000a')
56
+ expect(toAddressV2(
55
57
  1_234_567_890_123_456_789_012_345_678_901_234_567_890_123_456_789_012_345_678_901_234_567_890n,
56
58
  false,
57
59
  )).toBe(undefined)
58
- expect(Address.from(
60
+ expect(toAddressV2(
59
61
  1_234_567_890_123_456_789_012_345_678_901_234_567n,
60
62
  true,
61
63
  )).toBe('0000000000edc4e57e669eb52d3a8e6e7c9f4b87')
@@ -63,7 +65,7 @@ describe('address [zod]', () => {
63
65
 
64
66
  test('from [assert]', () => {
65
67
  try {
66
- Address.from(
68
+ toAddressV2(
67
69
  1_234_567_890_123_456_789_012_345_678_901_234_567_890_123_456_789_012_345_678_901_234_567_890n,
68
70
  true,
69
71
  )
@@ -1,11 +0,0 @@
1
- import type { Hex } from '../hex/index.ts';
2
- export type Address = Hex & {
3
- readonly __address: true;
4
- };
5
- export declare namespace Address {
6
- const identify: (value: unknown) => value is Address;
7
- const cast: (value: unknown, assert?: boolean) => Address | undefined;
8
- const from: (value: unknown, assert?: boolean) => Address | undefined;
9
- const toString: (value: Address) => string;
10
- }
11
- //# sourceMappingURL=AddressNamespace.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AddressNamespace.d.ts","sourceRoot":"","sources":["../../../src/address/AddressNamespace.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AAK1C,MAAM,MAAM,OAAO,GAAG,GAAG,GAAG;IAAE,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAA;CAAE,CAAA;AAGxD,yBAAiB,OAAO,CAAC;IAChB,MAAM,QAAQ,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,OAAwD,CAAA;IACpG,MAAM,IAAI,GAAI,OAAO,OAAO,EAAE,SAAQ,OAAe,KAAG,OAAO,GAAG,SAE3B,CAAA;IACvC,MAAM,IAAI,GAAI,OAAO,OAAO,EAAE,SAAQ,OAAe,KAAG,OAAO,GAAG,SAE5B,CAAA;IACtC,MAAM,QAAQ,GAAI,OAAO,OAAO,KAAG,MAAyB,CAAA;CACpE"}
@@ -1,18 +0,0 @@
1
- import type { Hex } from '../hex/index.ts'
2
- import { AddressZodTransform } from './AddressZodTransform.ts'
3
- import { AddressZodValidation } from './AddressZodValidation.ts'
4
-
5
- // using true instead of unique symbol to avoid conflicts with other versions of library
6
- export type Address = Hex & { readonly __address: true }
7
-
8
- // eslint-disable-next-line @typescript-eslint/no-namespace
9
- export namespace Address {
10
- export const identify = (value: unknown): value is Address => AddressZodValidation.safeParse(value).success
11
- export const cast = (value: unknown, assert: boolean = false): Address | undefined => assert
12
- ? AddressZodValidation.parse(value)
13
- : AddressZodValidation.safeParse(value).data
14
- export const from = (value: unknown, assert: boolean = false): Address | undefined => assert
15
- ? AddressZodTransform.parse(value)
16
- : AddressZodTransform.safeParse(value).data
17
- export const toString = (value: Address): string => value as string
18
- }