# TxEnvelopeEip7702

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

## Examples

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

* [Instantiating](#instantiating)

* [Signing](#signing)

* [Sending](#sending)

### Instantiating

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

```ts twoslash
import {
  Authorization,
  Secp256k1,
  TxEnvelopeEip7702,
  Value
} from 'ox'

const authorization = Authorization.from({
  address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  chainId: 1,
  nonce: 0n
})

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

const authorizationList = [
  Authorization.from(authorization, { signature })
]

const envelope = TxEnvelopeEip7702.from({
  // [!code focus]
  authorizationList, // [!code focus]
  chainId: 1, // [!code focus]
  maxFeePerGas: Value.fromGwei('10'), // [!code focus]
  maxPriorityFeePerGas: Value.fromGwei('1'), // [!code focus]
  to: '0x0000000000000000000000000000000000000000', // [!code focus]
  value: Value.fromEther('1') // [!code focus]
}) // [!code focus]
```

:::tip
See [`Authorization`](/api/) for more details on instantiating and signing EIP-7702 Authorizations.
:::

### Signing

Transaction Envelopes can be signed using [`TxEnvelopeEip7702.getSignPayload`](/api/TxEnvelopeEip7702/getSignPayload) and a signing function such as [`Secp256k1.sign`](/api/Secp256k1/sign) or [`P256.sign`](/api/P256/sign):

```ts twoslash
import {
  Authorization,
  Secp256k1,
  TxEnvelopeEip7702,
  Value
} from 'ox'

const authorization = Authorization.from({
  address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  chainId: 1,
  nonce: 0n
})

const signature_auth = Secp256k1.sign({
  payload: Authorization.getSignPayload(authorization),
  privateKey: '0x...'
})

const authorizationList = [
  Authorization.from(authorization, {
    signature: signature_auth
  })
]

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

const signature = Secp256k1.sign({
  // [!code focus]
  payload: TxEnvelopeEip7702.getSignPayload(envelope), // [!code focus]
  privateKey: '0x...' // [!code focus]
})

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

### 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
import {
  Authorization,
  RpcTransport,
  TxEnvelopeEip7702,
  Secp256k1,
  Value
} from 'ox'

const authorization = Authorization.from({
  address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  chainId: 1,
  nonce: 0n
})

const signature_auth = Secp256k1.sign({
  payload: Authorization.getSignPayload(authorization),
  privateKey: '0x...'
})

const authorizationList = [
  Authorization.from(authorization, {
    signature: signature_auth
  })
]

const envelope = TxEnvelopeEip7702.from({
  authorizationList,
  chainId: 1,
  maxFeePerGas: Value.fromGwei('10'),
  maxPriorityFeePerGas: Value.fromGwei('1'),
  nonce: 69n,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: Value.fromEther('1.5')
})

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

// Serialize the Envelope with the Signature. // [!code focus]
const serialized = TxEnvelopeEip7702.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]
```

## Functions

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

## Types

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`TxEnvelopeEip7702.Rpc`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702rpc) |  |
| [`TxEnvelopeEip7702.Serialized`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702serialized) |  |
| [`TxEnvelopeEip7702.SerializedType`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702serializedtype) |  |
| [`TxEnvelopeEip7702.Signed`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702signed) |  |
| [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702txenvelopeeip7702) |  |
| [`TxEnvelopeEip7702.Type`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702type) |  |
