// SPDX-License-Identifier: MIT pragma solidity 0.8.28; /// @title Sanario Multisig /// @notice An M-of-N smart-contract wallet for company treasuries. /// Owners approve a transaction off-chain by signing an EIP-712 /// message (from MetaMask, Rabby, a Ledger, a Trezor, ...), or on-chain /// with `approveHash` (for owners that are themselves smart contracts). /// Once `threshold` distinct owners have approved, anyone can submit /// the transaction and pay the gas. No single key can move funds. /// @dev Owner management (add / remove / replace owner, change threshold) /// can only be called by the wallet itself, so it needs the same /// M-of-N approval as any payment. contract SanarioMultisig { // ---------------------------------------------------------------- types string public constant NAME = "Sanario Multisig"; string public constant VERSION = "1"; bytes32 private constant DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"); bytes32 public constant TRANSACTION_TYPEHASH = keccak256("Transaction(address to,uint256 value,bytes data,uint256 nonce)"); /// secp256k1n / 2, used to reject malleable signatures (EIP-2). uint256 private constant HALF_N = 0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0; uint256 public constant MAX_OWNERS = 50; // ---------------------------------------------------------------- state address[] private _owners; mapping(address => bool) public isOwner; uint256 public threshold; uint256 public nonce; /// owner => tx hash => approved on-chain mapping(address => mapping(bytes32 => bool)) public approvedHashes; uint256 private _locked = 1; // --------------------------------------------------------------- events event Deposit(address indexed from, uint256 value); event Executed(bytes32 indexed txHash, uint256 indexed nonce, address indexed to, uint256 value, bool success, bytes returnData); event HashApproved(bytes32 indexed txHash, address indexed owner); event OwnerAdded(address indexed owner); event OwnerRemoved(address indexed owner); event ThresholdChanged(uint256 threshold); // --------------------------------------------------------------- errors error NotWallet(); error NotOwner(); error InvalidOwner(address owner); error InvalidThreshold(uint256 threshold, uint256 owners); error TooManyOwners(); error Reentrancy(); error SignerOrder(); error InvalidSignature(uint256 index); error NotEnoughApprovals(uint256 have, uint256 need); error LengthMismatch(); modifier onlyWallet() { if (msg.sender != address(this)) revert NotWallet(); _; } modifier nonReentrant() { if (_locked != 1) revert Reentrancy(); _locked = 2; _; _locked = 1; } constructor(address[] memory owners_, uint256 threshold_) { uint256 n = owners_.length; if (n > MAX_OWNERS) revert TooManyOwners(); for (uint256 i = 0; i < n; i++) _addOwner(owners_[i]); _setThreshold(threshold_); } receive() external payable { emit Deposit(msg.sender, msg.value); } // ---------------------------------------------------------------- views function getOwners() external view returns (address[] memory) { return _owners; } function domainSeparator() public view returns (bytes32) { return keccak256( abi.encode(DOMAIN_TYPEHASH, keccak256(bytes(NAME)), keccak256(bytes(VERSION)), block.chainid, address(this)) ); } /// @notice The EIP-712 digest owners sign for a transaction. function getTransactionHash(address to, uint256 value, bytes calldata data, uint256 txNonce) public view returns (bytes32) { bytes32 structHash = keccak256(abi.encode(TRANSACTION_TYPEHASH, to, value, keccak256(data), txNonce)); return keccak256(abi.encodePacked("\x19\x01", domainSeparator(), structHash)); } // ------------------------------------------------------------ execution /// @notice Execute a transaction approved by at least `threshold` owners. /// @param signers approving owners, strictly ascending by address /// @param signatures 65-byte EIP-712 signatures (r,s,v) aligned with /// `signers`; pass empty bytes for an owner who approved /// on-chain via `approveHash` or who is `msg.sender`. function execute( address to, uint256 value, bytes calldata data, address[] calldata signers, bytes[] calldata signatures ) external nonReentrant returns (bytes memory returnData) { if (signers.length != signatures.length) revert LengthMismatch(); uint256 txNonce = nonce; bytes32 txHash = getTransactionHash(to, value, data, txNonce); _checkApprovals(txHash, signers, signatures); nonce = txNonce + 1; bool success; (success, returnData) = to.call{value: value}(data); emit Executed(txHash, txNonce, to, value, success, returnData); if (!success) { // bubble up the revert reason; the nonce bump is rolled back too assembly { revert(add(returnData, 32), mload(returnData)) } } } /// @notice Approve a transaction hash on-chain (alternative to signing). function approveHash(bytes32 txHash) external { if (!isOwner[msg.sender]) revert NotOwner(); approvedHashes[msg.sender][txHash] = true; emit HashApproved(txHash, msg.sender); } function _checkApprovals(bytes32 txHash, address[] calldata signers, bytes[] calldata signatures) internal view { uint256 need = threshold; if (signers.length < need) revert NotEnoughApprovals(signers.length, need); address last = address(0); for (uint256 i = 0; i < signers.length; i++) { address signer = signers[i]; if (signer <= last) revert SignerOrder(); // ascending => no duplicates last = signer; if (!isOwner[signer]) revert InvalidOwner(signer); bytes calldata sig = signatures[i]; if (sig.length == 0) { if (signer != msg.sender && !approvedHashes[signer][txHash]) revert InvalidSignature(i); } else { if (_recover(txHash, sig) != signer) revert InvalidSignature(i); } } } function _recover(bytes32 digest, bytes calldata sig) private pure returns (address) { if (sig.length != 65) return address(0); bytes32 r = bytes32(sig[0:32]); bytes32 s = bytes32(sig[32:64]); uint8 v = uint8(sig[64]); if (v < 27) v += 27; if (v != 27 && v != 28) return address(0); if (uint256(s) > HALF_N) return address(0); return ecrecover(digest, v, r, s); } // -------------------------------------------------- owner management function addOwner(address owner, uint256 newThreshold) external onlyWallet { if (_owners.length >= MAX_OWNERS) revert TooManyOwners(); _addOwner(owner); _setThreshold(newThreshold); } function removeOwner(address owner, uint256 newThreshold) external onlyWallet { _removeOwner(owner); _setThreshold(newThreshold); } function replaceOwner(address oldOwner, address newOwner) external onlyWallet { _removeOwner(oldOwner); _addOwner(newOwner); } function changeThreshold(uint256 newThreshold) external onlyWallet { _setThreshold(newThreshold); } function _addOwner(address owner) private { if (owner == address(0) || owner == address(this) || isOwner[owner]) revert InvalidOwner(owner); isOwner[owner] = true; _owners.push(owner); emit OwnerAdded(owner); } function _removeOwner(address owner) private { if (!isOwner[owner]) revert InvalidOwner(owner); isOwner[owner] = false; uint256 n = _owners.length; for (uint256 i = 0; i < n; i++) { if (_owners[i] == owner) { _owners[i] = _owners[n - 1]; _owners.pop(); break; } } emit OwnerRemoved(owner); } function _setThreshold(uint256 t) private { if (t == 0 || t > _owners.length) revert InvalidThreshold(t, _owners.length); threshold = t; emit ThresholdChanged(t); } // ------------------------------------------------- token callbacks function onERC721Received(address, address, uint256, bytes calldata) external pure returns (bytes4) { return 0x150b7a02; } function onERC1155Received(address, address, uint256, uint256, bytes calldata) external pure returns (bytes4) { return 0xf23a6e61; } function onERC1155BatchReceived(address, address, uint256[] calldata, uint256[] calldata, bytes calldata) external pure returns (bytes4) { return 0xbc197c81; } function supportsInterface(bytes4 id) external pure returns (bool) { return id == 0x01ffc9a7 || id == 0x150b7a02 || id == 0x4e2312e0; } }