Skip to content

TransactionEnvelopeEip7702

Utility functions for working with EIP-7702 Typed Transaction Envelopes

Examples

Below are some examples demonstrating common usages of the TransactionEnvelopeEip7702 module:

Instantiating

Transaction Envelopes can be instantiated using TransactionEnvelopeEip7702.from:

import { Authorization, Secp256k1, TransactionEnvelopeEip7702, 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 = TransactionEnvelopeEip7702.from({ 
  authorizationList, 
  chainId: 1, 
  maxFeePerGas: Value.fromGwei('10'), 
  maxPriorityFeePerGas: Value.fromGwei('1'), 
  to: '0x0000000000000000000000000000000000000000', 
  value: Value.fromEther('1'), 
})

Signing

Transaction Envelopes can be signed using TransactionEnvelopeEip7702.getSignPayload and a signing function such as Secp256k1.sign or P256.sign:

import { Authorization, Secp256k1, TransactionEnvelopeEip7702, 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 = TransactionEnvelopeEip7702.from({
  authorizationList,
  chainId: 1,
  maxFeePerGas: Value.fromGwei('10'),
  maxPriorityFeePerGas: Value.fromGwei('1'),
  to: '0x0000000000000000000000000000000000000000',
  value: Value.fromEther('1'),
})
 
const signature = Secp256k1.sign({ 
  payload: TransactionEnvelopeEip7702.getSignPayload(envelope), 
  privateKey: '0x...', 
})
 
const envelope_signed = TransactionEnvelopeEip7702.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 to broadcast a eth_sendRawTransaction request over HTTP JSON-RPC.

import { Authorization, RpcTransport, TransactionEnvelopeEip7702, 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 = TransactionEnvelopeEip7702.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: TransactionEnvelopeEip7702.getSignPayload(envelope),
  privateKey: '0x...',
})
 
// Serialize the Envelope with the Signature.
const serialized = TransactionEnvelopeEip7702.serialize(envelope, { 
  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], 
})

Functions

NameDescription
TransactionEnvelopeEip7702.assertAsserts a TransactionEnvelopeEip7702.TransactionEnvelopeEip7702 is valid.
TransactionEnvelopeEip7702.deserializeDeserializes a TransactionEnvelopeEip7702.TransactionEnvelopeEip7702 from its serialized form.
TransactionEnvelopeEip7702.fromConverts an arbitrary transaction object into an EIP-7702 Transaction Envelope.
TransactionEnvelopeEip7702.getSignPayloadReturns the payload to sign for a TransactionEnvelopeEip7702.TransactionEnvelopeEip7702.
TransactionEnvelopeEip7702.hashHashes a TransactionEnvelopeEip7702.TransactionEnvelopeEip7702. This is the "transaction hash".
TransactionEnvelopeEip7702.serializeSerializes a TransactionEnvelopeEip7702.TransactionEnvelopeEip7702.
TransactionEnvelopeEip7702.validateValidates a TransactionEnvelopeEip7702.TransactionEnvelopeEip7702. Returns true if the envelope is valid, false otherwise.

Types

NameDescription
TransactionEnvelopeEip7702.Rpc
TransactionEnvelopeEip7702.Serialized
TransactionEnvelopeEip7702.SerializedType
TransactionEnvelopeEip7702.Signed
TransactionEnvelopeEip7702.TransactionEnvelopeEip7702
TransactionEnvelopeEip7702.Type