Skip to content

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.

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);
});
Schema Type Describes Used by
Permission.schema Permission.Permission a permission to call one method returned by Wallet.getPermissions and Wallet.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 and Wallet.revokePermissions
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
Field Type Notes
type string the kind of restriction
value unknown what it holds depends on type

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

import { type Permission, Wallet } from "@ethect/core/rpc";
// the accounts, with no caveat
const request: Permission.Request = { eth_accounts: {} };
const asked = Wallet.requestPermissions(request);
  • 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.

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

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 ?? []);
});