# TxEnvelopeEip4844

Utility functions for working with [EIP-4844 Typed Transaction Envelopes](https://eips.ethereum.org/EIPS/eip-4844)

## Examples

Below are some examples demonstrating common usages of the `TxEnvelopeEip4844` module:

* [Instantiating Blobs](#instantiating-blobs)

* [Instantiating](#instantiating)

* [Signing](#signing)

* [Serializing](#serializing)

* [Sending](#sending)

* [Computing Hashes](#computing-hashes)

### Instantiating Blobs

Blobs can be instantiated using [`Blobs.from`](/api/Blobs/from):

```ts twoslash
import { Blobs, Hex } from 'ox'

const blobs = Blobs.from(Hex.fromString('Hello World!'))
```

### Instantiating

Transaction Envelopes can be instantiated using [`TxEnvelopeEip4844.from`](/api/TxEnvelopeEip4844/from):

```ts twoslash
// @noErrors
import { Blobs, Hex, TxEnvelopeEip4844, Value } from 'ox'
import { kzg } from './kzg'

const blobs = Blobs.from(Hex.fromString('Hello World!'))
const blobVersionedHashes = Blobs.toVersionedHashes(blobs, {
  kzg
})

const envelope = TxEnvelopeEip4844.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 [`TxEnvelopeEip4844.getSignPayload`](/api/TxEnvelopeEip4844/getSignPayload) and a signing function such as [`Secp256k1.sign`](/api/Secp256k1/sign) or [`P256.sign`](/api/P256/sign):

```ts twoslash
// @noErrors
import { Blobs, Secp256k1, TxEnvelopeEip4844 } from 'ox'
import { kzg } from './kzg'

const blobs = Blobs.from('0xdeadbeef')
const blobVersionedHashes = Blobs.toVersionedHashes(blobs, {
  kzg
})

const envelope = TxEnvelopeEip4844.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({
  // [!code focus]
  payload: TxEnvelopeEip4844.getSignPayload(envelope), // [!code focus]
  privateKey: '0x...' // [!code focus]
}) // [!code focus]

const envelope_signed = TxEnvelopeEip4844.from(envelope, {
  signature
})
```

### Serializing

Transaction Envelopes can be serialized using [`TxEnvelopeEip4844.serialize`](/api/TxEnvelopeEip4844/serialize):

```ts twoslash
// @noErrors
import { Blobs, TxEnvelopeEip4844 } from 'ox'
import { kzg } from './kzg'

const blobs = Blobs.from('0xdeadbeef')
const blobVersionedHashes = Blobs.toVersionedHashes(blobs, {
  kzg
})

const envelope = TxEnvelopeEip4844.from({
  blobVersionedHashes,
  chainId: 1,
  maxFeePerBlobGas: Value.fromGwei('3'),
  maxFeePerGas: Value.fromGwei('10'),
  maxPriorityFeePerGas: Value.fromGwei('1'),
  to: '0x0000000000000000000000000000000000000000',
  value: Value.fromEther('1')
})

const serialized = TxEnvelopeEip4844.serialize(envelope) // [!code focus]
```

### 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`](/api/RpcTransport/fromHttp) to broadcast a `eth_sendRawTransaction` request over HTTP JSON-RPC.

```ts twoslash
// @noErrors
import {
  Blobs,
  RpcTransport,
  TxEnvelopeEip4844,
  Secp256k1,
  Value
} from 'ox'
import { kzg } from './kzg'

// Compute the Blob Versioned Hashes.
const blobs = Blobs.from('0xdeadbeef')
const blobVersionedHashes = Blobs.toVersionedHashes(blobs, {
  kzg
})

// Construct the Envelope.
const envelope = TxEnvelopeEip4844.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: TxEnvelopeEip4844.getSignPayload(envelope),
  privateKey: '0x...'
})

// Serialize the Envelope with the Signature. // [!code focus]
const serialized = TxEnvelopeEip4844.serialize(envelope, {
  // [!code focus]
  signature // [!code focus]
}) // [!code focus]

// Broadcast the Envelope with `eth_sendRawTransaction`. // [!code focus]
const transport = RpcTransport.fromHttp(
  'https://1.rpc.thirdweb.com'
) // [!code focus]
const hash = await transport.request({
  // [!code focus]
  method: 'eth_sendRawTransaction', // [!code focus]
  params: [serialized] // [!code focus]
}) // [!code focus]
```

### Computing Hashes

Transaction Hashes can be computed using [`TxEnvelopeEip4844.hash`](/api/TxEnvelopeEip4844/hash):

```ts twoslash
// @noErrors
import {
  Blobs,
  Secp256k1,
  TxEnvelopeEip4844,
  Value
} from 'ox'
import { kzg } from './kzg'

const blobs = Blobs.from('0xdeadbeef')
const blobVersionedHashes = Blobs.toVersionedHashes(blobs, {
  kzg
})

const envelope = TxEnvelopeEip4844.from({
  blobVersionedHashes,
  chainId: 1,
  maxFeePerGas: Value.fromGwei('10'),
  to: '0x0000000000000000000000000000000000000000',
  value: Value.fromEther('1')
})

const signature = Secp256k1.sign({
  payload: TxEnvelopeEip4844.getSignPayload(envelope),
  privateKey: '0x...'
})

const envelope_signed = TxEnvelopeEip4844.from(envelope, {
  signature
})

const hash = TxEnvelopeEip4844.hash(envelope_signed) // [!code focus]
```

## Functions

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`TxEnvelopeEip4844.assert`](/api/TxEnvelopeEip4844/assert) | Asserts a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844) is valid. |
| [`TxEnvelopeEip4844.deserialize`](/api/TxEnvelopeEip4844/deserialize) | Deserializes a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844) from its serialized form. |
| [`TxEnvelopeEip4844.from`](/api/TxEnvelopeEip4844/from) | Converts an arbitrary transaction object into an EIP-4844 Transaction Envelope. |
| [`TxEnvelopeEip4844.getSignPayload`](/api/TxEnvelopeEip4844/getSignPayload) | Returns the payload to sign for a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844). |
| [`TxEnvelopeEip4844.hash`](/api/TxEnvelopeEip4844/hash) | Hashes a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844). This is the "transaction hash". |
| [`TxEnvelopeEip4844.serialize`](/api/TxEnvelopeEip4844/serialize) | Serializes a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844). |
| [`TxEnvelopeEip4844.toRpc`](/api/TxEnvelopeEip4844/toRpc) | Converts an [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844) to an [`TxEnvelopeEip4844.Rpc`](/api/TxEnvelopeEip4844/types#rpc). |
| [`TxEnvelopeEip4844.validate`](/api/TxEnvelopeEip4844/validate) | Validates a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844). Returns `true` if the envelope is valid, `false` otherwise. |

## Errors

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`TxEnvelopeEip4844.LegacyBlobSidecarWrapperError`](/api/TxEnvelopeEip4844/errors#txenvelopeeip4844legacyblobsidecarwrappererror) | Thrown when attempting to deserialize a legacy 4-element EIP-4844 network wrapper. Only the PeerDAS (EIP-7594) 5-element wrapper with `wrapper_version = 0x01` is accepted. |

## Types

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`TxEnvelopeEip4844.Rpc`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844rpc) |  |
| [`TxEnvelopeEip4844.Serialized`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844serialized) |  |
| [`TxEnvelopeEip4844.SerializedType`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844serializedtype) |  |
| [`TxEnvelopeEip4844.Sidecars`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844sidecars) | PeerDAS (EIP-7594) sidecars carried alongside a type-3 transaction in the `PooledTransactions` p2p message. |
| [`TxEnvelopeEip4844.Signed`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844signed) |  |
| [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844txenvelopeeip4844) |  |
| [`TxEnvelopeEip4844.Type`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844type) |  |
