ox 1.8.12 → 1.8.14

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 (188) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/dist/core/AbiError.d.ts +2 -2
  3. package/dist/core/AbiError.d.ts.map +1 -1
  4. package/dist/core/AbiError.js +6 -2
  5. package/dist/core/AbiError.js.map +1 -1
  6. package/dist/core/AbiEvent.d.ts +71 -1
  7. package/dist/core/AbiEvent.d.ts.map +1 -1
  8. package/dist/core/AbiEvent.js +160 -19
  9. package/dist/core/AbiEvent.js.map +1 -1
  10. package/dist/core/AbiFunction.d.ts +1 -1
  11. package/dist/core/AbiFunction.d.ts.map +1 -1
  12. package/dist/core/AbiFunction.js +3 -5
  13. package/dist/core/AbiFunction.js.map +1 -1
  14. package/dist/core/AbiItem.d.ts.map +1 -1
  15. package/dist/core/AbiItem.js +18 -8
  16. package/dist/core/AbiItem.js.map +1 -1
  17. package/dist/core/Base32.d.ts +3 -3
  18. package/dist/core/Base32.d.ts.map +1 -1
  19. package/dist/core/Base32.js +5 -2
  20. package/dist/core/Base32.js.map +1 -1
  21. package/dist/core/Bytes.d.ts.map +1 -1
  22. package/dist/core/Bytes.js +4 -3
  23. package/dist/core/Bytes.js.map +1 -1
  24. package/dist/core/Caches.d.ts +1 -2
  25. package/dist/core/Caches.d.ts.map +1 -1
  26. package/dist/core/Caches.js +1 -4
  27. package/dist/core/Caches.js.map +1 -1
  28. package/dist/core/Cbor.d.ts.map +1 -1
  29. package/dist/core/Cbor.js +16 -2
  30. package/dist/core/Cbor.js.map +1 -1
  31. package/dist/core/CompactSize.d.ts +8 -1
  32. package/dist/core/CompactSize.d.ts.map +1 -1
  33. package/dist/core/CompactSize.js +9 -0
  34. package/dist/core/CompactSize.js.map +1 -1
  35. package/dist/core/Hex.d.ts.map +1 -1
  36. package/dist/core/Hex.js +9 -4
  37. package/dist/core/Hex.js.map +1 -1
  38. package/dist/core/Rlp.d.ts +9 -2
  39. package/dist/core/Rlp.d.ts.map +1 -1
  40. package/dist/core/Rlp.js +51 -8
  41. package/dist/core/Rlp.js.map +1 -1
  42. package/dist/core/RpcTransport.d.ts.map +1 -1
  43. package/dist/core/RpcTransport.js +11 -4
  44. package/dist/core/RpcTransport.js.map +1 -1
  45. package/dist/core/Siwe.d.ts.map +1 -1
  46. package/dist/core/Siwe.js +24 -9
  47. package/dist/core/Siwe.js.map +1 -1
  48. package/dist/core/TxEnvelope.d.ts.map +1 -1
  49. package/dist/core/TxEnvelope.js +5 -3
  50. package/dist/core/TxEnvelope.js.map +1 -1
  51. package/dist/core/TxEnvelopeEip1559.d.ts.map +1 -1
  52. package/dist/core/TxEnvelopeEip1559.js +6 -2
  53. package/dist/core/TxEnvelopeEip1559.js.map +1 -1
  54. package/dist/core/TxEnvelopeEip4844.d.ts.map +1 -1
  55. package/dist/core/TxEnvelopeEip4844.js +4 -0
  56. package/dist/core/TxEnvelopeEip4844.js.map +1 -1
  57. package/dist/core/TxEnvelopeEip7702.d.ts.map +1 -1
  58. package/dist/core/TxEnvelopeEip7702.js +4 -0
  59. package/dist/core/TxEnvelopeEip7702.js.map +1 -1
  60. package/dist/core/TypedData.d.ts +8 -1
  61. package/dist/core/TypedData.d.ts.map +1 -1
  62. package/dist/core/TypedData.js +55 -14
  63. package/dist/core/TypedData.js.map +1 -1
  64. package/dist/core/Value.d.ts.map +1 -1
  65. package/dist/core/Value.js +5 -3
  66. package/dist/core/Value.js.map +1 -1
  67. package/dist/core/internal/abiItem.d.ts +3 -1
  68. package/dist/core/internal/abiItem.d.ts.map +1 -1
  69. package/dist/core/internal/abiItem.js +46 -13
  70. package/dist/core/internal/abiItem.js.map +1 -1
  71. package/dist/core/internal/abiParameters.d.ts +2 -2
  72. package/dist/core/internal/abiParameters.d.ts.map +1 -1
  73. package/dist/core/internal/abiParameters.js +24 -7
  74. package/dist/core/internal/abiParameters.js.map +1 -1
  75. package/dist/core/internal/caches.d.ts +10 -0
  76. package/dist/core/internal/caches.d.ts.map +1 -0
  77. package/dist/core/internal/caches.js +10 -0
  78. package/dist/core/internal/caches.js.map +1 -0
  79. package/dist/core/internal/frameNonce.d.ts.map +1 -1
  80. package/dist/core/internal/frameNonce.js +2 -2
  81. package/dist/core/internal/frameNonce.js.map +1 -1
  82. package/dist/core/internal/rpcSchemas/eth.d.ts +3 -2
  83. package/dist/core/internal/rpcSchemas/eth.d.ts.map +1 -1
  84. package/dist/erc4337/UserOperation.d.ts +15 -2
  85. package/dist/erc4337/UserOperation.d.ts.map +1 -1
  86. package/dist/erc4337/UserOperation.js +8 -2
  87. package/dist/erc4337/UserOperation.js.map +1 -1
  88. package/dist/erc8021/Attribution.d.ts.map +1 -1
  89. package/dist/erc8021/Attribution.js +12 -3
  90. package/dist/erc8021/Attribution.js.map +1 -1
  91. package/dist/tempo/AccountOperation.d.ts +8 -0
  92. package/dist/tempo/AccountOperation.d.ts.map +1 -1
  93. package/dist/tempo/AccountOperation.js +26 -6
  94. package/dist/tempo/AccountOperation.js.map +1 -1
  95. package/dist/tempo/KeyAuthorization.d.ts +21 -6
  96. package/dist/tempo/KeyAuthorization.d.ts.map +1 -1
  97. package/dist/tempo/KeyAuthorization.js +7 -4
  98. package/dist/tempo/KeyAuthorization.js.map +1 -1
  99. package/dist/tempo/SignatureEnvelope.d.ts +4 -4
  100. package/dist/tempo/Tick.d.ts.map +1 -1
  101. package/dist/tempo/Tick.js +4 -3
  102. package/dist/tempo/Tick.js.map +1 -1
  103. package/dist/tempo/TxEnvelopeTempo.d.ts.map +1 -1
  104. package/dist/tempo/TxEnvelopeTempo.js +6 -2
  105. package/dist/tempo/TxEnvelopeTempo.js.map +1 -1
  106. package/dist/zod/Address.d.ts.map +1 -1
  107. package/dist/zod/Address.js +2 -1
  108. package/dist/zod/Address.js.map +1 -1
  109. package/dist/zod/FrameRequest.d.ts.map +1 -1
  110. package/dist/zod/FrameRequest.js +2 -1
  111. package/dist/zod/FrameRequest.js.map +1 -1
  112. package/dist/zod/Hex.d.ts.map +1 -1
  113. package/dist/zod/Hex.js +2 -1
  114. package/dist/zod/Hex.js.map +1 -1
  115. package/dist/zod/Solidity.d.ts.map +1 -1
  116. package/dist/zod/Solidity.js +12 -7
  117. package/dist/zod/Solidity.js.map +1 -1
  118. package/dist/zod/TypedData.d.ts +12 -12
  119. package/package.json +5 -2
  120. package/src/core/AbiError.ts +33 -22
  121. package/src/core/AbiEvent.ts +177 -18
  122. package/src/core/AbiFunction.ts +10 -7
  123. package/src/core/AbiItem.ts +17 -8
  124. package/src/core/Base32.ts +8 -3
  125. package/src/core/Bytes.ts +4 -3
  126. package/src/core/Caches.ts +1 -6
  127. package/src/core/Cbor.ts +20 -2
  128. package/src/core/CompactSize.ts +13 -0
  129. package/src/core/Hex.ts +10 -5
  130. package/src/core/Rlp.ts +55 -5
  131. package/src/core/RpcTransport.ts +11 -4
  132. package/src/core/Siwe.ts +26 -11
  133. package/src/core/TxEnvelope.ts +5 -3
  134. package/src/core/TxEnvelopeEip1559.ts +8 -2
  135. package/src/core/TxEnvelopeEip4844.ts +6 -0
  136. package/src/core/TxEnvelopeEip7702.ts +6 -0
  137. package/src/core/TypedData.ts +62 -13
  138. package/src/core/Value.ts +6 -3
  139. package/src/core/_test/AbiError.test-d.ts +10 -0
  140. package/src/core/_test/AbiError.test.ts +73 -0
  141. package/src/core/_test/AbiEvent.fuzz.ts +260 -0
  142. package/src/core/_test/AbiEvent.snap-d.ts +9 -0
  143. package/src/core/_test/AbiEvent.test.ts +421 -0
  144. package/src/core/_test/AbiFunction.snap-d.ts +25 -0
  145. package/src/core/_test/AbiFunction.test.ts +101 -18
  146. package/src/core/_test/AbiItem.snap-d.ts +11 -0
  147. package/src/core/_test/AbiItem.test.ts +393 -5
  148. package/src/core/_test/AbiParameters.decode.test.ts +140 -1
  149. package/src/core/_test/Base32.test.ts +16 -0
  150. package/src/core/_test/Bytes.test.ts +15 -0
  151. package/src/core/_test/Caches.test.ts +10 -1
  152. package/src/core/_test/Cbor.test.ts +23 -0
  153. package/src/core/_test/CompactSize.test.ts +14 -0
  154. package/src/core/_test/Hex.test.ts +25 -0
  155. package/src/core/_test/Rlp.test.ts +51 -0
  156. package/src/core/_test/RpcSchema.test-d.ts +23 -1
  157. package/src/core/_test/RpcTransport.test.ts +47 -0
  158. package/src/core/_test/Siwe.test.ts +28 -0
  159. package/src/core/_test/TransactionEnvelopeEip1559.test.ts +24 -0
  160. package/src/core/_test/TransactionEnvelopeEip4844.test.ts +13 -0
  161. package/src/core/_test/TransactionEnvelopeEip7702.test.ts +13 -0
  162. package/src/core/_test/TypedData.test.ts +203 -1
  163. package/src/core/internal/abiItem.ts +58 -18
  164. package/src/core/internal/abiParameters.ts +26 -8
  165. package/src/core/internal/caches.ts +12 -0
  166. package/src/core/internal/frameNonce.ts +3 -4
  167. package/src/core/internal/rpcSchemas/eth.ts +2 -1
  168. package/src/erc4337/UserOperation.ts +30 -3
  169. package/src/erc4337/_test/UserOperation.test.ts +132 -35
  170. package/src/erc8021/Attribution.ts +9 -7
  171. package/src/erc8021/_test/Attribution.test.ts +37 -0
  172. package/src/tempo/AccountOperation.test.ts +116 -0
  173. package/src/tempo/AccountOperation.ts +38 -5
  174. package/src/tempo/AccountSimulation.test.ts +1 -1
  175. package/src/tempo/KeyAuthorization.test.ts +6 -4
  176. package/src/tempo/KeyAuthorization.ts +26 -8
  177. package/src/tempo/SignatureEnvelope.test.ts +12 -0
  178. package/src/tempo/SignatureEnvelope.ts +4 -4
  179. package/src/tempo/Tick.test.ts +10 -0
  180. package/src/tempo/Tick.ts +5 -3
  181. package/src/tempo/TxEnvelopeTempo.test.ts +26 -0
  182. package/src/tempo/TxEnvelopeTempo.ts +8 -2
  183. package/src/tempo/e2e.test.ts +73 -17
  184. package/src/version.ts +1 -1
  185. package/src/zod/Address.ts +3 -3
  186. package/src/zod/FrameRequest.ts +3 -4
  187. package/src/zod/Hex.ts +3 -1
  188. package/src/zod/Solidity.ts +15 -18
@@ -4,6 +4,7 @@ import type * as AbiItem from '../AbiItem.js'
4
4
  import type * as AbiParameters from '../AbiParameters.js'
5
5
  import * as Address from '../Address.js'
6
6
  import * as Errors from '../Errors.js'
7
+ import * as Hex from '../Hex.js'
7
8
  import { getArrayComponents } from './abiParameters.js'
8
9
  import type {
9
10
  Compute,
@@ -54,7 +55,17 @@ export type ExtractForArgs<
54
55
  >
55
56
  }
56
57
  >
57
- : TupleToUnion<abiItems, abi, name, args> // convert back to union (removes `never` tuple entries: `['foo', never, 'bar'][number]` => `'foo' | 'bar'`)
58
+ : readonly [] extends args
59
+ ? TupleToUnion<abiItems, abi, name, args> extends infer zero
60
+ ? Compute<
61
+ zero & {
62
+ readonly overloads: UnionToTuple<
63
+ Exclude<abiItems[number], zero>
64
+ >
65
+ }
66
+ >
67
+ : never
68
+ : TupleToUnion<abiItems, abi, name, args> // convert back to union (removes `never` tuple entries: `['foo', never, 'bar'][number]` => `'foo' | 'bar'`)
58
69
  : never
59
70
  : abiItem
60
71
  : never
@@ -601,11 +612,31 @@ export function isArgOfType(
601
612
  },
602
613
  )
603
614
 
604
- if (integerTypeRegex.test(abiParameterType))
605
- return argType === 'number' || argType === 'bigint'
615
+ const integerMatch = integerTypeRegex.exec(abiParameterType)
616
+ if (integerMatch) {
617
+ if (typeof arg !== 'bigint' && typeof arg !== 'number') return false
618
+ if (typeof arg === 'number' && !Number.isInteger(arg)) return false
619
+
620
+ // An overload whose integer range excludes the value does not match.
621
+ const signed = abiParameterType.startsWith('int')
622
+ const bits = Number.parseInt(integerMatch[1] ?? '256', 10)
623
+ const max = 2n ** (BigInt(bits) - (signed ? 1n : 0n)) - 1n
624
+ const min = signed ? -max - 1n : 0n
625
+ return arg >= min && arg <= max
626
+ }
606
627
 
607
- if (fixedBytesTypeRegex.test(abiParameterType))
608
- return argType === 'string' || arg instanceof Uint8Array
628
+ if (fixedBytesTypeRegex.test(abiParameterType)) {
629
+ if (typeof arg !== 'string' && !(arg instanceof Uint8Array))
630
+ return false
631
+ // An overload whose fixed size differs from the value does not match.
632
+ const bytes = abiParameterType.slice(5)
633
+ // Dynamic `bytes` is hex or raw bytes. A plain string is only `string`.
634
+ if (!bytes) return arg instanceof Uint8Array || Hex.validate(arg)
635
+ if (arg instanceof Uint8Array)
636
+ return arg.length === Number.parseInt(bytes, 10)
637
+ if (!Hex.validate(arg)) return false
638
+ return Hex.size(arg as Hex.Hex) === Number.parseInt(bytes, 10)
639
+ }
609
640
 
610
641
  // Fixed-length (`<type>[M]`) and dynamic (`<type>[]`) arrays.
611
642
  const arrayComponents = getArrayComponents(abiParameterType)
@@ -683,6 +714,15 @@ export function getAmbiguousTypes(
683
714
  [(args[parameterIndex] as readonly unknown[] | undefined)?.[0]],
684
715
  )
685
716
  if (types) return types
717
+ // Same element type, different array shape: `uint256[]` and `uint256[2]`
718
+ // both accept `[1n, 1n]`, but one is dynamic and the other is static.
719
+ // Skip this when the element types differ. `string[]` vs `address[]`
720
+ // stays unambiguous for a non-address string.
721
+ if (
722
+ sourceArray[1] === targetArray[1] &&
723
+ sourceParameter.type !== targetParameter.type
724
+ )
725
+ return [sourceParameter.type, targetParameter.type]
686
726
  continue
687
727
  }
688
728
 
@@ -690,20 +730,20 @@ export function getAmbiguousTypes(
690
730
  const targetType = targetParameter.type
691
731
  const arg = args[parameterIndex] as Address.Address
692
732
 
693
- let ambiguous = false
694
- if (
695
- (sourceType === 'address' && targetType === 'bytes20') ||
696
- (sourceType === 'bytes20' && targetType === 'address')
697
- )
698
- ambiguous = true
699
- else if (
700
- (sourceType === 'address' && targetType === 'string') ||
701
- (sourceType === 'string' && targetType === 'address') ||
702
- (sourceType === 'address' && targetType === 'bytes') ||
703
- (sourceType === 'bytes' && targetType === 'address')
704
- )
733
+ const ambiguous = (() => {
705
734
  // An `undefined` value comes from an empty array, which matches both types.
706
- ambiguous = arg === undefined || Address.validate(arg, { strict: false })
735
+ // This stays above the catch-all: a non-address string is not ambiguous.
736
+ if (
737
+ (sourceType === 'address' && targetType === 'string') ||
738
+ (sourceType === 'string' && targetType === 'address') ||
739
+ (sourceType === 'address' && targetType === 'bytes') ||
740
+ (sourceType === 'bytes' && targetType === 'address')
741
+ )
742
+ return arg === undefined || Address.validate(arg, { strict: false })
743
+ // Distinct types that both matched cannot be distinguished at runtime
744
+ // (e.g. `bytes` vs `bytes32`, `uint256` vs `int256`, `address` vs `bytes20`).
745
+ return sourceType !== targetType
746
+ })()
707
747
 
708
748
  if (ambiguous) return [sourceType, targetType]
709
749
  }
@@ -178,6 +178,14 @@ export function decodeAddress(
178
178
  ) {
179
179
  const { checksum = false } = options
180
180
  const value = cursor.readBytes(32)
181
+ // An address occupies the low 20 bytes of the word; the high 12 bytes
182
+ // must be zero padding.
183
+ for (let i = 0; i < 12; i++)
184
+ if (value[i] !== 0)
185
+ throw new Address.InvalidAddressError({
186
+ address: Hex.fromBytes(value),
187
+ cause: new Address.InvalidInputError(),
188
+ })
181
189
  const wrap = (address: Hex.Hex) =>
182
190
  checksum ? Address.checksum(address) : address
183
191
  return [wrap(Hex.fromBytes(Bytes.slice(value, -20))), 32]
@@ -185,6 +193,7 @@ export function decodeAddress(
185
193
 
186
194
  export declare namespace decodeAddress {
187
195
  type ErrorType =
196
+ | Address.InvalidAddressError
188
197
  | Hex.fromBytes.ErrorType
189
198
  | Bytes.slice.ErrorType
190
199
  | Errors.GlobalErrorType
@@ -356,19 +365,28 @@ export function decodeNumber(
356
365
  ) {
357
366
  const signed = param.type.startsWith('int')
358
367
  const size = Number.parseInt(param.type.split('int')[1] || '256', 10)
359
- const value = cursor.readBytes(32)
360
- return [
361
- size > 48
362
- ? Bytes.toBigInt(value, { signed })
363
- : Bytes.toNumber(value, { signed }),
364
- 32,
365
- ]
368
+ const value = Bytes.toBigInt(cursor.readBytes(32), { signed })
369
+ // The word must hold a value that fits the declared size (zero-padded for
370
+ // unsigned, sign-extended for signed types).
371
+ if (size < 256) {
372
+ const max = (1n << BigInt(signed ? size - 1 : size)) - 1n
373
+ const min = signed ? -max - 1n : 0n
374
+ if (value > max || value < min)
375
+ throw new Hex.IntegerOutOfRangeError({
376
+ max: max.toString(),
377
+ min: min.toString(),
378
+ signed,
379
+ size: size / 8,
380
+ value: value.toString(),
381
+ })
382
+ }
383
+ return [size > 48 ? value : Number(value), 32]
366
384
  }
367
385
 
368
386
  export declare namespace decodeNumber {
369
387
  type ErrorType =
370
- | Bytes.toNumber.ErrorType
371
388
  | Bytes.toBigInt.ErrorType
389
+ | Hex.IntegerOutOfRangeError
372
390
  | Errors.GlobalErrorType
373
391
  }
374
392
 
@@ -0,0 +1,12 @@
1
+ import type * as Address from '../Address.js'
2
+ import type * as Hex from '../Hex.js'
3
+ import { BoundedMap } from './lru.js'
4
+
5
+ /**
6
+ * Global caches, cleared together by `Caches.clear` and on every engine change.
7
+ * Only `checksum` is public (`Caches.checksum`).
8
+ */
9
+ export const caches = {
10
+ checksum: /*#__PURE__*/ new BoundedMap<Address.Address>(32_768),
11
+ signatureHash: /*#__PURE__*/ new BoundedMap<Hex.Hex>(8_192),
12
+ }
@@ -2,6 +2,8 @@ import * as Errors from '../Errors.js'
2
2
  import * as Hex from '../Hex.js'
3
3
  import * as Quantity from './quantity.js'
4
4
 
5
+ const quantityRegex = /^0x(?:0|[1-9a-fA-F][0-9a-fA-F]*)$/
6
+
5
7
  export function assert(keys: readonly bigint[], nonce?: bigint): void {
6
8
  if (!Array.isArray(keys) || keys.length < 1 || keys.length > 16)
7
9
  throw new InvalidError('Expected between 1 and 16 nonce keys.')
@@ -28,10 +30,7 @@ export function fromRpc(
28
30
  ): readonly bigint[] {
29
31
  if (!Array.isArray(keys)) throw new InvalidError('Expected a nonce key list.')
30
32
  const integer = (value: Hex.Hex): bigint => {
31
- if (
32
- typeof value !== 'string' ||
33
- !/^0x(?:0|[1-9a-fA-F][0-9a-fA-F]*)$/.test(value)
34
- )
33
+ if (typeof value !== 'string' || !quantityRegex.test(value))
35
34
  throw new InvalidError('Expected a canonical RPC nonce quantity.')
36
35
  return Hex.toBigInt(value)
37
36
  }
@@ -13,6 +13,7 @@ import type * as Transaction from '../../Transaction.js'
13
13
  import type * as TransactionReceipt from '../../TransactionReceipt.js'
14
14
  import type * as TransactionRequest from '../../TransactionRequest.js'
15
15
  import type * as TxEnvelope from '../../TxEnvelope.js'
16
+ import type { OneOf, UnionCompute } from '../types.js'
16
17
 
17
18
  /**
18
19
  * Union of all JSON-RPC Methods for the `eth_` namespace.
@@ -237,7 +238,7 @@ export type Eth = RpcSchema.From<
237
238
  ReturnType: {
238
239
  capabilities?: Record<string, unknown> | undefined
239
240
  raw?: Hex.Hex | undefined
240
- tx: TxEnvelope.Rpc & { from: Address.Address }
241
+ tx: UnionCompute<OneOf<TxEnvelope.Rpc> & { from: Address.Address }>
241
242
  }
242
243
  }
243
244
  /**
@@ -704,12 +704,24 @@ export declare namespace hash {
704
704
  * @param userOperation - The user operation to convert.
705
705
  * @returns The init code.
706
706
  */
707
- export function toInitCode(userOperation: Partial<UserOperation>): Hex.Hex {
707
+ export function toInitCode(
708
+ userOperation: Partial<UserOperation>,
709
+ options: toInitCode.Options = {},
710
+ ): Hex.Hex {
711
+ const { forHash } = options
708
712
  const { authorization, factory, factoryData } = userOperation
709
713
  if (
710
714
  factory === '0x7702' ||
711
715
  factory === '0x7702000000000000000000000000000000000000'
712
716
  ) {
717
+ // Onchain, the EntryPoint detects EIP-7702 by the 20-byte `0x7702` marker
718
+ // and reads the initialization data from `initCode[20:]`.
719
+ if (!forHash)
720
+ return Hex.concat(
721
+ '0x7702000000000000000000000000000000000000',
722
+ factoryData ?? '0x',
723
+ )
724
+ // For the hash, the EntryPoint replaces the marker with the delegate.
713
725
  if (!authorization) return '0x7702000000000000000000000000000000000000'
714
726
  const delegation = authorization.address
715
727
  return Hex.concat(delegation, factoryData ?? '0x')
@@ -718,6 +730,18 @@ export function toInitCode(userOperation: Partial<UserOperation>): Hex.Hex {
718
730
  return Hex.concat(factory, factoryData ?? '0x')
719
731
  }
720
732
 
733
+ export declare namespace toInitCode {
734
+ type Options = {
735
+ /**
736
+ * Prepares the init code for hashing: replaces the EIP-7702 `0x7702`
737
+ * marker with the delegate address.
738
+ */
739
+ forHash?: boolean | undefined
740
+ }
741
+
742
+ type ErrorType = Hex.concat.ErrorType | Errors.GlobalErrorType
743
+ }
744
+
721
745
  /**
722
746
  * Transforms a User Operation into "packed" format.
723
747
  *
@@ -766,7 +790,7 @@ export function toPacked(
766
790
  verificationGasLimit || 0n,
767
791
  callGasLimit || 0n,
768
792
  )
769
- const initCode = toInitCode(userOperation)
793
+ const initCode = toInitCode(userOperation, { forHash: options.forHash })
770
794
  const gasFees = packUint128Pair(
771
795
  maxPriorityFeePerGas || 0n,
772
796
  maxFeePerGas || 0n,
@@ -806,7 +830,10 @@ export function toPacked(
806
830
  export declare namespace toPacked {
807
831
  /** Packing options. */
808
832
  export type Options = {
809
- /** Omits the paymaster signature while retaining its marker. */
833
+ /**
834
+ * Prepares for hashing: omits the paymaster signature while retaining its
835
+ * marker, and replaces the EIP-7702 `initCode` marker with the delegate.
836
+ */
810
837
  forHash?: boolean | undefined
811
838
  }
812
839
 
@@ -912,6 +912,62 @@ describe('toPacked', () => {
912
912
  ).toMatchInlineSnapshot(`"0x004122e325a297439656"`)
913
913
  })
914
914
  })
915
+
916
+ describe('eip7702', () => {
917
+ const userOperation = {
918
+ authorization: {
919
+ address: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357',
920
+ chainId: 1,
921
+ nonce: 69n,
922
+ yParity: 0,
923
+ r: '0x0000000000000000000000000000000000000000000000000000000000000001',
924
+ s: '0x0000000000000000000000000000000000000000000000000000000000000002',
925
+ },
926
+ callData: '0xdeadbeef',
927
+ callGasLimit: 6942069n,
928
+ factory: '0x7702',
929
+ factoryData: '0xcafebabe',
930
+ maxFeePerGas: 69420n,
931
+ maxPriorityFeePerGas: 69n,
932
+ nonce: 0n,
933
+ preVerificationGas: 6942069n,
934
+ sender: '0x1234567890123456789012345678901234567890',
935
+ signature: '0x',
936
+ verificationGasLimit: 6942069n,
937
+ } as const satisfies UserOperation.UserOperation<'0.8', true>
938
+
939
+ test('default: keeps the EntryPoint `0x7702` initCode marker', () => {
940
+ const packed = UserOperation.toPacked(userOperation)
941
+ expect(packed).toMatchInlineSnapshot(`
942
+ {
943
+ "accountGasLimits": "0x0000000000000000000000000069ed750000000000000000000000000069ed75",
944
+ "callData": "0xdeadbeef",
945
+ "gasFees": "0x0000000000000000000000000000004500000000000000000000000000010f2c",
946
+ "initCode": "0x7702000000000000000000000000000000000000cafebabe",
947
+ "nonce": 0n,
948
+ "paymasterAndData": "0x",
949
+ "preVerificationGas": 6942069n,
950
+ "sender": "0x1234567890123456789012345678901234567890",
951
+ "signature": "0x",
952
+ }
953
+ `)
954
+ const { factory, factoryData } = UserOperation.fromPacked(packed)
955
+ expect({ factory, factoryData }).toMatchInlineSnapshot(`
956
+ {
957
+ "factory": "0x7702000000000000000000000000000000000000",
958
+ "factoryData": "0xcafebabe",
959
+ }
960
+ `)
961
+ })
962
+
963
+ test('options: forHash', () => {
964
+ expect(
965
+ UserOperation.toPacked(userOperation, { forHash: true }).initCode,
966
+ ).toMatchInlineSnapshot(
967
+ `"0x9f1fdab6458c5fc642fa0f4c5af7473c46837357cafebabe"`,
968
+ )
969
+ })
970
+ })
915
971
  })
916
972
 
917
973
  describe('toInitCode', () => {
@@ -965,55 +1021,96 @@ describe('toInitCode', () => {
965
1021
  ).toBe('0x7702000000000000000000000000000000000000')
966
1022
  })
967
1023
 
968
- test('behavior: EIP-7702 factory with authorization and no factoryData', () => {
1024
+ test('options: forHash: EIP-7702 factory with authorization and no factoryData', () => {
969
1025
  expect(
970
- UserOperation.toInitCode({
971
- factory: '0x7702',
972
- factoryData: undefined,
973
- authorization: {
974
- address: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357',
975
- chainId: 1,
976
- nonce: 69n,
977
- yParity: 0,
978
- r: '0x0000000000000000000000000000000000000000000000000000000000000001',
979
- s: '0x0000000000000000000000000000000000000000000000000000000000000002',
1026
+ UserOperation.toInitCode(
1027
+ {
1028
+ factory: '0x7702',
1029
+ factoryData: undefined,
1030
+ authorization: {
1031
+ address: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357',
1032
+ chainId: 1,
1033
+ nonce: 69n,
1034
+ yParity: 0,
1035
+ r: '0x0000000000000000000000000000000000000000000000000000000000000001',
1036
+ s: '0x0000000000000000000000000000000000000000000000000000000000000002',
1037
+ },
980
1038
  },
981
- }),
1039
+ { forHash: true },
1040
+ ),
982
1041
  ).toBe('0x9f1fdab6458c5fc642fa0f4c5af7473c46837357')
983
1042
  })
984
1043
 
985
- test('behavior: EIP-7702 factory with authorization and factoryData', () => {
1044
+ test('options: forHash: EIP-7702 factory with authorization and factoryData', () => {
986
1045
  expect(
987
- UserOperation.toInitCode({
988
- factory: '0x7702',
989
- factoryData: '0xdeadbeef',
990
- authorization: {
991
- address: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357',
992
- chainId: 1,
993
- nonce: 69n,
994
- yParity: 0,
995
- r: '0x0000000000000000000000000000000000000000000000000000000000000001',
996
- s: '0x0000000000000000000000000000000000000000000000000000000000000002',
1046
+ UserOperation.toInitCode(
1047
+ {
1048
+ factory: '0x7702',
1049
+ factoryData: '0xdeadbeef',
1050
+ authorization: {
1051
+ address: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357',
1052
+ chainId: 1,
1053
+ nonce: 69n,
1054
+ yParity: 0,
1055
+ r: '0x0000000000000000000000000000000000000000000000000000000000000001',
1056
+ s: '0x0000000000000000000000000000000000000000000000000000000000000002',
1057
+ },
997
1058
  },
998
- }),
1059
+ { forHash: true },
1060
+ ),
999
1061
  ).toBe('0x9f1fdab6458c5fc642fa0f4c5af7473c46837357deadbeef')
1000
1062
  })
1001
1063
 
1002
- test('behavior: EIP-7702 factory (full form) with authorization and factoryData', () => {
1064
+ test('options: forHash: EIP-7702 factory (full form) with authorization and factoryData', () => {
1065
+ expect(
1066
+ UserOperation.toInitCode(
1067
+ {
1068
+ factory: '0x7702000000000000000000000000000000000000',
1069
+ factoryData: '0xcafebabe',
1070
+ authorization: {
1071
+ address: '0x1234567890123456789012345678901234567890',
1072
+ chainId: 1,
1073
+ nonce: 42n,
1074
+ yParity: 1,
1075
+ r: '0x000000000000000000000000000000000000000000000000000000000000007b',
1076
+ s: '0x00000000000000000000000000000000000000000000000000000000000001c8',
1077
+ },
1078
+ },
1079
+ { forHash: true },
1080
+ ),
1081
+ ).toBe('0x1234567890123456789012345678901234567890cafebabe')
1082
+ })
1083
+
1084
+ test('behavior: EIP-7702 factory with authorization packs the 20-byte marker', () => {
1085
+ const authorization = {
1086
+ address: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357',
1087
+ chainId: 1,
1088
+ nonce: 69n,
1089
+ yParity: 0,
1090
+ r: '0x0000000000000000000000000000000000000000000000000000000000000001',
1091
+ s: '0x0000000000000000000000000000000000000000000000000000000000000002',
1092
+ } as const
1093
+ expect(
1094
+ UserOperation.toInitCode({
1095
+ factory: '0x7702',
1096
+ factoryData: undefined,
1097
+ authorization,
1098
+ }),
1099
+ ).toBe('0x7702000000000000000000000000000000000000')
1100
+ expect(
1101
+ UserOperation.toInitCode({
1102
+ factory: '0x7702',
1103
+ factoryData: '0xdeadbeef',
1104
+ authorization,
1105
+ }),
1106
+ ).toBe('0x7702000000000000000000000000000000000000deadbeef')
1003
1107
  expect(
1004
1108
  UserOperation.toInitCode({
1005
1109
  factory: '0x7702000000000000000000000000000000000000',
1006
- factoryData: '0xcafebabe',
1007
- authorization: {
1008
- address: '0x1234567890123456789012345678901234567890',
1009
- chainId: 1,
1010
- nonce: 42n,
1011
- yParity: 1,
1012
- r: '0x000000000000000000000000000000000000000000000000000000000000007b',
1013
- s: '0x00000000000000000000000000000000000000000000000000000000000001c8',
1014
- },
1110
+ factoryData: '0xdeadbeef',
1111
+ authorization,
1015
1112
  }),
1016
- ).toBe('0x1234567890123456789012345678901234567890cafebabe')
1113
+ ).toBe('0x7702000000000000000000000000000000000000deadbeef')
1017
1114
  })
1018
1115
  })
1019
1116
 
@@ -354,6 +354,8 @@ function decodeCodesTail(
354
354
 
355
355
  const codesEnd = codesLengthStart
356
356
  const codesStart = codesEnd - codesLength
357
+ // `Hex.slice` clamps a negative start that runs past the beginning of `data`.
358
+ if (Hex.size(data) + codesStart < 0) return undefined
357
359
  const codesHex = Hex.slice(data, codesStart, codesEnd)
358
360
  const codesString = Hex.toString(codesHex)
359
361
  const codes = codesString.length > 0 ? codesString.split(',') : []
@@ -472,16 +474,16 @@ function schema2ToDataSuffix(attribution: AttributionSchemaId2): Hex.Hex {
472
474
 
473
475
  function schema2FromData(data: Hex.Hex): AttributionSchemaId2 | undefined {
474
476
  // cborLength is 2 bytes before schema ID
475
- const cborLengthHex = Hex.slice(
476
- data,
477
- -ercSuffixSize - 1 - 2,
478
- -ercSuffixSize - 1,
479
- )
477
+ const cborLengthStart = -ercSuffixSize - 1 - 2
478
+ const cborLengthEnd = -ercSuffixSize - 1
479
+ if (Hex.size(data) + cborLengthStart < 0) return undefined
480
+ const cborLengthHex = Hex.slice(data, cborLengthStart, cborLengthEnd)
480
481
  const cborLength = Hex.toNumber(cborLengthHex)
481
482
 
482
483
  // Extract CBOR data
483
- const cborStart = -ercSuffixSize - 1 - 2 - cborLength
484
- const cborEnd = -ercSuffixSize - 1 - 2
484
+ const cborStart = cborLengthStart - cborLength
485
+ if (Hex.size(data) + cborStart < 0) return undefined
486
+ const cborEnd = cborLengthStart
485
487
  const cborHex = Hex.slice(data, cborStart, cborEnd)
486
488
 
487
489
  // Decode CBOR
@@ -442,6 +442,24 @@ describe('fromData', () => {
442
442
 
443
443
  expect(result).toBeUndefined()
444
444
  })
445
+
446
+ test('codesLength claims more bytes than are available', () => {
447
+ // codesLength 0xff with no codes bytes before it
448
+ const input = '0xff0080218021802180218021802180218021'
449
+
450
+ const result = Attribution.fromData(input)
451
+
452
+ expect(result).toBeUndefined()
453
+ })
454
+
455
+ test('codesLength overruns into unrelated leading bytes', () => {
456
+ // codesLength 0x0a with only 4 bytes before it
457
+ const input = '0xdeadbeef0a0080218021802180218021802180218021'
458
+
459
+ const result = Attribution.fromData(input)
460
+
461
+ expect(result).toBeUndefined()
462
+ })
445
463
  })
446
464
  })
447
465
 
@@ -768,5 +786,24 @@ describe('schema 2 (CBOR-encoded)', () => {
768
786
  expect(parsed).toEqual(attribution)
769
787
  },
770
788
  )
789
+
790
+ test('cborLength field missing', () => {
791
+ // 18 bytes: one short of the 2-byte cborLength schema 2 needs
792
+ const input = '0x000280218021802180218021802180218021'
793
+
794
+ const result = Attribution.fromData(input)
795
+
796
+ expect(result).toBeUndefined()
797
+ })
798
+
799
+ test('cborLength claims more bytes than are available', () => {
800
+ // cborLength 0x000c with only the 11-byte `{ a: 'baseapp' }` map before it
801
+ const input =
802
+ '0xa161616762617365617070000c0280218021802180218021802180218021'
803
+
804
+ const result = Attribution.fromData(input)
805
+
806
+ expect(result).toBeUndefined()
807
+ })
771
808
  })
772
809
  })