# Authorization (/docs/risex/contracts/contract-interface/authorization)

## Overview

The `RISExAuthorization` contract manages session key lifecycle (register, revoke, permissions) and provides
EIP-712 typed data signature verification. It combines three sub-interfaces:

* **ISignerManager** — Session key registration, revocation, and permission management
* **IPermitWitness** — Witness permits with deadline validation
* **IPermitSingle** — ERC-2612 style single-token permits

### Nonce System

RISExAuthorization uses a bitmap-based nonce system for replay protection:

* `nonceAnchor` (uint48): The epoch anchor for the nonce window
* `nonceBitmap` (uint8): The bit index within the 256-bit bitmap for this epoch

This allows up to 256 concurrent valid nonces per epoch, rather than requiring sequential nonce usage.

### Permission System

Session keys can have granular permissions:

```solidity
enum Permission {
    None,     // No permissions
    All,      // Full access
    Perps,    // Perpetual trading only
    Spot,     // Spot trading only
    MoveFund  // Fund movement (deposit/withdraw) only
}
```

***

## Contract Functions

### Register Signer

**Description**

Registers a signer (session key) to sign on behalf of an account. The account must provide an EIP-712 typed data
signature authorizing the signer. The signer is granted `Permission.All` by default.

```solidity
function registerSigner(
    address account,
    address signer,
    string memory message,
    uint48 nonceAnchor,
    uint8 nonceBitmap,
    uint32 expiration,
    bytes memory accountSignature
) external;
```

**Parameters**

* `account`: The account address that will authorize the signer
* `signer`: The signer address that will sign on behalf of the account
* `message`: A human-readable message string included in the account's signature
* `nonceAnchor`: The epoch anchor for replay protection
* `nonceBitmap`: The bit index within the nonce bitmap
* `expiration`: The expiration timestamp (uint32) when the session key expires
* `accountSignature`: EIP-712 signature from the account authorizing the signer

**Example**

```solidity
address account = 0x...;
address signer = 0x...;
string memory message = "Authorize signer";
uint48 nonceAnchor = ...; // Current nonce anchor
uint8 nonceBitmap = 0; // First available bit
uint32 expiration = uint32(block.timestamp + 30 days);

bytes32 registerHash = _hashTypedDataV4(
    keccak256(
        abi.encode(
            REGISTER_SIGNER_TYPEHASH,
            // "RegisterSigner(address signer,string message,uint32 expiration,uint48 nonceAnchor,uint8 nonceBitmap)"
            signer,
            keccak256(abi.encodePacked(message)),
            expiration,
            nonceAnchor,
            nonceBitmap
        )
    )
);
bytes memory accountSignature = _signTypedDataHash(accountPrivateKey, registerHash);

risExAuthorization.registerSigner(
    account,
    signer,
    message,
    nonceAnchor,
    nonceBitmap,
    expiration,
    accountSignature
);
```

***

### Register Sender Signer

**Description**

Registers a signer to sign on behalf of `msg.sender` directly, without requiring a separate account signature.
This is a convenience function when the account owner is the transaction sender. The signer is granted
`Permission.All` by default.

```solidity
function registerSenderSigner(
    address signer,
    uint48 nonceAnchor,
    uint8 nonceBitmap,
    uint32 expiration
) external;
```

**Parameters**

* `signer`: The signer address to authorize
* `nonceAnchor`: The epoch anchor for replay protection
* `nonceBitmap`: The bit index within the nonce bitmap
* `expiration`: The expiration timestamp when the session key expires

**Example**

```solidity
// Register a signer for the calling account (no separate signature needed)
uint48 nonceAnchor = ...; // Current nonce anchor from getNonceState()
risExAuthorization.registerSenderSigner(
    signerAddress,
    nonceAnchor,
    0, // nonceBitmap
    uint32(block.timestamp + 30 days)
);
```

***

### Revoke Signer

**Description**

Revokes a signer's authorization to sign on behalf of an account. The signer must currently be authorized.
The account must provide an EIP-712 typed data signature.

```solidity
function revokeSigner(
    address account,
    address signer,
    uint48 nonceAnchor,
    uint8 nonceBitmap,
    bytes memory accountSignature
) external;
```

**Parameters**

* `account`: The account address that authorized the signer
* `signer`: The signer address to revoke
* `nonceAnchor`: The epoch anchor for replay protection
* `nonceBitmap`: The bit index within the nonce bitmap
* `accountSignature`: EIP-712 signature from the account authorizing the revocation

**Example**

```solidity
address account = 0x...;
address signer = 0x...;
uint48 nonceAnchor = ...;
uint8 nonceBitmap = 1;

bytes32 revokeHash = _hashTypedDataV4(
    keccak256(
        abi.encode(
            REVOKE_SIGNER_TYPEHASH,
            // "RevokeSigner(address signer,uint48 nonceAnchor,uint8 nonceBitmap)"
            signer,
            nonceAnchor,
            nonceBitmap
        )
    )
);
bytes memory accountSignature = _signTypedDataHash(accountPrivateKey, revokeHash);

risExAuthorization.revokeSigner(account, signer, nonceAnchor, nonceBitmap, accountSignature);
```

***

### Enable Permission

**Description**

Enables a specific permission for a signer. Requires an EIP-712 signature from the account.

```solidity
function enablePermission(
    address account,
    address signer,
    uint48 nonceAnchor,
    uint8 nonceBitmap,
    Permission permission,
    bytes memory accountSignature
) external;
```

**Parameters**

* `account`: The account address
* `signer`: The signer address to grant the permission to
* `nonceAnchor`: The epoch anchor for replay protection
* `nonceBitmap`: The bit index within the nonce bitmap
* `permission`: The permission to enable (Perps, Spot, MoveFund, or All)
* `accountSignature`: EIP-712 signature from the account

***

### Disable Permission

**Description**

Disables a specific permission for a signer. Requires an EIP-712 signature from the signer.

```solidity
function disablePermission(
    address account,
    address signer,
    uint48 nonceAnchor,
    uint8 nonceBitmap,
    Permission permission,
    bytes memory signerSignature
) external;
```

**Parameters**

* `account`: The account address
* `signer`: The signer address to revoke the permission from
* `nonceAnchor`: The epoch anchor for replay protection
* `nonceBitmap`: The bit index within the nonce bitmap
* `permission`: The permission to disable
* `signerSignature`: EIP-712 signature from the signer

***

## View Functions

### Get Session Key Status

**Description**

Returns the current status of a session key.

```solidity
function getSessionKeyStatus(
    address account,
    address signer
) external view returns (SessionKeyStatus);
```

**Returns**

* `SessionKeyStatus`: One of `None`, `Authorized`, `Expired`, or `Revoked`

***

### Has Permission

**Description**

Checks whether a signer has a specific permission for an account.

```solidity
function hasPermission(
    address account,
    address signer,
    Permission permission
) external view returns (bool);
```

***

### Session Keys

**Description**

Returns the session key details for an account and signer pair.

```solidity
function sessionKeys(
    address account,
    address signer
) external view returns (uint32 expiration, uint32 permissionBitmap, SessionKeyStatus status);
```

**Returns**

* `expiration`: The expiration timestamp
* `permissionBitmap`: Bitmap of enabled permissions
* `status`: Current session key status

***

### Is Nonce Used

**Description**

Returns whether a specific nonce has been used for an account.

```solidity
function isNonceUsed(
    address account,
    uint48 nonceAnchor,
    uint8 nonceBitmap
) external view returns (bool);
```

***

### Get Nonce State

**Description**

Returns the current nonce anchor and full bitmap for an account.

```solidity
function getNonceState(
    address account
) external view returns (uint48 nonceAnchor, uint256 bitmap);
```

***

### EIP-712 Typehashes

The following typehash accessors are available for constructing EIP-712 signatures:

```solidity
function REGISTER_SIGNER_TYPEHASH() external pure returns (bytes32);
function VERIFY_SIGNER_TYPEHASH() external pure returns (bytes32);
function REVOKE_SIGNER_TYPEHASH() external pure returns (bytes32);
function ENABLE_PERMISSION_TYPEHASH() external pure returns (bytes32);
function DISABLE_PERMISSION_TYPEHASH() external pure returns (bytes32);
function VERIFY_WITNESS_TYPEHASH() external pure returns (bytes32);
function PERMIT_SINGLE_TYPEHASH() external pure returns (bytes32);
```
