@xylabs/hex 5.0.5 → 5.0.7

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 (96) hide show
  1. package/dist/neutral/address/AddressTransformZod.d.ts +8 -0
  2. package/dist/neutral/address/AddressTransformZod.d.ts.map +1 -0
  3. package/dist/neutral/address/AddressValidationZod.d.ts +8 -0
  4. package/dist/neutral/address/AddressValidationZod.d.ts.map +1 -0
  5. package/dist/neutral/address/address.d.ts +8 -23
  6. package/dist/neutral/address/address.d.ts.map +1 -1
  7. package/dist/neutral/address/addressDeprecated.d.ts +18 -0
  8. package/dist/neutral/address/addressDeprecated.d.ts.map +1 -0
  9. package/dist/neutral/address/as.d.ts +7 -0
  10. package/dist/neutral/address/as.d.ts.map +1 -0
  11. package/dist/neutral/address/index.d.ts +6 -2
  12. package/dist/neutral/address/index.d.ts.map +1 -1
  13. package/dist/neutral/address/is.d.ts +6 -0
  14. package/dist/neutral/address/is.d.ts.map +1 -0
  15. package/dist/neutral/address/to.d.ts +6 -0
  16. package/dist/neutral/address/to.d.ts.map +1 -0
  17. package/dist/neutral/ethAddress.d.ts +5 -1
  18. package/dist/neutral/ethAddress.d.ts.map +1 -1
  19. package/dist/neutral/hash/as.d.ts +5 -0
  20. package/dist/neutral/hash/as.d.ts.map +1 -0
  21. package/dist/neutral/hash/hash.d.ts +14 -0
  22. package/dist/neutral/hash/hash.d.ts.map +1 -0
  23. package/dist/neutral/hash/index.d.ts +5 -0
  24. package/dist/neutral/hash/index.d.ts.map +1 -0
  25. package/dist/neutral/hash/is.d.ts +3 -0
  26. package/dist/neutral/hash/is.d.ts.map +1 -0
  27. package/dist/neutral/hash/zod.d.ts +20 -0
  28. package/dist/neutral/hash/zod.d.ts.map +1 -0
  29. package/dist/neutral/hex/as.d.ts +1 -1
  30. package/dist/neutral/hex/as.d.ts.map +1 -1
  31. package/dist/neutral/hex/from/from.d.ts +1 -1
  32. package/dist/neutral/hex/from/from.d.ts.map +1 -1
  33. package/dist/neutral/hex/from/fromArrayBuffer.d.ts +1 -1
  34. package/dist/neutral/hex/from/fromArrayBuffer.d.ts.map +1 -1
  35. package/dist/neutral/hex/from/fromBigInt.d.ts +1 -1
  36. package/dist/neutral/hex/from/fromBigInt.d.ts.map +1 -1
  37. package/dist/neutral/hex/from/fromHexString.d.ts +1 -1
  38. package/dist/neutral/hex/from/fromHexString.d.ts.map +1 -1
  39. package/dist/neutral/hex/from/fromNumber.d.ts +1 -1
  40. package/dist/neutral/hex/from/fromNumber.d.ts.map +1 -1
  41. package/dist/neutral/hex/{model.d.ts → hex.d.ts} +2 -2
  42. package/dist/neutral/hex/hex.d.ts.map +1 -0
  43. package/dist/neutral/hex/index.d.ts +1 -1
  44. package/dist/neutral/hex/index.d.ts.map +1 -1
  45. package/dist/neutral/hex/is.d.ts +1 -1
  46. package/dist/neutral/hex/is.d.ts.map +1 -1
  47. package/dist/neutral/hex/to.d.ts +1 -1
  48. package/dist/neutral/hex/to.d.ts.map +1 -1
  49. package/dist/neutral/index-internal.d.ts +1 -1
  50. package/dist/neutral/index-internal.d.ts.map +1 -1
  51. package/dist/neutral/index-internal.mjs +101 -62
  52. package/dist/neutral/index-internal.mjs.map +1 -1
  53. package/dist/neutral/index.d.ts +1 -0
  54. package/dist/neutral/index.d.ts.map +1 -1
  55. package/dist/neutral/index.mjs +108 -62
  56. package/dist/neutral/index.mjs.map +1 -1
  57. package/dist/neutral/zod.d.ts +10 -0
  58. package/dist/neutral/zod.d.ts.map +1 -0
  59. package/package.json +3 -3
  60. package/src/address/AddressTransformZod.ts +26 -0
  61. package/src/address/{AddressZodValidation.ts → AddressValidationZod.ts} +3 -3
  62. package/src/address/address.ts +7 -77
  63. package/src/address/addressDeprecated.ts +18 -0
  64. package/src/address/as.ts +37 -0
  65. package/src/address/index.ts +6 -2
  66. package/src/address/is.ts +14 -0
  67. package/src/address/to.ts +18 -0
  68. package/src/ethAddress.ts +10 -3
  69. package/src/hash/as.ts +24 -0
  70. package/src/hash/hash.ts +23 -0
  71. package/src/hash/index.ts +4 -0
  72. package/src/hash/is.ts +6 -0
  73. package/src/hash/zod.ts +8 -0
  74. package/src/hex/as.ts +1 -1
  75. package/src/hex/from/from.ts +1 -1
  76. package/src/hex/from/fromArrayBuffer.ts +1 -1
  77. package/src/hex/from/fromBigInt.ts +1 -1
  78. package/src/hex/from/fromHexString.ts +1 -1
  79. package/src/hex/from/fromNumber.ts +1 -1
  80. package/src/hex/{model.ts → hex.ts} +1 -1
  81. package/src/hex/index.ts +1 -1
  82. package/src/hex/is.ts +1 -1
  83. package/src/hex/to.ts +1 -1
  84. package/src/index-internal.ts +1 -1
  85. package/src/index.ts +1 -0
  86. package/src/spec/hash.spec.ts +1 -1
  87. package/src/zod.ts +7 -0
  88. package/dist/neutral/address/AddressZodTransform.d.ts +0 -5
  89. package/dist/neutral/address/AddressZodTransform.d.ts.map +0 -1
  90. package/dist/neutral/address/AddressZodValidation.d.ts +0 -5
  91. package/dist/neutral/address/AddressZodValidation.d.ts.map +0 -1
  92. package/dist/neutral/hash.d.ts +0 -17
  93. package/dist/neutral/hash.d.ts.map +0 -1
  94. package/dist/neutral/hex/model.d.ts.map +0 -1
  95. package/src/address/AddressZodTransform.ts +0 -19
  96. package/src/hash.ts +0 -51
@@ -0,0 +1,14 @@
1
+ import type { HexConfig } from '../hex/index.ts'
2
+ import { isHex } from '../hex/index.ts'
3
+ import type { Address } from './address.ts'
4
+ import { AddressValidationZod } from './AddressValidationZod.ts'
5
+
6
+ export const isAddress = (value: unknown, config: HexConfig = {}): value is Address => {
7
+ const { bitLength = 160, prefix = false } = config
8
+ return isHex(value, { bitLength, prefix })
9
+ }
10
+
11
+ /** @alpha */
12
+ export function isAddressV2(value: unknown): value is Address {
13
+ return AddressValidationZod.safeParse(value).success
14
+ }
@@ -0,0 +1,18 @@
1
+ import type { HexConfig } from '../hex/index.ts'
2
+ import { hexFrom } from '../hex/index.ts'
3
+ import type { Address } from './address.ts'
4
+ import { AddressTransformZod } from './AddressTransformZod.ts'
5
+
6
+ export const toAddress = (value: string | number | bigint | ArrayBufferLike, config: HexConfig = {}): Address => {
7
+ const { bitLength = 160, prefix = false } = config
8
+ return hexFrom(value, {
9
+ bitLength, prefix, ...config,
10
+ }) as unknown as Address
11
+ }
12
+
13
+ /** @alpha */
14
+ export function toAddressV2(value: unknown, assert: boolean = false): Address | undefined {
15
+ return assert
16
+ ? AddressTransformZod.parse(value)
17
+ : AddressTransformZod.safeParse(value).data
18
+ }
package/src/ethAddress.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Brand } from '@xylabs/typeof'
2
- import z from 'zod'
2
+ import { z } from 'zod'
3
3
 
4
4
  import type { AssertConfig } from './assert.ts'
5
5
  import { assertError } from './assert.ts'
@@ -11,8 +11,15 @@ import { HexRegExMinMaxMixedCaseWithPrefix } from './HexRegEx.ts'
11
11
 
12
12
  export const EthAddressRegEx = HexRegExMinMaxMixedCaseWithPrefix(20, 20)
13
13
 
14
- export const EthAddressToStringSchema = z.string().regex(EthAddressRegEx)
15
- export const EthAddressFromStringSchema = z.string().regex(EthAddressRegEx).transform(v => toEthAddress(v))
14
+ export const EthAddressToStringZod = z.string().regex(EthAddressRegEx)
15
+
16
+ /** @deprecated use EthAddressToStringZod */
17
+ export const EthAddressToStringSchema = EthAddressToStringZod
18
+
19
+ export const EthAddressFromStringZod = z.string().regex(EthAddressRegEx).transform(v => toEthAddress(v))
20
+
21
+ /** @deprecated use EthAddressFromStringZod */
22
+ export const EthAddressFromStringSchema = EthAddressFromStringZod
16
23
 
17
24
  // using true instead of unique symbol to avoid conflicts with other versions of library
18
25
  export type EthAddress = Brand<string, { readonly __eth_address: true }>
package/src/hash/as.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { isUndefined } from '@xylabs/typeof'
2
+
3
+ import type { AssertConfig } from '../assert.ts'
4
+ import { assertError } from '../assert.ts'
5
+ import { hexFromHexString } from '../hex/index.ts'
6
+ import { type Hash } from './hash.ts'
7
+ import { isHash } from './is.ts'
8
+
9
+ export function asHash(value: unknown): Hash | undefined
10
+ export function asHash(value: unknown, assert: AssertConfig): Hash
11
+ export function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {
12
+ let stringValue: string | undefined = undefined
13
+
14
+ switch (typeof value) {
15
+ case 'string': {
16
+ stringValue = hexFromHexString(value)
17
+ break
18
+ }
19
+ default: {
20
+ return isUndefined(assert) ? undefined : assertError(value, assert, `Unsupported type [${typeof value}]`)
21
+ }
22
+ }
23
+ return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)
24
+ }
@@ -0,0 +1,23 @@
1
+ import type { Brand } from '@xylabs/typeof'
2
+ import { z } from 'zod'
3
+
4
+ import type { Hex } from '../hex/index.ts'
5
+ import { HexRegExMinMax } from '../HexRegEx.ts'
6
+
7
+ export const HASH_LENGTH = 32 as const
8
+
9
+ export const HashRegEx = HexRegExMinMax(HASH_LENGTH, HASH_LENGTH)
10
+
11
+ export const ZERO_HASH = '0000000000000000000000000000000000000000000000000000000000000000' as Hash
12
+
13
+ export type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096
14
+ export const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]
15
+
16
+ export const isHashBitLength = (value: unknown): value is HashBitLength => {
17
+ return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)
18
+ }
19
+
20
+ export const HashZod = z.string()
21
+ .regex(HashRegEx, { message: 'Invalid hex format' }) as unknown as z.ZodType<Brand<Hex, { readonly __hash: true }>>
22
+
23
+ export type Hash = z.infer<typeof HashZod>
@@ -0,0 +1,4 @@
1
+ export * from './as.ts'
2
+ export * from './hash.ts'
3
+ export * from './is.ts'
4
+ export * from './zod.ts'
package/src/hash/is.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { isHex } from '../hex/index.ts'
2
+ import type { Hash, HashBitLength } from './hash.ts'
3
+
4
+ export const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {
5
+ return isHex(value, { bitLength })
6
+ }
@@ -0,0 +1,8 @@
1
+ import { z } from 'zod'
2
+
3
+ import { asHash } from './as.ts'
4
+ import type { Hash } from './hash.ts'
5
+ import { HashZod } from './hash.ts'
6
+
7
+ export const HashToJsonZod = HashZod.transform<string>(v => v)
8
+ export const JsonToHashZod = z.string().transform<Hash>(v => asHash(v, true))
package/src/hex/as.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import type { AssertConfig } from '../assert.ts'
2
2
  import { assertError } from '../assert.ts'
3
3
  import { hexFromHexString } from './from/index.ts'
4
+ import type { Hex } from './hex.ts'
4
5
  import { isHex } from './is.ts'
5
- import type { Hex } from './model.ts'
6
6
 
7
7
  export function asHex(value: unknown): Hex | undefined
8
8
  export function asHex(value: unknown, assert: AssertConfig): Hex
@@ -1,4 +1,4 @@
1
- import type { Hex, HexConfig } from '../model.ts'
1
+ import type { Hex, HexConfig } from '../hex.ts'
2
2
  import { hexFromArrayBuffer } from './fromArrayBuffer.ts'
3
3
  import { hexFromBigInt } from './fromBigInt.ts'
4
4
  import { hexFromHexString } from './fromHexString.ts'
@@ -1,4 +1,4 @@
1
- import type { Hex, HexConfig } from '../model.ts'
1
+ import type { Hex, HexConfig } from '../hex.ts'
2
2
  import { hexFromHexString } from './fromHexString.ts'
3
3
 
4
4
  /** Convert an ArrayBuffer to a hex string */
@@ -1,4 +1,4 @@
1
- import type { Hex, HexConfig } from '../model.ts'
1
+ import type { Hex, HexConfig } from '../hex.ts'
2
2
  import { hexFromHexString } from './fromHexString.ts'
3
3
 
4
4
  /** Convert a bigint to a hex string */
@@ -1,7 +1,7 @@
1
1
  import { isNumber } from '@xylabs/typeof'
2
2
 
3
+ import type { Hex, HexConfig } from '../hex.ts'
3
4
  import { isHex } from '../is.ts'
4
- import type { Hex, HexConfig } from '../model.ts'
5
5
  import { bitsToNibbles } from '../nibble.ts'
6
6
 
7
7
  export const hexFromHexString = (value: string, config: HexConfig = {}): Hex => {
@@ -1,4 +1,4 @@
1
- import type { Hex, HexConfig } from '../model.ts'
1
+ import type { Hex, HexConfig } from '../hex.ts'
2
2
  import { hexFromBigInt } from './fromBigInt.ts'
3
3
 
4
4
  export const hexFromNumber = (value: number, config?: HexConfig): Hex => {
@@ -1,5 +1,5 @@
1
1
  import type { Brand } from '@xylabs/typeof'
2
- import z from 'zod'
2
+ import { z } from 'zod'
3
3
 
4
4
  import { HexRegEx } from '../HexRegEx.ts'
5
5
 
package/src/hex/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export * from './as.ts'
2
2
  export * from './from/index.ts'
3
+ export * from './hex.ts'
3
4
  export * from './is.ts'
4
5
  export * from './isHexZero.ts'
5
6
  export * from './legacy.ts'
6
- export * from './model.ts'
7
7
  export * from './nibble.ts'
8
8
  export * from './to.ts'
package/src/hex/is.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { HexRegEx, HexRegExWithPrefix } from '../HexRegEx.ts'
2
- import type { Hex, HexConfig } from './model.ts'
2
+ import type { Hex, HexConfig } from './hex.ts'
3
3
  import { bitsToNibbles } from './nibble.ts'
4
4
 
5
5
  export const isHex = (value: unknown, config?: HexConfig): value is Hex => {
package/src/hex/to.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { hexFrom } from './from/index.ts'
2
- import type { HexConfig } from './model.ts'
2
+ import type { HexConfig } from './hex.ts'
3
3
 
4
4
  /** takes any value and tries our best to convert it to a hex string */
5
5
  export const toHex = (
@@ -1,7 +1,7 @@
1
1
  export * from './address/index.ts'
2
2
  export * from './assert.ts'
3
3
  export * from './ethAddress.ts'
4
- export * from './hash.ts'
4
+ export * from './hash/index.ts'
5
5
  export * from './hex/index.ts'
6
6
  export * from './HexRegEx.ts'
7
7
  export * from './hexToBigInt.ts'
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './Brand.ts'
2
2
  export * from './index-internal.ts'
3
+ export * from './zod.ts'
@@ -4,7 +4,7 @@ import {
4
4
  describe, expect, test,
5
5
  } from 'vitest'
6
6
 
7
- import { isHash } from '../hash.ts'
7
+ import { isHash } from '../hash/index.ts'
8
8
  import { hexFromNumber } from '../hex/index.ts'
9
9
 
10
10
  describe('hash', () => {
package/src/zod.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { z } from 'zod'
2
+
3
+ import { toHex } from './hex/index.ts'
4
+ import { hexToBigInt } from './hexToBigInt.ts'
5
+
6
+ export const BigIntToJsonZod = z.bigint().nonnegative().transform(x => toHex(x))
7
+ export const JsonToBigIntZod = z.string().transform(x => toHex(x)).transform(x => hexToBigInt(x))
@@ -1,5 +0,0 @@
1
- import z from 'zod';
2
- import type { Address } from './address.ts';
3
- export declare const AddressZodTransform: z.ZodPipe<z.ZodPipe<z.ZodUnion<readonly [z.ZodString, z.ZodBigInt]>, z.ZodTransform<string, string | bigint>>, z.ZodTransform<Address, string>>;
4
- export type AddressZodTransformType = z.infer<typeof AddressZodTransform>;
5
- //# sourceMappingURL=AddressZodTransform.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AddressZodTransform.d.ts","sourceRoot":"","sources":["../../../src/address/AddressZodTransform.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAA;AAEnB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAG3C,eAAO,MAAM,mBAAmB,iJAWD,CAAA;AAE/B,MAAM,MAAM,uBAAuB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAA"}
@@ -1,5 +0,0 @@
1
- import z from 'zod';
2
- import type { Address } from './address.ts';
3
- export declare const AddressZodValidation: z.ZodPipe<z.ZodString, z.ZodTransform<Address, string>>;
4
- export type AddressZodValidationType = z.infer<typeof AddressZodValidation>;
5
- //# sourceMappingURL=AddressZodValidation.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AddressZodValidation.d.ts","sourceRoot":"","sources":["../../../src/address/AddressZodValidation.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAA;AAGnB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAG3C,eAAO,MAAM,oBAAoB,yDAGF,CAAA;AAE/B,MAAM,MAAM,wBAAwB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAA"}
@@ -1,17 +0,0 @@
1
- import type { Brand } from '@xylabs/typeof';
2
- import z from 'zod';
3
- import type { AssertConfig } from './assert.ts';
4
- import type { Hex } from './hex/index.ts';
5
- export declare const HashRegEx: RegExp;
6
- export declare const ZERO_HASH: Hash;
7
- export type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096;
8
- export declare const HashBitLength: HashBitLength[];
9
- export declare const isHashBitLength: (value: unknown) => value is HashBitLength;
10
- export type Hash = Brand<Hex, {
11
- readonly __hash: true;
12
- }>;
13
- export declare const isHash: (value: unknown, bitLength?: HashBitLength) => value is Hash;
14
- export declare const HashZod: z.ZodString;
15
- export declare function asHash(value: unknown): Hash | undefined;
16
- export declare function asHash(value: unknown, assert: AssertConfig): Hash;
17
- //# sourceMappingURL=hash.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAE3C,OAAO,CAAC,MAAM,KAAK,CAAA;AAEnB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAA;AAIzC,eAAO,MAAM,SAAS,QAAyB,CAAA;AAE/C,eAAO,MAAM,SAAS,EAAyE,IAAI,CAAA;AAEnG,MAAM,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAA;AAC1E,eAAO,MAAM,aAAa,EAAE,aAAa,EAA8C,CAAA;AAEvF,eAAO,MAAM,eAAe,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,aAEzD,CAAA;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 +0,0 @@
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;AAInB,eAAO,MAAM,MAAM,EACiD,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE;IAAE,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAA;CAAE,CAAC,CAAC,CAAA;AAEjI,MAAM,MAAM,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,MAAM,CAAC,CAAA;AAExC,oDAAoD;AACpD,MAAM,WAAW,SAAS;IACxB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB"}
@@ -1,19 +0,0 @@
1
- import z from 'zod'
2
-
3
- import type { Address } from './address.ts'
4
- import { AddressZodValidation } from './AddressZodValidation.ts'
5
-
6
- export const AddressZodTransform = z.union([z.string(), z.bigint()])
7
- .transform((value) => {
8
- if (typeof value === 'bigint') {
9
- return value.toString(16).padStart(40, '0')
10
- }
11
- if (value.startsWith('0x')) {
12
- return value.slice(2)
13
- }
14
- return value
15
- })
16
- .refine(x => AddressZodValidation.safeParse(x).data)
17
- .transform(x => x as Address)
18
-
19
- export type AddressZodTransformType = z.infer<typeof AddressZodTransform>
package/src/hash.ts DELETED
@@ -1,51 +0,0 @@
1
- import type { Brand } from '@xylabs/typeof'
2
- import { isUndefined } from '@xylabs/typeof'
3
- import z from 'zod'
4
-
5
- import type { AssertConfig } from './assert.ts'
6
- import { assertError } from './assert.ts'
7
- import type { Hex } from './hex/index.ts'
8
- import { hexFromHexString, isHex } from './hex/index.ts'
9
- import { HexRegExMinMax } from './HexRegEx.ts'
10
-
11
- export const HashRegEx = HexRegExMinMax(32, 32)
12
-
13
- export const ZERO_HASH = '0000000000000000000000000000000000000000000000000000000000000000' as Hash
14
-
15
- export type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096
16
- export const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]
17
-
18
- export const isHashBitLength = (value: unknown): value is HashBitLength => {
19
- return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)
20
- }
21
-
22
- // using true instead of unique symbol to avoid conflicts with other versions of library
23
- export type Hash = Brand<Hex, { readonly __hash: true }>
24
-
25
- export const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {
26
- return isHex(value, { bitLength })
27
- }
28
-
29
- export const HashZod = z.string()
30
- .toLowerCase()
31
- .regex(HashRegEx, { message: 'Invalid hash format' })
32
- .refine(
33
- isHash,
34
- )
35
-
36
- export function asHash(value: unknown): Hash | undefined
37
- export function asHash(value: unknown, assert: AssertConfig): Hash
38
- export function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {
39
- let stringValue: string | undefined = undefined
40
-
41
- switch (typeof value) {
42
- case 'string': {
43
- stringValue = hexFromHexString(value)
44
- break
45
- }
46
- default: {
47
- return isUndefined(assert) ? undefined : assertError(value, assert, `Unsupported type [${typeof value}]`)
48
- }
49
- }
50
- return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)
51
- }