ox 0.14.44 → 0.14.46

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 (149) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/Frame/package.json +6 -0
  3. package/FrameReceipt/package.json +6 -0
  4. package/FrameSignature/package.json +6 -0
  5. package/TxEnvelopeEip8141/package.json +6 -0
  6. package/_cjs/core/Frame.js +147 -0
  7. package/_cjs/core/Frame.js.map +1 -0
  8. package/_cjs/core/FrameReceipt.js +34 -0
  9. package/_cjs/core/FrameReceipt.js.map +1 -0
  10. package/_cjs/core/FrameSignature.js +203 -0
  11. package/_cjs/core/FrameSignature.js.map +1 -0
  12. package/_cjs/core/Transaction.js +18 -2
  13. package/_cjs/core/Transaction.js.map +1 -1
  14. package/_cjs/core/TransactionReceipt.js +10 -0
  15. package/_cjs/core/TransactionReceipt.js.map +1 -1
  16. package/_cjs/core/TransactionRequest.js +11 -1
  17. package/_cjs/core/TransactionRequest.js.map +1 -1
  18. package/_cjs/core/TxEnvelopeEip8141.js +283 -0
  19. package/_cjs/core/TxEnvelopeEip8141.js.map +1 -0
  20. package/_cjs/core/internal/curves.js +6 -0
  21. package/_cjs/core/internal/curves.js.map +1 -0
  22. package/_cjs/core/internal/quantity.js +10 -0
  23. package/_cjs/core/internal/quantity.js.map +1 -0
  24. package/_cjs/erc4337/UserOperationReceipt.js.map +1 -1
  25. package/_cjs/index.js +6 -2
  26. package/_cjs/index.js.map +1 -1
  27. package/_cjs/tempo/KeyAuthorization.js +43 -10
  28. package/_cjs/tempo/KeyAuthorization.js.map +1 -1
  29. package/_cjs/tempo/MultisigConfig.js +33 -11
  30. package/_cjs/tempo/MultisigConfig.js.map +1 -1
  31. package/_cjs/tempo/MultisigOperation.js +18 -94
  32. package/_cjs/tempo/MultisigOperation.js.map +1 -1
  33. package/_cjs/tempo/MultisigSimulation.js +18 -46
  34. package/_cjs/tempo/MultisigSimulation.js.map +1 -1
  35. package/_cjs/tempo/SignatureEnvelope.js +72 -109
  36. package/_cjs/tempo/SignatureEnvelope.js.map +1 -1
  37. package/_cjs/tempo/TransactionRequest.js +5 -1
  38. package/_cjs/tempo/TransactionRequest.js.map +1 -1
  39. package/_cjs/tempo/index.js.map +1 -1
  40. package/_cjs/version.js +1 -1
  41. package/_esm/core/Frame.js +311 -0
  42. package/_esm/core/Frame.js.map +1 -0
  43. package/_esm/core/FrameReceipt.js +72 -0
  44. package/_esm/core/FrameReceipt.js.map +1 -0
  45. package/_esm/core/FrameSignature.js +422 -0
  46. package/_esm/core/FrameSignature.js.map +1 -0
  47. package/_esm/core/Transaction.js +18 -2
  48. package/_esm/core/Transaction.js.map +1 -1
  49. package/_esm/core/TransactionReceipt.js +10 -0
  50. package/_esm/core/TransactionReceipt.js.map +1 -1
  51. package/_esm/core/TransactionRequest.js +11 -1
  52. package/_esm/core/TransactionRequest.js.map +1 -1
  53. package/_esm/core/TxEnvelopeEip8141.js +490 -0
  54. package/_esm/core/TxEnvelopeEip8141.js.map +1 -0
  55. package/_esm/core/internal/curves.js +19 -0
  56. package/_esm/core/internal/curves.js.map +1 -0
  57. package/_esm/core/internal/quantity.js +14 -0
  58. package/_esm/core/internal/quantity.js.map +1 -0
  59. package/_esm/erc4337/UserOperationReceipt.js.map +1 -1
  60. package/_esm/index.js +73 -0
  61. package/_esm/index.js.map +1 -1
  62. package/_esm/tempo/KeyAuthorization.js +42 -9
  63. package/_esm/tempo/KeyAuthorization.js.map +1 -1
  64. package/_esm/tempo/MultisigConfig.js +41 -22
  65. package/_esm/tempo/MultisigConfig.js.map +1 -1
  66. package/_esm/tempo/MultisigOperation.js +20 -100
  67. package/_esm/tempo/MultisigOperation.js.map +1 -1
  68. package/_esm/tempo/MultisigSimulation.js +18 -46
  69. package/_esm/tempo/MultisigSimulation.js.map +1 -1
  70. package/_esm/tempo/SignatureEnvelope.js +107 -169
  71. package/_esm/tempo/SignatureEnvelope.js.map +1 -1
  72. package/_esm/tempo/TransactionRequest.js +5 -1
  73. package/_esm/tempo/TransactionRequest.js.map +1 -1
  74. package/_esm/tempo/index.js +3 -1
  75. package/_esm/tempo/index.js.map +1 -1
  76. package/_esm/version.js +1 -1
  77. package/_types/core/Frame.d.ts +290 -0
  78. package/_types/core/Frame.d.ts.map +1 -0
  79. package/_types/core/FrameReceipt.d.ts +92 -0
  80. package/_types/core/FrameReceipt.d.ts.map +1 -0
  81. package/_types/core/FrameSignature.d.ts +357 -0
  82. package/_types/core/FrameSignature.d.ts.map +1 -0
  83. package/_types/core/Transaction.d.ts +29 -4
  84. package/_types/core/Transaction.d.ts.map +1 -1
  85. package/_types/core/TransactionReceipt.d.ts +15 -6
  86. package/_types/core/TransactionReceipt.d.ts.map +1 -1
  87. package/_types/core/TransactionRequest.d.ts +10 -4
  88. package/_types/core/TransactionRequest.d.ts.map +1 -1
  89. package/_types/core/TxEnvelopeEip8141.d.ts +343 -0
  90. package/_types/core/TxEnvelopeEip8141.d.ts.map +1 -0
  91. package/_types/core/internal/curves.d.ts +19 -0
  92. package/_types/core/internal/curves.d.ts.map +1 -0
  93. package/_types/core/internal/quantity.d.ts +10 -0
  94. package/_types/core/internal/quantity.d.ts.map +1 -0
  95. package/_types/erc4337/UserOperationReceipt.d.ts +1 -1
  96. package/_types/erc4337/UserOperationReceipt.d.ts.map +1 -1
  97. package/_types/index.d.ts +73 -0
  98. package/_types/index.d.ts.map +1 -1
  99. package/_types/tempo/KeyAuthorization.d.ts +48 -38
  100. package/_types/tempo/KeyAuthorization.d.ts.map +1 -1
  101. package/_types/tempo/MultisigConfig.d.ts +15 -14
  102. package/_types/tempo/MultisigConfig.d.ts.map +1 -1
  103. package/_types/tempo/MultisigOperation.d.ts +3 -2
  104. package/_types/tempo/MultisigOperation.d.ts.map +1 -1
  105. package/_types/tempo/MultisigSimulation.d.ts +2 -49
  106. package/_types/tempo/MultisigSimulation.d.ts.map +1 -1
  107. package/_types/tempo/SignatureEnvelope.d.ts +69 -128
  108. package/_types/tempo/SignatureEnvelope.d.ts.map +1 -1
  109. package/_types/tempo/Transaction.d.ts +2 -0
  110. package/_types/tempo/Transaction.d.ts.map +1 -1
  111. package/_types/tempo/TransactionReceipt.d.ts +2 -0
  112. package/_types/tempo/TransactionReceipt.d.ts.map +1 -1
  113. package/_types/tempo/TransactionRequest.d.ts +5 -2
  114. package/_types/tempo/TransactionRequest.d.ts.map +1 -1
  115. package/_types/tempo/index.d.ts +3 -1
  116. package/_types/tempo/index.d.ts.map +1 -1
  117. package/_types/version.d.ts +1 -1
  118. package/core/Frame.ts +443 -0
  119. package/core/FrameReceipt.ts +117 -0
  120. package/core/FrameSignature.ts +606 -0
  121. package/core/Transaction.ts +73 -4
  122. package/core/TransactionReceipt.ts +41 -4
  123. package/core/TransactionRequest.ts +26 -2
  124. package/core/TxEnvelopeEip8141.ts +717 -0
  125. package/core/internal/curves.ts +22 -0
  126. package/core/internal/quantity.ts +16 -0
  127. package/erc4337/UserOperationReceipt.ts +1 -6
  128. package/index.ts +73 -0
  129. package/package.json +21 -1
  130. package/tempo/AuthorizationTempo.test.ts +8 -4
  131. package/tempo/KeyAuthorization.test-d.ts +68 -33
  132. package/tempo/KeyAuthorization.test.ts +89 -8
  133. package/tempo/KeyAuthorization.ts +124 -61
  134. package/tempo/MultisigConfig.test.ts +34 -26
  135. package/tempo/MultisigConfig.ts +54 -24
  136. package/tempo/MultisigOperation.test.ts +79 -550
  137. package/tempo/MultisigOperation.ts +31 -146
  138. package/tempo/MultisigSimulation.test-d.ts +2 -2
  139. package/tempo/MultisigSimulation.test.ts +21 -110
  140. package/tempo/MultisigSimulation.ts +25 -99
  141. package/tempo/SignatureEnvelope.test-d.ts +29 -67
  142. package/tempo/SignatureEnvelope.test.ts +301 -568
  143. package/tempo/SignatureEnvelope.ts +156 -274
  144. package/tempo/TransactionRequest.test-d.ts +6 -5
  145. package/tempo/TransactionRequest.test.ts +17 -80
  146. package/tempo/TransactionRequest.ts +23 -3
  147. package/tempo/index.ts +3 -1
  148. package/tempo/multisig.e2e.test.ts +322 -861
  149. package/version.ts +1 -1
@@ -0,0 +1,717 @@
1
+ import * as Address from './Address.js'
2
+ import * as Blobs from './Blobs.js'
3
+ import * as Bytes from './Bytes.js'
4
+ import * as Errors from './Errors.js'
5
+ import * as Frame from './Frame.js'
6
+ import * as FrameSignature from './FrameSignature.js'
7
+ import * as Hash from './Hash.js'
8
+ import * as Hex from './Hex.js'
9
+ import * as Quantity from './internal/quantity.js'
10
+ import type { Assign, Compute, PartialBy } from './internal/types.js'
11
+ import * as Rlp from './Rlp.js'
12
+
13
+ /** An EIP-8141 transaction containing independently budgeted call frames. */
14
+ export type TxEnvelopeEip8141 = {
15
+ /** Versioned blob hashes. @default [] */
16
+ blobVersionedHashes?: readonly Hex.Hex[] | undefined
17
+ /** Chain ID. Use bigint for IDs beyond the safe integer range. */
18
+ chainId: number | bigint
19
+ /** Frames to execute, in order. */
20
+ frames: readonly Frame.Frame[]
21
+ /** Maximum fee per blob gas, in wei. @default 0n */
22
+ maxFeePerBlobGas?: bigint | undefined
23
+ /** Maximum fee per gas, in wei. @default 0n */
24
+ maxFeePerGas?: bigint | undefined
25
+ /** Maximum priority fee per gas, in wei. @default 0n */
26
+ maxPriorityFeePerGas?: bigint | undefined
27
+ /** Sender nonce. @default 0n */
28
+ nonce?: bigint | undefined
29
+ /** Account authorizing execution. */
30
+ sender: Address.Address
31
+ /** PeerDAS sidecars. Excluded from transaction and signing hashes. */
32
+ sidecars?:
33
+ | {
34
+ blobs: readonly Hex.Hex[]
35
+ cellProofs: readonly Hex.Hex[]
36
+ commitments: readonly Hex.Hex[]
37
+ }
38
+ | undefined
39
+ /** Signature entries, including unsigned placeholders before signing. @default [] */
40
+ signatures?: readonly FrameSignature.FrameSignature[] | undefined
41
+ /** Transaction type. */
42
+ type: Type
43
+ }
44
+
45
+ /** JSON-RPC representation of an EIP-8141 envelope. */
46
+ export type Rpc = {
47
+ /** Versioned blob hashes. */
48
+ blobVersionedHashes: readonly Hex.Hex[]
49
+ /** Chain ID. */
50
+ chainId: Hex.Hex
51
+ /** Frames in execution order. */
52
+ frames: readonly Frame.Rpc[]
53
+ /** Account authorizing execution. */
54
+ from: Address.Address
55
+ /** Maximum fee per blob gas. */
56
+ maxFeePerBlobGas: Hex.Hex
57
+ /** Maximum fee per gas. */
58
+ maxFeePerGas: Hex.Hex
59
+ /** Maximum priority fee per gas. */
60
+ maxPriorityFeePerGas: Hex.Hex
61
+ /** Sender nonce. */
62
+ nonce: Hex.Hex
63
+ /** Frame signature entries. */
64
+ signatures: readonly FrameSignature.Rpc[]
65
+ /** RPC transaction type. */
66
+ type: '0x6'
67
+ }
68
+
69
+ /** Serialized EIP-8141 transaction envelope. */
70
+ export type Serialized = `${SerializedType}${string}`
71
+
72
+ /** Serialized EIP-8141 transaction type prefix. */
73
+ export const serializedType = '0x06' as const
74
+
75
+ /** Serialized EIP-8141 transaction type. */
76
+ export type SerializedType = typeof serializedType
77
+
78
+ /** EIP-8141 transaction type. */
79
+ export const type = 'eip8141' as const
80
+
81
+ /** EIP-8141 transaction type identifier. */
82
+ export type Type = typeof type
83
+
84
+ /**
85
+ * Asserts that an EIP-8141 envelope satisfies structural transaction constraints.
86
+ *
87
+ * Accepts unsigned signature entries for construction. Does not verify signatures,
88
+ * account authorization, balances, or blob proofs.
89
+ *
90
+ * @example
91
+ * ### Basic Usage
92
+ *
93
+ * ```ts twoslash
94
+ * import { TxEnvelopeEip8141 } from 'ox'
95
+ *
96
+ * TxEnvelopeEip8141.assert({
97
+ * chainId: 1,
98
+ * frames: [{ gas: 50_000n, mode: 'verify' }],
99
+ * sender: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
100
+ * })
101
+ * ```
102
+ *
103
+ * @param envelope - The transaction envelope to assert.
104
+ */
105
+ export function assert(envelope: PartialBy<TxEnvelopeEip8141, 'type'>): void {
106
+ const {
107
+ blobVersionedHashes = [],
108
+ chainId,
109
+ frames,
110
+ maxFeePerBlobGas = 0n,
111
+ maxFeePerGas = 0n,
112
+ maxPriorityFeePerGas = 0n,
113
+ nonce = 0n,
114
+ sender,
115
+ signatures = [],
116
+ } = envelope
117
+ if (
118
+ (typeof chainId !== 'bigint' &&
119
+ (typeof chainId !== 'number' || !Number.isSafeInteger(chainId))) ||
120
+ BigInt(chainId) < 0n ||
121
+ BigInt(chainId) >= 2n ** 256n
122
+ )
123
+ throw new InvalidError(
124
+ 'chainId must be an unsigned 256-bit integer; use bigint for large IDs.',
125
+ )
126
+ Address.assert(sender, { strict: false })
127
+ for (const [field, value, bits] of [
128
+ ['maxFeePerBlobGas', maxFeePerBlobGas, 256n],
129
+ ['maxFeePerGas', maxFeePerGas, 256n],
130
+ ['maxPriorityFeePerGas', maxPriorityFeePerGas, 256n],
131
+ ['nonce', nonce, 64n],
132
+ ] as const)
133
+ if (typeof value !== 'bigint' || value < 0n || value >= 2n ** bits)
134
+ throw new InvalidError(
135
+ `${field} must be an unsigned ${bits}-bit integer.`,
136
+ )
137
+ if (maxPriorityFeePerGas > maxFeePerGas)
138
+ throw new InvalidError('maxPriorityFeePerGas exceeds maxFeePerGas.')
139
+ if (!Array.isArray(frames) || frames.length === 0 || frames.length > 64)
140
+ throw new InvalidError('Expected between 1 and 64 frames.')
141
+ if (!Array.isArray(signatures) || !Array.isArray(blobVersionedHashes))
142
+ throw new InvalidError(
143
+ 'Signatures and blob versioned hashes must be lists.',
144
+ )
145
+ for (const hash of blobVersionedHashes) {
146
+ Hex.assert(hash, { strict: true })
147
+ if (hash.length !== 66 || Hex.slice(hash, 0, 1) !== '0x01')
148
+ throw new InvalidError(
149
+ 'Blob versioned hashes must contain 32 bytes and version 0x01.',
150
+ )
151
+ }
152
+ if (blobVersionedHashes.length === 0 && maxFeePerBlobGas !== 0n)
153
+ throw new InvalidError('maxFeePerBlobGas must be zero without blobs.')
154
+ let gas = 0n
155
+ let stateGas = 0n
156
+ let previousBatch = false
157
+ let expiry = false
158
+ let tokens = 0n
159
+ let size = 0n
160
+ let intrinsic = 12_000n + BigInt(frames.length) * 475n
161
+ const count = (data: Hex.Hex) => {
162
+ for (const byte of Hex.toBytes(data)) tokens += byte === 0 ? 1n : 4n
163
+ size += BigInt(Hex.size(data))
164
+ }
165
+ for (const [index, frame] of (frames as readonly Frame.Frame[]).entries()) {
166
+ Frame.assert(frame)
167
+ const flags =
168
+ typeof frame.flags === 'string'
169
+ ? Frame.flags[frame.flags]
170
+ : (frame.flags ?? 0)
171
+ const batch = (flags & 4) !== 0
172
+ if (
173
+ flags & 2 &&
174
+ frame.target !== undefined &&
175
+ !Address.isEqual(frame.target, sender)
176
+ )
177
+ throw new InvalidError('Execution approval must target the sender.')
178
+ if ((batch || previousBatch) && flags & 3)
179
+ throw new InvalidError('Atomic batches cannot contain approval frames.')
180
+ if (batch) {
181
+ const next = frames[index + 1]
182
+ if (!next || next.mode === 1 || next.mode === 'verify')
183
+ throw new InvalidError(
184
+ 'An atomic batch requires a following non-verify frame.',
185
+ )
186
+ }
187
+ previousBatch = batch
188
+ if (
189
+ (frame.mode === 1 || frame.mode === 'verify') &&
190
+ frame.target?.toLowerCase() ===
191
+ '0x0000000000000000000000000000000000008141'
192
+ ) {
193
+ if (
194
+ expiry ||
195
+ flags !== 0 ||
196
+ (frame.stateGas ?? 0n) !== 0n ||
197
+ Hex.size(frame.data ?? '0x') !== 8
198
+ )
199
+ throw new InvalidError('Invalid or duplicate expiry verifier frame.')
200
+ expiry = true
201
+ }
202
+ gas += frame.gas ?? 0n
203
+ stateGas += frame.stateGas ?? 0n
204
+ count(frame.data ?? '0x')
205
+ if (
206
+ (frame.value ?? 0n) > 0n &&
207
+ frame.target !== undefined &&
208
+ !Address.isEqual(frame.target, sender)
209
+ )
210
+ intrinsic += 6_000n
211
+ }
212
+ for (const entry of signatures) {
213
+ const [scheme, signer, payload, signature] = FrameSignature.toTuple(entry)
214
+ intrinsic += scheme === '0x' ? 100n : scheme === '0x01' ? 2_800n : 6_700n
215
+ count(signer)
216
+ count(payload)
217
+ count(signature)
218
+ }
219
+ if (gas + stateGas >= 2n ** 64n)
220
+ throw new InvalidError('Combined frame gas must be less than 2^64.')
221
+ if (
222
+ intrinsic + tokens * 4n + gas > 16_777_216n ||
223
+ intrinsic + size * 64n > 16_777_216n
224
+ )
225
+ throw new InvalidError('Transaction execution gas exceeds 16777216.')
226
+ }
227
+ export declare namespace assert {
228
+ type ErrorType =
229
+ | InvalidError
230
+ | Frame.assert.ErrorType
231
+ | FrameSignature.toTuple.ErrorType
232
+ | Address.assert.ErrorType
233
+ | Errors.GlobalErrorType
234
+ }
235
+
236
+ /**
237
+ * Deserializes an EIP-8141 transaction body or PeerDAS network wrapper.
238
+ *
239
+ * @example
240
+ * ### Basic Usage
241
+ *
242
+ * ```ts twoslash
243
+ * import { Frame, TxEnvelopeEip8141 } from 'ox'
244
+ *
245
+ * const envelope = TxEnvelopeEip8141.from({
246
+ * chainId: 1,
247
+ * frames: [Frame.from({})],
248
+ * sender: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
249
+ * })
250
+ *
251
+ * const serialized = TxEnvelopeEip8141.serialize(envelope)
252
+ * const decoded = TxEnvelopeEip8141.deserialize(serialized)
253
+ * ```
254
+ *
255
+ * @param serialized - Serialized transaction or network wrapper.
256
+ * @returns The transaction envelope.
257
+ */
258
+ export function deserialize(serialized: Serialized): TxEnvelopeEip8141 {
259
+ Hex.assert(serialized, { strict: true })
260
+ if (Hex.slice(serialized, 0, 1) !== serializedType)
261
+ throw new InvalidError('Expected transaction type 0x06.')
262
+ const decoded = Rlp.toHex(Hex.slice(serialized, 1))
263
+ if (!Array.isArray(decoded))
264
+ throw new InvalidError('Expected a transaction list.')
265
+ const wrapped = Array.isArray(decoded[0])
266
+ const body = wrapped ? decoded[0] : decoded
267
+ if (!Array.isArray(body) || body.length !== 7)
268
+ throw new InvalidError('Expected seven transaction fields.')
269
+ const [chainId, nonce, sender, frames, signatures, fees, hashes] = body
270
+ if (
271
+ typeof sender !== 'string' ||
272
+ !Array.isArray(frames) ||
273
+ !Array.isArray(signatures) ||
274
+ !Array.isArray(fees) ||
275
+ fees.length !== 3 ||
276
+ !Array.isArray(hashes) ||
277
+ hashes.some((hash) => typeof hash !== 'string')
278
+ )
279
+ throw new InvalidError('Invalid transaction field shape.')
280
+ const integer = (value: unknown): bigint => {
281
+ if (
282
+ typeof value !== 'string' ||
283
+ (value !== '0x' && value.startsWith('0x00'))
284
+ )
285
+ throw new InvalidError('Expected a minimally encoded integer.')
286
+ return value === '0x' ? 0n : Hex.toBigInt(value as Hex.Hex)
287
+ }
288
+ const id = integer(chainId)
289
+ const envelope: TxEnvelopeEip8141 = {
290
+ blobVersionedHashes: hashes as Hex.Hex[],
291
+ chainId: id <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(id) : id,
292
+ frames: frames.map((frame) => Frame.fromTuple(frame as Frame.Tuple)),
293
+ maxFeePerBlobGas: integer(fees[2]),
294
+ maxFeePerGas: integer(fees[1]),
295
+ maxPriorityFeePerGas: integer(fees[0]),
296
+ nonce: integer(nonce),
297
+ sender: sender as Address.Address,
298
+ signatures: signatures.map((entry) =>
299
+ FrameSignature.fromTuple(entry as FrameSignature.Tuple),
300
+ ),
301
+ type,
302
+ }
303
+ if (wrapped) {
304
+ if (
305
+ decoded.length !== 5 ||
306
+ decoded[1] !== '0x01' ||
307
+ !Array.isArray(decoded[2]) ||
308
+ !Array.isArray(decoded[3]) ||
309
+ !Array.isArray(decoded[4])
310
+ )
311
+ throw new InvalidError('Expected a version 1 PeerDAS wrapper.')
312
+ envelope.sidecars = {
313
+ blobs: decoded[2] as Hex.Hex[],
314
+ cellProofs: decoded[4] as Hex.Hex[],
315
+ commitments: decoded[3] as Hex.Hex[],
316
+ }
317
+ }
318
+ assert(envelope)
319
+ // Re-encoding rejects nonminimal RLP lengths as well as malformed nested lists.
320
+ if (serialize(envelope).toLowerCase() !== serialized.toLowerCase())
321
+ throw new InvalidError('Noncanonical transaction encoding.')
322
+ return envelope
323
+ }
324
+ export declare namespace deserialize {
325
+ type ErrorType =
326
+ | assert.ErrorType
327
+ | Hex.slice.ErrorType
328
+ | Rlp.toHex.ErrorType
329
+ | serialize.ErrorType
330
+ | Errors.GlobalErrorType
331
+ }
332
+
333
+ /**
334
+ * Creates an EIP-8141 transaction envelope from an object or serialized transaction.
335
+ *
336
+ * @example
337
+ * ### Basic Usage
338
+ *
339
+ * ```ts twoslash
340
+ * import { TxEnvelopeEip8141 } from 'ox'
341
+ *
342
+ * const envelope = TxEnvelopeEip8141.from({
343
+ * chainId: 1,
344
+ * frames: [
345
+ * {
346
+ * flags: 'approveExecutionAndPayment',
347
+ * gas: 50_000n,
348
+ * mode: 'verify'
349
+ * }
350
+ * ],
351
+ * sender: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
352
+ * })
353
+ * ```
354
+ *
355
+ * @param envelope - An envelope object or serialized transaction.
356
+ * @returns The envelope with its transaction type.
357
+ */
358
+ export function from<
359
+ const envelope extends PartialBy<TxEnvelopeEip8141, 'type'> | Serialized,
360
+ >(envelope: envelope): from.ReturnType<envelope> {
361
+ const value = (
362
+ typeof envelope === 'string' ? deserialize(envelope) : envelope
363
+ ) as PartialBy<TxEnvelopeEip8141, 'type'>
364
+ assert(value)
365
+ return { ...value, type } as from.ReturnType<envelope>
366
+ }
367
+ export declare namespace from {
368
+ type ReturnType<
369
+ envelope extends PartialBy<TxEnvelopeEip8141, 'type'> | Serialized,
370
+ > = envelope extends Serialized
371
+ ? TxEnvelopeEip8141
372
+ : Compute<Assign<envelope, { readonly type: Type }>>
373
+ type ErrorType =
374
+ | assert.ErrorType
375
+ | deserialize.ErrorType
376
+ | Errors.GlobalErrorType
377
+ }
378
+
379
+ /**
380
+ * Converts an RPC envelope to an EIP-8141 envelope without losing chain ID precision.
381
+ *
382
+ * @example
383
+ * ### Basic Usage
384
+ *
385
+ * ```ts twoslash
386
+ * import { Frame, TxEnvelopeEip8141 } from 'ox'
387
+ *
388
+ * const rpc = TxEnvelopeEip8141.toRpc(
389
+ * TxEnvelopeEip8141.from({
390
+ * chainId: 1,
391
+ * frames: [Frame.from({})],
392
+ * sender: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
393
+ * })
394
+ * )
395
+ * const envelope = TxEnvelopeEip8141.fromRpc(rpc)
396
+ * ```
397
+ *
398
+ * @param envelope - The value to convert.
399
+ * @returns The converted value.
400
+ */
401
+ export function fromRpc(envelope: Rpc): TxEnvelopeEip8141 {
402
+ const chainId = Hex.toBigInt(envelope.chainId)
403
+ return from({
404
+ blobVersionedHashes: envelope.blobVersionedHashes,
405
+ chainId:
406
+ chainId <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(chainId) : chainId,
407
+ frames: envelope.frames.map(Frame.fromRpc),
408
+ maxFeePerBlobGas: Hex.toBigInt(envelope.maxFeePerBlobGas),
409
+ maxFeePerGas: Hex.toBigInt(envelope.maxFeePerGas),
410
+ maxPriorityFeePerGas: Hex.toBigInt(envelope.maxPriorityFeePerGas),
411
+ nonce: Hex.toBigInt(envelope.nonce),
412
+ sender: envelope.from,
413
+ signatures: envelope.signatures.map(FrameSignature.fromRpc),
414
+ })
415
+ }
416
+
417
+ export declare namespace fromRpc {
418
+ type ErrorType =
419
+ | from.ErrorType
420
+ | Frame.fromRpc.ErrorType
421
+ | FrameSignature.fromRpc.ErrorType
422
+ | Hex.toBigInt.ErrorType
423
+ }
424
+
425
+ /**
426
+ * Converts an EIP-8141 envelope to RPC fields. Sidecars are excluded.
427
+ *
428
+ * @example
429
+ * ### Basic Usage
430
+ *
431
+ * ```ts twoslash
432
+ * import { Frame, TxEnvelopeEip8141 } from 'ox'
433
+ *
434
+ * const envelope = TxEnvelopeEip8141.from({
435
+ * chainId: 1,
436
+ * frames: [Frame.from({})],
437
+ * sender: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
438
+ * })
439
+ * const rpc = TxEnvelopeEip8141.toRpc(envelope)
440
+ * ```
441
+ *
442
+ * @param envelope - The value to convert.
443
+ * @returns The converted value.
444
+ */
445
+ export function toRpc(envelope: toRpc.Input): Rpc {
446
+ return {
447
+ blobVersionedHashes: envelope.blobVersionedHashes ?? [],
448
+ chainId: Quantity.fromNumberish(envelope.chainId),
449
+ frames: envelope.frames.map(Frame.toRpc),
450
+ from: envelope.sender,
451
+ maxFeePerBlobGas: Quantity.fromNumberish(envelope.maxFeePerBlobGas ?? 0n),
452
+ maxFeePerGas: Quantity.fromNumberish(envelope.maxFeePerGas ?? 0n),
453
+ maxPriorityFeePerGas: Quantity.fromNumberish(
454
+ envelope.maxPriorityFeePerGas ?? 0n,
455
+ ),
456
+ nonce: Quantity.fromNumberish(envelope.nonce ?? 0n),
457
+ signatures: (envelope.signatures ?? []).map(FrameSignature.toRpc),
458
+ type: '0x6',
459
+ }
460
+ }
461
+
462
+ export declare namespace toRpc {
463
+ type Input = Omit<
464
+ TxEnvelopeEip8141,
465
+ | 'chainId'
466
+ | 'frames'
467
+ | 'maxFeePerBlobGas'
468
+ | 'maxFeePerGas'
469
+ | 'maxPriorityFeePerGas'
470
+ | 'nonce'
471
+ | 'type'
472
+ > & {
473
+ chainId: Hex.Hex | bigint | number
474
+ frames: readonly Frame.toRpc.Input[]
475
+ maxFeePerBlobGas?: Hex.Hex | bigint | number | undefined
476
+ maxFeePerGas?: Hex.Hex | bigint | number | undefined
477
+ maxPriorityFeePerGas?: Hex.Hex | bigint | number | undefined
478
+ nonce?: Hex.Hex | bigint | number | undefined
479
+ type?: Type | undefined
480
+ }
481
+ type ErrorType =
482
+ | Frame.toRpc.ErrorType
483
+ | FrameSignature.toRpc.ErrorType
484
+ | Hex.fromNumber.ErrorType
485
+ }
486
+
487
+ /**
488
+ * Returns the canonical signing payload of an EIP-8141 envelope.
489
+ *
490
+ * Elides signature bytes only for entries with an empty payload. Explicit-payload
491
+ * signatures remain committed. The envelope is not mutated.
492
+ *
493
+ * @example
494
+ * ### Signing
495
+ *
496
+ * ```ts twoslash
497
+ * import {
498
+ * Address,
499
+ * FrameSignature,
500
+ * Secp256k1,
501
+ * TxEnvelopeEip8141
502
+ * } from 'ox'
503
+ *
504
+ * const privateKey = Secp256k1.randomPrivateKey()
505
+ * const sender = Address.fromPublicKey(
506
+ * Secp256k1.getPublicKey({ privateKey })
507
+ * )
508
+ * const envelope = TxEnvelopeEip8141.from({
509
+ * chainId: 1,
510
+ * frames: [
511
+ * {
512
+ * flags: 'approveExecutionAndPayment',
513
+ * gas: 50_000n,
514
+ * mode: 'verify'
515
+ * }
516
+ * ],
517
+ * sender,
518
+ * signatures: [FrameSignature.from({ scheme: 'secp256k1' })]
519
+ * })
520
+ *
521
+ * const payload = TxEnvelopeEip8141.getSignPayload(envelope)
522
+ * const signature = Secp256k1.sign({ payload, privateKey })
523
+ * const signed = TxEnvelopeEip8141.from({
524
+ * ...envelope,
525
+ * signatures: [
526
+ * FrameSignature.from({ scheme: 'secp256k1', signature })
527
+ * ]
528
+ * })
529
+ * ```
530
+ *
531
+ * @param envelope - The transaction envelope to sign.
532
+ * @returns The canonical signing digest.
533
+ */
534
+ export function getSignPayload(
535
+ envelope: PartialBy<TxEnvelopeEip8141, 'type'>,
536
+ ): Hex.Hex {
537
+ return hash(envelope, { presign: true })
538
+ }
539
+ export declare namespace getSignPayload {
540
+ type ErrorType = hash.ErrorType
541
+ }
542
+
543
+ /**
544
+ * Hashes an EIP-8141 transaction, excluding its blob sidecars.
545
+ *
546
+ * @example
547
+ * ### Basic Usage
548
+ *
549
+ * ```ts twoslash
550
+ * import { Frame, TxEnvelopeEip8141 } from 'ox'
551
+ *
552
+ * const hash = TxEnvelopeEip8141.hash({
553
+ * chainId: 1,
554
+ * frames: [Frame.from({})],
555
+ * sender: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
556
+ * })
557
+ * ```
558
+ *
559
+ * @param envelope - The transaction envelope to hash.
560
+ * @param options - Hashing options.
561
+ * @returns The transaction hash or signing digest.
562
+ */
563
+ export function hash(
564
+ envelope: PartialBy<TxEnvelopeEip8141, 'type'>,
565
+ options: hash.Options = {},
566
+ ): Hex.Hex {
567
+ assert(envelope)
568
+ const body = toTuple(envelope)
569
+ if (options.presign)
570
+ body[4] = body[4].map((entry) =>
571
+ entry[2] === '0x' ? [entry[0], entry[1], entry[2], '0x'] : entry,
572
+ )
573
+ return Hash.keccak256(Hex.concat(serializedType, Rlp.fromHex(body)))
574
+ }
575
+ export declare namespace hash {
576
+ type Options = {
577
+ /** Whether to compute the canonical signing digest. @default false */
578
+ presign?: boolean | undefined
579
+ }
580
+ type ErrorType =
581
+ | assert.ErrorType
582
+ | Hash.keccak256.ErrorType
583
+ | Rlp.fromHex.ErrorType
584
+ | Errors.GlobalErrorType
585
+ }
586
+
587
+ /**
588
+ * Serializes an EIP-8141 envelope, including a PeerDAS wrapper when sidecars exist.
589
+ *
590
+ * @example
591
+ * ### Basic Usage
592
+ *
593
+ * ```ts twoslash
594
+ * import { TxEnvelopeEip8141 } from 'ox'
595
+ *
596
+ * const envelope = TxEnvelopeEip8141.from({
597
+ * chainId: 1,
598
+ * frames: [{ gas: 50_000n, mode: 'sender' }],
599
+ * sender: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
600
+ * })
601
+ * const serialized = TxEnvelopeEip8141.serialize(envelope)
602
+ * ```
603
+ *
604
+ * @param envelope - The transaction envelope to serialize.
605
+ * @returns The serialized transaction.
606
+ */
607
+ export function serialize(
608
+ envelope: PartialBy<TxEnvelopeEip8141, 'type'>,
609
+ ): Serialized {
610
+ assert(envelope)
611
+ const body = toTuple(envelope)
612
+ const { sidecars } = envelope
613
+ if (sidecars) {
614
+ const { blobs, cellProofs, commitments } = sidecars
615
+ const hashes = envelope.blobVersionedHashes ?? []
616
+ if (
617
+ hashes.length === 0 ||
618
+ blobs.length !== hashes.length ||
619
+ commitments.length !== hashes.length ||
620
+ cellProofs.length !== hashes.length * 128
621
+ )
622
+ throw new InvalidError('PeerDAS sidecar counts do not match blob hashes.')
623
+ for (const [index, blob] of blobs.entries()) {
624
+ Hex.assert(blob, { strict: true })
625
+ const commitment = commitments[index]!
626
+ Hex.assert(commitment, { strict: true })
627
+ if (
628
+ blob.length !== 2 + Blobs.bytesPerBlob * 2 ||
629
+ commitment.length !== 98 ||
630
+ Blobs.commitmentToVersionedHash(commitment).toLowerCase() !==
631
+ hashes[index]!.toLowerCase()
632
+ )
633
+ throw new InvalidError('Invalid blob size or commitment.')
634
+ }
635
+ for (const proof of cellProofs) {
636
+ Hex.assert(proof, { strict: true })
637
+ if (proof.length !== 98)
638
+ throw new InvalidError('Cell proofs must contain 48 bytes.')
639
+ }
640
+ return Hex.concat(
641
+ serializedType,
642
+ Rlp.fromHex([body, '0x01', blobs, commitments, cellProofs]),
643
+ ) as Serialized
644
+ }
645
+ return Hex.concat(serializedType, Rlp.fromHex(body)) as Serialized
646
+ }
647
+ export declare namespace serialize {
648
+ type ErrorType =
649
+ | assert.ErrorType
650
+ | Rlp.fromHex.ErrorType
651
+ | Hex.concat.ErrorType
652
+ | Errors.GlobalErrorType
653
+ }
654
+
655
+ /**
656
+ * Returns whether an EIP-8141 envelope satisfies structural constraints.
657
+ *
658
+ * @example
659
+ * ### Basic Usage
660
+ *
661
+ * ```ts twoslash
662
+ * import { Frame, TxEnvelopeEip8141 } from 'ox'
663
+ *
664
+ * const valid = TxEnvelopeEip8141.validate({
665
+ * chainId: 1,
666
+ * frames: [Frame.from({})],
667
+ * sender: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
668
+ * })
669
+ * ```
670
+ *
671
+ * @param envelope - The transaction envelope to validate.
672
+ * @returns Whether the envelope is structurally valid.
673
+ */
674
+ export function validate(
675
+ envelope: PartialBy<TxEnvelopeEip8141, 'type'>,
676
+ ): boolean {
677
+ try {
678
+ assert(envelope)
679
+ return true
680
+ } catch {
681
+ return false
682
+ }
683
+ }
684
+ export declare namespace validate {
685
+ type ErrorType = Errors.GlobalErrorType
686
+ }
687
+
688
+ /** Thrown when an EIP-8141 envelope is structurally invalid. */
689
+ export class InvalidError extends Errors.BaseError {
690
+ override readonly name = 'TxEnvelopeEip8141.InvalidError'
691
+ }
692
+
693
+ function toTuple(envelope: PartialBy<TxEnvelopeEip8141, 'type'>) {
694
+ const quantity = (value: bigint | number | undefined): Hex.Hex =>
695
+ value ? Hex.fromBytes(Bytes.fromNumber(value)) : '0x'
696
+ return [
697
+ quantity(envelope.chainId),
698
+ quantity(envelope.nonce),
699
+ envelope.sender,
700
+ envelope.frames.map((frame) => Frame.toTuple(frame)),
701
+ (envelope.signatures ?? []).map((entry) => FrameSignature.toTuple(entry)),
702
+ [
703
+ quantity(envelope.maxPriorityFeePerGas),
704
+ quantity(envelope.maxFeePerGas),
705
+ quantity(envelope.maxFeePerBlobGas),
706
+ ],
707
+ envelope.blobVersionedHashes ?? [],
708
+ ] satisfies [
709
+ Hex.Hex,
710
+ Hex.Hex,
711
+ Address.Address,
712
+ Frame.Tuple[],
713
+ FrameSignature.Tuple[],
714
+ Hex.Hex[],
715
+ readonly Hex.Hex[],
716
+ ]
717
+ }