diff --git a/fedimint-core/src/encoding/btc.rs b/fedimint-core/src/encoding/btc.rs index e896fc8fd51..612df96e0db 100644 --- a/fedimint-core/src/encoding/btc.rs +++ b/fedimint-core/src/encoding/btc.rs @@ -6,6 +6,7 @@ use bitcoin::address::NetworkUnchecked; use bitcoin::hashes::Hash as BitcoinHash; use hex::{FromHex, ToHex}; use miniscript::{Descriptor, MiniscriptKey}; +use serde::{Deserialize, Serialize}; use super::{BufBitcoinReader, CountWrite, SimpleBitcoinRead}; use crate::bitcoin_migration::{bitcoin30_to_bitcoin32_network, bitcoin32_to_bitcoin30_address}; @@ -170,6 +171,29 @@ impl Decodable for bitcoin::Network { } } +#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] +pub struct NetworkSaneEncodingWrapper(pub bitcoin::Network); + +impl Encodable for NetworkSaneEncodingWrapper { + fn consensus_encode(&self, writer: &mut W) -> Result { + self.0.magic().to_bytes().consensus_encode(writer) + } +} + +impl Decodable for NetworkSaneEncodingWrapper { + fn consensus_decode( + d: &mut D, + modules: &ModuleDecoderRegistry, + ) -> Result { + Ok(Self( + bitcoin::Network::from_magic(bitcoin::p2p::Magic::from_bytes( + Decodable::consensus_decode(d, modules)?, + )) + .ok_or_else(|| DecodeError::new_custom(format_err!("Unknown network magic")))?, + )) + } +} + impl Encodable for bitcoin::Amount { fn consensus_encode(&self, writer: &mut W) -> Result { self.to_sat().consensus_encode(writer) diff --git a/fedimint-core/src/encoding/mod.rs b/fedimint-core/src/encoding/mod.rs index 60ccc2f627c..d15a16bc8bf 100644 --- a/fedimint-core/src/encoding/mod.rs +++ b/fedimint-core/src/encoding/mod.rs @@ -10,7 +10,7 @@ pub mod as_hex; mod bls12_381; -mod btc; +pub mod btc; mod secp256k1; mod threshold_crypto; diff --git a/gateway/ln-gateway/src/lib.rs b/gateway/ln-gateway/src/lib.rs index 5866f8243e0..5f3261febba 100644 --- a/gateway/ln-gateway/src/lib.rs +++ b/gateway/ln-gateway/src/lib.rs @@ -1872,14 +1872,14 @@ impl Gateway { ))))?; let ln_cfg: &fedimint_lnv2_common::config::LightningClientConfig = cfg.cast()?; - if ln_cfg.network != network { + if ln_cfg.network.0 != network { error!( "Federation {federation_id} runs on {} but this gateway supports {network}", - ln_cfg.network, + ln_cfg.network.0, ); return Err(AdminGatewayError::ClientCreationError(anyhow!(format!( "Unsupported network {}", - ln_cfg.network + ln_cfg.network.0 )))); } diff --git a/modules/fedimint-lnv2-client/src/lib.rs b/modules/fedimint-lnv2-client/src/lib.rs index 56e6c4bbff8..10938b50bbb 100644 --- a/modules/fedimint-lnv2-client/src/lib.rs +++ b/modules/fedimint-lnv2-client/src/lib.rs @@ -497,10 +497,10 @@ impl LightningClientModule { return Err(SendPaymentError::InvoiceExpired); } - if self.cfg.network != invoice.currency().into() { + if self.cfg.network.0 != invoice.currency().into() { return Err(SendPaymentError::WrongCurrency { invoice_currency: invoice.currency(), - federation_currency: self.cfg.network.into(), + federation_currency: self.cfg.network.0.into(), }); } diff --git a/modules/fedimint-lnv2-common/src/config.rs b/modules/fedimint-lnv2-common/src/config.rs index eeadb1c4cbf..9cb1796d205 100644 --- a/modules/fedimint-lnv2-common/src/config.rs +++ b/modules/fedimint-lnv2-common/src/config.rs @@ -2,6 +2,7 @@ use std::collections::BTreeMap; pub use bitcoin::Network; use fedimint_core::core::ModuleKind; +use fedimint_core::encoding::btc::NetworkSaneEncodingWrapper; use fedimint_core::encoding::{Decodable, Encodable}; use fedimint_core::envs::BitcoinRpcConfig; use fedimint_core::{plugin_types_trait_impl_config, Amount, PeerId}; @@ -58,7 +59,7 @@ pub struct LightningConfigConsensus { pub tpe_agg_pk: AggregatePublicKey, pub tpe_pks: BTreeMap, pub fee_consensus: FeeConsensus, - pub network: Network, + pub network: NetworkSaneEncodingWrapper, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -71,7 +72,7 @@ pub struct LightningClientConfig { pub tpe_agg_pk: AggregatePublicKey, pub tpe_pks: BTreeMap, pub fee_consensus: FeeConsensus, - pub network: Network, + pub network: NetworkSaneEncodingWrapper, } impl std::fmt::Display for LightningClientConfig { @@ -188,7 +189,7 @@ fn migrate_config_consensus( }) .collect(), fee_consensus: FeeConsensus::new(1000).expect("Relative fee is within range"), - network: config.network, + network: NetworkSaneEncodingWrapper(config.network), } } diff --git a/modules/fedimint-lnv2-server/src/lib.rs b/modules/fedimint-lnv2-server/src/lib.rs index 93bfc232a14..7757c4d5f5f 100644 --- a/modules/fedimint-lnv2-server/src/lib.rs +++ b/modules/fedimint-lnv2-server/src/lib.rs @@ -16,6 +16,7 @@ use fedimint_core::config::{ }; use fedimint_core::core::ModuleInstanceId; use fedimint_core::db::{Database, DatabaseTransaction, IDatabaseTransactionOpsCoreTyped}; +use fedimint_core::encoding::btc::NetworkSaneEncodingWrapper; use fedimint_core::module::audit::Audit; use fedimint_core::module::{ api_endpoint, ApiEndpoint, ApiVersion, CoreConsensusVersion, InputMeta, ModuleConsensusVersion, @@ -205,7 +206,7 @@ impl ServerModuleInit for LightningInit { tpe_agg_pk, tpe_pks: tpe_pks.clone(), fee_consensus: params.consensus.fee_consensus.clone(), - network: params.consensus.network, + network: NetworkSaneEncodingWrapper(params.consensus.network), }, private: LightningConfigPrivate { sk: sks[peer.to_usize()], @@ -247,7 +248,7 @@ impl ServerModuleInit for LightningInit { }) .collect(), fee_consensus: params.consensus.fee_consensus.clone(), - network: params.consensus.network, + network: NetworkSaneEncodingWrapper(params.consensus.network), }, private: LightningConfigPrivate { sk: SecretKeyShare(sk),