Skip to content
Lattice

ShieldedPoolLib

Git Source

Title: ShieldedPoolLib

Author: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80)

Library implementing fixed-denomination shielded (private) ERC-20 transfers (Tornado-style): deposits insert Poseidon(nullifier, secret) commitments into a Poseidon LeanIMT; withdrawals prove membership in zero knowledge and burn a one-time nullifier hash to an arbitrary recipient.

Three-layer pattern: this library holds the logic + namespaced storage; the stateless ShieldedPool facet forwards to it. The on-chain mechanics are wrapped from the shared IncrementalMerkleTreeLib (Poseidon LeanIMT + recent-root ring) and NullifierRegistryLib; the withdraw circuit + verifier are consumer-supplied per pool. Withdrawals follow strict CEI: the nullifier is spent BEFORE any token transfer, so a reentrant token cannot replay a spend.

Functions

shieldedPoolStorage

function shieldedPoolStorage() internal pure returns (ShieldedPoolStorage storage $);

Returns

NameTypeDescription
$ShieldedPoolStorage

__ShieldedPool_init

Initializes the ShieldedPool module.

Must be called inside a pre/postInitializer block. Registers IShieldedPool for ERC-165.

function __ShieldedPool_init() internal;

registerInterface

Registers support for the IShieldedPool interface via ERC-165.

function registerInterface() internal;

createPool

Creates a fixed-denomination shielded pool. Gated on the default admin role.

function createPool(address token, uint256 denomination, address verifier) internal returns (uint256 poolId);

Parameters

NameTypeDescription
tokenaddress
denominationuint256
verifieraddress

Returns

NameTypeDescription
poolIduint256

deposit

Deposits one denomination and inserts commitment.

Pulls denomination from msg.sender first, then inserts the leaf. Guarded against reentrancy (the diamond-wide guard also blocks cross-entry reentry into withdraw).

function deposit(uint256 poolId, uint256 commitment) internal;

Parameters

NameTypeDescription
poolIduint256
commitmentuint256

withdraw

Withdraws denomination - fee to recipient and fee to relayer against a proof.

Strict CEI: verify → spend nullifier → transfer. The proof's 5 public signals are [root, nullifierHash, recipient, relayer, fee], binding the destination + fee.

function withdraw(
        uint256 poolId,
        IShieldedPool.WithdrawProof calldata proof,
        uint256 root,
        uint256 nullifierHash,
        address recipient,
        address relayer,
        uint256 fee
    ) internal;

Parameters

NameTypeDescription
poolIduint256
proofIShieldedPool.WithdrawProof
rootuint256
nullifierHashuint256
recipientaddress
relayeraddress
feeuint256

poolCount

function poolCount() internal view returns (uint256);

Returns

NameTypeDescription
<none>uint256

getPool

function getPool(uint256 poolId)
        internal
        view
        returns (address token, uint256 denomination, address verifier, uint256 merkleRoot, uint256 numLeaves);

Parameters

NameTypeDescription
poolIduint256

Returns

NameTypeDescription
tokenaddress
denominationuint256
verifieraddress
merkleRootuint256
numLeavesuint256

isSpent

function isSpent(uint256 poolId, uint256 nullifierHash) internal view returns (bool);

Parameters

NameTypeDescription
poolIduint256
nullifierHashuint256

Returns

NameTypeDescription
<none>bool

isKnownRoot

function isKnownRoot(uint256 poolId, uint256 root) internal view returns (bool);

Parameters

NameTypeDescription
poolIduint256
rootuint256

Returns

NameTypeDescription
<none>bool

_safeTransferFrom

transferFrom tolerating non-standard (no-return) ERC-20s; reverts on explicit failure, and on empty return data from an address with no code (matches OpenZeppelin SafeERC20).

function _safeTransferFrom(address token, address from, address to, uint256 amount) private;

Parameters

NameTypeDescription
tokenaddress
fromaddress
toaddress
amountuint256

_safeTransfer

transfer tolerating non-standard (no-return) ERC-20s; reverts on explicit failure, and on empty return data from an address with no code (matches OpenZeppelin SafeERC20).

function _safeTransfer(address token, address to, uint256 amount) private;

Parameters

NameTypeDescription
tokenaddress
toaddress
amountuint256