From 841dd1ea3a95e05c325923606cc46787c46005d2 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Sat, 8 Apr 2023 21:24:17 -0500 Subject: [PATCH] Convert Network to and from Currency --- lightning-invoice/src/lib.rs | 38 +++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) 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) -- 2.39.5