Skip to content

Commit 398314b

Browse files
authored
Merge pull request #3234 from TheBlueMatt/2024-08-lightning-dep-inv
Swap the dep order between `lightning` and `lightning-invoice`
2 parents fc21640 + ae59d1d commit 398314b

34 files changed

+1873
-1602
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ resolver = "2"
33

44
members = [
55
"lightning",
6+
"lightning-types",
67
"lightning-block-sync",
78
"lightning-invoice",
89
"lightning-net-tokio",

fuzz/src/chanmon_consistency.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use lightning::ln::msgs::{
5656
self, ChannelMessageHandler, CommitmentUpdate, DecodeError, Init, UpdateAddHTLC,
5757
};
5858
use lightning::ln::script::ShutdownScript;
59-
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
59+
use lightning::ln::types::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
6060
use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice};
6161
use lightning::offers::invoice_request::UnsignedInvoiceRequest;
6262
use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath};
@@ -71,6 +71,8 @@ use lightning::util::logger::Logger;
7171
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
7272
use lightning::util::test_channel_signer::{EnforcementState, TestChannelSigner};
7373

74+
use lightning_invoice::RawBolt11Invoice;
75+
7476
use crate::utils::test_logger::{self, Output};
7577
use crate::utils::test_persister::TestPersister;
7678

@@ -79,7 +81,6 @@ use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
7981
use bitcoin::secp256k1::schnorr;
8082
use bitcoin::secp256k1::{self, Message, PublicKey, Scalar, Secp256k1, SecretKey};
8183

82-
use bech32::u5;
8384
use std::cmp::{self, Ordering};
8485
use std::io::Cursor;
8586
use std::mem;
@@ -332,7 +333,7 @@ impl NodeSigner for KeyProvider {
332333
}
333334

334335
fn sign_invoice(
335-
&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient,
336+
&self, _invoice: &RawBolt11Invoice, _recipient: Recipient,
336337
) -> Result<RecoverableSignature, ()> {
337338
unreachable!()
338339
}

fuzz/src/full_stack.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use lightning::ln::peer_handler::{
4949
IgnoringMessageHandler, MessageHandler, PeerManager, SocketDescriptor,
5050
};
5151
use lightning::ln::script::ShutdownScript;
52-
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
52+
use lightning::ln::types::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
5353
use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice};
5454
use lightning::offers::invoice_request::UnsignedInvoiceRequest;
5555
use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath};
@@ -68,6 +68,8 @@ use lightning::util::logger::Logger;
6868
use lightning::util::ser::{Readable, ReadableArgs, Writeable};
6969
use lightning::util::test_channel_signer::{EnforcementState, TestChannelSigner};
7070

71+
use lightning_invoice::RawBolt11Invoice;
72+
7173
use crate::utils::test_logger;
7274
use crate::utils::test_persister::TestPersister;
7375

@@ -76,7 +78,6 @@ use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
7678
use bitcoin::secp256k1::schnorr;
7779
use bitcoin::secp256k1::{self, Message, PublicKey, Scalar, Secp256k1, SecretKey};
7880

79-
use bech32::u5;
8081
use std::cell::RefCell;
8182
use std::cmp;
8283
use std::convert::TryInto;
@@ -406,7 +407,7 @@ impl NodeSigner for KeyProvider {
406407
}
407408

408409
fn sign_invoice(
409-
&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient,
410+
&self, _invoice: &RawBolt11Invoice, _recipient: Recipient,
410411
) -> Result<RecoverableSignature, ()> {
411412
unreachable!()
412413
}

fuzz/src/onion_message.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Imports that need to be added manually
2-
use bech32::u5;
32
use bitcoin::script::ScriptBuf;
43
use bitcoin::secp256k1::ecdh::SharedSecret;
54
use bitcoin::secp256k1::ecdsa::RecoverableSignature;
@@ -27,6 +26,8 @@ use lightning::util::logger::Logger;
2726
use lightning::util::ser::{Readable, Writeable, Writer};
2827
use lightning::util::test_channel_signer::TestChannelSigner;
2928

29+
use lightning_invoice::RawBolt11Invoice;
30+
3031
use crate::utils::test_logger;
3132

3233
use std::io::{self, Cursor};
@@ -225,7 +226,7 @@ impl NodeSigner for KeyProvider {
225226
}
226227

227228
fn sign_invoice(
228-
&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient,
229+
&self, _invoice: &RawBolt11Invoice, _recipient: Recipient,
229230
) -> Result<RecoverableSignature, ()> {
230231
unreachable!()
231232
}

fuzz/src/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use lightning::ln::channel_state::{ChannelCounterparty, ChannelDetails, ChannelS
1818
use lightning::ln::channelmanager;
1919
use lightning::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
2020
use lightning::ln::msgs;
21-
use lightning::ln::ChannelId;
21+
use lightning::ln::types::ChannelId;
2222
use lightning::offers::invoice::BlindedPayInfo;
2323
use lightning::routing::gossip::{NetworkGraph, RoutingFees};
2424
use lightning::routing::router::{

lightning-invoice/Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +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 }
24-
lightning = { version = "0.0.123-beta", path = "../lightning", default-features = false }
24+
lightning-types = { version = "0.1", path = "../lightning-types", default-features = false }
2525
secp256k1 = { version = "0.28.0", default-features = false, features = ["recovery", "alloc"] }
2626
serde = { version = "1.0.118", optional = true }
2727
bitcoin = { version = "0.31.2", default-features = false }
2828

2929
[dev-dependencies]
30-
lightning = { version = "0.0.123-beta", path = "../lightning", default-features = false, features = ["_test_utils"] }
3130
serde_json = { version = "1"}
3231
hashbrown = { version = "0.13", default-features = false }
3332

lightning-invoice/src/de.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ use bitcoin::{PubkeyHash, ScriptHash, WitnessVersion};
1414
use bitcoin::hashes::Hash;
1515
use bitcoin::hashes::sha256;
1616
use crate::prelude::*;
17-
use lightning::ln::types::PaymentSecret;
18-
use lightning::routing::gossip::RoutingFees;
19-
use lightning::routing::router::{RouteHint, RouteHintHop};
17+
use lightning_types::payment::PaymentSecret;
18+
use lightning_types::routing::{RoutingFees, RouteHint, RouteHintHop};
2019

2120
use secp256k1::ecdsa::{RecoveryId, RecoverableSignature};
2221
use secp256k1::PublicKey;
@@ -918,8 +917,7 @@ mod test {
918917

919918
#[test]
920919
fn test_parse_route() {
921-
use lightning::routing::gossip::RoutingFees;
922-
use lightning::routing::router::{RouteHint, RouteHintHop};
920+
use lightning_types::routing::{RoutingFees, RouteHint, RouteHintHop};
923921
use crate::PrivateRoute;
924922
use bech32::FromBase32;
925923

@@ -974,7 +972,7 @@ mod test {
974972

975973
#[test]
976974
fn test_payment_secret_and_features_de_and_ser() {
977-
use lightning::ln::features::Bolt11InvoiceFeatures;
975+
use lightning_types::features::Bolt11InvoiceFeatures;
978976
use secp256k1::ecdsa::{RecoveryId, RecoverableSignature};
979977
use crate::TaggedField::*;
980978
use crate::{SiPrefix, SignedRawBolt11Invoice, Bolt11InvoiceSignature, RawBolt11Invoice, RawHrp, RawDataPart,

lightning-invoice/src/lib.rs

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@
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;
29+
extern crate lightning_types;
3330
extern crate secp256k1;
3431
extern crate alloc;
3532
#[cfg(any(test, feature = "std"))]
@@ -40,12 +37,11 @@ extern crate serde;
4037
#[cfg(feature = "std")]
4138
use std::time::SystemTime;
4239

43-
use bech32::u5;
40+
use bech32::{FromBase32, u5};
4441
use bitcoin::{Address, Network, PubkeyHash, ScriptHash, WitnessProgram, WitnessVersion};
4542
use bitcoin::address::Payload;
4643
use bitcoin::hashes::{Hash, sha256};
47-
use lightning::ln::features::Bolt11InvoiceFeatures;
48-
use lightning::util::invoice::construct_invoice_preimage;
44+
use lightning_types::features::Bolt11InvoiceFeatures;
4945

5046
use secp256k1::PublicKey;
5147
use secp256k1::{Message, Secp256k1};
@@ -64,12 +60,10 @@ use core::str;
6460
use serde::{Deserialize, Deserializer,Serialize, Serializer, de::Error};
6561

6662
#[doc(no_inline)]
67-
pub use lightning::ln::types::PaymentSecret;
68-
#[doc(no_inline)]
69-
pub use lightning::routing::router::{RouteHint, RouteHintHop};
63+
pub use lightning_types::payment::PaymentSecret;
7064
#[doc(no_inline)]
71-
pub use lightning::routing::gossip::RoutingFees;
72-
use lightning::util::string::UntrustedString;
65+
pub use lightning_types::routing::{RoutingFees, RouteHint, RouteHintHop};
66+
use lightning_types::string::UntrustedString;
7367

7468
mod de;
7569
mod ser;
@@ -139,19 +133,16 @@ pub const DEFAULT_EXPIRY_TIME: u64 = 3600;
139133

140134
/// Default minimum final CLTV expiry as defined by [BOLT 11].
141135
///
142-
/// Note that this is *not* the same value as rust-lightning's minimum CLTV expiry, which is
143-
/// provided in [`MIN_FINAL_CLTV_EXPIRY_DELTA`].
136+
/// Note that this is *not* the same value as rust-lightning's minimum CLTV expiry.
144137
///
145138
/// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
146-
/// [`MIN_FINAL_CLTV_EXPIRY_DELTA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA
147139
pub const DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA: u64 = 18;
148140

149141
/// Builder for [`Bolt11Invoice`]s. It's the most convenient and advised way to use this library. It
150142
/// ensures that only a semantically and syntactically correct invoice can be built using it.
151143
///
152144
/// ```
153145
/// extern crate secp256k1;
154-
/// extern crate lightning;
155146
/// extern crate lightning_invoice;
156147
/// extern crate bitcoin;
157148
///
@@ -161,7 +152,7 @@ pub const DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA: u64 = 18;
161152
/// use secp256k1::Secp256k1;
162153
/// use secp256k1::SecretKey;
163154
///
164-
/// use lightning::ln::types::PaymentSecret;
155+
/// use lightning_types::payment::PaymentSecret;
165156
///
166157
/// use lightning_invoice::{Currency, InvoiceBuilder};
167158
///
@@ -970,7 +961,23 @@ macro_rules! find_all_extract {
970961
impl RawBolt11Invoice {
971962
/// Hash the HRP as bytes and signatureless data part.
972963
fn hash_from_parts(hrp_bytes: &[u8], data_without_signature: &[u5]) -> [u8; 32] {
973-
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+
974981
let mut hash: [u8; 32] = Default::default();
975982
hash.copy_from_slice(&sha256::Hash::hash(&preimage)[..]);
976983
hash
@@ -1636,15 +1643,12 @@ pub enum CreationError {
16361643
/// The supplied millisatoshi amount was greater than the total bitcoin supply.
16371644
InvalidAmount,
16381645

1639-
/// Route hints were required for this invoice and were missing. Applies to
1640-
/// [phantom invoices].
1641-
///
1642-
/// [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.
16431649
MissingRouteHints,
16441650

1645-
/// The provided `min_final_cltv_expiry_delta` was less than [`MIN_FINAL_CLTV_EXPIRY_DELTA`].
1646-
///
1647-
/// [`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.
16481652
MinFinalCltvExpiryDeltaTooShort,
16491653
}
16501654

@@ -1877,14 +1881,14 @@ mod test {
18771881
#[test]
18781882
fn test_check_feature_bits() {
18791883
use crate::TaggedField::*;
1880-
use lightning::ln::features::Bolt11InvoiceFeatures;
1884+
use lightning_types::features::Bolt11InvoiceFeatures;
18811885
use secp256k1::Secp256k1;
18821886
use secp256k1::SecretKey;
18831887
use crate::{Bolt11Invoice, RawBolt11Invoice, RawHrp, RawDataPart, Currency, Sha256, PositiveTimestamp,
18841888
Bolt11SemanticError};
18851889

18861890
let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
1887-
let payment_secret = lightning::ln::types::PaymentSecret([21; 32]);
1891+
let payment_secret = lightning_types::payment::PaymentSecret([21; 32]);
18881892
let invoice_template = RawBolt11Invoice {
18891893
hrp: RawHrp {
18901894
currency: Currency::Bitcoin,
@@ -1998,7 +2002,7 @@ mod test {
19982002
#[test]
19992003
fn test_builder_fail() {
20002004
use crate::*;
2001-
use lightning::routing::router::RouteHintHop;
2005+
use lightning_types::routing::RouteHintHop;
20022006
use std::iter::FromIterator;
20032007
use secp256k1::PublicKey;
20042008

@@ -2052,7 +2056,7 @@ mod test {
20522056
#[test]
20532057
fn test_builder_ok() {
20542058
use crate::*;
2055-
use lightning::routing::router::RouteHintHop;
2059+
use lightning_types::routing::RouteHintHop;
20562060
use secp256k1::Secp256k1;
20572061
use secp256k1::{SecretKey, PublicKey};
20582062
use std::time::Duration;

lightning-invoice/tests/ser_de.rs

Lines changed: 0 additions & 1 deletion
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

lightning-types/Cargo.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "lightning-types"
3+
version = "0.1.0"
4+
authors = ["Matt Corallo"]
5+
license = "MIT OR Apache-2.0"
6+
repository = "https://github.com/lightningdevkit/rust-lightning/"
7+
description = """
8+
Basic types which are used in the lightning network
9+
"""
10+
edition = "2021"
11+
12+
[package.metadata.docs.rs]
13+
rustdoc-args = ["--cfg", "docsrs"]
14+
15+
[features]
16+
_test_utils = []
17+
18+
[dependencies]
19+
bitcoin = { version = "0.31", default-features = false }
20+
# TODO: Once we switch to bitcoin 0.32 drop this explicit dep:
21+
hex-conservative = { version = "0.2", default-features = false }
22+
bech32 = { version = "0.9", default-features = false }
23+
24+
[lints]
25+
workspace = true

0 commit comments

Comments
 (0)