// SPDX-License-Identifier: MIT pragma solidity 0.8.24; import {QuantumVault} from "./QuantumVault.sol"; /// @title QuantumVaultFactory: deterministic vault addresses. /// @notice A vault's address is fixed by its starting configuration (daily key, vault key root, seed, height, limit, /// salt), so it can receive funds before it is deployed, and nobody can deploy different settings at it. /// Each vault is a minimal proxy (EIP-1167) of one immutable implementation; there is no admin and no upgrade. contract QuantumVaultFactory { address public immutable implementation; event VaultCreated(address indexed vault, address indexed owner, bytes32 root, uint8 height); constructor() { implementation = address(new QuantumVault()); } function create(address owner, bytes32 root, bytes32 pubSeed, uint8 height, uint256 ethLimit, bytes32 salt) external returns (address vault) { bytes32 s = _salt(owner, root, pubSeed, height, ethLimit, salt); vault = _predict(s); if (vault.code.length != 0) return vault; bytes memory code = _initCode(); assembly ("memory-safe") { vault := create2(0, add(code, 32), mload(code), s) } require(vault != address(0), "create2"); QuantumVault(payable(vault)).initialize(owner, root, pubSeed, height, ethLimit); emit VaultCreated(vault, owner, root, height); } function predict(address owner, bytes32 root, bytes32 pubSeed, uint8 height, uint256 ethLimit, bytes32 salt) external view returns (address) { return _predict(_salt(owner, root, pubSeed, height, ethLimit, salt)); } function _salt(address owner, bytes32 root, bytes32 pubSeed, uint8 height, uint256 ethLimit, bytes32 salt) private pure returns (bytes32) { return keccak256(abi.encode(owner, root, pubSeed, height, ethLimit, salt)); } function _initCode() private view returns (bytes memory) { return abi.encodePacked(hex"3d602d80600a3d3981f3363d3d373d3d3d363d73", implementation, hex"5af43d82803e903d91602b57fd5bf3"); } function _predict(bytes32 s) private view returns (address) { return address(uint160(uint256(keccak256(abi.encodePacked(bytes1(0xff), address(this), s, keccak256(_initCode())))))); } }