viem 0.4.0-main.20230418T011420 → 0.4.0-main.20230419T210450

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 (127) hide show
  1. package/README.md +6 -0
  2. package/dist/cjs/actions/public/call.js +97 -10
  3. package/dist/cjs/actions/public/call.js.map +1 -1
  4. package/dist/cjs/actions/public/simulateContract.js +1 -0
  5. package/dist/cjs/actions/public/simulateContract.js.map +1 -1
  6. package/dist/cjs/chains.js +1 -24
  7. package/dist/cjs/chains.js.map +1 -1
  8. package/dist/cjs/clients/createPublicClient.js +2 -1
  9. package/dist/cjs/clients/createPublicClient.js.map +1 -1
  10. package/dist/cjs/constants/contract.js +5 -0
  11. package/dist/cjs/constants/contract.js.map +1 -0
  12. package/dist/cjs/constants/index.js +3 -1
  13. package/dist/cjs/constants/index.js.map +1 -1
  14. package/dist/cjs/errors/chain.js +13 -1
  15. package/dist/cjs/errors/chain.js.map +1 -1
  16. package/dist/cjs/errors/contract.js +1 -1
  17. package/dist/cjs/errors/contract.js.map +1 -1
  18. package/dist/cjs/errors/encoding.js +13 -1
  19. package/dist/cjs/errors/encoding.js.map +1 -1
  20. package/dist/cjs/errors/index.js +5 -2
  21. package/dist/cjs/errors/index.js.map +1 -1
  22. package/dist/cjs/errors/version.js +1 -1
  23. package/dist/cjs/index.js +5 -4
  24. package/dist/cjs/index.js.map +1 -1
  25. package/dist/cjs/utils/encoding/fromBytes.js +29 -12
  26. package/dist/cjs/utils/encoding/fromBytes.js.map +1 -1
  27. package/dist/cjs/utils/encoding/fromHex.js +32 -10
  28. package/dist/cjs/utils/encoding/fromHex.js.map +1 -1
  29. package/dist/cjs/utils/encoding/toBytes.js +31 -15
  30. package/dist/cjs/utils/encoding/toBytes.js.map +1 -1
  31. package/dist/cjs/utils/encoding/toHex.js +31 -20
  32. package/dist/cjs/utils/encoding/toHex.js.map +1 -1
  33. package/dist/cjs/utils/errors/getContractError.js +7 -5
  34. package/dist/cjs/utils/errors/getContractError.js.map +1 -1
  35. package/dist/cjs/utils/promise/createBatchScheduler.js +47 -0
  36. package/dist/cjs/utils/promise/createBatchScheduler.js.map +1 -0
  37. package/dist/cjs/utils/promise/index.js +3 -1
  38. package/dist/cjs/utils/promise/index.js.map +1 -1
  39. package/dist/esm/actions/public/call.js +91 -4
  40. package/dist/esm/actions/public/call.js.map +1 -1
  41. package/dist/esm/actions/public/simulateContract.js +1 -0
  42. package/dist/esm/actions/public/simulateContract.js.map +1 -1
  43. package/dist/esm/clients/createPublicClient.js +2 -1
  44. package/dist/esm/clients/createPublicClient.js.map +1 -1
  45. package/dist/esm/constants/contract.js +2 -0
  46. package/dist/esm/constants/contract.js.map +1 -0
  47. package/dist/esm/constants/index.js +1 -0
  48. package/dist/esm/constants/index.js.map +1 -1
  49. package/dist/esm/errors/chain.js +11 -0
  50. package/dist/esm/errors/chain.js.map +1 -1
  51. package/dist/esm/errors/contract.js +1 -1
  52. package/dist/esm/errors/contract.js.map +1 -1
  53. package/dist/esm/errors/encoding.js +11 -0
  54. package/dist/esm/errors/encoding.js.map +1 -1
  55. package/dist/esm/errors/index.js +2 -2
  56. package/dist/esm/errors/index.js.map +1 -1
  57. package/dist/esm/errors/version.js +1 -1
  58. package/dist/esm/index.js +1 -1
  59. package/dist/esm/index.js.map +1 -1
  60. package/dist/esm/utils/encoding/fromBytes.js +30 -13
  61. package/dist/esm/utils/encoding/fromBytes.js.map +1 -1
  62. package/dist/esm/utils/encoding/fromHex.js +31 -10
  63. package/dist/esm/utils/encoding/fromHex.js.map +1 -1
  64. package/dist/esm/utils/encoding/toBytes.js +31 -15
  65. package/dist/esm/utils/encoding/toBytes.js.map +1 -1
  66. package/dist/esm/utils/encoding/toHex.js +28 -17
  67. package/dist/esm/utils/encoding/toHex.js.map +1 -1
  68. package/dist/esm/utils/errors/getContractError.js +7 -5
  69. package/dist/esm/utils/errors/getContractError.js.map +1 -1
  70. package/dist/esm/utils/promise/createBatchScheduler.js +43 -0
  71. package/dist/esm/utils/promise/createBatchScheduler.js.map +1 -0
  72. package/dist/esm/utils/promise/index.js +1 -0
  73. package/dist/esm/utils/promise/index.js.map +1 -1
  74. package/dist/types/actions/public/call.d.ts +1 -0
  75. package/dist/types/actions/public/call.d.ts.map +1 -1
  76. package/dist/types/actions/public/simulateContract.d.ts +1 -1
  77. package/dist/types/actions/public/simulateContract.d.ts.map +1 -1
  78. package/dist/types/clients/createPublicClient.d.ts +15 -3
  79. package/dist/types/clients/createPublicClient.d.ts.map +1 -1
  80. package/dist/types/constants/contract.d.ts +2 -0
  81. package/dist/types/constants/contract.d.ts.map +1 -0
  82. package/dist/types/constants/index.d.ts +1 -0
  83. package/dist/types/constants/index.d.ts.map +1 -1
  84. package/dist/types/errors/chain.d.ts +4 -0
  85. package/dist/types/errors/chain.d.ts.map +1 -1
  86. package/dist/types/errors/encoding.d.ts +7 -0
  87. package/dist/types/errors/encoding.d.ts.map +1 -1
  88. package/dist/types/errors/index.d.ts +2 -2
  89. package/dist/types/errors/index.d.ts.map +1 -1
  90. package/dist/types/errors/version.d.ts +1 -1
  91. package/dist/types/index.d.ts +1 -1
  92. package/dist/types/index.d.ts.map +1 -1
  93. package/dist/types/types/eip1193.d.ts +1 -1
  94. package/dist/types/types/eip1193.d.ts.map +1 -1
  95. package/dist/types/utils/encoding/fromBytes.d.ts +95 -10
  96. package/dist/types/utils/encoding/fromBytes.d.ts.map +1 -1
  97. package/dist/types/utils/encoding/fromHex.d.ts +122 -12
  98. package/dist/types/utils/encoding/fromHex.d.ts.map +1 -1
  99. package/dist/types/utils/encoding/toBytes.d.ts +113 -9
  100. package/dist/types/utils/encoding/toBytes.d.ts.map +1 -1
  101. package/dist/types/utils/encoding/toHex.d.ts +120 -10
  102. package/dist/types/utils/encoding/toHex.d.ts.map +1 -1
  103. package/dist/types/utils/errors/getContractError.d.ts.map +1 -1
  104. package/dist/types/utils/promise/createBatchScheduler.d.ts +15 -0
  105. package/dist/types/utils/promise/createBatchScheduler.d.ts.map +1 -0
  106. package/dist/types/utils/promise/index.d.ts +1 -0
  107. package/dist/types/utils/promise/index.d.ts.map +1 -1
  108. package/package.json +1 -1
  109. package/src/actions/public/call.ts +141 -7
  110. package/src/actions/public/simulateContract.ts +2 -1
  111. package/src/clients/createPublicClient.ts +17 -1
  112. package/src/constants/contract.ts +1 -0
  113. package/src/constants/index.ts +2 -0
  114. package/src/errors/chain.ts +8 -0
  115. package/src/errors/contract.ts +1 -1
  116. package/src/errors/encoding.ts +9 -0
  117. package/src/errors/index.ts +2 -0
  118. package/src/errors/version.ts +1 -1
  119. package/src/index.ts +1 -0
  120. package/src/types/eip1193.ts +1 -1
  121. package/src/utils/encoding/fromBytes.ts +147 -18
  122. package/src/utils/encoding/fromHex.ts +162 -19
  123. package/src/utils/encoding/toBytes.ts +148 -18
  124. package/src/utils/encoding/toHex.ts +151 -26
  125. package/src/utils/errors/getContractError.ts +9 -5
  126. package/src/utils/promise/createBatchScheduler.ts +82 -0
  127. package/src/utils/promise/index.ts +1 -0
@@ -81,3 +81,12 @@ export class OffsetOutOfBoundsError extends BaseError {
81
81
  )
82
82
  }
83
83
  }
84
+
85
+ export class SizeOverflowError extends BaseError {
86
+ override name = 'SizeOverflowError'
87
+ constructor({ givenSize, maxSize }: { givenSize: number; maxSize: number }) {
88
+ super(
89
+ `Size cannot exceed ${maxSize} bytes. Given size: ${givenSize} bytes.`,
90
+ )
91
+ }
92
+ }
@@ -37,6 +37,7 @@ export {
37
37
  ChainDoesNotSupportContract,
38
38
  ChainMismatchError,
39
39
  ChainNotFoundError,
40
+ ClientChainNotConfiguredError,
40
41
  InvalidChainIdError,
41
42
  } from './chain.js'
42
43
 
@@ -58,6 +59,7 @@ export {
58
59
  InvalidHexBooleanError,
59
60
  InvalidHexValueError,
60
61
  OffsetOutOfBoundsError,
62
+ SizeOverflowError,
61
63
  } from './encoding.js'
62
64
 
63
65
  export {
@@ -1 +1 @@
1
- export const version = '0.4.0-main.20230418T011420'
1
+ export const version = '0.4.0-main.20230419T210450'
package/src/index.ts CHANGED
@@ -182,6 +182,7 @@ export {
182
182
  CallExecutionError,
183
183
  ChainDisconnectedError,
184
184
  ChainDoesNotSupportContract,
185
+ ClientChainNotConfiguredError,
185
186
  ContractFunctionExecutionError,
186
187
  ContractFunctionRevertedError,
187
188
  ContractFunctionZeroDataError,
@@ -207,7 +207,7 @@ export type PublicRequests = {
207
207
  */
208
208
  method: 'eth_call'
209
209
  params: [
210
- request: TransactionRequest,
210
+ request: Partial<TransactionRequest>,
211
211
  block: BlockNumber | BlockTag | BlockIdentifier,
212
212
  ]
213
213
  }): Promise<Hex>
@@ -1,8 +1,20 @@
1
1
  import { InvalidBytesBooleanError } from '../../errors/index.js'
2
2
  import type { ByteArray, Hex } from '../../types/index.js'
3
- import { hexToBigInt, hexToNumber } from './fromHex.js'
3
+ import { trim } from '../data/trim.js'
4
+ import { assertSize, hexToBigInt, hexToNumber } from './fromHex.js'
4
5
  import { bytesToHex } from './toHex.js'
5
6
 
7
+ export type FromBytesParameters<
8
+ TTo extends 'string' | 'hex' | 'bigint' | 'number' | 'boolean',
9
+ > =
10
+ | TTo
11
+ | {
12
+ /** Size of the bytes. */
13
+ size?: number
14
+ /** Type to convert to. */
15
+ to: TTo
16
+ }
17
+
6
18
  type FromBytesReturnType<TTo> = TTo extends 'string'
7
19
  ? string
8
20
  : TTo extends 'hex'
@@ -16,30 +28,106 @@ type FromBytesReturnType<TTo> = TTo extends 'string'
16
28
  : never
17
29
 
18
30
  /**
19
- * @description Decodes a byte array into a UTF-8 string, hex value, number, bigint or boolean.
31
+ * Decodes a byte array into a UTF-8 string, hex value, number, bigint or boolean.
32
+ *
33
+ * - Docs: https://viem.sh/docs/utilities/fromBytes.html
34
+ * - Example: https://viem.sh/docs/utilities/fromBytes.html#usage
35
+ *
36
+ * @param bytes Byte array to decode.
37
+ * @param toOrOpts Type to convert to or options.
38
+ * @returns Decoded value.
39
+ *
40
+ * @example
41
+ * import { fromBytes } from 'viem'
42
+ * const data = fromBytes(new Uint8Array([1, 164]), 'number')
43
+ * // 420
44
+ *
45
+ * @example
46
+ * import { fromBytes } from 'viem'
47
+ * const data = fromBytes(
48
+ * new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]),
49
+ * 'string'
50
+ * )
51
+ * // 'Hello world'
20
52
  */
21
53
  export function fromBytes<
22
54
  TTo extends 'string' | 'hex' | 'bigint' | 'number' | 'boolean',
23
- >(bytes: ByteArray, to: TTo): FromBytesReturnType<TTo> {
24
- if (to === 'number') return bytesToNumber(bytes) as FromBytesReturnType<TTo>
25
- if (to === 'bigint') return bytesToBigint(bytes) as FromBytesReturnType<TTo>
26
- if (to === 'boolean') return bytesToBool(bytes) as FromBytesReturnType<TTo>
27
- if (to === 'string') return bytesToString(bytes) as FromBytesReturnType<TTo>
28
- return bytesToHex(bytes) as FromBytesReturnType<TTo>
55
+ >(
56
+ bytes: ByteArray,
57
+ toOrOpts: FromBytesParameters<TTo>,
58
+ ): FromBytesReturnType<TTo> {
59
+ const opts = typeof toOrOpts === 'string' ? { to: toOrOpts } : toOrOpts
60
+ const to = opts.to
61
+
62
+ if (to === 'number')
63
+ return bytesToNumber(bytes, opts) as FromBytesReturnType<TTo>
64
+ if (to === 'bigint')
65
+ return bytesToBigint(bytes, opts) as FromBytesReturnType<TTo>
66
+ if (to === 'boolean')
67
+ return bytesToBool(bytes, opts) as FromBytesReturnType<TTo>
68
+ if (to === 'string')
69
+ return bytesToString(bytes, opts) as FromBytesReturnType<TTo>
70
+ return bytesToHex(bytes, opts) as FromBytesReturnType<TTo>
71
+ }
72
+
73
+ export type BytesToBigIntOpts = {
74
+ /** Whether or not the number of a signed representation. */
75
+ signed?: boolean
76
+ /** Size of the bytes. */
77
+ size?: number
29
78
  }
30
79
 
31
80
  /**
32
- * @description Decodes a byte array into a bigint.
81
+ * Decodes a byte array into a bigint.
82
+ *
83
+ * - Docs: https://viem.sh/docs/utilities/fromBytes.html#bytestobigint
84
+ *
85
+ * @param bytes Byte array to decode.
86
+ * @param opts Options.
87
+ * @returns BigInt value.
88
+ *
89
+ * @example
90
+ * import { bytesToBigint } from 'viem'
91
+ * const data = bytesToBigint(new Uint8Array([1, 164]))
92
+ * // 420n
33
93
  */
34
- export function bytesToBigint(bytes: ByteArray): bigint {
35
- const hex = bytesToHex(bytes)
94
+ export function bytesToBigint(
95
+ bytes: ByteArray,
96
+ opts: BytesToBigIntOpts = {},
97
+ ): bigint {
98
+ if (typeof opts.size !== 'undefined') assertSize(bytes, { size: opts.size })
99
+ const hex = bytesToHex(bytes, opts)
36
100
  return hexToBigInt(hex)
37
101
  }
38
102
 
103
+ export type BytesToBoolOpts = {
104
+ /** Size of the bytes. */
105
+ size?: number
106
+ }
107
+
39
108
  /**
40
- * @description Decodes a byte array into a boolean.
109
+ * Decodes a byte array into a boolean.
110
+ *
111
+ * - Docs: https://viem.sh/docs/utilities/fromBytes.html#bytestobool
112
+ *
113
+ * @param bytes Byte array to decode.
114
+ * @param opts Options.
115
+ * @returns Boolean value.
116
+ *
117
+ * @example
118
+ * import { bytesToBool } from 'viem'
119
+ * const data = bytesToBool(new Uint8Array([1]))
120
+ * // true
41
121
  */
42
- export function bytesToBool(bytes: ByteArray): boolean {
122
+ export function bytesToBool(
123
+ bytes_: ByteArray,
124
+ opts: BytesToBoolOpts = {},
125
+ ): boolean {
126
+ let bytes = bytes_
127
+ if (typeof opts.size !== 'undefined') {
128
+ assertSize(bytes, { size: opts.size })
129
+ bytes = trim(bytes)
130
+ }
43
131
  if (bytes.length > 1 || bytes[0] > 1)
44
132
  throw new InvalidBytesBooleanError(bytes)
45
133
  return Boolean(bytes[0])
@@ -47,17 +135,58 @@ export function bytesToBool(bytes: ByteArray): boolean {
47
135
 
48
136
  export { bytesToHex }
49
137
 
138
+ export type BytesToNumberOpts = BytesToBigIntOpts
139
+
50
140
  /**
51
- * @description Decodes a byte array into a number.
141
+ * Decodes a byte array into a number.
142
+ *
143
+ * - Docs: https://viem.sh/docs/utilities/fromBytes.html#bytestonumber
144
+ *
145
+ * @param bytes Byte array to decode.
146
+ * @param opts Options.
147
+ * @returns Number value.
148
+ *
149
+ * @example
150
+ * import { bytesToNumber } from 'viem'
151
+ * const data = bytesToNumber(new Uint8Array([1, 164]))
152
+ * // 420
52
153
  */
53
- export function bytesToNumber(bytes: ByteArray): number {
54
- const hex = bytesToHex(bytes)
154
+ export function bytesToNumber(
155
+ bytes: ByteArray,
156
+ opts: BytesToNumberOpts = {},
157
+ ): number {
158
+ if (typeof opts.size !== 'undefined') assertSize(bytes, { size: opts.size })
159
+ const hex = bytesToHex(bytes, opts)
55
160
  return hexToNumber(hex)
56
161
  }
57
162
 
163
+ export type BytesToStringOpts = {
164
+ /** Size of the bytes. */
165
+ size?: number
166
+ }
167
+
58
168
  /**
59
- * @description Decodes a byte array into a UTF-8 string.
169
+ * Decodes a byte array into a UTF-8 string.
170
+ *
171
+ * - Docs: https://viem.sh/docs/utilities/fromBytes.html#bytestostring
172
+ *
173
+ * @param bytes Byte array to decode.
174
+ * @param opts Options.
175
+ * @returns String value.
176
+ *
177
+ * @example
178
+ * import { bytesToString } from 'viem'
179
+ * const data = bytesToString(new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]))
180
+ * // 'Hello world'
60
181
  */
61
- export function bytesToString(bytes: ByteArray): string {
182
+ export function bytesToString(
183
+ bytes_: ByteArray,
184
+ opts: BytesToStringOpts = {},
185
+ ): string {
186
+ let bytes = bytes_
187
+ if (typeof opts.size !== 'undefined') {
188
+ assertSize(bytes, { size: opts.size })
189
+ bytes = trim(bytes, { dir: 'right' })
190
+ }
62
191
  return new TextDecoder().decode(bytes)
63
192
  }
@@ -1,9 +1,35 @@
1
- import { InvalidHexBooleanError } from '../../errors/index.js'
1
+ import {
2
+ InvalidHexBooleanError,
3
+ SizeOverflowError,
4
+ } from '../../errors/index.js'
2
5
  import type { ByteArray, Hex } from '../../types/index.js'
6
+ import { size as size_ } from '../data/size.js'
3
7
  import { trim } from '../data/index.js'
4
8
  import { hexToBytes } from './toBytes.js'
5
9
 
6
- type FromHexReturnType<TTo> = TTo extends 'string'
10
+ export function assertSize(
11
+ hexOrBytes: Hex | ByteArray,
12
+ { size }: { size: number },
13
+ ): void {
14
+ if (size_(hexOrBytes) > size)
15
+ throw new SizeOverflowError({
16
+ givenSize: size_(hexOrBytes),
17
+ maxSize: size,
18
+ })
19
+ }
20
+
21
+ export type FromHexParameters<
22
+ TTo extends 'string' | 'bigint' | 'number' | 'bytes' | 'boolean',
23
+ > =
24
+ | TTo
25
+ | {
26
+ /** Size (in bytes) of the hex value. */
27
+ size?: number
28
+ /** Type to convert to. */
29
+ to: TTo
30
+ }
31
+
32
+ export type FromHexReturnType<TTo> = TTo extends 'string'
7
33
  ? string
8
34
  : TTo extends 'bigint'
9
35
  ? bigint
@@ -16,29 +42,77 @@ type FromHexReturnType<TTo> = TTo extends 'string'
16
42
  : never
17
43
 
18
44
  /**
19
- * @description Decodes a hex string into a string, number, bigint, boolean, or bytes32 array.
45
+ * Decodes a hex string into a string, number, bigint, boolean, or byte array.
46
+ *
47
+ * - Docs: https://viem.sh/docs/utilities/fromHex.html
48
+ * - Example: https://viem.sh/docs/utilities/fromHex.html#usage
49
+ *
50
+ * @param hex Hex string to decode.
51
+ * @param toOrOpts Type to convert to or options.
52
+ * @returns Decoded value.
53
+ *
54
+ * @example
55
+ * import { fromHex } from 'viem'
56
+ * const data = fromHex('0x1a4', 'number')
57
+ * // 420
58
+ *
59
+ * @example
60
+ * import { fromHex } from 'viem'
61
+ * const data = fromHex('0x48656c6c6f20576f726c6421', 'string')
62
+ * // 'Hello world'
63
+ *
64
+ * @example
65
+ * import { fromHex } from 'viem'
66
+ * const data = fromHex('0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', {
67
+ * size: 32,
68
+ * to: 'string'
69
+ * })
70
+ * // 'Hello world'
20
71
  */
21
72
  export function fromHex<
22
73
  TTo extends 'string' | 'bigint' | 'number' | 'bytes' | 'boolean',
23
- >(hex: Hex, to: TTo): FromHexReturnType<TTo> {
24
- if (to === 'number') return hexToNumber(hex) as FromHexReturnType<TTo>
25
- if (to === 'bigint') return hexToBigInt(hex) as FromHexReturnType<TTo>
26
- if (to === 'string') return hexToString(hex) as FromHexReturnType<TTo>
27
- if (to === 'boolean') return hexToBool(hex) as FromHexReturnType<TTo>
28
- return hexToBytes(hex) as FromHexReturnType<TTo>
74
+ >(hex: Hex, toOrOpts: FromHexParameters<TTo>): FromHexReturnType<TTo> {
75
+ const opts = typeof toOrOpts === 'string' ? { to: toOrOpts } : toOrOpts
76
+ const to = opts.to
77
+
78
+ if (to === 'number') return hexToNumber(hex, opts) as FromHexReturnType<TTo>
79
+ if (to === 'bigint') return hexToBigInt(hex, opts) as FromHexReturnType<TTo>
80
+ if (to === 'string') return hexToString(hex, opts) as FromHexReturnType<TTo>
81
+ if (to === 'boolean') return hexToBool(hex, opts) as FromHexReturnType<TTo>
82
+ return hexToBytes(hex, opts) as FromHexReturnType<TTo>
29
83
  }
30
84
 
31
85
  export type HexToBigIntOpts = {
32
- // Whether or not the number of a signed representation.
86
+ /** Whether or not the number of a signed representation. */
33
87
  signed?: boolean
88
+ /** Size (in bytes) of the hex value. */
89
+ size?: number
34
90
  }
35
91
 
36
92
  /**
37
- * @description Decodes a hex string into a bigint.
93
+ * Decodes a hex value into a bigint.
94
+ *
95
+ * - Docs: https://viem.sh/docs/utilities/fromHex.html#hextobigint
96
+ *
97
+ * @param hex Hex value to decode.
98
+ * @param opts Options.
99
+ * @returns BigInt value.
100
+ *
101
+ * @example
102
+ * import { hexToBigInt } from 'viem'
103
+ * const data = hexToBigInt('0x1a4', { signed: true })
104
+ * // 420n
105
+ *
106
+ * @example
107
+ * import { hexToBigInt } from 'viem'
108
+ * const data = hexToBigInt('0x00000000000000000000000000000000000000000000000000000000000001a4', { size: 32 })
109
+ * // 420n
38
110
  */
39
111
  export function hexToBigInt(hex: Hex, opts: HexToBigIntOpts = {}): bigint {
40
112
  const { signed } = opts
41
113
 
114
+ if (opts.size) assertSize(hex, { size: opts.size })
115
+
42
116
  const value = BigInt(hex)
43
117
  if (!signed) return value
44
118
 
@@ -49,28 +123,97 @@ export function hexToBigInt(hex: Hex, opts: HexToBigIntOpts = {}): bigint {
49
123
  return value - BigInt(`0x${'f'.padStart(size * 2, 'f')}`) - 1n
50
124
  }
51
125
 
126
+ export type HexToBoolOpts = {
127
+ /** Size (in bytes) of the hex value. */
128
+ size?: number
129
+ }
130
+
52
131
  /**
53
- * @description Decodes a hex string into a boolean.
132
+ * Decodes a hex value into a boolean.
133
+ *
134
+ * - Docs: https://viem.sh/docs/utilities/fromHex.html#hextobool
135
+ *
136
+ * @param hex Hex value to decode.
137
+ * @param opts Options.
138
+ * @returns Boolean value.
139
+ *
140
+ * @example
141
+ * import { hexToBool } from 'viem'
142
+ * const data = hexToBool('0x1')
143
+ * // true
144
+ *
145
+ * @example
146
+ * import { hexToBool } from 'viem'
147
+ * const data = hexToBool('0x0000000000000000000000000000000000000000000000000000000000000001', { size: 32 })
148
+ * // true
54
149
  */
55
- export function hexToBool(hex: Hex): boolean {
150
+ export function hexToBool(hex_: Hex, opts: HexToBoolOpts = {}): boolean {
151
+ let hex = hex_
152
+ if (opts.size) {
153
+ assertSize(hex, { size: opts.size })
154
+ hex = trim(hex)
155
+ }
56
156
  if (trim(hex) === '0x0') return false
57
157
  if (trim(hex) === '0x1') return true
58
158
  throw new InvalidHexBooleanError(hex)
59
159
  }
60
160
 
61
- type NumberToHexOpts = HexToBigIntOpts
161
+ export type HexToNumberOpts = HexToBigIntOpts
62
162
 
63
163
  /**
64
- * @description Decodes a hex string into a number.
164
+ * Decodes a hex string into a number.
165
+ *
166
+ * - Docs: https://viem.sh/docs/utilities/fromHex.html#hextonumber
167
+ *
168
+ * @param hex Hex value to decode.
169
+ * @param opts Options.
170
+ * @returns Number value.
171
+ *
172
+ * @example
173
+ * import { hexToNumber } from 'viem'
174
+ * const data = hexToNumber('0x1a4')
175
+ * // 420
176
+ *
177
+ * @example
178
+ * import { hexToNumber } from 'viem'
179
+ * const data = hexToBigInt('0x00000000000000000000000000000000000000000000000000000000000001a4', { size: 32 })
180
+ * // 420
65
181
  */
66
- export function hexToNumber(hex: Hex, opts: NumberToHexOpts = {}): number {
182
+ export function hexToNumber(hex: Hex, opts: HexToNumberOpts = {}): number {
67
183
  return Number(hexToBigInt(hex, opts))
68
184
  }
69
185
 
186
+ export type HexToStringOpts = {
187
+ /** Size (in bytes) of the hex value. */
188
+ size?: number
189
+ }
190
+
70
191
  /**
71
- * @description Decodes a hex string into a UTF-8 string.
192
+ * Decodes a hex value into a UTF-8 string.
193
+ *
194
+ * - Docs: https://viem.sh/docs/utilities/fromHex.html#hextostring
195
+ *
196
+ * @param hex Hex value to decode.
197
+ * @param opts Options.
198
+ * @returns String value.
199
+ *
200
+ * @example
201
+ * import { hexToString } from 'viem'
202
+ * const data = hexToString('0x48656c6c6f20576f726c6421')
203
+ * // 'Hello world!'
204
+ *
205
+ * @example
206
+ * import { hexToString } from 'viem'
207
+ * const data = hexToString('0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', {
208
+ * size: 32,
209
+ * })
210
+ * // 'Hello world'
72
211
  */
73
- export function hexToString(hex: Hex): string {
74
- const bytes = hexToBytes(hex)
212
+ export function hexToString(hex: Hex, opts: HexToStringOpts = {}): string {
213
+ let bytes = hexToBytes(hex)
214
+ if (opts.size) {
215
+ assertSize(bytes, { size: opts.size })
216
+ bytes = trim(bytes, { dir: 'right' })
217
+ }
75
218
  return new TextDecoder().decode(bytes)
76
219
  }