# Permission

A wallet permission (EIP-2255): a method the user lets an app call, with the caveats that
restrict it. The module also holds the request an app makes to get or give back permissions.

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

const methods = Effect.gen(function* () {
  const permissions = yield* Wallet.getPermissions();
  // permissions: ReadonlyArray<Permission.Permission>
  return permissions.map((permission) => permission.parentCapability);
});
```

## Schemas

| Schema | Type | Describes | Used by |
| --- | --- | --- | --- |
| `Permission.schema` | `Permission.Permission` | a permission to call one method | returned by [`Wallet.getPermissions`](https://ethect.dev/rpc/wallet/permissions/#getpermissions) and [`Wallet.requestPermissions`](https://ethect.dev/rpc/wallet/permissions/#requestpermissions) |
| `Permission.caveatSchema` | `Permission.Caveat` | a restriction on a permitted method | the `caveats` of a permission |
| `Permission.requestSchema` | `Permission.Request` | the permissions to ask for or to revoke | passed to [`Wallet.requestPermissions`](https://ethect.dev/rpc/wallet/permissions/#requestpermissions) and [`Wallet.revokePermissions`](https://ethect.dev/rpc/wallet/permissions/#revokepermissions) |

## Permission fields

| Field | Type | Notes |
| --- | --- | --- |
| `parentCapability` | `string` | the method the permission is for, such as `"eth_accounts"` |
| `invoker` | `string` (optional) | the origin of the app the permission was given to |
| `caveats` | `ReadonlyArray<Permission.Caveat>` (optional) | the restrictions on the method |
| `id` | `string` (optional) | |
| `date` | `number` (optional) | the time of the request, as a timestamp |

## Caveat fields

| Field | Type | Notes |
| --- | --- | --- |
| `type` | `string` | the kind of restriction |
| `value` | `unknown` | what it holds depends on `type` |

## Request

A `Permission.Request` is a record: by method name, the caveats wanted on it, by caveat name.

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

// the accounts, with no caveat
const request: Permission.Request = { eth_accounts: {} };

const asked = Wallet.requestPermissions(request);
```

## Behavior

- **A wallet tells what it wants.** Only `parentCapability` is required. MetaMask sends every
  field, with the permitted accounts in a `restrictReturnedAccounts` caveat. Rabby only names
  the method.
- **Caveat values are open.** `value` is typed `unknown`: decode it with a schema of your own
  before use.
- **Unknown fields are dropped.** A field the schema does not know is not in the result.
- **Not a grant.** The permissions a session key gets through `wallet_grantPermissions` are
  another type: see [`PermissionGrant`](https://ethect.dev/rpc/types/permission-grant/).

Decode the accounts of a `restrictReturnedAccounts` caveat with `Address.wireSchema`.

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

const permitted = Effect.gen(function* () {
  const permissions = yield* Wallet.getPermissions();
  const caveat = permissions
    .flatMap((permission) => permission.caveats ?? [])
    .find((caveat) => caveat.type === "restrictReturnedAccounts");
  const decode = Schema.decodeUnknownEffect(Schema.Array(Address.wireSchema));
  return yield* decode(caveat?.value ?? []);
});
```
