# Signer.all

Several signers as one. The sender of a transaction says which of them signs it.

## Usage

```ts title="team.ts"
import { Config, Effect, Layer } from "effect";
import { Address, Signer } from "@ethect/core";

/** Signs a digest with the key of the treasury, held by a KMS. */
declare const kmsSign: (digest: `0x${string}`) => Effect.Effect<`0x${string}`, Signer.Error>;

export const Team = Layer.unwrap(
  Effect.gen(function* () {
    const key = yield* Config.Redacted("PRIVATE_KEY");
    const treasury = yield* Address.make(yield* Config.String("TREASURY_ADDRESS"));
    return Signer.all(
      Signer.privateKey(key),
      Signer.fromSign({ address: treasury, sign: kmsSign }),
    );
  }),
);

const program = Effect.gen(function* () {
  const [hot, treasury] = yield* Signer.accounts;
  // the key signs this one: its account is the first, the sender when none is given
  const mine = yield* Signer.signMessage("from the hot key");
  const theirs = yield* Signer.signMessage("from the treasury", { from: treasury! });
  return { hot, mine, theirs };
}).pipe(Effect.provide(Team));
```

Everything that takes a sender routes by it: `from` in [`Tx.send`](https://ethect.dev/reference/tx/send/) and in the
functions that sign, the account of a [`Tx.Step`](https://ethect.dev/reference/tx/plan/#txstep), the account of
[`Signer.capabilities`](https://ethect.dev/reference/signer/accounts/#capabilities).

### A wallet and a key

Any signer fits, not only those that sign here. What the result needs and can fail with is
what its signers need and can fail with.

```ts
import { Config, Effect, Layer } from "effect";
import { type Eip1193, Signer } from "@ethect/core";

const withWallet = (provider: Eip1193.Provider) =>
  Layer.unwrap(
    Effect.map(Config.Redacted("PRIVATE_KEY"), (key) =>
      Signer.all(Signer.wallet(provider), Signer.privateKey(key)),
    ),
  );
// Layer<Signer.Signer, Signer.Error | ConfigError>
```

## Returns

`Layer<Signer.Signer, E, R>`

A layer that provides one [`Signer`](https://ethect.dev/reference/signer/#the-service) service over all of
them. `E` is what building any of the signers can fail with, and `R` what any of them needs.

## Parameters

### signers

- **Type:** `ReadonlyArray<Layer<Signer.Signer, any, any>>`

The signers, in order. The order decides the first account, and who signs for an account two
of them hold.

## Behavior

- **Every account of every signer.** `Signer.accounts` gives them in the order of the signers,
  each address once.
- **`from` picks the signer.** Each signer is asked in turn for its accounts, and the first
  that holds the sender signs.
- **An address held twice goes to the first.** The others are never asked to sign for it.
- **An account nobody holds is refused** with `"Unauthorized"`.
- **A signer that cannot say what it holds is passed over** when another holds the sender. A
  wallet that is not connected does not stop a key from signing. When nobody else holds the
  sender, the failure is that signer's own.
- **A handle goes back to the signer of its account.** [`Tx.wait`](https://ethect.dev/reference/tx/wait/) asks the
  signer that gave it.
- **Each signer keeps its own way with nonces.** Signers of the same account on the same
  transport share one [counter](https://ethect.dev/reference/signer/nonces/#behavior).

## Errors

| Error | Reason | When |
| --- | --- | --- |
| `Signer.Error` | `"Unauthorized"` | no signer holds the account asked for |
| what a signer fails with | | building it, listing its accounts, or signing with it |

## Under the hood

**`Signer.accounts`**

- **each signer**: its accounts, in order

**anything with a sender**

- **each signer**: its accounts in turn, until one holds it
- **that signer**: then its own function, with the same arguments

A local signer answers `accounts` without asking anyone. A wallet answers from what it knows,
or asks `eth_accounts`.
