Skip to main content

smplx_sdk_test/provider/
network.rs

1use std::str::FromStr;
2
3use simplicityhl::simplicity::elements;
4use simplicityhl::simplicity::hashes::{Hash, sha256};
5
6use elements_miniscript::bitcoin::NetworkKind;
7
8use crate::constants::{LIQUID_DEFAULT_REGTEST_ASSET_STR, LIQUID_POLICY_ASSET_STR, LIQUID_TESTNET_POLICY_ASSET_STR};
9
10/// The default Bitcoin `AssetId` used on Liquid testnet.
11pub static LIQUID_TESTNET_BITCOIN_ASSET: std::sync::LazyLock<elements::AssetId> = std::sync::LazyLock::new(|| {
12    elements::AssetId::from_inner(sha256::Midstate([
13        0x49, 0x9a, 0x81, 0x85, 0x45, 0xf6, 0xba, 0xe3, 0x9f, 0xc0, 0x3b, 0x63, 0x7f, 0x2a, 0x4e, 0x1e, 0x64, 0xe5,
14        0x90, 0xca, 0xc1, 0xbc, 0x3a, 0x6f, 0x6d, 0x71, 0xaa, 0x44, 0x43, 0x65, 0x4c, 0x14,
15    ]))
16});
17
18/// The genesis block hash for Liquid mainnet.
19pub static LIQUID_MAINNET_GENESIS: std::sync::LazyLock<elements::BlockHash> = std::sync::LazyLock::new(|| {
20    elements::BlockHash::from_byte_array([
21        0x03, 0x60, 0x20, 0x8a, 0x88, 0x96, 0x92, 0x37, 0x2c, 0x8d, 0x68, 0xb0, 0x84, 0xa6, 0x2e, 0xfd, 0xf6, 0x0e,
22        0xa1, 0xa3, 0x59, 0xa0, 0x4c, 0x94, 0xb2, 0x0d, 0x22, 0x36, 0x58, 0x27, 0x66, 0x14,
23    ])
24});
25
26/// The genesis block hash for Liquid testnet.
27pub static LIQUID_TESTNET_GENESIS: std::sync::LazyLock<elements::BlockHash> = std::sync::LazyLock::new(|| {
28    elements::BlockHash::from_byte_array([
29        0xc1, 0xb1, 0x6a, 0xe2, 0x4f, 0x24, 0x23, 0xae, 0xa2, 0xea, 0x34, 0x55, 0x22, 0x92, 0x79, 0x3b, 0x5b, 0x5e,
30        0x82, 0x99, 0x9a, 0x1e, 0xed, 0x81, 0xd5, 0x6a, 0xee, 0x52, 0x8e, 0xda, 0x71, 0xa7,
31    ])
32});
33
34/// The genesis block hash for Elements regtest environments.
35pub static LIQUID_REGTEST_GENESIS: std::sync::LazyLock<elements::BlockHash> = std::sync::LazyLock::new(|| {
36    elements::BlockHash::from_byte_array([
37        0x21, 0xca, 0xb1, 0xe5, 0xda, 0x47, 0x18, 0xea, 0x14, 0x0d, 0x97, 0x16, 0x93, 0x17, 0x02, 0x42, 0x2f, 0x0e,
38        0x6a, 0xd9, 0x15, 0xc8, 0xd9, 0xb5, 0x83, 0xca, 0xc2, 0x70, 0x6b, 0x2a, 0x90, 0x00,
39    ])
40});
41
42/// Represents the target network configuration for Simplicity interactions.
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
44pub enum SimplicityNetwork {
45    /// Liquid mainnet.
46    Liquid,
47    /// Liquid testnet.
48    LiquidTestnet,
49    /// Local Elements Regtest environment.
50    ElementsRegtest {
51        /// Regtest mode `AssetId`, which is used as a default policy asset locally.
52        policy_asset: elements::AssetId,
53    },
54    /// Custom Elements environment whose genesis is derived from its chain parameters.
55    ElementsCustom {
56        /// Custom network policy asset.
57        policy_asset: elements::AssetId,
58        /// Custom network genesis block hash.
59        genesis_hash: elements::BlockHash,
60    },
61}
62
63impl SimplicityNetwork {
64    /// Creates a default Elements Regtest configuration.
65    ///
66    /// # Panics
67    /// This function will panic if the provided `LIQUID_DEFAULT_REGTEST_ASSET_STR` cannot be parsed.
68    #[must_use]
69    pub fn default_regtest() -> Self {
70        let policy_asset = elements::AssetId::from_str(LIQUID_DEFAULT_REGTEST_ASSET_STR).unwrap();
71        Self::ElementsRegtest { policy_asset }
72    }
73
74    /// Returns the policy `AssetId` associated with the current network.
75    ///
76    /// # Panics
77    /// This function will panic if the provided `LIQUID_DEFAULT_REGTEST_ASSET_STR` cannot be parsed.
78    #[must_use]
79    pub fn policy_asset(&self) -> elements::AssetId {
80        match self {
81            Self::Liquid => elements::AssetId::from_str(LIQUID_POLICY_ASSET_STR).unwrap(),
82            Self::LiquidTestnet => elements::AssetId::from_str(LIQUID_TESTNET_POLICY_ASSET_STR).unwrap(),
83            Self::ElementsRegtest { policy_asset } | Self::ElementsCustom { policy_asset, .. } => *policy_asset,
84        }
85    }
86
87    /// Returns the genesis block hash for the network variant.
88    #[must_use]
89    pub fn genesis_block_hash(&self) -> elements::BlockHash {
90        match self {
91            Self::Liquid => *LIQUID_MAINNET_GENESIS,
92            Self::LiquidTestnet => *LIQUID_TESTNET_GENESIS,
93            Self::ElementsRegtest { .. } => *LIQUID_REGTEST_GENESIS,
94            Self::ElementsCustom { genesis_hash, .. } => *genesis_hash,
95        }
96    }
97
98    /// Determines if the current network is the mainnet (Liquid).
99    #[must_use]
100    pub fn is_mainnet(&self) -> bool {
101        self == &Self::Liquid
102    }
103
104    /// Returns the address parameters associated with the current enum variant.
105    #[must_use]
106    pub const fn address_params(&self) -> &'static elements::AddressParams {
107        match self {
108            Self::Liquid => &elements::AddressParams::LIQUID,
109            Self::LiquidTestnet => &elements::AddressParams::LIQUID_TESTNET,
110            Self::ElementsRegtest { .. } | Self::ElementsCustom { .. } => &elements::AddressParams::ELEMENTS,
111        }
112    }
113}
114
115impl From<SimplicityNetwork> for NetworkKind {
116    fn from(value: SimplicityNetwork) -> Self {
117        (&value).into()
118    }
119}
120
121impl From<&SimplicityNetwork> for NetworkKind {
122    fn from(value: &SimplicityNetwork) -> Self {
123        if value.is_mainnet() {
124            NetworkKind::Main
125        } else {
126            NetworkKind::Test
127        }
128    }
129}
130
131#[cfg(test)]
132mod tests {
133    use super::*;
134
135    #[test]
136    fn supported_networks_expose_policy_and_address_metadata() {
137        let liquid = SimplicityNetwork::Liquid;
138        let testnet = SimplicityNetwork::LiquidTestnet;
139        let regtest = SimplicityNetwork::default_regtest();
140
141        assert!(liquid.is_mainnet());
142        assert!(!testnet.is_mainnet());
143        assert!(!regtest.is_mainnet());
144
145        for (network, asset, genesis, params) in [
146            (
147                liquid,
148                "6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d",
149                "1466275836220db2944ca059a3a10ef6fd2ea684b0688d2c379296888a206003",
150                &elements::AddressParams::LIQUID,
151            ),
152            (
153                testnet,
154                "144c654344aa716d6f3abcc1ca90e5641e4e2a7f633bc09fe3baf64585819a49",
155                "a771da8e52ee6ad581ed1e9a99825e5b3b7992225534eaa2ae23244fe26ab1c1",
156                &elements::AddressParams::LIQUID_TESTNET,
157            ),
158            (
159                regtest,
160                "5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225",
161                "00902a6b70c2ca83b5d9c815d96a0e2f4202179316970d14ea1847dae5b1ca21",
162                &elements::AddressParams::ELEMENTS,
163            ),
164        ] {
165            assert_eq!(network.policy_asset().to_string(), asset);
166            assert_eq!(network.genesis_block_hash().to_string(), genesis);
167            assert_eq!(network.address_params(), params);
168        }
169
170        assert_eq!(NetworkKind::from(liquid), NetworkKind::Main);
171        assert_eq!(NetworkKind::from(&testnet), NetworkKind::Test);
172        assert_eq!(NetworkKind::from(regtest), NetworkKind::Test);
173    }
174}