From: benthecarman Date: Sun, 9 Apr 2023 02:24:17 +0000 (-0500) Subject: Convert Network to and from Currency X-Git-Tag: v0.0.115~26^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=841dd1ea3a95e05c325923606cc46787c46005d2;p=rust-lightning Convert Network to and from Currency --- diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index 097bdc458..e9565f4ce 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -390,6 +390,29 @@ pub enum Currency { Signet, } +impl From for Currency { + fn from(network: Network) -> Self { + match network { + Network::Bitcoin => Currency::Bitcoin, + Network::Testnet => Currency::BitcoinTestnet, + Network::Regtest => Currency::Regtest, + Network::Signet => Currency::Signet, + } + } +} + +impl From for Network { + fn from(currency: Currency) -> Self { + match currency { + Currency::Bitcoin => Network::Bitcoin, + Currency::BitcoinTestnet => Network::Testnet, + Currency::Regtest => Network::Regtest, + Currency::Simnet => Network::Regtest, + Currency::Signet => Network::Signet, + } + } +} + /// Tagged field which may have an unknown tag /// /// This is not exported to bindings users as we don't currently support TaggedField @@ -1303,14 +1326,6 @@ impl Invoice { /// Returns a list of all fallback addresses as [`Address`]es pub fn fallback_addresses(&self) -> Vec
{ self.fallbacks().iter().map(|fallback| { - let network = match self.currency() { - Currency::Bitcoin => Network::Bitcoin, - Currency::BitcoinTestnet => Network::Testnet, - Currency::Regtest => Network::Regtest, - Currency::Simnet => Network::Regtest, - Currency::Signet => Network::Signet, - }; - let payload = match fallback { Fallback::SegWitProgram { version, program } => { Payload::WitnessProgram { version: *version, program: program.to_vec() } @@ -1323,7 +1338,7 @@ impl Invoice { } }; - Address { payload, network } + Address { payload, network: self.network() } }).collect() } @@ -1344,6 +1359,11 @@ impl Invoice { self.signed_invoice.currency() } + /// Returns the network for which the invoice was issued + pub fn network(&self) -> Network { + self.signed_invoice.currency().into() + } + /// Returns the amount if specified in the invoice as millisatoshis. pub fn amount_milli_satoshis(&self) -> Option { self.signed_invoice.amount_pico_btc().map(|v| v / 10)