Skip to content

Relay Server

Rust application serving as Association Set Provider (ASP) for Privacy Pools.

Architecture

relay_server_rs/
├── src/
│   ├── main.rs
│   ├── server/routes.rs     # API endpoints
│   ├── merkle/              # Tree implementation
│   ├── indexer/             # Event sync
│   └── starknet/relayer.rs  # Tx submission
└── Cargo.toml

Responsibilities

FunctionDescription
Event IndexingSync deposits from Starknet
Merkle TreesMaintain deposit + association trees
Merkle PathsProvide proofs for frontend
Tx RelaySubmit transactions (gas abstraction)

API Endpoints

EndpointMethodPurpose
/api/merkle/path/:indexGETGet Merkle proof
/api/merkle/rootGETCurrent root
/api/commitment/:hashGETCheck if indexed
/api/swapPOSTSubmit swap tx

Privacy Pools

Dual Merkle tree for compliance:

rust
struct DualMerkleTree {
    deposit_tree: MerkleTree,      // All deposits
    association_tree: MerkleTree,  // Curated "clean" deposits
}
  • Deposit Tree: Every commitment, no filtering
  • Association Tree: Curated by ASP policy, excludes flagged deposits

Users can prove against either tree.

Event Indexing

Syncs LeafInsertedBN254 events:

rust
async fn sync_deposits(&mut self) -> Result<()> {
    let events = self.starknet
        .get_events(self.contract, "LeafInsertedBN254", self.last_block)
        .await?;

    for event in events {
        let leaf = parse_leaf(&event);
        self.deposit_tree.insert(leaf);

        if self.is_clean(&leaf) {
            self.association_tree.insert(leaf);
        }
    }
    Ok(())
}

Transaction Relay

Gas abstraction—relay pays gas, user pays fee from shielded amount:

rust
async fn relay_swap(&self, request: SwapRequest) -> Result<TxHash> {
    let tx = self.build_swap_tx(&request);
    let hash = self.starknet.send_transaction(tx).await?;
    Ok(hash)
}

Trust Model

Can DoCannot Do
Censor transactionsSteal funds
Exclude from association setForge proofs
See public dataSee private inputs

Semi-trusted: affects availability, not security.

Stack

ComponentTechnology
RuntimeTokio
HTTPAxum
DatabaseSQLite
Starknetstarknet-rs
Hashinglight-poseidon

Released under the MIT License.