From 0ea225cfe7ea2d7e8e3692f6dcd3984232e78482 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Wed, 6 Nov 2024 18:53:18 -0600 Subject: [PATCH] Remove no-std support from invoice_utils functions The upcoming ChannelManager::create_bolt11_invoice will not support setting a specific creation time, so remove that functionality from the invoice_utils functions. This will avoid duplicate code when deprecating. --- lightning/src/ln/invoice_utils.rs | 135 ++++++++++-------------------- 1 file changed, 46 insertions(+), 89 deletions(-) diff --git a/lightning/src/ln/invoice_utils.rs b/lightning/src/ln/invoice_utils.rs index 1cccb69f4..8de460c8a 100644 --- a/lightning/src/ln/invoice_utils.rs +++ b/lightning/src/ln/invoice_utils.rs @@ -346,11 +346,16 @@ where L::Target: Logger, { use std::time::SystemTime; - let duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) + let duration_since_epoch = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) .expect("for the foreseeable future this shouldn't happen"); - create_invoice_from_channelmanager_and_duration_since_epoch( + + _create_invoice_from_channelmanager_and_duration_since_epoch( channelmanager, node_signer, logger, network, amt_msat, - description, duration, invoice_expiry_delta_secs, min_final_cltv_expiry_delta, + Bolt11InvoiceDescription::Direct( + Description::new(description).map_err(SignOrCreationError::CreationError)?, + ), + duration_since_epoch, invoice_expiry_delta_secs, min_final_cltv_expiry_delta, ) } @@ -388,39 +393,10 @@ where L::Target: Logger, { use std::time::SystemTime; - - let duration = SystemTime::now() + let duration_since_epoch = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .expect("for the foreseeable future this shouldn't happen"); - create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch( - channelmanager, node_signer, logger, network, amt_msat, - description_hash, duration, invoice_expiry_delta_secs, min_final_cltv_expiry_delta, - ) -} - -/// Utility to construct an invoice. Generally, unless you want to do something like a custom -/// `cltv_expiry`, this is what you should be using to create an invoice. -#[cfg_attr(feature = "std", doc = "")] -#[cfg_attr(feature = "std", doc = "See [`create_invoice_from_channelmanager_with_description_hash`] for more information.")] -#[cfg_attr(feature = "std", doc = "")] -#[cfg_attr(feature = "std", doc = "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.")] -pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch( - channelmanager: &ChannelManager, node_signer: NS, logger: L, - network: Currency, amt_msat: Option, description_hash: Sha256, - duration_since_epoch: Duration, invoice_expiry_delta_secs: u32, min_final_cltv_expiry_delta: Option, -) -> Result> -where - M::Target: chain::Watch<::EcdsaSigner>, - T::Target: BroadcasterInterface, - ES::Target: EntropySource, - NS::Target: NodeSigner, - SP::Target: SignerProvider, - F::Target: FeeEstimator, - R::Target: Router, - MR::Target: MessageRouter, - L::Target: Logger, -{ _create_invoice_from_channelmanager_and_duration_since_epoch( channelmanager, node_signer, logger, network, amt_msat, Bolt11InvoiceDescription::Hash(description_hash), @@ -428,37 +404,6 @@ where ) } -/// Utility to construct an invoice. Generally, unless you want to do something like a custom -/// `cltv_expiry`, this is what you should be using to create an invoice. -#[cfg_attr(feature = "std", doc = "")] -#[cfg_attr(feature = "std", doc = "See [`create_invoice_from_channelmanager`] for more information.")] -#[cfg_attr(feature = "std", doc = "")] -#[cfg_attr(feature = "std", doc = "This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not available and the current time is supplied by the caller.")] -pub fn create_invoice_from_channelmanager_and_duration_since_epoch( - channelmanager: &ChannelManager, node_signer: NS, logger: L, - network: Currency, amt_msat: Option, description: String, duration_since_epoch: Duration, - invoice_expiry_delta_secs: u32, min_final_cltv_expiry_delta: Option, -) -> Result> -where - M::Target: chain::Watch<::EcdsaSigner>, - T::Target: BroadcasterInterface, - ES::Target: EntropySource, - NS::Target: NodeSigner, - SP::Target: SignerProvider, - F::Target: FeeEstimator, - R::Target: Router, - MR::Target: MessageRouter, - L::Target: Logger, -{ - _create_invoice_from_channelmanager_and_duration_since_epoch( - channelmanager, node_signer, logger, network, amt_msat, - Bolt11InvoiceDescription::Direct( - Description::new(description).map_err(SignOrCreationError::CreationError)?, - ), - duration_since_epoch, invoice_expiry_delta_secs, min_final_cltv_expiry_delta, - ) -} - fn _create_invoice_from_channelmanager_and_duration_since_epoch( channelmanager: &ChannelManager, node_signer: NS, logger: L, network: Currency, amt_msat: Option, description: Bolt11InvoiceDescription, @@ -489,15 +434,17 @@ where invoice_expiry_delta_secs, payment_hash, payment_secret, min_final_cltv_expiry_delta) } -/// See [`create_invoice_from_channelmanager_and_duration_since_epoch`] +#[cfg(feature = "std")] +/// See [`create_invoice_from_channelmanager`]. +/// /// This version allows for providing custom [`PaymentHash`] and description hash for the invoice. /// /// This may be useful if you're building an on-chain swap or involving another protocol where /// the payment hash is also involved outside the scope of lightning and want to set the /// description hash. -pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch_with_payment_hash( +pub fn create_invoice_from_channelmanager_with_description_hash_and_payment_hash( channelmanager: &ChannelManager, node_signer: NS, logger: L, - network: Currency, amt_msat: Option, description_hash: Sha256, duration_since_epoch: Duration, + network: Currency, amt_msat: Option, description_hash: Sha256, invoice_expiry_delta_secs: u32, payment_hash: PaymentHash, min_final_cltv_expiry_delta: Option, ) -> Result> where @@ -511,6 +458,11 @@ where MR::Target: MessageRouter, L::Target: Logger, { + use std::time::SystemTime; + let duration_since_epoch = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .expect("for the foreseeable future this shouldn't happen"); + let payment_secret = channelmanager .create_inbound_payment_for_hash(payment_hash, amt_msat, invoice_expiry_delta_secs, min_final_cltv_expiry_delta) @@ -523,14 +475,16 @@ where ) } -/// See [`create_invoice_from_channelmanager_and_duration_since_epoch`] +#[cfg(feature = "std")] +/// See [`create_invoice_from_channelmanager`]. +/// /// This version allows for providing a custom [`PaymentHash`] for the invoice. /// This may be useful if you're building an on-chain swap or involving another protocol where /// the payment hash is also involved outside the scope of lightning. -pub fn create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash( +pub fn create_invoice_from_channelmanager_with_payment_hash( channelmanager: &ChannelManager, node_signer: NS, logger: L, - network: Currency, amt_msat: Option, description: String, duration_since_epoch: Duration, - invoice_expiry_delta_secs: u32, payment_hash: PaymentHash, min_final_cltv_expiry_delta: Option, + network: Currency, amt_msat: Option, description: String, invoice_expiry_delta_secs: u32, + payment_hash: PaymentHash, min_final_cltv_expiry_delta: Option, ) -> Result> where M::Target: chain::Watch<::EcdsaSigner>, @@ -543,6 +497,11 @@ where MR::Target: MessageRouter, L::Target: Logger, { + use std::time::SystemTime; + let duration_since_epoch = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .expect("for the foreseeable future this shouldn't happen"); + let payment_secret = channelmanager .create_inbound_payment_for_hash(payment_hash, amt_msat, invoice_expiry_delta_secs, min_final_cltv_expiry_delta) @@ -913,10 +872,10 @@ mod test { let nodes = create_network(2, &node_cfgs, &node_chanmgrs); create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001); let non_default_invoice_expiry_secs = 4200; - let invoice = create_invoice_from_channelmanager_and_duration_since_epoch( + let invoice = create_invoice_from_channelmanager( 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(); + Some(10_000), "test".to_string(), non_default_invoice_expiry_secs, None, + ).unwrap(); 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); @@ -964,9 +923,9 @@ mod test { let nodes = create_network(2, &node_cfgs, &node_chanmgrs); let custom_min_final_cltv_expiry_delta = Some(50); - let invoice = create_invoice_from_channelmanager_and_duration_since_epoch( + let invoice = create_invoice_from_channelmanager( nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet, - Some(10_000), "".into(), Duration::from_secs(1234567), 3600, + Some(10_000), "".into(), 3600, if with_custom_delta { custom_min_final_cltv_expiry_delta } else { None }, ).unwrap(); assert_eq!(invoice.min_final_cltv_expiry_delta(), if with_custom_delta { @@ -987,10 +946,9 @@ mod test { let nodes = create_network(2, &node_cfgs, &node_chanmgrs); let custom_min_final_cltv_expiry_delta = Some(21); - let invoice = create_invoice_from_channelmanager_and_duration_since_epoch( + let invoice = create_invoice_from_channelmanager( 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, + Some(10_000), "".into(), 3600, custom_min_final_cltv_expiry_delta, ).unwrap(); assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); } @@ -1002,9 +960,9 @@ mod test { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let nodes = create_network(2, &node_cfgs, &node_chanmgrs); let description_hash = Sha256(Hash::hash("Testing description_hash".as_bytes())); - let invoice = create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch( + let invoice = create_invoice_from_channelmanager_with_description_hash( nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet, - Some(10_000), description_hash, Duration::from_secs(1234567), 3600, None, + Some(10_000), description_hash, 3600, None, ).unwrap(); assert_eq!(invoice.amount_milli_satoshis(), Some(10_000)); assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); @@ -1018,10 +976,9 @@ 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 = create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash( + let invoice = create_invoice_from_channelmanager_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, + Some(10_000), "test".to_string(), 3600, payment_hash, None, ).unwrap(); assert_eq!(invoice.amount_milli_satoshis(), Some(10_000)); assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); @@ -1310,10 +1267,10 @@ mod test { invoice_node: &Node<'a, 'b, 'c>, mut chan_ids_to_match: HashSet ) { - let invoice = create_invoice_from_channelmanager_and_duration_since_epoch( + let invoice = create_invoice_from_channelmanager( invoice_node.node, invoice_node.keys_manager, invoice_node.logger, - Currency::BitcoinTestnet, invoice_amt, "test".to_string(), Duration::from_secs(1234567), - 3600, None).unwrap(); + Currency::BitcoinTestnet, invoice_amt, "test".to_string(), 3600, None, + ).unwrap(); let hints = invoice.private_routes(); for hint in hints { @@ -1948,9 +1905,9 @@ 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 = create_invoice_from_channelmanager_and_duration_since_epoch( + let result = create_invoice_from_channelmanager( 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), + Some(10_000), "Some description".into(), 3600, Some(MIN_FINAL_CLTV_EXPIRY_DELTA - 4), ); match result { Err(SignOrCreationError::CreationError(CreationError::MinFinalCltvExpiryDeltaTooShort)) => {}, -- 2.39.5