Merge pull request #2127 from TheBlueMatt/2023-03-payment-metadata
[rust-lightning] / lightning-invoice / src / lib.rs
index f1d3d973acf00e3ebbb0fa4e5d376b2f3c59c56c..0923fcc1756d4fd05305ab53b2653d08539949f9 100644 (file)
@@ -394,6 +394,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
@@ -1368,14 +1391,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() }
@@ -1388,7 +1403,7 @@ impl Invoice {
                                }
                        };
 
-                       Address { payload, network }
+                       Address { payload, network: self.network() }
                }).collect()
        }
 
@@ -1409,6 +1424,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)
@@ -1694,7 +1716,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)
        }