Skip to content

Commit e80e8c8

Browse files
committed
Have Invoice Description use UntrustedString
1 parent 3def307 commit e80e8c8

File tree

4 files changed

+15
-27
lines changed

4 files changed

+15
-27
lines changed

lightning-invoice/src/lib.rs

+6-19
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub use lightning::ln::PaymentSecret;
7373
pub use lightning::routing::router::{RouteHint, RouteHintHop};
7474
#[doc(no_inline)]
7575
pub use lightning::routing::gossip::RoutingFees;
76+
use lightning::util::string::UntrustedString;
7677

7778
mod de;
7879
mod ser;
@@ -480,7 +481,7 @@ impl Sha256 {
480481
/// # Invariants
481482
/// The description can be at most 639 __bytes__ long
482483
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Default)]
483-
pub struct Description(String);
484+
pub struct Description(UntrustedString);
484485

485486
/// Payee public key
486487
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
@@ -684,7 +685,7 @@ impl<H: tb::Bool, T: tb::Bool, C: tb::Bool, S: tb::Bool, M: tb::Bool> InvoiceBui
684685
pub fn invoice_description(self, description: Bolt11InvoiceDescription) -> InvoiceBuilder<tb::True, H, T, C, S, M> {
685686
match description {
686687
Bolt11InvoiceDescription::Direct(desc) => {
687-
self.description(desc.clone().into_inner())
688+
self.description(desc.clone().into_inner().0)
688689
}
689690
Bolt11InvoiceDescription::Hash(hash) => {
690691
self.description_hash(hash.0)
@@ -1517,30 +1518,16 @@ impl Description {
15171518
if description.len() > 639 {
15181519
Err(CreationError::DescriptionTooLong)
15191520
} else {
1520-
Ok(Description(description))
1521+
Ok(Description(UntrustedString(description)))
15211522
}
15221523
}
15231524

1524-
/// Returns the underlying description [`String`]
1525-
pub fn into_inner(self) -> String {
1525+
/// Returns the underlying description [`UntrustedString`]
1526+
pub fn into_inner(self) -> UntrustedString {
15261527
self.0
15271528
}
15281529
}
15291530

1530-
impl From<Description> for String {
1531-
fn from(val: Description) -> Self {
1532-
val.into_inner()
1533-
}
1534-
}
1535-
1536-
impl Deref for Description {
1537-
type Target = str;
1538-
1539-
fn deref(&self) -> &str {
1540-
&self.0
1541-
}
1542-
}
1543-
15441531
impl Display for Description {
15451532
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
15461533
write!(f, "{}", self.0)

lightning-invoice/src/ser.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,13 @@ impl Base32Len for Sha256 {
279279

280280
impl ToBase32 for Description {
281281
fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
282-
self.as_bytes().write_base32(writer)
282+
self.0.0.as_bytes().write_base32(writer)
283283
}
284284
}
285285

286286
impl Base32Len for Description {
287287
fn base32_len(&self) -> usize {
288-
self.0.as_bytes().base32_len()
288+
self.0.0.as_bytes().base32_len()
289289
}
290290
}
291291

lightning-invoice/src/utils.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ where
158158

159159
let invoice = match description {
160160
Bolt11InvoiceDescription::Direct(description) => {
161-
InvoiceBuilder::new(network).description(description.0.clone())
161+
InvoiceBuilder::new(network).description(description.0.0.clone())
162162
}
163163
Bolt11InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0),
164164
};
@@ -538,7 +538,7 @@ fn _create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_has
538538

539539
let invoice = match description {
540540
Bolt11InvoiceDescription::Direct(description) => {
541-
InvoiceBuilder::new(network).description(description.0.clone())
541+
InvoiceBuilder::new(network).description(description.0.0.clone())
542542
}
543543
Bolt11InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0),
544544
};
@@ -808,6 +808,7 @@ mod test {
808808
use lightning::util::config::UserConfig;
809809
use crate::utils::{create_invoice_from_channelmanager_and_duration_since_epoch, rotate_through_iterators};
810810
use std::collections::HashSet;
811+
use lightning::util::string::UntrustedString;
811812

812813
#[test]
813814
fn test_prefer_current_channel() {
@@ -852,7 +853,7 @@ mod test {
852853
assert_eq!(invoice.amount_pico_btc(), Some(100_000));
853854
// If no `min_final_cltv_expiry_delta` is specified, then it should be `MIN_FINAL_CLTV_EXPIRY_DELTA`.
854855
assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
855-
assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description("test".to_string())));
856+
assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description(UntrustedString("test".to_string()))));
856857
assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into()));
857858

858859
// Invoice SCIDs should always use inbound SCID aliases over the real channel ID, if one is
@@ -963,7 +964,7 @@ mod test {
963964
).unwrap();
964965
assert_eq!(invoice.amount_pico_btc(), Some(100_000));
965966
assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
966-
assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description("test".to_string())));
967+
assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description(UntrustedString("test".to_string()))));
967968
assert_eq!(invoice.payment_hash(), &sha256::Hash::from_slice(&payment_hash.0[..]).unwrap());
968969
}
969970

@@ -1315,7 +1316,7 @@ mod test {
13151316
};
13161317

13171318
assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
1318-
assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description("test".to_string())));
1319+
assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description(UntrustedString("test".to_string()))));
13191320
assert_eq!(invoice.route_hints().len(), 2);
13201321
assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into()));
13211322
assert!(!invoice.features().unwrap().supports_basic_mpp());

lightning/src/util/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::ln::msgs;
1616
use crate::util::ser::{Writeable, Writer, Readable};
1717

1818
/// Struct to `Display` fields in a safe way using `PrintableString`
19-
#[derive(Clone, Debug, PartialEq, Eq)]
19+
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
2020
pub struct UntrustedString(pub String);
2121

2222
impl Writeable for UntrustedString {

0 commit comments

Comments
 (0)