Skip to main content
After you read this page, you can seal data that only the engine can read, open a reply that only you can read, read back the committed rule document of an account, and check that the engine signed the reply. The engine is a black box here. The SDK seals each request to the public key that AnafiRegistry.engineEncKey() publishes on chain. The Anafi API carries the sealed bytes and cannot read them. See security and sealed requests.

Cipher suite

HPKE (RFC 9180) in base mode with: The engine uses the same suite. A change of any part makes every envelope unreadable.

One-way seal

seal

Seals payload to the engine key under info. Each call uses a new ephemeral key, so two seals of one payload differ. enginePubkey must be 32 bytes, else it throws Error("enginePubkey must be 32 bytes, got N"). Both fields are JSON arrays of numbers, not hex strings. The key name is kem_pk in snake case.

dslSealInfo

The HPKE info of a rule document: the UTF-8 text anafi/dsl-seal/<network>/, then the 20 raw bytes of the account address. The engine derives the same info from the account in the request path. So a document sealed for one account and network does not open for another account or network. It throws for an unknown network or an invalid address.

Request and response channel

A seal alone gives the engine no private way to answer. The channel keeps the HPKE context of the request and exports a 32-byte response key from it. The engine derives the same key and seals its reply with it. Only the caller that sealed the request can open the reply.

sealRequest

Seals the request as seal does, and keeps the response key, exported with the context anafi/confidential-call/resp/v1. The envelope has the same wire form as a seal envelope. Keep secret in memory only, and only until you open the reply.

openResponse

Opens a sealed reply with ChaCha20-Poly1305, the exported key, and an empty additional data. A reply for another request fails to open and rejects.

Request tags

Each route takes the body { "sealed": SealedEnvelope }. A sealed reply has the form SealedResponse.

recover

Reads back the state of an account from the engine: the committed rule document in plain text, the status of the account, and the verified notification inbox. The chain holds only the salted commitment, so this is the only way to read the text of a committed document.

Steps

  1. Make a 16-byte random nonce, as 32 hex characters with no 0x.
  2. Render the sign-in with renderSiweMessage: the domain siweDomain (or window.location.host), the address eoa, the statement Sign in to Anafi to recover your wallet state., the URI https://<domain>, the chainId, the nonce and Issued At. No expiry line.
  3. Prompt: walletClient.signMessage({ account: eoa, message }).
  4. Read engineEncKey() and seal { "aa", "siwe_message", "signature" } with CONFIDENTIAL_CALL_REQUEST_INFO.
  5. POST /api/wallets/:aa/recover with { sealed }.
  6. Open the reply, and parse it as RecoverResponsePayload.
  7. Read AnafiRegistry.engineSigKey(). Check that engine_sig recovers to that address over recoverResponseDigest(nonce, state). Else throw.
  8. Check the tenant field (assertRecoveredTenantValid). Else throw.
  9. Return state.
The sign-in message:
The Anafi API accepts this sign-in only when the message names the domain of the Anafi dapp, app.ana.fi. It refuses a sign-in for another domain with status 422. An integration on another domain cannot complete recover today.
Throws: BackendRequestError for a failed request (for example 422 for a refused sign-in), an Error for a reply that does not open, a bad signature, or a bad tenant, and the error of the wallet when the owner rejects the prompt.

RecoveredUserState

The field names are in snake case, as the engine writes them.

TenantLifecycle

ParkedReason

parked_token names the token that caused the stop, when one token did. It is present only on a parked account.

Example reply

The engine signature check

The channel proves that only the engine key holder could make the reply. The signature proves that the engine signing key, which the registry publishes as engineSigKey(), wrote this exact state for this exact request. recoverResponseDigest(nonce, state) is keccak256(utf8(nonce + canonicalStateJson)):
  • nonce is the nonce of the sign-in. It binds the reply to this request, so an old reply cannot answer a new prompt.
  • The state JSON is compact, with no spaces. The fields come in this order: dsl (with status, yaml, version, commitment), then grant_hash and grant_json when present, then tenant (with state, adapter, since_block, reason, then parked_token when present), then verified_notification_email when present. Keys of any map inside grant_json are sorted by code point.
  • An absent field is left out. A null field prints null.
  • A non-integer number makes the function throw.
assertRecoverResponseSignedByEngine checks that engineSig is 65 bytes of hex, recovers the signer of the raw digest (no EIP-191 prefix), and throws unless the signer is engineSigKey. assertRecoveredTenantValid checks the tenant field after the signature check: a known state; parked_token only on a parked account and only as an address; for none, all of adapter, since_block, reason are null; for active and parked, an address and a block of 0 or more; for active, a null reason; for parked, a known reason. An absent tenant passes.

Sign-in messages

renderSiweMessage

Renders an EIP-4361 message. The lines are joined with \n, and the address is checksummed:

The sign-ins of the SDK

The notification-email sign-in is on notification email. The rule generation sign-in is on rule builders.