Skip to main content

walletkit_core/storage/
paths.rs

1//! Storage path helpers.
2
3use std::path::{Path, PathBuf};
4
5const VAULT_FILENAME: &str = "account.vault.sqlite";
6const CACHE_FILENAME: &str = "account.cache.sqlite";
7const LOCK_FILENAME: &str = "lock";
8const GROTH16_DIRNAME: &str = "groth16";
9const QUERY_ZKEY_FILENAME: &str = "OPRFQuery.arks.zkey";
10const NULLIFIER_ZKEY_FILENAME: &str = "OPRFNullifier.arks.zkey";
11const QUERY_GRAPH_FILENAME: &str = "OPRFQueryGraph.bin";
12const NULLIFIER_GRAPH_FILENAME: &str = "OPRFNullifierGraph.bin";
13
14/// Paths for credential storage artifacts under `<root>/worldid`.
15#[derive(Debug, Clone, uniffi::Object)]
16pub struct StoragePaths {
17    root: PathBuf,
18    worldid_dir: PathBuf,
19}
20
21impl StoragePaths {
22    /// Builds storage paths rooted at `root`.
23    #[must_use]
24    pub fn new(root: impl AsRef<Path>) -> Self {
25        let root = root.as_ref().to_path_buf();
26        let worldid_dir = root.join("worldid");
27        Self { root, worldid_dir }
28    }
29
30    /// Returns the storage root directory.
31    #[must_use]
32    pub fn root(&self) -> &Path {
33        &self.root
34    }
35
36    /// Returns the World ID storage directory.
37    #[must_use]
38    pub fn worldid_dir(&self) -> &Path {
39        &self.worldid_dir
40    }
41
42    /// Returns the path to the vault database.
43    #[must_use]
44    pub fn vault_db_path(&self) -> PathBuf {
45        self.worldid_dir.join(VAULT_FILENAME)
46    }
47
48    /// Returns the path to the cache database.
49    #[must_use]
50    pub fn cache_db_path(&self) -> PathBuf {
51        self.worldid_dir.join(CACHE_FILENAME)
52    }
53
54    /// Returns the path to the lock file.
55    #[must_use]
56    pub fn lock_path(&self) -> PathBuf {
57        self.worldid_dir.join(LOCK_FILENAME)
58    }
59
60    /// Returns the path to the Groth16 material directory.
61    #[must_use]
62    pub fn groth16_dir(&self) -> PathBuf {
63        self.worldid_dir.join(GROTH16_DIRNAME)
64    }
65
66    /// Returns the path to the query zkey file.
67    #[must_use]
68    pub fn query_zkey_path(&self) -> PathBuf {
69        self.groth16_dir().join(QUERY_ZKEY_FILENAME)
70    }
71
72    /// Returns the path to the nullifier zkey file.
73    #[must_use]
74    pub fn nullifier_zkey_path(&self) -> PathBuf {
75        self.groth16_dir().join(NULLIFIER_ZKEY_FILENAME)
76    }
77
78    /// Returns the path to the query graph file.
79    #[must_use]
80    pub fn query_graph_path(&self) -> PathBuf {
81        self.groth16_dir().join(QUERY_GRAPH_FILENAME)
82    }
83
84    /// Returns the path to the nullifier graph file.
85    #[must_use]
86    pub fn nullifier_graph_path(&self) -> PathBuf {
87        self.groth16_dir().join(NULLIFIER_GRAPH_FILENAME)
88    }
89}
90
91#[uniffi::export]
92impl StoragePaths {
93    /// Builds storage paths rooted at `root`.
94    #[uniffi::constructor]
95    #[must_use]
96    pub fn from_root(root: String) -> Self {
97        Self::new(PathBuf::from(root))
98    }
99
100    /// Returns the storage root directory as a string.
101    #[must_use]
102    pub fn root_path_string(&self) -> String {
103        self.root.to_string_lossy().to_string()
104    }
105
106    /// Returns the World ID storage directory as a string.
107    #[must_use]
108    pub fn worldid_dir_path_string(&self) -> String {
109        self.worldid_dir.to_string_lossy().to_string()
110    }
111
112    /// Returns the path to the vault database as a string.
113    #[must_use]
114    pub fn vault_db_path_string(&self) -> String {
115        self.vault_db_path().to_string_lossy().to_string()
116    }
117
118    /// Returns the path to the cache database as a string.
119    #[must_use]
120    pub fn cache_db_path_string(&self) -> String {
121        self.cache_db_path().to_string_lossy().to_string()
122    }
123
124    /// Returns the path to the lock file as a string.
125    #[must_use]
126    pub fn lock_path_string(&self) -> String {
127        self.lock_path().to_string_lossy().to_string()
128    }
129
130    /// Returns the path to the Groth16 material directory as a string.
131    #[must_use]
132    pub fn groth16_dir_path_string(&self) -> String {
133        self.groth16_dir().to_string_lossy().to_string()
134    }
135
136    /// Returns the path to the query zkey file as a string.
137    #[must_use]
138    pub fn query_zkey_path_string(&self) -> String {
139        self.query_zkey_path().to_string_lossy().to_string()
140    }
141
142    /// Returns the path to the nullifier zkey file as a string.
143    #[must_use]
144    pub fn nullifier_zkey_path_string(&self) -> String {
145        self.nullifier_zkey_path().to_string_lossy().to_string()
146    }
147
148    /// Returns the path to the query graph file as a string.
149    #[must_use]
150    pub fn query_graph_path_string(&self) -> String {
151        self.query_graph_path().to_string_lossy().to_string()
152    }
153
154    /// Returns the path to the nullifier graph file as a string.
155    #[must_use]
156    pub fn nullifier_graph_path_string(&self) -> String {
157        self.nullifier_graph_path().to_string_lossy().to_string()
158    }
159}
160
161#[cfg(test)]
162mod tests {
163    use super::StoragePaths;
164    use std::path::PathBuf;
165
166    #[test]
167    fn test_groth16_paths() {
168        let root = PathBuf::from("/tmp/walletkit-paths");
169        let paths = StoragePaths::new(&root);
170        let worldid = root.join("worldid");
171        let groth16 = worldid.join("groth16");
172
173        assert_eq!(paths.groth16_dir(), groth16);
174        assert_eq!(paths.query_zkey_path(), groth16.join("OPRFQuery.arks.zkey"));
175        assert_eq!(
176            paths.nullifier_zkey_path(),
177            groth16.join("OPRFNullifier.arks.zkey")
178        );
179        assert_eq!(paths.query_graph_path(), groth16.join("OPRFQueryGraph.bin"));
180        assert_eq!(
181            paths.nullifier_graph_path(),
182            groth16.join("OPRFNullifierGraph.bin")
183        );
184    }
185
186    #[test]
187    fn test_groth16_path_strings() {
188        let root = PathBuf::from("/tmp/walletkit-paths");
189        let paths = StoragePaths::new(&root);
190
191        assert_eq!(
192            paths.groth16_dir_path_string(),
193            paths.groth16_dir().to_string_lossy()
194        );
195        assert_eq!(
196            paths.query_zkey_path_string(),
197            paths.query_zkey_path().to_string_lossy()
198        );
199        assert_eq!(
200            paths.nullifier_zkey_path_string(),
201            paths.nullifier_zkey_path().to_string_lossy()
202        );
203        assert_eq!(
204            paths.query_graph_path_string(),
205            paths.query_graph_path().to_string_lossy()
206        );
207        assert_eq!(
208            paths.nullifier_graph_path_string(),
209            paths.nullifier_graph_path().to_string_lossy()
210        );
211    }
212}