Verified contractExactMIT
WhitelistFeature
Address0xe0948e1aecdb037d90aa1079672c947500e58bfd
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.35
- Same code
Only this contract
Details
- Compiler
- solc 0.8.35+commit.47b9dedd
- Language
- solidity
- Optimization
- Yes, 10,000 runs
- EVM version
- cancun
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,280,018
- Creation transaction
- 0x5f23c35df9…04099a8479
src/modules/features/WhitelistFeature.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import {MerkleProofLib} from "solady/utils/MerkleProofLib.sol";
import {IFeatureModule} from "@namespace/interfaces/IFeatureModule.sol";
import {FeatureData} from "@namespace/libraries/FeatureData.sol";
import {Types} from "@namespace/libraries/Types.sol";
import {FeatureModule} from "@namespace/modules/features/FeatureModule.sol";
/// @title WhitelistFeature
/// @notice Merkle allowlist that either gates minting or makes it free for members. Config:
/// `abi.encode(Mode mode, bytes32 root)`.
/// @author artii.eth
/// @author envoy1084.eth
contract WhitelistFeature is FeatureModule {
////////////////////////////////////////////////////////////////////////
// Types
////////////////////////////////////////////////////////////////////////
enum Mode {
Gate,
Free
}
struct Config {
Mode mode;
bytes32 root;
}
////////////////////////////////////////////////////////////////////////
// Storage
////////////////////////////////////////////////////////////////////////
mapping(bytes32 activationId => Config) public configs;
////////////////////////////////////////////////////////////////////////
// Events
////////////////////////////////////////////////////////////////////////
event WhitelistConfigured(bytes32 indexed activationId, Mode mode, bytes32 root);
////////////////////////////////////////////////////////////////////////
// Errors
////////////////////////////////////////////////////////////////////////
error InvalidWhitelistData();
////////////////////////////////////////////////////////////////////////
// Initialization
////////////////////////////////////////////////////////////////////////
constructor(address manager_) FeatureModule(manager_) {}
////////////////////////////////////////////////////////////////////////
// Implementation
////////////////////////////////////////////////////////////////////////
/// @inheritdoc IFeatureModule
function configure(bytes32 activationId, bytes calldata data) external onlyManager {
if (data.length != 64) revert InvalidWhitelistData();
(Mode mode, bytes32 root) = abi.decode(data, (Mode, bytes32));
if (root == bytes32(0)) revert InvalidWhitelistData();
configs[activationId] = Config(mode, root);
emit WhitelistConfigured(activationId, mode, root);
}
/// @inheritdoc IFeatureModule
function evaluate(MintContext calldata ctx) external view returns (Types.MintEvaluation memory result) {
Config storage config = configs[ctx.activationId];
bool whitelisted = _isWhitelisted(ctx.extraData, config.root, ctx.minter);
if (config.mode == Mode.Gate) {
if (!whitelisted) result.denied = Types.DeniedReason.MinterNotAllowed;
return result;
}
if (whitelisted) {
result.priceLevel = Types.PriceLevel.Free;
result.price = 0;
}
}
////////////////////////////////////////////////////////////////////////
// Internal Functions
////////////////////////////////////////////////////////////////////////
function _isWhitelisted(bytes calldata extraData, bytes32 root, address minter) private view returns (bool) {
bytes memory claim = FeatureData.get(extraData, address(this));
if (claim.length == 0) return false;
bytes32[] memory proof = abi.decode(claim, (bytes32[]));
return MerkleProofLib.verify(proof, root, keccak256(abi.encodePacked(minter)));
}
}
../../node_modules/solady/src/utils/MerkleProofLib.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Gas optimized verification of proof of inclusion for a leaf in a Merkle tree.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/MerkleProofLib.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/MerkleProofLib.sol)
/// @author Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/MerkleProof.sol)
library MerkleProofLib {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* MERKLE PROOF VERIFICATION OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns whether `leaf` exists in the Merkle tree with `root`, given `proof`.
function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf)
internal
pure
returns (bool isValid)
{
/// @solidity memory-safe-assembly
assembly {
if mload(proof) {
// Initialize `offset` to the offset of `proof` elements in memory.
let offset := add(proof, 0x20)
// Left shift by 5 is equivalent to multiplying by 0x20.
let end := add(offset, shl(5, mload(proof)))
// Iterate over proof elements to compute root hash.
for {} 1 {} {
// Slot of `leaf` in scratch space.
// If the condition is true: 0x20, otherwise: 0x00.
let scratch := shl(5, gt(leaf, mload(offset)))
// Store elements to hash contiguously in scratch space.
// Scratch space is 64 bytes (0x00 - 0x3f) and both elements are 32 bytes.
mstore(scratch, leaf)
mstore(xor(scratch, 0x20), mload(offset))
// Reuse `leaf` to store the hash to reduce stack operations.
leaf := keccak256(0x00, 0x40)
offset := add(offset, 0x20)
if iszero(lt(offset, end)) { break }
}
}
isValid := eq(leaf, root)
}
}
/// @dev Returns whether `leaf` exists in the Merkle tree with `root`, given `proof`.
function verifyCalldata(bytes32[] calldata proof, bytes32 root, bytes32 leaf)
internal
pure
returns (bool isValid)
{
/// @solidity memory-safe-assembly
assembly {
if proof.length {
// Left shift by 5 is equivalent to multiplying by 0x20.
let end := add(proof.offset, shl(5, proof.length))
// Initialize `offset` to the offset of `proof` in the calldata.
let offset := proof.offset
// Iterate over proof elements to compute root hash.
for {} 1 {} {
// Slot of `leaf` in scratch space.
// If the condition is true: 0x20, otherwise: 0x00.
let scratch := shl(5, gt(leaf, calldataload(offset)))
// Store elements to hash contiguously in scratch space.
// Scratch space is 64 bytes (0x00 - 0x3f) and both elements are 32 bytes.
mstore(scratch, leaf)
mstore(xor(scratch, 0x20), calldataload(offset))
// Reuse `leaf` to store the hash to reduce stack operations.
leaf := keccak256(0x00, 0x40)
offset := add(offset, 0x20)
if iszero(lt(offset, end)) { break }
}
}
isValid := eq(leaf, root)
}
}
/// @dev Returns whether all `leaves` exist in the Merkle tree with `root`,
/// given `proof` and `flags`.
///
/// Note:
/// - Breaking the invariant `flags.length == (leaves.length - 1) + proof.length`
/// will always return false.
/// - The sum of the lengths of `proof` and `leaves` must never overflow.
/// - Any non-zero word in the `flags` array is treated as true.
/// - The memory offset of `proof` must be non-zero
/// (i.e. `proof` is not pointing to the scratch space).
function verifyMultiProof(
bytes32[] memory proof,
bytes32 root,
bytes32[] memory leaves,
bool[] memory flags
) internal pure returns (bool isValid) {
// Rebuilds the root by consuming and producing values on a queue.
// The queue starts with the `leaves` array, and goes into a `hashes` array.
// After the process, the last element on the queue is verified
// to be equal to the `root`.
//
// The `flags` array denotes whether the sibling
// should be popped from the queue (`flag == true`), or
// should be popped from the `proof` (`flag == false`).
/// @solidity memory-safe-assembly
assembly {
// Cache the lengths of the arrays.
let leavesLength := mload(leaves)
let proofLength := mload(proof)
let flagsLength := mload(flags)
// Advance the pointers of the arrays to point to the data.
leaves := add(0x20, leaves)
proof := add(0x20, proof)
flags := add(0x20, flags)
// If the number of flags is correct.
for {} eq(add(leavesLength, proofLength), add(flagsLength, 1)) {} {
// For the case where `proof.length + leaves.length == 1`.
if iszero(flagsLength) {
// `isValid = (proof.length == 1 ? proof[0] : leaves[0]) == root`.
isValid := eq(mload(xor(leaves, mul(xor(proof, leaves), proofLength))), root)
break
}
// The required final proof offset if `flagsLength` is not zero, otherwise zero.
let proofEnd := add(proof, shl(5, proofLength))
// We can use the free memory space for the queue.
// We don't need to allocate, since the queue is temporary.
let hashesFront := mload(0x40)
// Copy the leaves into the hashes.
// Sometimes, a little memory expansion costs less than branching.
// Should cost less, even with a high free memory offset of 0x7d00.
leavesLength := shl(5, leavesLength)
for { let i := 0 } iszero(eq(i, leavesLength)) { i := add(i, 0x20) } {
mstore(add(hashesFront, i), mload(add(leaves, i)))
}
// Compute the back of the hashes.
let hashesBack := add(hashesFront, leavesLength)
// This is the end of the memory for the queue.
// We recycle `flagsLength` to save on stack variables (sometimes save gas).
flagsLength := add(hashesBack, shl(5, flagsLength))
for {} 1 {} {
// Pop from `hashes`.
let a := mload(hashesFront)
// Pop from `hashes`.
let b := mload(add(hashesFront, 0x20))
hashesFront := add(hashesFront, 0x40)
// If the flag is false, load the next proof,
// else, pops from the queue.
if iszero(mload(flags)) {
// Loads the next proof.
b := mload(proof)
proof := add(proof, 0x20)
// Unpop from `hashes`.
hashesFront := sub(hashesFront, 0x20)
}
// Advance to the next flag.
flags := add(flags, 0x20)
// Slot of `a` in scratch space.
// If the condition is true: 0x20, otherwise: 0x00.
let scratch := shl(5, gt(a, b))
// Hash the scratch space and push the result onto the queue.
mstore(scratch, a)
mstore(xor(scratch, 0x20), b)
mstore(hashesBack, keccak256(0x00, 0x40))
hashesBack := add(hashesBack, 0x20)
if iszero(lt(hashesBack, flagsLength)) { break }
}
isValid :=
and(
// Checks if the last value in the queue is same as the root.
eq(mload(sub(hashesBack, 0x20)), root),
// And whether all the proofs are used, if required.
eq(proofEnd, proof)
)
break
}
}
}
/// @dev Returns whether all `leaves` exist in the Merkle tree with `root`,
/// given `proof` and `flags`.
///
/// Note:
/// - Breaking the invariant `flags.length == (leaves.length - 1) + proof.length`
/// will always return false.
/// - Any non-zero word in the `flags` array is treated as true.
/// - The calldata offset of `proof` must be non-zero
/// (i.e. `proof` is from a regular Solidity function with a 4-byte selector).
function verifyMultiProofCalldata(
bytes32[] calldata proof,
bytes32 root,
bytes32[] calldata leaves,
bool[] calldata flags
) internal pure returns (bool isValid) {
// Rebuilds the root by consuming and producing values on a queue.
// The queue starts with the `leaves` array, and goes into a `hashes` array.
// After the process, the last element on the queue is verified
// to be equal to the `root`.
//
// The `flags` array denotes whether the sibling
// should be popped from the queue (`flag == true`), or
// should be popped from the `proof` (`flag == false`).
/// @solidity memory-safe-assembly
assembly {
// If the number of flags is correct.
for {} eq(add(leaves.length, proof.length), add(flags.length, 1)) {} {
// For the case where `proof.length + leaves.length == 1`.
if iszero(flags.length) {
// `isValid = (proof.length == 1 ? proof[0] : leaves[0]) == root`.
// forgefmt: disable-next-item
isValid := eq(
calldataload(
xor(leaves.offset, mul(xor(proof.offset, leaves.offset), proof.length))
),
root
)
break
}
// The required final proof offset if `flagsLength` is not zero, otherwise zero.
let proofEnd := add(proof.offset, shl(5, proof.length))
// We can use the free memory space for the queue.
// We don't need to allocate, since the queue is temporary.
let hashesFront := mload(0x40)
// Copy the leaves into the hashes.
// Sometimes, a little memory expansion costs less than branching.
// Should cost less, even with a high free memory offset of 0x7d00.
calldatacopy(hashesFront, leaves.offset, shl(5, leaves.length))
// Compute the back of the hashes.
let hashesBack := add(hashesFront, shl(5, leaves.length))
// This is the end of the memory for the queue.
// We recycle `flagsLength` to save on stack variables (sometimes save gas).
flags.length := add(hashesBack, shl(5, flags.length))
// We don't need to make a copy of `proof.offset` or `flags.offset`,
// as they are pass-by-value (this trick may not always save gas).
for {} 1 {} {
// Pop from `hashes`.
let a := mload(hashesFront)
// Pop from `hashes`.
let b := mload(add(hashesFront, 0x20))
hashesFront := add(hashesFront, 0x40)
// If the flag is false, load the next proof,
// else, pops from the queue.
if iszero(calldataload(flags.offset)) {
// Loads the next proof.
b := calldataload(proof.offset)
proof.offset := add(proof.offset, 0x20)
// Unpop from `hashes`.
hashesFront := sub(hashesFront, 0x20)
}
// Advance to the next flag offset.
flags.offset := add(flags.offset, 0x20)
// Slot of `a` in scratch space.
// If the condition is true: 0x20, otherwise: 0x00.
let scratch := shl(5, gt(a, b))
// Hash the scratch space and push the result onto the queue.
mstore(scratch, a)
mstore(xor(scratch, 0x20), b)
mstore(hashesBack, keccak256(0x00, 0x40))
hashesBack := add(hashesBack, 0x20)
if iszero(lt(hashesBack, flags.length)) { break }
}
isValid :=
and(
// Checks if the last value in the queue is same as the root.
eq(mload(sub(hashesBack, 0x20)), root),
// And whether all the proofs are used, if required.
eq(proofEnd, proof.offset)
)
break
}
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* EMPTY CALLDATA HELPERS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Returns an empty calldata bytes32 array.
function emptyProof() internal pure returns (bytes32[] calldata proof) {
/// @solidity memory-safe-assembly
assembly {
proof.length := 0
}
}
/// @dev Returns an empty calldata bytes32 array.
function emptyLeaves() internal pure returns (bytes32[] calldata leaves) {
/// @solidity memory-safe-assembly
assembly {
leaves.length := 0
}
}
/// @dev Returns an empty calldata bool array.
function emptyFlags() internal pure returns (bool[] calldata flags) {
/// @solidity memory-safe-assembly
assembly {
flags.length := 0
}
}
}
src/interfaces/IFeatureModule.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import {Types} from "@namespace/libraries/Types.sol";
/// @title IFeatureModule
/// @notice Owner-configurable mint policy that stores its own config per activationId and evaluates it at quote
/// time. Registered on the manager at runtime.
/// @author artii.eth
/// @author envoy1084.eth
interface IFeatureModule {
/// @param activationId Config key; a re-activation moves it, orphaning the module's old config.
/// @param renewal True when quoting a renewal, whose denials the manager skips.
/// @param extraData Minter-supplied `FeatureData` envelope carrying claim material such as proofs.
struct MintContext {
bytes32 activationId;
string label;
address minter;
uint64 duration;
bool renewal;
bytes extraData;
}
/// @notice Validates and stores the activation's config. Manager-only.
function configure(bytes32 activationId, bytes calldata data) external;
/// @notice Judges one mint: a module may deny it, price it, or both.
function evaluate(MintContext calldata ctx) external view returns (Types.MintEvaluation memory result);
}
src/libraries/FeatureData.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
/// @title FeatureData
/// @notice The `extraData` envelope: `abi.encode(Entry[])`, one slice per feature address.
/// @author artii.eth
/// @author envoy1084.eth
library FeatureData {
struct Entry {
address feature;
bytes data;
}
function get(bytes calldata extraData, address feature) internal pure returns (bytes memory) {
if (extraData.length == 0) return "";
Entry[] memory entries = abi.decode(extraData, (Entry[]));
for (uint256 i; i < entries.length; i++) {
if (entries[i].feature == feature) return entries[i].data;
}
return "";
}
}
src/libraries/Types.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
/// @title Types
/// @notice Types shared by the manager, the issuer and the modules.
/// @author artii.eth
/// @author envoy1084.eth
library Types {
/// @dev Append-only: the deployed manager decodes module results against the members it was compiled with.
enum DeniedReason {
None,
NotActive,
EmptyLabel,
NoSubnameRegistry,
LabelUnavailable,
MintNotStarted,
MintEnded,
MinterNotAllowed,
ModuleDenied,
LabelReserved,
DurationTooShort,
NotRenewable
}
/// @dev Expirable charges the price as an annual rate over `duration`; NonExpirable charges it flat, forever.
enum Expiration {
Expirable,
NonExpirable
}
/// @dev The highest level wins, the last configured on a tie. Append-only, like DeniedReason.
enum PriceLevel {
None,
Base,
Class,
Override,
Free,
Reserved
}
struct MintEvaluation {
DeniedReason denied;
PriceLevel priceLevel;
uint256 price;
}
struct MintMetadata {
Receiver[] receivers;
uint256 roleBitmap;
address resolver;
}
/// @dev A zero `to` stands for the live name owner until the manager resolves it.
struct Receiver {
address to;
uint16 bps;
}
struct QuoteMintResult {
bool canMint;
uint256 price;
DeniedReason denied;
Expiration expiration;
bytes32 activationId;
address subregistry;
}
struct QuoteRenewalResult {
bool canRenew;
uint256 price;
DeniedReason denied;
bytes32 activationId;
address subregistry;
}
}
src/modules/features/FeatureModule.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import {IFeatureModule} from "@namespace/interfaces/IFeatureModule.sol";
/// @title FeatureModule
/// @notice Base for feature modules: pins the manager and gates `configure` to it.
/// @author artii.eth
/// @author envoy1084.eth
abstract contract FeatureModule is IFeatureModule {
address public immutable manager;
error ZeroManager();
error NotManager(address caller);
modifier onlyManager() {
if (msg.sender != manager) revert NotManager(msg.sender);
_;
}
constructor(address manager_) {
if (manager_ == address(0)) revert ZeroManager();
manager = manager_;
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 10000,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"@openzeppelin/contracts/=../../node_modules/@openzeppelin/contracts/",
"@openzeppelin/contracts-upgradeable/=../../node_modules/@openzeppelin/contracts-upgradeable/",
"forge-std/=../../node_modules/forge-std/src/",
"solady/=../../node_modules/solady/src/",
"@namespace/=src/",
"@test/=test/",
"@ens/contracts/=vendor/ens/ens-v1/",
"@ensv2/=vendor/ens/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.