# WalletCapabilities

The optional features of a wallet (EIP-5792), by name: `atomic`, `paymasterService`,
`flowControl` and so on. The module types what a wallet reports, what an app asks for with a
request, and the capabilities of a wallet by chain.

```ts
import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Wallet } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const names = Effect.gen(function* () {
  const byChain = yield* Wallet.getCapabilities(ADDRESS, [8453]);
  // byChain: WalletCapabilities.ByChain
  return Object.keys(byChain[8453] ?? {});
});
```

## Schemas

| Schema | Type | Describes | Used by |
| --- | --- | --- | --- |
| `WalletCapabilities.schema` | `WalletCapabilities.WalletCapabilities` | capabilities by name, as a wallet reports them or attaches them to an answer | the results of [`Wallet.sendCalls`](https://ethect.dev/rpc/wallet/send-calls/), [`Wallet.getCallsStatus`](https://ethect.dev/rpc/wallet/calls-status/#getcallsstatus) and [`Wallet.connect`](https://ethect.dev/rpc/wallet/connections/#connect) |
| `WalletCapabilities.requestSchema` | `WalletCapabilities.Request` | capabilities asked for with a request, by name | the `capabilities` of a [`Calls`](https://ethect.dev/rpc/types/calls/) batch and of each call |
| `WalletCapabilities.byChainSchema` | `WalletCapabilities.ByChain` | a wallet's capabilities by chain id | returned by [`Wallet.getCapabilities`](https://ethect.dev/rpc/wallet/get-capabilities/) |

## Shapes

The three types are records. Only their envelope is typed: what a capability holds is left
open, because each one is defined by an ERC of its own.

| Type | Shape | Notes |
| --- | --- | --- |
| `WalletCapabilities.WalletCapabilities` | `{ [name: string]: { [key: string]: unknown } }` | one object per capability |
| `WalletCapabilities.Request` | `{ [name: string]: { optional?: boolean, [key: string]: unknown } }` | the same, with the `optional` flag |
| `WalletCapabilities.ByChain` | `{ [chainId: number]: WalletCapabilities }` | chain `0` holds on every chain |

Ask for a capability by adding it to a batch.

```ts
import type { WalletCapabilities } from "@ethect/core/rpc";

const capabilities: WalletCapabilities.Request = {
  // the wallet may ignore it: a wallet without it still sends the batch
  paymasterService: { url: "https://paymaster.example", optional: true },
};
```

## Behavior

- **`optional` decides.** A wallet that lacks a capability asked for rejects the request,
  unless that capability is marked `optional: true`.
- **Chain ids are numbers.** In `ByChain`, the key `"0x2105"` decodes to `8453`. Uppercase hex
  digits decode too: `"0x14A34"` is `84532`.
- **Chain `0` means every chain.** A capability the wallet has on all its chains is listed
  once, under `0`.
- **A capability is an object.** A value that is not one, such as `true`, fails the decoding.
- **Values are `unknown`.** Read a capability with optional chaining, and check what you find:
  `byChain[8453]?.atomic?.status === "supported"`.
- **Every key must be a chain id.** A key of `ByChain` that is not a hex quantity fails the
  decoding: a wallet that answers `{ "8453": { … } }` gives a `CodecError`, not an empty
  object.
