Skip to content

Signer.privateKey

A signer over a private key the program holds. The key is always a Redacted, and is never written anywhere.

script.ts
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 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 string
Signer.privateKey(process.env["PRIVATE_KEY"] ?? "");

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));

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 }) }),
),
);

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.

  • 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.

How nonces are handed out, and how many transactions of the account may be on their way at once.

  • 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 transaction sets 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 submit fails with Signer.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.Partial then 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.
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

Signing happens in the process. Everything else is asked of the Transport in context.

  1. submit({ calls, from, chainId, transaction })

    1. your nodeeth_chainIdonce per transport. Not asked under Transport.on
    2. the fees, unless set: one estimate for all the calls

      1. your nodeeth_getBlockByNumber("latest")its base fee. None: eth_gasPrice, and a legacy transaction
      2. your nodeeth_maxPriorityFeePerGasthe tip. The cap is twice the base fee plus the tip
      3. a node without that method: eth_gasPrice less the base fee, and never less than zero

    3. the gas, unless set

      1. one call

        1. your nodeeth_estimateGas
      2. several

        1. your nodeeth_simulateV1of the sequence: what each call used, and half as much again
    4. then for each call, in order

      1. your nodeeth_getTransactionCount(from, "pending")the first time only: then the counter is the truth
      2. your programthe transaction is signed, here
      3. your nodeeth_sendRawTransaction(raw)through Tx.Broadcastgives its hash

What happens when the node refuses a transaction, or does not answer, is on Signer.Nonces. How the bytes go out is Tx.Broadcast.