> ## 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.

# AnafiDslRegistry

> Reference of the contract that stores the commit of each account's rule document.

After you read this page, you can read the commit of an account, check a commit attestation, and decode each event and error of the DSL registry.

`AnafiDslRegistry` stores one commit for each account: a 32-byte hash of the rule document and a version counter. It runs behind an ERC-1967 proxy with UUPS upgrades. It has no owner of its own. Its admin is `AnafiRegistry.owner()`.

The contract does not read the rule document and does not run rules. The hash is opaque to it. Each commit names the adapter that the document is committed against.

## The commit value

The account address is always `msg.sender`. The contract writes the commit under the address that calls it:

* A Safe calls the DSL registry in a Safe transaction, so the Safe is `msg.sender`.
* An ERC20Allow account calls the DSL registry from its own address.

The SDK computes the hash with `dslCommitment(salt, yaml)`:

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

// keccak256 of the 32-byte salt followed by the UTF-8 bytes of the YAML text
const commitment = dslCommitment(salt, yaml);
```

The contract accepts any `bytes32`. See [Documents](https://docs.ana.fi/sdk/documents.md) for the salt and the document text.

## Functions for the account

### commitDsl

```solidity theme={null}
function commitDsl(bytes32 hash, uint256 validUntil, bytes calldata engineSig, address adapter) external
```

Records a commit that the engine attested. This is the call that the SDK sends for both account kinds.

| Parameter | Type | Description |
| - | - | - |
| `hash` | `bytes32` | The commitment of the rule document. |
| `validUntil` | `uint256` | The Unix time after which the attestation is not valid. |
| `engineSig` | `bytes` | A 65-byte ECDSA signature of the engine key over the raw digest below. It is mandatory. |
| `adapter` | `address` | The adapter that the document is committed against. |

The engine signs this digest, with no EIP-191 prefix and no EIP-712 domain:

```solidity theme={null}
uint64 newVersion = commitments(msg.sender).version + 1;
bytes32 digest = keccak256(abi.encode(block.chainid, address(dslRegistry), msg.sender, newVersion, hash, validUntil, adapter));
```

The checks run in this order:

1. `block.timestamp > validUntil` reverts `AttestationExpired()`. A call at `validUntil` passes.
2. A malformed signature reverts with an OpenZeppelin `ECDSA` error. A signer other than `AnafiRegistry.engineSigKey()` reverts `InvalidEngineSignature()`.

Effects: stores `(hash, newVersion)` for `msg.sender` and emits `DslCommitted(msg.sender, hash, newVersion, adapter)`.

Because the digest holds `newVersion`, an attestation is valid for one version only. If another commit of the same account lands first, the version moves and the old attestation fails with `InvalidEngineSignature()`. The digest also holds the account, the chain, the DSL registry and the adapter, so a relay cannot change any of them.

The contract does not check that `adapter` is listed in the registry, or that the account granted it a permission.

### commitDslDirect

```solidity theme={null}
function commitDslDirect(bytes32 dslHash, uint64 expectedVersion, bytes calldata sealedDsl, address adapter) external
```

Records a commit with no engine attestation. The account publishes the sealed document itself, in the event.

| Parameter | Type | Description |
| - | - | - |
| `dslHash` | `bytes32` | The commitment of the rule document. |
| `expectedVersion` | `uint64` | The version that this commit must get: `commitments(msg.sender).version + 1`. |
| `sealedDsl` | `bytes` | The sealed rule document. The contract does not store it. It puts it in the event. |
| `adapter` | `address` | The adapter that the document is committed against. |

Reverts `VersionMismatch()` when `expectedVersion` is not the next version. This check stops a stale commit. A stale commit cannot replace a newer one.

Effects: stores `(dslHash, newVersion)` for `msg.sender` and emits `DslCommittedDirect(msg.sender, newVersion, dslHash, sealedDsl, adapter)`.

`commitDsl` and `commitDslDirect` share one version counter. The public SDK flows send `commitDsl`.

## Admin

```solidity theme={null}
function upgradeToAndCall(address newImplementation, bytes memory data) public payable
```

Only `AnafiRegistry.owner()` can upgrade. Another caller gets `CallerNotRegistryOwner()`. The OpenZeppelin UUPS errors also apply: `UUPSUnauthorizedCallContext()`, `UUPSUnsupportedProxiableUUID(bytes32)`, `ERC1967InvalidImplementation(address)`, `ERC1967NonPayable()`.

```solidity theme={null}
function initialize(address anafiRegistry_) external
```

The deployment calls `initialize` once. It reverts `RegistryNotUsable(address registry)` when the registry has no code or its `owner()` is zero, and `InvalidInitialization()` on a second call.

## Views

| Function | Returns | Description |
| - | - | - |
| `commitments(address aaWallet)` | `(bytes32 hash, uint64 version)` | The latest commit of the account. `version` is 0 when the account never committed, and then `hash` is zero. |
| `anafiRegistry()` | `address` | The registry that this contract reads. It never changes. |
| `owner()` | `address` | `AnafiRegistry.owner()`, read at the time of the call. |
| `UPGRADE_INTERFACE_VERSION()` | `string` | `"5.0.0"`. |
| `proxiableUUID()` | `bytes32` | The ERC-1967 slot. Reverts through the proxy. |

```ts theme={null}
import { createPublicClient, http } from "viem";
import { polygonAmoy } from "viem/chains";
import { ANAFI_DSL_REGISTRY_ABI } from "@anafi-cc/anafi-sdk";

const publicClient = createPublicClient({ chain: polygonAmoy, transport: http() });

const [hash, version] = await publicClient.readContract({
  address: "0xa60904ABC7e96B06005234EACF02217afA02d5C3", // polygon-amoy AnafiDslRegistry
  abi: ANAFI_DSL_REGISTRY_ABI,
  functionName: "commitments",
  args: [account],
});
```

## Events

```solidity theme={null}
event DslCommitted(address indexed aaWallet, bytes32 commitment, uint64 version, address adapter);
event DslCommittedDirect(address indexed aaWallet, uint64 version, bytes32 dslHash, bytes sealedDsl, address adapter);

// OpenZeppelin
event Upgraded(address indexed implementation);
event Initialized(uint64 version);
```

| Event | Emitted by | Fields |
| - | - | - |
| `DslCommitted` | `commitDsl` | The account, the commitment, the new version, and the adapter. |
| `DslCommittedDirect` | `commitDslDirect` | The account, the new version, the commitment, the sealed document as given, and the adapter. Note the field order: the version comes before the hash. |

To list the commit history of one account, filter both events on the indexed `aaWallet` topic.

## Errors

| Error | Raised by | Cause |
| - | - | - |
| `InvalidEngineSignature()` | `commitDsl` | The signature recovers to an address other than `engineSigKey()`. This also happens when the version moved after the engine signed. |
| `AttestationExpired()` | `commitDsl` | `block.timestamp` is more than `validUntil`. Get a new attestation. |
| `VersionMismatch()` | `commitDslDirect` | `expectedVersion` is not the next version. Read `commitments` again. |
| `CallerNotRegistryOwner()` | `upgradeToAndCall` | The caller is not `AnafiRegistry.owner()`. |
| `RegistryNotUsable(address registry)` | `initialize` | The registry has no code, or its `owner()` is zero. |
| `ECDSAInvalidSignature()`, `ECDSAInvalidSignatureLength(uint256 length)`, `ECDSAInvalidSignatureS(bytes32 s)` | `commitDsl` | The signature is malformed. |
| `InvalidInitialization()`, `NotInitializing()` | `initialize` | OpenZeppelin initializer guards. |
| `UUPSUnauthorizedCallContext()`, `UUPSUnsupportedProxiableUUID(bytes32 slot)`, `ERC1967InvalidImplementation(address implementation)`, `ERC1967NonPayable()` | `upgradeToAndCall`, `proxiableUUID` | OpenZeppelin upgrade guards. |

## Test vector

Use this vector to check your own computation of the `commitDsl` digest.

| Input | Value |
| - | - |
| `block.chainid` | `196` |
| DSL registry | `0x1111111111111111111111111111111111111111` |
| `msg.sender` | `0x2222222222222222222222222222222222222222` |
| `newVersion` | `4` |
| `hash` | `keccak256("anafi-dsl-commit-digest-vector")` = `0x423dd889a3d166e725bff0e2c83029b6a29745000b38ec2f588591405a65eb68` |
| `validUntil` | `1800000000` |
| `adapter` | `0x3333333333333333333333333333333333333333` |
| Digest | `0x3c36924a25f6adc11ce9e4f3be42cb18c0c570103332aaf2cd4c383464229a32` |

```ts theme={null}
import { encodeAbiParameters, keccak256, recoverAddress } from "viem";

const digest = keccak256(
  encodeAbiParameters(
    [
      { type: "uint256" },
      { type: "address" },
      { type: "address" },
      { type: "uint64" },
      { type: "bytes32" },
      { type: "uint256" },
      { type: "address" },
    ],
    [chainId, dslRegistry, account, newVersion, hash, validUntil, adapter],
  ),
);
// The engine signs the raw digest, so recover over the digest itself.
const signer = await recoverAddress({ hash: digest, signature: engineSig });
```

## Invariants

* The commit of an account changes only in a call that the account sends itself.
* The version of an account goes up by exactly 1 on each commit, and never goes down.
* A commit through `commitDsl` exists only if the engine key of that moment signed the exact account, version, hash, deadline and adapter.

## Storage

All state is in one ERC-7201 namespace, `anafi.storage.AnafiDslRegistry`, at slot `0x9ab5bf54f06b29ee85846e1165ef6b3afdd7211c6fe5730f2fc7a89773778500`.


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