Skip to content

Commit

Permalink
Merge pull request #3377 from G8XSU/hex-impls
Browse files Browse the repository at this point in the history
Implement to_lower_hex() Display/Debug for PaymentId & OfferId
  • Loading branch information
TheBlueMatt authored Oct 22, 2024
2 parents 66e4458 + 2e11841 commit 6104eb0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
23 changes: 15 additions & 8 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,14 @@ use alloc::collections::{btree_map, BTreeMap};
use crate::io;
use crate::prelude::*;
use core::{cmp, mem};
use core::borrow::Borrow;
use core::cell::RefCell;
use crate::io::Read;
use crate::sync::{Arc, Mutex, RwLock, RwLockReadGuard, FairRwLock, LockTestExt, LockHeldState};
use core::sync::atomic::{AtomicUsize, AtomicBool, Ordering};
use core::time::Duration;
use core::ops::Deref;

use bitcoin::hex::impl_fmt_traits;
// Re-export this for use in the public API.
pub use crate::ln::outbound_payment::{Bolt12PaymentError, PaymentSendFailure, ProbeSendFailure, Retry, RetryableSendFailure, RecipientOnionFields};
use crate::ln::script::ShutdownScript;
Expand Down Expand Up @@ -468,7 +469,7 @@ impl Verification for PaymentHash {
/// a payment and ensure idempotency in LDK.
///
/// This is not exported to bindings users as we just use [u8; 32] directly
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Hash, Copy, Clone, PartialEq, Eq)]
pub struct PaymentId(pub [u8; Self::LENGTH]);

impl PaymentId {
Expand Down Expand Up @@ -528,6 +529,18 @@ impl PaymentId {
}
}

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

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

impl Writeable for PaymentId {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
self.0.write(w)
Expand All @@ -541,12 +554,6 @@ impl Readable for PaymentId {
}
}

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

/// An identifier used to uniquely identify an intercepted HTLC to LDK.
///
/// This is not exported to bindings users as we just use [u8; 32] directly
Expand Down
16 changes: 15 additions & 1 deletion lightning/src/offers/offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
//! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
//! [`ChannelManager::create_offer_builder`]: crate::ln::channelmanager::ChannelManager::create_offer_builder

use core::borrow::Borrow;
use bitcoin::constants::ChainHash;
use bitcoin::network::Network;
use bitcoin::secp256k1::{Keypair, PublicKey, Secp256k1, self};
Expand Down Expand Up @@ -111,12 +112,13 @@ use crate::prelude::*;

#[cfg(feature = "std")]
use std::time::SystemTime;
use bitcoin::hex::impl_fmt_traits;

pub(super) const IV_BYTES_WITH_METADATA: &[u8; IV_LEN] = b"LDK Offer ~~~~~~";
pub(super) const IV_BYTES_WITHOUT_METADATA: &[u8; IV_LEN] = b"LDK Offer v2~~~~";

/// An identifier for an [`Offer`] built using [`DerivedMetadata`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct OfferId(pub [u8; 32]);

impl OfferId {
Expand All @@ -134,6 +136,18 @@ impl OfferId {
}
}

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

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

impl Writeable for OfferId {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
self.0.write(w)
Expand Down

0 comments on commit 6104eb0

Please sign in to comment.