# Connections

The three methods that manage the accounts an app is connected to: connect with capabilities,
disconnect, and add a sub-account. They follow two ERCs that are still drafts.

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`connect`](#connect) | `wallet_connect` | `{ accounts }` | Connects accounts and asks for capabilities in the same prompt (ERC-7846). |
| [`disconnect`](#disconnect) | `wallet_disconnect` | `void` | Disconnects the app's accounts (ERC-7846). |
| [`addSubAccount`](#addsubaccount) | `wallet_addSubAccount` | `SubAccount` | Has the wallet create or track an account that its main account owns (ERC-7895). |

`connect` and `addSubAccount` take one object whose `version` is the version of the method,
`"1"`. Each method returns an
[`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method).

To connect accounts and nothing else, use
[`Wallet.requestAccounts`](https://ethect.dev/rpc/wallet/request-accounts/).

## connect

Connects accounts and, in the same prompt, asks for capabilities such as signing in (ERC-7846,
a draft).

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

const signedIn = Effect.gen(function* () {
  const { accounts } = yield* Wallet.connect({
    version: "1",
    capabilities: {
      signInWithEthereum: { nonce: "12345678", chainId: "0x1" },
    },
  });
  // the message the wallet built, and its signature
  return accounts.at(0)?.capabilities?.signInWithEthereum;
});
```

- **JSON-RPC:** `wallet_connect`
- **Returns:** an object with `accounts`, a list. Each entry has the `address` of a connected
  account and, when the wallet sends them, the `capabilities` granted for it.
- A capability is sent as you wrote it. The `chainId` of `signInWithEthereum` above stays the
  string `"0x1"`.

It takes one object.

| Parameter | Type | Description |
| --- | --- | --- |
| `version` | `string` | the version of the method, `"1"` |
| `capabilities` | [`WalletCapabilities`](https://ethect.dev/rpc/types/wallet-capabilities/) (optional) | the capabilities to ask for, by name |

## disconnect

Disconnects the app's accounts and revokes what [`connect`](#connect) granted (ERC-7846, a
draft).

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

const signOut = Effect.gen(function* () {
  yield* Wallet.disconnect();
  return "disconnected";
});
```

- **JSON-RPC:** `wallet_disconnect`
- **Parameters:** none.
- **Returns:** nothing. Whatever the wallet answers is ignored, and the effect succeeds with
  `void`.

## addSubAccount

Has the wallet create or track an account that its main account owns (ERC-7895, a draft).

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

const subAccount = Effect.gen(function* () {
  const { address } = yield* Wallet.addSubAccount({
    version: "1",
    account: {
      type: "create",
      // a public key of the app, to sign for the new account
      keys: [{ type: "webauthn-p256", publicKey: `0x${"04".repeat(64)}` }],
    },
  });
  return address;
});
```

- **JSON-RPC:** `wallet_addSubAccount`
- **Returns:** a [`SubAccount.SubAccount`](https://ethect.dev/rpc/types/sub-account/): the `address` of the
  account and, where the wallet knows how to deploy it, `factory` and `factoryData`.

It takes one object.

| Parameter | Type | Description |
| --- | --- | --- |
| `version` | `string` | the version of the method, `"1"` |
| `account` | [`SubAccount.Payload`](https://ethect.dev/rpc/types/sub-account/) | the account to add, in one of the three shapes below |

The `type` of `account` selects its shape.

| `type` | Other fields | The account |
| --- | --- | --- |
| `"create"` | `keys` (optional) | one for the wallet to create, with the app's keys as signers if it gives any |
| `"deployed"` | `address`, `chainId` (optional) | one that is already deployed |
| `"undeployed"` | `address`, `factory`, `factoryData`, `chainId` (optional) | one the wallet can deploy with `factory` and `factoryData` |

An account the app created and has not deployed yet goes with what deploys it.

```ts
import { type Address, type Hex } from "@ethect/core";
import { Wallet } from "@ethect/core/rpc";

const track = (
  address: Address.Address,
  factory: Address.Address,
  factoryData: Hex.Hex,
) =>
  Wallet.addSubAccount({
    version: "1",
    account: { type: "undeployed", address, chainId: 8453, factory, factoryData },
  });
```

## Behavior

- **Drafts.** ERC-7846 and ERC-7895 are not final: their shapes can change, and a wallet may
  not serve these methods.
- **The chain id is a `number`.** The `chainId` of a sub-account is sent as a hex quantity:
  `8453` leaves as `"0x2105"`.
- **Lowercase addresses.** The addresses of the accounts and of `factory` decode to lowercase
  [`Address`](https://ethect.dev/reference/primitives/address/) values.
- **Not served by a node.** anvil 1.9 answers the three methods with the reason
  `MethodNotFound`.
