Skip to main content

smplx_sdk/provider/
network.rs

1use std::str::FromStr;
2
3use simplicityhl::simplicity::elements;
4use simplicityhl::simplicity::hashes::{Hash, sha256};
5
6use crate::constants::{LIQUID_DEFAULT_REGTEST_ASSET_STR, LIQUID_POLICY_ASSET_STR, LIQUID_TESTNET_POLICY_ASSET_STR};
7
8/// The default Bitcoin `AssetId` used on Liquid testnet.
9pub static LIQUID_TESTNET_BITCOIN_ASSET: std::sync::LazyLock<elements::AssetId> = std::sync::LazyLock::new(|| {
10    elements::AssetId::from_inner(sha256::Midstate([
11        0x49, 0x9a, 0x81, 0x85, 0x45, 0xf6, 0xba, 0xe3, 0x9f, 0xc0, 0x3b, 0x63, 0x7f, 0x2a, 0x4e, 0x1e, 0x64, 0xe5,
12        0x90, 0xca, 0xc1, 0xbc, 0x3a, 0x6f, 0x6d, 0x71, 0xaa, 0x44, 0x43, 0x65, 0x4c, 0x14,
13    ]))
14});
15
16/// The genesis block hash for Liquid mainnet.
17pub static LIQUID_MAINNET_GENESIS: std::sync::LazyLock<elements::BlockHash> = std::sync::LazyLock::new(|| {
18    elements::BlockHash::from_byte_array([
19        0x03, 0x60, 0x20, 0x8a, 0x88, 0x96, 0x92, 0x37, 0x2c, 0x8d, 0x68, 0xb0, 0x84, 0xa6, 0x2e, 0xfd, 0xf6, 0x0e,
20        0xa1, 0xa3, 0x59, 0xa0, 0x4c, 0x94, 0xb2, 0x0d, 0x22, 0x36, 0x58, 0x27, 0x66, 0x14,
21    ])
22});
23
24/// The genesis block hash for Liquid testnet.
25pub static LIQUID_TESTNET_GENESIS: std::sync::LazyLock<elements::BlockHash> = std::sync::LazyLock::new(|| {
26    elements::BlockHash::from_byte_array([
27        0xc1, 0xb1, 0x6a, 0xe2, 0x4f, 0x24, 0x23, 0xae, 0xa2, 0xea, 0x34, 0x55, 0x22, 0x92, 0x79, 0x3b, 0x5b, 0x5e,
28        0x82, 0x99, 0x9a, 0x1e, 0xed, 0x81, 0xd5, 0x6a, 0xee, 0x52, 0x8e, 0xda, 0x71, 0xa7,
29    ])
30});
31
32/// The genesis block hash for Elements regtest environments.
33pub static LIQUID_REGTEST_GENESIS: std::sync::LazyLock<elements::BlockHash> = std::sync::LazyLock::new(|| {
34    elements::BlockHash::from_byte_array([
35        0x21, 0xca, 0xb1, 0xe5, 0xda, 0x47, 0x18, 0xea, 0x14, 0x0d, 0x97, 0x16, 0x93, 0x17, 0x02, 0x42, 0x2f, 0x0e,
36        0x6a, 0xd9, 0x15, 0xc8, 0xd9, 0xb5, 0x83, 0xca, 0xc2, 0x70, 0x6b, 0x2a, 0x90, 0x00,
37    ])
38});
39
40/// Represents the target network configuration for Simplicity interactions.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
42pub enum SimplicityNetwork {
43    /// Liquid mainnet.
44    Liquid,
45    /// Liquid testnet.
46    LiquidTestnet,
47    /// Local Elements Regtest environment.
48    ElementsRegtest {
49        /// Regtest mode `AssetId`, which is used as a default policy asset locally.
50        policy_asset: elements::AssetId,
51    },
52}
53
54impl SimplicityNetwork {
55    /// Creates a default Elements Regtest configuration.
56    ///
57    /// # Panics
58    /// This function will panic if the provided `LIQUID_DEFAULT_REGTEST_ASSET_STR` cannot be parsed.
59    #[must_use]
60    pub fn default_regtest() -> Self {
61        let policy_asset = elements::AssetId::from_str(LIQUID_DEFAULT_REGTEST_ASSET_STR).unwrap();
62        Self::ElementsRegtest { policy_asset }
63    }
64
65    /// Returns the policy `AssetId` associated with the current network.
66    ///
67    /// # Panics
68    /// This function will panic if the provided `LIQUID_DEFAULT_REGTEST_ASSET_STR` cannot be parsed.
69    #[must_use]
70    pub fn policy_asset(&self) -> elements::AssetId {
71        match self {
72            Self::Liquid => elements::AssetId::from_str(LIQUID_POLICY_ASSET_STR).unwrap(),
73            Self::LiquidTestnet => elements::AssetId::from_str(LIQUID_TESTNET_POLICY_ASSET_STR).unwrap(),
74            Self::ElementsRegtest { policy_asset } => *policy_asset,
75        }
76    }
77
78    /// Returns the genesis block hash for the network variant.
79    #[must_use]
80    pub fn genesis_block_hash(&self) -> elements::BlockHash {
81        match self {
82            Self::Liquid => *LIQUID_MAINNET_GENESIS,
83            Self::LiquidTestnet => *LIQUID_TESTNET_GENESIS,
84            Self::ElementsRegtest { .. } => *LIQUID_REGTEST_GENESIS,
85        }
86    }
87
88    /// Determines if the current network is the mainnet (Liquid).
89    #[must_use]
90    pub fn is_mainnet(&self) -> bool {
91        self == &Self::Liquid
92    }
93
94    /// Returns the address parameters associated with the current enum variant.
95    #[must_use]
96    pub const fn address_params(&self) -> &'static elements::AddressParams {
97        match self {
98            Self::Liquid => &elements::AddressParams::LIQUID,
99            Self::LiquidTestnet => &elements::AddressParams::LIQUID_TESTNET,
100            Self::ElementsRegtest { .. } => &elements::AddressParams::ELEMENTS,
101        }
102    }
103}