Skip to content

Eth.signTransaction

Asks the node or wallet to sign a transaction with an account it holds the key of, without sending it. Returns the signed transaction, encoded.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const program = Eth.signTransaction({ from: FROM, to: TO, value: 1n });
const transaction = await program.pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// "0x02f86c827a69…": an EIP-1559 transaction of 111 bytes, signed

The result is the bytes a node accepts in Eth.sendRawTransaction. Nothing is sent.

This method asks the node or the wallet to sign: the key stays there, and from must be an account it can sign for. To sign with a key your program holds, use Signer.signTransaction with Signer.privateKey.

The examples below leave the transport out.

Keep the signed bytes and send them when you choose, through any node.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const hash = Effect.gen(function* () {
const transaction = yield* Eth.signTransaction({ from: FROM, to: TO, value: 1n });
return yield* Eth.sendRawTransaction(transaction);
});

Set the nonce, the gas and the fees yourself to know exactly what is signed.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const transaction = Effect.gen(function* () {
return yield* Eth.signTransaction({
from: FROM,
to: TO,
value: 1n,
nonce: yield* Eth.getTransactionCount(FROM, "pending"),
gas: 21_000n,
maxFeePerGas: 30n * 10n ** 9n,
maxPriorityFeePerGas: 10n ** 9n,
});
});

Effect<Hex, Method.Error, Transport>

The signed transaction as Hex: the type byte followed by the RLP of its fields, signature included.

The transaction to sign. The fields are those of Eth.sendTransaction.

import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const transaction = Eth.signTransaction({ from: FROM, to: TO, value: 1n });
  • One result shape. Some nodes answer with the hex, and geth with an object that holds it under raw. Both decode to the hex.
  • anvil fills in the rest. A request without a nonce, gas or fees is completed before it is signed, as Eth.fillTransaction does.
  • The hash is known. The hash of the transaction is the Keccak-256 of the result. See Eth.sendRawTransaction.
  • The key is the node’s. A node that holds no key for from refuses the request.

The three errors every method can fail with. The cases specific to this method, as anvil 1.9 reports them:

Error Reason When
RpcError InvalidParams the node holds no key for from

eth_signTransaction