diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index 77c4fb66847..ad1056ea922 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -932,7 +932,7 @@ mod tests { use lightning::chain::transaction::OutPoint; use lightning::events::{Event, PathFailure, MessageSendEventsProvider, MessageSendEvent}; use lightning::{get_event_msg, get_event}; - use lightning::ln::{PaymentHash, ChannelId}; + use lightning::ln::types::{PaymentHash, ChannelId}; use lightning::ln::channelmanager; use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters, MIN_CLTV_EXPIRY_DELTA, PaymentId}; use lightning::ln::features::{ChannelFeatures, NodeFeatures}; diff --git a/lightning-invoice/src/de.rs b/lightning-invoice/src/de.rs index 56e5c53ba1f..674518272d0 100644 --- a/lightning-invoice/src/de.rs +++ b/lightning-invoice/src/de.rs @@ -15,7 +15,7 @@ use bitcoin::address::WitnessVersion; use bitcoin::hashes::Hash; use bitcoin::hashes::sha256; use crate::prelude::*; -use lightning::ln::PaymentSecret; +use lightning::ln::types::PaymentSecret; use lightning::routing::gossip::RoutingFees; use lightning::routing::router::{RouteHint, RouteHintHop}; diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index f06eeaa1744..920d44b1561 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -65,7 +65,7 @@ use core::str; use serde::{Deserialize, Deserializer,Serialize, Serializer, de::Error}; #[doc(no_inline)] -pub use lightning::ln::PaymentSecret; +pub use lightning::ln::types::PaymentSecret; #[doc(no_inline)] pub use lightning::routing::router::{RouteHint, RouteHintHop}; #[doc(no_inline)] @@ -162,7 +162,7 @@ pub const DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA: u64 = 18; /// use secp256k1::Secp256k1; /// use secp256k1::SecretKey; /// -/// use lightning::ln::PaymentSecret; +/// use lightning::ln::types::PaymentSecret; /// /// use lightning_invoice::{Currency, InvoiceBuilder}; /// @@ -1877,7 +1877,7 @@ mod test { Bolt11SemanticError}; let private_key = SecretKey::from_slice(&[42; 32]).unwrap(); - let payment_secret = lightning::ln::PaymentSecret([21; 32]); + let payment_secret = lightning::ln::types::PaymentSecret([21; 32]); let invoice_template = RawBolt11Invoice { hrp: RawHrp { currency: Currency::Bitcoin, diff --git a/lightning-invoice/src/payment.rs b/lightning-invoice/src/payment.rs index 8196fa9eb89..a8ffce7bbd9 100644 --- a/lightning-invoice/src/payment.rs +++ b/lightning-invoice/src/payment.rs @@ -12,7 +12,7 @@ use crate::Bolt11Invoice; use bitcoin::hashes::Hash; -use lightning::ln::PaymentHash; +use lightning::ln::types::PaymentHash; use lightning::ln::channelmanager::RecipientOnionFields; use lightning::routing::router::{PaymentParameters, RouteParameters}; @@ -85,7 +85,7 @@ mod tests { use super::*; use crate::{InvoiceBuilder, Currency}; use bitcoin::hashes::sha256::Hash as Sha256; - use lightning::ln::PaymentSecret; + use lightning::ln::types::PaymentSecret; use lightning::routing::router::Payee; use secp256k1::{SecretKey, PublicKey, Secp256k1}; use core::time::Duration; diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index 1d6b9210afd..22e493ae1c5 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -8,7 +8,7 @@ use bitcoin::hashes::Hash; use lightning::chain; use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use lightning::sign::{Recipient, NodeSigner, SignerProvider, EntropySource}; -use lightning::ln::{PaymentHash, PaymentSecret}; +use lightning::ln::types::{PaymentHash, PaymentSecret}; use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, MIN_FINAL_CLTV_EXPIRY_DELTA}; use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA}; use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey}; @@ -824,9 +824,9 @@ mod test { use bitcoin::hashes::sha256::Hash as Sha256; use lightning::sign::PhantomKeysManager; use lightning::events::{MessageSendEvent, MessageSendEventsProvider}; - use lightning::ln::PaymentHash; + use lightning::ln::types::PaymentHash; #[cfg(feature = "std")] - use lightning::ln::PaymentPreimage; + use lightning::ln::types::PaymentPreimage; use lightning::ln::channelmanager::{PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY_DELTA, PaymentId, RecipientOnionFields, Retry}; use lightning::ln::functional_test_utils::*; use lightning::ln::msgs::ChannelMessageHandler; diff --git a/lightning/src/blinded_path/message.rs b/lightning/src/blinded_path/message.rs index df7f8e7ad61..9a282a5f87d 100644 --- a/lightning/src/blinded_path/message.rs +++ b/lightning/src/blinded_path/message.rs @@ -3,7 +3,7 @@ use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey}; #[allow(unused_imports)] use crate::prelude::*; -use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NodeIdLookUp}; +use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp}; use crate::blinded_path::utils; use crate::io; use crate::io::Cursor; @@ -20,7 +20,7 @@ use core::ops::Deref; /// route, they are encoded into [`BlindedHop::encrypted_payload`]. pub(crate) struct ForwardTlvs { /// The next hop in the onion message's path. - pub(crate) next_hop: NextHop, + pub(crate) next_hop: NextMessageHop, /// Senders to a blinded path use this value to concatenate the route they find to the /// introduction node with the blinded path. pub(crate) next_blinding_override: Option, @@ -34,20 +34,11 @@ pub(crate) struct ReceiveTlvs { pub(crate) path_id: Option<[u8; 32]>, } -/// The next hop to forward the onion message along its path. -#[derive(Debug)] -pub enum NextHop { - /// The node id of the next hop. - NodeId(PublicKey), - /// The short channel id leading to the next hop. - ShortChannelId(u64), -} - impl Writeable for ForwardTlvs { fn write(&self, writer: &mut W) -> Result<(), io::Error> { let (next_node_id, short_channel_id) = match self.next_hop { - NextHop::NodeId(pubkey) => (Some(pubkey), None), - NextHop::ShortChannelId(scid) => (None, Some(scid)), + NextMessageHop::NodeId(pubkey) => (Some(pubkey), None), + NextMessageHop::ShortChannelId(scid) => (None, Some(scid)), }; // TODO: write padding encode_tlv_stream!(writer, { @@ -75,7 +66,7 @@ pub(super) fn blinded_hops( ) -> Result, secp256k1::Error> { let blinded_tlvs = unblinded_path.iter() .skip(1) // The first node's TLVs contains the next node's pubkey - .map(|pk| ForwardTlvs { next_hop: NextHop::NodeId(*pk), next_blinding_override: None }) + .map(|pk| ForwardTlvs { next_hop: NextMessageHop::NodeId(*pk), next_blinding_override: None }) .map(|tlvs| ControlTlvs::Forward(tlvs)) .chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { path_id: None }))); @@ -102,8 +93,8 @@ where readable: ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override }) }) => { let next_node_id = match next_hop { - NextHop::NodeId(pubkey) => pubkey, - NextHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) { + NextMessageHop::NodeId(pubkey) => pubkey, + NextMessageHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) { Some(pubkey) => pubkey, None => return Err(()), }, diff --git a/lightning/src/blinded_path/mod.rs b/lightning/src/blinded_path/mod.rs index 07fa7b77024..3dc6b121b00 100644 --- a/lightning/src/blinded_path/mod.rs +++ b/lightning/src/blinded_path/mod.rs @@ -24,6 +24,17 @@ use crate::util::ser::{Readable, Writeable, Writer}; use crate::io; use crate::prelude::*; +/// The next hop to forward an onion message along its path. +/// +/// Note that payment blinded paths always specify their next hop using an explicit node id. +#[derive(Clone, Debug, Hash, PartialEq, Eq)] +pub enum NextMessageHop { + /// The node id of the next hop. + NodeId(PublicKey), + /// The short channel id leading to the next hop. + ShortChannelId(u64), +} + /// Onion messages and payments can be sent and received to blinded paths, which serve to hide the /// identity of the recipient. #[derive(Clone, Debug, Hash, PartialEq, Eq)] diff --git a/lightning/src/blinded_path/payment.rs b/lightning/src/blinded_path/payment.rs index ec441c18c98..dfe89e0ebc6 100644 --- a/lightning/src/blinded_path/payment.rs +++ b/lightning/src/blinded_path/payment.rs @@ -7,7 +7,7 @@ use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey}; use crate::blinded_path::BlindedHop; use crate::blinded_path::utils; use crate::io; -use crate::ln::PaymentSecret; +use crate::ln::types::PaymentSecret; use crate::ln::channelmanager::CounterpartyForwardingInfo; use crate::ln::features::BlindedHopFeatures; use crate::ln::msgs::DecodeError; @@ -425,7 +425,7 @@ impl_writeable_tlv_based!(Bolt12RefundContext, {}); mod tests { use bitcoin::secp256k1::PublicKey; use crate::blinded_path::payment::{ForwardNode, ForwardTlvs, ReceiveTlvs, PaymentConstraints, PaymentContext, PaymentRelay}; - use crate::ln::PaymentSecret; + use crate::ln::types::PaymentSecret; use crate::ln::features::BlindedHopFeatures; use crate::ln::functional_test_utils::TEST_FINAL_CLTV; diff --git a/lightning/src/chain/chainmonitor.rs b/lightning/src/chain/chainmonitor.rs index 8e567087054..ccb30e132c2 100644 --- a/lightning/src/chain/chainmonitor.rs +++ b/lightning/src/chain/chainmonitor.rs @@ -31,7 +31,7 @@ use crate::chain::{ChannelMonitorUpdateStatus, Filter, WatchedOutput}; use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, Balance, MonitorEvent, TransactionOutputs, WithChannelMonitor, LATENCY_GRACE_PERIOD_BLOCKS}; use crate::chain::transaction::{OutPoint, TransactionData}; -use crate::ln::ChannelId; +use crate::ln::types::ChannelId; use crate::sign::ecdsa::WriteableEcdsaChannelSigner; use crate::events; use crate::events::{Event, EventHandler}; diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index a7c88cd08a2..b8598eabb97 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -34,7 +34,7 @@ use bitcoin::secp256k1; use bitcoin::sighash::EcdsaSighashType; use crate::ln::channel::INITIAL_COMMITMENT_NUMBER; -use crate::ln::{PaymentHash, PaymentPreimage, ChannelId}; +use crate::ln::types::{PaymentHash, PaymentPreimage, ChannelId}; use crate::ln::msgs::DecodeError; use crate::ln::channel_keys::{DelayedPaymentKey, DelayedPaymentBasepoint, HtlcBasepoint, HtlcKey, RevocationKey, RevocationBasepoint}; use crate::ln::chan_utils::{self,CommitmentTransaction, CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCClaim, ChannelTransactionParameters, HolderCommitmentTransaction, TxCreationKeys}; @@ -4811,7 +4811,7 @@ mod tests { use crate::chain::package::{weight_offered_htlc, weight_received_htlc, weight_revoked_offered_htlc, weight_revoked_received_htlc, WEIGHT_REVOKED_OUTPUT}; use crate::chain::transaction::OutPoint; use crate::sign::InMemorySigner; - use crate::ln::{PaymentPreimage, PaymentHash, ChannelId}; + use crate::ln::types::{PaymentPreimage, PaymentHash, ChannelId}; use crate::ln::channel_keys::{DelayedPaymentBasepoint, DelayedPaymentKey, HtlcBasepoint, RevocationBasepoint, RevocationKey}; use crate::ln::chan_utils::{self,HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters}; use crate::ln::channelmanager::{PaymentSendFailure, PaymentId, RecipientOnionFields}; diff --git a/lightning/src/chain/mod.rs b/lightning/src/chain/mod.rs index 1fb30a9aeb5..a8afc7a1acb 100644 --- a/lightning/src/chain/mod.rs +++ b/lightning/src/chain/mod.rs @@ -17,7 +17,7 @@ use bitcoin::network::constants::Network; use bitcoin::secp256k1::PublicKey; use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, MonitorEvent}; -use crate::ln::ChannelId; +use crate::ln::types::ChannelId; use crate::sign::ecdsa::WriteableEcdsaChannelSigner; use crate::chain::transaction::{OutPoint, TransactionData}; use crate::impl_writeable_tlv_based; diff --git a/lightning/src/chain/onchaintx.rs b/lightning/src/chain/onchaintx.rs index 94d6aa35746..4b4a4dcd1e2 100644 --- a/lightning/src/chain/onchaintx.rs +++ b/lightning/src/chain/onchaintx.rs @@ -25,7 +25,7 @@ use bitcoin::secp256k1; use crate::chain::chaininterface::compute_feerate_sat_per_1000_weight; use crate::sign::{ChannelDerivationParameters, HTLCDescriptor, ChannelSigner, EntropySource, SignerProvider, ecdsa::WriteableEcdsaChannelSigner}; use crate::ln::msgs::DecodeError; -use crate::ln::PaymentPreimage; +use crate::ln::types::PaymentPreimage; use crate::ln::chan_utils::{self, ChannelTransactionParameters, HTLCOutputInCommitment, HolderCommitmentTransaction}; use crate::chain::ClaimId; use crate::chain::chaininterface::{ConfirmationTarget, FeeEstimator, BroadcasterInterface, LowerBoundedFeeEstimator}; diff --git a/lightning/src/chain/package.rs b/lightning/src/chain/package.rs index 3023be604d3..1bbcd6e0451 100644 --- a/lightning/src/chain/package.rs +++ b/lightning/src/chain/package.rs @@ -22,7 +22,7 @@ use bitcoin::hash_types::Txid; use bitcoin::secp256k1::{SecretKey,PublicKey}; use bitcoin::sighash::EcdsaSighashType; -use crate::ln::PaymentPreimage; +use crate::ln::types::PaymentPreimage; use crate::ln::chan_utils::{self, TxCreationKeys, HTLCOutputInCommitment}; use crate::ln::features::ChannelTypeFeatures; use crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint}; @@ -1195,7 +1195,7 @@ mod tests { use crate::chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderHTLCOutput, PackageTemplate, PackageSolvingData, RevokedOutput, WEIGHT_REVOKED_OUTPUT, weight_offered_htlc, weight_received_htlc}; use crate::chain::Txid; use crate::ln::chan_utils::HTLCOutputInCommitment; - use crate::ln::{PaymentPreimage, PaymentHash}; + use crate::ln::types::{PaymentPreimage, PaymentHash}; use crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint}; use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR; diff --git a/lightning/src/chain/transaction.rs b/lightning/src/chain/transaction.rs index 7a447dd5d90..5450586ee96 100644 --- a/lightning/src/chain/transaction.rs +++ b/lightning/src/chain/transaction.rs @@ -88,7 +88,7 @@ impl MaybeSignedTransaction { #[cfg(test)] mod tests { use crate::chain::transaction::OutPoint; - use crate::ln::ChannelId; + use crate::ln::types::ChannelId; use bitcoin::blockdata::transaction::Transaction; use bitcoin::consensus::encode; diff --git a/lightning/src/events/bump_transaction.rs b/lightning/src/events/bump_transaction.rs index 8b52af69fe3..9c24e00f13e 100644 --- a/lightning/src/events/bump_transaction.rs +++ b/lightning/src/events/bump_transaction.rs @@ -18,7 +18,7 @@ use crate::chain::chaininterface::{BroadcasterInterface, fee_for_weight}; use crate::chain::ClaimId; use crate::io_extras::sink; use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI; -use crate::ln::ChannelId; +use crate::ln::types::ChannelId; use crate::ln::chan_utils; use crate::ln::chan_utils::{ ANCHOR_INPUT_WITNESS_WEIGHT, HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT, diff --git a/lightning/src/events/mod.rs b/lightning/src/events/mod.rs index e72bc0228fd..2a1f698782b 100644 --- a/lightning/src/events/mod.rs +++ b/lightning/src/events/mod.rs @@ -24,7 +24,7 @@ use crate::ln::channelmanager::{InterceptId, PaymentId, RecipientOnionFields}; use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS; use crate::ln::features::ChannelTypeFeatures; use crate::ln::msgs; -use crate::ln::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret}; +use crate::ln::types::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret}; use crate::chain::transaction; use crate::routing::gossip::NetworkUpdate; use crate::util::errors::APIError; diff --git a/lightning/src/ln/blinded_payment_tests.rs b/lightning/src/ln/blinded_payment_tests.rs index a0438b2447a..12e91b42172 100644 --- a/lightning/src/ln/blinded_payment_tests.rs +++ b/lightning/src/ln/blinded_payment_tests.rs @@ -11,7 +11,7 @@ use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey}; use crate::blinded_path::BlindedPath; use crate::blinded_path::payment::{ForwardNode, ForwardTlvs, PaymentConstraints, PaymentContext, PaymentRelay, ReceiveTlvs}; use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PaymentFailureReason}; -use crate::ln::PaymentSecret; +use crate::ln::types::PaymentSecret; use crate::ln::channelmanager; use crate::ln::channelmanager::{PaymentId, RecipientOnionFields}; use crate::ln::features::BlindedHopFeatures; diff --git a/lightning/src/ln/chan_utils.rs b/lightning/src/ln/chan_utils.rs index b7676ddff15..3c99cdb0943 100644 --- a/lightning/src/ln/chan_utils.rs +++ b/lightning/src/ln/chan_utils.rs @@ -25,7 +25,7 @@ use bitcoin::hash_types::{Txid, PubkeyHash, WPubkeyHash}; use crate::chain::chaininterface::fee_for_weight; use crate::chain::package::WEIGHT_REVOKED_OUTPUT; use crate::sign::EntropySource; -use crate::ln::{PaymentHash, PaymentPreimage}; +use crate::ln::types::{PaymentHash, PaymentPreimage}; use crate::ln::msgs::DecodeError; use crate::util::ser::{Readable, RequiredWrapper, Writeable, Writer}; use crate::util::transaction_utils; @@ -1826,7 +1826,7 @@ mod tests { use bitcoin::{Network, Txid, ScriptBuf}; use bitcoin::hashes::Hash; use bitcoin::hashes::hex::FromHex; - use crate::ln::PaymentHash; + use crate::ln::types::PaymentHash; use bitcoin::address::Payload; use bitcoin::PublicKey as BitcoinPublicKey; use crate::ln::features::ChannelTypeFeatures; diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs index 11ab1fbb851..7d332f2ad78 100644 --- a/lightning/src/ln/chanmon_update_fail_tests.rs +++ b/lightning/src/ln/chanmon_update_fail_tests.rs @@ -21,7 +21,8 @@ use crate::chain::{ChannelMonitorUpdateStatus, Listen, Watch}; use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose, ClosureReason, HTLCDestination}; use crate::ln::channelmanager::{RAACommitmentOrder, PaymentSendFailure, PaymentId, RecipientOnionFields}; use crate::ln::channel::{AnnouncementSigsState, ChannelPhase}; -use crate::ln::{msgs, ChannelId}; +use crate::ln::msgs; +use crate::ln::types::ChannelId; use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler}; use crate::util::test_channel_signer::TestChannelSigner; use crate::util::errors::APIError; diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 66301c95bd6..afe265c8a6a 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -24,7 +24,7 @@ use bitcoin::secp256k1::{PublicKey,SecretKey}; use bitcoin::secp256k1::{Secp256k1,ecdsa::Signature}; use bitcoin::secp256k1; -use crate::ln::{ChannelId, PaymentPreimage, PaymentHash}; +use crate::ln::types::{ChannelId, PaymentPreimage, PaymentHash}; use crate::ln::features::{ChannelTypeFeatures, InitFeatures}; use crate::ln::msgs; use crate::ln::msgs::DecodeError; @@ -9338,7 +9338,7 @@ mod tests { use bitcoin::blockdata::opcodes; use bitcoin::network::constants::Network; use crate::ln::onion_utils::INVALID_ONION_BLINDING; - use crate::ln::{PaymentHash, PaymentPreimage}; + use crate::ln::types::{PaymentHash, PaymentPreimage}; use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint}; use crate::ln::channelmanager::{self, HTLCSource, PaymentId}; use crate::ln::channel::InitFeatures; diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 721f95c08b7..61f1f18166e 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -42,7 +42,8 @@ use crate::events; use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination, PaymentFailureReason}; // Since this struct is returned in `list_channels` methods, expose it here in case users want to // construct one themselves. -use crate::ln::{inbound_payment, ChannelId, PaymentHash, PaymentPreimage, PaymentSecret}; +use crate::ln::inbound_payment; +use crate::ln::types::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret}; use crate::ln::channel::{self, Channel, ChannelPhase, ChannelContext, ChannelError, ChannelUpdateStatus, ShutdownResult, UnfundedChannelContext, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel, WithChannelContext}; pub use crate::ln::channel::{InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails, OutboundHTLCStateDetails}; use crate::ln::features::{Bolt12InvoiceFeatures, ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures}; @@ -1387,7 +1388,7 @@ where /// /// ``` /// # use bitcoin::secp256k1::PublicKey; -/// # use lightning::ln::ChannelId; +/// # use lightning::ln::types::ChannelId; /// # use lightning::ln::channelmanager::AChannelManager; /// # use lightning::events::{Event, EventsProvider}; /// # @@ -1490,7 +1491,7 @@ where /// /// ``` /// # use lightning::events::{Event, EventsProvider}; -/// # use lightning::ln::PaymentHash; +/// # use lightning::ln::types::PaymentHash; /// # use lightning::ln::channelmanager::{AChannelManager, PaymentId, RecentPaymentDetails, RecipientOnionFields, Retry}; /// # use lightning::routing::router::RouteParameters; /// # @@ -12374,8 +12375,7 @@ mod tests { use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey}; use core::sync::atomic::Ordering; use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, ClosureReason}; - use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret}; - use crate::ln::ChannelId; + use crate::ln::types::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret}; use crate::ln::channelmanager::{create_recv_pending_htlc_info, HTLCForwardInfo, inbound_payment, PaymentId, PaymentSendFailure, RecipientOnionFields, InterceptId}; use crate::ln::functional_test_utils::*; use crate::ln::msgs::{self, ErrorAction}; diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index cf52d946e34..c5361318fca 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -15,7 +15,7 @@ use crate::chain::channelmonitor::ChannelMonitor; use crate::chain::transaction::OutPoint; use crate::events::{ClaimedHTLC, ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, PaymentFailureReason}; use crate::events::bump_transaction::{BumpTransactionEvent, BumpTransactionEventHandler, Wallet, WalletSource}; -use crate::ln::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret}; +use crate::ln::types::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret}; use crate::ln::channelmanager::{AChannelManager, ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, PaymentId, MIN_CLTV_EXPIRY_DELTA}; use crate::ln::features::InitFeatures; use crate::ln::msgs; diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 151626f94ab..e068e86b4c6 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -19,7 +19,7 @@ use crate::chain::channelmonitor::{CLOSED_CHANNEL_UPDATE_ID, CLTV_CLAIM_BUFFER, use crate::chain::transaction::OutPoint; use crate::sign::{ecdsa::EcdsaChannelSigner, EntropySource, OutputSpender, SignerProvider}; use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, ClosureReason, HTLCDestination, PaymentFailureReason}; -use crate::ln::{ChannelId, PaymentPreimage, PaymentSecret, PaymentHash}; +use crate::ln::types::{ChannelId, PaymentPreimage, PaymentSecret, PaymentHash}; use crate::ln::channel::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT, get_holder_selected_channel_reserve_satoshis, OutboundV1Channel, InboundV1Channel, COINBASE_MATURITY, ChannelPhase}; use crate::ln::channelmanager::{self, PaymentId, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, BREAKDOWN_TIMEOUT, ENABLE_GOSSIP_TICKS, DISABLE_GOSSIP_TICKS, MIN_CLTV_EXPIRY_DELTA}; use crate::ln::channel::{DISCONNECT_PEER_AWAITING_RESPONSE_TICKS, ChannelError}; diff --git a/lightning/src/ln/inbound_payment.rs b/lightning/src/ln/inbound_payment.rs index f27d8aab119..a85d93b7135 100644 --- a/lightning/src/ln/inbound_payment.rs +++ b/lightning/src/ln/inbound_payment.rs @@ -14,7 +14,7 @@ use bitcoin::hashes::cmp::fixed_time_eq; use bitcoin::hashes::hmac::{Hmac, HmacEngine}; use bitcoin::hashes::sha256::Hash as Sha256; use crate::sign::{KeyMaterial, EntropySource}; -use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; +use crate::ln::types::{PaymentHash, PaymentPreimage, PaymentSecret}; use crate::ln::msgs; use crate::ln::msgs::MAX_VALUE_MSAT; use crate::crypto::chacha20::ChaCha20; diff --git a/lightning/src/ln/interactivetxs.rs b/lightning/src/ln/interactivetxs.rs index 5f01bcd163b..60341620887 100644 --- a/lightning/src/ln/interactivetxs.rs +++ b/lightning/src/ln/interactivetxs.rs @@ -22,8 +22,9 @@ use bitcoin::{ use crate::chain::chaininterface::fee_for_weight; use crate::events::bump_transaction::{BASE_INPUT_WEIGHT, EMPTY_SCRIPT_SIG_WEIGHT}; use crate::ln::channel::TOTAL_BITCOIN_SUPPLY_SATOSHIS; +use crate::ln::msgs; use crate::ln::msgs::SerialId; -use crate::ln::{msgs, ChannelId}; +use crate::ln::types::ChannelId; use crate::sign::{EntropySource, P2TR_KEY_PATH_WITNESS_WEIGHT, P2WPKH_WITNESS_WEIGHT}; use crate::util::ser::TransactionU16LenLimited; @@ -1023,7 +1024,7 @@ mod tests { InteractiveTxMessageSend, MAX_INPUTS_OUTPUTS_COUNT, MAX_RECEIVED_TX_ADD_INPUT_COUNT, MAX_RECEIVED_TX_ADD_OUTPUT_COUNT, }; - use crate::ln::ChannelId; + use crate::ln::types::ChannelId; use crate::sign::EntropySource; use crate::util::atomic_counter::AtomicCounter; use crate::util::ser::TransactionU16LenLimited; diff --git a/lightning/src/ln/mod.rs b/lightning/src/ln/mod.rs index 26a384dffd4..7cbb2ce5ebe 100644 --- a/lightning/src/ln/mod.rs +++ b/lightning/src/ln/mod.rs @@ -22,7 +22,9 @@ pub mod peer_handler; pub mod chan_utils; pub mod features; pub mod script; -mod channel_id; +pub mod types; + +pub use types::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret}; #[cfg(fuzzing)] pub mod peer_channel_encryptor; @@ -34,9 +36,6 @@ pub mod channel; #[cfg(not(fuzzing))] pub(crate) mod channel; -// Re-export ChannelId -pub use channel_id::ChannelId; - pub(crate) mod onion_utils; mod outbound_payment; pub mod wire; @@ -86,75 +85,3 @@ mod offers_tests; pub(crate) mod interactivetxs; pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN; - -use bitcoin::hashes::{sha256::Hash as Sha256, Hash}; - -/// payment_hash type, use to cross-lock hop -/// -/// 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) - } -} - -/// payment_preimage type, use to route payment between hop -/// -/// 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 for PaymentHash { - fn from(value: PaymentPreimage) -> Self { - PaymentHash(Sha256::hash(&value.0).to_byte_array()) - } -} - -/// payment_secret type, use to authenticate sender to the receiver and tie MPP HTLCs together -/// -/// 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]); - -#[allow(unused_imports)] -use crate::prelude::*; - -use bitcoin::bech32; -use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, WriteBase32, u5}; - -impl FromBase32 for PaymentSecret { - type Err = bech32::Error; - - fn from_base32(field_data: &[u5]) -> Result { - if field_data.len() != 52 { - return Err(bech32::Error::InvalidLength) - } else { - let data_bytes = Vec::::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(&self, writer: &mut W) -> Result<(), ::Err> { - (&self.0[..]).write_base32(writer) - } -} - -impl Base32Len for PaymentSecret { - fn base32_len(&self) -> usize { - 52 - } -} diff --git a/lightning/src/ln/monitor_tests.rs b/lightning/src/ln/monitor_tests.rs index d5f28c23b4e..52bda818583 100644 --- a/lightning/src/ln/monitor_tests.rs +++ b/lightning/src/ln/monitor_tests.rs @@ -15,7 +15,8 @@ use crate::chain::transaction::OutPoint; use crate::chain::chaininterface::{LowerBoundedFeeEstimator, compute_feerate_sat_per_1000_weight}; use crate::events::bump_transaction::{BumpTransactionEvent, WalletSource}; use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination}; -use crate::ln::{channel, ChannelId}; +use crate::ln::channel; +use crate::ln::types::ChannelId; use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, PaymentId, RecipientOnionFields}; use crate::ln::msgs::ChannelMessageHandler; use crate::util::config::UserConfig; diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 136ed4d317b..87e8a814d33 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -32,7 +32,7 @@ use bitcoin::blockdata::script::ScriptBuf; use bitcoin::hash_types::Txid; use crate::blinded_path::payment::{BlindedPaymentTlvs, ForwardTlvs, ReceiveTlvs}; -use crate::ln::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret}; +use crate::ln::types::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret}; use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures}; use crate::ln::onion_utils; use crate::onion_message; @@ -1678,7 +1678,7 @@ pub struct FinalOnionHopData { mod fuzzy_internal_msgs { use bitcoin::secp256k1::PublicKey; use crate::blinded_path::payment::{PaymentConstraints, PaymentContext, PaymentRelay}; - use crate::ln::{PaymentPreimage, PaymentSecret}; + use crate::ln::types::{PaymentPreimage, PaymentSecret}; use crate::ln::features::BlindedHopFeatures; use super::{FinalOnionHopData, TrampolineOnionPacket}; @@ -3176,8 +3176,7 @@ impl_writeable_msg!(GossipTimestampFilter, { mod tests { use bitcoin::{Transaction, TxIn, ScriptBuf, Sequence, Witness, TxOut}; use hex::DisplayHex; - use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret}; - use crate::ln::ChannelId; + use crate::ln::types::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret}; use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures}; use crate::ln::msgs::{self, FinalOnionHopData, OnionErrorPacket, CommonOpenChannelFields, CommonAcceptChannelFields, TrampolineOnionPacket}; use crate::ln::msgs::SocketAddress; diff --git a/lightning/src/ln/onion_payment.rs b/lightning/src/ln/onion_payment.rs index db8c4cd0337..8293a32021e 100644 --- a/lightning/src/ln/onion_payment.rs +++ b/lightning/src/ln/onion_payment.rs @@ -11,7 +11,7 @@ use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1}; use crate::blinded_path; use crate::blinded_path::payment::{PaymentConstraints, PaymentRelay}; use crate::chain::channelmonitor::{HTLC_FAIL_BACK_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS}; -use crate::ln::PaymentHash; +use crate::ln::types::PaymentHash; use crate::ln::channelmanager::{BlindedFailure, BlindedForward, CLTV_FAR_FAR_AWAY, HTLCFailureMsg, MIN_CLTV_EXPIRY_DELTA, PendingHTLCInfo, PendingHTLCRouting}; use crate::ln::features::BlindedHopFeatures; use crate::ln::msgs; @@ -504,8 +504,7 @@ mod tests { use bitcoin::hashes::Hash; use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey}; - use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret}; - use crate::ln::ChannelId; + use crate::ln::types::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret}; use crate::ln::channelmanager::RecipientOnionFields; use crate::ln::features::{ChannelFeatures, NodeFeatures}; use crate::ln::msgs; diff --git a/lightning/src/ln/onion_route_tests.rs b/lightning/src/ln/onion_route_tests.rs index aeb175bc626..5ca4b4d5722 100644 --- a/lightning/src/ln/onion_route_tests.rs +++ b/lightning/src/ln/onion_route_tests.rs @@ -14,7 +14,7 @@ use crate::chain::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS}; use crate::sign::{EntropySource, NodeSigner, Recipient}; use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentFailureReason}; -use crate::ln::{PaymentHash, PaymentSecret}; +use crate::ln::types::{PaymentHash, PaymentSecret}; use crate::ln::channel::EXPIRE_PREV_CONFIG_TICKS; use crate::ln::channelmanager::{HTLCForwardInfo, FailureCode, CLTV_FAR_FAR_AWAY, DISABLE_GOSSIP_TICKS, MIN_CLTV_EXPIRY_DELTA, PendingAddHTLCInfo, PendingHTLCInfo, PendingHTLCRouting, PaymentId, RecipientOnionFields}; use crate::ln::onion_utils; diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs index f2b5c69e9e6..0abcb7de26d 100644 --- a/lightning/src/ln/onion_utils.rs +++ b/lightning/src/ln/onion_utils.rs @@ -11,8 +11,8 @@ use crate::crypto::chacha20::ChaCha20; use crate::crypto::streams::ChaChaReader; use crate::ln::channelmanager::{HTLCSource, RecipientOnionFields}; use crate::ln::msgs; +use crate::ln::types::{PaymentHash, PaymentPreimage}; use crate::ln::wire::Encode; -use crate::ln::{PaymentHash, PaymentPreimage}; use crate::routing::gossip::NetworkUpdate; use crate::routing::router::{BlindedTail, Path, RouteHop}; use crate::sign::NodeSigner; @@ -1240,7 +1240,7 @@ mod tests { use crate::io; use crate::ln::features::{ChannelFeatures, NodeFeatures}; use crate::ln::msgs; - use crate::ln::PaymentHash; + use crate::ln::types::PaymentHash; use crate::routing::router::{Path, Route, RouteHop}; use crate::util::ser::{VecWriter, Writeable, Writer}; diff --git a/lightning/src/ln/outbound_payment.rs b/lightning/src/ln/outbound_payment.rs index f37a7450729..2dd1e2a7409 100644 --- a/lightning/src/ln/outbound_payment.rs +++ b/lightning/src/ln/outbound_payment.rs @@ -15,7 +15,7 @@ use bitcoin::secp256k1::{self, Secp256k1, SecretKey}; use crate::sign::{EntropySource, NodeSigner, Recipient}; use crate::events::{self, PaymentFailureReason}; -use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; +use crate::ln::types::{PaymentHash, PaymentPreimage, PaymentSecret}; use crate::ln::channelmanager::{ChannelDetails, EventCompletionAction, HTLCSource, PaymentId}; use crate::ln::onion_utils::{DecodedOnionFailure, HTLCFailReason}; use crate::offers::invoice::Bolt12Invoice; @@ -1837,7 +1837,7 @@ mod tests { use core::time::Duration; use crate::events::{Event, PathFailure, PaymentFailureReason}; - use crate::ln::PaymentHash; + use crate::ln::types::PaymentHash; use crate::ln::channelmanager::{PaymentId, RecipientOnionFields}; use crate::ln::features::{ChannelFeatures, NodeFeatures}; use crate::ln::msgs::{ErrorAction, LightningError}; diff --git a/lightning/src/ln/payment_tests.rs b/lightning/src/ln/payment_tests.rs index d1fa58372dd..c9329c147bb 100644 --- a/lightning/src/ln/payment_tests.rs +++ b/lightning/src/ln/payment_tests.rs @@ -19,7 +19,8 @@ use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, Mes use crate::ln::channel::{EXPIRE_PREV_CONFIG_TICKS, commit_tx_fee_msat, get_holder_selected_channel_reserve_satoshis, ANCHOR_OUTPUT_VALUE_SATOSHI}; use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, MPP_TIMEOUT_TICKS, MIN_CLTV_EXPIRY_DELTA, PaymentId, PaymentSendFailure, RecentPaymentDetails, RecipientOnionFields, HTLCForwardInfo, PendingHTLCRouting, PendingAddHTLCInfo}; use crate::ln::features::{Bolt11InvoiceFeatures, ChannelTypeFeatures}; -use crate::ln::{msgs, ChannelId, PaymentHash, PaymentSecret, PaymentPreimage}; +use crate::ln::msgs; +use crate::ln::types::{ChannelId, PaymentHash, PaymentSecret, PaymentPreimage}; use crate::ln::msgs::ChannelMessageHandler; use crate::ln::onion_utils; use crate::ln::outbound_payment::{IDEMPOTENCY_TIMEOUT_TICKS, Retry}; diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 9ce23086145..17e46a274b1 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -20,7 +20,7 @@ use bitcoin::secp256k1::{self, Secp256k1, SecretKey, PublicKey}; use crate::sign::{NodeSigner, Recipient}; use crate::events::{EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider}; -use crate::ln::ChannelId; +use crate::ln::types::ChannelId; use crate::ln::features::{InitFeatures, NodeFeatures}; use crate::ln::msgs; use crate::ln::msgs::{ChannelMessageHandler, LightningError, SocketAddress, OnionMessageHandler, RoutingMessageHandler}; @@ -2676,7 +2676,7 @@ mod tests { use crate::sign::{NodeSigner, Recipient}; use crate::events; use crate::io; - use crate::ln::ChannelId; + use crate::ln::types::ChannelId; use crate::ln::features::{InitFeatures, NodeFeatures}; use crate::ln::peer_channel_encryptor::PeerChannelEncryptor; use crate::ln::peer_handler::{CustomMessageHandler, PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler, filter_addresses, ErroringMessageHandler, MAX_BUFFER_DRAIN_TICK_INTERVALS_PER_PEER}; diff --git a/lightning/src/ln/priv_short_conf_tests.rs b/lightning/src/ln/priv_short_conf_tests.rs index 6fd8623d317..fba97eae382 100644 --- a/lightning/src/ln/priv_short_conf_tests.rs +++ b/lightning/src/ln/priv_short_conf_tests.rs @@ -18,7 +18,8 @@ use crate::ln::channelmanager::{MIN_CLTV_EXPIRY_DELTA, PaymentId, RecipientOnion use crate::routing::gossip::RoutingFees; use crate::routing::router::{PaymentParameters, RouteHint, RouteHintHop}; use crate::ln::features::ChannelTypeFeatures; -use crate::ln::{msgs, ChannelId}; +use crate::ln::msgs; +use crate::ln::types::ChannelId; use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ChannelUpdate, ErrorAction}; use crate::ln::wire::Encode; use crate::util::config::{UserConfig, MaxDustHTLCExposure}; diff --git a/lightning/src/ln/reload_tests.rs b/lightning/src/ln/reload_tests.rs index 8b25f7701be..36b620c6bab 100644 --- a/lightning/src/ln/reload_tests.rs +++ b/lightning/src/ln/reload_tests.rs @@ -16,7 +16,8 @@ use crate::sign::EntropySource; use crate::chain::transaction::OutPoint; use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider}; use crate::ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, PaymentId, RecipientOnionFields}; -use crate::ln::{msgs, ChannelId}; +use crate::ln::msgs; +use crate::ln::types::ChannelId; use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ErrorAction}; use crate::util::test_channel_signer::TestChannelSigner; use crate::util::test_utils; diff --git a/lightning/src/ln/reorg_tests.rs b/lightning/src/ln/reorg_tests.rs index c1536562901..571521291b6 100644 --- a/lightning/src/ln/reorg_tests.rs +++ b/lightning/src/ln/reorg_tests.rs @@ -15,6 +15,7 @@ use crate::chain::transaction::OutPoint; use crate::chain::Confirm; use crate::events::{Event, MessageSendEventsProvider, ClosureReason, HTLCDestination, MessageSendEvent}; use crate::ln::msgs::{ChannelMessageHandler, Init}; +use crate::ln::types::ChannelId; use crate::sign::OutputSpender; use crate::util::test_utils; use crate::util::ser::Writeable; @@ -26,7 +27,7 @@ use bitcoin::secp256k1::Secp256k1; use crate::prelude::*; -use crate::ln::{functional_test_utils::*, ChannelId}; +use crate::ln::functional_test_utils::*; fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { // Our on-chain HTLC-claim learning has a few properties worth testing: diff --git a/lightning/src/ln/shutdown_tests.rs b/lightning/src/ln/shutdown_tests.rs index e4b69321eb7..12421ce9c3f 100644 --- a/lightning/src/ln/shutdown_tests.rs +++ b/lightning/src/ln/shutdown_tests.rs @@ -16,7 +16,8 @@ use crate::chain::transaction::OutPoint; use crate::events::{Event, MessageSendEvent, HTLCDestination, MessageSendEventsProvider, ClosureReason}; use crate::ln::channelmanager::{self, PaymentSendFailure, PaymentId, RecipientOnionFields, Retry, ChannelShutdownState, ChannelDetails}; use crate::routing::router::{PaymentParameters, get_route, RouteParameters}; -use crate::ln::{ChannelId, msgs}; +use crate::ln::msgs; +use crate::ln::types::ChannelId; use crate::ln::msgs::{ChannelMessageHandler, ErrorAction}; use crate::ln::onion_utils::INVALID_ONION_BLINDING; use crate::ln::script::ShutdownScript; diff --git a/lightning/src/ln/channel_id.rs b/lightning/src/ln/types.rs similarity index 74% rename from lightning/src/ln/channel_id.rs rename to lightning/src/ln/types.rs index 90efe3c94be..14774fb5e66 100644 --- a/lightning/src/ln/channel_id.rs +++ b/lightning/src/ln/types.rs @@ -7,7 +7,7 @@ // You may not use this file except in accordance with one or both of these // licenses. -//! ChannelId definition. +//! Various wrapper types (most around 32-byte arrays) for use in lightning. use crate::chain::transaction::OutPoint; use crate::io; @@ -16,6 +16,9 @@ use crate::sign::EntropySource; use crate::util::ser::{Readable, Writeable, Writer}; use super::channel_keys::RevocationBasepoint; +#[allow(unused_imports)] +use crate::prelude::*; + use bitcoin::hashes::{ Hash as _, HashEngine as _, @@ -120,6 +123,77 @@ 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 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 bitcoin::bech32; +use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, WriteBase32, u5}; + +impl FromBase32 for PaymentSecret { + type Err = bech32::Error; + + fn from_base32(field_data: &[u5]) -> Result { + if field_data.len() != 52 { + return Err(bech32::Error::InvalidLength) + } else { + let data_bytes = Vec::::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(&self, writer: &mut W) -> Result<(), ::Err> { + (&self.0[..]).write_base32(writer) + } +} + +impl Base32Len for PaymentSecret { + fn base32_len(&self) -> usize { + 52 + } +} + #[cfg(test)] mod tests { use bitcoin::hashes::{ @@ -131,7 +205,7 @@ mod tests { use bitcoin::secp256k1::PublicKey; use hex::DisplayHex; - use crate::ln::ChannelId; + use super::ChannelId; use crate::ln::channel_keys::RevocationBasepoint; use crate::util::ser::{Readable, Writeable}; use crate::util::test_utils; diff --git a/lightning/src/offers/invoice.rs b/lightning/src/offers/invoice.rs index 47e05edf910..6f907d785c7 100644 --- a/lightning/src/offers/invoice.rs +++ b/lightning/src/offers/invoice.rs @@ -28,7 +28,7 @@ //! use lightning::offers::refund::Refund; //! use lightning::util::ser::Writeable; //! -//! # use lightning::ln::PaymentHash; +//! # use lightning::ln::types::PaymentHash; //! # use lightning::offers::invoice::{BlindedPayInfo, ExplicitSigningPubkey, InvoiceBuilder}; //! # use lightning::blinded_path::BlindedPath; //! # @@ -113,7 +113,7 @@ use core::time::Duration; use core::hash::{Hash, Hasher}; use crate::io; use crate::blinded_path::BlindedPath; -use crate::ln::PaymentHash; +use crate::ln::types::PaymentHash; use crate::ln::channelmanager::PaymentId; use crate::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures, InvoiceRequestFeatures, OfferFeatures}; use crate::ln::inbound_payment::ExpandedKey; diff --git a/lightning/src/offers/invoice_request.rs b/lightning/src/offers/invoice_request.rs index 1fbb00d6f0d..faeef26c82f 100644 --- a/lightning/src/offers/invoice_request.rs +++ b/lightning/src/offers/invoice_request.rs @@ -65,7 +65,7 @@ use core::ops::Deref; use crate::sign::EntropySource; use crate::io; use crate::blinded_path::BlindedPath; -use crate::ln::PaymentHash; +use crate::ln::types::PaymentHash; use crate::ln::channelmanager::PaymentId; use crate::ln::features::InvoiceRequestFeatures; use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce}; diff --git a/lightning/src/offers/refund.rs b/lightning/src/offers/refund.rs index bcbab0fd04b..6a1d24106e9 100644 --- a/lightning/src/offers/refund.rs +++ b/lightning/src/offers/refund.rs @@ -92,7 +92,7 @@ use core::time::Duration; use crate::sign::EntropySource; use crate::io; use crate::blinded_path::BlindedPath; -use crate::ln::PaymentHash; +use crate::ln::types::PaymentHash; use crate::ln::channelmanager::PaymentId; use crate::ln::features::InvoiceRequestFeatures; use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce}; diff --git a/lightning/src/offers/test_utils.rs b/lightning/src/offers/test_utils.rs index 149ba15c3a2..ee8cf3a7c17 100644 --- a/lightning/src/offers/test_utils.rs +++ b/lightning/src/offers/test_utils.rs @@ -15,7 +15,7 @@ use bitcoin::secp256k1::schnorr::Signature; use core::time::Duration; use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode}; use crate::sign::EntropySource; -use crate::ln::PaymentHash; +use crate::ln::types::PaymentHash; use crate::ln::features::BlindedHopFeatures; use crate::offers::invoice::BlindedPayInfo; use crate::offers::merkle::TaggedHash; diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 1d7a730fa36..476655ebeb8 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -15,8 +15,8 @@ use bitcoin::hashes::hmac::{Hmac, HmacEngine}; use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey}; -use crate::blinded_path::{BlindedPath, IntroductionNode, NodeIdLookUp}; -use crate::blinded_path::message::{advance_path_by_one, ForwardTlvs, NextHop, ReceiveTlvs}; +use crate::blinded_path::{BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp}; +use crate::blinded_path::message::{advance_path_by_one, ForwardTlvs, ReceiveTlvs}; use crate::blinded_path::utils; use crate::events::{Event, EventHandler, EventsProvider}; use crate::sign::{EntropySource, NodeSigner, Recipient}; @@ -569,10 +569,10 @@ pub trait CustomOnionMessageHandler { /// A processed incoming onion message, containing either a Forward (another onion message) /// or a Receive payload with decrypted contents. -#[derive(Debug)] +#[derive(Clone, Debug)] pub enum PeeledOnion { /// Forwarded onion, with the next node id and a new onion - Forward(NextHop, OnionMessage), + Forward(NextMessageHop, OnionMessage), /// Received onion message, with decrypted contents, path_id, and reply path Receive(ParsedOnionMessageContents, Option<[u8; 32]>, Option) } @@ -1050,8 +1050,8 @@ where }, Ok(PeeledOnion::Forward(next_hop, onion_message)) => { let next_node_id = match next_hop { - NextHop::NodeId(pubkey) => pubkey, - NextHop::ShortChannelId(scid) => match self.node_id_lookup.next_node_id(scid) { + NextMessageHop::NodeId(pubkey) => pubkey, + NextMessageHop::ShortChannelId(scid) => match self.node_id_lookup.next_node_id(scid) { Some(pubkey) => pubkey, None => { log_trace!(self.logger, "Dropping forwarded onion messager: unable to resolve next hop using SCID {}", scid); @@ -1255,7 +1255,7 @@ fn packet_payloads_and_keys { /// The contents of an [`OnionMessage`] as read from the wire. /// /// [`OnionMessage`]: crate::ln::msgs::OnionMessage -#[derive(Debug)] +#[derive(Clone, Debug)] pub enum ParsedOnionMessageContents { /// A message related to BOLT 12 Offers. Offers(OffersMessage), @@ -293,8 +293,8 @@ impl Readable for ControlTlvs { let next_hop = match (short_channel_id, next_node_id) { (Some(_), Some(_)) => return Err(DecodeError::InvalidValue), - (Some(scid), None) => Some(NextHop::ShortChannelId(scid)), - (None, Some(pubkey)) => Some(NextHop::NodeId(pubkey)), + (Some(scid), None) => Some(NextMessageHop::ShortChannelId(scid)), + (None, Some(pubkey)) => Some(NextMessageHop::NodeId(pubkey)), (None, None) => None, }; diff --git a/lightning/src/routing/gossip.rs b/lightning/src/routing/gossip.rs index 42bf20a78a5..db35ff01bf0 100644 --- a/lightning/src/routing/gossip.rs +++ b/lightning/src/routing/gossip.rs @@ -21,7 +21,7 @@ use bitcoin::hashes::Hash; use bitcoin::network::constants::Network; use crate::events::{MessageSendEvent, MessageSendEventsProvider}; -use crate::ln::ChannelId; +use crate::ln::types::ChannelId; use crate::ln::features::{ChannelFeatures, NodeFeatures, InitFeatures}; use crate::ln::msgs::{DecodeError, ErrorAction, Init, LightningError, RoutingMessageHandler, SocketAddress, MAX_VALUE_MSAT}; use crate::ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, GossipTimestampFilter}; diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 59ec3f61862..8744761e72d 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -13,7 +13,7 @@ use bitcoin::secp256k1::{PublicKey, Secp256k1, self}; use crate::blinded_path::{BlindedHop, BlindedPath, Direction, IntroductionNode}; use crate::blinded_path::payment::{ForwardNode, ForwardTlvs, PaymentConstraints, PaymentRelay, ReceiveTlvs}; -use crate::ln::PaymentHash; +use crate::ln::types::PaymentHash; use crate::ln::channelmanager::{ChannelDetails, PaymentId, MIN_FINAL_CLTV_EXPIRY_DELTA}; use crate::ln::features::{BlindedHopFeatures, Bolt11InvoiceFeatures, Bolt12InvoiceFeatures, ChannelFeatures, NodeFeatures}; use crate::ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT}; @@ -3291,7 +3291,7 @@ mod tests { use crate::routing::test_utils::{add_channel, add_or_update_node, build_graph, build_line_graph, id_to_feature_flags, get_nodes, update_channel}; use crate::chain::transaction::OutPoint; use crate::sign::EntropySource; - use crate::ln::ChannelId; + use crate::ln::types::ChannelId; use crate::ln::features::{BlindedHopFeatures, ChannelFeatures, InitFeatures, NodeFeatures}; use crate::ln::msgs::{ErrorAction, LightningError, UnsignedChannelUpdate, MAX_VALUE_MSAT}; use crate::ln::channelmanager; @@ -8413,7 +8413,7 @@ pub(crate) mod bench_utils { use crate::chain::transaction::OutPoint; use crate::routing::scoring::ScoreUpdate; use crate::sign::KeysManager; - use crate::ln::ChannelId; + use crate::ln::types::ChannelId; use crate::ln::channelmanager::{self, ChannelCounterparty}; use crate::util::config::UserConfig; use crate::util::test_utils::TestLogger; diff --git a/lightning/src/sign/ecdsa.rs b/lightning/src/sign/ecdsa.rs index aa22eb366f3..a0409e54505 100644 --- a/lightning/src/sign/ecdsa.rs +++ b/lightning/src/sign/ecdsa.rs @@ -10,7 +10,7 @@ use crate::ln::chan_utils::{ ClosingTransaction, CommitmentTransaction, HTLCOutputInCommitment, HolderCommitmentTransaction, }; use crate::ln::msgs::UnsignedChannelAnnouncement; -use crate::ln::PaymentPreimage; +use crate::ln::types::PaymentPreimage; use crate::util::ser::Writeable; #[allow(unused_imports)] diff --git a/lightning/src/sign/mod.rs b/lightning/src/sign/mod.rs index 1710fe65c86..79edf0aed2e 100644 --- a/lightning/src/sign/mod.rs +++ b/lightning/src/sign/mod.rs @@ -39,6 +39,7 @@ use bitcoin::{secp256k1, Sequence, Txid, Witness}; use crate::chain::transaction::OutPoint; use crate::crypto::utils::{hkdf_extract_expand_twice, sign, sign_with_aux_rand}; +use crate::ln::chan_utils; use crate::ln::chan_utils::{ get_revokeable_redeemscript, make_funding_redeemscript, ChannelPublicKeys, ChannelTransactionParameters, ClosingTransaction, CommitmentTransaction, @@ -53,7 +54,7 @@ use crate::ln::channel_keys::{ use crate::ln::msgs::PartialSignatureWithNonce; use crate::ln::msgs::{UnsignedChannelAnnouncement, UnsignedGossipMessage}; use crate::ln::script::ShutdownScript; -use crate::ln::{chan_utils, PaymentPreimage}; +use crate::ln::types::PaymentPreimage; use crate::offers::invoice::UnsignedBolt12Invoice; use crate::offers::invoice_request::UnsignedInvoiceRequest; use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer}; @@ -923,6 +924,8 @@ pub trait OutputSpender { // Primarily needed in doctests because of https://github.com/rust-lang/rust/issues/67295 /// A dynamic [`SignerProvider`] temporarily needed for doc tests. +/// +/// This is not exported to bindings users as it is not intended for public consumption. #[cfg(taproot)] #[doc(hidden)] #[deprecated(note = "Remove once taproot cfg is removed")] @@ -930,6 +933,8 @@ pub type DynSignerProvider = dyn SignerProvider; /// A dynamic [`SignerProvider`] temporarily needed for doc tests. +/// +/// This is not exported to bindings users as it is not intended for public consumption. #[cfg(not(taproot))] #[doc(hidden)] #[deprecated(note = "Remove once taproot cfg is removed")] diff --git a/lightning/src/util/logger.rs b/lightning/src/util/logger.rs index e48cefaa044..904b7edd2f1 100644 --- a/lightning/src/util/logger.rs +++ b/lightning/src/util/logger.rs @@ -20,7 +20,7 @@ use core::cmp; use core::fmt; use core::ops::Deref; -use crate::ln::ChannelId; +use crate::ln::types::ChannelId; #[cfg(c_bindings)] use crate::prelude::*; // Needed for String @@ -244,7 +244,7 @@ impl + Clone> fmt::Display fo #[cfg(test)] mod tests { use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1}; - use crate::ln::ChannelId; + use crate::ln::types::ChannelId; use crate::util::logger::{Logger, Level, WithContext}; use crate::util::test_utils::TestLogger; use crate::sync::Arc; diff --git a/lightning/src/util/macro_logger.rs b/lightning/src/util/macro_logger.rs index f962251cd65..f6de098720f 100644 --- a/lightning/src/util/macro_logger.rs +++ b/lightning/src/util/macro_logger.rs @@ -7,7 +7,7 @@ // You may not use this file except in accordance with one or both of these // licenses. -use crate::ln::ChannelId; +use crate::ln::types::ChannelId; use crate::sign::SpendableOutputDescriptor; use bitcoin::blockdata::transaction::Transaction; diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index f88d9f36a72..5e7c6f85659 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -39,7 +39,7 @@ use crate::chain::ClaimId; use crate::ln::msgs::DecodeError; #[cfg(taproot)] use crate::ln::msgs::PartialSignatureWithNonce; -use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret}; +use crate::ln::types::{PaymentPreimage, PaymentHash, PaymentSecret}; use crate::util::byte_utils::{be48_to_array, slice_to_be48}; use crate::util::string::UntrustedString; diff --git a/lightning/src/util/sweep.rs b/lightning/src/util/sweep.rs index 398216c0cef..5135549689c 100644 --- a/lightning/src/util/sweep.rs +++ b/lightning/src/util/sweep.rs @@ -13,7 +13,7 @@ use crate::chain::channelmonitor::ANTI_REORG_DELAY; use crate::chain::{self, BestBlock, Confirm, Filter, Listen, WatchedOutput}; use crate::io; use crate::ln::msgs::DecodeError; -use crate::ln::ChannelId; +use crate::ln::types::ChannelId; use crate::prelude::Vec; use crate::sign::{ChangeDestinationSource, OutputSpender, SpendableOutputDescriptor}; use crate::sync::Mutex; diff --git a/lightning/src/util/test_channel_signer.rs b/lightning/src/util/test_channel_signer.rs index 43ac9ff87e9..64320bbbaf5 100644 --- a/lightning/src/util/test_channel_signer.rs +++ b/lightning/src/util/test_channel_signer.rs @@ -10,7 +10,8 @@ use crate::ln::channel::{ANCHOR_OUTPUT_VALUE_SATOSHI, MIN_CHAN_DUST_LIMIT_SATOSHIS}; use crate::ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction, ClosingTransaction}; use crate::ln::channel_keys::{HtlcKey}; -use crate::ln::{msgs, PaymentPreimage}; +use crate::ln::msgs; +use crate::ln::types::PaymentPreimage; use crate::sign::{InMemorySigner, ChannelSigner}; use crate::sign::ecdsa::{EcdsaChannelSigner, WriteableEcdsaChannelSigner}; diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 95bc2a7c661..6b4d2acd4d9 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -24,7 +24,7 @@ use crate::routing::router::{CandidateRouteHop, FirstHopCandidate, PublicHopCand use crate::sign; use crate::events; use crate::events::bump_transaction::{WalletSource, Utxo}; -use crate::ln::ChannelId; +use crate::ln::types::ChannelId; use crate::ln::channelmanager::{ChannelDetails, self}; #[cfg(test)] use crate::ln::chan_utils::CommitmentTransaction; diff --git a/rustfmt_excluded_files b/rustfmt_excluded_files index 51731c40d09..29f075c0a74 100644 --- a/rustfmt_excluded_files +++ b/rustfmt_excluded_files @@ -210,6 +210,7 @@ ./lightning/src/ln/reorg_tests.rs ./lightning/src/ln/script.rs ./lightning/src/ln/shutdown_tests.rs +./lightning/src/ln/types.rs ./lightning/src/ln/wire.rs ./lightning/src/offers/invoice.rs ./lightning/src/offers/invoice_error.rs