# RedactCore.sol

### Overview

[RedactCore.sol](https://github.com/FhenixProtocol/redact/blob/master/packages/hardhat/contracts/RedactCore.sol) is the central management contract for the Redact protocol. It handles the deployment and tracking of confidential tokens (ConfidentialERC20) and manages special cases like ConfidentialETH. The contract maintains a registry of all deployed confidential tokens and their relationships with underlying ERC20 tokens.

### Key Concepts

#### Token Registry

* Maintains a mapping between ERC20 tokens and their confidential wrappers
* Uses `EnumerableMap` for efficient iteration and lookup
* Prevents duplicate deployments of confidential wrappers

#### Special Token Handling

* Manages ConfidentialETH (eETH) as a special case
* Tracks stablecoins separately for potential special handling
* Maintains WETH integration for ETH operations

#### Access Control

* Implements `Ownable2Step` for secure ownership management
* Restricts critical operations to the contract owner (EX: renaming ConfidentialERC20s)

### External Functions

#### View Functions

**`getFherc20(address erc20)`**

* Returns the address of the confidential wrapper for a given ERC20 token
* Returns `address(0)` if no wrapper exists
* Used to look up confidential token addresses

**`getIsStablecoin(address erc20)`**

* Returns whether a token is registered as a stablecoin
* Used for special handling of stablecoin tokens

**`getIsWETH(address erc20)`**

* Returns whether a token is the WETH contract
* Used for ETH-specific operations

**`getDeployedFherc20s()`**

* Returns an array of all deployed confidential tokens
* Includes both the original ERC20 and its confidential wrapper
* Useful for UI integration and monitoring

#### State-Changing Functions

**`deployFherc20(IERC20 erc20)`**

* Deploys a new ConfidentialERC20 wrapper for the specified token
* Prevents deployment for:
  * Already wrapped tokens
  * Stablecoins
  * WETH (handled separately)
* Emits `Fherc20Deployed` event

**`updateStablecoin(address stablecoin, bool isStablecoin)`**

* Updates the stablecoin status of a token
* Restricted to contract owner
* Emits `StablecoinUpdated` event

**`updateFherc20Symbol(ConfidentialERC20 fherc20, string memory updatedSymbol)`**

* Updates the symbol of a deployed confidential token
* Restricted to contract owner
* Emits `Fherc20SymbolUpdated` event

### Special Cases

#### ConfidentialETH (eETH)

* Pre-configured during contract deployment
* Uses WETH as the underlying token
* Handles native ETH wrapping/unwrapping
* Special gas optimizations for ETH operations

#### Stablecoins

* Can be marked as stablecoins for special handling
* Prevents deployment of confidential wrappers for stablecoins
* Allows for future implementation of stablecoin-specific features
* Prepares for a future unified encrypted stablecoin.

### Events

#### `Fherc20Deployed(address indexed erc20, address indexed fherc20)`

* Emitted when a new confidential token is deployed
* Includes both the original and confidential token addresses

#### `StablecoinUpdated(address indexed erc20, bool isStablecoin)`

* Emitted when a token's stablecoin status is updated
* Includes the token address and new status

#### `Fherc20SymbolUpdated(address indexed fherc20, string symbol)`

* Emitted when a confidential token's symbol is updated
* Includes the token address and new symbol

### Error Handling

#### Custom Errors

* `Invalid_AlreadyDeployed()`: Attempted to deploy wrapper for already wrapped token
* `Invalid_Stablecoin()`: Attempted to deploy wrapper for stablecoin
* `Invalid_WETH()`: Invalid WETH address provided
* `Invalid_eETH()`: Invalid eETH address provided

### Integration Guide

#### Deployment

1. Deploy WETH contract if not already deployed
2. Deploy ConfidentialETH (eETH) contract
3. Deploy RedactCore with WETH and eETH addresses

#### Adding New Tokens

1. Check if token is already wrapped using `getFherc20`
2. If not wrapped, call `deployFherc20` with the token address
3. Monitor `Fherc20Deployed` event for confirmation

#### UI Integration

1. Use `getDeployedFherc20s` to list all available confidential tokens
2. Use `getFherc20` to look up specific token wrappers
3. Handle special cases for ETH and stablecoins appropriately

### Security Considerations

1. **Access Control**
   * Critical functions restricted to owner
   * Two-step ownership transfer process
   * Careful management of stablecoin status
2. **Deployment Safety**
   * Prevention of duplicate deployments
   * Validation of WETH and eETH addresses
   * Protection against stablecoin wrapping
3. **Token Management**
   * Secure symbol updates
   * Proper event emission for tracking
   * Safe handling of special cases
