Signer.make
A signer over an account that signs transactions, messages and typed data itself, such as a hardware wallet.
An account is an address and three functions. Each is given what a person would read on a device, and gives a signature. The signer does the rest: fees, gas, nonces, the bytes that go out.
import { Effect } from "effect";import { type Address, type Hex, Signer } from "@ethect/core";
/** Your device: it shows what it is asked to sign, and signs when its button is pressed. */declare const device: { readonly address: Address.Address; readonly transaction: (transaction: Signer.Unsigned) => Effect.Effect<Hex.Hex, "refused">; readonly message: (message: Hex.Hex) => Effect.Effect<Hex.Hex, "refused">; readonly typedData: (data: Signer.TypedData) => Effect.Effect<Hex.Hex, "refused">;};
const refused = () => new Signer.Rejected({ message: "The request was refused on the device" });
const account: Signer.Account = { address: device.address, // the whole transaction: the call, the chain, the nonce, the gas, the fees signTransaction: (transaction) => Effect.mapError(device.transaction(transaction), refused), // the bytes of the message, before the EIP-191 prefix signMessage: (message) => Effect.mapError(device.message(message), refused), signTypedData: (data) => Effect.mapError(device.typedData(data), refused),};
export const Device = Signer.make(account);To sign with a key that only signs digests, such as a KMS, use
Signer.fromSign: it builds these three functions for you.
Returns
Section titled “Returns”Layer<Signer.Signer>
A layer that provides the Signer service, with one
account. Building it cannot fail.
Parameters
Section titled “Parameters”account
Section titled “account”- Type:
Signer.Account
The account that signs.
options.nonces (optional)
Section titled “options.nonces (optional)”- Type:
Signer.Nonces - Default:
Signer.Nonces.serial
How nonces are handed out. With serial, the account signs inside the lock: nothing else of
the account is sent while a person reads a device.
Account
Section titled “Account”- Type:
Signer.Account
| Member | Type | Given |
|---|---|---|
address |
Address.Address |
|
signTransaction |
(transaction: Signer.Unsigned) => Effect<Hex, Signer.Rejected | Signer.Error> |
the transaction, with every field decided |
signMessage |
(message: Hex) => Effect<Hex, Signer.Rejected | Signer.Error> |
the bytes of the message, without the EIP-191 prefix |
signTypedData |
(data: Signer.TypedData) => Effect<Hex, Signer.Rejected | Signer.Error> |
the typed data |
Each gives a signature as hex: r ‖ s ‖ v in 65 bytes, or r ‖ s in 64. The signer
serialises the transaction: an account never gives signed bytes.
Unsigned
Section titled “Unsigned”- Type:
Signer.Unsigned
A transaction with every field decided, as an account is asked to sign it. It is flat: the call, then its fields.
| Field | Type | |
|---|---|---|
to |
Address.Address | undefined |
undefined for a deployment |
data |
Hex |
|
value |
bigint |
|
chainId |
number |
the chain of the transport |
nonce |
number |
|
gas |
bigint |
|
maxFeePerGas, maxPriorityFeePerGas |
bigint | undefined |
set for an EIP-1559 transaction |
gasPrice |
bigint | undefined |
set for a legacy or an EIP-2930 transaction |
accessList |
AccessList | undefined |
|
type |
string | undefined |
set when the caller named it |
import { Effect } from "effect";import type { Signer } from "@ethect/core";
/** What a device shows before it signs. */const summary = ({ to, value, chainId, nonce, gas, maxFeePerGas }: Signer.Unsigned) => Effect.succeed( `${to ?? "a new contract"} gets ${value} wei on chain ${chainId}, ` + `nonce ${nonce}, up to ${gas * (maxFeePerGas ?? 0n)} wei of fees`, );Behavior
Section titled “Behavior”- The account signs, the signer serialises. The signature is put in the transaction, whose bytes and hash the signer computes.
- Every signature is checked against the address. One that does not recover it fails with
"InvalidKey", before a node sees a transaction: a device on the wrong account sends nothing. - A refusal is
Signer.Rejected. It is passed on as it is. The nonce the transaction took is given back. - The account is asked once for each nonce. When the node does not answer, the same signed bytes are sent again: nobody presses a button twice. Only “nonce too low” asks again: the transaction is signed anew, at the nonce the node says is next.
Errors
Section titled “Errors”It fails as Signer.privateKey does, and with:
| Error | Reason | When |
|---|---|---|
Signer.Rejected |
the account refused to sign | |
Signer.Error |
"InvalidKey" |
the signature is not 64 or 65 bytes, or does not recover the address |
Under the hood
Section titled “Under the hood”submit,signTransaction- your nodethe fees, the gas, the nonce: asked of the
Transport, asSigner.privateKeydoes - the account
account.signTransaction(unsigned)one call - your node
eth_sendRawTransaction(raw)forsubmitonly, throughTx.Broadcast
- your nodethe fees, the gas, the nonce: asked of the
signMessage,signTypedData- the account
account.signMessage(bytes), account.signTypedData(data)one call. No node is asked
- the account