Skip to content

Commit 3b3774e

Browse files
committed
Move UntrustedString and PrintableString to lightning-types
`lightning-invoice` currently has a dependency on the entire `lightning` crate just because it wants to use some of the useful types from it. This is obviously backwards and leads to some awkwardness like the BOLT 11 invoice signing API in the `lightning` crate taking a `[u5]` rather than a `Bolt11Invoice`. This takes one more step, moving the `UntrustedString` and `PrintableString` types to `lightning-types`.
1 parent 0c5922e commit 3b3774e

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

lightning-types/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ extern crate core;
2626
pub mod features;
2727
pub mod payment;
2828
pub mod routing;
29+
pub mod string;

lightning/src/util/string.rs renamed to lightning-types/src/string.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,13 @@
99

1010
//! Utilities for strings.
1111
12+
use alloc::string::String;
1213
use core::fmt;
13-
use crate::io::{self, Read};
14-
use crate::ln::msgs;
15-
use crate::util::ser::{Writeable, Writer, Readable};
16-
17-
#[allow(unused_imports)]
18-
use crate::prelude::*;
1914

2015
/// Struct to `Display` fields in a safe way using `PrintableString`
2116
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
2217
pub struct UntrustedString(pub String);
2318

24-
impl Writeable for UntrustedString {
25-
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
26-
self.0.write(w)
27-
}
28-
}
29-
30-
impl Readable for UntrustedString {
31-
fn read<R: Read>(r: &mut R) -> Result<Self, msgs::DecodeError> {
32-
let s: String = Readable::read(r)?;
33-
Ok(Self(s))
34-
}
35-
}
36-
3719
impl fmt::Display for UntrustedString {
3820
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3921
PrintableString(&self.0).fmt(f)

lightning/src/util/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub mod message_signing;
2121
pub mod invoice;
2222
pub mod persist;
2323
pub mod scid_utils;
24-
pub mod string;
2524
pub mod sweep;
2625
pub mod wakers;
2726
#[cfg(fuzzing)]
@@ -54,3 +53,7 @@ pub mod test_utils;
5453
#[cfg(any(test, feature = "_test_utils"))]
5554
pub mod test_channel_signer;
5655

56+
pub mod string {
57+
//! Utilities to wrap untrusted strings and handle them (more) safely
58+
pub use lightning_types::string::{PrintableString, UntrustedString};
59+
}

lightning/src/util/ser.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,18 @@ impl<'a> From<&'a String> for WithoutLength<&'a String> {
627627
fn from(s: &'a String) -> Self { Self(s) }
628628
}
629629

630+
impl Writeable for UntrustedString {
631+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
632+
self.0.write(w)
633+
}
634+
}
635+
636+
impl Readable for UntrustedString {
637+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
638+
let s: String = Readable::read(r)?;
639+
Ok(Self(s))
640+
}
641+
}
630642

631643
impl Writeable for WithoutLength<&UntrustedString> {
632644
#[inline]

0 commit comments

Comments
 (0)