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.
Send it later, or elsewhere
Section titled “Send it later, or elsewhere”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 });Every field set
Section titled “Every field set”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, }, });Several in a row
Section titled “Several in a row”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]; });Returns
Section titled “Returns”Effect<Signer.Signed, Signer.Rejected | Tx.Unsupported | AbiError | Signer.Error | Transport.Error, Signer.Signer | Transport.Transport>
Signed
Section titled “Signed”- 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 |
Parameters
Section titled “Parameters”- Type:
Call.Call
The call to sign: of a function, a raw one, or a deployment.
options.from (optional)
Section titled “options.from (optional)”- Type:
Address.Address - Default: the signer’s first account
The account that signs.
options.transaction (optional)
Section titled “options.transaction (optional)”- Type:
Tx.Fields
Fields to set instead of leaving them to an estimate: nonce, gas, the fees, accessList,
type.
Behavior
Section titled “Behavior”- 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. Setgasto sign it anyway. - The type follows the fees. EIP-1559 where the chain has a base fee, unless
gasPriceortypesays otherwise.legacy,eip2930andeip1559are signed. - Most browser wallets cannot. They sign only what they send, and fail with
Tx.Unsupported, reason"signing".
Errors
Section titled “Errors”| 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 |
Under the hood
Section titled “Under the hood”For a signer that signs here. Each line is skipped when transaction sets what it is for.
- your node
eth_chainIdonce per transport. Not asked underTransport.on - your node
eth_getBlockByNumber("latest")the base fee - your node
eth_maxPriorityFeePerGasthe tip. The cap is twice the base fee plus the tip - your node
eth_estimateGas(call)the gas - your node
eth_getTransactionCount(from, "pending")the nonce, when the signer has sent nothing yet - your programthe transaction is signed, in the process: nothing is sent
A wallet is asked eth_signTransaction.