Skip to main content

urma_runtime/
endpoints.rs

1use crate::error::{Error, ensure};
2use urma_chain::observation::Chain;
3
4#[derive(Clone, Debug)]
5pub enum PublicEndpoint {
6    Rpc(String),
7    Esplora(String),
8}
9
10impl PublicEndpoint {
11    pub fn url(&self) -> &str {
12        match self {
13            Self::Rpc(url) | Self::Esplora(url) => url,
14        }
15    }
16
17    pub fn validate(&self) -> Result<(), Error> {
18        let url = url::Url::parse(self.url())?;
19        ensure!(
20            url.scheme() == "https"
21                && url.username().is_empty()
22                && url.password().iter().count() == 0
23                && url.query().iter().count() == 0
24                && url.fragment().iter().count() == 0,
25            "public sources require HTTPS without embedded credentials"
26        );
27        Ok(())
28    }
29}
30
31pub fn defaults(chain: Chain) -> Vec<PublicEndpoint> {
32    match chain {
33        Chain::LitecoinMainnet => vec![
34            PublicEndpoint::Esplora("https://litecoinspace.org/api".into()),
35            PublicEndpoint::Rpc("https://litecoin-mainnet.gateway.tatum.io".into()),
36        ],
37        Chain::LitecoinTestnet => vec![
38            PublicEndpoint::Esplora("https://litecoinspace.org/testnet/api".into()),
39            PublicEndpoint::Esplora("https://testnetscan.com/ltc-testnet/api".into()),
40            PublicEndpoint::Rpc("https://litecoin-testnet.gateway.tatum.io".into()),
41        ],
42        Chain::BitcoinTestnet4 => vec![PublicEndpoint::Esplora(
43            "https://mempool.space/testnet4/api".into(),
44        )],
45        Chain::BitcoinRegtest => Vec::new(),
46    }
47}