]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Move `UntrustedString` and `PrintableString` to `lightning-types`
authorMatt Corallo <git@bluematt.me>
Fri, 9 Aug 2024 01:26:21 +0000 (01:26 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 13 Aug 2024 12:54:59 +0000 (12:54 +0000)
`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`.

lightning-types/src/lib.rs
lightning-types/src/string.rs [new file with mode: 0644]
lightning/src/util/mod.rs
lightning/src/util/ser.rs
lightning/src/util/string.rs [deleted file]

index bcfb2b62d4393c648fad53004b09375ba993b551..1539401d38377141ca1b410972dd1ecbe84822dc 100644 (file)
@@ -26,3 +26,4 @@ extern crate core;
 pub mod features;
 pub mod payment;
 pub mod routing;
+pub mod string;
diff --git a/lightning-types/src/string.rs b/lightning-types/src/string.rs
new file mode 100644 (file)
index 0000000..ae5395a
--- /dev/null
@@ -0,0 +1,53 @@
+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
+// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
+//! Utilities for strings.
+
+use alloc::string::String;
+use core::fmt;
+
+/// Struct to `Display` fields in a safe way using `PrintableString`
+#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
+pub struct UntrustedString(pub String);
+
+impl fmt::Display for UntrustedString {
+       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+               PrintableString(&self.0).fmt(f)
+       }
+}
+
+/// A string that displays only printable characters, replacing control characters with
+/// [`core::char::REPLACEMENT_CHARACTER`].
+#[derive(Debug, PartialEq)]
+pub struct PrintableString<'a>(pub &'a str);
+
+impl<'a> fmt::Display for PrintableString<'a> {
+       fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+               use core::fmt::Write;
+               for c in self.0.chars() {
+                       let c = if c.is_control() { core::char::REPLACEMENT_CHARACTER } else { c };
+                       f.write_char(c)?;
+               }
+
+               Ok(())
+       }
+}
+
+#[cfg(test)]
+mod tests {
+       use super::PrintableString;
+
+       #[test]
+       fn displays_printable_string() {
+               assert_eq!(
+                       format!("{}", PrintableString("I \u{1F496} LDK!\t\u{26A1}")),
+                       "I \u{1F496} LDK!\u{FFFD}\u{26A1}",
+               );
+       }
+}
index a81a36c5583b713bc491f6556e57999ce681af5e..cfcea837971c647f76cd752f86f21ad29c72e5b4 100644 (file)
@@ -21,7 +21,6 @@ pub mod message_signing;
 pub mod invoice;
 pub mod persist;
 pub mod scid_utils;
-pub mod string;
 pub mod sweep;
 pub mod wakers;
 #[cfg(fuzzing)]
@@ -54,3 +53,7 @@ pub mod test_utils;
 #[cfg(any(test, feature = "_test_utils"))]
 pub mod test_channel_signer;
 
+pub mod string {
+       //! Utilities to wrap untrusted strings and handle them (more) safely
+       pub use lightning_types::string::{PrintableString, UntrustedString};
+}
index 3d3d97252f284aec09a2c320125d71f5e5b8c14d..29c52f6a08b1aaeeabf9c7c9107dc77e3693ace6 100644 (file)
@@ -627,6 +627,18 @@ impl<'a> From<&'a String> for WithoutLength<&'a String> {
        fn from(s: &'a String) -> Self { Self(s) }
 }
 
+impl Writeable for UntrustedString {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+               self.0.write(w)
+       }
+}
+
+impl Readable for UntrustedString {
+       fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
+               let s: String = Readable::read(r)?;
+               Ok(Self(s))
+       }
+}
 
 impl Writeable for WithoutLength<&UntrustedString> {
        #[inline]
diff --git a/lightning/src/util/string.rs b/lightning/src/util/string.rs
deleted file mode 100644 (file)
index ab12486..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-// This file is Copyright its original authors, visible in version control
-// history.
-//
-// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
-// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
-// You may not use this file except in accordance with one or both of these
-// licenses.
-
-//! Utilities for strings.
-
-use core::fmt;
-use crate::io::{self, Read};
-use crate::ln::msgs;
-use crate::util::ser::{Writeable, Writer, Readable};
-
-#[allow(unused_imports)]
-use crate::prelude::*;
-
-/// Struct to `Display` fields in a safe way using `PrintableString`
-#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
-pub struct UntrustedString(pub String);
-
-impl Writeable for UntrustedString {
-       fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
-               self.0.write(w)
-       }
-}
-
-impl Readable for UntrustedString {
-       fn read<R: Read>(r: &mut R) -> Result<Self, msgs::DecodeError> {
-               let s: String = Readable::read(r)?;
-               Ok(Self(s))
-       }
-}
-
-impl fmt::Display for UntrustedString {
-       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-               PrintableString(&self.0).fmt(f)
-       }
-}
-
-/// A string that displays only printable characters, replacing control characters with
-/// [`core::char::REPLACEMENT_CHARACTER`].
-#[derive(Debug, PartialEq)]
-pub struct PrintableString<'a>(pub &'a str);
-
-impl<'a> fmt::Display for PrintableString<'a> {
-       fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
-               use core::fmt::Write;
-               for c in self.0.chars() {
-                       let c = if c.is_control() { core::char::REPLACEMENT_CHARACTER } else { c };
-                       f.write_char(c)?;
-               }
-
-               Ok(())
-       }
-}
-
-#[cfg(test)]
-mod tests {
-       use super::PrintableString;
-
-       #[test]
-       fn displays_printable_string() {
-               assert_eq!(
-                       format!("{}", PrintableString("I \u{1F496} LDK!\t\u{26A1}")),
-                       "I \u{1F496} LDK!\u{FFFD}\u{26A1}",
-               );
-       }
-}