diff --git a/web/explorer/explorer-server/src/blockchain.rs b/web/explorer/explorer-server/src/blockchain.rs --- a/web/explorer/explorer-server/src/blockchain.rs +++ b/web/explorer/explorer-server/src/blockchain.rs @@ -4,6 +4,8 @@ }; use bitcoinsuite_error::Result; +use crate::chain::Chain; + pub fn to_be_hex(slice: &[u8]) -> String { let mut vec = slice.to_vec(); vec.reverse(); @@ -61,7 +63,7 @@ } } -pub fn to_legacy_address(cash_address: &CashAddress) -> String { +pub fn to_legacy_address(cash_address: &CashAddress, chain: &Chain) -> String { use bitcoin::{ hashes::{hash160, Hash}, PubkeyHash, ScriptHash, @@ -76,9 +78,13 @@ bitcoin::Script::new_p2sh(&ScriptHash::from_hash(hash)) } }; - let address = - bitcoin::Address::from_script(&script, bitcoin::Network::Bitcoin) - .expect("Invalid address"); + let legacy_chain = match chain { + Chain::Mainnet => bitcoin::Network::Bitcoin, + Chain::Testnet => bitcoin::Network::Testnet, + Chain::Regtest => bitcoin::Network::Regtest, + }; + let address = bitcoin::Address::from_script(&script, legacy_chain) + .expect("Invalid address"); address.to_string() } diff --git a/web/explorer/explorer-server/src/chain.rs b/web/explorer/explorer-server/src/chain.rs --- a/web/explorer/explorer-server/src/chain.rs +++ b/web/explorer/explorer-server/src/chain.rs @@ -10,7 +10,7 @@ InvalidChain(String), } -#[derive(Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq)] pub enum Chain { Mainnet, Testnet, diff --git a/web/explorer/explorer-server/src/server.rs b/web/explorer/explorer-server/src/server.rs --- a/web/explorer/explorer-server/src/server.rs +++ b/web/explorer/explorer-server/src/server.rs @@ -42,6 +42,7 @@ pub struct Server { chronik: ChronikClient, base_dir: PathBuf, + chain: Chain, satoshi_addr_prefix: &'static str, tokens_addr_prefix: &'static str, token_icon_url: &'static str, @@ -58,6 +59,7 @@ Ok(Server { chronik, base_dir, + chain: chain.clone(), satoshi_addr_prefix: match chain { Chain::Mainnet => "ecash", Chain::Testnet => "ectest", @@ -466,7 +468,7 @@ let sats_address = address.with_prefix(self.satoshi_addr_prefix); let token_address = address.with_prefix(self.tokens_addr_prefix); - let legacy_address = to_legacy_address(&address); + let legacy_address = to_legacy_address(&address, &self.chain); let sats_address = sats_address.as_str(); let token_address = token_address.as_str();