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.
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
Theinfo 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:
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, notkemPk. - 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
A200 reply is plain JSON, with one sealed field. See the operation “Stage a rule document” in this reference. Before the owner signs commitDsl, check:
commitmentequals your ownkeccak256(salt ‖ yaml).adapterequals the adapter in your frame.attestationValidUntilis in the future.
sealedWarnings with the response key. The plaintext is:
Open a staging refusal
A409 or a 422 with this body is a refusal of the document:
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 andfetch, without the SDK client.
Read back the account state
Plaintext
The engine checks the sign-in in this order. A failure gives
422.
- The signature recovers to the address in the message.
- The domain is
app.ana.fiand theChain IDis the chain of the host. - The time is inside
Not BeforeandExpiration Time, when the message has them. - The message has no
Resourceslist. - The
Issued Atis 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. - 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_moduleaccount, or the address itself for anerc20_allowaccount. Another signer gets422. If the engine never accepted a commit of this account, the reply holds the empty state:dslisnullandtenant.stateisnone.
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 eachrecover reply. Check the signature before you show the document or act on the state.
- Compute
digest = keccak256(UTF-8(nonce) ‖ UTF-8(canonical JSON of state)).nonceis theNoncevalue of your sign-in message, as text. - Recover the signer from
digestandengine_sigwith plainecrecover, without an EIP-191 prefix. - Compare the signer with
engineSigKey()of theAnafiRegistry. They must be equal.
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:
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
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.