Merge pull request #2196 from wpaulino/ci-ignore-master-cancel-prev
[rust-lightning] / lightning-invoice / src / lib.rs
index 097bdc458ce5391090f7c87a1dde32e1782728a5..94a93fe62b7f1d160c5f1dc0c89ba66b56bf34c1 100644 (file)
@@ -390,6 +390,29 @@ pub enum Currency {
        Signet,
 }
 
+impl From<Network> 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<Currency> 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<Address> {
                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,13 @@ impl Invoice {
                self.signed_invoice.currency()
        }
 
+       /// Returns the network for which the invoice was issued
+       ///
+       /// This is not exported to bindings users, see [`Self::currency`] instead.
+       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<u64> {
                self.signed_invoice.amount_pico_btc().map(|v| v / 10)
@@ -1628,7 +1650,7 @@ impl<'de> Deserialize<'de> for Invoice {
        fn deserialize<D>(deserializer: D) -> Result<Invoice, D::Error> where D: Deserializer<'de> {
                let bolt11 = String::deserialize(deserializer)?
                        .parse::<Invoice>()
-                       .map_err(|e| D::Error::custom(format!("{:?}", e)))?;
+                       .map_err(|e| D::Error::custom(alloc::format!("{:?}", e)))?;
 
                Ok(bolt11)
        }