world_id_primitives/
config.rs1use std::path::{Path, PathBuf};
2
3use serde::{Deserialize, Serialize};
4
5use alloy_primitives::Address;
6use url::Url;
7
8use crate::PrimitiveError;
9
10const fn default_nullifier_oracle_threshold() -> usize {
11 2
12}
13
14#[derive(Clone, Debug, Serialize, Deserialize)]
18pub struct Config {
19 rpc_url: Option<Url>,
23 chain_id: u64,
25 registry_address: Address,
27 indexer_url: String,
29 gateway_url: String,
31 nullifier_oracle_urls: Vec<String>,
33 #[serde(default = "default_nullifier_oracle_threshold")]
35 nullifier_oracle_threshold: usize,
36 #[serde(default)]
38 zkey_cache_dir: Option<PathBuf>,
39}
40
41impl Config {
42 pub fn new(
48 rpc_url: Option<String>,
49 chain_id: u64,
50 registry_address: Address,
51 indexer_url: String,
52 gateway_url: String,
53 nullifier_oracle_urls: Vec<String>,
54 nullifier_oracle_threshold: usize,
55 ) -> Result<Self, PrimitiveError> {
56 let rpc_url = rpc_url
57 .map(|url| {
58 Url::parse(&url).map_err(|e| PrimitiveError::InvalidInput {
59 reason: e.to_string(),
60 attribute: "rpc_url".to_string(),
61 })
62 })
63 .transpose()?;
64
65 Ok(Self {
66 rpc_url,
67 chain_id,
68 registry_address,
69 indexer_url,
70 gateway_url,
71 nullifier_oracle_urls,
72 nullifier_oracle_threshold,
73 zkey_cache_dir: None,
74 })
75 }
76
77 pub fn from_json(json_str: &str) -> Result<Self, PrimitiveError> {
82 serde_json::from_str(json_str)
83 .map_err(|e| PrimitiveError::Serialization(format!("failed to parse config: {e}")))
84 }
85
86 #[must_use]
88 pub const fn rpc_url(&self) -> Option<&Url> {
89 self.rpc_url.as_ref()
90 }
91
92 #[must_use]
94 pub const fn chain_id(&self) -> u64 {
95 self.chain_id
96 }
97
98 #[must_use]
100 pub const fn registry_address(&self) -> &Address {
101 &self.registry_address
102 }
103
104 #[must_use]
106 pub const fn indexer_url(&self) -> &String {
107 &self.indexer_url
108 }
109
110 #[must_use]
113 pub const fn gateway_url(&self) -> &String {
114 &self.gateway_url
115 }
116
117 #[must_use]
119 pub const fn nullifier_oracle_urls(&self) -> &Vec<String> {
120 &self.nullifier_oracle_urls
121 }
122
123 #[must_use]
125 pub const fn nullifier_oracle_threshold(&self) -> usize {
126 self.nullifier_oracle_threshold
127 }
128
129 #[must_use]
131 pub fn zkey_cache_dir(&self) -> Option<&Path> {
132 self.zkey_cache_dir.as_deref()
133 }
134
135 #[must_use]
137 pub fn with_zkey_cache_dir(mut self, zkey_cache_dir: impl Into<PathBuf>) -> Self {
138 self.zkey_cache_dir = Some(zkey_cache_dir.into());
139 self
140 }
141}