world_id_primitives/config.rs
1use serde::{Deserialize, Serialize};
2
3use alloy_primitives::Address;
4use url::Url;
5
6use crate::PrimitiveError;
7
8const fn default_nullifier_oracle_threshold() -> usize {
9 2
10}
11
12/// Global configuration to interact with the different components of the Protocol.
13///
14/// Used by Authenticators and RPs.
15#[derive(Clone, Debug, Serialize, Deserialize)]
16pub struct Config {
17 /// A fully qualified RPC domain to perform on-chain call functions.
18 ///
19 /// When not available, other services will be used (e.g. the indexer to fetch packed account index).
20 rpc_url: Option<Url>,
21 /// The chain ID of the network where the `WorldIDRegistry` contract is deployed.
22 chain_id: u64,
23 /// The address of the `WorldIDRegistry` contract
24 registry_address: Address,
25 /// Base URL of a deployed `world-id-indexer`. Used to fetch inclusion proofs from the `WorldIDRegistry`.
26 indexer_url: String,
27 /// Base URL of a deployed `world-id-gateway`. Used to submit management operations on authenticators.
28 gateway_url: String,
29 /// The Base URLs of all Nullifier Oracles to use
30 nullifier_oracle_urls: Vec<String>,
31 /// Minimum number of Nullifier Oracle responses required to build a nullifier.
32 #[serde(default = "default_nullifier_oracle_threshold")]
33 nullifier_oracle_threshold: usize,
34}
35
36impl Config {
37 /// Instantiates a new configuration.
38 ///
39 /// # Errors
40 ///
41 /// Returns an error if the `rpc_url` is invalid.
42 pub fn new(
43 rpc_url: Option<String>,
44 chain_id: u64,
45 registry_address: Address,
46 indexer_url: String,
47 gateway_url: String,
48 nullifier_oracle_urls: Vec<String>,
49 nullifier_oracle_threshold: usize,
50 ) -> Result<Self, PrimitiveError> {
51 let rpc_url = rpc_url
52 .map(|url| {
53 Url::parse(&url).map_err(|e| PrimitiveError::InvalidInput {
54 reason: e.to_string(),
55 attribute: "rpc_url".to_string(),
56 })
57 })
58 .transpose()?;
59
60 Ok(Self {
61 rpc_url,
62 chain_id,
63 registry_address,
64 indexer_url,
65 gateway_url,
66 nullifier_oracle_urls,
67 nullifier_oracle_threshold,
68 })
69 }
70
71 /// Loads a configuration from JSON.
72 ///
73 /// # Errors
74 /// Will error if the JSON is not valid.
75 pub fn from_json(json_str: &str) -> Result<Self, PrimitiveError> {
76 serde_json::from_str(json_str)
77 .map_err(|e| PrimitiveError::Serialization(format!("failed to parse config: {e}")))
78 }
79
80 /// The RPC endpoint to perform RPC calls.
81 #[must_use]
82 pub const fn rpc_url(&self) -> Option<&Url> {
83 self.rpc_url.as_ref()
84 }
85
86 /// The chain ID of the network where the `WorldIDRegistry` contract is deployed.
87 #[must_use]
88 pub const fn chain_id(&self) -> u64 {
89 self.chain_id
90 }
91
92 /// The address of the `WorldIDRegistry` contract.
93 #[must_use]
94 pub const fn registry_address(&self) -> &Address {
95 &self.registry_address
96 }
97
98 /// The URL of the `world-id-indexer` service to use. The indexer is used to fetch inclusion proofs from the `WorldIDRegistry` contract.
99 #[must_use]
100 pub const fn indexer_url(&self) -> &String {
101 &self.indexer_url
102 }
103
104 /// The URL of the `world-id-gateway` service to use. The gateway is used to perform operations on the `WorldIDRegistry` contract
105 /// without leaking a wallet address.
106 #[must_use]
107 pub const fn gateway_url(&self) -> &String {
108 &self.gateway_url
109 }
110
111 /// The list of URLs of all and each node of the Nullifier Oracle.
112 #[must_use]
113 pub const fn nullifier_oracle_urls(&self) -> &Vec<String> {
114 &self.nullifier_oracle_urls
115 }
116
117 /// The minimum number of Nullifier Oracle responses required to build a nullifier.
118 #[must_use]
119 pub const fn nullifier_oracle_threshold(&self) -> usize {
120 self.nullifier_oracle_threshold
121 }
122}