# Permissions

The four methods about what the user lets an app do: read, ask for and give back the
permissions on methods (EIP-2255), and ask for a session key to act for the user (ERC-7715, a
draft).

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`getPermissions`](#getpermissions) | `wallet_getPermissions` | `Permission[]` | The permissions the user gave the app. |
| [`requestPermissions`](#requestpermissions) | `wallet_requestPermissions` | `Permission[]` | Asks the user for permissions, and returns the ones given. |
| [`revokePermissions`](#revokepermissions) | `wallet_revokePermissions` | `void` | Gives permissions back. |
| [`grantPermissions`](#grantpermissions) | `wallet_grantPermissions` | `PermissionGrant` | Asks the user to let a session key or account act for them. |

Each method returns an
[`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method). The first three
exchange [`Permission`](https://ethect.dev/rpc/types/permission/) values, and `grantPermissions` a
[`PermissionGrant`](https://ethect.dev/rpc/types/permission-grant/).

## getPermissions

Returns the permissions the user gave the app (EIP-2255).

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

const canReadAccounts = Wallet.getPermissions().pipe(
  Effect.map((permissions) =>
    permissions.some(({ parentCapability }) => parentCapability === "eth_accounts"),
  ),
);
```

- **JSON-RPC:** `wallet_getPermissions`
- **Parameters:** none.
- **Returns:** `ReadonlyArray<Permission.Permission>`. Each one names the method it permits in
  `parentCapability`. The list is empty when the app has none.

## requestPermissions

Asks the user for permissions, and returns the ones given (EIP-2255).

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

const connected = Wallet.requestPermissions({ eth_accounts: {} }).pipe(
  Effect.as(true),
  Effect.catchReason("RpcError", "UserRejectedRequest", () =>
    Effect.succeed(false),
  ),
);
```

- **JSON-RPC:** `wallet_requestPermissions`
- **Parameters:** `permissions` ([`Permission.Request`](https://ethect.dev/rpc/types/permission/)): by
  method name, the caveats wanted on it. `{ eth_accounts: {} }` asks for the accounts, with no
  caveat.
- **Returns:** `ReadonlyArray<Permission.Permission>`, the permissions the user gave.
- By the EIP, a wallet rejects with code 4001 when the user refuses: the reason is
  `UserRejectedRequest`.

## revokePermissions

Gives permissions back, such as `eth_accounts` to disconnect the app.

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

const disconnect = Effect.gen(function* () {
  yield* Wallet.revokePermissions({ eth_accounts: {} });
  return yield* Wallet.getPermissions();
});
```

- **JSON-RPC:** `wallet_revokePermissions`
- **Parameters:** `permissions` ([`Permission.Request`](https://ethect.dev/rpc/types/permission/)): the
  permissions to give back, by method name.
- **Returns:** nothing. Whatever the wallet answers is ignored, and the effect succeeds with
  `void`.
- Not served by every wallet.

## grantPermissions

Asks the user to let a session key or account act for them, within limits (ERC-7715, a draft).

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

const session = Effect.gen(function* () {
  const grant = yield* Wallet.grantPermissions({
    // a Unix timestamp, in seconds
    expiry: 1716846083,
    permissions: [
      {
        type: "native-token-transfer",
        data: { ticker: "ETH" },
        policies: [
          { type: "token-allowance", data: { allowance: "0xde0b6b3a7640000" } },
        ],
        required: true,
      },
    ],
    signer: {
      type: "key",
      data: { id: "did:key:zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169" },
    },
  });
  return grant.permissionsContext;
});
```

- **JSON-RPC:** `wallet_grantPermissions`
- **Parameters:** `payload`
  ([`PermissionGrant.Payload`](https://ethect.dev/rpc/types/permission-grant/)): the `permissions` to
  grant, each narrowed by its `policies`, until `expiry`, to a `signer`.
- **Returns:** a [`PermissionGrant.PermissionGrant`](https://ethect.dev/rpc/types/permission-grant/): what
  the wallet granted, which may be less than asked. `permissionsContext` identifies the grant
  when using it.
- The ERC has since been rewritten around another method,
  `wallet_requestExecutionPermissions`. This is the earlier shape, the one wallets that serve
  `wallet_grantPermissions` speak.

## Behavior

- **Two kinds of permission.** An EIP-2255 permission lets the app call a method of the wallet.
  An ERC-7715 grant lets a signer act for the user. They are separate types and do not mix.
- **A wallet tells what it wants.** Only `parentCapability` is required in a `Permission`.
  MetaMask sends every field, with the permitted accounts in a `restrictReturnedAccounts`
  caveat. Rabby only names the method.
- **Open values.** The `value` of a caveat and the `data` of a granted permission, a policy or
  a signer are typed `unknown`: check them before use.
- **Not served by a node.** anvil 1.9 answers the four methods with the reason
  `MethodNotFound`.
