Skip to main content

fedimint_core/
bolt11.rs

1//! Mapping between Bitcoin networks and BOLT11 invoice currencies.
2//!
3//! BOLT11 uses the `tb` prefix for both testnet3 and testnet4, so
4//! [`Currency::BitcoinTestnet`](lightning_invoice::Currency::BitcoinTestnet)
5//! cannot be converted back to a single [`Network`](bitcoin::Network). The
6//! `From` conversions in `lightning-invoice` are lossy in both directions
7//! (`Network::Testnet4` becomes `Currency::Regtest`), so use these helpers
8//! instead.
9
10use bitcoin::Network;
11use lightning_invoice::Currency;
12
13/// Returns the BOLT11 currency used for invoices on `network`.
14pub fn currency_for_network(network: Network) -> Currency {
15    match network {
16        Network::Bitcoin => Currency::Bitcoin,
17        Network::Testnet | Network::Testnet4 => Currency::BitcoinTestnet,
18        Network::Signet => Currency::Signet,
19        Network::Regtest => Currency::Regtest,
20    }
21}
22
23/// Returns whether an invoice in `currency` can be paid on `network`.
24pub fn currency_matches_network(currency: &Currency, network: Network) -> bool {
25    match currency {
26        Currency::Bitcoin => network == Network::Bitcoin,
27        Currency::BitcoinTestnet => matches!(network, Network::Testnet | Network::Testnet4),
28        Currency::Signet => network == Network::Signet,
29        Currency::Regtest => network == Network::Regtest,
30        Currency::Simnet => false,
31    }
32}
33
34#[cfg(test)]
35mod tests;