From a741a57249086d83812237ae8dc68c5d2ef6b725 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 9 Aug 2024 01:29:48 +0000 Subject: [PATCH] Swap the dep order between `lightning` and `lightning-invoice` `lightning-invoice` previously had a dependency on the entire `lightning` crate just because it wants to use some of the useful types from it. This is obviously backwards and leads to some awkwardness like the BOLT 11 invoice signing API in the `lightning` crate taking a `[u5]` rather than a `Bolt11Invoice`. Here we finally rectify this issue, swapping the dependency order and making `lightning` depend on `lightning-invoice` rather than the other way around. This moves various utilities which were in `lightning-invoice` but relied on `lightning` payment types to make payments to where they belong (the `lightning` crate), but doesn't bother with integrating them well in their new home. --- lightning-invoice/Cargo.toml | 6 +- lightning-invoice/src/lib.rs | 41 +++-- lightning-invoice/tests/ser_de.rs | 1 - lightning/Cargo.toml | 5 +- .../src/ln/bolt11_payment.rs | 32 ++-- .../src/ln/invoice_utils.rs | 155 +++++++++--------- lightning/src/ln/mod.rs | 5 + 7 files changed, 127 insertions(+), 118 deletions(-) rename lightning-invoice/src/payment.rs => lightning/src/ln/bolt11_payment.rs (88%) rename lightning-invoice/src/utils.rs => lightning/src/ln/invoice_utils.rs (94%) diff --git a/lightning-invoice/Cargo.toml b/lightning-invoice/Cargo.toml index 0f5f464b8..f73321296 100644 --- a/lightning-invoice/Cargo.toml +++ b/lightning-invoice/Cargo.toml @@ -16,19 +16,17 @@ rustdoc-args = ["--cfg", "docsrs"] [features] default = ["std"] -no-std = ["lightning/no-std"] -std = ["bitcoin/std", "lightning/std", "bech32/std"] +no-std = ["bitcoin/no-std"] +std = ["bitcoin/std", "bech32/std"] [dependencies] bech32 = { version = "0.9.1", default-features = false } lightning-types = { version = "0.1", path = "../lightning-types", default-features = false } -lightning = { version = "0.0.123-beta", path = "../lightning", default-features = false } secp256k1 = { version = "0.28.0", default-features = false, features = ["recovery", "alloc"] } serde = { version = "1.0.118", optional = true } bitcoin = { version = "0.31.2", default-features = false } [dev-dependencies] -lightning = { version = "0.0.123-beta", path = "../lightning", default-features = false, features = ["_test_utils"] } serde_json = { version = "1"} hashbrown = { version = "0.13", default-features = false } diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index 0784911fc..56ca21a9b 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -25,11 +25,7 @@ #[cfg(not(any(feature = "std", feature = "no-std")))] compile_error!("at least one of the `std` or `no-std` features must be enabled"); -pub mod payment; -pub mod utils; - extern crate bech32; -#[macro_use] extern crate lightning; extern crate lightning_types; extern crate secp256k1; extern crate alloc; @@ -41,12 +37,11 @@ extern crate serde; #[cfg(feature = "std")] use std::time::SystemTime; -use bech32::u5; +use bech32::{FromBase32, u5}; use bitcoin::{Address, Network, PubkeyHash, ScriptHash, WitnessProgram, WitnessVersion}; use bitcoin::address::Payload; use bitcoin::hashes::{Hash, sha256}; use lightning_types::features::Bolt11InvoiceFeatures; -use lightning::util::invoice::construct_invoice_preimage; use secp256k1::PublicKey; use secp256k1::{Message, Secp256k1}; @@ -138,11 +133,9 @@ pub const DEFAULT_EXPIRY_TIME: u64 = 3600; /// Default minimum final CLTV expiry as defined by [BOLT 11]. /// -/// Note that this is *not* the same value as rust-lightning's minimum CLTV expiry, which is -/// provided in [`MIN_FINAL_CLTV_EXPIRY_DELTA`]. +/// Note that this is *not* the same value as rust-lightning's minimum CLTV expiry. /// /// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md -/// [`MIN_FINAL_CLTV_EXPIRY_DELTA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA pub const DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA: u64 = 18; /// Builder for [`Bolt11Invoice`]s. It's the most convenient and advised way to use this library. It @@ -150,7 +143,6 @@ pub const DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA: u64 = 18; /// /// ``` /// extern crate secp256k1; -/// extern crate lightning; /// extern crate lightning_invoice; /// extern crate bitcoin; /// @@ -969,7 +961,23 @@ macro_rules! find_all_extract { impl RawBolt11Invoice { /// Hash the HRP as bytes and signatureless data part. fn hash_from_parts(hrp_bytes: &[u8], data_without_signature: &[u5]) -> [u8; 32] { - let preimage = construct_invoice_preimage(hrp_bytes, data_without_signature); + let mut preimage = Vec::::from(hrp_bytes); + + let mut data_part = Vec::from(data_without_signature); + let overhang = (data_part.len() * 5) % 8; + if overhang > 0 { + // add padding if data does not end at a byte boundary + data_part.push(u5::try_from_u8(0).unwrap()); + + // if overhang is in (1..3) we need to add u5(0) padding two times + if overhang < 3 { + data_part.push(u5::try_from_u8(0).unwrap()); + } + } + + preimage.extend_from_slice(&Vec::::from_base32(&data_part) + .expect("No padding error may occur due to appended zero above.")); + let mut hash: [u8; 32] = Default::default(); hash.copy_from_slice(&sha256::Hash::hash(&preimage)[..]); hash @@ -1635,15 +1643,12 @@ pub enum CreationError { /// The supplied millisatoshi amount was greater than the total bitcoin supply. InvalidAmount, - /// Route hints were required for this invoice and were missing. Applies to - /// [phantom invoices]. - /// - /// [phantom invoices]: crate::utils::create_phantom_invoice + // TODO: These two errors are really errors with things in the `lightning` crate and thus + // shouldn't live here. + /// Route hints were required for this invoice and were missing. MissingRouteHints, - /// The provided `min_final_cltv_expiry_delta` was less than [`MIN_FINAL_CLTV_EXPIRY_DELTA`]. - /// - /// [`MIN_FINAL_CLTV_EXPIRY_DELTA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA + /// The provided `min_final_cltv_expiry_delta` was less than rust-lightning's minimum. MinFinalCltvExpiryDeltaTooShort, } diff --git a/lightning-invoice/tests/ser_de.rs b/lightning-invoice/tests/ser_de.rs index d0058c9a1..d9d6ad4c1 100644 --- a/lightning-invoice/tests/ser_de.rs +++ b/lightning-invoice/tests/ser_de.rs @@ -1,5 +1,4 @@ extern crate bech32; -extern crate lightning; extern crate lightning_invoice; extern crate secp256k1; diff --git a/lightning/Cargo.toml b/lightning/Cargo.toml index b55b675b0..c6d821130 100644 --- a/lightning/Cargo.toml +++ b/lightning/Cargo.toml @@ -31,8 +31,8 @@ unsafe_revoked_tx_signing = [] # Override signing to not include randomness when generating signatures for test vectors. _test_vectors = [] -no-std = ["hashbrown", "possiblyrandom", "bitcoin/no-std", "core2/alloc", "libm"] -std = ["bitcoin/std", "bech32/std"] +no-std = ["hashbrown", "possiblyrandom", "bitcoin/no-std", "lightning-invoice/no-std", "core2/alloc", "libm"] +std = ["bitcoin/std", "bech32/std", "lightning-invoice/std"] # Generates low-r bitcoin signatures, which saves 1 byte in 50% of the cases grind_signatures = [] @@ -41,6 +41,7 @@ default = ["std", "grind_signatures"] [dependencies] lightning-types = { version = "0.1", path = "../lightning-types", default-features = false } +lightning-invoice = { version = "0.31.0-beta", path = "../lightning-invoice", default-features = false } bech32 = { version = "0.9.1", default-features = false } bitcoin = { version = "0.31.2", default-features = false, features = ["secp-recovery"] } diff --git a/lightning-invoice/src/payment.rs b/lightning/src/ln/bolt11_payment.rs similarity index 88% rename from lightning-invoice/src/payment.rs rename to lightning/src/ln/bolt11_payment.rs index a8ffce7bb..5c5e3711a 100644 --- a/lightning-invoice/src/payment.rs +++ b/lightning/src/ln/bolt11_payment.rs @@ -9,12 +9,12 @@ //! Convenient utilities for paying Lightning invoices. -use crate::Bolt11Invoice; use bitcoin::hashes::Hash; +use lightning_invoice::Bolt11Invoice; -use lightning::ln::types::PaymentHash; -use lightning::ln::channelmanager::RecipientOnionFields; -use lightning::routing::router::{PaymentParameters, RouteParameters}; +use crate::ln::channelmanager::RecipientOnionFields; +use crate::ln::types::PaymentHash; +use crate::routing::router::{PaymentParameters, RouteParameters}; /// Builds the necessary parameters to pay or pre-flight probe the given zero-amount /// [`Bolt11Invoice`] using [`ChannelManager::send_payment`] or @@ -26,8 +26,8 @@ use lightning::routing::router::{PaymentParameters, RouteParameters}; /// Will always succeed unless the invoice has an amount specified, in which case /// [`payment_parameters_from_invoice`] should be used. /// -/// [`ChannelManager::send_payment`]: lightning::ln::channelmanager::ChannelManager::send_payment -/// [`ChannelManager::send_preflight_probes`]: lightning::ln::channelmanager::ChannelManager::send_preflight_probes +/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment +/// [`ChannelManager::send_preflight_probes`]: crate::ln::channelmanager::ChannelManager::send_preflight_probes pub fn payment_parameters_from_zero_amount_invoice(invoice: &Bolt11Invoice, amount_msat: u64) -> Result<(PaymentHash, RecipientOnionFields, RouteParameters), ()> { if invoice.amount_milli_satoshis().is_some() { @@ -46,8 +46,8 @@ pub fn payment_parameters_from_zero_amount_invoice(invoice: &Bolt11Invoice, amou /// Will always succeed unless the invoice has no amount specified, in which case /// [`payment_parameters_from_zero_amount_invoice`] should be used. /// -/// [`ChannelManager::send_payment`]: lightning::ln::channelmanager::ChannelManager::send_payment -/// [`ChannelManager::send_preflight_probes`]: lightning::ln::channelmanager::ChannelManager::send_preflight_probes +/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment +/// [`ChannelManager::send_preflight_probes`]: crate::ln::channelmanager::ChannelManager::send_preflight_probes pub fn payment_parameters_from_invoice(invoice: &Bolt11Invoice) -> Result<(PaymentHash, RecipientOnionFields, RouteParameters), ()> { if let Some(amount_msat) = invoice.amount_milli_satoshis() { @@ -83,11 +83,11 @@ fn params_from_invoice(invoice: &Bolt11Invoice, amount_msat: u64) #[cfg(test)] mod tests { use super::*; - use crate::{InvoiceBuilder, Currency}; + use lightning_invoice::{InvoiceBuilder, Currency}; use bitcoin::hashes::sha256::Hash as Sha256; - use lightning::ln::types::PaymentSecret; - use lightning::routing::router::Payee; - use secp256k1::{SecretKey, PublicKey, Secp256k1}; + use crate::ln::types::PaymentSecret; + use crate::routing::router::Payee; + use bitcoin::secp256k1::{SecretKey, PublicKey, Secp256k1}; use core::time::Duration; #[cfg(feature = "std")] use std::time::SystemTime; @@ -169,10 +169,10 @@ mod tests { #[test] #[cfg(feature = "std")] fn payment_metadata_end_to_end() { - use lightning::events::Event; - use lightning::ln::channelmanager::{Retry, PaymentId}; - use lightning::ln::msgs::ChannelMessageHandler; - use lightning::ln::functional_test_utils::*; + use crate::events::Event; + use crate::ln::channelmanager::{Retry, PaymentId}; + use crate::ln::msgs::ChannelMessageHandler; + use crate::ln::functional_test_utils::*; // Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all // the way out through the `PaymentClaimable` event. let chanmon_cfgs = create_chanmon_cfgs(2); diff --git a/lightning-invoice/src/utils.rs b/lightning/src/ln/invoice_utils.rs similarity index 94% rename from lightning-invoice/src/utils.rs rename to lightning/src/ln/invoice_utils.rs index 5b94c3ada..5e4ba508f 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning/src/ln/invoice_utils.rs @@ -1,22 +1,24 @@ //! Convenient utilities to create an invoice. -use crate::{Bolt11Invoice, CreationError, Currency, InvoiceBuilder, SignOrCreationError}; +use lightning_invoice::{Bolt11Invoice, CreationError, Currency, InvoiceBuilder, SignOrCreationError}; +use lightning_invoice::{Description, Bolt11InvoiceDescription, Sha256}; + +use crate::prelude::*; -use crate::{prelude::*, Description, Bolt11InvoiceDescription, Sha256}; use bech32::ToBase32; use bitcoin::hashes::Hash; -use lightning::chain; -use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator}; -use lightning::sign::{Recipient, NodeSigner, SignerProvider, EntropySource}; -use lightning::ln::types::{PaymentHash, PaymentSecret}; -use lightning::ln::channel_state::ChannelDetails; -use lightning::ln::channelmanager::{ChannelManager, MIN_FINAL_CLTV_EXPIRY_DELTA}; -use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA}; -use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey}; -use lightning::routing::gossip::RoutingFees; -use lightning::routing::router::{RouteHint, RouteHintHop, Router}; -use lightning::util::logger::{Logger, Record}; -use secp256k1::PublicKey; +use crate::chain; +use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator}; +use crate::sign::{Recipient, NodeSigner, SignerProvider, EntropySource}; +use crate::ln::types::{PaymentHash, PaymentSecret}; +use crate::ln::channel_state::ChannelDetails; +use crate::ln::channelmanager::{ChannelManager, MIN_FINAL_CLTV_EXPIRY_DELTA}; +use crate::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA}; +use crate::ln::inbound_payment::{create, create_from_hash, ExpandedKey}; +use crate::routing::gossip::RoutingFees; +use crate::routing::router::{RouteHint, RouteHintHop, Router}; +use crate::util::logger::{Logger, Record}; +use bitcoin::secp256k1::PublicKey; use alloc::collections::{btree_map, BTreeMap}; use core::ops::Deref; use core::time::Duration; @@ -54,12 +56,12 @@ use core::iter::Iterator; /// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this /// requirement). /// -/// [`PhantomKeysManager`]: lightning::sign::PhantomKeysManager -/// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints -/// [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment -/// [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash -/// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels -/// [`MIN_FINAL_CLTV_EXPIRY_DETLA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA +/// [`PhantomKeysManager`]: crate::sign::PhantomKeysManager +/// [`ChannelManager::get_phantom_route_hints`]: crate::ln::channelmanager::ChannelManager::get_phantom_route_hints +/// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment +/// [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash +/// [`PhantomRouteHints::channels`]: crate::ln::channelmanager::PhantomRouteHints::channels +/// [`MIN_FINAL_CLTV_EXPIRY_DETLA`]: crate::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA /// /// This can be used in a `no_std` environment, where [`std::time::SystemTime`] is not /// available and the current time is supplied by the caller. @@ -111,11 +113,11 @@ where /// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this /// requirement). /// -/// [`PhantomKeysManager`]: lightning::sign::PhantomKeysManager -/// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints -/// [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment -/// [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash -/// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels +/// [`PhantomKeysManager`]: crate::sign::PhantomKeysManager +/// [`ChannelManager::get_phantom_route_hints`]: crate::ln::channelmanager::ChannelManager::get_phantom_route_hints +/// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment +/// [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash +/// [`PhantomRouteHints::channels`]: crate::ln::channelmanager::PhantomRouteHints::channels /// /// This can be used in a `no_std` environment, where [`std::time::SystemTime`] is not /// available and the current time is supplied by the caller. @@ -161,7 +163,7 @@ where let invoice = match description { Bolt11InvoiceDescription::Direct(description) => { - InvoiceBuilder::new(network).description(description.0.0.clone()) + InvoiceBuilder::new(network).description(description.as_inner().0.clone()) } Bolt11InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0), }; @@ -234,7 +236,7 @@ where /// * Select up to three channels per node. /// * Select one hint from each node, up to three hints or until we run out of hints. /// -/// [`PhantomKeysManager`]: lightning::sign::PhantomKeysManager +/// [`PhantomKeysManager`]: crate::sign::PhantomKeysManager fn select_phantom_hints(amt_msat: Option, phantom_route_hints: Vec, logger: L) -> impl Iterator where @@ -331,7 +333,7 @@ fn rotate_through_iterators>(mut vecs: Vec) -> impl /// Note that LDK will add a buffer of 3 blocks to the delta to allow for up to a few new block /// confirmations during routing. /// -/// [`MIN_FINAL_CLTV_EXPIRY_DETLA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA +/// [`MIN_FINAL_CLTV_EXPIRY_DETLA`]: crate::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA pub fn create_invoice_from_channelmanager( channelmanager: &ChannelManager, node_signer: NS, logger: L, network: Currency, amt_msat: Option, description: String, invoice_expiry_delta_secs: u32, @@ -372,7 +374,7 @@ where /// Note that LDK will add a buffer of 3 blocks to the delta to allow for up to a few new block /// confirmations during routing. /// -/// [`MIN_FINAL_CLTV_EXPIRY_DETLA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA +/// [`MIN_FINAL_CLTV_EXPIRY_DETLA`]: crate::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA pub fn create_invoice_from_channelmanager_with_description_hash( channelmanager: &ChannelManager, node_signer: NS, logger: L, network: Currency, amt_msat: Option, description_hash: Sha256, @@ -541,7 +543,7 @@ fn _create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_has let invoice = match description { Bolt11InvoiceDescription::Direct(description) => { - InvoiceBuilder::new(network).description(description.0.0.clone()) + InvoiceBuilder::new(network).description(description.as_inner().0.clone()) } Bolt11InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0), }; @@ -819,50 +821,49 @@ impl<'a, 'b, L: Deref> WithChannelDetails<'a, 'b, L> where L::Target: Logger { #[cfg(test)] mod test { + use super::*; use core::time::Duration; - use crate::{Currency, Description, Bolt11InvoiceDescription, SignOrCreationError, CreationError}; + use lightning_invoice::{Currency, Description, Bolt11InvoiceDescription, SignOrCreationError, CreationError}; use bitcoin::hashes::{Hash, sha256}; use bitcoin::hashes::sha256::Hash as Sha256; - use lightning::sign::PhantomKeysManager; - use lightning::events::{MessageSendEvent, MessageSendEventsProvider}; - use lightning::ln::types::PaymentHash; + use crate::sign::PhantomKeysManager; + use crate::events::{MessageSendEvent, MessageSendEventsProvider}; + use crate::ln::types::PaymentHash; #[cfg(feature = "std")] - use lightning::ln::types::PaymentPreimage; - use lightning::ln::channelmanager::{PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY_DELTA, PaymentId, RecipientOnionFields, Retry}; - use lightning::ln::functional_test_utils::*; - use lightning::ln::msgs::ChannelMessageHandler; - use lightning::routing::router::{PaymentParameters, RouteParameters}; - use lightning::util::test_utils; - use lightning::util::config::UserConfig; - use crate::utils::{create_invoice_from_channelmanager_and_duration_since_epoch, rotate_through_iterators}; + use crate::ln::types::PaymentPreimage; + use crate::ln::channelmanager::{PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY_DELTA, PaymentId, RecipientOnionFields, Retry}; + use crate::ln::functional_test_utils::*; + use crate::ln::msgs::ChannelMessageHandler; + use crate::routing::router::{PaymentParameters, RouteParameters}; + use crate::util::test_utils; + use crate::util::config::UserConfig; use std::collections::HashSet; - use lightning::util::string::UntrustedString; #[test] fn test_prefer_current_channel() { // No minimum, prefer larger candidate channel. - assert_eq!(crate::utils::prefer_current_channel(None, 100, 200), false); + assert_eq!(prefer_current_channel(None, 100, 200), false); // No minimum, prefer larger current channel. - assert_eq!(crate::utils::prefer_current_channel(None, 200, 100), true); + assert_eq!(prefer_current_channel(None, 200, 100), true); // Minimum set, prefer current channel over minimum + buffer. - assert_eq!(crate::utils::prefer_current_channel(Some(100), 115, 100), true); + assert_eq!(prefer_current_channel(Some(100), 115, 100), true); // Minimum set, prefer candidate channel over minimum + buffer. - assert_eq!(crate::utils::prefer_current_channel(Some(100), 105, 125), false); + assert_eq!(prefer_current_channel(Some(100), 105, 125), false); // Minimum set, both channels sufficient, prefer smaller current channel. - assert_eq!(crate::utils::prefer_current_channel(Some(100), 115, 125), true); + assert_eq!(prefer_current_channel(Some(100), 115, 125), true); // Minimum set, both channels sufficient, prefer smaller candidate channel. - assert_eq!(crate::utils::prefer_current_channel(Some(100), 200, 160), false); + assert_eq!(prefer_current_channel(Some(100), 200, 160), false); // Minimum set, neither sufficient, prefer larger current channel. - assert_eq!(crate::utils::prefer_current_channel(Some(200), 100, 50), true); + assert_eq!(prefer_current_channel(Some(200), 100, 50), true); // Minimum set, neither sufficient, prefer larger candidate channel. - assert_eq!(crate::utils::prefer_current_channel(Some(200), 100, 150), false); + assert_eq!(prefer_current_channel(Some(200), 100, 150), false); } @@ -878,10 +879,10 @@ mod test { nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet, Some(10_000), "test".to_string(), Duration::from_secs(1234567), non_default_invoice_expiry_secs, None).unwrap(); - assert_eq!(invoice.amount_pico_btc(), Some(100_000)); + assert_eq!(invoice.amount_milli_satoshis(), Some(10_000)); // If no `min_final_cltv_expiry_delta` is specified, then it should be `MIN_FINAL_CLTV_EXPIRY_DELTA`. assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); - assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description(UntrustedString("test".to_string())))); + assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description::new("test".to_string()).unwrap())); assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into())); // Invoice SCIDs should always use inbound SCID aliases over the real channel ID, if one is @@ -925,7 +926,7 @@ mod test { let nodes = create_network(2, &node_cfgs, &node_chanmgrs); let custom_min_final_cltv_expiry_delta = Some(50); - let invoice = crate::utils::create_invoice_from_channelmanager_and_duration_since_epoch( + let invoice = create_invoice_from_channelmanager_and_duration_since_epoch( nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet, Some(10_000), "".into(), Duration::from_secs(1234567), 3600, if with_custom_delta { custom_min_final_cltv_expiry_delta } else { None }, @@ -948,7 +949,7 @@ mod test { let nodes = create_network(2, &node_cfgs, &node_chanmgrs); let custom_min_final_cltv_expiry_delta = Some(21); - let invoice = crate::utils::create_invoice_from_channelmanager_and_duration_since_epoch( + let invoice = create_invoice_from_channelmanager_and_duration_since_epoch( nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet, Some(10_000), "".into(), Duration::from_secs(1234567), 3600, custom_min_final_cltv_expiry_delta, @@ -962,14 +963,14 @@ mod test { let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let nodes = create_network(2, &node_cfgs, &node_chanmgrs); - let description_hash = crate::Sha256(Hash::hash("Testing description_hash".as_bytes())); - let invoice = crate::utils::create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch( + let description_hash = Sha256(Hash::hash("Testing description_hash".as_bytes())); + let invoice = create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch( nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet, Some(10_000), description_hash, Duration::from_secs(1234567), 3600, None, ).unwrap(); - assert_eq!(invoice.amount_pico_btc(), Some(100_000)); + assert_eq!(invoice.amount_milli_satoshis(), Some(10_000)); assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); - assert_eq!(invoice.description(), Bolt11InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Testing description_hash".as_bytes())))); + assert_eq!(invoice.description(), Bolt11InvoiceDescription::Hash(&Sha256(Sha256::hash("Testing description_hash".as_bytes())))); } #[test] @@ -979,14 +980,14 @@ mod test { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let nodes = create_network(2, &node_cfgs, &node_chanmgrs); let payment_hash = PaymentHash([0; 32]); - let invoice = crate::utils::create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash( + let invoice = create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash( nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet, Some(10_000), "test".to_string(), Duration::from_secs(1234567), 3600, payment_hash, None, ).unwrap(); - assert_eq!(invoice.amount_pico_btc(), Some(100_000)); + assert_eq!(invoice.amount_milli_satoshis(), Some(10_000)); assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); - assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description(UntrustedString("test".to_string())))); + assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description::new("test".to_string()).unwrap())); assert_eq!(invoice.payment_hash(), &sha256::Hash::from_slice(&payment_hash.0[..]).unwrap()); } @@ -1278,7 +1279,7 @@ mod test { let hints = invoice.private_routes(); for hint in hints { - let hint_short_chan_id = (hint.0).0[0].short_channel_id; + let hint_short_chan_id = hint.0[0].short_channel_id; assert!(chan_ids_to_match.remove(&hint_short_chan_id)); } assert!(chan_ids_to_match.is_empty(), "Unmatched short channel ids: {:?}", chan_ids_to_match); @@ -1293,7 +1294,7 @@ mod test { #[cfg(feature = "std")] fn do_test_multi_node_receive(user_generated_pmt_hash: bool) { - use lightning::events::{Event, EventsProvider}; + use crate::events::{Event, EventsProvider}; use core::cell::RefCell; let mut chanmon_cfgs = create_chanmon_cfgs(3); @@ -1328,7 +1329,7 @@ mod test { let non_default_invoice_expiry_secs = 4200; let invoice = - crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestLogger>( + create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestLogger>( Some(payment_amt), payment_hash, "test".to_string(), non_default_invoice_expiry_secs, route_hints, nodes[1].keys_manager, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet, None, Duration::from_secs(genesis_timestamp) @@ -1341,7 +1342,7 @@ mod test { }; assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); - assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description(UntrustedString("test".to_string())))); + assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description::new("test".to_string()).unwrap())); assert_eq!(invoice.route_hints().len(), 2); assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into())); assert!(!invoice.features().unwrap().supports_basic_mpp()); @@ -1421,7 +1422,7 @@ mod test { nodes[2].node.get_phantom_route_hints(), ]; - let invoice = crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, + let invoice = create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestLogger>(Some(payment_amt), Some(payment_hash), "test".to_string(), 3600, route_hints, nodes[1].keys_manager, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet, None, Duration::from_secs(1234567)).unwrap(); @@ -1437,7 +1438,7 @@ mod test { #[test] #[cfg(feature = "std")] - fn create_phantom_invoice_with_description_hash() { + fn test_create_phantom_invoice_with_description_hash() { let chanmon_cfgs = create_chanmon_cfgs(3); let node_cfgs = create_node_cfgs(3, &chanmon_cfgs); let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]); @@ -1449,9 +1450,9 @@ mod test { nodes[2].node.get_phantom_route_hints(), ]; - let description_hash = crate::Sha256(Hash::hash("Description hash phantom invoice".as_bytes())); + let description_hash = Sha256(Hash::hash("Description hash phantom invoice".as_bytes())); let non_default_invoice_expiry_secs = 4200; - let invoice = crate::utils::create_phantom_invoice_with_description_hash::< + let invoice = create_phantom_invoice_with_description_hash::< &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestLogger, >( Some(payment_amt), None, non_default_invoice_expiry_secs, description_hash, @@ -1459,10 +1460,10 @@ mod test { Currency::BitcoinTestnet, None, Duration::from_secs(1234567), ) .unwrap(); - assert_eq!(invoice.amount_pico_btc(), Some(200_000)); + assert_eq!(invoice.amount_milli_satoshis(), Some(20_000)); assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into())); - assert_eq!(invoice.description(), Bolt11InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Description hash phantom invoice".as_bytes())))); + assert_eq!(invoice.description(), Bolt11InvoiceDescription::Hash(&Sha256(Sha256::hash("Description hash phantom invoice".as_bytes())))); } #[test] @@ -1483,11 +1484,11 @@ mod test { let non_default_invoice_expiry_secs = 4200; let min_final_cltv_expiry_delta = Some(100); let duration_since_epoch = Duration::from_secs(1234567); - let invoice = crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, + let invoice = create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestLogger>(Some(payment_amt), payment_hash, "".to_string(), non_default_invoice_expiry_secs, route_hints, nodes[1].keys_manager, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet, min_final_cltv_expiry_delta, duration_since_epoch).unwrap(); - assert_eq!(invoice.amount_pico_btc(), Some(200_000)); + assert_eq!(invoice.amount_milli_satoshis(), Some(20_000)); assert_eq!(invoice.min_final_cltv_expiry_delta(), (min_final_cltv_expiry_delta.unwrap() + 3) as u64); assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into())); } @@ -1888,7 +1889,7 @@ mod test { .map(|route_hint| route_hint.phantom_scid) .collect::>(); - let invoice = crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, + let invoice = create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestLogger>(invoice_amt, None, "test".to_string(), 3600, phantom_route_hints, invoice_node.keys_manager, invoice_node.keys_manager, invoice_node.logger, Currency::BitcoinTestnet, None, Duration::from_secs(1234567)).unwrap(); @@ -1896,7 +1897,7 @@ mod test { let invoice_hints = invoice.private_routes(); for hint in invoice_hints { - let hints = &(hint.0).0; + let hints = &hint.0; match hints.len() { 1 => { assert!(nodes_contains_public_channels); @@ -1921,7 +1922,7 @@ mod test { let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let nodes = create_network(2, &node_cfgs, &node_chanmgrs); - let result = crate::utils::create_invoice_from_channelmanager_and_duration_since_epoch( + let result = create_invoice_from_channelmanager_and_duration_since_epoch( nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet, Some(10_000), "Some description".into(), Duration::from_secs(1234567), 3600, Some(MIN_FINAL_CLTV_EXPIRY_DELTA - 4), ); diff --git a/lightning/src/ln/mod.rs b/lightning/src/ln/mod.rs index 9bac19104..8009f31b0 100644 --- a/lightning/src/ln/mod.rs +++ b/lightning/src/ln/mod.rs @@ -25,6 +25,11 @@ pub mod features; pub mod script; pub mod types; +// TODO: These modules were moved from lightning-invoice and need to be better integrated into this +// crate now: +pub mod invoice_utils; +pub mod bolt11_payment; + pub use lightning_types::payment::{PaymentHash, PaymentPreimage, PaymentSecret}; #[cfg(fuzzing)] -- 2.39.5