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

# AnafiOffRampFallbackHandler

> Reference of the Safe fallback handler that accepts an engine-signed bank payout order under a policy of the Safe.

After you read this page, you know how a Safe accepts an engine-signed off-ramp order, which policy bounds it, and every function, event and error of the handler.

`AnafiOffRampFallbackHandler` is a Safe fallback handler. A Safe installs it with its own `setFallbackHandler`. The off-ramp provider that the rule language names, Monerium, asks the Safe for an ERC-1271 signature check, `isValidSignature(hash, signature)`. The Safe forwards the question to this handler. The stablecoin stays in the Safe, and the provider burns it from the Safe.

The handler is one plain contract for all Safes. It has no owner, no proxy and no upgrade path. Its state is keyed by the Safe that calls it. A fix is a new handler that each Safe installs itself.

> [!NOTE]
>
> Only the `polygon-amoy` deployment records an `offRampFallbackHandler` address. See [Deployments](https://docs.ana.fi/contracts/deployments.md).

## Requirements and limits

* The handler works with Safe 1.5.0 only. Its standard signature path calls `checkSignatures(address,bytes32,bytes)`, which only a 1.5.0 Safe has. On a 1.3.0 or 1.4.1 Safe, an owner signature does not validate through this handler.
* A Safe has one fallback handler. When the Safe installs another handler, orders stop validation. No funds move. The policy and the approvals stay in this handler and apply again when the Safe installs it again.
* For an order, the handler checks the policy and the engine signature. It reads no token balance.

## What the handler accepts

`isValidSignature(bytes32 hash, bytes signature)` answers for `msg.sender`, which is the Safe that forwards the call. It tries three paths in this order.

### 1. An approved message hash

When `signature` is empty and the Safe approved `hash` with `setApprovedMessageHash`, and the approval did not lapse, the answer is the magic value `0x1626ba7e`.

An approved hash is a public proof. Anyone can present it with an empty signature while the approval is valid. It does not authorize an order.

### 2. An engine-signed order

When `signature` is at least 160 bytes long and its first 32-byte word equals `OFF_RAMP_BLOB_KIND` (1), the handler reads it as an order:

```solidity theme={null}
abi.encode(uint8 kind, uint256 amountMinor, string iban, string timestamp, bytes engineSig)
```

The handler answers the magic value `0x1626ba7e` only when every check passes. Each failed check answers `0xffffffff`, and the call does not revert:

1. The blob decodes.
2. The policy of the Safe has a non-zero `authority`.
3. `block.timestamp` is less than the policy `expiry`.
4. `amountMinor` is not more than the policy `perOrderCapMinor`.
5. The IBAN is on the policy allowlist, or the allowlist is empty. The handler compares `keccak256(bytes(iban))`.
6. The EIP-191 hash of the sentence `Send EUR <amount> to <iban> at <timestamp>` equals `hash`. The handler writes `<amount>` as the whole euros, a point, and exactly two digits of cents.
7. `engineSig` recovers, with no error, to a non-zero signer over the raw digest below.
8. `authority.engineSigKey()` answers with one address word, and that address equals the signer.

```solidity theme={null}
bytes32 OFF_RAMP_ORDER_TAG = keccak256("ANAFI_OFF_RAMP_ORDER_V1");
// = 0x0e0c5852fdbb8e26d9e1ef15f1aa5226392585128d1c2bfa9ef7c53725a227c7

bytes32 engineSignedDigest = keccak256(abi.encode(OFF_RAMP_ORDER_TAG, block.chainid, safe, hash));
```

The digest holds the chain and the Safe, so one order is valid for one Safe on one chain. The engine key is the one that the authority returns at the time of the check, so a key change at the registry applies at once.

The handler keeps no record of the orders that it accepted. One order stays valid at its own Safe while its policy checks pass. The handler itself does not limit a replay of an order.

Example of the sentence and its hash:

| Part | Value |
| - | - |
| `amountMinor` | `150000` |
| `iban` | `DE89370400440532013000` |
| `timestamp` | `2026-10-01T07:02Z` |
| Sentence | `Send EUR 1500.00 to DE89370400440532013000 at 2026-10-01T07:02Z` |
| `hash`, from `hashMessage` in viem | `0x9a9ae50c1737e3c0199181e1146de19edcf025c10d186d3554922c94143ed700` |

### 3. The standard Safe check

Each other signature goes to the standard Safe check, which the handler copies from Safe `CompatibilityFallbackHandler` v1.5.0:

* With an empty signature, the Safe must have the `SafeMessage` hash of `hash` in its `signedMessages`. Else the call reverts with `"Hash not approved"`.
* With a signature, the Safe checks the owner signatures over the `SafeMessage` hash with `checkSignatures(address(0), messageHash, signature)`. A refusal reverts in the Safe.

## Functions of the Safe

The Safe calls these functions on itself, for example `safe.setOffRampPolicy(terms)` in a Safe transaction. The Safe fallback forwards the call to the handler with the original sender appended. The handler accepts the call only when that sender is the Safe itself. Else it reverts `CallerNotSafeItself()`.

A module that the Safe enabled can also make the Safe call itself, so a module can also write these entries.

### setOffRampPolicy

```solidity theme={null}
struct OffRampPolicyTerms {
    address authority;         // the AnafiRegistry whose engineSigKey() may sign; zero clears the policy
    uint256 perOrderCapMinor;  // the largest order, in cents
    uint64 expiry;             // the Unix time from which the policy answers nothing; zero never passes
    string[] ibanAllowlist;    // the IBANs that an order may pay; empty accepts any
}

function setOffRampPolicy(OffRampPolicyTerms calldata terms) external
```

Replaces the off-ramp policy of the Safe. A zero `authority` switches off-ramp orders off. The handler stores `keccak256(bytes(iban))` of each IBAN. It emits `OffRampPolicySet` with the hashes.

Reverts `OffRampAllowlistTooLong()` when the allowlist has more than `MAX_OFF_RAMP_IBAN_ALLOWLIST` (16) entries, and `CallerNotSafeItself()` for a stranger.

### setApprovedMessageHash

```solidity theme={null}
function setApprovedMessageHash(bytes32 messageHash, uint64 validUntil) external
```

Approves `messageHash` for the Safe until `validUntil`. The handler then accepts it with an empty signature. A zero `validUntil` withdraws the approval. A new call replaces an earlier approval. Emits `MessageHashApprovalSet`.

A Safe can use this path to prove its address to the provider at link time, with the EIP-191 hash of the link sentence of the provider. The link sentence is the same for every account, so its approval lets anyone claim the Safe address while the approval is valid. The owner signature path of the standard check proves ownership with no such public approval.

## Views

| Function | Returns | Description |
| - | - | - |
| `offRampPolicyOf(address safe)` | `(address authority, uint256 perOrderCapMinor, uint64 expiry, bytes32[] ibanAllowlist)` | The policy of the Safe. `ibanAllowlist` holds the IBAN hashes. A zero `authority` means no policy. |
| `isMessageHashApproved(address safe, bytes32 messageHash)` | `bool` | Whether the handler accepts `messageHash` for the Safe with an empty signature now. |
| `decodeOffRampBlob(bytes blob)` | `(uint8 kind, uint256 amountMinor, string iban, string timestamp, bytes engineSig)` | Decodes an order blob. Pure. Reverts on malformed bytes. `isValidSignature` calls it under `try`. |
| `isValidSignature(bytes32 hash, bytes signature)` | `bytes4` | See above. It answers for `msg.sender`. |

```ts theme={null}
import { createPublicClient, http, parseAbi } from "viem";
import { polygonAmoy } from "viem/chains";

const publicClient = createPublicClient({ chain: polygonAmoy, transport: http() });
const HANDLER_ABI = parseAbi([
  "function offRampPolicyOf(address safe) view returns (address authority, uint256 perOrderCapMinor, uint64 expiry, bytes32[] ibanAllowlist)",
]);

const [authority, perOrderCapMinor, expiry, ibanAllowlist] = await publicClient.readContract({
  address: "0x5F5dAF4084529D60a9144e002e67eC2DF4D35f7F", // polygon-amoy
  abi: HANDLER_ABI,
  functionName: "offRampPolicyOf",
  args: [safe],
});
```

## Standard Safe handler functions

The handler copies these functions from Safe `CompatibilityFallbackHandler` v1.5.0, and inherits the token callbacks from Safe `TokenCallbackHandler`. Each one assumes that the caller is a Safe.

| Function | Returns | Description |
| - | - | - |
| `getMessageHash(bytes message)` | `bytes32` | The `SafeMessage` hash of `message` for the Safe that calls. |
| `encodeMessageDataForSafe(address safe, bytes message)` | `bytes` | The EIP-712 preimage of the `SafeMessage` hash for `safe`. |
| `getMessageHashForSafe(address safe, bytes message)` | `bytes32` | The `SafeMessage` hash of `message` for `safe`. |
| `getModules()` | `address[]` | The first 10 modules of the Safe that calls. |
| `simulate(address targetContract, bytes calldataPayload)` | `bytes` | Simulates a delegatecall through the Safe that calls, and reverts its effects. |
| `encodeTransactionData(address to, uint256 value, bytes data, uint8 operation, uint256 safeTxGas, uint256 baseGas, uint256 gasPrice, address gasToken, address refundReceiver, uint256 nonce)` | `bytes` | The preimage of the Safe transaction hash. |
| `onERC1155Received(address, address, uint256, uint256, bytes)` | `bytes4` | `0xf23a6e61`. Reverts `"not a fallback call"` unless the caller is a Safe with this handler installed. |
| `onERC1155BatchReceived(address, address, uint256[], uint256[], bytes)` | `bytes4` | `0xbc197c81`. Same guard. |
| `onERC721Received(address, address, uint256, bytes)` | `bytes4` | `0x150b7a02`. Same guard. |
| `tokensReceived(address, address, address, uint256, bytes, bytes)` | none | Does nothing. |
| `supportsInterface(bytes4 interfaceId)` | `bool` | `true` for the ERC-1155 receiver, ERC-721 receiver and ERC-165 interfaces. |

## Constants

| Constant | Value | Description |
| - | - | - |
| `OFF_RAMP_BLOB_KIND` | `1` | The first word of an order blob. |
| `OFF_RAMP_ORDER_TAG` | `0x0e0c5852fdbb8e26d9e1ef15f1aa5226392585128d1c2bfa9ef7c53725a227c7` | `keccak256("ANAFI_OFF_RAMP_ORDER_V1")`, the tag at the start of the order digest. |
| `MAX_OFF_RAMP_IBAN_ALLOWLIST` | `16` | The most IBANs in a policy. |

## Events

```solidity theme={null}
event OffRampPolicySet(
    address indexed safe,
    address indexed authority,
    uint256 perOrderCapMinor,
    uint64 expiry,
    bytes32[] ibanAllowlist
);
event MessageHashApprovalSet(address indexed safe, bytes32 indexed messageHash, uint64 validUntil);
```

| Event | When | Fields |
| - | - | - |
| `OffRampPolicySet` | `setOffRampPolicy` | The Safe, the authority (zero for a clear), the cap in cents, the expiry, and the hash of each allowed IBAN. |
| `MessageHashApprovalSet` | `setApprovedMessageHash` | The Safe, the message hash, and the end of the approval (zero for a withdrawal). |

## Errors

| Error | Raised by | Cause |
| - | - | - |
| `CallerNotSafeItself()` | `setOffRampPolicy`, `setApprovedMessageHash` | The call did not come from the Safe itself through its fallback. |
| `OffRampAllowlistTooLong()` | `setOffRampPolicy` | The allowlist has more than 16 entries. |

The order path never reverts. It answers `0xffffffff` for each refusal. The standard path can revert, as the Safe handler does.

## Stop off-ramp orders

The Safe owners can stop the order path in one Safe transaction, with either call:

* `setOffRampPolicy` with a zero `authority`.
* `setFallbackHandler` on the Safe with another handler.


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