# Signers (Signer)

A signer holds accounts and makes them sign: a local key, a wallet, or the wallet an app is
connected to. Each one is a `Layer` that provides the `Signer` service, which
[`Tx`](https://ethect.dev/reference/tx/) sends through. A program that only reads needs no signer, and one that
sends does not compile without one.

```ts
import { Signer } from "@ethect/core";
```

| Export | Kind | Description |
| --- | --- | --- |
| [`Signer.privateKey(key, options?)`](https://ethect.dev/reference/signer/private-key/) | layer | A local key. Always a `Redacted`: a plain string does not compile. |
| [`Signer.fromSign(account, options?)`](https://ethect.dev/reference/signer/from-sign/) | layer | Anything that signs a digest: a KMS, an HSM. |
| [`Signer.make(account, options?)`](https://ethect.dev/reference/signer/make/) | layer | An account that signs transactions, messages and typed data itself. |
| [`Signer.all(...signers)`](https://ethect.dev/reference/signer/all/) | layer | Several signers as one: `from` picks the account. |
| [`Signer.wallet(provider, options?)`](https://ethect.dev/reference/signer/wallet/) | layer | An EIP-1193 wallet. |
| [`Signer.connected(options?)`](https://ethect.dev/reference/signer/connected/) | layer | The wallet the app is connected to, whichever it is at the time. |
| [`Signer.accounts`](https://ethect.dev/reference/signer/accounts/) | effect | The accounts the signer holds. |
| [`Signer.address`](https://ethect.dev/reference/signer/accounts/#address) | effect | The first of them: the sender when no `from` is given. |
| [`Signer.capabilities(options?)`](https://ethect.dev/reference/signer/accounts/#capabilities) | function | What the signer can do for an account on a chain. |
| [`Signer.signMessage(message, options?)`](https://ethect.dev/reference/signer/sign-message/) | function | Signs a message, and sends nothing. |
| [`Signer.signTypedData(data, options?)`](https://ethect.dev/reference/signer/sign-typed-data/) | function | Signs EIP-712 typed data. |
| [`Signer.signTransaction(call, options?)`](https://ethect.dev/reference/signer/sign-transaction/) | function | Signs a call as a transaction, and sends nothing. |
| [`Signer.Nonces`](https://ethect.dev/reference/signer/nonces/) | strategies | How a local signer hands out nonces: `serial`, the default, or `pipelined`. |
| [`Signer.Signer`](#the-service) | service | What every signer provides. |
| [`Signer.Error`](#failures) | error | The signer cannot do what was asked. Its `_tag` is `"SignerError"`, and `reason` says why. |
| [`Signer.Rejected`](#failures) | error | The user refused what the wallet asked: to connect, to sign, to switch chain. Its `_tag` is `"SignerRejected"`. |
| `Signer.Estimate` | module | `Signer.Estimate.Reverted`: what a signer fails with when a call would revert, before anything is signed. |
| [`Signer.Capabilities`](https://ethect.dev/reference/signer/accounts/#capabilities) | type | `atomic`, `unattended`, and `all` that a wallet reports. |
| [`Signer.TypedData`](https://ethect.dev/reference/signer/sign-typed-data/#data) | type | EIP-712 typed data: `domain`, `types`, `primaryType`, `message`. |
| [`Signer.Signed`](https://ethect.dev/reference/signer/sign-transaction/#signed) | type | A transaction that is signed and not sent: `raw`, `hash`, `nonce`, `from`, `chainId`. |
| [`Signer.Account`](https://ethect.dev/reference/signer/make/#account) | type | What `Signer.make` takes: an `address` and the three functions that sign. |
| [`Signer.Unsigned`](https://ethect.dev/reference/signer/make/#unsigned) | type | A transaction with every field decided, as an `Account` signs it. |
| [`Signer.LocalOptions`](https://ethect.dev/reference/signer/nonces/#localoptions) | type | The options of a signer that signs here: `nonces`. |

## The service

A signer of your own is a layer that provides `Signer.Signer`. Calls arrive encoded: a signer
never sees an ABI. It uses the `Transport` of whoever calls it, and keeps none of its own.

| Member | Does |
| --- | --- |
| `accounts` | The accounts it holds. |
| `capabilities(account, chainId)` | What it can do for an account on a chain. |
| `submit(request)` | Signs the calls as one and hands them to the network. Gives a `Tx.Pending`. |
| `status(handle)` | Where a `Tx.Handle` it gave stands: a `Tx.Handle.Status`, with hashes and no receipts. |
| `signMessage(account, message)` | Signs the bytes of a message. |
| `signTypedData(account, data)` | Signs typed data. |
| `signTransaction(account, call, fields)` | Signs a transaction, and sends nothing. |

## Failures

A user who refuses is `Signer.Rejected`, whose `_tag` is `"SignerRejected"`: it has a `message`
and nothing else. It is its own Schema, written as
`{ "_tag": "SignerRejected", "message": "The user refused to sign" }`. `new` checks nothing: with fields of their types, it never throws. Its
tag was `"Rejected"` in 0.1.0-alpha.0.

Anything else the signer cannot do is `Signer.Error`, whose `_tag` is `"SignerError"`. It is
its own Schema too, and its `cause` is written as its own class, an
[`RpcError`](https://ethect.dev/reference/errors/rpc-error/#as-a-schema) or a
[`CodecError`](https://ethect.dev/reference/errors/codec-error/#as-a-schema):
`{ "_tag": "SignerError", "reason": "UnknownChain", "message": "…", "cause": { "_tag": "RpcError", "code": 4902, … } }`.

| `Signer.Error` `reason` | Means |
| --- | --- |
| `"Unauthorized"` | the signer does not hold the account, or holds none |
| `"ChainMismatch"` | the wallet is on another chain than the transaction is for |
| `"UnknownChain"` | the wallet does not know the chain |
| `"Disconnected"` | no wallet is connected, or it cannot be reached |
| `"InvalidKey"` | what was given as a key is not one |
| `"UnknownBatch"` | the wallet does not know the batch it is asked about: it is not the wallet that took it, or it has forgotten it. The batch may still be mined |
| `"Unknown"` | anything else. `cause` holds what the wallet answered: its refusal, an `RpcError`, or a `CodecError` for an answer that cannot be read, with the answer as its `value` |

## Under the hood

A local key signs in your program and sends through your node. A wallet is asked for everything.

**a local key: `Signer.privateKey`, `Signer.fromSign`**

- **your program**: the accounts and every signature. No request
- **your node** `eth_sendRawTransaction`: to send, after the fees, the gas and the nonce

**a wallet: `Signer.wallet`, `Signer.connected`**

- **the wallet** `eth_accounts`: the accounts. `Signer.connected` has them from the connection, with no request
- **the wallet** `eth_chainId`: before every signature that is for a chain
- **the wallet** `eth_sendTransaction`: one call. Several calls: `wallet_sendCalls`
- **the wallet** `personal_sign`: a message. Typed data: `eth_signTypedData_v4`

See [`Signer.privateKey`](https://ethect.dev/reference/signer/private-key/#under-the-hood),
[`Signer.wallet`](https://ethect.dev/reference/signer/wallet/#under-the-hood) and
[`Signer.connected`](https://ethect.dev/reference/signer/connected/#under-the-hood).
