ox 1.8.16 → 1.8.19

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 (242) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/dist/core/AbiError.d.ts +3 -3
  3. package/dist/core/AbiError.js +2 -2
  4. package/dist/core/AbiEvent.d.ts +3 -3
  5. package/dist/core/AbiEvent.d.ts.map +1 -1
  6. package/dist/core/AbiEvent.js +29 -24
  7. package/dist/core/AbiEvent.js.map +1 -1
  8. package/dist/core/AbiItem.d.ts.map +1 -1
  9. package/dist/core/AbiItem.js +15 -5
  10. package/dist/core/AbiItem.js.map +1 -1
  11. package/dist/core/AbiParameters.d.ts +1 -1
  12. package/dist/core/AbiParameters.d.ts.map +1 -1
  13. package/dist/core/AbiParameters.js +8 -2
  14. package/dist/core/AbiParameters.js.map +1 -1
  15. package/dist/core/Authorization.d.ts.map +1 -1
  16. package/dist/core/Authorization.js +6 -3
  17. package/dist/core/Authorization.js.map +1 -1
  18. package/dist/core/Base58.d.ts.map +1 -1
  19. package/dist/core/Base58.js +16 -4
  20. package/dist/core/Base58.js.map +1 -1
  21. package/dist/core/BinaryStateTree.js +3 -1
  22. package/dist/core/BinaryStateTree.js.map +1 -1
  23. package/dist/core/Blobs.d.ts +4 -5
  24. package/dist/core/Blobs.d.ts.map +1 -1
  25. package/dist/core/Blobs.js +40 -45
  26. package/dist/core/Blobs.js.map +1 -1
  27. package/dist/core/Block.d.ts +2 -0
  28. package/dist/core/Block.d.ts.map +1 -1
  29. package/dist/core/Block.js +1 -0
  30. package/dist/core/Block.js.map +1 -1
  31. package/dist/core/BlockOverrides.d.ts +3 -2
  32. package/dist/core/BlockOverrides.d.ts.map +1 -1
  33. package/dist/core/BlockOverrides.js +4 -2
  34. package/dist/core/BlockOverrides.js.map +1 -1
  35. package/dist/core/Bytes.d.ts.map +1 -1
  36. package/dist/core/Bytes.js +10 -2
  37. package/dist/core/Bytes.js.map +1 -1
  38. package/dist/core/Cbor.d.ts +13 -6
  39. package/dist/core/Cbor.d.ts.map +1 -1
  40. package/dist/core/Cbor.js +31 -18
  41. package/dist/core/Cbor.js.map +1 -1
  42. package/dist/core/Ens.d.ts +1 -1
  43. package/dist/core/Ens.js +3 -3
  44. package/dist/core/Ens.js.map +1 -1
  45. package/dist/core/FrameReceipt.d.ts.map +1 -1
  46. package/dist/core/FrameReceipt.js +4 -1
  47. package/dist/core/FrameReceipt.js.map +1 -1
  48. package/dist/core/Hash.d.ts +1 -1
  49. package/dist/core/Hash.js +1 -1
  50. package/dist/core/Hex.d.ts.map +1 -1
  51. package/dist/core/Hex.js +34 -31
  52. package/dist/core/Hex.js.map +1 -1
  53. package/dist/core/Rlp.d.ts +1 -1
  54. package/dist/core/Rlp.d.ts.map +1 -1
  55. package/dist/core/Rlp.js +107 -0
  56. package/dist/core/Rlp.js.map +1 -1
  57. package/dist/core/RpcTransport.d.ts.map +1 -1
  58. package/dist/core/RpcTransport.js +3 -1
  59. package/dist/core/RpcTransport.js.map +1 -1
  60. package/dist/core/Siwe.d.ts +1 -0
  61. package/dist/core/Siwe.d.ts.map +1 -1
  62. package/dist/core/Siwe.js +15 -8
  63. package/dist/core/Siwe.js.map +1 -1
  64. package/dist/core/Transaction.d.ts.map +1 -1
  65. package/dist/core/Transaction.js +9 -3
  66. package/dist/core/Transaction.js.map +1 -1
  67. package/dist/core/TransactionReceipt.d.ts.map +1 -1
  68. package/dist/core/TransactionReceipt.js +8 -2
  69. package/dist/core/TransactionReceipt.js.map +1 -1
  70. package/dist/core/TypedData.d.ts +2 -2
  71. package/dist/core/TypedData.d.ts.map +1 -1
  72. package/dist/core/TypedData.js +16 -4
  73. package/dist/core/TypedData.js.map +1 -1
  74. package/dist/core/internal/abiParameters.d.ts +3 -3
  75. package/dist/core/internal/abiParameters.d.ts.map +1 -1
  76. package/dist/core/internal/abiParameters.js +93 -68
  77. package/dist/core/internal/abiParameters.js.map +1 -1
  78. package/dist/core/internal/base58.d.ts.map +1 -1
  79. package/dist/core/internal/base58.js +9 -3
  80. package/dist/core/internal/base58.js.map +1 -1
  81. package/dist/core/internal/caches.d.ts +6 -0
  82. package/dist/core/internal/caches.d.ts.map +1 -1
  83. package/dist/core/internal/caches.js +3 -1
  84. package/dist/core/internal/caches.js.map +1 -1
  85. package/dist/core/internal/codec/hex.d.ts +13 -0
  86. package/dist/core/internal/codec/hex.d.ts.map +1 -1
  87. package/dist/core/internal/codec/hex.js +25 -2
  88. package/dist/core/internal/codec/hex.js.map +1 -1
  89. package/dist/core/internal/codec/int.d.ts.map +1 -1
  90. package/dist/core/internal/codec/int.js +48 -32
  91. package/dist/core/internal/codec/int.js.map +1 -1
  92. package/dist/core/internal/cursor.d.ts +11 -4
  93. package/dist/core/internal/cursor.d.ts.map +1 -1
  94. package/dist/core/internal/cursor.js +39 -15
  95. package/dist/core/internal/cursor.js.map +1 -1
  96. package/dist/core/internal/lru.d.ts +11 -12
  97. package/dist/core/internal/lru.d.ts.map +1 -1
  98. package/dist/core/internal/lru.js +22 -32
  99. package/dist/core/internal/lru.js.map +1 -1
  100. package/dist/core/internal/promise.d.ts.map +1 -1
  101. package/dist/core/internal/promise.js +13 -7
  102. package/dist/core/internal/promise.js.map +1 -1
  103. package/dist/erc6492/SignatureErc6492.d.ts +1 -1
  104. package/dist/erc6492/SignatureErc6492.d.ts.map +1 -1
  105. package/dist/erc6492/SignatureErc6492.js +1 -1
  106. package/dist/erc6492/SignatureErc6492.js.map +1 -1
  107. package/dist/tempo/SignatureEnvelope.d.ts.map +1 -1
  108. package/dist/tempo/SignatureEnvelope.js +7 -41
  109. package/dist/tempo/SignatureEnvelope.js.map +1 -1
  110. package/dist/tempo/TransactionReceipt.d.ts.map +1 -1
  111. package/dist/tempo/TransactionReceipt.js +4 -1
  112. package/dist/tempo/TransactionReceipt.js.map +1 -1
  113. package/dist/tempo/ZoneId.d.ts +3 -1
  114. package/dist/tempo/ZoneId.d.ts.map +1 -1
  115. package/dist/tempo/ZoneId.js +7 -1
  116. package/dist/tempo/ZoneId.js.map +1 -1
  117. package/dist/tempo/internal/virtualMasterPool.d.ts.map +1 -1
  118. package/dist/tempo/internal/virtualMasterPool.js +17 -6
  119. package/dist/tempo/internal/virtualMasterPool.js.map +1 -1
  120. package/dist/tempo/internal/webauthnData.d.ts +23 -0
  121. package/dist/tempo/internal/webauthnData.d.ts.map +1 -0
  122. package/dist/tempo/internal/webauthnData.js +75 -0
  123. package/dist/tempo/internal/webauthnData.js.map +1 -0
  124. package/dist/zod/Authorization.d.ts +14 -14
  125. package/dist/zod/Authorization.d.ts.map +1 -1
  126. package/dist/zod/Authorization.js +9 -2
  127. package/dist/zod/Authorization.js.map +1 -1
  128. package/dist/zod/Block.d.ts +12 -6
  129. package/dist/zod/Block.d.ts.map +1 -1
  130. package/dist/zod/Block.js +1 -0
  131. package/dist/zod/Block.js.map +1 -1
  132. package/dist/zod/BlockOverrides.d.ts.map +1 -1
  133. package/dist/zod/BlockOverrides.js +15 -4
  134. package/dist/zod/BlockOverrides.js.map +1 -1
  135. package/dist/zod/RpcSchema.d.ts +58 -46
  136. package/dist/zod/RpcSchema.d.ts.map +1 -1
  137. package/dist/zod/Signature.d.ts.map +1 -1
  138. package/dist/zod/Signature.js +2 -1
  139. package/dist/zod/Signature.js.map +1 -1
  140. package/dist/zod/Transaction.d.ts +12 -12
  141. package/dist/zod/TransactionEnvelope.d.ts +8 -8
  142. package/dist/zod/TransactionRequest.d.ts +4 -4
  143. package/dist/zod/TxEnvelopeEip7702.d.ts +8 -8
  144. package/dist/zod/TypedData.d.ts +4 -4
  145. package/dist/zod/internal/rpcSchemas/Eth.d.ts +58 -46
  146. package/dist/zod/internal/rpcSchemas/Eth.d.ts.map +1 -1
  147. package/dist/zod/internal/rpcSchemas/Wallet.d.ts +4 -4
  148. package/dist/zod/tempo/RpcSchemaTempo.d.ts +16 -8
  149. package/dist/zod/tempo/RpcSchemaTempo.d.ts.map +1 -1
  150. package/dist/zod/tempo/SignatureEnvelope.d.ts.map +1 -1
  151. package/dist/zod/tempo/SignatureEnvelope.js +5 -15
  152. package/dist/zod/tempo/SignatureEnvelope.js.map +1 -1
  153. package/dist/zod/tempo/Transaction.d.ts +6 -6
  154. package/package.json +2 -2
  155. package/src/core/AbiError.ts +3 -3
  156. package/src/core/AbiEvent.bench.ts +45 -0
  157. package/src/core/AbiEvent.ts +32 -25
  158. package/src/core/AbiFunction.bench.ts +16 -0
  159. package/src/core/AbiItem.ts +13 -4
  160. package/src/core/AbiParameters.bench.ts +33 -0
  161. package/src/core/AbiParameters.ts +8 -1
  162. package/src/core/Authorization.ts +6 -3
  163. package/src/core/Base58.bench.ts +28 -0
  164. package/src/core/Base58.ts +15 -4
  165. package/src/core/BinaryStateTree.ts +4 -1
  166. package/src/core/Blobs.bench.ts +40 -0
  167. package/src/core/Blobs.ts +49 -57
  168. package/src/core/Block.ts +3 -0
  169. package/src/core/BlockOverrides.ts +7 -3
  170. package/src/core/Bytes.bench.ts +34 -0
  171. package/src/core/Bytes.ts +10 -2
  172. package/src/core/Cbor.ts +36 -14
  173. package/src/core/Ens.ts +3 -3
  174. package/src/core/FrameReceipt.ts +4 -1
  175. package/src/core/Hash.ts +1 -1
  176. package/src/core/Hex.bench.ts +17 -0
  177. package/src/core/Hex.ts +29 -32
  178. package/src/core/Rlp.ts +139 -1
  179. package/src/core/Rlp_tx.bench.ts +55 -0
  180. package/src/core/RpcTransport.ts +3 -1
  181. package/src/core/Siwe.ts +18 -8
  182. package/src/core/Transaction.ts +9 -3
  183. package/src/core/TransactionReceipt.ts +8 -2
  184. package/src/core/TypedData.bench.ts +50 -0
  185. package/src/core/TypedData.ts +16 -4
  186. package/src/core/_test/AbiEvent.test.ts +83 -0
  187. package/src/core/_test/AbiFunction.test.ts +7 -6
  188. package/src/core/_test/AbiParameters.decode.test.ts +34 -1
  189. package/src/core/_test/AbiParameters.encode.test.ts +10 -0
  190. package/src/core/_test/AbiParameters.test.ts +15 -0
  191. package/src/core/_test/Authorization.test.ts +36 -0
  192. package/src/core/_test/BinaryStateTree.test.ts +35 -0
  193. package/src/core/_test/Block.test.ts +54 -0
  194. package/src/core/_test/BlockOverrides.test.ts +14 -1
  195. package/src/core/_test/Bytes.test.ts +33 -0
  196. package/src/core/_test/Caches.test.ts +20 -1
  197. package/src/core/_test/Cbor.test.ts +26 -0
  198. package/src/core/_test/FrameReceipt.test.ts +13 -0
  199. package/src/core/_test/Rlp.fuzz.ts +55 -1
  200. package/src/core/_test/Rlp.test.ts +64 -0
  201. package/src/core/_test/RpcTransport.test.ts +15 -2
  202. package/src/core/_test/Siwe.test.ts +126 -1
  203. package/src/core/_test/Transaction.test.ts +47 -2
  204. package/src/core/_test/TransactionEnvelopeEip7702.test.ts +2 -2
  205. package/src/core/_test/TransactionReceipt.test.ts +41 -0
  206. package/src/core/_test/TypedData.test.ts +19 -0
  207. package/src/core/_test/internal/cursor.test.ts +42 -6
  208. package/src/core/internal/abiParameters.ts +117 -80
  209. package/src/core/internal/base58.ts +9 -3
  210. package/src/core/internal/caches.ts +3 -1
  211. package/src/core/internal/codec/hex.ts +27 -2
  212. package/src/core/internal/codec/int.ts +45 -32
  213. package/src/core/internal/cursor.ts +52 -23
  214. package/src/core/internal/lru.ts +21 -35
  215. package/src/core/internal/promise.ts +11 -5
  216. package/src/erc4337/_test/UserOperation.test.ts +2 -2
  217. package/src/erc6492/SignatureErc6492.ts +1 -1
  218. package/src/erc6492/_test/SignatureErc6492.test.ts +48 -1
  219. package/src/tempo/SignatureEnvelope.test.ts +89 -1
  220. package/src/tempo/SignatureEnvelope.ts +7 -43
  221. package/src/tempo/TransactionReceipt.test.ts +8 -0
  222. package/src/tempo/TransactionReceipt.ts +4 -1
  223. package/src/tempo/VirtualMaster.bench.ts +7 -0
  224. package/src/tempo/ZoneId.test.ts +11 -0
  225. package/src/tempo/ZoneId.ts +7 -1
  226. package/src/tempo/internal/virtualMasterPool.ts +20 -6
  227. package/src/tempo/internal/webauthnData.ts +84 -0
  228. package/src/version.ts +1 -1
  229. package/src/zod/Authorization.ts +10 -2
  230. package/src/zod/Block.ts +1 -0
  231. package/src/zod/BlockOverrides.ts +26 -7
  232. package/src/zod/Signature.ts +2 -1
  233. package/src/zod/_test/Authorization.test.ts +26 -0
  234. package/src/zod/_test/Block.test-d.ts +6 -0
  235. package/src/zod/_test/Block.test.ts +5 -0
  236. package/src/zod/_test/BlockOverrides.test.ts +36 -1
  237. package/src/zod/_test/Signature.test.ts +12 -0
  238. package/src/zod/tempo/SignatureEnvelope.ts +7 -16
  239. package/src/zod/tempo/_test/SignatureEnvelope.test.ts +37 -1
  240. package/src/core/_test/internal/lru.test.ts +0 -38
  241. package/src/core/_test/internal/uid.test.ts +0 -7
  242. package/src/core/internal/uid.ts +0 -15
package/src/core/Blobs.ts CHANGED
@@ -3,7 +3,6 @@ import * as Bytes from './Bytes.js'
3
3
  import * as Errors from './Errors.js'
4
4
  import * as Hash from './Hash.js'
5
5
  import * as Hex from './Hex.js'
6
- import * as Cursor from './internal/cursor.js'
7
6
  import type { Compute } from './internal/types.js'
8
7
  import * as Kzg from './Kzg.js'
9
8
 
@@ -290,43 +289,40 @@ export function from<
290
289
  size: size_,
291
290
  })
292
291
 
293
- const blobs = []
292
+ const blobs: Bytes.Bytes[] = []
294
293
 
295
294
  let active = true
296
295
  let position = 0
297
296
  while (active) {
298
- const blob = Cursor.create(new Uint8Array(bytesPerBlob))
299
-
300
- let size = 0
301
- while (size < fieldElementsPerBlob) {
302
- const bytes = data_.slice(position, position + (bytesPerFieldElement - 1))
303
-
304
- // Push a zero byte so the field element doesn't overflow the BLS modulus.
305
- blob.pushByte(0x00)
306
-
307
- // Push the current segment of data bytes.
308
- blob.pushBytes(bytes)
309
-
310
- // If we detect that the current segment of data bytes is less than 31 bytes,
311
- // we can stop processing and push a terminator byte to indicate the end of the blob.
312
- if (bytes.length < 31) {
313
- blob.pushByte(0x80)
297
+ const blob = new Uint8Array(bytesPerBlob)
298
+
299
+ // Each field element is a zero byte, so it doesn't overflow the BLS
300
+ // modulus, followed by 31 data bytes. The zero byte is already in place.
301
+ // Copying byte by byte beats a `subarray` view plus `set` at this size, and
302
+ // allocates nothing.
303
+ for (
304
+ let offset = 1;
305
+ offset < bytesPerBlob;
306
+ offset += bytesPerFieldElement
307
+ ) {
308
+ const length = Math.min(31, size_ - position)
309
+ for (let i = 0; i < length; i++) blob[offset + i] = data_[position + i]!
310
+
311
+ // A segment shorter than 31 bytes ends the data: push a terminator byte
312
+ // to mark it.
313
+ if (length < 31) {
314
+ blob[offset + length] = 0x80
314
315
  active = false
315
316
  break
316
317
  }
317
318
 
318
- size++
319
319
  position += 31
320
320
  }
321
321
 
322
322
  blobs.push(blob)
323
323
  }
324
324
 
325
- return (
326
- as === 'Bytes'
327
- ? blobs.map((x) => x.bytes)
328
- : blobs.map((x) => Hex.fromBytes(x.bytes))
329
- ) as never
325
+ return (as === 'Bytes' ? blobs : blobs.map((x) => Hex.fromBytes(x))) as never
330
326
  }
331
327
 
332
328
  export declare namespace from {
@@ -344,7 +340,6 @@ export declare namespace from {
344
340
  | EmptyBlobError
345
341
  | Bytes.fromHex.ErrorType
346
342
  | Hex.fromBytes.ErrorType
347
- | Cursor.create.ErrorType
348
343
  | Bytes.size.ErrorType
349
344
  | Errors.GlobalErrorType
350
345
  }
@@ -371,7 +366,7 @@ export declare namespace from {
371
366
  *
372
367
  * const blobs = Blobs.from('0xdeadbeef')
373
368
  * const data = Blobs.to(blobs, 'Bytes')
374
- * // @log: Uint8Array [ 13, 174, 190, 239 ]
369
+ * // @log: Uint8Array [ 222, 173, 190, 239 ]
375
370
  * ```
376
371
  *
377
372
  * @param blobs - The {@link ox#Blobs.Blobs} to transform.
@@ -394,39 +389,37 @@ export function to<
394
389
  : blobs
395
390
  ) as Bytes.Bytes[]
396
391
 
397
- const length = blobs_.reduce((length, blob) => length + blob.length, 0)
398
- const data = Cursor.create(new Uint8Array(length))
399
- let active = true
392
+ // Only the final blob contains a terminator: the last `0x80` byte in it, when
393
+ // that byte is data rather than the zero byte leading a field element.
394
+ // Preceding blobs contain only data.
395
+ const last = blobs_[blobs_.length - 1]
396
+ const terminator = last ? last.lastIndexOf(0x80) : -1
397
+ const lastEnd =
398
+ terminator % bytesPerFieldElement > 0 ? terminator : (last?.length ?? 0)
400
399
 
401
- for (const [index, blob] of blobs_.entries()) {
402
- const cursor = Cursor.create(blob)
403
- // Only the final blob contains a terminator; preceding blobs contain only data.
404
- const isLastBlob = index === blobs_.length - 1
405
- while (active && cursor.position < blob.length) {
406
- // First byte will be a zero 0x00 byte – we can skip.
407
- cursor.incrementPosition(1)
408
-
409
- let consume = 31
410
- if (blob.length - cursor.position < 31)
411
- consume = blob.length - cursor.position
412
-
413
- for (let i = 0; i < consume; i++) {
414
- const byte = cursor.readByte()
415
- const isTerminator =
416
- isLastBlob &&
417
- byte === 0x80 &&
418
- !cursor.inspectBytes(cursor.remaining).includes(0x80)
419
- if (isTerminator) {
420
- active = false
421
- break
422
- }
423
- data.pushByte(byte)
424
- }
400
+ // Each field element contributes all of its bytes but the leading zero byte.
401
+ const dataSize = (end: number) =>
402
+ Math.floor(end / bytesPerFieldElement) * 31 +
403
+ Math.max(0, (end % bytesPerFieldElement) - 1)
404
+
405
+ let length = dataSize(lastEnd)
406
+ for (let index = 0; index < blobs_.length - 1; index++)
407
+ length += dataSize(blobs_[index]!.length)
408
+
409
+ const data = new Uint8Array(length)
410
+ let position = 0
411
+ for (let index = 0; index < blobs_.length; index++) {
412
+ const blob = blobs_[index]!
413
+ const end = index === blobs_.length - 1 ? lastEnd : blob.length
414
+ // Skip the zero byte leading each field element. Copying byte by byte
415
+ // beats a `subarray` view plus `set` for 31 bytes, and allocates nothing.
416
+ for (let offset = 1; offset < end; offset += bytesPerFieldElement) {
417
+ const stop = Math.min(offset + 31, end)
418
+ for (let i = offset; i < stop; i++) data[position++] = blob[i]!
425
419
  }
426
420
  }
427
421
 
428
- const trimmedData = data.bytes.slice(0, data.position)
429
- return (to_ === 'Hex' ? Hex.fromBytes(trimmedData) : trimmedData) as never
422
+ return (to_ === 'Hex' ? Hex.fromBytes(data) : data) as never
430
423
  }
431
424
 
432
425
  export declare namespace to {
@@ -437,7 +430,6 @@ export declare namespace to {
437
430
  type ErrorType =
438
431
  | Hex.fromBytes.ErrorType
439
432
  | Bytes.fromHex.ErrorType
440
- | Cursor.create.ErrorType
441
433
  | Errors.GlobalErrorType
442
434
  }
443
435
 
@@ -473,7 +465,7 @@ export declare namespace toHex {
473
465
  *
474
466
  * const blobs = Blobs.from('0xdeadbeef')
475
467
  * const data = Blobs.toBytes(blobs) // [!code focus]
476
- * // @log: Uint8Array [ 13, 174, 190, 239 ]
468
+ * // @log: Uint8Array [ 222, 173, 190, 239 ]
477
469
  * ```
478
470
  */
479
471
  export function toBytes(
package/src/core/Block.ts CHANGED
@@ -49,6 +49,8 @@ export type Block<
49
49
  parentHash: Hex.Hex
50
50
  /** Root of the this block’s receipts trie */
51
51
  receiptsRoot: Hex.Hex
52
+ /** EIP-7685 requests hash */
53
+ requestsHash?: Hex.Hex | undefined
52
54
  sealFields?: readonly Hex.Hex[] | undefined
53
55
  /** SHA3 of the uncles data in this block */
54
56
  sha3Uncles: Hex.Hex
@@ -174,6 +176,7 @@ export function toRpc<
174
176
  parentBeaconBlockRoot: block.parentBeaconBlockRoot,
175
177
  parentHash: block.parentHash,
176
178
  receiptsRoot: block.receiptsRoot,
179
+ requestsHash: block.requestsHash,
177
180
  sealFields: block.sealFields,
178
181
  sha3Uncles: block.sha3Uncles,
179
182
  size: Quantity.fromNumberish(block.size),
@@ -1,5 +1,5 @@
1
1
  import type * as Address from './Address.js'
2
- import type * as Hex from './Hex.js'
2
+ import * as Hex from './Hex.js'
3
3
  import * as Quantity from './internal/quantity.js'
4
4
  import * as Withdrawal from './Withdrawal.js'
5
5
 
@@ -44,7 +44,8 @@ export type Rpc = BlockOverrides<Hex.Hex, Hex.Hex>
44
44
  * '0x0000000000000000000000000000000000000000',
45
45
  * gasLimit: '0x4',
46
46
  * number: '0x5',
47
- * prevRandao: '0x6',
47
+ * prevRandao:
48
+ * '0x0000000000000000000000000000000000000000000000000000000000000006',
48
49
  * time: '0x1234567890',
49
50
  * withdrawals: [
50
51
  * {
@@ -126,7 +127,10 @@ export function toRpc(blockOverrides: toRpc.Input): Rpc {
126
127
  if (blockOverrides.number !== undefined)
127
128
  rpc.number = Quantity.fromNumberish(blockOverrides.number)
128
129
  if (blockOverrides.prevRandao !== undefined)
129
- rpc.prevRandao = Quantity.fromNumberish(blockOverrides.prevRandao)
130
+ rpc.prevRandao = Hex.padLeft(
131
+ Quantity.fromNumberish(blockOverrides.prevRandao),
132
+ 32,
133
+ )
130
134
  if (blockOverrides.time !== undefined)
131
135
  rpc.time = Quantity.fromNumberish(blockOverrides.time)
132
136
  if (blockOverrides.withdrawals !== undefined)
@@ -21,6 +21,8 @@ const bytes32 = (() => {
21
21
  return b
22
22
  })()
23
23
  const bytes32Zero = new Uint8Array(32)
24
+ const bytes32Small = Bytes.fromNumber(1_234_567n, { size: 32 })
25
+ const bytes32Wei = Bytes.fromNumber(42n * 10n ** 18n, { size: 32 })
24
26
 
25
27
  describe('Bytes.fromHex', () => {
26
28
  bench('32 bytes', () => {
@@ -106,9 +108,31 @@ describe('Bytes.toBigInt', () => {
106
108
  Bytes.toBigInt(bytes32Zero)
107
109
  })
108
110
 
111
+ bench('32 bytes (small value)', () => {
112
+ Bytes.toBigInt(bytes32Small)
113
+ })
114
+
115
+ bench('32 bytes (wei value)', () => {
116
+ Bytes.toBigInt(bytes32Wei)
117
+ })
118
+
109
119
  bench('32 bytes, size: 32', () => {
110
120
  Bytes.toBigInt(bytes32, { size: 32 })
111
121
  })
122
+
123
+ bench('32-byte word holding a small value', () => {
124
+ Bytes.toBigInt(bytes32Small)
125
+ })
126
+
127
+ bench('32 bytes, signed', () => {
128
+ Bytes.toBigInt(bytes32, { signed: true })
129
+ })
130
+ })
131
+
132
+ describe('Bytes.toNumber', () => {
133
+ bench('32-byte word holding a small value', () => {
134
+ Bytes.toNumber(bytes32Small)
135
+ })
112
136
  })
113
137
 
114
138
  describe('Bytes.padLeft', () => {
@@ -119,3 +143,13 @@ describe('Bytes.padLeft', () => {
119
143
  Bytes.padLeft(small.subarray(0, 1), 32)
120
144
  })
121
145
  })
146
+
147
+ describe('Bytes.validate', () => {
148
+ bench('valid', () => {
149
+ Bytes.validate(small)
150
+ })
151
+
152
+ bench('invalid', () => {
153
+ Bytes.validate('0xdeadbeef')
154
+ })
155
+ })
package/src/core/Bytes.ts CHANGED
@@ -745,9 +745,17 @@ export declare namespace trimRight {
745
745
  * @returns `true` if the value is {@link ox#Bytes.Bytes}, otherwise `false`.
746
746
  */
747
747
  export function validate(value: unknown): value is Bytes {
748
+ // The checks `assert` makes, without building and catching an error.
749
+ if (!value || typeof value !== 'object') return false
750
+ // A revoked Proxy, or a trap or getter, can throw from these checks; that
751
+ // is invalid input too.
748
752
  try {
749
- assert(value)
750
- return true
753
+ if (value instanceof Uint8Array) return true
754
+ return (
755
+ 'BYTES_PER_ELEMENT' in value &&
756
+ value.BYTES_PER_ELEMENT === 1 &&
757
+ value.constructor.name === 'Uint8Array'
758
+ )
751
759
  } catch {
752
760
  return false
753
761
  }
package/src/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
  *
@@ -99,6 +102,15 @@ export declare namespace decode {
99
102
  | Errors.GlobalErrorType
100
103
  }
101
104
 
105
+ /** Thrown when CBOR data nests deeper than the decoder allows. */
106
+ export class DepthLimitExceededError extends Errors.BaseError {
107
+ override readonly name = 'Cbor.DepthLimitExceededError'
108
+
109
+ constructor({ limit }: { limit: number }) {
110
+ super(`CBOR depth limit of \`${limit}\` exceeded.`)
111
+ }
112
+ }
113
+
102
114
  export class InvalidMajorTypeError extends Errors.BaseError {
103
115
  override readonly name = 'Cbor.InvalidMajorTypeError'
104
116
 
@@ -168,7 +180,7 @@ export class NumberTooLargeError extends Errors.BaseError {
168
180
 
169
181
  constructor({ number }: { number: string }) {
170
182
  super(
171
- `Number exceeds maximum safe integer (${Number.MAX_SAFE_INTEGER}): ${number}`,
183
+ `Number is outside the encodable integer range (-4294967296 to 4294967295): ${number}`,
172
184
  )
173
185
  }
174
186
  }
@@ -610,7 +622,12 @@ namespace getEncodable {
610
622
 
611
623
  /** @internal */
612
624
  // biome-ignore lint/correctness/noUnusedVariables: _
613
- function decodeCursor(cursor: Cursor.Cursor): unknown {
625
+ function decodeCursor(cursor: Cursor.Cursor, depth = 0): unknown {
626
+ // Bounds recursion, so deeply nested data throws instead of overflowing the
627
+ // stack.
628
+ if (depth >= depthLimit)
629
+ throw new DepthLimitExceededError({ limit: depthLimit })
630
+
614
631
  const initialByte = cursor.readUint8()
615
632
  const majorType = initialByte >> 5
616
633
  const additionalInfo = initialByte & 0b00011111
@@ -626,19 +643,19 @@ function decodeCursor(cursor: Cursor.Cursor): unknown {
626
643
 
627
644
  // Major type 2: Byte string
628
645
  case 2:
629
- return decodeCursor.readByteString(cursor, additionalInfo)
646
+ return decodeCursor.readByteString(cursor, additionalInfo, depth)
630
647
 
631
648
  // Major type 3: Text string
632
649
  case 3:
633
- return decodeCursor.readTextString(cursor, additionalInfo)
650
+ return decodeCursor.readTextString(cursor, additionalInfo, depth)
634
651
 
635
652
  // Major type 4: Array
636
653
  case 4:
637
- return decodeCursor.readArray(cursor, additionalInfo)
654
+ return decodeCursor.readArray(cursor, additionalInfo, depth)
638
655
 
639
656
  // Major type 5: Map
640
657
  case 5:
641
- return decodeCursor.readMap(cursor, additionalInfo)
658
+ return decodeCursor.readMap(cursor, additionalInfo, depth)
642
659
 
643
660
  // Major type 6: Tagged data (not yet supported)
644
661
  case 6:
@@ -665,6 +682,7 @@ namespace decodeCursor {
665
682
  | readSimpleOrFloat.ErrorType
666
683
  | UnsupportedTagError
667
684
  | InvalidMajorTypeError
685
+ | DepthLimitExceededError
668
686
  | Errors.GlobalErrorType
669
687
 
670
688
  /** @internal */
@@ -717,6 +735,7 @@ namespace decodeCursor {
717
735
  export function readByteString(
718
736
  cursor: Cursor.Cursor,
719
737
  additionalInfo: number,
738
+ depth: number,
720
739
  ): Bytes.Bytes {
721
740
  // Indefinite-length byte string
722
741
  if (additionalInfo === 31) {
@@ -729,7 +748,7 @@ namespace decodeCursor {
729
748
  cursor.readUint8() // consume the break byte
730
749
  break
731
750
  }
732
- const chunk = decodeCursor(cursor) as Bytes.Bytes
751
+ const chunk = decodeCursor(cursor, depth + 1) as Bytes.Bytes
733
752
  if (!(chunk instanceof Uint8Array))
734
753
  throw new InvalidIndefiniteLengthChunkError({ type: 'byte string' })
735
754
  chunks.push(chunk)
@@ -762,6 +781,7 @@ namespace decodeCursor {
762
781
  export function readTextString(
763
782
  cursor: Cursor.Cursor,
764
783
  additionalInfo: number,
784
+ depth: number,
765
785
  ): string {
766
786
  // Indefinite-length text string
767
787
  if (additionalInfo === 31) {
@@ -773,7 +793,7 @@ namespace decodeCursor {
773
793
  cursor.readUint8() // consume the break byte
774
794
  break
775
795
  }
776
- const chunk = decodeCursor(cursor)
796
+ const chunk = decodeCursor(cursor, depth + 1)
777
797
  if (typeof chunk !== 'string')
778
798
  throw new InvalidIndefiniteLengthChunkError({ type: 'text string' })
779
799
  chunks.push(chunk)
@@ -800,6 +820,7 @@ namespace decodeCursor {
800
820
  export function readArray(
801
821
  cursor: Cursor.Cursor,
802
822
  additionalInfo: number,
823
+ depth: number,
803
824
  ): unknown[] {
804
825
  // Indefinite-length array
805
826
  if (additionalInfo === 31) {
@@ -811,7 +832,7 @@ namespace decodeCursor {
811
832
  cursor.readUint8() // consume the break byte
812
833
  break
813
834
  }
814
- result.push(decodeCursor(cursor))
835
+ result.push(decodeCursor(cursor, depth + 1))
815
836
  }
816
837
 
817
838
  return result
@@ -821,7 +842,7 @@ namespace decodeCursor {
821
842
  const result: unknown[] = []
822
843
 
823
844
  for (let i = 0; i < length; i++) {
824
- result.push(decodeCursor(cursor))
845
+ result.push(decodeCursor(cursor, depth + 1))
825
846
  }
826
847
 
827
848
  return result
@@ -836,6 +857,7 @@ namespace decodeCursor {
836
857
  export function readMap(
837
858
  cursor: Cursor.Cursor,
838
859
  additionalInfo: number,
860
+ depth: number,
839
861
  ): Record<string, unknown> {
840
862
  // Indefinite-length map
841
863
  if (additionalInfo === 31) {
@@ -847,7 +869,7 @@ namespace decodeCursor {
847
869
  cursor.readUint8() // consume the break byte
848
870
  break
849
871
  }
850
- const key = decodeCursor(cursor)
872
+ const key = decodeCursor(cursor, depth + 1)
851
873
  // Support both string and number keys (for COSE_Key with integer keys)
852
874
  const keyStr =
853
875
  typeof key === 'string'
@@ -855,7 +877,7 @@ namespace decodeCursor {
855
877
  : typeof key === 'number'
856
878
  ? String(key)
857
879
  : String(key)
858
- const value = decodeCursor(cursor)
880
+ const value = decodeCursor(cursor, depth + 1)
859
881
  setEntry(result, keyStr, value)
860
882
  }
861
883
 
@@ -866,7 +888,7 @@ namespace decodeCursor {
866
888
  const result: Record<string, unknown> = {}
867
889
 
868
890
  for (let i = 0; i < length; i++) {
869
- const key = decodeCursor(cursor)
891
+ const key = decodeCursor(cursor, depth + 1)
870
892
  // Support both string and number keys (for COSE_Key with integer keys)
871
893
  const keyStr =
872
894
  typeof key === 'string'
@@ -874,7 +896,7 @@ namespace decodeCursor {
874
896
  : typeof key === 'number'
875
897
  ? String(key)
876
898
  : String(key)
877
- const value = decodeCursor(cursor)
899
+ const value = decodeCursor(cursor, depth + 1)
878
900
  setEntry(result, keyStr, value)
879
901
  }
880
902
 
package/src/core/Ens.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ens_normalize } from '@adraffy/ens-normalize'
1
+ import { ensNormalize } from '@adraffy/ens-normalize.ts'
2
2
  import * as Bytes from './Bytes.js'
3
3
  import * as Errors from './Errors.js'
4
4
  import * as Hash from './Hash.js'
@@ -48,7 +48,7 @@ export declare namespace labelhash {
48
48
  * ```ts twoslash
49
49
  * import { Ens } from 'ox'
50
50
  * Ens.namehash('wevm.eth')
51
- * // @log: '0xf246651c1b9a6b141d19c2604e9a58f567973833990f830d882534a747801359'
51
+ * // @log: '0x08c85f2f4059e930c45a6aeff9dcd3bd95dc3c5c1cddef6a0626b31152248560'
52
52
  * ```
53
53
  *
54
54
  * @param name - ENS name.
@@ -98,7 +98,7 @@ export declare namespace namehash {
98
98
  * @returns Normalized ENS name.
99
99
  */
100
100
  export function normalize(name: string): string {
101
- return ens_normalize(name)
101
+ return ensNormalize(name)
102
102
  }
103
103
 
104
104
  export declare namespace normalize {
@@ -84,7 +84,10 @@ export function fromRpc(receipt: Rpc): FrameReceipt {
84
84
  gasUsed: Hex.toBigInt(receipt.gasUsed),
85
85
  logs: receipt.logs,
86
86
  stateGasUsed: Hex.toBigInt(receipt.stateGasUsed),
87
- status: fromRpcStatus[receipt.status],
87
+ // Own keys only: an inherited one such as `__proto__` is not a status.
88
+ status: Object.hasOwn(fromRpcStatus, receipt.status)
89
+ ? fromRpcStatus[receipt.status]
90
+ : (undefined as never),
88
91
  }
89
92
  }
90
93
 
package/src/core/Hash.ts CHANGED
@@ -261,7 +261,7 @@ export declare namespace blake3 {
261
261
  * import { Hash, Hex } from 'ox'
262
262
  *
263
263
  * Hash.keccak256(Hex.fromString('hello world'))
264
- * // @log: '0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0'
264
+ * // @log: '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'
265
265
  * ```
266
266
  *
267
267
  * @example
@@ -97,6 +97,9 @@ describe('Hex.toBigInt', () => {
97
97
  bench('32 bytes', () => {
98
98
  Hex.toBigInt(smallHex)
99
99
  })
100
+ bench('32 bytes, signed', () => {
101
+ Hex.toBigInt(hex32, { signed: true })
102
+ })
100
103
  })
101
104
 
102
105
  describe('Hex.padLeft', () => {
@@ -118,4 +121,18 @@ describe('Hex.validate (strict)', () => {
118
121
  bench('256 bytes', () => {
119
122
  Hex.validate(mediumHex, { strict: true })
120
123
  })
124
+
125
+ bench('invalid', () => {
126
+ Hex.validate('0xzz', { strict: true })
127
+ })
128
+ })
129
+
130
+ describe('Hex.validate', () => {
131
+ bench('valid', () => {
132
+ Hex.validate(smallHex)
133
+ })
134
+
135
+ bench('invalid (no prefix)', () => {
136
+ Hex.validate('deadbeef')
137
+ })
121
138
  })
package/src/core/Hex.ts CHANGED
@@ -82,9 +82,9 @@ export declare namespace assert {
82
82
  export function concat(...values: readonly Hex[]): Hex {
83
83
  if (values.length === 0) return '0x'
84
84
  if (values.length === 1) return values[0]!
85
- if (values.length === 2)
86
- return `0x${(values[0] as string).slice(2)}${(values[1] as string).slice(2)}` as Hex
87
- return `0x${values.reduce((hex, value) => hex + value.replace('0x', ''), '')}`
85
+ let hex = '0x'
86
+ for (let i = 0; i < values.length; i++) hex += values[i]!.slice(2)
87
+ return hex as Hex
88
88
  }
89
89
 
90
90
  export declare namespace concat {
@@ -253,27 +253,29 @@ export function fromNumber(
253
253
  ): Hex {
254
254
  const { signed, size } = options
255
255
 
256
- // Safe-integer unsigned fast path: skip BigInt conversion entirely.
256
+ // Unsigned fast path: a value fits `size` bytes exactly when its hex has at
257
+ // most `size * 2` digits, so the bound needs no BigInt arithmetic. Sizes
258
+ // that are not integers take the general path, which rejects them.
257
259
  if (
258
260
  !signed &&
259
- typeof value === 'number' &&
260
- value >= 0 &&
261
- Number.isSafeInteger(value)
261
+ (typeof value === 'bigint'
262
+ ? value >= 0n
263
+ : value >= 0 && Number.isSafeInteger(value)) &&
264
+ (!size || Number.isInteger(size))
262
265
  ) {
263
- if (size) {
264
- const maxValue = 2n ** (BigInt(size) * 8n) - 1n
265
- if (BigInt(value) > maxValue)
266
- throw new IntegerOutOfRangeError({
267
- max: `${maxValue}`,
268
- min: '0',
269
- signed: false,
270
- size,
271
- value: `${value}`,
272
- })
273
- const hex = `0x${value.toString(16)}` as Hex
274
- return padLeft(hex, size)
266
+ const hex = value.toString(16)
267
+ if (!size) return `0x${hex}`
268
+ if (hex.length > size * 2) {
269
+ const suffix = typeof value === 'bigint' ? 'n' : ''
270
+ throw new IntegerOutOfRangeError({
271
+ max: `${2n ** (BigInt(size) * 8n) - 1n}${suffix}`,
272
+ min: `0${suffix}`,
273
+ signed,
274
+ size,
275
+ value: `${value}${suffix}`,
276
+ })
275
277
  }
276
- return `0x${value.toString(16)}` as Hex
278
+ return `0x${hex.padStart(size * 2, '0')}`
277
279
  }
278
280
 
279
281
  const value_ = BigInt(value)
@@ -612,12 +614,7 @@ export function toBigInt(hex: Hex, options: toBigInt.Options = {}): bigint {
612
614
 
613
615
  const value = BigInt(hex)
614
616
  if (!signed) return value
615
-
616
- const max_unsigned = (1n << (BigInt(size(hex)) * 8n)) - 1n
617
- const max_signed = max_unsigned >> 1n
618
-
619
- if (value <= max_signed) return value
620
- return value - max_unsigned - 1n
617
+ return BigInt.asIntN(size(hex) * 8, value)
621
618
  }
622
619
 
623
620
  export declare namespace toBigInt {
@@ -829,12 +826,12 @@ export function validate(
829
826
  options: validate.Options = {},
830
827
  ): value is Hex {
831
828
  const { even = false, strict = false } = options
832
- try {
833
- assert(value, { even, strict })
834
- return true
835
- } catch {
836
- return false
837
- }
829
+ // The checks `assert` makes, without building and catching an error.
830
+ if (typeof value !== 'string') return false
831
+ if (strict && !hexRegex.test(value)) return false
832
+ if (!value.startsWith('0x')) return false
833
+ if (even && value.length % 2 !== 0) return false
834
+ return true
838
835
  }
839
836
 
840
837
  export declare namespace validate {