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
@@ -10,6 +10,7 @@ import * as Bytes from '../Bytes.js'
10
10
  import * as Errors from '../Errors.js'
11
11
  import * as Hex from '../Hex.js'
12
12
  import { bytesRegex, integerRegex } from '../Solidity.js'
13
+ import { nibbleTable } from './codec/hex.js'
13
14
  import { encoder } from './codec/utf8.js'
14
15
  import type * as Cursor from './cursor.js'
15
16
  import type { Compute, IsNarrowable, UnionToIntersection } from './types.js'
@@ -65,14 +66,8 @@ type MeasureContext = {
65
66
  checksumAddress: boolean
66
67
  }
67
68
 
68
- type HexPatch = {
69
- offset: number
70
- value: string
71
- }
72
-
73
69
  type WriteContext = {
74
70
  bytes: Bytes.Bytes
75
- patches: HexPatch[]
76
71
  view: DataView
77
72
  }
78
73
 
@@ -188,14 +183,14 @@ export function decodeAddress(
188
183
  })
189
184
  const wrap = (address: Hex.Hex) =>
190
185
  checksum ? Address.checksum(address) : address
191
- return [wrap(Hex.fromBytes(Bytes.slice(value, -20))), 32]
186
+ // A view, not a copy: it is hex-encoded at once and never escapes.
187
+ return [wrap(Hex.fromBytes(value.subarray(12))), 32]
192
188
  }
193
189
 
194
190
  export declare namespace decodeAddress {
195
191
  type ErrorType =
196
192
  | Address.InvalidAddressError
197
193
  | Hex.fromBytes.ErrorType
198
- | Bytes.slice.ErrorType
199
194
  | Errors.GlobalErrorType
200
195
  }
201
196
 
@@ -228,6 +223,7 @@ export function decodeArray(
228
223
 
229
224
  // Check if the array has any dynamic children.
230
225
  const dynamicChild = hasDynamicChild(param)
226
+ const decode = getDecoder(param, checksumAddress)
231
227
 
232
228
  let consumed = 0
233
229
  const value: unknown[] = []
@@ -235,10 +231,7 @@ export function decodeArray(
235
231
  // If any of the children is dynamic, then all elements will be offset pointer, thus size of one slot (32 bytes).
236
232
  // Otherwise, elements will be the size of their encoding (consumed bytes).
237
233
  cursor.setPosition(startOfData + (dynamicChild ? i * 32 : consumed))
238
- const [data, consumed_] = decodeParameter(cursor, param, {
239
- checksumAddress,
240
- staticPosition: startOfData,
241
- })
234
+ const [data, consumed_] = decode(cursor, startOfData)
242
235
  consumed += consumed_
243
236
  value.push(data)
244
237
  // Charge zero-width elements against the read limit to bound work
@@ -264,14 +257,12 @@ export function decodeArray(
264
257
  // Start is the static position of current slot + offset.
265
258
  const start = staticPosition + offset
266
259
 
260
+ const decode = getDecoder(param, checksumAddress)
267
261
  const value: unknown[] = []
268
262
  for (let i = 0; i < length; ++i) {
269
263
  // Move cursor along to the next slot (next offset pointer).
270
264
  cursor.setPosition(start + i * 32)
271
- const [data] = decodeParameter(cursor, param, {
272
- checksumAddress,
273
- staticPosition: start,
274
- })
265
+ const [data] = decode(cursor, start)
275
266
  value.push(data)
276
267
  }
277
268
 
@@ -285,13 +276,11 @@ export function decodeArray(
285
276
 
286
277
  // If the length of the array is known in advance and the array is deeply static,
287
278
  // then we can just decode each element in sequence.
279
+ const decode = getDecoder(param, checksumAddress)
288
280
  let consumed = 0
289
281
  const value: unknown[] = []
290
282
  for (let i = 0; i < length; ++i) {
291
- const [data, consumed_] = decodeParameter(cursor, param, {
292
- checksumAddress,
293
- staticPosition: staticPosition + consumed,
294
- })
283
+ const [data, consumed_] = decode(cursor, staticPosition + consumed)
295
284
  consumed += consumed_
296
285
  value.push(data)
297
286
  // Charge zero-width elements against the read limit to bound work
@@ -349,11 +338,21 @@ export function decodeBytes(
349
338
 
350
339
  // Read the whole slot so that a slot cut short of its padding is out of bounds.
351
340
  const word = cursor.readBytes(32)
352
- return [Hex.fromBytes(word.subarray(0, Number.parseInt(size, 10))), 32]
341
+ const size_ = Number.parseInt(size, 10)
342
+ // The value occupies the high `size` bytes of the word; the rest must be
343
+ // zero padding.
344
+ for (let i = size_; i < 32; i++)
345
+ if (word[i] !== 0)
346
+ throw new AbiParameters.BytesSizeMismatchError({
347
+ expectedSize: size_,
348
+ value: Hex.fromBytes(word),
349
+ })
350
+ return [Hex.fromBytes(word.subarray(0, size_)), 32]
353
351
  }
354
352
 
355
353
  export declare namespace decodeBytes {
356
354
  type ErrorType =
355
+ | AbiParameters.BytesSizeMismatchError
357
356
  | Hex.fromBytes.ErrorType
358
357
  | Bytes.toNumber.ErrorType
359
358
  | Errors.GlobalErrorType
@@ -364,24 +363,7 @@ export function decodeNumber(
364
363
  cursor: Cursor.Cursor,
365
364
  param: AbiParameters.Parameter,
366
365
  ) {
367
- const signed = param.type.startsWith('int')
368
- const size = Number.parseInt(param.type.split('int')[1] || '256', 10)
369
- const value = Bytes.toBigInt(cursor.readBytes(32), { signed })
370
- // The word must hold a value that fits the declared size (zero-padded for
371
- // unsigned, sign-extended for signed types).
372
- if (size < 256) {
373
- const max = (1n << BigInt(signed ? size - 1 : size)) - 1n
374
- const min = signed ? -max - 1n : 0n
375
- if (value > max || value < min)
376
- throw new Hex.IntegerOutOfRangeError({
377
- max: max.toString(),
378
- min: min.toString(),
379
- signed,
380
- size: size / 8,
381
- value: value.toString(),
382
- })
383
- }
384
- return [size > 48 ? value : Number(value), 32]
366
+ return decodeInteger(cursor, getInteger(param.type))
385
367
  }
386
368
 
387
369
  export declare namespace decodeNumber {
@@ -391,6 +373,65 @@ export declare namespace decodeNumber {
391
373
  | Errors.GlobalErrorType
392
374
  }
393
375
 
376
+ type Integer = {
377
+ max: bigint
378
+ min: bigint
379
+ signed: boolean
380
+ size: number
381
+ }
382
+
383
+ /** Parses an integer type and its bounds, once per type rather than per value. */
384
+ function getInteger(type: string): Integer {
385
+ const signed = type.startsWith('int')
386
+ const size = Number.parseInt(type.split('int')[1] || '256', 10)
387
+ if (!(size < 256)) return { max: 0n, min: 0n, signed, size }
388
+ const max = (1n << BigInt(signed ? size - 1 : size)) - 1n
389
+ return { max, min: signed ? -max - 1n : 0n, signed, size }
390
+ }
391
+
392
+ function decodeInteger(cursor: Cursor.Cursor, integer: Integer) {
393
+ const { max, min, signed, size } = integer
394
+ const value = Bytes.toBigInt(cursor.readBytes(32), { signed })
395
+ // The word must hold a value that fits the declared size (zero-padded for
396
+ // unsigned, sign-extended for signed types).
397
+ if (size < 256 && (value > max || value < min))
398
+ throw new Hex.IntegerOutOfRangeError({
399
+ max: max.toString(),
400
+ min: min.toString(),
401
+ signed,
402
+ size: size / 8,
403
+ value: value.toString(),
404
+ })
405
+ return [size > 48 ? value : Number(value), 32]
406
+ }
407
+
408
+ /**
409
+ * Resolves how to decode a parameter once, so that the elements of an array
410
+ * do not each re-parse its type. Types other than `address`, `bool` and
411
+ * integers go through `decodeParameter`.
412
+ */
413
+ function getDecoder(
414
+ param: AbiParameters.Parameter,
415
+ checksumAddress: boolean | undefined,
416
+ ): (
417
+ cursor: Cursor.Cursor,
418
+ staticPosition: number,
419
+ ) => ReturnType<typeof decodeParameter> {
420
+ const { type } = param
421
+ if (type === 'address')
422
+ return (cursor) => decodeAddress(cursor, { checksum: checksumAddress })
423
+ if (type === 'bool') return decodeBool
424
+ if (
425
+ (type.startsWith('uint') || type.startsWith('int')) &&
426
+ !getArrayComponents(type)
427
+ ) {
428
+ const integer = getInteger(type)
429
+ return (cursor) => decodeInteger(cursor, integer)
430
+ }
431
+ return (cursor, staticPosition) =>
432
+ decodeParameter(cursor, param, { checksumAddress, staticPosition })
433
+ }
434
+
394
435
  /** @internal */
395
436
  export type TupleAbiParameter = AbiParameters.Parameter & {
396
437
  components: readonly AbiParameters.Parameter[]
@@ -521,21 +562,13 @@ export function encodeParameters(
521
562
  const bytes = new Uint8Array(prefixSize + size)
522
563
  const writer: WriteContext = {
523
564
  bytes,
524
- patches: [],
525
565
  view: new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength),
526
566
  }
527
567
 
528
568
  if (prefix) writeHex(writer, 0, prefix)
529
569
  writeParameters(writer, prefixSize, compiled, values)
530
570
 
531
- let data = Hex.fromBytes(bytes)
532
- for (let i = 0; i < writer.patches.length; i++) {
533
- const patch = writer.patches[i]!
534
- const start = 2 + patch.offset * 2
535
- data =
536
- `${data.slice(0, start)}${patch.value}${data.slice(start + patch.value.length)}` as Hex.Hex
537
- }
538
- return data
571
+ return Hex.fromBytes(bytes)
539
572
  }
540
573
 
541
574
  export declare namespace encodeParameters {
@@ -552,6 +585,7 @@ export declare namespace encodeParameters {
552
585
  | AbiParameters.InvalidTypeError
553
586
  | Address.assert.ErrorType
554
587
  | Hex.IntegerOutOfRangeError
588
+ | Hex.InvalidHexValueError
555
589
  | Hex.InvalidLengthError
556
590
  | Hex.fromBytes.ErrorType
557
591
  | Errors.GlobalErrorType
@@ -808,18 +842,6 @@ function normalizeString(value: unknown): string {
808
842
  return `${value}`
809
843
  }
810
844
 
811
- function isWritableHex(value: Hex.Hex): boolean {
812
- if (
813
- typeof value !== 'string' ||
814
- value.charCodeAt(0) !== 48 ||
815
- value.charCodeAt(1) !== 120
816
- )
817
- return false
818
- for (let i = 2; i < value.length; i++)
819
- if (toNibble(value.charCodeAt(i)) === -1) return false
820
- return true
821
- }
822
-
823
845
  function writeParameters(
824
846
  context: WriteContext,
825
847
  offset: number,
@@ -922,18 +944,36 @@ function writeHex(
922
944
  offset: number,
923
945
  value: Hex.Hex,
924
946
  ): number {
925
- if (!isWritableHex(value)) {
926
- const patch = value.slice(2)
927
- context.patches.push({ offset, value: patch })
928
- return offset + Math.ceil(patch.length / 2)
929
- }
930
-
947
+ if (
948
+ typeof value !== 'string' ||
949
+ value.charCodeAt(0) !== 48 ||
950
+ value.charCodeAt(1) !== 120
951
+ )
952
+ throw new Hex.InvalidHexValueError(value)
953
+
954
+ // Validate and write in one pass. A valid nibble is at most `0x0f`, so only
955
+ // the `0xff` sentinel sets bit 7; one check after the loop covers every
956
+ // character. `nibbleTable` only spans Latin-1, so a code unit above it reads
957
+ // `undefined` and is mapped to the sentinel.
958
+ const { bytes } = context
959
+ // A local reference: some module transforms turn every read of an imported
960
+ // binding into a property lookup, which would land in the hot loop.
961
+ const table = nibbleTable
962
+ const length = value.length
963
+ let invalid = 0
931
964
  let i = 2
932
- for (; i + 1 < value.length; i += 2)
933
- context.bytes[offset++] =
934
- (toNibble(value.charCodeAt(i)) << 4) | toNibble(value.charCodeAt(i + 1))
935
- if (i < value.length)
936
- context.bytes[offset++] = toNibble(value.charCodeAt(i)) << 4
965
+ for (; i + 1 < length; i += 2) {
966
+ const hi = table[value.charCodeAt(i)] ?? 0xff
967
+ const lo = table[value.charCodeAt(i + 1)] ?? 0xff
968
+ invalid |= hi | lo
969
+ bytes[offset++] = (hi << 4) | lo
970
+ }
971
+ if (i < length) {
972
+ const hi = table[value.charCodeAt(i)] ?? 0xff
973
+ invalid |= hi
974
+ bytes[offset++] = hi << 4
975
+ }
976
+ if (invalid & 0x80) throw new Hex.InvalidHexValueError(value)
937
977
  return offset
938
978
  }
939
979
 
@@ -952,25 +992,22 @@ function writeWord(
952
992
  }
953
993
 
954
994
  const hex = value_.toString(16)
995
+ const table = nibbleTable
955
996
  let cursor = offset + 32 - Math.ceil(hex.length / 2)
956
997
  let i = hex.length % 2
957
- if (i === 1) context.bytes[cursor++] = toNibble(hex.charCodeAt(0))
998
+ if (i === 1) context.bytes[cursor++] = table[hex.charCodeAt(0)]!
958
999
  for (; i < hex.length; i += 2)
959
1000
  context.bytes[cursor++] =
960
- (toNibble(hex.charCodeAt(i)) << 4) | toNibble(hex.charCodeAt(i + 1))
961
- }
962
-
963
- function toNibble(code: number): number {
964
- if (code >= 48 && code <= 57) return code - 48
965
- if (code >= 65 && code <= 70) return code - 55
966
- if (code >= 97 && code <= 102) return code - 87
967
- return -1
1001
+ (table[hex.charCodeAt(i)]! << 4) | table[hex.charCodeAt(i + 1)]!
968
1002
  }
969
1003
 
970
1004
  /** @internal */
971
1005
  export function getArrayComponents(
972
1006
  type: string,
973
1007
  ): [length: number | null, innerType: string] | undefined {
1008
+ // Only a type ending in `]` can match, so skip the regex for the element
1009
+ // types (`uint256`, `address`, ...) that decoding checks once per value.
1010
+ if (type.charCodeAt(type.length - 1) !== 93 /* ] */) return undefined
974
1011
  const matches = arraySuffixRegex.exec(type)
975
1012
  return matches
976
1013
  ? // Return `null` if the array is dynamic.
@@ -84,11 +84,17 @@ export function from(value: Hex.Hex | Bytes.Bytes) {
84
84
  ? BigInt(Hex.fromBytes(bytes))
85
85
  : BigInt(value as string)
86
86
 
87
+ // Divide by 58^9, the largest power of 58 below 2^53, so each BigInt
88
+ // division yields nine digits that number arithmetic can split.
87
89
  let result = ''
88
90
  while (integer > 0n) {
89
- const remainder = Number(integer % 58n)
90
- integer = integer / 58n
91
- result = integerToAlphabet[remainder] + result
91
+ let remainder = Number(integer % 7_427_658_739_644_928n)
92
+ integer /= 7_427_658_739_644_928n
93
+ // Every chunk but the most significant one is zero-padded to nine digits.
94
+ for (let i = 0; i < 9 && (remainder > 0 || integer > 0n); i++) {
95
+ result = integerToAlphabet[remainder % 58] + result
96
+ remainder = Math.floor(remainder / 58)
97
+ }
92
98
  }
93
99
 
94
100
  return '1'.repeat(pad) + result
@@ -1,6 +1,6 @@
1
1
  import type * as Address from '../Address.js'
2
2
  import type * as Hex from '../Hex.js'
3
- import { BoundedMap } from './lru.js'
3
+ import { BoundedMap, weakCache } from './lru.js'
4
4
 
5
5
  /**
6
6
  * Global caches, cleared together by `Caches.clear` and on every engine change.
@@ -9,4 +9,6 @@ import { BoundedMap } from './lru.js'
9
9
  export const caches = {
10
10
  checksum: /*#__PURE__*/ new BoundedMap<Address.Address>(32_768),
11
11
  signatureHash: /*#__PURE__*/ new BoundedMap<Hex.Hex>(8_192),
12
+ typeHash: /*#__PURE__*/ new BoundedMap<Hex.Hex>(1_024),
13
+ signatureHashByItem: /*#__PURE__*/ weakCache<object, Hex.Hex>(),
12
14
  }
@@ -8,8 +8,12 @@ const hexes = /*#__PURE__*/ Array.from({ length: 256 }, (_v, i) =>
8
8
  i.toString(16).padStart(2, '0'),
9
9
  )
10
10
 
11
- // Char-code → 4-bit nibble lookup table. Sentinel `0xff` = invalid.
12
- const nibbleTable = /*#__PURE__*/ (() => {
11
+ /**
12
+ * Char-code → 4-bit nibble lookup table. Sentinel `0xff` = invalid.
13
+ *
14
+ * @internal
15
+ */
16
+ export const nibbleTable = /*#__PURE__*/ (() => {
13
17
  const table = new Uint8Array(256).fill(0xff)
14
18
  for (let i = 0; i < 10; i++) table[48 + i] = i // '0'-'9'
15
19
  for (let i = 0; i < 6; i++) {
@@ -179,6 +183,27 @@ export function hexToBytesLoop(value: string, length: number): Uint8Array {
179
183
  return out
180
184
  }
181
185
 
186
+ /**
187
+ * Throws exactly what {@link hexToBytes} throws for `value`, without decoding
188
+ * it, for callers that read the hex string directly.
189
+ *
190
+ * @internal
191
+ */
192
+ export function assertHexBytes(value: string): void {
193
+ if (
194
+ typeof value !== 'string' ||
195
+ value.length < 2 ||
196
+ value.charCodeAt(0) !== 48 /* '0' */ ||
197
+ value.charCodeAt(1) !== 120 /* 'x' */
198
+ )
199
+ throw new InvalidHexValueError(value)
200
+ if (((value.length - 2) & 1) !== 0) throw new InvalidLengthError(value as Hex)
201
+ // A regular expression scans about twice as fast as a lookup-table loop.
202
+ if (!hexRegex.test(value)) throw new InvalidHexValueError(value)
203
+ }
204
+
205
+ const hexRegex = /^0x[0-9a-fA-F]*$/
206
+
182
207
  /** @internal */
183
208
  export function charCodeToBase16(char: number): number | undefined {
184
209
  const v = nibbleTable[char] ?? 0xff
@@ -1,6 +1,15 @@
1
1
  import * as Errors from '../../Errors.js'
2
2
  import { bytesToHex } from './hex.js'
3
3
 
4
+ /**
5
+ * Significant byte count up to which {@link bytesToBigInt} shifts 8-byte chunks
6
+ * into the result. Each shift copies the result so far, so the loop is
7
+ * quadratic, and parsing hex overtakes it past 64 bytes on Node 24 and Bun.
8
+ *
9
+ * @internal
10
+ */
11
+ const shiftMaxBytes = 64
12
+
4
13
  /**
5
14
  * Decodes a big-endian unsigned `Uint8Array` into a `bigint`. When `signed` is
6
15
  * true, applies twos-complement on the high bit.
@@ -10,36 +19,35 @@ import { bytesToHex } from './hex.js'
10
19
  export function bytesToBigInt(value: Uint8Array, signed?: boolean): bigint {
11
20
  const len = value.length
12
21
  if (len === 0) return 0n
13
-
14
- // Fast path: unsigned big-endian read using DataView for 8-byte chunks,
15
- // skipping the hex round-trip.
16
- if (!signed) {
17
- let val = 0n
18
- let i = 0
19
- if (len >= 8) {
20
- const view = new DataView(value.buffer, value.byteOffset, len)
21
- const tail = len & 7
22
- const fullEnd = len - tail
23
- while (i < fullEnd) {
24
- val = (val << 64n) | view.getBigUint64(i, false)
25
- i += 8
26
- }
27
- }
28
- while (i < len) {
29
- val = (val << 8n) | BigInt(value[i]!)
30
- i++
22
+ // Two's complement of the unsigned value, without the hex round-trip.
23
+ if (signed) return BigInt.asIntN(len * 8, bytesToBigInt(value))
24
+
25
+ // Skip leading zeros first: ABI words are 32 bytes but usually hold small
26
+ // values, which then fit a Number and skip the BigInt arithmetic.
27
+ let i = 0
28
+ while (i < len && value[i] === 0) i++
29
+ if (len - i <= 6) {
30
+ let n = 0
31
+ for (; i < len; i++) n = n * 256 + value[i]!
32
+ return BigInt(n)
33
+ }
34
+ if (len - i > shiftMaxBytes) return BigInt(bytesToHex(value))
35
+
36
+ // Unsigned big-endian read using DataView for 8-byte chunks.
37
+ let val = 0n
38
+ const fullEnd = len - ((len - i) & 7)
39
+ if (i < fullEnd) {
40
+ const view = new DataView(value.buffer, value.byteOffset, len)
41
+ while (i < fullEnd) {
42
+ val = (val << 64n) | view.getBigUint64(i, false)
43
+ i += 8
31
44
  }
32
- return val
33
45
  }
34
-
35
- // Signed: BigInt parses hex strings faster than the per-byte loop for
36
- // > 8-byte inputs in V8/JSC, so route through `bytesToHex`.
37
- const hex = bytesToHex(value)
38
- const n = hex === '0x' ? 0n : BigInt(hex)
39
- const max_unsigned = 1n << (BigInt(len) * 8n)
40
- const max_signed = max_unsigned >> 1n
41
- if (n < max_signed) return n
42
- return n - max_unsigned
46
+ while (i < len) {
47
+ val = (val << 8n) | BigInt(value[i]!)
48
+ i++
49
+ }
50
+ return val
43
51
  }
44
52
 
45
53
  /**
@@ -49,11 +57,16 @@ export function bytesToBigInt(value: Uint8Array, signed?: boolean): bigint {
49
57
  * @internal
50
58
  */
51
59
  export function bytesToSafeNumber(value: Uint8Array, signed?: boolean): number {
52
- if (!signed && value.length <= 6) {
60
+ if (!signed) {
53
61
  // Small unsigned values fit in a number directly without BigInt round-trip.
54
- let n = 0
55
- for (let i = 0; i < value.length; i++) n = n * 256 + value[i]!
56
- return n
62
+ const length = value.length
63
+ let i = 0
64
+ while (i < length && value[i] === 0) i++
65
+ if (length - i <= 6) {
66
+ let n = 0
67
+ for (; i < length; i++) n = n * 256 + value[i]!
68
+ return n
69
+ }
57
70
  }
58
71
  const big = bytesToBigInt(value, signed)
59
72
  const num = Number(big)
@@ -4,10 +4,18 @@ import * as Errors from '../Errors.js'
4
4
  /** @internal */
5
5
  export type Cursor = {
6
6
  bytes: Bytes
7
- dataView: DataView
7
+ /** Lazy: created on first access, since most cursors never use it. */
8
+ readonly dataView: DataView
9
+ /** Backing field of `dataView`. */
10
+ _dataView: DataView | undefined
8
11
  position: number
9
- /** Lazy: only allocated when slow-path tracking kicks in. */
10
- positionReadCount: Map<number, number> | undefined
12
+ /**
13
+ * Positions read so far, so a repeated read can be counted. Word-aligned
14
+ * positions, where ABI data is read, go in a bit set with one bit per 32
15
+ * bytes; other positions go in a set. Both are allocated on first use.
16
+ */
17
+ alignedReads: Uint8Array | undefined
18
+ unalignedReads: Set<number> | undefined
11
19
  /** Total number of bytes read. */
12
20
  readSize: number
13
21
  /** Maximum total number of bytes that can be read. */
@@ -19,7 +27,6 @@ export type Cursor = {
19
27
  assertReadSize(length: number): void
20
28
  assertPosition(position: number): void
21
29
  decrementPosition(offset: number): void
22
- getReadCount(position?: number): number
23
30
  incrementPosition(offset: number): void
24
31
  inspectByte(position?: number): Bytes[number]
25
32
  inspectBytes(length: number, position?: number): Bytes
@@ -45,9 +52,17 @@ export type Cursor = {
45
52
 
46
53
  const staticCursor: Cursor = {
47
54
  bytes: new Uint8Array(),
48
- dataView: new DataView(new ArrayBuffer(0)),
55
+ get dataView() {
56
+ return (this._dataView ??= new DataView(
57
+ this.bytes.buffer,
58
+ this.bytes.byteOffset,
59
+ this.bytes.byteLength,
60
+ ))
61
+ },
62
+ _dataView: undefined,
49
63
  position: 0,
50
- positionReadCount: undefined,
64
+ alignedReads: undefined,
65
+ unalignedReads: undefined,
51
66
  readSize: 0,
52
67
  readSizeLimit: Number.POSITIVE_INFINITY,
53
68
  recursiveReadCount: 0,
@@ -80,9 +95,6 @@ const staticCursor: Cursor = {
80
95
  this.assertPosition(position)
81
96
  this.position = position
82
97
  },
83
- getReadCount(position) {
84
- return this.positionReadCount?.get(position || this.position) || 0
85
- },
86
98
  incrementPosition(offset) {
87
99
  if (offset < 0) throw new NegativeOffsetError({ offset })
88
100
  const position = this.position + offset
@@ -212,10 +224,29 @@ const staticCursor: Cursor = {
212
224
  },
213
225
  _touch() {
214
226
  if (this.recursiveReadLimit === Number.POSITIVE_INFINITY) return
215
- const map = (this.positionReadCount ??= new Map())
216
- const count = map.get(this.position) || 0
217
- map.set(this.position, count + 1)
218
- if (count > 0) this.recursiveReadCount++
227
+ const { position } = this
228
+ if (position % 32 !== 0 || position > this.bytes.length) {
229
+ const reads = (this.unalignedReads ??= new Set())
230
+ if (reads.has(position)) this.recursiveReadCount++
231
+ else reads.add(position)
232
+ return
233
+ }
234
+ // One bit per 32-byte word, so the set is 1/256 the size of the data. It
235
+ // is sized from the data's length at the time of the read, so a resizable
236
+ // buffer that grows while decoding still has every read counted.
237
+ const word = position / 32
238
+ const index = Math.floor(word / 8)
239
+ let reads = this.alignedReads
240
+ if (!reads || index >= reads.length) {
241
+ const grown = new Uint8Array(
242
+ Math.max(Math.floor(this.bytes.length / 256) + 1, index + 1),
243
+ )
244
+ if (reads) grown.set(reads)
245
+ reads = this.alignedReads = grown
246
+ }
247
+ const bit = 1 << (word & 7)
248
+ if (reads[index]! & bit) this.recursiveReadCount++
249
+ else reads[index]! |= bit
219
250
  },
220
251
  }
221
252
 
@@ -226,17 +257,15 @@ export function create(
226
257
  ): Cursor {
227
258
  const cursor: Cursor = Object.create(staticCursor)
228
259
  cursor.bytes = bytes
229
- cursor.dataView = new DataView(
230
- bytes.buffer,
231
- bytes.byteOffset,
232
- bytes.byteLength,
233
- )
234
- cursor.positionReadCount = undefined
260
+ cursor._dataView = undefined
261
+ cursor.alignedReads = undefined
262
+ cursor.unalignedReads = undefined
235
263
  cursor.recursiveReadLimit = recursiveReadLimit
236
- // `recursiveReadLimit` allows one read per position plus that many repeated
237
- // reads. Allow as many bytes as that number of 32-byte reads, so that reads
238
- // of any length are bounded by the size of the data as well.
239
- cursor.readSizeLimit = 32 * (bytes.length + recursiveReadLimit)
264
+ // A well-formed payload reads each byte once. Allow twice that, plus as many
265
+ // bytes as `recursiveReadLimit` repeated 32-byte reads (256 KiB by default),
266
+ // so that offsets pointing many times at one region can add at most that
267
+ // fixed amount to the decoded output, whatever the size of the data.
268
+ cursor.readSizeLimit = 2 * bytes.length + 32 * recursiveReadLimit
240
269
  return cursor
241
270
  }
242
271