Skip to main content
An account can authorize account keys, which can create messages on its behalf. The owner of the account can revoke an account key at any time. To add an account key, you’ll need to follow six steps:
  1. Set up Viem clients and @farcaster/hub-web account key.
  2. Register an app FID if your app does not already have one.
  3. Create a new account key for the user.
  4. Use your app account to create a Signed Key Request.
  5. Collect an Add signature from the user.
  6. Call the Key Gateway contract to add the key onchain.

Requirements

  • An ETH wallet on OP Mainnet, with some ETH.
  • An ETH RPC URL for OP Mainnet (e.g. via Alchemy, Infura or QuickNode).

1. Set up clients and account key

First, set up Viem clients and @farcaster/hub-web account key. In this example, we’ll use Viem local accounts and account key, but you can also use ViemWalletEip712Signer to connect to a user’s wallet rather than a local account.

2. Register an app FID

Register an app FID if you don’t already have one. To register an FID, you’ll need to read the price from the ID Gateway, then call the ID Gateway and pay the registration price. You can read back your new FID from the Id Registry contract, or parse it from a Register event. Here, we’ll read it from the registry contract.

3. Create a new account key

Create a new Ed25519 key pair for the user. In a real app, ensure you keep the private key secret.

4. Use your app account to create a Signed Key Request

Create a Signed Key Request, signed by your app account. To do so, you can use the getSignedKeyRequestMetadata helper, which generates and signs the Signed Key Request.

5. Collect an Add signature from the user.

Collect an EIP-712 Add signature from the user to authorize adding an account key to their FID. In a real world app, you’ll likely collect this signature on your frontend, from the user’s wallet. In a frontend context, you can us a ViemEip712WalletSigner to connect to a browser wallet rather than a local signer.

6. Call the Key Gateway contract to add the key onchain.

Call the Key Gateway contract and provide the user’s signature to add the key onchain.

Full code example

See the full code example below for all the steps above.
[@farcaster/hub-web]
See the Key Registry reference for more details.