Skip to content

Commit 3468669

Browse files
Merge pull request #2173 from benthecarman/convert-currency-to-netowkr
Convert Network to and from Currency
2 parents 74328bd + 841dd1e commit 3468669

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

lightning-invoice/src/lib.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,29 @@ pub enum Currency {
390390
Signet,
391391
}
392392

393+
impl From<Network> for Currency {
394+
fn from(network: Network) -> Self {
395+
match network {
396+
Network::Bitcoin => Currency::Bitcoin,
397+
Network::Testnet => Currency::BitcoinTestnet,
398+
Network::Regtest => Currency::Regtest,
399+
Network::Signet => Currency::Signet,
400+
}
401+
}
402+
}
403+
404+
impl From<Currency> for Network {
405+
fn from(currency: Currency) -> Self {
406+
match currency {
407+
Currency::Bitcoin => Network::Bitcoin,
408+
Currency::BitcoinTestnet => Network::Testnet,
409+
Currency::Regtest => Network::Regtest,
410+
Currency::Simnet => Network::Regtest,
411+
Currency::Signet => Network::Signet,
412+
}
413+
}
414+
}
415+
393416
/// Tagged field which may have an unknown tag
394417
///
395418
/// This is not exported to bindings users as we don't currently support TaggedField
@@ -1303,14 +1326,6 @@ impl Invoice {
13031326
/// Returns a list of all fallback addresses as [`Address`]es
13041327
pub fn fallback_addresses(&self) -> Vec<Address> {
13051328
self.fallbacks().iter().map(|fallback| {
1306-
let network = match self.currency() {
1307-
Currency::Bitcoin => Network::Bitcoin,
1308-
Currency::BitcoinTestnet => Network::Testnet,
1309-
Currency::Regtest => Network::Regtest,
1310-
Currency::Simnet => Network::Regtest,
1311-
Currency::Signet => Network::Signet,
1312-
};
1313-
13141329
let payload = match fallback {
13151330
Fallback::SegWitProgram { version, program } => {
13161331
Payload::WitnessProgram { version: *version, program: program.to_vec() }
@@ -1323,7 +1338,7 @@ impl Invoice {
13231338
}
13241339
};
13251340

1326-
Address { payload, network }
1341+
Address { payload, network: self.network() }
13271342
}).collect()
13281343
}
13291344

@@ -1344,6 +1359,11 @@ impl Invoice {
13441359
self.signed_invoice.currency()
13451360
}
13461361

1362+
/// Returns the network for which the invoice was issued
1363+
pub fn network(&self) -> Network {
1364+
self.signed_invoice.currency().into()
1365+
}
1366+
13471367
/// Returns the amount if specified in the invoice as millisatoshis.
13481368
pub fn amount_milli_satoshis(&self) -> Option<u64> {
13491369
self.signed_invoice.amount_pico_btc().map(|v| v / 10)

0 commit comments

Comments
 (0)