Shielded x402 uses a suite of smart contracts to enable privacy-preserving payments and credit channel settlements on EVM-compatible chains and Solana.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nhestrompia/shielded-x402/llms.txt
Use this file to discover all available pages before exploring further.
Contract Architecture
The protocol consists of three core contracts:- ShieldedPool - Privacy-preserving deposit and spend pool with Merkle tree
- CreditChannelSettlement - Off-chain credit channel settlement with challenge periods
- CommitmentRegistryV1 - On-chain audit log for sequencer commitment epochs
ShieldedPool
The ShieldedPool contract enables privacy-preserving deposits and spends using zero-knowledge proofs.Features
- Deposit - Deposit ERC20 tokens with a commitment hash
- Spend - Spend from pool with ZK proof (creates merchant + change commitments)
- Withdraw - Merchant can withdraw to an address with challenge verification
- Merkle Tree - 24-level sparse Merkle tree for commitment tracking
- Nullifier Tracking - Prevent double-spending via nullifier registry
Key Functions
- amount - Amount of tokens to deposit
- commitment - Keccak256 commitment hash
- Emits:
Deposited(commitment, index, root, amount)
- proof - Zero-knowledge proof bytes (Noir/Groth16)
- nullifier - Unique nullifier to prevent double-spending
- root - Historical Merkle root
- merchantCommitment - Commitment for merchant’s output
- changeCommitment - Commitment for sender’s change
- challengeHash - Challenge hash for withdrawal verification
- amount - Spend amount
- Emits:
Spent(nullifier, merchantCommitment, changeCommitment, amount, ...)
- nullifier - Nullifier from the spend transaction
- challengeNonce - Pre-image of the challenge hash
- recipient - Withdrawal recipient address
- Emits:
Withdrawn(nullifier, recipient, amount, challengeNonce)
Contract Deployment
- asset_ - ERC20 token address (e.g., USDC)
- verifier_ - Proof verifier contract address
Constants
- TREE_DEPTH - 24 (supports ~16.7M commitments)
- SNARK_SCALAR_FIELD - BN254 scalar field modulus
Source
contracts/src/ShieldedPool.sol
CreditChannelSettlement
Enables off-chain credit channels with on-chain settlement and challenge periods.Features
- Credit Channels - Agent-relayer payment channels with escrow
- Optimistic Settlement - Challenge period before final settlement
- Dual Signatures - Both parties must sign state updates (EIP-712)
- Challenge Mechanism - Either party can challenge with a higher sequence number
Channel Lifecycle
- Open - Relayer opens channel with agent and deposits funds
- Topup - Relayer can add more funds to existing channel
- Start Close - Either party submits signed state to begin closing
- Challenge - Either party can challenge with newer state during challenge period
- Finalize - After challenge period, funds are distributed according to final state
Key Functions
- channelId - Unique channel identifier
- agent - Agent address
- amount - Amount to deposit (from msg.sender = relayer)
- Emits:
ChannelOpenedorChannelToppedUp
- signedState - Dual-signed credit state with agent + relayer signatures
- Emits:
CloseStarted(channelId, seq, challengeDeadline, available)
- signedState - Must have
seq > closeSeq - Resets challenge period
- Emits:
CloseChallenged
- Distributes funds:
availableto agent, remainder to relayer - Emits:
CloseFinalized(channelId, seq, paidToAgent, paidToRelayer)
Credit State Structure
Contract Deployment
- asset_ - ERC20 token address
- challengePeriodSeconds_ - Challenge period duration (e.g., 86400 for 1 day)
Signature Scheme
Uses EIP-712 typed structured data hashing:- Domain -
shielded-x402-creditversion1 - Type -
CreditStatewith all fields - Signatures - Both agent and relayer must sign the same digest
Source
contracts/src/CreditChannelSettlement.sol
CommitmentRegistryV1
Audit log for sequencer commitment epochs posted to Base.Features
- Epoch Tracking - Sequential epoch IDs with commitment roots
- Operator Control - Only sequencer operator can post
- Root Chaining - Each epoch references previous root
- Metadata - Stores commitment count, timestamp, and key ID
Key Functions
- epochId - Must be
latestEpochId + 1 - root - Merkle root of authorization commitments
- count - Number of authorizations in epoch
- prevRoot - Previous epoch root (for chaining)
- sequencerKeyId - Key ID used by sequencer
- Emits:
CommitmentPosted
Commitment Metadata
Contract Deployment
- sequencerOperator_ - Address authorized to post commitments
Source
contracts/src/CommitmentRegistryV1.sol
Proof Verification
Shielded x402 supports multiple proof verification systems:NoirVerifierAdapter
Adapter for Noir/Barretenberg proof system (Groth16).- verify(bytes proof, bytes32[] publicInputs) - Verifies a Noir proof
- Used by ShieldedPool for spend verification
- Source:
contracts/src/verifiers/NoirVerifierAdapter.sol
MockProofVerifier
Mock verifier for testing that always returns true. Source:contracts/src/verifiers/MockProofVerifier.sol
Test Utilities
MockUSDC
ERC20 test token with mint function.MockFeeOnTransferUSDC
ERC20 token that charges a fee on transfer (for testing ShieldedPool fee detection).DummyShieldedService
Minimal contract for testing merchant integrations.Deployment
Prerequisites
- Solidity ^0.8.26
- Foundry or Hardhat
- ERC20 asset token (e.g., USDC)
- Deployed proof verifier contract
Deployment Order
- Deploy proof verifier (NoirVerifierAdapter or custom)
- Deploy ShieldedPool with asset and verifier addresses
- Deploy CreditChannelSettlement with asset and challenge period
- Deploy CommitmentRegistryV1 with sequencer operator address
Example (Foundry)
Next Steps
Deployment Guide
Full deployment instructions
Sequencer Setup
Configure the credit sequencer