Skip to content

Convert Network to and from Currency #2173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions lightning-invoice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() }
Expand All @@ -1323,7 +1338,7 @@ impl Invoice {
}
};

Address { payload, network }
Address { payload, network: self.network() }
}).collect()
}

Expand All @@ -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<u64> {
self.signed_invoice.amount_pico_btc().map(|v| v / 10)
Expand Down