Skip to content
Verified contractExactMIT

WalletLib

Address0x5f1469e20ed5b6b96891cdfa0902d8e412951ed2
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.30

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,379,502
Creation transaction
0xb615f20053…dbdcdd5261

src/libraries/WalletLib.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

import {IWallet} from "src/interfaces/IWallet.sol";
import {WalletTypes} from "src/types/WalletTypes.sol";

/**
 * @title WalletLib
 * @notice Linked external library for multisig wallet transaction logic.
 * @dev Extracted to keep `Chamber` implementation under the EIP-170 size limit.
 */
library WalletLib {
    /// @dev Wallet calls may not spend approvals third parties gave the chamber (R-1012-M3).
    ///      Depositors approve the chamber on the vault asset, so calls to `asset` are limited to
    ///      `transfer` and `approve` (plain value sends with empty data are allowed). A selector
    ///      denylist cannot cover every pull alias (DAI `move` / `pull`, ERC-1363
    ///      `transferFromAndCall`), so it is only the fallback for other targets: there, ERC-20/721
    ///      `transferFrom`, ERC-721/1155 `safeTransferFrom`, ERC-1155 `safeBatchTransferFrom`,
    ///      `burnFrom`, DAI `move` / `pull`, ERC-1363 `transferFromAndCall` and ERC-777
    ///      `operatorSend` must name the chamber as `from` (first argument). Runs in the chamber's
    ///      context, so `address(this)` is the chamber.
    function requireNoThirdPartyPull(address target, address asset, bytes memory data) external view {
        if (target == asset) {
            if (data.length == 0) return;
            if (data.length >= 4) {
                bytes4 s = bytes4(data);
                if (s == 0xa9059cbb || s == 0x095ea7b3) return;
            }
            revert IWallet.InvalidTarget();
        }
        if (data.length < 36) return;
        bytes4 selector = bytes4(data);
        if (
            selector != 0x23b872dd && selector != 0x42842e0e && selector != 0xb88d4fde && selector != 0xf242432a
                && selector != 0x2eb2c2d6 && selector != 0x79cc6790 && selector != 0xbb35783b && selector != 0xf2d5d56b
                && selector != 0xd8fbe994 && selector != 0xc1d34b89 && selector != 0x62ad1b83
        ) return;
        uint256 from;
        assembly {
            from := mload(add(data, 36))
        }
        if (from != uint256(uint160(address(this)))) revert IWallet.InvalidTarget();
    }

    using WalletTypes for WalletTypes.WalletStorage;

    function txExists(WalletTypes.WalletStorage storage $, uint256 nonce) internal view {
        if (nonce >= $.transactions.length) revert IWallet.TransactionDoesNotExist();
    }

    function notExecuted(WalletTypes.WalletStorage storage $, uint256 nonce) internal view {
        if ($.transactions[nonce].executed) revert IWallet.TransactionAlreadyExecuted();
    }

    function notCancelled(WalletTypes.WalletStorage storage $, uint256 nonce) internal view {
        if ($.cancelled[nonce]) revert IWallet.TransactionAlreadyCancelled();
    }

    function notExpired(WalletTypes.WalletStorage storage $, uint256 nonce) internal view {
        uint256 deadline = $.transactionDeadline[nonce];
        if (deadline != 0 && block.timestamp > deadline) revert IWallet.TransactionExpired();
    }

    function notConfirmed(WalletTypes.WalletStorage storage $, uint256 tokenId, uint256 nonce) internal view {
        if ($.isConfirmed[nonce][tokenId]) revert IWallet.TransactionAlreadyConfirmed();
    }

    function resolveDeadline(uint256 deadline) internal view returns (uint256 resolved) {
        uint256 maxDeadline = block.timestamp + WalletTypes.DEFAULT_TRANSACTION_MAX_AGE;
        if (deadline == 0) {
            return maxDeadline;
        }
        if (deadline <= block.timestamp || deadline > maxDeadline) {
            revert IWallet.InvalidDeadline();
        }
        return deadline;
    }

    function submitTransactionWithMetadata(
        WalletTypes.WalletStorage storage $,
        uint256 tokenId,
        address target,
        uint256 value,
        bytes memory data,
        string memory metadataURI,
        uint256 deadline,
        uint256 submitQuorum,
        bool storeCalldata
    ) external {
        uint256 nonce = $.transactions.length;
        uint256 resolvedDeadline = resolveDeadline(deadline);

        bytes32 dataHash = keccak256(data);
        $.transactions
            .push(
                WalletTypes.Transaction({
                    target: target, value: value, dataHash: dataHash, executed: false, confirmations: 0
                })
            );
        if (storeCalldata) {
            $.transactionCalldata[nonce] = data;
        }
        $.transactionRequiredQuorum[nonce] = submitQuorum;
        $.transactionDeadline[nonce] = resolvedDeadline;
        emit IWallet.TransactionDeadlineSet(nonce, resolvedDeadline);
        if (bytes(metadataURI).length != 0) {
            $.transactionMetadataURI[nonce] = metadataURI;
            emit IWallet.ProposalMetadataSet(nonce, metadataURI);
        }
        confirmTransaction($, tokenId, nonce);
        emit IWallet.SubmitTransaction(tokenId, nonce, target, value, data);
    }

    function confirmTransaction(WalletTypes.WalletStorage storage $, uint256 tokenId, uint256 nonce) internal {
        txExists($, nonce);
        notExecuted($, nonce);
        notCancelled($, nonce);
        notExpired($, nonce);
        notConfirmed($, tokenId, nonce);

        WalletTypes.Transaction storage transaction = $.transactions[nonce];
        transaction.confirmations += 1;
        $.isConfirmed[nonce][tokenId] = true;

        emit IWallet.ConfirmTransaction(tokenId, nonce);
    }

    function revokeConfirmation(WalletTypes.WalletStorage storage $, uint256 tokenId, uint256 nonce) external {
        txExists($, nonce);
        notExecuted($, nonce);
        notCancelled($, nonce);
        notExpired($, nonce);

        if (!$.isConfirmed[nonce][tokenId]) revert IWallet.TransactionNotConfirmed();

        WalletTypes.Transaction storage transaction = $.transactions[nonce];

        if (transaction.confirmations > 0) {
            unchecked {
                transaction.confirmations -= 1;
            }
        }
        $.isConfirmed[nonce][tokenId] = false;

        emit IWallet.RevokeConfirmation(tokenId, nonce);
    }

    function recordCancelVote(WalletTypes.WalletStorage storage $, uint256 tokenId, uint256 nonce) external {
        txExists($, nonce);
        notExecuted($, nonce);

        if ($.cancelled[nonce]) revert IWallet.TransactionAlreadyCancelled();
        if ($.isCancelConfirmed[nonce][tokenId]) revert IWallet.TransactionCancelAlreadyConfirmed();

        $.isCancelConfirmed[nonce][tokenId] = true;
        $.cancelConfirmations[nonce] += 1;

        emit IWallet.CancelTransaction(tokenId, nonce);
    }

    function cancelTransaction(WalletTypes.WalletStorage storage $, uint256 nonce) external {
        $.cancelled[nonce] = true;
        emit IWallet.TransactionCancelled(nonce);
    }

    function executeTransaction(
        WalletTypes.WalletStorage storage $,
        uint256 tokenId,
        uint256 nonce,
        bytes calldata data,
        address selfAddress
    ) external {
        txExists($, nonce);
        notExecuted($, nonce);
        notCancelled($, nonce);
        notExpired($, nonce);

        WalletTypes.Transaction storage transaction = $.transactions[nonce];

        if (transaction.target == address(0)) revert IWallet.InvalidTarget();

        bytes memory payload = resolveExecutionCalldata($, nonce, transaction, data, selfAddress);

        address target = transaction.target;
        uint256 value = transaction.value;

        transaction.executed = true;

        (bool success, bytes memory returnData) = target.call{value: value}(payload);
        if (!success) {
            transaction.executed = false;
            revert IWallet.TransactionFailed(returnData);
        }

        emit IWallet.ExecuteTransaction(tokenId, nonce);
    }

    function resolveExecutionCalldata(
        WalletTypes.WalletStorage storage $,
        uint256 nonce,
        WalletTypes.Transaction storage transaction,
        bytes calldata data,
        address selfAddress
    ) internal view returns (bytes memory payload) {
        bytes memory stored = $.transactionCalldata[nonce];
        if (stored.length != 0 || transaction.target == selfAddress) {
            payload = stored;
            if (data.length != 0 && keccak256(data) != transaction.dataHash) revert IWallet.DataHashMismatch();
        } else {
            payload = data;
        }
        if (keccak256(payload) != transaction.dataHash) revert IWallet.DataHashMismatch();
    }
}

src/interfaces/IWallet.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

/**
 * @title IWallet
 * @author xhad, Loreum DAO LLC
 * @notice Director-gated multisig: submit, confirm, execute, revoke, and cancel transaction flows.
 * @dev Ordinary calls store only `keccak256(calldata)`; callers must retain full calldata to execute.
 *      Self-calls (`target == address(this)`) and, on Chamber, calls to its ProxyAdmin also persist the
 *      original bytes onchain so a confirmed upgrade remains executable if logs are unavailable.
 *      Confirmation and execution require a quorum of distinct director token IDs as enforced by `Chamber`.
 *      That quorum is token-weighted: one address that holds `quorum` top-seat membership NFTs
 *      can submit, self-confirm, and execute (a single-actor treasury). Confirmations are not
 *      capped per owner.
 *      Submit records a deadline (default 30 days, or an explicit future timestamp up to that max).
 *      After a non-zero deadline, confirm/revoke/execute revert with `TransactionExpired`.
 *      Stored deadline `0` is unset (pre-upgrade pending) and is not expired.
 */
interface IWallet {
    /**
     * @notice Submits a new transaction for approval. The submitting token's confirmation is recorded.
     * @param tokenId The tokenId submitting the transaction
     * @param target The address to send the transaction to
     * @param value The amount of Ether to send
     * @param data The calldata (hash stored always; full bytes stored when `target` is this wallet or, on a Chamber, its ProxyAdmin)
     * @param metadataURI URI or content hash describing the proposal; empty stores nothing
     * @param deadline Exclusive-after unix timestamp; `0` applies the 30-day default max age
     */
    function submitTransaction(
        uint256 tokenId,
        address target,
        uint256 value,
        bytes memory data,
        string memory metadataURI,
        uint256 deadline
    ) external;

    /**
     * @notice Adds one confirmation for `transactionId` from director `tokenId`.
     * @dev Reverts if the transaction is cancelled, already executed, or already confirmed by this `tokenId`.
     */
    function confirmTransaction(uint256 tokenId, uint256 transactionId) external;

    /**
     * @notice Executes a transaction if current directors still meet the required quorum
     * @dev Caller must supply the original calldata unless it was stored onchain for a self-call
     *      (Chamber: also calls to its ProxyAdmin, i.e. upgrades). Empty `data` then uses the stored bytes.
     *      The contract always verifies keccak256(payload) == stored hash.
     *      Chamber counts only tokenIds still in the top-seat set, against
     *      `max(submitQuorum, liveQuorum)`. Deadline `0` is unset and is not expired.
     * @param tokenId The tokenId executing the transaction
     * @param transactionId The ID of the transaction to execute
     * @param data The original calldata, or empty to use stored self-call bytes
     */
    function executeTransaction(uint256 tokenId, uint256 transactionId, bytes calldata data) external;

    /**
     * @notice Revokes a confirmation for a transaction
     * @dev Chamber allows the token owner to revoke after leaving
     *      the board (no ERC-1271; see director-authorization.md).
     * @param tokenId The tokenId revoking the confirmation
     * @param transactionId The ID of the transaction to revoke confirmation for
     */
    function revokeConfirmation(uint256 tokenId, uint256 transactionId) external;

    /**
     * @notice Records a director's vote to cancel a transaction. Requires quorum of directors to cancel.
     * @param tokenId The tokenId voting to cancel
     * @param transactionId The ID of the transaction to cancel
     */
    function cancelTransaction(uint256 tokenId, uint256 transactionId) external;

    /**
     * @notice Submits multiple transactions for approval in a single call
     * @param tokenId The tokenId submitting the transactions
     * @param targets The array of addresses to send the transactions to
     * @param values The array of amounts of Ether to send
     * @param data The array of calldata (hash stored always; full bytes stored for self-calls)
     */
    function submitBatchTransactions(
        uint256 tokenId,
        address[] memory targets,
        uint256[] memory values,
        bytes[] memory data
    ) external;

    /**
     * @notice Confirms multiple transactions in a single call
     * @param tokenId The tokenId confirming the transactions
     * @param transactionIds The array of transaction IDs to confirm
     */
    function confirmBatchTransactions(uint256 tokenId, uint256[] memory transactionIds) external;

    /**
     * @notice Executes multiple transactions in a single call if they have enough current-director confirmations
     * @dev Caller must supply the original calldata for each hash-only transaction, in the same
     *      order as transactionIds. Self-call entries may be empty and use the onchain store.
     *      Each nonce recounts live top-seat flags against `max(submitQuorum, liveQuorum)`.
     * @param tokenId The tokenId executing the transactions
     * @param transactionIds The array of transaction IDs to execute
     * @param data The array of original calldata for each transaction (empty allowed for stored self-calls)
     */
    function executeBatchTransactions(uint256 tokenId, uint256[] memory transactionIds, bytes[] calldata data) external;

    /**
     * @notice Returns the total number of transactions
     * @return The total number of transactions
     */
    function getTransactionCount() external view returns (uint256);

    /**
     * @notice Returns the details of a specific transaction
     * @param nonce The index of the transaction to retrieve
     * @return executed Whether the transaction has been executed
     * @return confirmations Number of confirmations
     * @return target The target address
     * @return value The ETH value
     * @return dataHash keccak256 of the original calldata (re-supply at execution unless stored)
     */
    function getTransaction(uint256 nonce)
        external
        view
        returns (bool executed, uint8 confirmations, address target, uint256 value, bytes32 dataHash);

    /**
     * @notice Returns durable metadata URI or content hash for a transaction proposal
     * @param nonce The index of the transaction to retrieve metadata for
     * @return metadataURI URI or content hash supplied at proposal creation
     */
    function getTransactionMetadata(uint256 nonce) external view returns (string memory metadataURI);

    /**
     * @notice Returns onchain-stored calldata for a self-call / upgrade, or empty bytes for hash-only txs
     * @param nonce The index of the transaction to retrieve stored calldata for
     * @return storedCalldata Original bytes if this nonce targeted the wallet itself; otherwise empty
     */
    function getTransactionCalldata(uint256 nonce) external view returns (bytes memory storedCalldata);

    /**
     * @notice Returns the quorum snapshotted when the transaction was submitted
     * @param nonce The index of the transaction to retrieve
     * @return requiredQuorum Quorum recorded at submit time (0 if unset / legacy)
     */
    function getTransactionRequiredQuorum(uint256 nonce) external view returns (uint256 requiredQuorum);

    /**
     * @notice Checks if a transaction is confirmed by a specific director
     * @param tokenId The tokenId of the director to check confirmation for
     * @param nonce The index of the transaction to check
     * @return True if the transaction is confirmed by the director, false otherwise
     */
    function getConfirmation(uint256 tokenId, uint256 nonce) external view returns (bool);

    /**
     * @notice Returns whether a transaction has been cancelled
     * @param nonce The index of the transaction to check
     * @return True if the transaction is cancelled
     */
    function getCancelled(uint256 nonce) external view returns (bool);

    /**
     * @notice Returns the exclusive-after unix timestamp after which the transaction cannot be executed
     * @param nonce The index of the transaction to retrieve
     * @return deadline Stored deadline timestamp; `0` means unset / no expiry
     */
    function getTransactionDeadline(uint256 nonce) external view returns (uint256 deadline);

    /**
     * @notice Checks if a director has voted to cancel a transaction
     * @param tokenId The tokenId of the director to check
     * @param nonce The index of the transaction to check
     * @return True if the director has voted to cancel
     */
    function getCancelConfirmation(uint256 tokenId, uint256 nonce) external view returns (bool);

    /**
     * @notice Returns the number of directors who have voted to cancel a transaction
     * @param nonce The index of the transaction to check
     * @return The count of cancel votes
     */
    function getCancelConfirmations(uint256 nonce) external view returns (uint8);

    /// Events
    /**
     * @notice Emitted when a new transaction is submitted
     * @param tokenId The tokenId submitting the transaction
     * @param nonce The unique identifier for the transaction
     * @param to The target address for the transaction
     * @param value The amount of ETH to send
     * @param data The original calldata (emitted for offchain persistence; also stored onchain for self-calls)
     */
    event SubmitTransaction(
        uint256 indexed tokenId, uint256 indexed nonce, address indexed to, uint256 value, bytes data
    );

    /**
     * @notice Emitted when proposal metadata is attached to a transaction
     * @param nonce The transaction identifier
     * @param metadataURI URI or content hash describing the proposal
     */
    event ProposalMetadataSet(uint256 indexed nonce, string metadataURI);

    /**
     * @notice Emitted when a transaction is confirmed by a director
     * @param tokenId The token ID of the director confirming
     * @param nonce The identifier of the confirmed transaction
     */
    event ConfirmTransaction(uint256 indexed tokenId, uint256 indexed nonce);

    /**
     * @notice Emitted when a confirmation is revoked by a director
     * @param tokenId The token ID of the director revoking confirmation
     * @param nonce The identifier of the transaction
     */
    event RevokeConfirmation(uint256 indexed tokenId, uint256 indexed nonce);

    /**
     * @notice Emitted when a transaction is executed
     * @param tokenId The token ID of the director executing the transaction
     * @param nonce The identifier of the executed transaction
     */
    event ExecuteTransaction(uint256 indexed tokenId, uint256 indexed nonce);

    /**
     * @notice Emitted when a director votes to cancel a transaction
     * @param tokenId The token ID of the director voting to cancel
     * @param nonce The identifier of the transaction
     */
    event CancelTransaction(uint256 indexed tokenId, uint256 indexed nonce);

    /**
     * @notice Emitted when a transaction is cancelled (quorum of cancel votes reached)
     * @param nonce The identifier of the cancelled transaction
     */
    event TransactionCancelled(uint256 indexed nonce);

    /**
     * @notice Emitted when a transaction deadline is recorded at submit time
     * @param nonce The identifier of the transaction
     * @param deadline Exclusive-after unix timestamp after which execute/confirm/revoke revert (never `0`)
     */
    event TransactionDeadlineSet(uint256 indexed nonce, uint256 deadline);

    /// Errors
    /// @notice Thrown when a transaction does not exist
    error TransactionDoesNotExist();

    /// @notice Thrown when a transaction has already been executed
    error TransactionAlreadyExecuted();

    /// @notice Thrown when a transaction has already been confirmed by the same tokenId
    error TransactionAlreadyConfirmed();

    /// @notice Thrown when trying to revoke a confirmation that doesn't exist
    error TransactionNotConfirmed();

    /// @notice Thrown when a transaction has already been cancelled
    error TransactionAlreadyCancelled();

    /// @notice Thrown when a transaction's deadline has passed
    error TransactionExpired();

    /// @notice Thrown when a submit deadline is not in the future or exceeds the 30-day max age
    error InvalidDeadline();

    /// @notice Thrown when a director has already voted to cancel
    error TransactionCancelAlreadyConfirmed();

    /// @notice Thrown when a transaction execution fails
    /// @param reason The reason for the failure
    error TransactionFailed(bytes reason);

    /// @notice Thrown when transaction target is invalid
    error InvalidTarget();

    /// @notice Thrown when the supplied calldata does not match the stored keccak256 hash
    error DataHashMismatch();
}

src/types/WalletTypes.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

/**
 * @title WalletTypes
 * @author xhad, Loreum DAO LLC
 * @notice Shared struct matching on-chain `Wallet` storage layouts.
 * @dev Uses `bytes32 dataHash` (keccak256 of calldata), not inline `bytes data`.
 */
library WalletTypes {
    /**
     * @notice Structure representing a transaction in the wallet
     */
    struct Transaction {
        bool executed;
        uint8 confirmations;
        address target;
        uint256 value;
        bytes32 dataHash;
    }

    /**
     * @notice ERC-7201 namespaced storage layout for Wallet
     * @custom:storage-location erc7201:loreum.Wallet
     */
    struct WalletStorage {
        Transaction[] transactions;
        mapping(uint256 nonce => string metadataURI) transactionMetadataURI;
        mapping(uint256 nonce => mapping(uint256 tokenId => bool)) isConfirmed;
        mapping(uint256 nonce => bool) cancelled;
        mapping(uint256 nonce => uint8) cancelConfirmations;
        mapping(uint256 nonce => mapping(uint256 tokenId => bool)) isCancelConfirmed;
        mapping(uint256 nonce => bytes storedCalldata) transactionCalldata;
        mapping(uint256 nonce => uint256 requiredQuorum) transactionRequiredQuorum;
        mapping(uint256 nonce => uint256 deadline) transactionDeadline;
    }

    /// @dev keccak256(abi.encode(uint256(keccak256("erc7201:loreum.Wallet")) - 1)) & ~bytes32(uint256(0xff))
    bytes32 internal constant STORAGE_SLOT = 0x471e5819b63496fc9e7b0c9d30efc265f73588bc9e02c472310feaa7f9bb8000;

    /// @notice Default lifetime for a submitted transaction when no deadline is provided
    uint256 internal constant DEFAULT_TRANSACTION_MAX_AGE = 30 days;
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "ds-test/=lib/openzeppelin-contracts-upgradeable/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts-upgradeable/lib/erc4626-tests/",
    "forge-std/=lib/forge-std/src/",
    "halmos-cheatcodes/=lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.