Skip to content
Verified contractExactMIT

Grails5SignatureGatedMintPassRedeemer

Address0x6fa6e4fbf76b84e478eeeede7e8b6273b1702c19
Balance

0 ETH

 

Transactions

176

 

Compiler

solc 0.8.21

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.21+commit.d9974bed
Language
solidity
Optimization
Yes, 9,999 runs
EVM version
paris
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
18,778,567
Creation transaction
0x25bdc4a01a…6568cc5853

90 files

The source code is large: one file is shown at a time. Choose a file in the list.

/home/dave/solidifylabs/solidifylabs/contracts/sellers/src/mechanics/SignatureGated.sol

// SPDX-License-Identifier: MIT
// Copyright 2023 PROOF Holdings Inc
pragma solidity ^0.8.16;

import {ECDSA} from "openzeppelin-contracts/utils/cryptography/ECDSA.sol";

import {Seller} from "../base/Seller.sol";
import {AccessControlled} from "../base/AccessControlled.sol";

library SignatureGatedLib {
    /**
     * @notice Message struct to encode allowances.
     * @param receiver The address to which the allowance is granted.
     * @param numMax The maximum number of purchases that can be made.
     * @param price The price per purchase.
     * @param activeAfterTimestamp The timestamp after which the allowance is active.
     * @param activeUntilTimestamp The timestamp after which the allowance is no longer active.
     * @dev The allowance is active in the timestamp interval [activeAfterTimestamp, activeUntilTimestamp).
     */
    struct Allowance {
        address receiver;
        uint64 numMax;
        uint256 price;
        uint256 activeAfterTimestamp;
        uint256 activeUntilTimestamp;
        uint256 nonce;
    }

    /**
     * @notice Encodes an approved allowance (i.e. signed by an authorised signer).
     */
    struct SignedAllowance {
        Allowance allowance;
        bytes signature;
    }

    /**
     * @notice Encodes the purchase data for the use in internal `Seller` hooks.
     */
    function encodePurchaseData(bytes32 digest_, SignedAllowance memory signedAllowance)
        internal
        pure
        returns (bytes memory)
    {
        return abi.encode(digest_, signedAllowance);
    }

    /**
     * @notice Inverse of `_encodePurchaseData`.
     */
    function decodePurchaseData(bytes memory data) internal pure returns (bytes32, SignedAllowance memory) {
        return abi.decode(data, (bytes32, SignedAllowance));
    }

    /**
     * @notice Computes the hash of a given allowance depending on address and chainId of the seller contract.
     */
    function digest(Allowance memory allowance, uint256 chainId, address target) internal pure returns (bytes32) {
        // We do not use EIP712 signatures here for the time being for simplicity (and since we will be the only ones
        // signing).
        return ECDSA.toEthSignedMessageHash(
            abi.encode(
                allowance,
                // Adding chain id and the verifying contract address to prevent
                // replay attacks.
                chainId,
                target
            )
        );
    }
}

/**
 * @notice Introduces claimability based on signed allowances.
 */
abstract contract SignatureGatedBase {
    // =================================================================================================================
    //                           Errors
    // =================================================================================================================

    /**
     * @notice Thrown if there are too many requests for a given allowance.
     */
    error TooManyPurchasesRequested(SignatureGatedLib.Allowance, uint256 numLeft, uint256 numRequested);

    /**
     * @notice Thrown if a given allowance is not active.
     */
    error InactiveAllowance(SignatureGatedLib.Allowance);

    /**
     * @notice Thrown if a the signer of an allowance is not authorised.
     */
    error UnauthorisedSigner(SignatureGatedLib.SignedAllowance, address recovered);

    // =================================================================================================================
    //                           Storage
    // =================================================================================================================

    /**
     * @notice Tracks how many purchases have been made with a signed allowance.
     */
    mapping(bytes32 => uint256) private _numPurchasesByAllowanceDigest;

    // =================================================================================================================
    //                           Purchasing
    // =================================================================================================================

    /**
     * @notice Computes the hash of a given allowance.
     * @dev This is the raw bytes32 message that will finally be signed by one of the authorised `_signers`.
     */
    function digest(SignatureGatedLib.Allowance memory allowance) public view returns (bytes32) {
        return SignatureGatedLib.digest(allowance, block.chainid, address(this));
    }

    /**
     * @notice Returns the number of purchases that have already been redeemed from a given allowance.
     */
    function numPurchasedWithAllowances(SignatureGatedLib.Allowance[] calldata allowances)
        external
        view
        returns (uint256[] memory)
    {
        uint256[] memory nums = new uint[](allowances.length);
        for (uint256 i; i < allowances.length; ++i) {
            nums[i] = _numPurchasesByAllowanceDigest[digest(allowances[i])];
        }
        return nums;
    }

    function _checkSignedAllowance(bytes32 digest_, SignatureGatedLib.SignedAllowance memory sa, uint256 num)
        internal
        view
    {
        // solhint-disable not-rely-on-time
        if (block.timestamp < sa.allowance.activeAfterTimestamp || block.timestamp >= sa.allowance.activeUntilTimestamp)
        {
            revert InactiveAllowance(sa.allowance);
        }
        // solhint-enable not-rely-on-time

        address signer = ECDSA.recover(digest_, sa.signature);
        if (!_isAuthorizedAllowanceSigner(signer)) {
            revert UnauthorisedSigner(sa, signer);
        }

        uint256 numLeft = sa.allowance.numMax - _numPurchasesByAllowanceDigest[digest_];
        if (numLeft < num) {
            revert TooManyPurchasesRequested(sa.allowance, numLeft, num);
        }
    }

    function _trackAllowanceUsage(bytes32 digest_, uint256 num) internal {
        _numPurchasesByAllowanceDigest[digest_] += num;
    }

    /**
     * @notice Returns true iff the given signer is authorised to sign allowances.
     */
    function _isAuthorizedAllowanceSigner(address signer) internal view virtual returns (bool);
}

/**
 * @notice Introduces claimability based on signed allowances.
 * @dev Adds signer authorisation via role based access control.
 */
abstract contract AccessControlledSignatureGatedBase is SignatureGatedBase, AccessControlled {
    /**
     * @notice The role required to sign allowances.
     */
    bytes32 public constant ALLOWANCE_SIGNER_ROLE = keccak256("ALLOWANCE_SIGNER_ROLE");

    constructor() {
        _setRoleAdmin(ALLOWANCE_SIGNER_ROLE, DEFAULT_STEERING_ROLE);
    }

    /**
     * @inheritdoc SignatureGatedBase
     */
    function _isAuthorizedAllowanceSigner(address signer) internal view virtual override returns (bool) {
        return hasRole(ALLOWANCE_SIGNER_ROLE, signer);
    }
}

/**
 * @notice Introduces claimability based on signed allowances.
 */
abstract contract SignatureGated is Seller, SignatureGatedBase {
    /**
     * @notice Encodes a purchase with a signed allowance, partially consuming it.
     */
    struct SignedAllowancePurchase {
        SignatureGatedLib.SignedAllowance signedAllowance;
        uint64 num;
    }

    /**
     * @notice Interface to perform purchases with signed allowances.
     * @dev Reverts if an allowances was incorrectly signed by an approved signed, is not active, or exhausted.
     */
    function purchase(SignedAllowancePurchase[] calldata purchases) public payable virtual {
        for (uint256 i; i < purchases.length; ++i) {
            _purchaseWithSignedAllowance(purchases[i]);
        }
    }

    /**
     * @notice Interface to perform a purchase with a signed allowance.
     * @dev Reverts if an allowances was incorrectly signed by an approved signed, is not active, or exhausted.
     */
    function _purchaseWithSignedAllowance(SignedAllowancePurchase calldata purchase_) internal virtual {
        // For clarity: Calling the virtual `_purchase` inherited from `Seller` here.
        _purchase(
            purchase_.signedAllowance.allowance.receiver,
            purchase_.num,
            purchase_.signedAllowance.allowance.price * purchase_.num,
            SignatureGatedLib.encodePurchaseData(digest(purchase_.signedAllowance.allowance), purchase_.signedAllowance)
        );
    }

    /**
     * @notice Validates a given purchase, i.e. signature validity and the number of purchases that have already been
     * performed.
     */
    function _checkAndModifyPurchase(address to, uint64 num, uint256 cost, bytes memory data)
        internal
        view
        virtual
        override
        returns (address, uint64, uint256)
    {
        (bytes32 digest_, SignatureGatedLib.SignedAllowance memory sa) = SignatureGatedLib.decodePurchaseData(data);
        _checkSignedAllowance(digest_, sa, num);

        return (to, num, cost);
    }

    /**
     * @inheritdoc Seller
     * @dev Updates the number of purchases that have already been performed with a given allowance.
     */
    function _beforePurchase(address, uint64 num, uint256, bytes memory data) internal virtual override {
        (bytes32 digest_,) = SignatureGatedLib.decodePurchaseData(data);
        _trackAllowanceUsage(digest_, num);
    }
}

abstract contract AccessControlledSignatureGated is AccessControlledSignatureGatedBase, SignatureGated {}
Compiler settingsShow
{
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 9999,
    "enabled": true
  },
  "evmVersion": "paris",
  "remappings": [
    "solidifylabs/presets/=/home/dave/solidifylabs/solidifylabs/contracts/presets/src/",
    "solidifylabs/artblocks/=/home/dave/solidifylabs/solidifylabs/contracts/artblocks/src/",
    "artblocks-contracts/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/artblocks-contracts_7f0af677/packages/contracts/contracts/",
    "artblocks-contracts_root/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/artblocks-contracts_7f0af677/",
    "openzeppelin-contracts-4-7-0/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/openzeppelin-contracts_4-7-0_exact_remap/contracts/",
    "openzeppelin-contracts_root-4-7-0/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/openzeppelin-contracts_4-7-0_exact_remap/",
    "@openzeppelin-4-7-0/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/openzeppelin-contracts_4-7-0_exact_remap/",
    "@openzeppelin-4.7/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/openzeppelin-contracts_4-7-0_exact_remap/",
    "solidifylabs/ethier/=/home/dave/solidifylabs/solidifylabs/contracts/ethier/src/",
    "ERC721A/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/ERC721A_4-2-3/contracts/",
    "ERC721A_root/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/ERC721A_4-2-3/",
    "erc721a/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/ERC721A_4-2-3/",
    "ethier/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/ethier_0-55-0/contracts/",
    "ethier_root/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/ethier_0-55-0/",
    "@divergencetech/ethier/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/ethier_0-55-0/",
    "openzeppelin-contracts/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/openzeppelin-contracts_4-8-1/contracts/",
    "openzeppelin-contracts_root/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/openzeppelin-contracts_4-8-1/",
    "@openzeppelin/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/openzeppelin-contracts_4-8-1/",
    "operator-filter-registry/src/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/operator-filter-registry_1-4-1/src/",
    "operator-filter-registry_root/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/operator-filter-registry_1-4-1/",
    "solidifylabs/redemption/=/home/dave/solidifylabs/solidifylabs/contracts/redemption/src/",
    "solidifylabs/sellers/=/home/dave/solidifylabs/solidifylabs/contracts/sellers/src/",
    "delegation-registry/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/delegation-registry_40d367d7/src/",
    "delegation-registry_root/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/delegation-registry_40d367d7/",
    "solidifylabs/artblocks_test/=/home/dave/solidifylabs/solidifylabs/contracts/artblocks/test/",
    "solidifylabs/constants/=/home/dave/solidifylabs/solidifylabs/contracts/constants/src/",
    "ds-test/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/ds-test_013e6c64/src/",
    "ds-test_root/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/ds-test_013e6c64/",
    "forge-std/=/home/dave/.cache/bazel/_bazel_dave/1a5ef124e5847560f6bb614f5401924c/external/forge-std_1-7-3/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.