// SPDX-License-Identifier: MIT pragma solidity 0.8.28; import {AgentRegistry} from "../AgentRegistry.sol"; import {VenueLock} from "../VenueLock.sol"; import {HyperCoreAdapter} from "./HyperCoreAdapter.sol"; /// @title HlAgentAdapter — `hl_agent()` and `hl_agent_revoke()`: CoreWriter action 9. /// /// docs/HYPERLIQUID_VENUE_PLAN.md §6, §7.3 and §7.6a items 1-2. /// /// `hl_agent` registers G, the venue enclave's agent key, as V's API wallet in /// the unnamed slot: action 9 `(G, "")`. G IS READ FROM THE OWNER'S LOCK and /// from nowhere else: the intent carries no address at all. VenueLock binds G /// only with A's signature AND the venue enclave's receipt (V5), so neither /// the keeper nor a compromised prod can get its own key registered. /// /// `hl_agent_revoke` is action 9 `(address(0), "")`, which removes the agent /// in the unnamed slot (U2). It is an EXIT: it never reads the lock, so it /// works with no lock, with a lock set by an owner V no longer has, and with /// a lock contract that reverts on V (`lockOf` can, for an owner it cannot /// read). /// /// A KEY IS REGISTERED ONCE (audit 2026-10-25, findings 1 and 2). Before /// action 9, V calls `venueLock.useKey(HYPERLIQUID, G)` with calldata written /// here. VenueLock refuses a key it has registered for (V, hyperliquid) /// before, ever, whichever adapter instance asked, and a closed lock (zero cap /// or no products). Once G is revoked or pruned (H10), Hyperliquid may let /// actions G signed in the last days replay against V if G were approved /// again, so G is never approved again: re-registering after a revoke or a /// prune, or re-registering a live G to restart its 90 days, all refuse. The /// way back is a re-arm: A signs a `set` binding a NEW G, with the enclave's /// receipt (HL5, "rotated, never reused"). And an owner who tightened the /// lock to zero cap or no products has closed it to new agents as well. /// /// EXPIRY. Action 9 has no expiry field; the agent lives exactly 90 days from /// execution (U1). `AgentRegistered` carries the expected end so the renewal /// (a re-arm rotates G, HL5) can be scheduled from the log; the real value is /// `extraAgents` / `userRole` on `/info`. /// /// NO BALANCE MOVES, so there is no delta to measure. The effect is checked /// next block with `userRole(G)` (§7.3). /// /// READINGS TAKEN WHERE THE PLAN LEFT A GAP (most restrictive): /// - The lock must bind a `KEY_AGENT` key (VenueLock's `boundAddress` also /// answers for an account wallet; Hyperliquid uses only agents). /// - G may not be V itself. contract HlAgentAdapter is HyperCoreAdapter { bytes32 public constant OP_AGENT = "hl_agent"; bytes32 public constant OP_AGENT_REVOKE = "hl_agent_revoke"; /// What an action-9 agent gets (U1). uint256 public constant AGENT_LIFETIME = 90 days; bytes32 internal constant REGISTER_TYPEHASH = keccak256( "HlAgentRegister(address account,address owner,bytes32 taskId,uint256 deadline)" ); bytes32 internal constant REVOKE_TYPEHASH = keccak256( "HlAgentRevoke(address account,address owner,bytes32 taskId,uint256 deadline)" ); /// Both steps carry the binding and nothing else: no agent, no name. /// Different typehashes, so a signature for one is not one for the other. struct Step { address account; address owner; bytes32 taskId; uint256 deadline; } /// Read on `hl_agent`, and called BY V there for one function only, /// `useKey`, with calldata written here. Never touched on the revoke. VenueLock public immutable venueLock; event AgentRegistered( address indexed account, bytes32 indexed taskId, address indexed agent, uint256 expectedUntil ); event AgentRevoked(address indexed account, bytes32 indexed taskId); error BadVenueLock(address venueLock); error NoHyperliquidLock(address account); error LockIsNotAnAgentLock(address account, uint8 kind); error AgentIsTheAccount(address account); constructor(AgentRegistry registry_, VenueLock venueLock_) HyperCoreAdapter(registry_) { address l = address(venueLock_); if (l.code.length == 0 || l == CORE_WRITER) revert BadVenueLock(l); venueLock = venueLock_; } function _domainNameAndVersion() internal pure override returns (string memory, string memory) { return ("DeFiLoopsHlAgentAdapter", "1"); } function registerDigest(Step calldata s) public view returns (bytes32) { return _hashTypedData( keccak256(abi.encode(REGISTER_TYPEHASH, s.account, s.owner, s.taskId, s.deadline)) ); } function revokeDigest(Step calldata s) public view returns (bytes32) { return _hashTypedData( keccak256(abi.encode(REVOKE_TYPEHASH, s.account, s.owner, s.taskId, s.deadline)) ); } /// `hl_agent()`. function registerAgent(Step calldata s, bytes calldata signature) external { _consume(s.account, s.owner, s.taskId, s.deadline, registerDigest(s), signature); _requireOperation(s.account, OP_AGENT); VenueLock.Lock memory lock = venueLock.lockOf(s.account, HYPERLIQUID); if (lock.owner == address(0)) revert NoHyperliquidLock(s.account); if (lock.binding.kind != LOCK_KEY_AGENT) { revert LockIsNotAnAgentLock(s.account, lock.binding.kind); } // VenueLock refuses a non-address key and a zero key for KEY_AGENT. address agent = address(uint160(uint256(lock.binding.key))); if (agent == s.account) revert AgentIsTheAccount(s.account); // The shared record: refuses a closed lock and a key ever registered // for (V, hyperliquid) before. Before any action is queued. _exec( s.account, address(venueLock), 0, abi.encodeCall(VenueLock.useKey, (HYPERLIQUID, lock.binding.key)) ); _actSetAgent(s.account, s.taskId, agent); emit AgentRegistered(s.account, s.taskId, agent, block.timestamp + AGENT_LIFETIME); } /// `hl_agent_revoke()`. An exit: no lock is read. function revokeAgent(Step calldata s, bytes calldata signature) external { _consume(s.account, s.owner, s.taskId, s.deadline, revokeDigest(s), signature); _requireOperation(s.account, OP_AGENT_REVOKE); _actSetAgent(s.account, s.taskId, address(0)); emit AgentRevoked(s.account, s.taskId); } }