Skip to content

TxEnvelopeLegacy

Utility functions for working with Legacy Transaction Envelopes.

Examples

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

Instantiating

Transaction Envelopes can be instantiated using TxEnvelopeLegacy.from:

import { TxEnvelopeLegacy, Value } from 'ox'
 
const envelope = TxEnvelopeLegacy.from({
  gasPrice: Value.fromGwei('10'),
  to: '0x0000000000000000000000000000000000000000',
  value: Value.fromEther('1'),
})

Signing

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

import { Secp256k1, TxEnvelopeLegacy } from 'ox'
 
const envelope = TxEnvelopeLegacy.from({
  nonce: 0n,
  gasPrice: 1000000000n,
  gas: 21000n,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n,
})
 
const signature = Secp256k1.sign({ 
  payload: TxEnvelopeLegacy.getSignPayload(envelope), 
  privateKey: '0x...'
})
 
const envelope_signed = TxEnvelopeLegacy.from(envelope, { signature })

Serializing

Transaction Envelopes can be serialized using TxEnvelopeLegacy.serialize:

import { TxEnvelopeLegacy, Value } from 'ox'
 
const envelope = TxEnvelopeLegacy.from({
  chainId: 1,
  gasPrice: Value.fromGwei('10'),
  to: '0x0000000000000000000000000000000000000000',
  value: Value.fromEther('1'),
})
 
const serialized = TxEnvelopeLegacy.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 { RpcTransport, TxEnvelopeLegacy, Secp256k1, Value } from 'ox'
 
// Construct the Envelope.
const envelope = TxEnvelopeLegacy.from({
  chainId: 1,
  gasPrice: Value.fromGwei('10'),
  nonce: 69n,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: Value.fromEther('1.5'),
})
 
// Sign over the Envelope.
const signature = Secp256k1.sign({
  payload: TxEnvelopeLegacy.getSignPayload(envelope),
  privateKey: '0x...',
})
 
// Serialize the Envelope with the Signature.
const serialized = TxEnvelopeLegacy.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], 
})

If you are interfacing with an RPC that supports eth_sendTransaction, you can also use TxEnvelopeLegacy.toRpc to convert an Envelope to an RPC-compatible format. This means you can skip the ceremony of manually filling & signing the Transaction.

import 'ox/window'
import { Provider, TxEnvelopeLegacy, Value } from 'ox'
 
const envelope = TxEnvelopeLegacy.from({
  chainId: 1,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: Value.fromEther('1.5'),
})
 
const envelope_rpc = TxEnvelopeLegacy.toRpc(envelope)
 
const provider = Provider.from(window.ethereum)
const hash = await provider.request({
  method: 'eth_sendTransaction',
  params: [envelope_rpc],
})

Computing Hashes

Transaction Hashes can be computed using TxEnvelopeLegacy.hash:

import { Secp256k1, TxEnvelopeLegacy } from 'ox'
 
const envelope = TxEnvelopeLegacy.from({
  chainId: 1,
  nonce: 0n,
  gasPrice: 1000000000n,
  gas: 21000n,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n,
  data: '0x',
})
 
const signature = Secp256k1.sign({
  payload: TxEnvelopeLegacy.getSignPayload(envelope),
  privateKey: '0x...'
})
 
const envelope_signed = TxEnvelopeLegacy.from(envelope, { signature })
 
const hash = TxEnvelopeLegacy.hash(envelope_signed)

Functions

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

Types

NameDescription
TxEnvelopeLegacy.Rpc
TxEnvelopeLegacy.Serialized
TxEnvelopeLegacy.Signed
TxEnvelopeLegacy.TxEnvelopeLegacy
TxEnvelopeLegacy.Type