Skip to main content
After you read this page, you can call the four sealed routes of the Anafi API without the SDK. You can seal a payload to the engine, open the reply, and check the engine signature. The four sealed routes: The Anafi API passes each envelope to the engine without change and cannot read it. Only the engine can open it. The reply is sealed to the caller with a key that only the caller and the engine know.
Do not plan an integration on another domain that uses recover or the notification-email code request. These two sign-in (EIP-4361) flows are accepted only when the sign-in message names the domain of the Anafi dapp, app.ana.fi. A sign-in for another domain is refused with 422. An integration on another domain cannot complete these two flows today. The stage route and the verify route take no sign-in.

The steps

1

Read the engine key

Call engineEncKey() on the AnafiRegistry of the network. It returns a bytes32. These 32 bytes are the raw X25519 public key of the engine. The registry address of each network is on Deployments. The key can change, so read it before each seal. See Registry.
2

Build the plaintext

Build the payload of the route, as the sections below show.
3

Seal and export

Seal the payload with HPKE under the info tag of the route. Then export the 32-byte response key from the same HPKE context. Keep the response key in memory. It is the only key that opens the reply.
4

Send the envelope

Send {"sealed": {"kem_pk": [...], "ct": [...]}} to the route, with Content-Type: application/json.
5

Open the reply

Open {"nonce": [...], "ct": [...]} with ChaCha20-Poly1305 under the response key. For recover, check the engine signature in the opened payload.

HPKE suite

The engine accepts only this suite. A change of any part makes the envelope unopenable.
  • Seal once per context (single-shot), with an empty AAD.
  • Use a new ephemeral key for each seal. Two seals of one payload give different envelopes.
  • The engine public key must be exactly 32 bytes.

Info tags

The info tag binds the envelope to its purpose. For three routes it also binds the network and the account. An envelope sealed for one account or one network does not open for another one. The engine derives the tag from the aa path segment and from its own network, so a relay cannot move an envelope to another account. The network token is polygon, xlayer or polygon-amoy, with no other spelling. The address bytes are the raw 20 bytes, not hex text. Example: the stage tag for account 0xf9836de53d455019ed985896fce8af80be66b7f9 on polygon-amoy is 48 bytes:
The SDK builds these tags with dslSealInfo(network, aa), notificationEmailSealInfo(route, network, aa) and the constant CONFIDENTIAL_CALL_REQUEST_INFO.

Response key

After the seal, export a secret from the same HPKE sender context: All four routes use this exporter context. The info tag already separates the routes.

Envelope format

The request body of each sealed route:
  • Each field is a JSON array of integers from 0 to 255. A hex string is refused with 422.
  • The keys are snake_case: kem_pk, not kemPk.
  • The body of the stage route, the recover route and the email routes is at most 512 KiB. Each byte of an array takes up to 4 characters of JSON.

Reply format

A sealed reply has no encapsulated key. It uses the response key that you exported.
Open it with ChaCha20-Poly1305: key = the response key, nonce = nonce, AAD = empty. The plaintext is UTF-8 JSON. The sealed reply comes as the whole 200 body of recover and of the email routes. The stage route puts sealed replies in sealedWarnings (on 200) and in sealed_error (on 409 and 422). Only the holder of the engine private key can derive your response key. So a reply that opens came from the engine and answers your request. If the reply does not open, discard it.

Stage a rule document

Plaintext

The stage plaintext is a binary frame, not JSON:
The commitment is keccak256(salt ‖ yaml). The adapter and the parent are not part of the commitment.

Reply

A 200 reply is plain JSON, with one sealed field. See the operation “Stage a rule document” in this reference. Before the owner signs commitDsl, check:
  1. commitment equals your own keccak256(salt ‖ yaml).
  2. adapter equals the adapter in your frame.
  3. attestationValidUntil is in the future.
Open sealedWarnings with the response key. The plaintext is:
The list is empty for a clean document. The findings are advice. They do not stop the commit.

Open a staging refusal

A 409 or a 422 with this body is a refusal of the document:
Open sealed_error with the response key. The plaintext is:

Engine signature

engineSig in the stage reply is the attestation of the engine for the commit. You do not check it off chain. AnafiDslRegistry.commitDsl checks it on chain against engineSigKey of the registry. See DSL registry.

Example

This example stages a document with SDK exports and fetch, without the SDK client.

Read back the account state

Plaintext

The engine checks the sign-in in this order. A failure gives 422.
  1. The signature recovers to the address in the message.
  2. The domain is app.ana.fi and the Chain ID is the chain of the host.
  3. The time is inside Not Before and Expiration Time, when the message has them.
  4. The message has no Resources list.
  5. The Issued At is at most 5 minutes old and at most 60 seconds in the future, and the address has not used the nonce before. The nonce is used up from this step on.
  6. If the engine accepted a commit of this account before, the signer must be a signer of the account: an owner of the Safe for a safe_module account, or the address itself for an erc20_allow account. Another signer gets 422. If the engine never accepted a commit of this account, the reply holds the empty state: dsl is null and tenant.state is none.
The SDK signs this statement: Sign in to Anafi to recover your wallet state. The message format is the EIP-4361 format that renderSiweMessage writes.

Reply plaintext

Account states in state.tenant.state: Reasons in state.tenant.reason for a parked account: See ERC-20 allowance adapter and Safe adapter.

Check the engine signature

The engine signs each recover reply. Check the signature before you show the document or act on the state.
  1. Compute digest = keccak256(UTF-8(nonce) ‖ UTF-8(canonical JSON of state)). nonce is the Nonce value of your sign-in message, as text.
  2. Recover the signer from digest and engine_sig with plain ecrecover, without an EIP-191 prefix.
  3. Compare the signer with engineSigKey() of the AnafiRegistry. They must be equal.
The canonical JSON of state is compact, with no spaces, and has its keys in this order: Strings use the escapes of JSON.stringify. Numbers are integers. The nonce in the digest binds the reply to your request. An old reply to another sign-in does not verify. The SDK exports recoverResponseDigest(nonce, state) and assertRecoverResponseSignedByEngine({ nonce, state, engineSig, engineSigKey }) for this check.

Verify a notification email

Code request plaintext

The canonical email is the email value with ASCII white space removed at both ends, in lowercase. An address with a non-ASCII character, white space inside, or one of the characters <, >, ,, ;, " has no canonical form. The sign-in must pass the checks 1, 2, 3 and 5 of the recover sign-in, and the signer must be a signer of the account. In place of check 4, its Resources list must hold exactly one entry:
The SDK signs the statement Verify <canonical email> to receive notifications from wallet <aa in lowercase hex>. and an Expiration Time 5 minutes after Issued At. The SDK function notificationEmailCodeSiweMessage writes this message.

Code request reply plaintext

Check that aa equals your account.

Verify plaintext and reply

The reply plaintext has aa, email and outcome: The email replies carry no engine signature. The response key proves that the engine made them. Check that aa equals your account. See Notification email flow and SDK notification email.

Engine signatures

See Security for the trust model and SDK confidential channel for the SDK functions.