TransactionEnvelopeEip4844
Utility functions for working with EIP-4844 Typed Transaction Envelopes
Examples
Below are some examples demonstrating common usages of the TransactionEnvelopeEip4844
module:
Instantiating Blobs
Blobs can be instantiated using Blobs.from
:
import { Blobs, Hex } from 'ox'
const blobs = Blobs.from(Hex.fromString('Hello World!'))
Instantiating
Transaction Envelopes can be instantiated using TransactionEnvelopeEip4844.from
:
import { Blobs, Hex, TransactionEnvelopeEip4844, Value } from 'ox'
import { kzg } from './kzg'
const blobs = Blobs.from(Hex.fromString('Hello World!'))
const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg })
const envelope = TransactionEnvelopeEip4844.from({
chainId: 1,
blobVersionedHashes,
maxFeePerBlobGas: Value.fromGwei('3'),
maxFeePerGas: Value.fromGwei('10'),
maxPriorityFeePerGas: Value.fromGwei('1'),
to: '0x0000000000000000000000000000000000000000',
value: Value.fromEther('1'),
})
Signing
Transaction Envelopes can be signed using TransactionEnvelopeEip4844.getSignPayload
and a signing function such as Secp256k1.sign
or P256.sign
:
import { Blobs, Secp256k1, TransactionEnvelopeEip4844 } from 'ox'
import { kzg } from './kzg'
const blobs = Blobs.from('0xdeadbeef')
const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg })
const sidecars = Blobs.toSidecars(blobs, { kzg })
const envelope = TransactionEnvelopeEip4844.from({
blobVersionedHashes,
chainId: 1,
nonce: 0n,
maxFeePerBlobGas: Value.fromGwei('3'),
maxFeePerGas: Value.fromGwei('10'),
maxPriorityFeePerGas: Value.fromGwei('1'),
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: Value.fromEther('1'),
})
const signature = Secp256k1.sign({
payload: TransactionEnvelopeEip4844.getSignPayload(envelope),
privateKey: '0x...'
})
const envelope_signed = TransactionEnvelopeEip4844.from(envelope, {
sidecars,
signature
})
Serializing
Transaction Envelopes can be serialized using TransactionEnvelopeEip4844.serialize
:
import { Blobs, TransactionEnvelopeEip4844 } from 'ox'
import { kzg } from './kzg'
const blobs = Blobs.from('0xdeadbeef')
const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg })
const envelope = TransactionEnvelopeEip4844.from({
blobVersionedHashes,
chainId: 1,
maxFeePerBlobGas: Value.fromGwei('3'),
maxFeePerGas: Value.fromGwei('10'),
maxPriorityFeePerGas: Value.fromGwei('1'),
to: '0x0000000000000000000000000000000000000000',
value: Value.fromEther('1'),
})
const serialized = TransactionEnvelopeEip4844.serialize(envelope)
Sending
We can send a Transaction Envelope to the network by serializing the signed envelope with .serialize
, and then broadcasting it over JSON-RPC with eth_sendRawTransaction
.
In this example, we will use RpcTransport.fromHttp
to broadcast a eth_sendRawTransaction
request over HTTP JSON-RPC.
import { Blobs, RpcTransport, TransactionEnvelopeEip4844, Secp256k1, Value } from 'ox'
import { kzg } from './kzg'
// Compute the Blob Versioned Hashes.
const blobs = Blobs.from('0xdeadbeef')
const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg })
const sidecars = Blobs.toSidecars(blobs, { kzg })
// Construct the Envelope.
const envelope = TransactionEnvelopeEip4844.from({
chainId: 1,
blobVersionedHashes,
maxFeePerBlobGas: Value.fromGwei('3'),
maxFeePerGas: Value.fromGwei('10'),
maxPriorityFeePerGas: Value.fromGwei('1'),
nonce: 0n,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: Value.fromEther('1.5'),
})
// Sign over the Envelope.
const signature = Secp256k1.sign({
payload: TransactionEnvelopeEip4844.getSignPayload(envelope),
privateKey: '0x...',
})
// Serialize the Envelope with the Signature.
const serialized = TransactionEnvelopeEip4844.serialize(envelope, {
sidecars,
signature
})
// Broadcast the Envelope with `eth_sendRawTransaction`.
const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com')
const hash = await transport.request({
method: 'eth_sendRawTransaction',
params: [serialized],
})
Computing Hashes
Transaction Hashes can be computed using TransactionEnvelopeEip4844.hash
:
import { Blobs, Secp256k1, TransactionEnvelopeEip4844, Value } from 'ox'
import { kzg } from './kzg'
const blobs = Blobs.from('0xdeadbeef')
const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg })
const envelope = TransactionEnvelopeEip4844.from({
blobVersionedHashes,
chainId: 1,
maxFeePerGas: Value.fromGwei('10'),
to: '0x0000000000000000000000000000000000000000',
value: Value.fromEther('1'),
})
const signature = Secp256k1.sign({
payload: TransactionEnvelopeEip4844.getSignPayload(envelope),
privateKey: '0x...'
})
const envelope_signed = TransactionEnvelopeEip4844.from(envelope, { signature })
const hash = TransactionEnvelopeEip4844.hash(envelope_signed)