// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; /* KEYFALL VAULT — Elysium (Hyperliquid L2) Withdrawals are authorised by a Lamport one-time signature (hash-based, built only on keccak256). Hash-based signatures are believed to resist quantum attacks (Shor's algorithm does not apply to them). Each vault stores only the keccak hash of its current Lamport public key. Every withdrawal reveals one signature, so the same call MUST commit to the NEXT public key hash. A key is never used twice. Anyone may submit a withdrawal transaction (any wallet can pay gas): the signature binds chain id, this contract, vault id, nonce, recipient, amount and the next key, so a relayer cannot change any of them. */ interface IERC20Balance { function balanceOf(address) external view returns (uint256); } contract KeyfallVault { struct Vault { bytes32 pkHash; // keccak256 of the current 512-hash Lamport public key uint64 nonce; // number of keys already used uint256 balance; // ETH held (wei) } mapping(uint256 => Vault) public vaults; uint256 public vaultCount; address public owner; address public feeRecipient; uint16 public feeBps; // withdrawal fee, basis points (max 100 = 1%) uint16 public constant MAX_FEE_BPS = 100; address public holderToken; // $KEYFALL token (set after launch) uint256 public holderMin; // recipients holding >= this pay no fee bool private locked; event VaultOpened(uint256 indexed id, bytes32 pkHash, uint256 amount); event Deposited(uint256 indexed id, address indexed from, uint256 amount); event Withdrawn(uint256 indexed id, address indexed to, uint256 amount, uint256 fee, uint64 newNonce, bytes32 nextPkHash); event OwnerChanged(address owner); event FeeChanged(address recipient, uint16 bps); event HolderRuleChanged(address token, uint256 minBalance); modifier onlyOwner() { require(msg.sender == owner, "not owner"); _; } modifier nonReentrant() { require(!locked, "reentrant"); locked = true; _; locked = false; } constructor(address _feeRecipient, uint16 _feeBps) { require(_feeBps <= MAX_FEE_BPS, "fee too high"); owner = msg.sender; feeRecipient = _feeRecipient; feeBps = _feeBps; } // ------------------------------------------------------------------ users function open(bytes32 pkHash) external payable returns (uint256 id) { require(pkHash != bytes32(0), "empty key"); id = ++vaultCount; vaults[id] = Vault({pkHash: pkHash, nonce: 0, balance: msg.value}); emit VaultOpened(id, pkHash, msg.value); } function deposit(uint256 id) external payable { require(vaults[id].pkHash != bytes32(0), "no vault"); require(msg.value > 0, "zero"); vaults[id].balance += msg.value; emit Deposited(id, msg.sender, msg.value); } /// The 256-bit message a vault key signs for one withdrawal. function messageHash( uint256 id, uint64 nonce, address to, uint256 amount, bytes32 nextPkHash ) public view returns (bytes32) { return keccak256(abi.encode(block.chainid, address(this), id, nonce, to, amount, nextPkHash)); } /// @param sig 256 revealed secrets, one per message bit /// @param other 256 public-key halves for the side NOT revealed /// Use amount = 0 to only rotate the key. function withdraw( uint256 id, address payable to, uint256 amount, bytes32 nextPkHash, bytes32[256] calldata sig, bytes32[256] calldata other ) external nonReentrant { Vault storage v = vaults[id]; require(v.pkHash != bytes32(0), "no vault"); require(nextPkHash != bytes32(0) && nextPkHash != v.pkHash, "bad next key"); require(amount <= v.balance, "insufficient"); require(to != address(0) || amount == 0, "bad recipient"); bytes32 m = messageHash(id, v.nonce, to, amount, nextPkHash); require(_lamportKeyHash(m, sig, other) == v.pkHash, "bad signature"); // effects v.pkHash = nextPkHash; v.nonce += 1; v.balance -= amount; uint256 fee = 0; if (amount > 0 && feeBps > 0 && feeRecipient != address(0) && !_isHolder(to)) { fee = (amount * feeBps) / 10_000; } emit Withdrawn(id, to, amount, fee, v.nonce, nextPkHash); // interactions if (fee > 0) { (bool okFee, ) = feeRecipient.call{value: fee}(""); require(okFee, "fee transfer failed"); } if (amount - fee > 0) { (bool ok, ) = to.call{value: amount - fee}(""); require(ok, "transfer failed"); } } /// Rebuilds the public key hash from a signature. Public for off-chain checks. function lamportKeyHash( bytes32 m, bytes32[256] calldata sig, bytes32[256] calldata other ) external pure returns (bytes32) { return _lamportKeyHash(m, sig, other); } function _lamportKeyHash( bytes32 m, bytes32[256] calldata sig, bytes32[256] calldata other ) internal pure returns (bytes32) { bytes32[512] memory pk; uint256 bits = uint256(m); for (uint256 i = 0; i < 256; ++i) { bytes32 h = keccak256(abi.encodePacked(sig[i])); if ((bits >> (255 - i)) & 1 == 0) { pk[2 * i] = h; pk[2 * i + 1] = other[i]; } else { pk[2 * i] = other[i]; pk[2 * i + 1] = h; } } return keccak256(abi.encodePacked(pk)); } function _isHolder(address who) internal view returns (bool) { if (holderToken == address(0) || holderMin == 0) return false; try IERC20Balance(holderToken).balanceOf(who) returns (uint256 bal) { return bal >= holderMin; } catch { return false; } } // ------------------------------------------------------------------ admin // The owner can NOT touch vault funds. Only fee settings and the holder rule. function setFee(address recipient, uint16 bps) external onlyOwner { require(bps <= MAX_FEE_BPS, "fee too high"); feeRecipient = recipient; feeBps = bps; emit FeeChanged(recipient, bps); } function setHolderRule(address token, uint256 minBalance) external onlyOwner { holderToken = token; holderMin = minBalance; emit HolderRuleChanged(token, minBalance); } function transferOwnership(address newOwner) external onlyOwner { require(newOwner != address(0), "zero"); owner = newOwner; emit OwnerChanged(newOwner); } function totalLocked() external view returns (uint256) { return address(this).balance; } }