Skip to content

Commit 92a5810

Browse files
committed
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.
1 parent 54754af commit 92a5810

File tree

7 files changed

+127
-118
lines changed

7 files changed

+127
-118
lines changed

lightning-invoice/Cargo.toml

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,17 @@ rustdoc-args = ["--cfg", "docsrs"]
1616

1717
[features]
1818
default = ["std"]
19-
no-std = ["lightning/no-std"]
20-
std = ["bitcoin/std", "lightning/std", "bech32/std"]
19+
no-std = ["bitcoin/no-std"]
20+
std = ["bitcoin/std", "bech32/std"]
2121

2222
[dependencies]
2323
bech32 = { version = "0.9.1", default-features = false }
2424
lightning-types = { version = "0.1", path = "../lightning-types", default-features = false }
25-
lightning = { version = "0.0.123-beta", path = "../lightning", default-features = false }
2625
secp256k1 = { version = "0.28.0", default-features = false, features = ["recovery", "alloc"] }
2726
serde = { version = "1.0.118", optional = true }
2827
bitcoin = { version = "0.31.2", default-features = false }
2928

3029
[dev-dependencies]
31-
lightning = { version = "0.0.123-beta", path = "../lightning", default-features = false, features = ["_test_utils"] }
3230
hex = { package = "hex-conservative", version = "0.1.1", default-features = false }
3331
serde_json = { version = "1"}
3432
hashbrown = { version = "0.13", default-features = false }

lightning-invoice/src/lib.rs

+23-18
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@
2525
#[cfg(not(any(feature = "std", feature = "no-std")))]
2626
compile_error!("at least one of the `std` or `no-std` features must be enabled");
2727

28-
pub mod payment;
29-
pub mod utils;
30-
3128
extern crate bech32;
32-
#[macro_use] extern crate lightning;
3329
extern crate lightning_types;
3430
extern crate secp256k1;
3531
extern crate alloc;
@@ -41,12 +37,11 @@ extern crate serde;
4137
#[cfg(feature = "std")]
4238
use std::time::SystemTime;
4339

44-
use bech32::u5;
40+
use bech32::{FromBase32, u5};
4541
use bitcoin::{Address, Network, PubkeyHash, ScriptHash, WitnessProgram, WitnessVersion};
4642
use bitcoin::address::Payload;
4743
use bitcoin::hashes::{Hash, sha256};
4844
use lightning_types::features::Bolt11InvoiceFeatures;
49-
use lightning::util::invoice::construct_invoice_preimage;
5045

5146
use secp256k1::PublicKey;
5247
use secp256k1::{Message, Secp256k1};
@@ -138,19 +133,16 @@ pub const DEFAULT_EXPIRY_TIME: u64 = 3600;
138133

139134
/// Default minimum final CLTV expiry as defined by [BOLT 11].
140135
///
141-
/// Note that this is *not* the same value as rust-lightning's minimum CLTV expiry, which is
142-
/// provided in [`MIN_FINAL_CLTV_EXPIRY_DELTA`].
136+
/// Note that this is *not* the same value as rust-lightning's minimum CLTV expiry.
143137
///
144138
/// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
145-
/// [`MIN_FINAL_CLTV_EXPIRY_DELTA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA
146139
pub const DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA: u64 = 18;
147140

148141
/// Builder for [`Bolt11Invoice`]s. It's the most convenient and advised way to use this library. It
149142
/// ensures that only a semantically and syntactically correct invoice can be built using it.
150143
///
151144
/// ```
152145
/// extern crate secp256k1;
153-
/// extern crate lightning;
154146
/// extern crate lightning_invoice;
155147
/// extern crate bitcoin;
156148
///
@@ -969,7 +961,23 @@ macro_rules! find_all_extract {
969961
impl RawBolt11Invoice {
970962
/// Hash the HRP as bytes and signatureless data part.
971963
fn hash_from_parts(hrp_bytes: &[u8], data_without_signature: &[u5]) -> [u8; 32] {
972-
let preimage = construct_invoice_preimage(hrp_bytes, data_without_signature);
964+
let mut preimage = Vec::<u8>::from(hrp_bytes);
965+
966+
let mut data_part = Vec::from(data_without_signature);
967+
let overhang = (data_part.len() * 5) % 8;
968+
if overhang > 0 {
969+
// add padding if data does not end at a byte boundary
970+
data_part.push(u5::try_from_u8(0).unwrap());
971+
972+
// if overhang is in (1..3) we need to add u5(0) padding two times
973+
if overhang < 3 {
974+
data_part.push(u5::try_from_u8(0).unwrap());
975+
}
976+
}
977+
978+
preimage.extend_from_slice(&Vec::<u8>::from_base32(&data_part)
979+
.expect("No padding error may occur due to appended zero above."));
980+
973981
let mut hash: [u8; 32] = Default::default();
974982
hash.copy_from_slice(&sha256::Hash::hash(&preimage)[..]);
975983
hash
@@ -1635,15 +1643,12 @@ pub enum CreationError {
16351643
/// The supplied millisatoshi amount was greater than the total bitcoin supply.
16361644
InvalidAmount,
16371645

1638-
/// Route hints were required for this invoice and were missing. Applies to
1639-
/// [phantom invoices].
1640-
///
1641-
/// [phantom invoices]: crate::utils::create_phantom_invoice
1646+
// TODO: These two errors are really errors with things in the `lightning` crate and thus
1647+
// shouldn't live here.
1648+
/// Route hints were required for this invoice and were missing.
16421649
MissingRouteHints,
16431650

1644-
/// The provided `min_final_cltv_expiry_delta` was less than [`MIN_FINAL_CLTV_EXPIRY_DELTA`].
1645-
///
1646-
/// [`MIN_FINAL_CLTV_EXPIRY_DELTA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA
1651+
/// The provided `min_final_cltv_expiry_delta` was less than rust-lightning's minimum.
16471652
MinFinalCltvExpiryDeltaTooShort,
16481653
}
16491654

lightning-invoice/tests/ser_de.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
extern crate bech32;
2-
extern crate lightning;
32
extern crate lightning_invoice;
43
extern crate secp256k1;
54
extern crate hex;

lightning/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ unsafe_revoked_tx_signing = []
3131
# Override signing to not include randomness when generating signatures for test vectors.
3232
_test_vectors = []
3333

34-
no-std = ["hashbrown", "possiblyrandom", "bitcoin/no-std", "core2/alloc", "libm"]
35-
std = ["bitcoin/std", "bech32/std"]
34+
no-std = ["hashbrown", "possiblyrandom", "bitcoin/no-std", "lightning-invoice/no-std", "core2/alloc", "libm"]
35+
std = ["bitcoin/std", "bech32/std", "lightning-invoice/std"]
3636

3737
# Generates low-r bitcoin signatures, which saves 1 byte in 50% of the cases
3838
grind_signatures = []
@@ -41,6 +41,7 @@ default = ["std", "grind_signatures"]
4141

4242
[dependencies]
4343
lightning-types = { version = "0.1", path = "../lightning-types", default-features = false }
44+
lightning-invoice = { version = "0.31.0-beta", path = "../lightning-invoice", default-features = false }
4445

4546
bech32 = { version = "0.9.1", default-features = false }
4647
bitcoin = { version = "0.31.2", default-features = false, features = ["secp-recovery"] }

lightning-invoice/src/payment.rs renamed to lightning/src/ln/bolt11_payment.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
//! Convenient utilities for paying Lightning invoices.
1111
12-
use crate::Bolt11Invoice;
1312
use bitcoin::hashes::Hash;
13+
use lightning_invoice::Bolt11Invoice;
1414

15-
use lightning::ln::types::PaymentHash;
16-
use lightning::ln::channelmanager::RecipientOnionFields;
17-
use lightning::routing::router::{PaymentParameters, RouteParameters};
15+
use crate::ln::channelmanager::RecipientOnionFields;
16+
use crate::ln::types::PaymentHash;
17+
use crate::routing::router::{PaymentParameters, RouteParameters};
1818

1919
/// Builds the necessary parameters to pay or pre-flight probe the given zero-amount
2020
/// [`Bolt11Invoice`] using [`ChannelManager::send_payment`] or
@@ -26,8 +26,8 @@ use lightning::routing::router::{PaymentParameters, RouteParameters};
2626
/// Will always succeed unless the invoice has an amount specified, in which case
2727
/// [`payment_parameters_from_invoice`] should be used.
2828
///
29-
/// [`ChannelManager::send_payment`]: lightning::ln::channelmanager::ChannelManager::send_payment
30-
/// [`ChannelManager::send_preflight_probes`]: lightning::ln::channelmanager::ChannelManager::send_preflight_probes
29+
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
30+
/// [`ChannelManager::send_preflight_probes`]: crate::ln::channelmanager::ChannelManager::send_preflight_probes
3131
pub fn payment_parameters_from_zero_amount_invoice(invoice: &Bolt11Invoice, amount_msat: u64)
3232
-> Result<(PaymentHash, RecipientOnionFields, RouteParameters), ()> {
3333
if invoice.amount_milli_satoshis().is_some() {
@@ -46,8 +46,8 @@ pub fn payment_parameters_from_zero_amount_invoice(invoice: &Bolt11Invoice, amou
4646
/// Will always succeed unless the invoice has no amount specified, in which case
4747
/// [`payment_parameters_from_zero_amount_invoice`] should be used.
4848
///
49-
/// [`ChannelManager::send_payment`]: lightning::ln::channelmanager::ChannelManager::send_payment
50-
/// [`ChannelManager::send_preflight_probes`]: lightning::ln::channelmanager::ChannelManager::send_preflight_probes
49+
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
50+
/// [`ChannelManager::send_preflight_probes`]: crate::ln::channelmanager::ChannelManager::send_preflight_probes
5151
pub fn payment_parameters_from_invoice(invoice: &Bolt11Invoice)
5252
-> Result<(PaymentHash, RecipientOnionFields, RouteParameters), ()> {
5353
if let Some(amount_msat) = invoice.amount_milli_satoshis() {
@@ -83,11 +83,11 @@ fn params_from_invoice(invoice: &Bolt11Invoice, amount_msat: u64)
8383
#[cfg(test)]
8484
mod tests {
8585
use super::*;
86-
use crate::{InvoiceBuilder, Currency};
86+
use lightning_invoice::{InvoiceBuilder, Currency};
8787
use bitcoin::hashes::sha256::Hash as Sha256;
88-
use lightning::ln::types::PaymentSecret;
89-
use lightning::routing::router::Payee;
90-
use secp256k1::{SecretKey, PublicKey, Secp256k1};
88+
use crate::ln::types::PaymentSecret;
89+
use crate::routing::router::Payee;
90+
use bitcoin::secp256k1::{SecretKey, PublicKey, Secp256k1};
9191
use core::time::Duration;
9292
#[cfg(feature = "std")]
9393
use std::time::SystemTime;
@@ -169,10 +169,10 @@ mod tests {
169169
#[test]
170170
#[cfg(feature = "std")]
171171
fn payment_metadata_end_to_end() {
172-
use lightning::events::Event;
173-
use lightning::ln::channelmanager::{Retry, PaymentId};
174-
use lightning::ln::msgs::ChannelMessageHandler;
175-
use lightning::ln::functional_test_utils::*;
172+
use crate::events::Event;
173+
use crate::ln::channelmanager::{Retry, PaymentId};
174+
use crate::ln::msgs::ChannelMessageHandler;
175+
use crate::ln::functional_test_utils::*;
176176
// Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
177177
// the way out through the `PaymentClaimable` event.
178178
let chanmon_cfgs = create_chanmon_cfgs(2);

0 commit comments

Comments
 (0)