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/Rlp.ts CHANGED
@@ -2,12 +2,20 @@ import * as Bytes from './Bytes.js'
2
2
  import * as Errors from './Errors.js'
3
3
  import * as Hex from './Hex.js'
4
4
  import * as internal_bytes from './internal/bytes.js'
5
+ import { assertHexBytes, nibbleTable } from './internal/codec/hex.js'
5
6
  import * as Cursor from './internal/cursor.js'
6
7
  import type { ExactPartial, RecursiveArray } from './internal/types.js'
7
8
 
8
9
  /** Maximum nesting depth permitted when decoding an RLP value. */
9
10
  const depthLimit = 1_024
10
11
 
12
+ /**
13
+ * Hex input length up to which decoded hex leaves are sliced from the input. A
14
+ * sliced string keeps its whole parent alive, so this bounds what one kept leaf
15
+ * can retain.
16
+ */
17
+ const sliceMaxLength = 4_096
18
+
11
19
  /**
12
20
  * Decodes a Recursive-Length Prefix (RLP) value into a {@link ox#Bytes.Bytes} value.
13
21
  *
@@ -63,6 +71,16 @@ export function to<
63
71
  >(value: value, to: to | 'Hex' | 'Bytes'): to.ReturnType<to> {
64
72
  const to_ = to ?? (typeof value === 'string' ? 'Hex' : 'Bytes')
65
73
 
74
+ // Hex in and out: read a short input straight from the string, skipping the
75
+ // byte copy. A long input pays a JavaScript scan to validate it, which costs
76
+ // more than `Bytes.fromHex`, and leaves sliced from it would keep it alive.
77
+ if (
78
+ typeof value === 'string' &&
79
+ to_ === 'Hex' &&
80
+ value.length <= sliceMaxLength
81
+ )
82
+ return decodeHex(value) as to.ReturnType<to>
83
+
66
84
  const bytes =
67
85
  typeof value === 'string' ? Bytes.fromHex(value) : (value as Bytes.Bytes)
68
86
 
@@ -155,7 +173,10 @@ export declare namespace decodeRlpCursor {
155
173
 
156
174
  /** @internal */
157
175
  export function readLength(
158
- cursor: Cursor.Cursor,
176
+ cursor: Pick<
177
+ Cursor.Cursor,
178
+ 'readUint8' | 'readUint16' | 'readUint24' | 'readUint32'
179
+ >,
159
180
  prefix: number,
160
181
  offset: number,
161
182
  ) {
@@ -233,6 +254,123 @@ export declare namespace readList {
233
254
  | Errors.GlobalErrorType
234
255
  }
235
256
 
257
+ type HexReader = {
258
+ /** Byte length of the input. */
259
+ length: number
260
+ /** Position in bytes. */
261
+ position: number
262
+ readUint8(): number
263
+ readUint16(): number
264
+ readUint24(): number
265
+ readUint32(): number
266
+ /** Lowercase hex input. */
267
+ source: string
268
+ }
269
+
270
+ /**
271
+ * Decodes a hex RLP value straight from its string, slicing leaves out of it.
272
+ * Mirrors `Bytes.fromHex` followed by {@link decodeRlpCursor}, including the
273
+ * cursor's bounds errors.
274
+ *
275
+ * @internal
276
+ */
277
+ function decodeHex(value: Hex.Hex): RecursiveArray<Hex.Hex> {
278
+ assertHexBytes(value)
279
+ const length = (value.length - 2) / 2
280
+ if (length === 0) return '0x'
281
+ const reader: HexReader = {
282
+ length,
283
+ position: 0,
284
+ readUint8() {
285
+ return readHexUint(this, 1)
286
+ },
287
+ readUint16() {
288
+ return readHexUint(this, 2)
289
+ },
290
+ readUint24() {
291
+ return readHexUint(this, 3)
292
+ },
293
+ readUint32() {
294
+ return readHexUint(this, 4)
295
+ },
296
+ source: value.toLowerCase(),
297
+ }
298
+ const result = decodeHexItem(reader, 0)
299
+ // RLP payloads encode exactly one item (Yellow Paper, Appendix B).
300
+ if (reader.position < length)
301
+ throw new TrailingBytesError({ count: length - reader.position })
302
+ return result
303
+ }
304
+
305
+ function decodeHexItem(
306
+ reader: HexReader,
307
+ depth: number,
308
+ ): RecursiveArray<Hex.Hex> {
309
+ if (depth >= depthLimit)
310
+ throw new DepthLimitExceededError({ limit: depthLimit })
311
+
312
+ const prefix = reader.readUint8()
313
+ if (prefix < 0x80) reader.position--
314
+
315
+ // bytes
316
+ if (prefix < 0xc0) {
317
+ const length = readLength(reader, prefix, 0x80)
318
+ const start = reader.position
319
+ assertHexPosition(reader, start + length - 1)
320
+ reader.position += length
321
+ const first = length === 1 ? readSourceByte(reader.source, start) : 0
322
+ // See `decodeRlpCursor`.
323
+ if (prefix >= 0x80 && length === 1 && first < 0x80)
324
+ throw new NonCanonicalError({
325
+ reason: `single byte \`0x${first.toString(16).padStart(2, '0')}\` is wrapped in an explicit length prefix instead of being encoded as itself`,
326
+ })
327
+ return `0x${reader.source.slice(2 + start * 2, 2 + reader.position * 2)}`
328
+ }
329
+
330
+ // list
331
+ const length = readLength(reader, prefix, 0xc0)
332
+ const position = reader.position
333
+ const value: RecursiveArray<Hex.Hex>[] = []
334
+ while (reader.position - position < length)
335
+ value.push(decodeHexItem(reader, depth + 1))
336
+ // Items must consume exactly the declared list length.
337
+ if (reader.position - position !== length)
338
+ throw new ListBoundaryExceededError({
339
+ consumed: reader.position - position,
340
+ declared: length,
341
+ })
342
+ return value as RecursiveArray<Hex.Hex>
343
+ }
344
+
345
+ /**
346
+ * Throws the cursor's error for reading past the end of the input.
347
+ *
348
+ * @internal
349
+ */
350
+ function assertHexPosition(reader: HexReader, position: number) {
351
+ if (position > reader.length - 1)
352
+ throw new Cursor.PositionOutOfBoundsError({
353
+ length: reader.length,
354
+ position,
355
+ })
356
+ }
357
+
358
+ function readHexUint(reader: HexReader, size: number): number {
359
+ assertHexPosition(reader, reader.position + size - 1)
360
+ let value = 0
361
+ for (let i = 0; i < size; i++)
362
+ value = value * 256 + readSourceByte(reader.source, reader.position++)
363
+ return value
364
+ }
365
+
366
+ function readSourceByte(source: string, position: number): number {
367
+ const i = 2 + position * 2
368
+ return (
369
+ (nibbleTable[source.charCodeAt(i)]! << 4) |
370
+ nibbleTable[source.charCodeAt(i + 1)]!
371
+ )
372
+ }
373
+
236
374
  /**
237
375
  * Encodes a value as Recursive-Length Prefix (RLP) and writes the encoded bytes
238
376
  * through a synchronous callback without allocating the complete encoding.
@@ -60,6 +60,53 @@ const eip7702Tuple = [
60
60
 
61
61
  const eip7702TupleHex = Rlp.fromHex(eip7702Tuple as never, { as: 'Hex' })
62
62
 
63
+ // Legacy (EIP-155) tuple: 9 elements.
64
+ const legacyTuple = [
65
+ '0x09',
66
+ '0x04a817c800',
67
+ '0x5208',
68
+ '0x3535353535353535353535353535353535353535',
69
+ '0x0de0b6b3a7640000',
70
+ '0x',
71
+ '0x25',
72
+ '0xa0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0',
73
+ '0xb0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0',
74
+ ] as const
75
+
76
+ const legacyTupleHex = Rlp.fromHex(legacyTuple as never, { as: 'Hex' })
77
+
78
+ // EIP-4844 network wrapper with 3 blobs, their commitments and proofs.
79
+ const blob = (i: number) =>
80
+ `0x${i.toString(16).padStart(2, '0').repeat(131_072)}` as const
81
+ const kzg = (i: number) =>
82
+ `0x${i.toString(16).padStart(2, '0').repeat(48)}` as const
83
+ const eip4844SidecarsHex = Rlp.fromHex(
84
+ [
85
+ [
86
+ '0x01',
87
+ '0x10',
88
+ '0x3b9aca00',
89
+ '0x6fc23ac0',
90
+ '0x5208',
91
+ '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
92
+ '0x0de0b6b3a7640000',
93
+ '0x',
94
+ [],
95
+ '0x3b9aca00',
96
+ [1, 2, 3].map(
97
+ (i) => `0x01${i.toString(16).padStart(2, '0').repeat(31)}` as const,
98
+ ),
99
+ '0x01',
100
+ '0xa0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0',
101
+ '0xb0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0',
102
+ ],
103
+ [1, 2, 3].map(blob),
104
+ [1, 2, 3].map(kzg),
105
+ [4, 5, 6].map(kzg),
106
+ ] as never,
107
+ { as: 'Hex' },
108
+ )
109
+
63
110
  describe('Rlp.fromHex (transaction-shaped)', () => {
64
111
  bench('eip1559 tuple', () => {
65
112
  Rlp.fromHex(eip1559Tuple as never, { as: 'Hex' })
@@ -71,6 +118,14 @@ describe('Rlp.fromHex (transaction-shaped)', () => {
71
118
  })
72
119
 
73
120
  describe('Rlp.toHex (transaction-shaped)', () => {
121
+ bench('legacy tuple', () => {
122
+ Rlp.toHex(legacyTupleHex)
123
+ })
124
+
125
+ bench('eip4844 network wrapper (3 blobs)', () => {
126
+ Rlp.toHex(eip4844SidecarsHex)
127
+ })
128
+
74
129
  bench('eip1559 tuple', () => {
75
130
  Rlp.toHex(eip1559TupleHex)
76
131
  })
@@ -218,7 +218,9 @@ export class HttpError extends Errors.BaseError {
218
218
  metaMessages: [
219
219
  `Status: ${response.status}`,
220
220
  `URL: ${getUrl(url)}`,
221
- body ? `Body: ${JSON.stringify(body)}` : undefined,
221
+ body
222
+ ? `Body: ${typeof body === 'string' ? body : JSON.stringify(body)}`
223
+ : undefined,
222
224
  ],
223
225
  })
224
226
  }
package/src/core/Siwe.ts CHANGED
@@ -13,11 +13,10 @@ export const localhostRegex = /^localhost(:[0-9]{1,5})?$/
13
13
 
14
14
  export const nonceRegex = /^[a-zA-Z0-9]{8,}$/
15
15
 
16
- export const schemeRegex = /^([a-zA-Z][a-zA-Z0-9+-.]*)$/
16
+ export const schemeRegex = /^([a-zA-Z][a-zA-Z0-9+\-.]*)$/
17
17
 
18
- // https://regexr.com/80gdj
19
18
  export const prefixRegex =
20
- /^(?:(?<scheme>[a-zA-Z][a-zA-Z0-9+-.]*):\/\/)?(?<domain>[a-zA-Z0-9+-.]*(?::[0-9]{1,5})?) (?:wants you to sign in with your Ethereum account:\n)(?<address>0x[a-fA-F0-9]{40})\n\n(?:(?<statement>.*)\n\n)?/
19
+ /^(?:(?<scheme>[a-zA-Z][a-zA-Z0-9+\-.]*):\/\/)?(?<domain>(?:\[[^\]\s@[]+\]|[a-zA-Z0-9+-.]*)(?::[0-9]{1,5})?) (?:wants you to sign in with your Ethereum account:\n)(?<address>0x[a-fA-F0-9]{40})\n\n(?:(?<statement>.*)\n\n)?/
21
20
 
22
21
  // https://regexr.com/80gf9
23
22
  export const suffixRegex =
@@ -29,6 +28,14 @@ const anchoredSuffixRegex = new RegExp(`^(?:${suffixRegex.source})$`)
29
28
  const siweDateTimeRegex =
30
29
  /^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])[Tt]([01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:[Zz]|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/
31
30
 
31
+ // `parseMessage` reads the statement with `.`, which stops at these.
32
+ const statementLineBreakRegex = /[\n\r\u2028\u2029]/
33
+
34
+ // An IP literal (IPv6 or IPvFuture) `domain` with an optional port. The
35
+ // literal itself is checked by `isAuthority`. `@` and `[` are excluded so a
36
+ // userinfo authority such as `[evil.com@[::1]` cannot pass as a literal.
37
+ const domainIpLiteralRegex = /^\[[^\]\s@[]+\](?::[0-9]{1,5})?$/
38
+
32
39
  // URI validation (see `isUri`).
33
40
  const uriIllegalCharacterRegex = /[^a-z0-9:/?#[\]@!$&'()*+,;=.\-_~%]/i
34
41
  const uriInvalidEscapeRegex = /%[^0-9a-f]/i
@@ -185,7 +192,8 @@ export function createMessage(value: Message): string {
185
192
  !(
186
193
  domainRegex.test(domain) ||
187
194
  ipRegex.test(domain) ||
188
- localhostRegex.test(domain)
195
+ localhostRegex.test(domain) ||
196
+ (domainIpLiteralRegex.test(domain) && isAuthority(domain))
189
197
  )
190
198
  )
191
199
  throw new InvalidMessageFieldError({
@@ -239,11 +247,11 @@ export function createMessage(value: Message): string {
239
247
  ],
240
248
  })
241
249
  const statement = value.statement
242
- if (statement?.includes('\n'))
250
+ if (statement && statementLineBreakRegex.test(statement))
243
251
  throw new InvalidMessageFieldError({
244
252
  field: 'statement',
245
253
  metaMessages: [
246
- "- Statement must not include '\\n'.",
254
+ "- Statement must not include a line break ('\\n', '\\r', '\\u2028' or '\\u2029').",
247
255
  '',
248
256
  `Provided value: ${statement}`,
249
257
  ],
@@ -359,7 +367,7 @@ export function isUri(value: string): false | string {
359
367
  // if authority is present, the path must be empty or begin with a /
360
368
  if (authority?.length) {
361
369
  if (!(path.length === 0 || path.startsWith('/'))) return false
362
- if (path.length === 0 && !isAuthority(authority)) return false
370
+ if (!isAuthority(authority)) return false
363
371
  } else {
364
372
  // if authority is not present, the path must not start with //
365
373
  if (path.startsWith('//')) return false
@@ -524,7 +532,8 @@ export function validateMessage(value: validateMessage.Value): boolean {
524
532
 
525
533
  if (domain && message.domain !== domain) return false
526
534
  if (nonce && message.nonce !== nonce) return false
527
- if (scheme && message.scheme !== scheme) return false
535
+ // EIP-4361: if `scheme` is not specified, HTTPS is assumed by default.
536
+ if (scheme && (message.scheme ?? 'https') !== scheme) return false
528
537
 
529
538
  // EIP-4361 requires `nonce = 8*( ALPHA / DIGIT )` and `version = "1"`.
530
539
  if (message.nonce !== undefined && !nonceRegex.test(message.nonce))
@@ -572,6 +581,7 @@ export declare namespace validateMessage {
572
581
  nonce?: string | undefined
573
582
  /**
574
583
  * [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.1) URI scheme to check against.
584
+ * If the message omits `scheme`, `https` is assumed ([EIP-4361](https://eips.ethereum.org/EIPS/eip-4361)).
575
585
  */
576
586
  scheme?: string | undefined
577
587
  /**
@@ -407,9 +407,11 @@ export function fromRpc<
407
407
  transaction_.maxFeePerGas = BigInt(transaction.maxFeePerGas)
408
408
  if (transaction.maxPriorityFeePerGas)
409
409
  transaction_.maxPriorityFeePerGas = BigInt(transaction.maxPriorityFeePerGas)
410
+ // Own keys only: an inherited one such as `constructor` is not a type.
410
411
  if (transaction.type)
411
- transaction_.type =
412
- (fromRpcType as any)[transaction.type] ?? transaction.type
412
+ transaction_.type = Object.hasOwn(fromRpcType, transaction.type)
413
+ ? fromRpcType[transaction.type as keyof typeof fromRpcType]
414
+ : transaction.type
413
415
  if (signature)
414
416
  transaction_.v =
415
417
  transaction.type === '0x0' && typeof transaction.v !== 'undefined'
@@ -521,8 +523,12 @@ export function toRpc<pending extends boolean = false>(
521
523
  )
522
524
  if (typeof transaction.r === 'string') rpc.r = transaction.r
523
525
  if (typeof transaction.s === 'string') rpc.s = transaction.s
526
+ // Legacy EIP-155 `v` is unsized; typed transactions keep a single byte.
524
527
  if (transaction.v !== undefined)
525
- rpc.v = Quantity.fromNumberish(transaction.v, { size: 1 })
528
+ rpc.v =
529
+ transaction.type === 'legacy'
530
+ ? Quantity.fromNumberish(transaction.v)
531
+ : Quantity.fromNumberish(transaction.v, { size: 1 })
526
532
  if (transaction.yParity !== undefined)
527
533
  rpc.yParity = Quantity.fromNumberish(transaction.yParity)
528
534
 
@@ -266,9 +266,15 @@ export function fromRpc<const receipt extends Rpc | null>(
266
266
  effectiveGasPrice: BigInt(receipt.effectiveGasPrice ?? 0n),
267
267
  gasUsed: BigInt(receipt.gasUsed ?? 0n),
268
268
  logs: receipt.logs.map((log) => Log.fromRpc(log, { pending: false })),
269
- status: fromRpcStatus[receipt.status],
269
+ // Own keys only: an inherited one such as `__proto__` or `toString` is
270
+ // not a status or type.
271
+ status: Object.hasOwn(fromRpcStatus, receipt.status)
272
+ ? fromRpcStatus[receipt.status]
273
+ : undefined,
270
274
  transactionIndex: Number(receipt.transactionIndex ?? 0),
271
- type: (fromRpcType as any)[receipt.type] || receipt.type,
275
+ type: Object.hasOwn(fromRpcType, receipt.type)
276
+ ? fromRpcType[receipt.type as keyof typeof fromRpcType]
277
+ : receipt.type,
272
278
  } as never
273
279
  }
274
280
 
@@ -0,0 +1,50 @@
1
+ import { bench, describe } from 'vp/test'
2
+ import * as TypedData from './TypedData.js'
3
+
4
+ const typedData = {
5
+ domain: {
6
+ chainId: 1,
7
+ name: 'Ether Mail',
8
+ verifyingContract: '0x0000000000000000000000000000000000000000',
9
+ version: '1',
10
+ },
11
+ message: {
12
+ contents: 'Hello, Bob!',
13
+ from: {
14
+ name: 'Cow',
15
+ wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
16
+ },
17
+ to: {
18
+ name: 'Bob',
19
+ wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
20
+ },
21
+ },
22
+ primaryType: 'Mail',
23
+ types: {
24
+ Mail: [
25
+ { name: 'from', type: 'Person' },
26
+ { name: 'to', type: 'Person' },
27
+ { name: 'contents', type: 'string' },
28
+ ],
29
+ Person: [
30
+ { name: 'name', type: 'string' },
31
+ { name: 'wallet', type: 'address' },
32
+ ],
33
+ },
34
+ } as const
35
+
36
+ describe('TypedData.getSignPayload', () => {
37
+ bench('Mail', () => {
38
+ TypedData.getSignPayload(typedData)
39
+ })
40
+ })
41
+
42
+ describe('TypedData.hashStruct', () => {
43
+ bench('Person', () => {
44
+ TypedData.hashStruct({
45
+ data: typedData.message.from,
46
+ primaryType: 'Person',
47
+ types: typedData.types,
48
+ })
49
+ })
50
+ })
@@ -5,6 +5,7 @@ import * as Bytes from './Bytes.js'
5
5
  import * as Errors from './Errors.js'
6
6
  import * as Hash from './Hash.js'
7
7
  import * as Hex from './Hex.js'
8
+ import { caches } from './internal/caches.js'
8
9
  import type { Compute } from './internal/types.js'
9
10
  import * as Json from './Json.js'
10
11
  import * as Solidity from './Solidity.js'
@@ -533,7 +534,7 @@ export declare namespace getSignPayload {
533
534
  * '0x0000000000000000000000000000000000000000'
534
535
  * }
535
536
  * })
536
- * // @log: '0x6192106f129ce05c9075d319c1fa6ea9b3ae37cbd0c1ef92e2be7137bb07baa1'
537
+ * // @log: '0x2fdf3441fcaf4f30c7e16292b258a5d7054a4e2e00dbd7b7d2f467f2b8fb9413'
537
538
  * ```
538
539
  *
539
540
  * @param value - The Typed Data domain and types.
@@ -592,7 +593,7 @@ export declare namespace hashDomain {
592
593
  * foo: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9'
593
594
  * }
594
595
  * })
595
- * // @log: '0x996fb3b6d48c50312d69abdd4c1b6fb02057c85aa86bb8d04c6f023326a168ce'
596
+ * // @log: '0x4305429dd165189369e3373e404dac11fa3bc66d93d5231b00198cb578d402e0'
596
597
  * ```
597
598
  *
598
599
  * @param value - The Typed Data struct to hash.
@@ -914,6 +915,9 @@ export declare namespace encodeData {
914
915
  | Errors.GlobalErrorType
915
916
  }
916
917
 
918
+ /** Longest encoded type, in characters, whose hash is kept in the cache. */
919
+ const maxCachedTypeLength = 4_096
920
+
917
921
  /** @internal */
918
922
  export function hashType(value: {
919
923
  primaryType: string
@@ -923,8 +927,16 @@ export function hashType(value: {
923
927
  const { primaryType, types, typeHashes } = value
924
928
  const cached = typeHashes?.get(primaryType)
925
929
  if (cached) return cached
926
- const encodedHashType = Hex.fromString(encodeType({ primaryType, types }))
927
- const hash = Hash.keccak256(encodedHashType)
930
+ // Keyed by the encoded type, so equal struct definitions share one hash
931
+ // across calls, whichever object their types came from.
932
+ const type = encodeType({ primaryType, types })
933
+ let hash = caches.typeHash.get(type)
934
+ if (!hash) {
935
+ hash = Hash.keccak256(Hex.fromString(type))
936
+ // Only short encoded types are cached, so the cache's memory is bounded
937
+ // by its entry count rather than by the size of the types it was given.
938
+ if (type.length <= maxCachedTypeLength) caches.typeHash.set(type, hash)
939
+ }
928
940
  typeHashes?.set(primaryType, hash)
929
941
  return hash
930
942
  }
@@ -866,6 +866,89 @@ describe('decode', () => {
866
866
  `)
867
867
  })
868
868
 
869
+ test('error: indexed topic longer than 32 bytes', () => {
870
+ const abiEvent = AbiEvent.from('event Foo(address indexed a)')
871
+
872
+ expect(() =>
873
+ AbiEvent.decode(abiEvent, {
874
+ topics: [
875
+ AbiEvent.getSelector(abiEvent),
876
+ '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac00',
877
+ ],
878
+ }),
879
+ ).toThrowErrorMatchingInlineSnapshot(
880
+ `[Hex.SizeOverflowError: Size cannot exceed \`32\` bytes. Given size: \`33\` bytes.]`,
881
+ )
882
+ })
883
+
884
+ test('error: indexed `address` topic with invalid characters', () => {
885
+ const abiEvent = AbiEvent.from('event Foo(address indexed a)')
886
+ const topic =
887
+ '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678zz'
888
+
889
+ expect(() =>
890
+ AbiEvent.decode(
891
+ abiEvent,
892
+ { topics: [AbiEvent.getSelector(abiEvent), topic] },
893
+ { checksumAddress: false },
894
+ ),
895
+ ).toThrowErrorMatchingInlineSnapshot(`
896
+ [Address.InvalidAddressError: Address "0xa5cc3c03994db5b0d9a5eedd10cabab0813678zz" is invalid.
897
+
898
+ Details: Address is not a 20 byte (40 hexadecimal character) value.]
899
+ `)
900
+ expect(() =>
901
+ AbiEvent.decode(abiEvent, {
902
+ topics: [AbiEvent.getSelector(abiEvent), topic],
903
+ }),
904
+ ).toThrowErrorMatchingInlineSnapshot(`
905
+ [Address.InvalidAddressError: Address "0xa5cc3c03994db5b0d9a5eedd10cabab0813678zz" is invalid.
906
+
907
+ Details: Address is not a 20 byte (40 hexadecimal character) value.]
908
+ `)
909
+ })
910
+
911
+ test('error: non-canonical indexed `bytesN` topics', () => {
912
+ const abiEvent = AbiEvent.from('event Foo(bytes4 indexed a)')
913
+
914
+ expect(
915
+ AbiEvent.decode(abiEvent, {
916
+ topics: [
917
+ AbiEvent.getSelector(abiEvent),
918
+ '0xdeadbeef00000000000000000000000000000000000000000000000000000000',
919
+ ],
920
+ }),
921
+ ).toMatchInlineSnapshot(`
922
+ {
923
+ "a": "0xdeadbeef",
924
+ }
925
+ `)
926
+ expect(() =>
927
+ AbiEvent.decode(abiEvent, {
928
+ topics: [
929
+ AbiEvent.getSelector(abiEvent),
930
+ '0xdeadbeef00000000000000000000000000000000000000000000000000000001',
931
+ ],
932
+ }),
933
+ ).toThrowErrorMatchingInlineSnapshot(
934
+ `[AbiParameters.BytesSizeMismatchError: Size of bytes "0xdeadbeef00000000000000000000000000000000000000000000000000000001" (bytes32) does not match expected size (bytes4).]`,
935
+ )
936
+
937
+ const bytes32 = AbiEvent.from('event Foo(bytes32 indexed a)')
938
+ expect(() =>
939
+ AbiEvent.decode(bytes32, {
940
+ topics: [
941
+ AbiEvent.getSelector(bytes32),
942
+ '0xdeadbeef000000000000000000000000000000000000000000000000000000zz',
943
+ ],
944
+ }),
945
+ ).toThrowErrorMatchingInlineSnapshot(`
946
+ [Hex.InvalidHexValueError: Value \`0xdeadbeef000000000000000000000000000000000000000000000000000000zz\` is an invalid hex value.
947
+
948
+ Hex values must start with \`"0x"\` and contain only hexadecimal characters (0-9, a-f, A-F).]
949
+ `)
950
+ })
951
+
869
952
  test('error: event selector topic mismatch', () => {
870
953
  const transfer = AbiEvent.from(
871
954
  'event Transfer(address indexed from, address indexed to, bool sender)',
@@ -322,13 +322,14 @@ describe('encodeData', () => {
322
322
  )
323
323
  })
324
324
 
325
- test('behavior: malformed bytes compatibility', () => {
325
+ test('error: malformed bytes', () => {
326
326
  const abiFunction = AbiFunction.from('function foo(bytes)')
327
- expect(
328
- AbiFunction.encodeData(abiFunction, ['0xzz11']),
329
- ).toMatchInlineSnapshot(
330
- `"0x30c8d1da00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002zz11000000000000000000000000000000000000000000000000000000000000"`,
331
- )
327
+ expect(() => AbiFunction.encodeData(abiFunction, ['0xzz11']))
328
+ .toThrowErrorMatchingInlineSnapshot(`
329
+ [Hex.InvalidHexValueError: Value \`0xzz11\` is an invalid hex value.
330
+
331
+ Hex values must start with \`"0x"\` and contain only hexadecimal characters (0-9, a-f, A-F).]
332
+ `)
332
333
  })
333
334
 
334
335
  test('behavior: with overloads', () => {
@@ -136,6 +136,17 @@ describe('static', () => {
136
136
  assertType<readonly [Hex.Hex]>(result)
137
137
  expect(result).toEqual(['0x0123456789abcdef'])
138
138
  })
139
+
140
+ test('error: non-zero padding', () => {
141
+ expect(() =>
142
+ AbiParameters.decode(
143
+ AbiParameters.from('bytes8'),
144
+ '0x0123456789abcdef000000000000000000000000000000000000000000000001',
145
+ ),
146
+ ).toThrowErrorMatchingInlineSnapshot(
147
+ `[AbiParameters.BytesSizeMismatchError: Size of bytes "0x0123456789abcdef000000000000000000000000000000000000000000000001" (bytes32) does not match expected size (bytes8).]`,
148
+ )
149
+ })
139
150
  })
140
151
 
141
152
  describe('bytes16', () => {
@@ -1890,6 +1901,28 @@ describe('read size', () => {
1890
1901
  expect(value).toEqual(Array.from({ length }, () => tail))
1891
1902
  })
1892
1903
 
1904
+ test('error: array elements sharing one large tail', () => {
1905
+ // 33 elements point at one 64 KiB tail, so the decoded value would be
1906
+ // 33 times the size of the data.
1907
+ const length = 33
1908
+ const tail = Hex.fromBytes(new Uint8Array(65_536).fill(0xab))
1909
+ const data = Hex.concat(
1910
+ Hex.fromNumber(32, { size: 32 }), // ptr
1911
+ Hex.fromNumber(length, { size: 32 }), // len
1912
+ ...Array.from({ length }, () =>
1913
+ Hex.fromNumber(32 * length, { size: 32 }),
1914
+ ),
1915
+ Hex.fromNumber(65_536, { size: 32 }), // len of tail
1916
+ tail,
1917
+ )
1918
+ expect(Hex.size(data)).toMatchInlineSnapshot(`66688`)
1919
+ expect(() =>
1920
+ AbiParameters.decode(AbiParameters.from('bytes[]'), data),
1921
+ ).toThrowErrorMatchingInlineSnapshot(
1922
+ `[Cursor.ReadSizeLimitExceededError: Read size limit of \`395520\` bytes exceeded (read size: \`459264\` bytes).]`,
1923
+ )
1924
+ })
1925
+
1893
1926
  test('error: array elements with overlapping tails', () => {
1894
1927
  // Each element starts 33 bytes after the previous one and runs to the end
1895
1928
  // of the data.
@@ -1907,7 +1940,7 @@ describe('read size', () => {
1907
1940
  expect(() =>
1908
1941
  AbiParameters.decode(AbiParameters.from('bytes[]'), data),
1909
1942
  ).toThrowErrorMatchingInlineSnapshot(
1910
- `[Cursor.ReadSizeLimitExceededError: Read size limit of \`1329152\` bytes exceeded (read size: \`1335257\` bytes).]`,
1943
+ `[Cursor.ReadSizeLimitExceededError: Read size limit of \`328832\` bytes exceeded (read size: \`332354\` bytes).]`,
1911
1944
  )
1912
1945
  })
1913
1946
  })
@@ -348,6 +348,16 @@ describe('static', () => {
348
348
  `[AbiParameters.BytesSizeMismatchError: Size of bytes "0x0000000000000000000000000000000000000000000000000000000000000000000000000123456789abcdef" (bytes44) does not match expected size (bytes8).]`,
349
349
  )
350
350
  })
351
+
352
+ test('error: invalid hex', () => {
353
+ expect(() =>
354
+ AbiParameters.encode([{ type: 'bytes8' }], ['0x0123456789abcdeg']),
355
+ ).toThrowErrorMatchingInlineSnapshot(`
356
+ [Hex.InvalidHexValueError: Value \`0x0123456789abcdeg\` is an invalid hex value.
357
+
358
+ Hex values must start with \`"0x"\` and contain only hexadecimal characters (0-9, a-f, A-F).]
359
+ `)
360
+ })
351
361
  })
352
362
 
353
363
  describe('bytes16', () => {