ox 0.14.67 → 0.14.69

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 (264) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/_cjs/core/AbiEvent.js +45 -1
  3. package/_cjs/core/AbiEvent.js.map +1 -1
  4. package/_cjs/core/AbiParameters.js +6 -2
  5. package/_cjs/core/AbiParameters.js.map +1 -1
  6. package/_cjs/core/Address.js +24 -21
  7. package/_cjs/core/Address.js.map +1 -1
  8. package/_cjs/core/Authorization.js +2 -0
  9. package/_cjs/core/Authorization.js.map +1 -1
  10. package/_cjs/core/Base58.js +14 -4
  11. package/_cjs/core/Base58.js.map +1 -1
  12. package/_cjs/core/BinaryStateTree.js +1 -1
  13. package/_cjs/core/BinaryStateTree.js.map +1 -1
  14. package/_cjs/core/Blobs.js +26 -34
  15. package/_cjs/core/Blobs.js.map +1 -1
  16. package/_cjs/core/Block.js +1 -0
  17. package/_cjs/core/Block.js.map +1 -1
  18. package/_cjs/core/BlockOverrides.js +1 -1
  19. package/_cjs/core/BlockOverrides.js.map +1 -1
  20. package/_cjs/core/Bytes.js +56 -13
  21. package/_cjs/core/Bytes.js.map +1 -1
  22. package/_cjs/core/Cbor.js +33 -18
  23. package/_cjs/core/Cbor.js.map +1 -1
  24. package/_cjs/core/FrameReceipt.js +6 -2
  25. package/_cjs/core/FrameReceipt.js.map +1 -1
  26. package/_cjs/core/Hex.js +54 -21
  27. package/_cjs/core/Hex.js.map +1 -1
  28. package/_cjs/core/Json.js +15 -2
  29. package/_cjs/core/Json.js.map +1 -1
  30. package/_cjs/core/Rlp.js +138 -94
  31. package/_cjs/core/Rlp.js.map +1 -1
  32. package/_cjs/core/RpcTransport.js +3 -1
  33. package/_cjs/core/RpcTransport.js.map +1 -1
  34. package/_cjs/core/Siwe.js +35 -9
  35. package/_cjs/core/Siwe.js.map +1 -1
  36. package/_cjs/core/Transaction.js +10 -4
  37. package/_cjs/core/Transaction.js.map +1 -1
  38. package/_cjs/core/TransactionReceipt.js +12 -4
  39. package/_cjs/core/TransactionReceipt.js.map +1 -1
  40. package/_cjs/core/TransactionRequest.js +6 -2
  41. package/_cjs/core/TransactionRequest.js.map +1 -1
  42. package/_cjs/core/TxEnvelopeEip4844.js +1 -1
  43. package/_cjs/core/TxEnvelopeEip4844.js.map +1 -1
  44. package/_cjs/core/TypedData.js +19 -8
  45. package/_cjs/core/TypedData.js.map +1 -1
  46. package/_cjs/core/internal/abiParameters.js +59 -18
  47. package/_cjs/core/internal/abiParameters.js.map +1 -1
  48. package/_cjs/core/internal/address.js +32 -0
  49. package/_cjs/core/internal/address.js.map +1 -0
  50. package/_cjs/core/internal/base58.js +11 -9
  51. package/_cjs/core/internal/base58.js.map +1 -1
  52. package/_cjs/core/internal/bytes.js +13 -22
  53. package/_cjs/core/internal/bytes.js.map +1 -1
  54. package/_cjs/core/internal/caches.js +1 -0
  55. package/_cjs/core/internal/caches.js.map +1 -1
  56. package/_cjs/core/internal/cursor.js +32 -10
  57. package/_cjs/core/internal/cursor.js.map +1 -1
  58. package/_cjs/core/internal/hex.js +3 -3
  59. package/_cjs/core/internal/hex.js.map +1 -1
  60. package/_cjs/core/internal/lru.js +6 -5
  61. package/_cjs/core/internal/lru.js.map +1 -1
  62. package/_cjs/core/internal/promise.js +3 -3
  63. package/_cjs/core/internal/promise.js.map +1 -1
  64. package/_cjs/erc6492/SignatureErc6492.js +1 -1
  65. package/_cjs/erc6492/SignatureErc6492.js.map +1 -1
  66. package/_cjs/tempo/Transaction.js +13 -7
  67. package/_cjs/tempo/Transaction.js.map +1 -1
  68. package/_cjs/tempo/TransactionRequest.js +23 -19
  69. package/_cjs/tempo/TransactionRequest.js.map +1 -1
  70. package/_cjs/tempo/TxEnvelopeTempo.js +23 -4
  71. package/_cjs/tempo/TxEnvelopeTempo.js.map +1 -1
  72. package/_cjs/tempo/ZoneId.js +3 -0
  73. package/_cjs/tempo/ZoneId.js.map +1 -1
  74. package/_cjs/tempo/internal/virtualMasterPool.js +12 -3
  75. package/_cjs/tempo/internal/virtualMasterPool.js.map +1 -1
  76. package/_cjs/version.js +1 -1
  77. package/_esm/core/AbiError.js +2 -2
  78. package/_esm/core/AbiEvent.js +71 -2
  79. package/_esm/core/AbiEvent.js.map +1 -1
  80. package/_esm/core/AbiParameters.js +8 -2
  81. package/_esm/core/AbiParameters.js.map +1 -1
  82. package/_esm/core/Address.js +27 -21
  83. package/_esm/core/Address.js.map +1 -1
  84. package/_esm/core/Authorization.js +3 -0
  85. package/_esm/core/Authorization.js.map +1 -1
  86. package/_esm/core/Base58.js +16 -4
  87. package/_esm/core/Base58.js.map +1 -1
  88. package/_esm/core/BinaryStateTree.js +3 -1
  89. package/_esm/core/BinaryStateTree.js.map +1 -1
  90. package/_esm/core/Blobs.js +40 -41
  91. package/_esm/core/Blobs.js.map +1 -1
  92. package/_esm/core/Block.js +1 -0
  93. package/_esm/core/Block.js.map +1 -1
  94. package/_esm/core/BlockOverrides.js +3 -2
  95. package/_esm/core/BlockOverrides.js.map +1 -1
  96. package/_esm/core/Bytes.js +66 -13
  97. package/_esm/core/Bytes.js.map +1 -1
  98. package/_esm/core/Cbor.js +33 -17
  99. package/_esm/core/Cbor.js.map +1 -1
  100. package/_esm/core/Ens.js +1 -1
  101. package/_esm/core/FrameReceipt.js +6 -2
  102. package/_esm/core/FrameReceipt.js.map +1 -1
  103. package/_esm/core/Hash.js +1 -1
  104. package/_esm/core/Hex.js +70 -21
  105. package/_esm/core/Hex.js.map +1 -1
  106. package/_esm/core/Json.js +21 -2
  107. package/_esm/core/Json.js.map +1 -1
  108. package/_esm/core/Rlp.js +146 -97
  109. package/_esm/core/Rlp.js.map +1 -1
  110. package/_esm/core/RpcTransport.js +4 -1
  111. package/_esm/core/RpcTransport.js.map +1 -1
  112. package/_esm/core/Siwe.js +40 -10
  113. package/_esm/core/Siwe.js.map +1 -1
  114. package/_esm/core/Transaction.js +11 -4
  115. package/_esm/core/Transaction.js.map +1 -1
  116. package/_esm/core/TransactionReceipt.js +12 -4
  117. package/_esm/core/TransactionReceipt.js.map +1 -1
  118. package/_esm/core/TransactionRequest.js +6 -2
  119. package/_esm/core/TransactionRequest.js.map +1 -1
  120. package/_esm/core/TxEnvelopeEip4844.js +1 -1
  121. package/_esm/core/TxEnvelopeEip4844.js.map +1 -1
  122. package/_esm/core/TypedData.js +26 -10
  123. package/_esm/core/TypedData.js.map +1 -1
  124. package/_esm/core/internal/abiParameters.js +78 -18
  125. package/_esm/core/internal/abiParameters.js.map +1 -1
  126. package/_esm/core/internal/address.js +41 -0
  127. package/_esm/core/internal/address.js.map +1 -0
  128. package/_esm/core/internal/base58.js +15 -9
  129. package/_esm/core/internal/base58.js.map +1 -1
  130. package/_esm/core/internal/bytes.js +14 -22
  131. package/_esm/core/internal/bytes.js.map +1 -1
  132. package/_esm/core/internal/caches.js +1 -0
  133. package/_esm/core/internal/caches.js.map +1 -1
  134. package/_esm/core/internal/cursor.js +37 -10
  135. package/_esm/core/internal/cursor.js.map +1 -1
  136. package/_esm/core/internal/hex.js +5 -3
  137. package/_esm/core/internal/hex.js.map +1 -1
  138. package/_esm/core/internal/lru.js +7 -5
  139. package/_esm/core/internal/lru.js.map +1 -1
  140. package/_esm/core/internal/promise.js +4 -3
  141. package/_esm/core/internal/promise.js.map +1 -1
  142. package/_esm/erc6492/SignatureErc6492.js +1 -1
  143. package/_esm/erc6492/SignatureErc6492.js.map +1 -1
  144. package/_esm/tempo/Transaction.js +15 -7
  145. package/_esm/tempo/Transaction.js.map +1 -1
  146. package/_esm/tempo/TransactionRequest.js +25 -19
  147. package/_esm/tempo/TransactionRequest.js.map +1 -1
  148. package/_esm/tempo/TxEnvelopeTempo.js +30 -4
  149. package/_esm/tempo/TxEnvelopeTempo.js.map +1 -1
  150. package/_esm/tempo/ZoneId.js +7 -1
  151. package/_esm/tempo/ZoneId.js.map +1 -1
  152. package/_esm/tempo/internal/virtualMasterPool.js +16 -3
  153. package/_esm/tempo/internal/virtualMasterPool.js.map +1 -1
  154. package/_esm/version.js +1 -1
  155. package/_types/core/AbiError.d.ts +3 -3
  156. package/_types/core/AbiEvent.d.ts +38 -3
  157. package/_types/core/AbiEvent.d.ts.map +1 -1
  158. package/_types/core/AbiParameters.d.ts +1 -1
  159. package/_types/core/AbiParameters.d.ts.map +1 -1
  160. package/_types/core/Address.d.ts.map +1 -1
  161. package/_types/core/Authorization.d.ts.map +1 -1
  162. package/_types/core/Base58.d.ts.map +1 -1
  163. package/_types/core/Blobs.d.ts +4 -5
  164. package/_types/core/Blobs.d.ts.map +1 -1
  165. package/_types/core/Block.d.ts +2 -0
  166. package/_types/core/Block.d.ts.map +1 -1
  167. package/_types/core/BlockOverrides.d.ts +2 -1
  168. package/_types/core/BlockOverrides.d.ts.map +1 -1
  169. package/_types/core/Bytes.d.ts +2 -0
  170. package/_types/core/Bytes.d.ts.map +1 -1
  171. package/_types/core/Cbor.d.ts +13 -6
  172. package/_types/core/Cbor.d.ts.map +1 -1
  173. package/_types/core/Ens.d.ts +1 -1
  174. package/_types/core/FrameReceipt.d.ts.map +1 -1
  175. package/_types/core/Hash.d.ts +1 -1
  176. package/_types/core/Hex.d.ts +3 -1
  177. package/_types/core/Hex.d.ts.map +1 -1
  178. package/_types/core/Json.d.ts.map +1 -1
  179. package/_types/core/Rlp.d.ts +38 -9
  180. package/_types/core/Rlp.d.ts.map +1 -1
  181. package/_types/core/RpcTransport.d.ts.map +1 -1
  182. package/_types/core/Siwe.d.ts +1 -0
  183. package/_types/core/Siwe.d.ts.map +1 -1
  184. package/_types/core/Transaction.d.ts.map +1 -1
  185. package/_types/core/TransactionReceipt.d.ts.map +1 -1
  186. package/_types/core/TransactionRequest.d.ts.map +1 -1
  187. package/_types/core/TypedData.d.ts +2 -2
  188. package/_types/core/TypedData.d.ts.map +1 -1
  189. package/_types/core/internal/abiParameters.d.ts +5 -5
  190. package/_types/core/internal/abiParameters.d.ts.map +1 -1
  191. package/_types/core/internal/address.d.ts +14 -0
  192. package/_types/core/internal/address.d.ts.map +1 -0
  193. package/_types/core/internal/base58.d.ts.map +1 -1
  194. package/_types/core/internal/bytes.d.ts +1 -10
  195. package/_types/core/internal/bytes.d.ts.map +1 -1
  196. package/_types/core/internal/caches.d.ts +1 -0
  197. package/_types/core/internal/caches.d.ts.map +1 -1
  198. package/_types/core/internal/cursor.d.ts +7 -2
  199. package/_types/core/internal/cursor.d.ts.map +1 -1
  200. package/_types/core/internal/hex.d.ts.map +1 -1
  201. package/_types/core/internal/lru.d.ts.map +1 -1
  202. package/_types/core/internal/promise.d.ts.map +1 -1
  203. package/_types/erc6492/SignatureErc6492.d.ts +1 -1
  204. package/_types/erc6492/SignatureErc6492.d.ts.map +1 -1
  205. package/_types/tempo/Transaction.d.ts.map +1 -1
  206. package/_types/tempo/TransactionRequest.d.ts.map +1 -1
  207. package/_types/tempo/TxEnvelopeTempo.d.ts.map +1 -1
  208. package/_types/tempo/ZoneId.d.ts +3 -1
  209. package/_types/tempo/ZoneId.d.ts.map +1 -1
  210. package/_types/tempo/internal/virtualMasterPool.d.ts.map +1 -1
  211. package/_types/version.d.ts +1 -1
  212. package/core/AbiError.ts +3 -3
  213. package/core/AbiEvent.ts +74 -2
  214. package/core/AbiParameters.ts +8 -1
  215. package/core/Address.ts +25 -20
  216. package/core/Authorization.ts +3 -0
  217. package/core/Base58.ts +15 -4
  218. package/core/BinaryStateTree.ts +4 -1
  219. package/core/Blobs.ts +49 -53
  220. package/core/Block.ts +3 -0
  221. package/core/BlockOverrides.ts +3 -2
  222. package/core/Bytes.ts +68 -17
  223. package/core/Cbor.ts +33 -13
  224. package/core/Ens.ts +1 -1
  225. package/core/FrameReceipt.ts +6 -2
  226. package/core/Hash.ts +1 -1
  227. package/core/Hex.ts +79 -22
  228. package/core/Json.ts +23 -2
  229. package/core/Rlp.ts +184 -94
  230. package/core/RpcTransport.ts +4 -1
  231. package/core/Siwe.ts +41 -10
  232. package/core/Transaction.ts +11 -4
  233. package/core/TransactionReceipt.ts +13 -4
  234. package/core/TransactionRequest.ts +10 -6
  235. package/core/TxEnvelopeEip4844.ts +1 -1
  236. package/core/TypedData.ts +28 -10
  237. package/core/internal/abiParameters.ts +82 -23
  238. package/core/internal/address.ts +38 -0
  239. package/core/internal/base58.ts +14 -9
  240. package/core/internal/bytes.ts +13 -23
  241. package/core/internal/caches.ts +1 -0
  242. package/core/internal/cursor.ts +44 -16
  243. package/core/internal/hex.ts +5 -3
  244. package/core/internal/lru.ts +6 -7
  245. package/core/internal/promise.ts +5 -3
  246. package/erc6492/SignatureErc6492.ts +1 -1
  247. package/package.json +1 -1
  248. package/tempo/AccountSimulation.test.ts +1 -1
  249. package/tempo/SignaturePreAuthorized.test.ts +1 -1
  250. package/tempo/Transaction.ts +15 -9
  251. package/tempo/TransactionRequest.ts +31 -24
  252. package/tempo/TxEnvelopeTempo.test.ts +52 -0
  253. package/tempo/TxEnvelopeTempo.ts +32 -6
  254. package/tempo/ZoneId.test.ts +11 -0
  255. package/tempo/ZoneId.ts +7 -1
  256. package/tempo/internal/virtualMasterPool.ts +18 -4
  257. package/version.ts +1 -1
  258. package/_cjs/core/internal/uid.js +0 -17
  259. package/_cjs/core/internal/uid.js.map +0 -1
  260. package/_esm/core/internal/uid.js +0 -15
  261. package/_esm/core/internal/uid.js.map +0 -1
  262. package/_types/core/internal/uid.d.ts +0 -3
  263. package/_types/core/internal/uid.d.ts.map +0 -1
  264. package/core/internal/uid.ts +0 -15
package/core/Bytes.ts CHANGED
@@ -211,29 +211,44 @@ export declare namespace fromBoolean {
211
211
  */
212
212
  export function fromHex(value: Hex.Hex, options: fromHex.Options = {}): Bytes {
213
213
  const { size } = options
214
-
215
- let hexString = value.slice(2)
216
- if (hexString.length % 2) hexString = `0${hexString}`
217
- if (size) {
218
- internal_hex.assertSize(value, size)
219
- hexString = hexString.padEnd(size * 2, '0')
214
+ if (size) internal_hex.assertSize(value, size)
215
+
216
+ // Read nibbles straight from `value`, without slicing or padding a copy of
217
+ // it. An odd number of nibbles reads as if it had a leading zero.
218
+ const odd = value.length % 2
219
+ const length = Math.max(0, value.length - 2 + odd) / 2
220
+ const bytes = new Uint8Array(Math.max(size || 0, length))
221
+ let index = 0
222
+ let j = 2
223
+ if (odd && length) {
224
+ const nibble = internal.charCodeToBase16(value.charCodeAt(j++))
225
+ if (nibble < 0) throw invalidByteSequence(value, size, index)
226
+ bytes[index++] = nibble
220
227
  }
221
-
222
- const length = hexString.length / 2
223
- const bytes = new Uint8Array(length)
224
- for (let index = 0, j = 0; index < length; index++) {
225
- const nibbleLeft = internal.charCodeToBase16(hexString.charCodeAt(j++))
226
- const nibbleRight = internal.charCodeToBase16(hexString.charCodeAt(j++))
227
- if (nibbleLeft === undefined || nibbleRight === undefined) {
228
- throw new Errors.BaseError(
229
- `Invalid byte sequence ("${hexString[j - 2]}${hexString[j - 1]}" in "${hexString}").`,
230
- )
231
- }
228
+ for (; index < length; index++) {
229
+ const nibbleLeft = internal.charCodeToBase16(value.charCodeAt(j++))
230
+ const nibbleRight = internal.charCodeToBase16(value.charCodeAt(j++))
231
+ if ((nibbleLeft | nibbleRight) < 0)
232
+ throw invalidByteSequence(value, size, index)
232
233
  bytes[index] = (nibbleLeft << 4) | nibbleRight
233
234
  }
234
235
  return bytes
235
236
  }
236
237
 
238
+ // Builds the error for an invalid hex digit in the byte at `index`.
239
+ function invalidByteSequence(
240
+ value: Hex.Hex,
241
+ size: number | undefined,
242
+ index: number,
243
+ ) {
244
+ let hexString = value.slice(2)
245
+ if (hexString.length % 2) hexString = `0${hexString}`
246
+ if (size) hexString = hexString.padEnd(size * 2, '0')
247
+ return new Errors.BaseError(
248
+ `Invalid byte sequence ("${hexString.slice(index * 2, index * 2 + 2)}" in "${hexString}").`,
249
+ )
250
+ }
251
+
237
252
  export declare namespace fromHex {
238
253
  type Options = {
239
254
  /** Size of the output bytes. */
@@ -523,6 +538,27 @@ export declare namespace slice {
523
538
  export function toBigInt(bytes: Bytes, options: toBigInt.Options = {}): bigint {
524
539
  const { size } = options
525
540
  if (typeof size !== 'undefined') internal.assertSize(bytes, size)
541
+ // An unsigned value of up to 6 significant bytes (most ABI words hold small
542
+ // values) is built as a number and converted once, skipping the hex string.
543
+ if (!options.signed && typeof size === 'undefined' && bytes.length > 0) {
544
+ let i = 0
545
+ while (i < bytes.length && bytes[i] === 0) i++
546
+ if (bytes.length - i <= 6) {
547
+ let value = 0
548
+ for (; i < bytes.length; i++) value = value * 256 + bytes[i]!
549
+ return BigInt(value)
550
+ }
551
+ // A full word reads as four 64-bit halves, skipping the hex string.
552
+ if (bytes.length === 32 && bytes instanceof Uint8Array) {
553
+ const view = new DataView(bytes.buffer, bytes.byteOffset, 32)
554
+ return (
555
+ (view.getBigUint64(0) << 192n) |
556
+ (view.getBigUint64(8) << 128n) |
557
+ (view.getBigUint64(16) << 64n) |
558
+ view.getBigUint64(24)
559
+ )
560
+ }
561
+ }
526
562
  const hex = Hex.fromBytes(bytes, options)
527
563
  return Hex.toBigInt(hex, options)
528
564
  }
@@ -614,6 +650,8 @@ export declare namespace toHex {
614
650
  /**
615
651
  * Decodes a {@link ox#Bytes.Bytes} into a number.
616
652
  *
653
+ * Throws `Hex.IntegerOutOfRangeError` when the value is not a safe integer. Use `toBigInt` for larger values.
654
+ *
617
655
  * @example
618
656
  * ```ts twoslash
619
657
  * import { Bytes } from 'ox'
@@ -625,6 +663,17 @@ export declare namespace toHex {
625
663
  export function toNumber(bytes: Bytes, options: toNumber.Options = {}): number {
626
664
  const { size } = options
627
665
  if (typeof size !== 'undefined') internal.assertSize(bytes, size)
666
+ if (!options.signed && size === undefined && bytes.length > 0) {
667
+ const length = bytes.length
668
+ let i = 0
669
+ while (i < length && bytes[i] === 0) i++
670
+ // Up to 6 bytes (48 bits) accumulate exactly as a number.
671
+ if (length - i <= 6) {
672
+ let value = 0
673
+ for (; i < length; i++) value = value * 256 + bytes[i]!
674
+ return value
675
+ }
676
+ }
628
677
  const hex = Hex.fromBytes(bytes, options)
629
678
  return Hex.toNumber(hex, options)
630
679
  }
@@ -743,6 +792,8 @@ export declare namespace trimRight {
743
792
  * @returns `true` if the value is {@link ox#Bytes.Bytes}, otherwise `false`.
744
793
  */
745
794
  export function validate(value: unknown): value is Bytes {
795
+ // Reject non-objects up front, without constructing and catching an error.
796
+ if (!value || typeof value !== 'object') return false
746
797
  try {
747
798
  assert(value)
748
799
  return true
package/core/Cbor.ts CHANGED
@@ -3,6 +3,9 @@ import * as Errors from './Errors.js'
3
3
  import * as Hex from './Hex.js'
4
4
  import * as Cursor from './internal/cursor.js'
5
5
 
6
+ /** Maximum nesting depth permitted when decoding a CBOR value. */
7
+ const depthLimit = 1_024
8
+
6
9
  /**
7
10
  * Encodes a value into CBOR (Concise Binary Object Representation) format.
8
11
  *
@@ -106,6 +109,15 @@ export declare namespace decode {
106
109
  | Errors.GlobalErrorType
107
110
  }
108
111
 
112
+ /** Thrown when a CBOR value nests deeper than the decode depth limit. */
113
+ export class DepthLimitExceededError extends Errors.BaseError {
114
+ override readonly name = 'Cbor.DepthLimitExceededError'
115
+
116
+ constructor({ limit }: { limit: number }) {
117
+ super(`CBOR depth limit of \`${limit}\` exceeded.`)
118
+ }
119
+ }
120
+
109
121
  export class InvalidMajorTypeError extends Errors.BaseError {
110
122
  override readonly name = 'Cbor.InvalidMajorTypeError'
111
123
 
@@ -617,7 +629,10 @@ namespace getEncodable {
617
629
 
618
630
  /** @internal */
619
631
  // biome-ignore lint/correctness/noUnusedVariables: _
620
- function decodeCursor(cursor: Cursor.Cursor): unknown {
632
+ function decodeCursor(cursor: Cursor.Cursor, depth = 0): unknown {
633
+ if (depth >= depthLimit)
634
+ throw new DepthLimitExceededError({ limit: depthLimit })
635
+
621
636
  const initialByte = cursor.readUint8()
622
637
  const majorType = initialByte >> 5
623
638
  const additionalInfo = initialByte & 0b00011111
@@ -633,19 +648,19 @@ function decodeCursor(cursor: Cursor.Cursor): unknown {
633
648
 
634
649
  // Major type 2: Byte string
635
650
  case 2:
636
- return decodeCursor.readByteString(cursor, additionalInfo)
651
+ return decodeCursor.readByteString(cursor, additionalInfo, depth + 1)
637
652
 
638
653
  // Major type 3: Text string
639
654
  case 3:
640
- return decodeCursor.readTextString(cursor, additionalInfo)
655
+ return decodeCursor.readTextString(cursor, additionalInfo, depth + 1)
641
656
 
642
657
  // Major type 4: Array
643
658
  case 4:
644
- return decodeCursor.readArray(cursor, additionalInfo)
659
+ return decodeCursor.readArray(cursor, additionalInfo, depth + 1)
645
660
 
646
661
  // Major type 5: Map
647
662
  case 5:
648
- return decodeCursor.readMap(cursor, additionalInfo)
663
+ return decodeCursor.readMap(cursor, additionalInfo, depth + 1)
649
664
 
650
665
  // Major type 6: Tagged data (not yet supported)
651
666
  case 6:
@@ -663,6 +678,7 @@ function decodeCursor(cursor: Cursor.Cursor): unknown {
663
678
  /** @internal */
664
679
  namespace decodeCursor {
665
680
  export type ErrorType =
681
+ | DepthLimitExceededError
666
682
  | readUnsignedInteger.ErrorType
667
683
  | readNegativeInteger.ErrorType
668
684
  | readByteString.ErrorType
@@ -724,6 +740,7 @@ namespace decodeCursor {
724
740
  export function readByteString(
725
741
  cursor: Cursor.Cursor,
726
742
  additionalInfo: number,
743
+ depth = 0,
727
744
  ): Bytes.Bytes {
728
745
  // Indefinite-length byte string
729
746
  if (additionalInfo === 31) {
@@ -736,7 +753,7 @@ namespace decodeCursor {
736
753
  cursor.readUint8() // consume the break byte
737
754
  break
738
755
  }
739
- const chunk = decodeCursor(cursor) as Bytes.Bytes
756
+ const chunk = decodeCursor(cursor, depth) as Bytes.Bytes
740
757
  if (!(chunk instanceof Uint8Array))
741
758
  throw new InvalidIndefiniteLengthChunkError({ type: 'byte string' })
742
759
  chunks.push(chunk)
@@ -769,6 +786,7 @@ namespace decodeCursor {
769
786
  export function readTextString(
770
787
  cursor: Cursor.Cursor,
771
788
  additionalInfo: number,
789
+ depth = 0,
772
790
  ): string {
773
791
  // Indefinite-length text string
774
792
  if (additionalInfo === 31) {
@@ -780,7 +798,7 @@ namespace decodeCursor {
780
798
  cursor.readUint8() // consume the break byte
781
799
  break
782
800
  }
783
- const chunk = decodeCursor(cursor)
801
+ const chunk = decodeCursor(cursor, depth)
784
802
  if (typeof chunk !== 'string')
785
803
  throw new InvalidIndefiniteLengthChunkError({ type: 'text string' })
786
804
  chunks.push(chunk)
@@ -807,6 +825,7 @@ namespace decodeCursor {
807
825
  export function readArray(
808
826
  cursor: Cursor.Cursor,
809
827
  additionalInfo: number,
828
+ depth = 0,
810
829
  ): unknown[] {
811
830
  // Indefinite-length array
812
831
  if (additionalInfo === 31) {
@@ -818,7 +837,7 @@ namespace decodeCursor {
818
837
  cursor.readUint8() // consume the break byte
819
838
  break
820
839
  }
821
- result.push(decodeCursor(cursor))
840
+ result.push(decodeCursor(cursor, depth))
822
841
  }
823
842
 
824
843
  return result
@@ -828,7 +847,7 @@ namespace decodeCursor {
828
847
  const result: unknown[] = []
829
848
 
830
849
  for (let i = 0; i < length; i++) {
831
- result.push(decodeCursor(cursor))
850
+ result.push(decodeCursor(cursor, depth))
832
851
  }
833
852
 
834
853
  return result
@@ -843,6 +862,7 @@ namespace decodeCursor {
843
862
  export function readMap(
844
863
  cursor: Cursor.Cursor,
845
864
  additionalInfo: number,
865
+ depth = 0,
846
866
  ): Record<string, unknown> {
847
867
  // Indefinite-length map
848
868
  if (additionalInfo === 31) {
@@ -854,7 +874,7 @@ namespace decodeCursor {
854
874
  cursor.readUint8() // consume the break byte
855
875
  break
856
876
  }
857
- const key = decodeCursor(cursor)
877
+ const key = decodeCursor(cursor, depth)
858
878
  // Support both string and number keys (for COSE_Key with integer keys)
859
879
  const keyStr =
860
880
  typeof key === 'string'
@@ -862,7 +882,7 @@ namespace decodeCursor {
862
882
  : typeof key === 'number'
863
883
  ? String(key)
864
884
  : String(key)
865
- const value = decodeCursor(cursor)
885
+ const value = decodeCursor(cursor, depth)
866
886
  setEntry(result, keyStr, value)
867
887
  }
868
888
 
@@ -873,7 +893,7 @@ namespace decodeCursor {
873
893
  const result: Record<string, unknown> = {}
874
894
 
875
895
  for (let i = 0; i < length; i++) {
876
- const key = decodeCursor(cursor)
896
+ const key = decodeCursor(cursor, depth)
877
897
  // Support both string and number keys (for COSE_Key with integer keys)
878
898
  const keyStr =
879
899
  typeof key === 'string'
@@ -881,7 +901,7 @@ namespace decodeCursor {
881
901
  : typeof key === 'number'
882
902
  ? String(key)
883
903
  : String(key)
884
- const value = decodeCursor(cursor)
904
+ const value = decodeCursor(cursor, depth)
885
905
  setEntry(result, keyStr, value)
886
906
  }
887
907
 
package/core/Ens.ts CHANGED
@@ -46,7 +46,7 @@ export declare namespace labelhash {
46
46
  * ```ts twoslash
47
47
  * import { Ens } from 'ox'
48
48
  * Ens.namehash('wevm.eth')
49
- * // @log: '0xf246651c1b9a6b141d19c2604e9a58f567973833990f830d882534a747801359'
49
+ * // @log: '0x08c85f2f4059e930c45a6aeff9dcd3bd95dc3c5c1cddef6a0626b31152248560'
50
50
  * ```
51
51
  *
52
52
  * @param name - ENS name.
@@ -78,7 +78,9 @@ export function fromRpc(receipt: Rpc): FrameReceipt {
78
78
  gasUsed: Hex.toBigInt(receipt.gasUsed),
79
79
  logs: receipt.logs,
80
80
  stateGasUsed: Hex.toBigInt(receipt.stateGasUsed),
81
- status: fromRpcStatus[receipt.status],
81
+ status: Object.hasOwn(fromRpcStatus, receipt.status)
82
+ ? fromRpcStatus[receipt.status]
83
+ : (undefined as never),
82
84
  }
83
85
  }
84
86
 
@@ -114,7 +116,9 @@ export function toRpc(receipt: toRpc.Input): Rpc {
114
116
  gasUsed: Quantity.fromNumberish(receipt.gasUsed),
115
117
  logs: receipt.logs,
116
118
  stateGasUsed: Quantity.fromNumberish(receipt.stateGasUsed),
117
- status: toRpcStatus[receipt.status],
119
+ status: Object.hasOwn(toRpcStatus, receipt.status)
120
+ ? toRpcStatus[receipt.status]
121
+ : (undefined as never),
118
122
  }
119
123
  }
120
124
 
package/core/Hash.ts CHANGED
@@ -26,7 +26,7 @@ import * as Hex from './Hex.js'
26
26
  * import { Hash, Hex } from 'ox'
27
27
  *
28
28
  * Hash.keccak256(Hex.fromString('hello world'))
29
- * // @log: '0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0'
29
+ * // @log: '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'
30
30
  * ```
31
31
  *
32
32
  * @example
package/core/Hex.ts CHANGED
@@ -5,12 +5,17 @@ import * as internal_bytes from './internal/bytes.js'
5
5
  import * as internal from './internal/hex.js'
6
6
  import * as Json from './Json.js'
7
7
 
8
+ const decoder = /*#__PURE__*/ new TextDecoder()
8
9
  const encoder = /*#__PURE__*/ new TextEncoder()
9
10
 
10
11
  const hexes = /*#__PURE__*/ Array.from({ length: 256 }, (_v, i) =>
11
12
  i.toString(16).padStart(2, '0'),
12
13
  )
13
14
 
15
+ // Taken from the prototype once, and called on the value, so that an instance
16
+ // or subclass defining its own `toHex` can't change the result.
17
+ const nativeToHex = (Uint8Array.prototype as { toHex?: () => string }).toHex
18
+
14
19
  /** Root type for a Hex string. */
15
20
  export type Hex = `0x${string}`
16
21
 
@@ -80,7 +85,14 @@ export declare namespace assert {
80
85
  * @returns The concatenated {@link ox#Hex.Hex} value.
81
86
  */
82
87
  export function concat(...values: readonly Hex[]): Hex {
83
- return `0x${(values as Hex[]).reduce((acc, x) => acc + x.replace('0x', ''), '')}`
88
+ let hex = '0x'
89
+ for (let i = 0; i < values.length; i++) {
90
+ const value = values[i]!
91
+ // Same result as `value.replace('0x', '')`, without a search when the
92
+ // prefix is present.
93
+ hex += value.startsWith('0x') ? value.slice(2) : value.replace('0x', '')
94
+ }
95
+ return hex as Hex
84
96
  }
85
97
 
86
98
  export declare namespace concat {
@@ -196,7 +208,13 @@ export function fromBytes(
196
208
  options: fromBytes.Options = {},
197
209
  ): Hex {
198
210
  let string = ''
199
- for (let i = 0; i < value.length; i++) string += hexes[value[i]!]
211
+ // Native `Uint8Array.prototype.toHex` is several times faster, and returns a
212
+ // flat string rather than a rope of concatenated pairs.
213
+ // Empty values (including detached or out-of-bounds views, whose length is
214
+ // 0) skip it, since it throws on those.
215
+ if (typeof nativeToHex === 'function' && value instanceof Uint8Array) {
216
+ if (value.length > 0) string = nativeToHex.call(value)
217
+ } else for (let i = 0; i < value.length; i++) string += hexes[value[i]!]
200
218
  const hex = `0x${string}` as const
201
219
 
202
220
  if (typeof options.size === 'number') {
@@ -242,6 +260,37 @@ export function fromNumber(
242
260
  ): Hex {
243
261
  const { signed, size } = options
244
262
 
263
+ // A non-negative `bigint` that fits `size` skips the range check too.
264
+ if (typeof value === 'bigint' && !signed && value >= 0n) {
265
+ const hex = value.toString(16)
266
+ if (!size) return `0x${hex}`
267
+ if (Number.isSafeInteger(size) && size > 0 && hex.length <= size * 2)
268
+ return `0x${hex.padStart(size * 2, '0')}`
269
+ }
270
+
271
+ // An unsigned safe integer is in range when there is no `size` or the size
272
+ // exceeds 6 bytes, so it skips the `bigint` range check.
273
+ if (
274
+ typeof value === 'number' &&
275
+ !signed &&
276
+ Number.isSafeInteger(value) &&
277
+ value >= 0 &&
278
+ (!size || size > 6)
279
+ ) {
280
+ const hex = `0x${value.toString(16)}` as Hex
281
+ return size ? padLeft(hex, size) : hex
282
+ }
283
+
284
+ return fromNumberChecked(value, signed, size)
285
+ }
286
+
287
+ // The range-checked path of `fromNumber`, kept separate so that `fromNumber`
288
+ // stays small enough to inline.
289
+ function fromNumberChecked(
290
+ value: number | bigint,
291
+ signed: boolean | undefined,
292
+ size: number | undefined,
293
+ ): Hex {
245
294
  const value_ = BigInt(value)
246
295
 
247
296
  let maxValue: bigint | number | undefined
@@ -268,7 +317,7 @@ export function fromNumber(
268
317
  }
269
318
 
270
319
  const stringValue = (
271
- signed && value_ < 0 ? BigInt.asUintN(size * 8, BigInt(value_)) : value_
320
+ signed && value_ < 0 ? BigInt.asUintN(size! * 8, BigInt(value_)) : value_
272
321
  ).toString(16)
273
322
 
274
323
  const hex = `0x${stringValue}` as Hex
@@ -455,9 +504,9 @@ export function slice(
455
504
  ): Hex {
456
505
  const { strict } = options
457
506
  internal.assertStartOffset(value, start)
458
- const value_ = `0x${value
459
- .replace('0x', '')
460
- .slice((start ?? 0) * 2, (end ?? value.length) * 2)}` as const
507
+ const value_ = `0x${(
508
+ value.startsWith('0x') ? value.slice(2) : value.replace('0x', '')
509
+ ).slice((start ?? 0) * 2, (end ?? value.length) * 2)}` as const
461
510
  if (strict) internal.assertEndOffset(value_, start, end)
462
511
  return value_
463
512
  }
@@ -569,12 +618,7 @@ export function toBigInt(hex: Hex, options: toBigInt.Options = {}): bigint {
569
618
 
570
619
  const value = BigInt(hex)
571
620
  if (!signed) return value
572
-
573
- const max_unsigned = (1n << (BigInt(size(hex)) * 8n)) - 1n
574
- const max_signed = max_unsigned >> 1n
575
-
576
- if (value <= max_signed) return value
577
- return value - max_unsigned - 1n
621
+ return BigInt.asIntN(size(hex) * 8, value)
578
622
  }
579
623
 
580
624
  export declare namespace toBigInt {
@@ -658,6 +702,8 @@ export declare namespace toBytes {
658
702
  /**
659
703
  * Decodes a {@link ox#Hex.Hex} value into a number.
660
704
  *
705
+ * Throws `Hex.IntegerOutOfRangeError` when the value is not a safe integer. Use `toBigInt` for larger values.
706
+ *
661
707
  * @example
662
708
  * ```ts twoslash
663
709
  * import { Hex } from 'ox'
@@ -675,14 +721,25 @@ export declare namespace toBytes {
675
721
  */
676
722
  export function toNumber(hex: Hex, options: toNumber.Options = {}): number {
677
723
  const { signed, size } = options
678
- if (!signed && !size) return Number(hex)
679
- return Number(toBigInt(hex, options))
724
+ const value = !signed && !size ? Number(hex) : Number(toBigInt(hex, options))
725
+ if (!Number.isSafeInteger(value))
726
+ throw new IntegerOutOfRangeError({
727
+ max: `${Number.MAX_SAFE_INTEGER}`,
728
+ min: signed ? `${Number.MIN_SAFE_INTEGER}` : '0',
729
+ signed,
730
+ size,
731
+ value: `${value}`,
732
+ })
733
+ return value
680
734
  }
681
735
 
682
736
  export declare namespace toNumber {
683
737
  type Options = toBigInt.Options
684
738
 
685
- type ErrorType = toBigInt.ErrorType | Errors.GlobalErrorType
739
+ type ErrorType =
740
+ | toBigInt.ErrorType
741
+ | IntegerOutOfRangeError
742
+ | Errors.GlobalErrorType
686
743
  }
687
744
 
688
745
  /**
@@ -713,7 +770,7 @@ export function toString(hex: Hex, options: toString.Options = {}): string {
713
770
  internal_bytes.assertSize(bytes, size)
714
771
  bytes = Bytes.trimRight(bytes)
715
772
  }
716
- return new TextDecoder().decode(bytes)
773
+ return decoder.decode(bytes)
717
774
  }
718
775
 
719
776
  export declare namespace toString {
@@ -752,12 +809,12 @@ export function validate(
752
809
  options: validate.Options = {},
753
810
  ): value is Hex {
754
811
  const { even = false, strict = false } = options
755
- try {
756
- assert(value, { even, strict })
757
- return true
758
- } catch {
759
- return false
760
- }
812
+ // Mirrors `assert` without constructing and catching an error.
813
+ if (!value || typeof value !== 'string') return false
814
+ if (strict && !hexRegex.test(value)) return false
815
+ if (!value.startsWith('0x')) return false
816
+ if (even && value.length % 2 !== 0) return false
817
+ return true
761
818
  }
762
819
 
763
820
  export declare namespace validate {
package/core/Json.ts CHANGED
@@ -2,6 +2,9 @@ import type * as Errors from './Errors.js'
2
2
 
3
3
  const bigIntSuffix = '#__bigint'
4
4
 
5
+ /** Text `BigInt` accepts once surrounding whitespace is trimmed. */
6
+ const bigIntPattern = /^(?:[+-]?\d+|0[bB][01]+|0[oO][0-7]+|0[xX][\da-fA-F]+)?$/
7
+
5
8
  /**
6
9
  * Serializes a value to a canonical JSON string as defined by
7
10
  * [RFC 8785 (JSON Canonicalization Scheme)](https://www.rfc-editor.org/rfc/rfc8785).
@@ -81,10 +84,28 @@ export function parse(
81
84
  string: string,
82
85
  reviver?: ((this: any, key: string, value: any) => any) | undefined,
83
86
  ) {
87
+ // Without a reviver, text that cannot hold the bigint marker (neither
88
+ // literally nor through a `\u` escape) parses without the reviver wrapper.
89
+ if (
90
+ typeof reviver !== 'function' &&
91
+ typeof string === 'string' &&
92
+ !string.includes(bigIntSuffix) &&
93
+ !string.includes('\\u')
94
+ )
95
+ return JSON.parse(string)
84
96
  return JSON.parse(string, (key, value_) => {
85
97
  const value = value_
86
- if (typeof value === 'string' && value.endsWith(bigIntSuffix))
87
- return BigInt(value.slice(0, -bigIntSuffix.length))
98
+ if (typeof value === 'string' && value.endsWith(bigIntSuffix)) {
99
+ // Check the text before calling `BigInt`, so that a string that is not
100
+ // a bigint (e.g. untrusted text) is kept without throwing and catching.
101
+ const text = value.slice(0, -bigIntSuffix.length).trim()
102
+ if (bigIntPattern.test(text))
103
+ try {
104
+ return BigInt(text)
105
+ } catch {
106
+ // Too large for a bigint, so keep the string.
107
+ }
108
+ }
88
109
  return typeof reviver === 'function' ? reviver(key, value) : value
89
110
  })
90
111
  }