Skip to content

Swap the dep order between lightning and lightning-invoice #3234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"

members = [
"lightning",
"lightning-types",
"lightning-block-sync",
"lightning-invoice",
"lightning-net-tokio",
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/chanmon_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use lightning::ln::msgs::{
self, ChannelMessageHandler, CommitmentUpdate, DecodeError, Init, UpdateAddHTLC,
};
use lightning::ln::script::ShutdownScript;
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::ln::types::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice};
use lightning::offers::invoice_request::UnsignedInvoiceRequest;
use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath};
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use lightning::ln::peer_handler::{
IgnoringMessageHandler, MessageHandler, PeerManager, SocketDescriptor,
};
use lightning::ln::script::ShutdownScript;
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::ln::types::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice};
use lightning::offers::invoice_request::UnsignedInvoiceRequest;
use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath};
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use lightning::ln::channel_state::{ChannelCounterparty, ChannelDetails, ChannelS
use lightning::ln::channelmanager;
use lightning::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
use lightning::ln::msgs;
use lightning::ln::ChannelId;
use lightning::ln::types::ChannelId;
use lightning::offers::invoice::BlindedPayInfo;
use lightning::routing::gossip::{NetworkGraph, RoutingFees};
use lightning::routing::router::{
Expand Down
24 changes: 24 additions & 0 deletions lightning-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "lightning-types"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading the PR description I was expecting a name like lightning-common, but types is fine too I guess

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*-common seems a bit vague as it might imply actual "core" lightning functionality. I've seen the suffix be used a bit loosely to not only refer to types. *-types seems more appropriate IMO :)

Copy link
Collaborator Author

@TheBlueMatt TheBlueMatt Aug 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its also not really intended to be used by downstream crates, so I'm not sure it matters that much :)

version = "0.1.0"
authors = ["Matt Corallo"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/lightningdevkit/rust-lightning/"
description = """
Basic types which are used in the lightning network
"""
edition = "2021"

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]

[features]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When building lightning-types by itself, I found that the std/no-std features are missing:

Suggested change
[features]
[features]
default = ["std"]
no-std = ["bitcoin/no-std"]
std = ["bitcoin/std", "bech32/std"]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be resolved when we upgrade to rust-bitcoin 32 in the same release.


[dependencies]
bitcoin = { version = "0.31", default-features = false }
# TODO: Once we switch to bitcoin 0.32 drop this explicit dep:
hex-conservative = { version = "0.2", default-features = false }
bech32 = { version = "0.9", default-features = false }

[lints]
workspace = true
26 changes: 26 additions & 0 deletions lightning-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

#![crate_name = "lightning_types"]

//! Various types which are used in the lightning network.
//!
//! See the `lightning` crate for usage of these.

#![cfg_attr(not(test), no_std)]
#![deny(missing_docs)]
#![forbid(unsafe_code)]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(rustdoc::private_intra_doc_links)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

extern crate alloc;
extern crate core;

pub mod payment;
115 changes: 115 additions & 0 deletions lightning-types/src/payment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

//! Types which describe payments in lightning.

use alloc::vec::Vec;

use core::borrow::Borrow;

use bitcoin::hashes::{
Hash as _,
sha256::Hash as Sha256,
};

// TODO: Once we switch to rust-bitcoin 0.32, import this as bitcoin::hex
use hex_conservative::display::impl_fmt_traits;

/// The payment hash is the hash of the [`PaymentPreimage`] which is the value used to lock funds
/// in HTLCs while they transit the lightning network.
///
/// This is not exported to bindings users as we just use [u8; 32] directly
#[derive(Hash, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub struct PaymentHash(pub [u8; 32]);

impl Borrow<[u8]> for PaymentHash {
fn borrow(&self) -> &[u8] {
&self.0[..]
}
}

impl_fmt_traits! {
impl fmt_traits for PaymentHash {
const LENGTH: usize = 32;
}
}

/// The payment preimage is the "secret key" which is used to claim the funds of an HTLC on-chain
/// or in a lightning channel.
///
/// This is not exported to bindings users as we just use [u8; 32] directly
#[derive(Hash, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub struct PaymentPreimage(pub [u8; 32]);

impl Borrow<[u8]> for PaymentPreimage {
fn borrow(&self) -> &[u8] {
&self.0[..]
}
}

impl_fmt_traits! {
impl fmt_traits for PaymentPreimage {
const LENGTH: usize = 32;
}
}

/// Converts a `PaymentPreimage` into a `PaymentHash` by hashing the preimage with SHA256.
impl From<PaymentPreimage> for PaymentHash {
fn from(value: PaymentPreimage) -> Self {
PaymentHash(Sha256::hash(&value.0).to_byte_array())
}
}

/// The payment secret is used to authenticate the sender of an HTLC to the recipient and tie
/// multi-part HTLCs together into a single payment.
///
/// This is not exported to bindings users as we just use [u8; 32] directly
#[derive(Hash, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub struct PaymentSecret(pub [u8; 32]);

impl Borrow<[u8]> for PaymentSecret {
fn borrow(&self) -> &[u8] {
&self.0[..]
}
}

impl_fmt_traits! {
impl fmt_traits for PaymentSecret {
const LENGTH: usize = 32;
}
}

use bech32::{Base32Len, FromBase32, ToBase32, WriteBase32, u5};

impl FromBase32 for PaymentSecret {
type Err = bech32::Error;

fn from_base32(field_data: &[u5]) -> Result<PaymentSecret, bech32::Error> {
if field_data.len() != 52 {
return Err(bech32::Error::InvalidLength)
} else {
let data_bytes = Vec::<u8>::from_base32(field_data)?;
let mut payment_secret = [0; 32];
payment_secret.copy_from_slice(&data_bytes);
Ok(PaymentSecret(payment_secret))
}
}
}

impl ToBase32 for PaymentSecret {
fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
(&self.0[..]).write_base32(writer)
}
}

impl Base32Len for PaymentSecret {
fn base32_len(&self) -> usize {
52
}
}
2 changes: 2 additions & 0 deletions lightning/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ grind_signatures = []
default = ["std", "grind_signatures"]

[dependencies]
lightning-types = { version = "0.1", path = "../lightning-types", default-features = false }

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

Expand Down
3 changes: 3 additions & 0 deletions lightning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ compile_error!("Tests will always fail with cfg=fuzzing");

#[macro_use]
extern crate alloc;

extern crate lightning_types;

pub extern crate bitcoin;
#[cfg(any(test, feature = "std"))]
extern crate core;
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub mod features;
pub mod script;
pub mod types;

pub use types::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
pub use lightning_types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};

#[cfg(fuzzing)]
pub mod peer_channel_encryptor;
Expand Down
70 changes: 1 addition & 69 deletions lightning/src/ln/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,75 +121,7 @@ impl fmt::Display for ChannelId {
}
}


/// The payment hash is the hash of the [`PaymentPreimage`] which is the value used to lock funds
/// in HTLCs while they transit the lightning network.
///
/// This is not exported to bindings users as we just use [u8; 32] directly
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
pub struct PaymentHash(pub [u8; 32]);

impl core::fmt::Display for PaymentHash {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
crate::util::logger::DebugBytes(&self.0).fmt(f)
}
}

/// The payment preimage is the "secret key" which is used to claim the funds of an HTLC on-chain
/// or in a lightning channel.
///
/// This is not exported to bindings users as we just use [u8; 32] directly
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
pub struct PaymentPreimage(pub [u8; 32]);

impl core::fmt::Display for PaymentPreimage {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
crate::util::logger::DebugBytes(&self.0).fmt(f)
}
}

/// Converts a `PaymentPreimage` into a `PaymentHash` by hashing the preimage with SHA256.
impl From<PaymentPreimage> for PaymentHash {
fn from(value: PaymentPreimage) -> Self {
PaymentHash(Sha256::hash(&value.0).to_byte_array())
}
}

/// The payment secret is used to authenticate the sender of an HTLC to the recipient and tie
/// multi-part HTLCs together into a single payment.
///
/// This is not exported to bindings users as we just use [u8; 32] directly
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
pub struct PaymentSecret(pub [u8; 32]);

use bech32::{Base32Len, FromBase32, ToBase32, WriteBase32, u5};

impl FromBase32 for PaymentSecret {
type Err = bech32::Error;

fn from_base32(field_data: &[u5]) -> Result<PaymentSecret, bech32::Error> {
if field_data.len() != 52 {
return Err(bech32::Error::InvalidLength)
} else {
let data_bytes = Vec::<u8>::from_base32(field_data)?;
let mut payment_secret = [0; 32];
payment_secret.copy_from_slice(&data_bytes);
Ok(PaymentSecret(payment_secret))
}
}
}

impl ToBase32 for PaymentSecret {
fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
(&self.0[..]).write_base32(writer)
}
}

impl Base32Len for PaymentSecret {
fn base32_len(&self) -> usize {
52
}
}
pub use lightning_types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};

#[cfg(test)]
mod tests {
Expand Down