ox 1.7.5 → 1.8.1

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