-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathcontract.rs
More file actions
126 lines (106 loc) · 4.95 KB
/
contract.rs
File metadata and controls
126 lines (106 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// ============================================================
// Contract Interface - Public API
// ============================================================
// This module defines the contract struct and delegates to core modules.
// Keeps the interface clean and focused on orchestration.
// ============================================================
use soroban_sdk::{contract, contractimpl, Address, BytesN, Env, String};
use crate::core::{admin, deposit, initialize, view, withdraw};
use crate::types::errors::Error;
use crate::types::state::{Denomination, PoolConfig, Proof, PublicInputs, VerifyingKey};
#[contract]
pub struct PrivacyPool;
#[contractimpl]
impl PrivacyPool {
// ──────────────────────────────────────────────────────────
// Initialization
// ──────────────────────────────────────────────────────────
/// Initialize the privacy pool.
///
/// Must be called once before any deposits or withdrawals.
/// Sets the admin, token, denomination, and verifying key.
pub fn initialize(
env: Env,
admin: Address,
token: Address,
denomination: Denomination,
vk: VerifyingKey,
) -> Result<(), Error> {
initialize::execute(env, admin, token, denomination, vk)
}
// ──────────────────────────────────────────────────────────
// Core Operations
// ──────────────────────────────────────────────────────────
/// Deposit into the shielded pool.
///
/// Transfers denomination amount and inserts commitment into Merkle tree.
pub fn deposit(
env: Env,
from: Address,
commitment: BytesN<32>,
) -> Result<(u32, BytesN<32>), Error> {
deposit::execute(env, from, commitment)
}
/// Withdraw from the shielded pool using a ZK proof.
///
/// Verifies proof and transfers funds to recipient.
pub fn withdraw(
env: Env,
proof: Proof,
pub_inputs: PublicInputs,
) -> Result<bool, Error> {
withdraw::execute(env, proof, pub_inputs)
}
// ──────────────────────────────────────────────────────────
// View Functions
// ──────────────────────────────────────────────────────────
/// Returns the current Merkle root (most recent).
pub fn get_root(env: Env) -> Result<BytesN<32>, Error> {
view::get_root(env)
}
/// Returns the total number of deposits.
pub fn deposit_count(env: Env) -> u32 {
view::deposit_count(env)
}
/// Check if a root is in the historical root buffer.
pub fn is_known_root(env: Env, root: BytesN<32>) -> bool {
view::is_known_root(env, root)
}
/// Check if a nullifier has been spent.
pub fn is_spent(env: Env, nullifier_hash: BytesN<32>) -> bool {
view::is_spent(env, nullifier_hash)
}
/// Returns the pool configuration.
pub fn get_config_view(env: Env) -> Result<PoolConfig, Error> {
view::get_config(env)
}
// ──────────────────────────────────────────────────────────
// Admin Functions
// ──────────────────────────────────────────────────────────
/// Pause the pool (admin only).
/// Records the pause timestamp and reason for audit trail.
pub fn pause(env: Env, admin: Address, reason: String) -> Result<(), Error> {
admin::pause(env, admin, reason)
}
/// Unpause the pool (admin only).
pub fn unpause(env: Env, admin: Address) -> Result<(), Error> {
admin::unpause(env, admin)
}
/// Check if the pool is currently paused.
pub fn is_paused(env: Env) -> bool {
view::is_paused(env)
}
/// Emergency withdrawal — transfers entire token balance to admin.
/// Only available when pool is paused. Admin-only.
pub fn emergency_withdraw(env: Env, admin: Address) -> Result<i128, Error> {
admin::emergency_withdraw(env, admin)
}
/// Update the Groth16 verifying key (admin only).
pub fn set_verifying_key(
env: Env,
admin: Address,
new_vk: VerifyingKey,
) -> Result<(), Error> {
admin::set_verifying_key(env, admin, new_vk)
}
}