Skip to content

Signer.signTransaction

Signs a call as a transaction and sends nothing. It gives the signed bytes, their hash and the nonce they carry.

import { Config, Effect, Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Call, Signer, Transport } from "@ethect/core";
const Script = Layer.unwrap(
Effect.gen(function* () {
const url = yield* Config.Redacted("RPC_URL");
const key = yield* Config.Redacted("PRIVATE_KEY");
return Layer.merge(Signer.privateKey(key), Transport.http(url));
}),
).pipe(Layer.provide(FetchHttpClient.layer));
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const program = Effect.gen(function* () {
const transfer = Call.make({ to: TO, value: 10n ** 15n });
// { raw, hash, nonce, from, chainId }
const signed = yield* Signer.signTransaction(transfer);
return signed.raw;
});
const raw = await program.pipe(Effect.provide(Script), Effect.runPromise);

For a relayer, a bundle, or a machine that signs and never sends. To sign and send, use Tx.send. The examples below leave the signer and the transport out.

The bytes are a whole transaction: any node takes them.

import { Effect } from "effect";
import { type Call, Signer } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const signThenSend = (call: Call.Call<unknown>) =>
Effect.gen(function* () {
const signed = yield* Signer.signTransaction(call);
const hash = yield* Eth.sendRawTransaction(signed.raw);
return hash === signed.hash; // true: the hash is known before anything is sent
});

What transaction sets is used as it is, and nothing is asked for it. With the nonce, the gas and the fees set, the node is asked nothing but its chain.

import { Effect } from "effect";
import { type Call, Signer } from "@ethect/core";
const offline = (call: Call.Call<unknown>, nonce: number) =>
Signer.signTransaction(call, {
transaction: {
nonce,
gas: 80_000n,
maxFeePerGas: 30n * 10n ** 9n,
maxPriorityFeePerGas: 10n ** 9n,
},
});

Signing takes no nonce: two transactions signed one after the other carry the same one. Set the nonce of those that follow.

import { Effect } from "effect";
import { type Call, Signer } from "@ethect/core";
const bundle = (first: Call.Call<unknown>, second: Call.Call<unknown>) =>
Effect.gen(function* () {
const one = yield* Signer.signTransaction(first);
const transaction = { nonce: one.nonce + 1 };
const two = yield* Signer.signTransaction(second, { transaction });
return [one.raw, two.raw];
});

Effect<Signer.Signed, Signer.Rejected | Tx.Unsupported | AbiError | Signer.Error | Transport.Error, Signer.Signer | Transport.Transport>

  • Type: Signer.Signed
Property Type Holds
raw Hex the signed transaction, as eth_sendRawTransaction takes it
hash Hash the hash it will be known by
nonce number the nonce it carries
from Address.Address the account that signed
chainId number the chain it is for

The call to sign: of a function, a raw one, or a deployment.

The account that signs.

Fields to set instead of leaving them to an estimate: nonce, gas, the fees, accessList, type.

  • Nothing is sent. Not through the node, and not through Tx.Broadcast.
  • The nonce is the one the next send would take, and it is not taken. A signer that signs here gives what its counter says, or the account’s pending count on the node when it has sent nothing yet. Sending what you signed elsewhere is not known to the counter. Once that transaction is mined, the next send finds “nonce too low”, reads the count again and goes on.
  • The chain is the one of the transport. The transaction is good for that chain only.
  • A call that would revert fails when its gas is estimated, with the node’s RpcError. Set gas to sign it anyway.
  • The type follows the fees. EIP-1559 where the chain has a base fee, unless gasPrice or type says otherwise. legacy, eip2930 and eip1559 are signed.
  • Most browser wallets cannot. They sign only what they send, and fail with Tx.Unsupported, reason "signing".
Error Reason When
Tx.Unsupported "signing" the signer signs nothing it does not send, or the type is one this library does not serialise
Signer.Rejected the user refused in the wallet, or on the device
Signer.Error "Unauthorized" the signer does not hold from, or holds no account
Signer.Error "Unknown" a field cannot be serialised, such as a tip above its cap
AbiError the call cannot be encoded
RpcError ExecutionReverted the call would revert: reason.data is what it reverted with
RpcError, TransportError the node refused a request, or did not answer

For a signer that signs here. Each line is skipped when transaction sets what it is for.

  1. your nodeeth_chainIdonce per transport. Not asked under Transport.on
  2. your nodeeth_getBlockByNumber("latest")the base fee
  3. your nodeeth_maxPriorityFeePerGasthe tip. The cap is twice the base fee plus the tip
  4. your nodeeth_estimateGas(call)the gas
  5. your nodeeth_getTransactionCount(from, "pending")the nonce, when the signer has sent nothing yet
  6. your programthe transaction is signed, in the process: nothing is sent

A wallet is asked eth_signTransaction.