ox 0.14.45 → 0.14.47

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