# Signer.accounts

The accounts the signer in context holds, the first of them, and what the signer can do for
an account on a chain.

| Export | Kind | Description |
| --- | --- | --- |
| [`Signer.accounts`](#usage) | effect | The accounts the signer holds. |
| [`Signer.address`](#address) | effect | The first of them: the sender when no `from` is given. |
| [`Signer.capabilities(options?)`](#capabilities) | function | What the signer can do for an account on a chain. |

## Usage

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

const Script = Layer.unwrap(Effect.map(Config.Redacted("PRIVATE_KEY"), Signer.privateKey));

const held = Signer.accounts.pipe(Effect.provide(Script));
// Effect<ReadonlyArray<Address.Address>, Signer.Error | ConfigError>: the address of the key
```

The three work with every signer: a key, a wallet, several as one.

## Returns

`Effect<ReadonlyArray<Address.Address>, Signer.Error, Signer.Signer>`

The accounts, in the signer's order. A local signer has one. A wallet has those it was
allowed to show, and [`Signer.all`](https://ethect.dev/reference/signer/all/) those of all its signers.

## address

- **Type:** `Effect<Address.Address, Signer.Error, Signer.Signer>`

The first account: the sender of everything that is given no `from`.

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

const greeting = Effect.gen(function* () {
  const me = yield* Signer.address;
  return `Signing as ${me}`;
});
```

It fails with `"Unauthorized"` when the signer holds no account, as a wallet nobody connected
does.

## capabilities

`Signer.capabilities(options?)` says what the signer can do for an account on a chain.
[`Tx.plan`](https://ethect.dev/reference/tx/plan/) reads it to decide how many signatures calls take.

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

const describe = Effect.gen(function* () {
  const { atomic, unattended } = yield* Signer.capabilities({ chainId: 8453 });
  // a local key: "unsupported" and true. A wallet that batches: "supported" and false.
  return atomic === "unsupported" && unattended ? "one transaction per call" : "asks a person";
});
```

### The chain

A signer answers for a chain. Say it, and nothing but a signer is needed. Leave it out, and
the chain is the one of the [`Transport`](https://ethect.dev/reference/transport/chains/#chainid) in context,
which the effect then needs: with no chain and no transport, the program does not compile.

```ts
import { Effect } from "effect";
import { Signer, Transport } from "@ethect/core";

const both = Effect.gen(function* () {
  // the chain is said: no transport is needed
  const onBase = yield* Signer.capabilities({ chainId: 8453 });
  // the chain of the part of the program this runs in
  const here = yield* Signer.capabilities().pipe(Transport.on(1));
  return { onBase, here };
});

// Effect<Signer.Capabilities, Signer.Error, Signer.Signer | Transport.Transport>
const needsItsChain = Signer.capabilities();
```

### Returns

| Called with | Returns |
| --- | --- |
| `{ chainId, from? }` | `Effect<Signer.Capabilities, Signer.Error, Signer.Signer>` |
| `{ from? }`, or nothing | `Effect<Signer.Capabilities, Signer.Error, Signer.Signer \| Transport.Transport>` |

| Property | Type | Says |
| --- | --- | --- |
| `atomic` | `"supported" \| "ready" \| "unsupported"` | whether several calls can be one transaction, all or nothing. `"ready"`: after an upgrade the user has to accept |
| `unattended` | `boolean` | whether it signs without asking anyone, so that several transactions can go out at once |
| `all` | `Record<string, unknown>` | every capability a wallet reports for the chain, as it reports them |

A signer that signs here answers `{ atomic: "unsupported", unattended: true, all: {} }`, on
every chain.

A wallet is asked once per account and chain, and its answer kept until it changes account.
One that does not answer within `capabilitiesRequestTimeout`, 3 seconds by default, has no
capability that time: `{ atomic: "unsupported", unattended: false, all: {} }`. Its answer is
kept when it comes. See [`Signer.wallet`](https://ethect.dev/reference/signer/wallet/#optionscapabilitiesrequesttimeout-optional).

### Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `options.chainId` (optional) | `number` | The chain. Left out: the chain of the transport in context. |
| `options.from` (optional) | `Address.Address` | The account. Default: the signer's first. |

## Errors

| Error | Reason | From | When |
| --- | --- | --- | --- |
| `Signer.Error` | `"Unauthorized"` | `address`, `capabilities` | the signer holds no account, or not the one asked for |
| `Signer.Error` | `"Disconnected"` | all three | a wallet could not be asked. For `capabilities` with no `chainId`: the transport did not answer when asked its chain |
| `Signer.Error` | `"Unknown"` | all three | a wallet answered with an error, or with what cannot be read: `cause` holds its `RpcError` or the `CodecError`. For `capabilities`, a wallet's error other than 4100, 4900 and 4901 is no capability, and not a failure. With no `chainId`: the node did, and `cause` holds its `RpcError` |

## Under the hood

- **a local signer**: nothing is asked: it knows its account, and what it can do
- **a wallet** `eth_accounts, wallet_getCapabilities`
- **capabilities()** `eth_chainId`: `Transport.chainId` first: once per transport, nothing under `Transport.on`
