Skip to content

Commit 61a1b14

Browse files
committed
Expose Display, Debug, and Eq for HRN bindings
Following the LDK 0.2-upgrade and the addition of a Display implementation for HumanReadableName in rust-lightning#3829, this commit exposes the Display, Debug, and Eq traits to UniFFI. This allows downstream users (Swift, Kotlin, Python) to compare names and print BIP 353 formatted strings natively. Added unit tests to verify trait implementation and BIP 353 prefix rendering.
1 parent 81d0e93 commit 61a1b14

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/ffi/types.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ impl std::fmt::Display for Offer {
382382
/// This struct can also be used for LN-Address recipients.
383383
///
384384
/// [Homograph Attacks]: https://en.wikipedia.org/wiki/IDN_homograph_attack
385-
#[derive(uniffi::Object)]
385+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, uniffi::Object)]
386+
#[uniffi::export(Debug, Display, Eq)]
386387
pub struct HumanReadableName {
387388
pub(crate) inner: LdkHumanReadableName,
388389
}
@@ -439,6 +440,12 @@ impl AsRef<LdkHumanReadableName> for HumanReadableName {
439440
}
440441
}
441442

443+
impl std::fmt::Display for HumanReadableName {
444+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
445+
write!(f, "{}", self.inner)
446+
}
447+
}
448+
442449
/// A `Refund` is a request to send an [`Bolt12Invoice`] without a preceding [`Offer`].
443450
///
444451
/// Typically, after an invoice is paid, the recipient may publish a refund allowing the sender to
@@ -1987,4 +1994,21 @@ mod tests {
19871994

19881995
assert_eq!(ldk_invoice.signable_hash().to_vec(), wrapped_invoice.signable_hash());
19891996
}
1997+
1998+
#[test]
1999+
fn test_hrn_traits() {
2000+
let encoded = "alice@lightning.space";
2001+
let hrn1 = HumanReadableName::from_encoded(encoded).unwrap();
2002+
let hrn2 = HumanReadableName::from_encoded(encoded).unwrap();
2003+
2004+
assert_eq!(hrn1.to_string(), "₿alice@lightning.space");
2005+
2006+
assert_eq!(hrn1, hrn2);
2007+
2008+
let debug_output = format!("{:?}", hrn1);
2009+
assert!(debug_output.contains("HumanReadableName"));
2010+
2011+
let hrn3 = hrn1;
2012+
assert_eq!(hrn1, hrn3);
2013+
}
19902014
}

0 commit comments

Comments
 (0)