Signer.privateKey
A signer over a private key the program holds. The key is always a Redacted, and is never
written anywhere.
import { Config, Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Address, Call, Signer, Transport, Tx } from "@ethect/core";
// The key is read from the environment as a secret, and given as it is.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* () { // Signed with the key, sent, and waited for: the receipt of the transfer. return yield* Tx.send(Call.make({ to: TO, value: 10n ** 15n }));});
const receipt = await program.pipe(Effect.provide(Script), Effect.runPromise);The signer is what Tx sends through. It needs no node of its own: it uses
the Transport of whoever calls it. The examples below leave the
transport out.
A plain string does not compile
Section titled “A plain string does not compile”A key that is a string can be logged, serialised, or end up in an error. A
Redacted cannot.
import { Config, Effect, Layer } from "effect";import { Signer } from "@ethect/core";
const FromEnvironment = Layer.unwrap(Effect.map(Config.Redacted("PRIVATE_KEY"), Signer.privateKey));
// @ts-expect-error a private key is never a plain stringSigner.privateKey(process.env["PRIVATE_KEY"] ?? "");One key on several chains
Section titled “One key on several chains”The signer signs for the chain of the transport it is called with. One key works on every
chain of Transport.chains, each with a nonce counter of its
own.
import { Config, Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Signer, Transport } from "@ethect/core";
const App = Layer.unwrap( Effect.map(Config.Redacted("PRIVATE_KEY"), (key) => Layer.merge( Signer.privateKey(key), Transport.chains({ 1: Transport.http("https://ethereum-rpc.publicnode.com"), 8453: Transport.http("https://mainnet.base.org"), }), ), ),).pipe(Layer.provide(FetchHttpClient.layer));
const onMainnet = Signer.capabilities().pipe(Transport.on(1));// A transaction signed under this is a transaction of Base.const onBase = Signer.capabilities().pipe(Transport.on(8453));More transactions at once
Section titled “More transactions at once”By default one transaction of an account is signed and sent at a time. Pass
Signer.Nonces.pipelined for more.
import { Config, Effect, Layer } from "effect";import { Signer } from "@ethect/core";
const Fast = Layer.unwrap( Effect.map(Config.Redacted("PRIVATE_KEY"), (key) => Signer.privateKey(key, { nonces: Signer.Nonces.pipelined({ inFlight: 16 }) }), ),);Returns
Section titled “Returns”Layer<Signer.Signer, Signer.Error>
A layer that provides the Signer service, with one
account: the address of the key. Building it fails with SignerError "InvalidKey" when the
value is no key.
Parameters
Section titled “Parameters”- Type:
Redacted<string>
The private key: 32 bytes of hex, with the 0x prefix or without it, in either case. It has
to be above zero and below the order of secp256k1.
options.nonces (optional)
Section titled “options.nonces (optional)”- Type:
Signer.Nonces - Default:
Signer.Nonces.serial
How nonces are handed out, and how many transactions of the account may be on their way at once.
Behavior
Section titled “Behavior”- The key stays in its wrapper. It is read for the time of one signature, and its bytes are wiped after. Nothing holds it unwrapped: not the layer, not the service.
- The key is never written. No failure, message, log or span holds it. Printing the signer or its layer shows no key. What is not a key fails with a message that quotes nothing.
- It is checked when the layer is built. A value that is no key fails the layer, before anything is asked of the signer.
- What
transactionsets is used as it is. The rest is asked of the node: the fees once for all the calls, then the gas. See Under the hood. - Nothing is signed for a call that would revert. The estimate finds it, and
submitfails withSigner.Estimate.Reverted, which holds the revert data. - Several calls are several transactions, with nonces in a row. They are not atomic: a
call after the first can be refused, and
Tx.Partialthen says what went out. - A transaction that cannot be signed is found first. A type this library does not serialise, or a tip above its cap, fails before the node is asked anything.
- It signs for the chain of the transport. Calls for another chain fail with
"ChainMismatch". - It gives transaction hashes, never a handle. Any node says where they stand.
Errors
Section titled “Errors”| Error | Reason | When |
|---|---|---|
Signer.Error |
"InvalidKey" |
the value is no key: the layer is not built |
Signer.Error |
"Unauthorized" |
from is not the address of the key |
Signer.Error |
"ChainMismatch" |
the calls are for another chain than the transport’s |
Signer.Error |
"Unknown" |
a field cannot be serialised, such as a tip above its cap. Or status: a local signer gives no handle |
Tx.Unsupported |
"atomic" |
several calls that must succeed or fail as one |
Tx.Unsupported |
"capability" |
an EIP-5792 capability asked for without optional |
Tx.Unsupported |
"gas" |
several calls with no gas set, on a node that has no eth_simulateV1 |
Tx.Unsupported |
"signing" |
a transaction type this library does not serialise: eip4844, eip7702 |
Signer.Estimate.Reverted |
a call would revert: nothing was sent. call is its index, data what it reverted with |
|
Tx.Partial |
a call after the first was refused. sent holds what went out |
|
RpcError |
the node refused the transaction: no funds, a fee too low | |
TransportError |
the node did not answer: an estimate, or the transaction for two minutes |
Under the hood
Section titled “Under the hood”Signing happens in the process. Everything else is asked of the Transport in context.
submit({ calls, from, chainId, transaction })- your node
eth_chainIdonce per transport. Not asked underTransport.on the fees, unless set: one estimate for all the calls
- your node
eth_getBlockByNumber("latest")its base fee. None:eth_gasPrice, and a legacy transaction - your node
eth_maxPriorityFeePerGasthe tip. The cap is twice the base fee plus the tip a node without that method:
eth_gasPriceless the base fee, and never less than zero
- your node
the gas, unless set
one call
- your node
eth_estimateGas
- your node
several
- your node
eth_simulateV1of the sequence: what each call used, and half as much again
- your node
then for each call, in order
- your node
eth_getTransactionCount(from, "pending")the first time only: then the counter is the truth - your programthe transaction is signed, here
- your node
eth_sendRawTransaction(raw)throughTx.Broadcastgives its hash
- your node
- your node
What happens when the node refuses a transaction, or does not answer, is on
Signer.Nonces. How the bytes go out is
Tx.Broadcast.