Skip to content

Commit 284cb28

Browse files
committed
Remove currency support from invoice_utils
When creating an invoice using a ChannelManager, payments for a specific ChainHash / Network are only valid. Use the one from the ChannelManager instead of allowing arbitrary ones in the form of a Currency.
1 parent 0ea225c commit 284cb28

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2199,7 +2199,7 @@ where
21992199
L::Target: Logger,
22002200
{
22012201
default_configuration: UserConfig,
2202-
chain_hash: ChainHash,
2202+
pub(super) chain_hash: ChainHash,
22032203
fee_estimator: LowerBoundedFeeEstimator<F>,
22042204
chain_monitor: M,
22052205
tx_broadcaster: T,

lightning/src/ln/invoice_utils.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use lightning_invoice::{Description, Bolt11InvoiceDescription, Sha256};
66
use crate::prelude::*;
77

88
use bitcoin::hashes::Hash;
9+
use bitcoin::network::Network;
910
use crate::chain;
1011
use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
1112
use crate::sign::{Recipient, NodeSigner, SignerProvider, EntropySource};
@@ -331,7 +332,7 @@ fn rotate_through_iterators<T, I: Iterator<Item = T>>(mut vecs: Vec<I>) -> impl
331332
/// [`MIN_FINAL_CLTV_EXPIRY_DETLA`]: crate::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA
332333
pub fn create_invoice_from_channelmanager<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
333334
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, MR, L>, node_signer: NS, logger: L,
334-
network: Currency, amt_msat: Option<u64>, description: String, invoice_expiry_delta_secs: u32,
335+
amt_msat: Option<u64>, description: String, invoice_expiry_delta_secs: u32,
335336
min_final_cltv_expiry_delta: Option<u16>,
336337
) -> Result<Bolt11Invoice, SignOrCreationError<()>>
337338
where
@@ -351,7 +352,7 @@ where
351352
.expect("for the foreseeable future this shouldn't happen");
352353

353354
_create_invoice_from_channelmanager_and_duration_since_epoch(
354-
channelmanager, node_signer, logger, network, amt_msat,
355+
channelmanager, node_signer, logger, amt_msat,
355356
Bolt11InvoiceDescription::Direct(
356357
Description::new(description).map_err(SignOrCreationError::CreationError)?,
357358
),
@@ -378,7 +379,7 @@ where
378379
/// [`MIN_FINAL_CLTV_EXPIRY_DETLA`]: crate::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA
379380
pub fn create_invoice_from_channelmanager_with_description_hash<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
380381
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, MR, L>, node_signer: NS, logger: L,
381-
network: Currency, amt_msat: Option<u64>, description_hash: Sha256,
382+
amt_msat: Option<u64>, description_hash: Sha256,
382383
invoice_expiry_delta_secs: u32, min_final_cltv_expiry_delta: Option<u16>,
383384
) -> Result<Bolt11Invoice, SignOrCreationError<()>>
384385
where
@@ -398,15 +399,15 @@ where
398399
.expect("for the foreseeable future this shouldn't happen");
399400

400401
_create_invoice_from_channelmanager_and_duration_since_epoch(
401-
channelmanager, node_signer, logger, network, amt_msat,
402+
channelmanager, node_signer, logger, amt_msat,
402403
Bolt11InvoiceDescription::Hash(description_hash),
403404
duration_since_epoch, invoice_expiry_delta_secs, min_final_cltv_expiry_delta,
404405
)
405406
}
406407

407408
fn _create_invoice_from_channelmanager_and_duration_since_epoch<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
408409
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, MR, L>, node_signer: NS, logger: L,
409-
network: Currency, amt_msat: Option<u64>, description: Bolt11InvoiceDescription,
410+
amt_msat: Option<u64>, description: Bolt11InvoiceDescription,
410411
duration_since_epoch: Duration, invoice_expiry_delta_secs: u32, min_final_cltv_expiry_delta: Option<u16>,
411412
) -> Result<Bolt11Invoice, SignOrCreationError<()>>
412413
where
@@ -430,7 +431,7 @@ where
430431
.create_inbound_payment(amt_msat, invoice_expiry_delta_secs, min_final_cltv_expiry_delta)
431432
.map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
432433
_create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash(
433-
channelmanager, node_signer, logger, network, amt_msat, description, duration_since_epoch,
434+
channelmanager, node_signer, logger, amt_msat, description, duration_since_epoch,
434435
invoice_expiry_delta_secs, payment_hash, payment_secret, min_final_cltv_expiry_delta)
435436
}
436437

@@ -444,7 +445,7 @@ where
444445
/// description hash.
445446
pub fn create_invoice_from_channelmanager_with_description_hash_and_payment_hash<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
446447
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, MR, L>, node_signer: NS, logger: L,
447-
network: Currency, amt_msat: Option<u64>, description_hash: Sha256,
448+
amt_msat: Option<u64>, description_hash: Sha256,
448449
invoice_expiry_delta_secs: u32, payment_hash: PaymentHash, min_final_cltv_expiry_delta: Option<u16>,
449450
) -> Result<Bolt11Invoice, SignOrCreationError<()>>
450451
where
@@ -468,7 +469,7 @@ where
468469
min_final_cltv_expiry_delta)
469470
.map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
470471
_create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash(
471-
channelmanager, node_signer, logger, network, amt_msat,
472+
channelmanager, node_signer, logger, amt_msat,
472473
Bolt11InvoiceDescription::Hash(description_hash),
473474
duration_since_epoch, invoice_expiry_delta_secs, payment_hash, payment_secret,
474475
min_final_cltv_expiry_delta,
@@ -483,7 +484,7 @@ where
483484
/// the payment hash is also involved outside the scope of lightning.
484485
pub fn create_invoice_from_channelmanager_with_payment_hash<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
485486
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, MR, L>, node_signer: NS, logger: L,
486-
network: Currency, amt_msat: Option<u64>, description: String, invoice_expiry_delta_secs: u32,
487+
amt_msat: Option<u64>, description: String, invoice_expiry_delta_secs: u32,
487488
payment_hash: PaymentHash, min_final_cltv_expiry_delta: Option<u16>,
488489
) -> Result<Bolt11Invoice, SignOrCreationError<()>>
489490
where
@@ -507,7 +508,7 @@ where
507508
min_final_cltv_expiry_delta)
508509
.map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
509510
_create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash(
510-
channelmanager, node_signer, logger, network, amt_msat,
511+
channelmanager, node_signer, logger, amt_msat,
511512
Bolt11InvoiceDescription::Direct(
512513
Description::new(description).map_err(SignOrCreationError::CreationError)?,
513514
),
@@ -518,7 +519,7 @@ where
518519

519520
fn _create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
520521
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, MR, L>, node_signer: NS, logger: L,
521-
network: Currency, amt_msat: Option<u64>, description: Bolt11InvoiceDescription,
522+
amt_msat: Option<u64>, description: Bolt11InvoiceDescription,
522523
duration_since_epoch: Duration, invoice_expiry_delta_secs: u32, payment_hash: PaymentHash,
523524
payment_secret: PaymentSecret, min_final_cltv_expiry_delta: Option<u16>,
524525
) -> Result<Bolt11Invoice, SignOrCreationError<()>>
@@ -536,6 +537,10 @@ where
536537
let our_node_pubkey = channelmanager.get_our_node_id();
537538
let channels = channelmanager.list_channels();
538539

540+
let network = Network::from_chain_hash(channelmanager.chain_hash)
541+
.map(Into::into)
542+
.unwrap_or(Currency::Bitcoin);
543+
539544
if min_final_cltv_expiry_delta.is_some() && min_final_cltv_expiry_delta.unwrap().saturating_add(3) < MIN_FINAL_CLTV_EXPIRY_DELTA {
540545
return Err(SignOrCreationError::CreationError(CreationError::MinFinalCltvExpiryDeltaTooShort));
541546
}
@@ -873,7 +878,7 @@ mod test {
873878
create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
874879
let non_default_invoice_expiry_secs = 4200;
875880
let invoice = create_invoice_from_channelmanager(
876-
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
881+
nodes[1].node, nodes[1].keys_manager, nodes[1].logger,
877882
Some(10_000), "test".to_string(), non_default_invoice_expiry_secs, None,
878883
).unwrap();
879884
assert_eq!(invoice.amount_milli_satoshis(), Some(10_000));
@@ -924,7 +929,7 @@ mod test {
924929
let custom_min_final_cltv_expiry_delta = Some(50);
925930

926931
let invoice = create_invoice_from_channelmanager(
927-
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
932+
nodes[1].node, nodes[1].keys_manager, nodes[1].logger,
928933
Some(10_000), "".into(), 3600,
929934
if with_custom_delta { custom_min_final_cltv_expiry_delta } else { None },
930935
).unwrap();
@@ -947,7 +952,7 @@ mod test {
947952
let custom_min_final_cltv_expiry_delta = Some(21);
948953

949954
let invoice = create_invoice_from_channelmanager(
950-
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
955+
nodes[1].node, nodes[1].keys_manager, nodes[1].logger,
951956
Some(10_000), "".into(), 3600, custom_min_final_cltv_expiry_delta,
952957
).unwrap();
953958
assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
@@ -961,7 +966,7 @@ mod test {
961966
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
962967
let description_hash = Sha256(Hash::hash("Testing description_hash".as_bytes()));
963968
let invoice = create_invoice_from_channelmanager_with_description_hash(
964-
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
969+
nodes[1].node, nodes[1].keys_manager, nodes[1].logger,
965970
Some(10_000), description_hash, 3600, None,
966971
).unwrap();
967972
assert_eq!(invoice.amount_milli_satoshis(), Some(10_000));
@@ -977,7 +982,7 @@ mod test {
977982
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
978983
let payment_hash = PaymentHash([0; 32]);
979984
let invoice = create_invoice_from_channelmanager_with_payment_hash(
980-
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
985+
nodes[1].node, nodes[1].keys_manager, nodes[1].logger,
981986
Some(10_000), "test".to_string(), 3600, payment_hash, None,
982987
).unwrap();
983988
assert_eq!(invoice.amount_milli_satoshis(), Some(10_000));
@@ -1269,7 +1274,7 @@ mod test {
12691274
) {
12701275
let invoice = create_invoice_from_channelmanager(
12711276
invoice_node.node, invoice_node.keys_manager, invoice_node.logger,
1272-
Currency::BitcoinTestnet, invoice_amt, "test".to_string(), 3600, None,
1277+
invoice_amt, "test".to_string(), 3600, None,
12731278
).unwrap();
12741279
let hints = invoice.private_routes();
12751280

@@ -1906,7 +1911,7 @@ mod test {
19061911
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
19071912
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
19081913
let result = create_invoice_from_channelmanager(
1909-
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
1914+
nodes[1].node, nodes[1].keys_manager, nodes[1].logger,
19101915
Some(10_000), "Some description".into(), 3600, Some(MIN_FINAL_CLTV_EXPIRY_DELTA - 4),
19111916
);
19121917
match result {

0 commit comments

Comments
 (0)