# SubAccount

A sub-account: a smart account owned by a wallet's main account, for `wallet_addSubAccount`
(ERC-7895, a draft). The module types the account an app asks the wallet to add, and the one
the wallet tracks.

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

const account: SubAccount.Payload = {
  type: "create",
  keys: [{ type: "webauthn-p256", publicKey: `0x${"04".repeat(64)}` }],
};

const added = Wallet.addSubAccount({ version: "1", account }).pipe(
  // subAccount: SubAccount.SubAccount
  Effect.map((subAccount) => subAccount.address),
);
```

## Schemas

| Schema | Type | Describes | Used by |
| --- | --- | --- | --- |
| `SubAccount.payloadSchema` | `SubAccount.Payload` | the account to add, in one of three shapes | passed to [`Wallet.addSubAccount`](https://ethect.dev/rpc/wallet/connections/#addsubaccount) |
| `SubAccount.schema` | `SubAccount.SubAccount` | a sub-account the wallet tracks | returned by [`Wallet.addSubAccount`](https://ethect.dev/rpc/wallet/connections/#addsubaccount) |
| `SubAccount.keySchema` | `SubAccount.Key` | a key that signs for a sub-account | the `keys` of a `"create"` payload |

## Payload fields

`SubAccount.Payload` is a union of three shapes, told apart by `type`.

| `type` | Field | Type | Notes |
| --- | --- | --- | --- |
| `"create"` | `keys` | `ReadonlyArray<SubAccount.Key>` (optional) | the app's keys, to be signers of the new account |
| `"deployed"` | `address` | [`Address`](https://ethect.dev/reference/primitives/address/) | an account already deployed |
| `"deployed"` | `chainId` | `number` (optional) | |
| `"undeployed"` | `address` | [`Address`](https://ethect.dev/reference/primitives/address/) | an account not deployed yet |
| `"undeployed"` | `chainId` | `number` (optional) | |
| `"undeployed"` | `factory` | [`Address`](https://ethect.dev/reference/primitives/address/) | the contract that deploys the account |
| `"undeployed"` | `factoryData` | [`Hex`](https://ethect.dev/reference/primitives/hex/) | the calldata for `factory` |

## Key fields

`SubAccount.Key`, a key that signs for a sub-account.

| Field | Type | Notes |
| --- | --- | --- |
| `publicKey` | [`Hex`](https://ethect.dev/reference/primitives/hex/) | what it holds depends on `type` |
| `type` | `"address" \| "p256" \| "webcrypto-p256" \| "webauthn-p256"` | the kind of key |

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

const key: SubAccount.Key = { type: "p256", publicKey: `0x${"04".repeat(32)}` };
const payload: SubAccount.Payload = { type: "create", keys: [key] };
```

## SubAccount fields

`SubAccount.SubAccount`.

| Field | Type | Notes |
| --- | --- | --- |
| `address` | [`Address`](https://ethect.dev/reference/primitives/address/) | the sub-account |
| `factory` | [`Address`](https://ethect.dev/reference/primitives/address/) (optional) | present where the wallet knows how to deploy the account |
| `factoryData` | [`Hex`](https://ethect.dev/reference/primitives/hex/) (optional) | the calldata for `factory` |

## Behavior

- **`type` narrows the payload.** TypeScript accepts `factory` only with
  `type: "undeployed"`, and `keys` only with `type: "create"`.
- **Keys are optional.** By the ERC, a wallet that gets no `keys` creates and manages the
  signing key of the new account itself.
- **The chain id is a `number`.** `chainId: 8453` is sent as `"0x2105"`.
- **A draft.** ERC-7895 is not final: the shapes can change, and a wallet may not serve the
  method.
