Skip to main content

walletkit_core/
defaults.rs

1use alloy_primitives::{address, Address};
2use world_id_core::primitives::Config;
3
4use crate::{error::WalletKitError, Environment, Region};
5
6/// The World ID Registry contract address on World Chain Mainnet.
7pub static WORLD_ID_REGISTRY: Address =
8    address!("0x8556d07D75025f286fe757C7EeEceC40D54FA16D");
9
10const OPRF_NODE_COUNT: usize = 5;
11
12/// Generates the list of OPRF node URLs for a given region and environment.
13fn oprf_node_urls(region: Region, environment: &Environment) -> Vec<String> {
14    let env_segment = match environment {
15        Environment::Staging => ".staging",
16        Environment::Production => "",
17    };
18
19    (0..OPRF_NODE_COUNT)
20        .map(|i| {
21            format!("https://node{i}.{region}{env_segment}.world.oprf.taceo.network")
22        })
23        .collect()
24}
25
26fn indexer_url(region: Region, environment: &Environment) -> String {
27    let domain = match environment {
28        Environment::Staging => "worldcoin.dev",
29        Environment::Production => "world.org",
30    };
31    format!("https://indexer.{region}.id-infra.{domain}")
32}
33
34/// Build a [`Config`] from well-known defaults for a given [`Environment`].
35pub trait DefaultConfig {
36    /// Returns a config populated with the default URLs and addresses for the given environment.
37    ///
38    /// # Errors
39    ///
40    /// Returns [`WalletKitError`] if the configuration cannot be constructed (e.g. invalid RPC URL).
41    fn from_environment(
42        environment: &Environment,
43        rpc_url: Option<String>,
44        region: Option<Region>,
45    ) -> Result<Self, WalletKitError>
46    where
47        Self: Sized;
48}
49
50impl DefaultConfig for Config {
51    fn from_environment(
52        environment: &Environment,
53        rpc_url: Option<String>,
54        region: Option<Region>,
55    ) -> Result<Self, WalletKitError> {
56        let region = region.unwrap_or_default();
57
58        match environment {
59            Environment::Staging => Self::new(
60                rpc_url,
61                480, // Staging also runs on World Chain Mainnet by default
62                WORLD_ID_REGISTRY,
63                indexer_url(region, environment),
64                "https://world-id-gateway.stage-crypto.worldcoin.org".to_string(),
65                oprf_node_urls(region, environment),
66                3,
67            )
68            .map_err(WalletKitError::from),
69
70            Environment::Production => todo!("There is no production environment yet"),
71        }
72    }
73}