> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ana.fi/llms.txt
> Use this file to discover all available pages before exploring further.

# Read-back

> Recover the plaintext rule document and the account state from the engine with one sign-in, and check the reply.

After this page, you know how `recover` gets the plaintext of the committed document and the account state, what the owner signs, how the SDK checks the reply, and when the plaintext is not available.

The chain holds only the salted hash of the document. The plaintext lives in the engine, encrypted on the way in and on the way out. `recover` is the one public way to read it back.

> [!WARNING]
>
> Today, the engine accepts the sign-in of `recover` only when the EIP-4361 message names the domain of the Anafi dapp, `app.ana.fi`. The Anafi API refuses a sign-in for another domain with `422`. An integration on another domain cannot complete this flow today. Keep your own copy of each document that you commit.

## Preconditions

* The signer is a signer of the account: an owner of the Safe, or the ERC20Allow address itself.
* The client config has `siweDomain`, or the code runs in a browser where `window.location.host` gives the domain.
* The call can reach the Anafi API. Today, the Anafi API hosts answer CORS only for `https://app.ana.fi`.

## Sequence

```mermaid theme={null}
sequenceDiagram
    autonumber
    participant W as Owner wallet
    participant A as Your app (SDK)
    participant API as Anafi API
    participant E as Anafi engine (black box)
    participant C as Chain
    A->>A: New random nonce, EIP-4361 text
    W-->>A: Sign-in message signature (personal_sign)
    A->>C: AnafiRegistry.engineEncKey()
    A->>API: POST /api/wallets/{account}/recover (sealed)
    API->>E: Relay
    E-->>A: Sealed reply: state and engine_sig
    A->>C: AnafiRegistry.engineSigKey()
    A->>A: Check the engine signature and the state
```

## Steps

**1. Sign in**

`recover({ aa, eoa })` builds an EIP-4361 message with these fields and asks the wallet to sign it with `signMessage` (a sign-in message prompt):

| Field | Value |
| - | - |
| domain | `siweDomain`, else `window.location.host` |
| address | `eoa` |
| statement | `Sign in to Anafi to recover your wallet state.` |
| URI | `https://` and the domain |
| Chain ID | the chain id of the client |
| Nonce | 16 random bytes as hex |
| Issued At | now, ISO 8601 |

The message has no `Expiration Time` and no `Resources`. The engine accepts a recover sign-in only when its `Resources` list is empty, so a sign-in for another purpose cannot read the state.

**2. Send the sealed request**

The SDK reads `AnafiRegistry.engineEncKey()` and encrypts `{ aa, siwe_message, signature }` to the engine. It keeps the key that opens the reply. It sends `POST /api/wallets/{aa}/recover` with `{ "sealed": ... }`. See [Sealed requests](https://docs.ana.fi/api-reference/sealed-requests.md) and [Confidential channel](https://docs.ana.fi/sdk/confidential-channel.md).

**3. Check the reply**

The reply is sealed to this request. The SDK opens it and reads `{ state, engine_sig }`. Then it:

1. reads `AnafiRegistry.engineSigKey()`;
2. checks that `engine_sig` recovers to that key over `keccak256(nonce ‖ state)`, with the state in a fixed JSON form. The nonce of this request is inside the digest, so a reply to an earlier request does not pass;
3. checks the shape of `state.tenant`.

It returns `state` as `RecoveredUserState`. A failed check throws.

## What can refuse

| Error | HTTP | Meaning | What to do |
| - | - | - | - |
| wallet rejection | none | The owner did not sign. | Ask again. |
| `BackendRequestError` | `422`, body `invalid recover request` | The request did not open, the sealed account is not the account of the path, the sign-in did not verify, it names another domain or chain, or it is outside its time window. | Check `siweDomain`, the chain, and the clock. |
| `BackendRequestError` | `422`, body `stale or replayed request` | The engine saw this sign-in before, or it is too old. | Sign again. |
| `BackendRequestError` | `422`, body `signer is not a signer of the wallet` | The signer is not an owner of the Safe, or not the ERC20Allow address. | Use an owner key. |
| `BackendRequestError` | `502` | The engine could not read the authorization state, or did not answer. The sign-in nonce can be spent. | Sign again later. |
| `BackendRequestError` | `500` | The engine could not read the stored state of the account. The sign-in nonce is spent. | Sign again later. |
| `Error` from the SDK | none | The reply is not signed by `engineSigKey`, or the state has a bad shape. | Do not show the reply. |

## The recovered state

| Field | Type | Meaning |
| - | - | - |
| `dsl` | `RecoveredDsl` or `null` | The document that the engine holds, or `null` when it holds none. |
| `dsl.status` | `"active"` or `"retired"` | `active`: the engine runs this document (it can have no rules). `retired`: no document runs. |
| `dsl.yaml` | string | The plaintext. |
| `dsl.version` | number or `null` | The registry version at which this document landed. `null` for a document that the engine adopted before it recorded versions. |
| `dsl.commitment` | string or `null` | The on-chain commitment of this document. |
| `tenant` | `TenantStatus`, optional | Where the account stands. Absent means the engine said nothing about it, never `none`. |
| `tenant.state` | `"none"`, `"active"`, `"parked"` | `none`: no commit admitted the account. `active`: the permission can be served through `tenant.adapter`. `parked`: the engine stopped the account. |
| `tenant.adapter` | string or `null` | The adapter that the last admitted commit named. |
| `tenant.since_block` | number or `null` | The block that put the account in its state. |
| `tenant.reason` | `ParkedReason` or `null` | Why the account is parked. |
| `tenant.parked_token` | string, optional | The token whose allowance or cap caused the park. |
| `verified_notification_email` | string or `null`, optional | The verified notification inbox, in canonical form. |
| `grant_hash`, `grant_json` | optional | A pair that some engines sign. `null` for a Safe. Treat `grant_json` as opaque. |

### Parked reasons

A parked account starts again when a new commit is admitted. The row and its history stay.

| `tenant.reason` | Meaning | What to do |
| - | - | - |
| `commit_refused` | A later commit of an active account failed admission at its block. | Fix the document. Stage and commit again. |
| `adapter_removed` | The registry removed the adapter. | Commit again through the live adapter of the network. Read it with `readLiveDelegationAdapters()`. |
| `safe_module_disabled` | The Safe disabled the module. | Enable the module and commit again (`write: "arm"`). |
| `safe_reported_disable` | A payment found the module off, and the engine confirmed it on chain. | Enable the module and commit again. |
| `safe_module_guard_set` | The Safe has a module guard. | Remove the guard, then commit again. See [Safe adapter](https://docs.ana.fi/contracts/safe-adapter.md). |
| `dsl_unloadable_at_boot` | The engine can no longer load the committed document. | Commit a new document. |
| `erc20_allow_delegation_blocked` | The account set the block switch. | `setErc20AllowBlocked(false)`, then commit again. |
| `erc20_allow_allowance_revoked` | An allowance went to 0. `parked_token` names it. | Approve the token again, then commit again. |
| `erc20_allow_allowance_too_low` | A payment needs more allowance of one token than the account gave. `parked_token` names it. | Approve the token again, then commit again. |
| `erc20_allow_cap_exhausted` | A cap has no room left. | Raise the cap or wait for the next window, then commit again. |
| `erc20_allow_cap_exceeded` | A payment moves more of one token than its cap leaves. | Raise the cap, then commit again. |

## Trust the plaintext only after a chain check

The engine signature proves that the engine wrote the reply. It does not prove that the reply is current. Before you show `dsl.yaml` as the live document:

1. Read `readDslCommitment(aa)`.
2. Compare its `hash` with `dsl.commitment`. You can also compute `dslCommitment(salt, yaml)` when you hold the salt. The reply does not carry the salt.
3. When they differ, the engine has not processed the latest commit yet, or it does not hold that text.

## When the plaintext is not available

| Case | What you see |
| - | - |
| The account never committed, or no commit was admitted | `dsl: null`, `tenant.state: "none"`. |
| The engine has not processed the commit block yet | `dsl` is older than the chain, or `null`. |
| The engine does not hold the text of the committed document | `dsl` is older than the chain, or `null`, also after the engine passed the commit block. |

To tell the second case from the third, use the synced height of the engine:

1. Read the identity on chain and note the block `B` of that read.
2. Call `readEngineSyncedHeight()`. It makes one unauthenticated GET to the Anafi API's status endpoint, with no wallet prompt. It returns the last block that the engine has processed, or `null` when the height is not known.
3. When the height is below `B`, the engine has not reached the commit. Wait and read again. Do not ask the owner to sign in a loop.
4. When the height is at or above `B`, call `recover`. When the reply still does not hold the committed document, the engine does not hold its text.

## Example

The client must run where the sign-in domain is accepted. See the warning at the top of this page.

```ts theme={null}
import { dslCommitment, type AnafiClient, type Hex } from "@anafi-cc/anafi-sdk";

export async function readBack(anafi: AnafiClient, aa: Hex, eoa: Hex, salt?: Hex) {
  const block = await anafi.resolveReadBlock(null);
  const onChain = await anafi.readDslCommitmentAtBlock(aa, block);
  if (onChain === null) return { kind: "nothing_committed" as const };

  const height = await anafi.readEngineSyncedHeight();
  if (height !== null && height < block) return { kind: "engine_behind" as const, height, block };

  const state = await anafi.recover({ aa, eoa });
  const dsl = state.dsl;
  const current = dsl?.commitment?.toLowerCase() === onChain.hash.toLowerCase();
  if (dsl && current && salt && dslCommitment(salt, dsl.yaml).toLowerCase() !== onChain.hash.toLowerCase()) {
    throw new Error("the text does not match the commitment");
  }
  return {
    kind: current ? ("current" as const) : ("not_current" as const),
    yaml: dsl?.yaml ?? null,
    tenant: state.tenant,
    email: state.verified_notification_email,
  };
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.