Skip to main content

world_id_primitives/
config.rs

1use 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/// Global configuration to interact with the different components of the Protocol.
15///
16/// Used by Authenticators and RPs.
17#[derive(Clone, Debug, Serialize, Deserialize)]
18pub struct Config {
19    /// A fully qualified RPC domain to perform on-chain call functions.
20    ///
21    /// When not available, other services will be used (e.g. the indexer to fetch packed account index).
22    rpc_url: Option<Url>,
23    /// The chain ID of the network where the `WorldIDRegistry` contract is deployed.
24    chain_id: u64,
25    /// The address of the `WorldIDRegistry` contract
26    registry_address: Address,
27    /// Base URL of a deployed `world-id-indexer`. Used to fetch inclusion proofs from the `WorldIDRegistry`.
28    indexer_url: String,
29    /// Base URL of a deployed `world-id-gateway`. Used to submit management operations on authenticators.
30    gateway_url: String,
31    /// The Base URLs of all Nullifier Oracles to use
32    nullifier_oracle_urls: Vec<String>,
33    /// Minimum number of Nullifier Oracle responses required to build a nullifier.
34    #[serde(default = "default_nullifier_oracle_threshold")]
35    nullifier_oracle_threshold: usize,
36    /// Optional directory to cache uncompressed circuit zkeys.
37    #[serde(default)]
38    zkey_cache_dir: Option<PathBuf>,
39}
40
41impl Config {
42    /// Instantiates a new configuration.
43    ///
44    /// # Errors
45    ///
46    /// Returns an error if the `rpc_url` is invalid.
47    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    /// Loads a configuration from JSON.
78    ///
79    /// # Errors
80    /// Will error if the JSON is not valid.
81    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    /// The RPC endpoint to perform RPC calls.
87    #[must_use]
88    pub const fn rpc_url(&self) -> Option<&Url> {
89        self.rpc_url.as_ref()
90    }
91
92    /// The chain ID of the network where the `WorldIDRegistry` contract is deployed.
93    #[must_use]
94    pub const fn chain_id(&self) -> u64 {
95        self.chain_id
96    }
97
98    /// The address of the `WorldIDRegistry` contract.
99    #[must_use]
100    pub const fn registry_address(&self) -> &Address {
101        &self.registry_address
102    }
103
104    /// The URL of the `world-id-indexer` service to use. The indexer is used to fetch inclusion proofs from the `WorldIDRegistry` contract.
105    #[must_use]
106    pub const fn indexer_url(&self) -> &String {
107        &self.indexer_url
108    }
109
110    /// The URL of the `world-id-gateway` service to use. The gateway is used to perform operations on the `WorldIDRegistry` contract
111    /// without leaking a wallet address.
112    #[must_use]
113    pub const fn gateway_url(&self) -> &String {
114        &self.gateway_url
115    }
116
117    /// The list of URLs of all and each node of the Nullifier Oracle.
118    #[must_use]
119    pub const fn nullifier_oracle_urls(&self) -> &Vec<String> {
120        &self.nullifier_oracle_urls
121    }
122
123    /// The minimum number of Nullifier Oracle responses required to build a nullifier.
124    #[must_use]
125    pub const fn nullifier_oracle_threshold(&self) -> usize {
126        self.nullifier_oracle_threshold
127    }
128
129    /// Optional directory to cache uncompressed circuit zkeys.
130    #[must_use]
131    pub fn zkey_cache_dir(&self) -> Option<&Path> {
132        self.zkey_cache_dir.as_deref()
133    }
134
135    /// Sets the directory used to cache uncompressed circuit zkeys.
136    #[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}