lwk/
network.rs

1use std::{fmt::Display, sync::Arc};
2
3use lwk_common::electrum_ssl::{LIQUID_SOCKET, LIQUID_TESTNET_SOCKET};
4
5use crate::{types::AssetId, ElectrumClient, EsploraClient, LwkError, TxBuilder};
6
7/// The network of the elements blockchain.
8#[derive(uniffi::Object, PartialEq, Eq, Debug, Clone, Copy)]
9#[uniffi::export(Display)]
10pub struct Network {
11    pub(crate) inner: lwk_wollet::ElementsNetwork,
12}
13
14impl Display for Network {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        write!(f, "{:?}", self.inner)
17    }
18}
19impl From<lwk_wollet::ElementsNetwork> for Network {
20    fn from(inner: lwk_wollet::ElementsNetwork) -> Self {
21        Self { inner }
22    }
23}
24
25impl From<Network> for lwk_wollet::ElementsNetwork {
26    fn from(value: Network) -> Self {
27        value.inner
28    }
29}
30
31impl From<&Network> for lwk_wollet::ElementsNetwork {
32    fn from(value: &Network) -> Self {
33        value.inner
34    }
35}
36
37impl From<&Network> for lwk_common::Network {
38    fn from(value: &Network) -> Self {
39        match value.inner {
40            lwk_wollet::ElementsNetwork::Liquid => lwk_common::Network::Liquid,
41            lwk_wollet::ElementsNetwork::LiquidTestnet => lwk_common::Network::TestnetLiquid,
42            lwk_wollet::ElementsNetwork::ElementsRegtest { .. } => {
43                lwk_common::Network::LocaltestLiquid
44            }
45        }
46    }
47}
48
49#[uniffi::export]
50impl Network {
51    /// Return the mainnet network
52    #[uniffi::constructor]
53    pub fn mainnet() -> Arc<Network> {
54        Arc::new(lwk_wollet::ElementsNetwork::Liquid.into())
55    }
56
57    /// Return the testnet network
58    #[uniffi::constructor]
59    pub fn testnet() -> Arc<Network> {
60        Arc::new(lwk_wollet::ElementsNetwork::LiquidTestnet.into())
61    }
62
63    /// Return the regtest network with the given policy asset
64    #[uniffi::constructor]
65    pub fn regtest(policy_asset: AssetId) -> Arc<Network> {
66        Arc::new(
67            lwk_wollet::ElementsNetwork::ElementsRegtest {
68                policy_asset: policy_asset.into(),
69            }
70            .into(),
71        )
72    }
73
74    /// Return the default regtest network with the default policy asset
75    #[uniffi::constructor]
76    pub fn regtest_default() -> Arc<Network> {
77        let policy_asset = "5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225";
78        let policy_asset: elements::AssetId = policy_asset.parse().expect("static");
79        Arc::new(lwk_wollet::ElementsNetwork::ElementsRegtest { policy_asset }.into())
80    }
81
82    /// Return the default electrum client for this network
83    pub fn default_electrum_client(&self) -> Result<Arc<ElectrumClient>, LwkError> {
84        let (url, validate_domain, tls) = match &self.inner {
85            lwk_wollet::ElementsNetwork::Liquid => (LIQUID_SOCKET, true, true),
86            lwk_wollet::ElementsNetwork::LiquidTestnet => (LIQUID_TESTNET_SOCKET, true, true),
87            lwk_wollet::ElementsNetwork::ElementsRegtest { policy_asset: _ } => {
88                ("127.0.0.1:50002", false, false)
89            }
90        };
91
92        ElectrumClient::new(url, tls, validate_domain)
93    }
94
95    /// Return the default esplora client for this network
96    pub fn default_esplora_client(&self) -> Result<Arc<EsploraClient>, LwkError> {
97        let url = match &self.inner {
98            lwk_wollet::ElementsNetwork::Liquid => "https://blockstream.info/liquid/api",
99            lwk_wollet::ElementsNetwork::LiquidTestnet => {
100                "https://blockstream.info/liquidtestnet/api"
101            }
102            lwk_wollet::ElementsNetwork::ElementsRegtest { policy_asset: _ } => "127.0.0.1:3000",
103        };
104
105        EsploraClient::new(url, &self.inner.into())
106    }
107
108    /// Return true if the network is the mainnet network
109    pub fn is_mainnet(&self) -> bool {
110        matches!(&self.inner, &lwk_wollet::ElementsNetwork::Liquid)
111    }
112
113    /// Return the policy asset (eg LBTC for mainnet) for this network
114    pub fn policy_asset(&self) -> AssetId {
115        self.inner.policy_asset().into()
116    }
117
118    /// Return a new `TxBuilder` for this network
119    pub fn tx_builder(&self) -> Arc<TxBuilder> {
120        Arc::new(TxBuilder::new(self))
121    }
122}