// SPDX-License-Identifier: BUSL-1.1 // Copyright (c) 2026 Liquidbox Corp. // Licensed under the Business Source License 1.1. See LICENSE.md. // Terms of use: https://www.inco.org/terms-of-services // Security contact team@inco.network pragma solidity ^0.8.29; import {UUPSUpgradeable} from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol"; import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol"; import {IWrapperFactory} from "@inco/wrapper-factory/src/interfaces/IWrapperFactory.sol"; import { CTOKEN_SESSION_VERIFIER_NAME, CTOKEN_SESSION_VERIFIER_MAJOR_VERSION, CTOKEN_SESSION_VERIFIER_MINOR_VERSION, CTOKEN_SESSION_VERIFIER_PATCH_VERSION } from "../version/CTokenSessionVerifierConfig.sol"; import {Version} from "../version/Version.sol"; import {ALLOWANCE_GRANTED_MAGIC_VALUE} from "../Types.sol"; import {LightningAddressGetter} from "../lightning-parts/primitives/LightningAddressGetter.sol"; import {IIncoLightning} from "../interfaces/IIncoLightning.sol"; /// @notice A cToken session grants a decrypter temporary access to the sharer's confidential token /// data — and to nothing else the sharer can read. /// @dev ABI encode this struct in the sharerArgData field of the voucher. /// The session is valid only if: /// 1. The current block timestamp is before expiresAt /// 2. The requesting account matches the authorized decrypter /// 3. The handle belongs to a cERC20 the WrapperFactory deployed for a token in scope struct CTokenSession { /// @notice The address authorized to decrypt the sharer's confidential token data address decrypter; /// @notice Unix timestamp after which the session is no longer valid uint256 expiresAt; /// @notice The underlying ERC-20s whose cERC20 wrappers this session covers, e.g. USDC for cUSDC. /// An empty list means the sharer is fine sharing any cERC20 the WrapperFactory deployed, /// and the requester names the tokens to check in a {CTokenSessionRequest} instead. address[] erc20Tokens; } /// @notice The requester's side of a cToken session: which tokens to check this handle against. /// @dev ABI encode this struct in the requesterArgData field of the proof. It is not covered by the /// sharer's signature, so it is read only when the sharer's own token list is empty — that /// empty list being the signed statement that any WrapperFactory cERC20 is acceptable. struct CTokenSessionRequest { /// @notice The underlying ERC-20s to resolve and check the handle against, e.g. USDC for cUSDC. /// Each is still subject to being registered with the pinned WrapperFactory. address[] erc20Tokens; } /// @title CTokenSessionVerifierStorage /// @notice Storage for CTokenSessionVerifier using the ERC-7201 namespaced storage pattern /// @dev Holds the wrapper factory the verifier resolves ERC-20s against. abstract contract CTokenSessionVerifierStorage { /// @notice Storage struct for the verifier's token-scoping configuration. struct CtsvStorage { /// @notice The factory that resolves an underlying ERC-20 to its canonical cERC20. /// @dev Deliberately *not* a field in the signed session payload — see the contract notes. address wrapperFactory; } /// @notice Storage slot location using keccak256 of a unique namespace string bytes32 private constant CTSV_STORAGE_LOCATION = keccak256("inco.storage.CTokenSessionVerifier"); /// @notice Retrieves the storage struct from its dedicated slot /// @dev Uses assembly to directly access the storage slot for gas efficiency /// @return $ Reference to the CtsvStorage struct function _getCtsvStorage() internal pure returns (CtsvStorage storage $) { bytes32 loc = CTSV_STORAGE_LOCATION; assembly { $.slot := loc } } } /// @title CTokenSessionVerifier /// @notice Inco access sharing verifier for browser dApp sessions, scoped to confidential tokens /// @dev Drop-in for {SessionVerifier} — same `canUseSession` signature, same selector — that narrows a /// session from "every handle the sharer can read" to "the sharer's cERC20 data". /// /// Usage: /// 1. User signs a voucher whose sharerArgData is an ABI-encoded {CTokenSession}, with the /// voucher's callFunction set to `CTokenSessionVerifier.canUseSession.selector`. /// 2. The decrypter presents that voucher; requesterArgData carries a {CTokenSessionRequest}. /// 3. This contract grants access when the session is live, the caller is the decrypter, and the /// handle is allowed on a cERC20 the pinned WrapperFactory deployed for an in-scope ERC-20. /// /// The two token lists are alternatives, never a union — the sharer's list always wins: /// /// | session.erc20Tokens | requesterArgData | tokens checked | intent | /// |---------------------|---------------------|---------------------|---------------------------| /// | [USDC] | ignored | [USDC] | sharer pinned the scope | /// | [] (blanket) | [USDC, DAI] | [USDC, DAI] | requester picks, at will | /// | [] (blanket) | empty | none -> denied | nothing named to resolve | /// /// A non-empty signed list is the sharer's scope and requesterArgData cannot widen it. An empty /// signed list is the sharer's signed statement that any WrapperFactory cERC20 is acceptable, so /// the requester supplies the candidates — this is what lets a dApp session cover tokens the user /// had not acquired yet at signing time, without re-signing. /// /// Concretely: a wallet signing "share my cUSDC with this dApp for an hour" sets /// `erc20Tokens: [USDC]` and leaves requesterArgData empty. A portfolio dApp asking for "my /// confidential tokens" signs `erc20Tokens: []` once, then names the tokens it is rendering in /// requesterArgData on each decrypt — an unbounded token set, but still bounded to cERC20s. contract CTokenSessionVerifier is CTokenSessionVerifierStorage, LightningAddressGetter, UUPSUpgradeable, OwnableUpgradeable, Version { /// @notice Emitted when the pinned wrapper factory changes. /// @param previousFactory The factory in effect before this call /// @param newFactory The factory now consulted to resolve ERC-20s to cERC20s event WrapperFactoryUpdated(address indexed previousFactory, address indexed newFactory); /// @notice Thrown when the zero address is passed where a wrapper factory is required. error InvalidWrapperFactory(); /// @notice Initializes the CTokenSessionVerifier with version information. /// @param _salt Unique salt used for deterministic deployment via CreateX /// @param _incoLightningAddress The IncoLightning contract this verifier runs ACL lookups against constructor(bytes32 _salt, address _incoLightningAddress) LightningAddressGetter(_incoLightningAddress) Version( CTOKEN_SESSION_VERIFIER_MAJOR_VERSION, CTOKEN_SESSION_VERIFIER_MINOR_VERSION, CTOKEN_SESSION_VERIFIER_PATCH_VERSION, _salt, CTOKEN_SESSION_VERIFIER_NAME ) {} /// @notice Verifies if an account can use a cToken session to access an encrypted handle. /// @dev This function is called by the ACL system when validating access permissions. /// Access is granted if the session has not expired, the caller is the authorized /// decrypter, and the handle belongs to an in-scope cERC20. /// @param handle The encrypted value handle access is being requested for /// @param account The address requesting access (must match session.decrypter) /// @param sharerArgData ABI-encoded CTokenSession struct containing decrypter, expiration, and scope /// @param requesterArgData ABI-encoded CTokenSessionRequest naming the ERC-20s to check. Read only /// when the session's own list is empty, so it can never widen a scope the sharer pinned down. /// @return ALLOWANCE_GRANTED_MAGIC_VALUE if access is granted, bytes32(0) otherwise function canUseSession(bytes32 handle, address account, bytes memory sharerArgData, bytes memory requesterArgData) external view returns (bytes32) { CTokenSession memory session = abi.decode(sharerArgData, (CTokenSession)); if (session.expiresAt < block.timestamp || session.decrypter != account) { return bytes32(0); } // An empty signed list is blanket consent to any factory cERC20, so the requester says which // ones to look at. A non-empty list is the whole scope and requesterArgData is ignored. address[] memory erc20Tokens; if (session.erc20Tokens.length > 0) { erc20Tokens = session.erc20Tokens; } else if (requesterArgData.length > 0) { erc20Tokens = abi.decode(requesterArgData, (CTokenSessionRequest)).erc20Tokens; } else { // Blanket consent, but nobody named a token: nothing to resolve, so nothing is in scope. return bytes32(0); } IWrapperFactory factory = IWrapperFactory(_getCtsvStorage().wrapperFactory); IIncoLightning lightning = IIncoLightning(incoLightningAddress); for (uint256 i = 0; i < erc20Tokens.length; i++) { address cToken = factory.getWrapper(IERC20(erc20Tokens[i])); if (cToken != address(0) && lightning.isAllowed(handle, cToken)) { return ALLOWANCE_GRANTED_MAGIC_VALUE; } } return bytes32(0); } /// @notice Returns the wrapper factory that resolves ERC-20s to cERC20s. function wrapperFactory() external view returns (address) { return _getCtsvStorage().wrapperFactory; } /// @notice Repoints the verifier at a different wrapper factory. /// @dev Changes the token universe of every existing session at once, so it is owner-gated. /// @param factory The wrapper factory to consult function setWrapperFactory(address factory) external onlyOwner { require(factory != address(0), InvalidWrapperFactory()); CtsvStorage storage $ = _getCtsvStorage(); emit WrapperFactoryUpdated($.wrapperFactory, factory); $.wrapperFactory = factory; } /// @notice Authorizes contract upgrades (restricted to owner only) /// @dev Required by UUPSUpgradeable. Only the contract owner can upgrade. function _authorizeUpgrade(address) internal view override { require(msg.sender == owner()); } /// @notice Initializes the contract with an owner address and a wrapper factory /// @dev Must be called immediately after deployment via proxy. Can only be called once. /// @param owner The address that will own this contract and can authorize upgrades /// @param factory The wrapper factory that resolves an underlying ERC-20 to its canonical cERC20 function initialize(address owner, address factory) public initializer { __Ownable_init(owner); require(factory != address(0), InvalidWrapperFactory()); _getCtsvStorage().wrapperFactory = factory; emit WrapperFactoryUpdated(address(0), factory); } /// @notice Required for CreateX deterministic deployment /// @dev Empty fallback allows the contract to be deployed via CreateX's create2 mechanism fallback() external {} }