X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Foffers%2Fparse.rs;h=e9477086ee981358aea419416e676e4b682c1fc1;hb=0b196ebae6b9b1f861c2fc2ceddcc128b9f46f18;hp=35c1425acc13b86316858065afb1ed15a7a6402e;hpb=3bd395f2a8a7398d7bf5ad9b2c7257e64f9c126b;p=rust-lightning diff --git a/lightning/src/offers/parse.rs b/lightning/src/offers/parse.rs index 35c1425a..e9477086 100644 --- a/lightning/src/offers/parse.rs +++ b/lightning/src/offers/parse.rs @@ -10,69 +10,83 @@ //! Parsing and formatting for bech32 message encoding. use bitcoin::bech32; -use bitcoin::bech32::{FromBase32, ToBase32}; use bitcoin::secp256k1; use core::convert::TryFrom; -use core::fmt; use crate::io; use crate::ln::msgs::DecodeError; use crate::util::ser::SeekReadable; use crate::prelude::*; -/// Indicates a message can be encoded using bech32. -pub(super) trait Bech32Encode: AsRef<[u8]> + TryFrom, Error=ParseError> { - /// Human readable part of the message's bech32 encoding. - const BECH32_HRP: &'static str; - - /// Parses a bech32-encoded message into a TLV stream. - fn from_bech32_str(s: &str) -> Result { - // Offer encoding may be split by '+' followed by optional whitespace. - let encoded = match s.split('+').skip(1).next() { - Some(_) => { - for chunk in s.split('+') { - let chunk = chunk.trim_start(); - if chunk.is_empty() || chunk.contains(char::is_whitespace) { - return Err(ParseError::InvalidContinuation); +#[cfg(not(fuzzing))] +pub(super) use sealed::Bech32Encode; + +#[cfg(fuzzing)] +pub use sealed::Bech32Encode; + +mod sealed { + use bitcoin::bech32; + use bitcoin::bech32::{FromBase32, ToBase32}; + use core::convert::TryFrom; + use core::fmt; + use super::Bolt12ParseError; + + use crate::prelude::*; + + /// Indicates a message can be encoded using bech32. + pub trait Bech32Encode: AsRef<[u8]> + TryFrom, Error=Bolt12ParseError> { + /// Human readable part of the message's bech32 encoding. + const BECH32_HRP: &'static str; + + /// Parses a bech32-encoded message into a TLV stream. + fn from_bech32_str(s: &str) -> Result { + // Offer encoding may be split by '+' followed by optional whitespace. + let encoded = match s.split('+').skip(1).next() { + Some(_) => { + for chunk in s.split('+') { + let chunk = chunk.trim_start(); + if chunk.is_empty() || chunk.contains(char::is_whitespace) { + return Err(Bolt12ParseError::InvalidContinuation); + } } - } - let s = s.chars().filter(|c| *c != '+' && !c.is_whitespace()).collect::(); - Bech32String::Owned(s) - }, - None => Bech32String::Borrowed(s), - }; + let s: String = s.chars().filter(|c| *c != '+' && !c.is_whitespace()).collect(); + Bech32String::Owned(s) + }, + None => Bech32String::Borrowed(s), + }; - let (hrp, data) = bech32::decode_without_checksum(encoded.as_ref())?; + let (hrp, data) = bech32::decode_without_checksum(encoded.as_ref())?; - if hrp != Self::BECH32_HRP { - return Err(ParseError::InvalidBech32Hrp); - } + if hrp != Self::BECH32_HRP { + return Err(Bolt12ParseError::InvalidBech32Hrp); + } - let data = Vec::::from_base32(&data)?; - Self::try_from(data) - } + let data = Vec::::from_base32(&data)?; + Self::try_from(data) + } - /// Formats the message using bech32-encoding. - fn fmt_bech32_str(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - bech32::encode_without_checksum_to_fmt(f, Self::BECH32_HRP, self.as_ref().to_base32()) - .expect("HRP is invalid").unwrap(); + /// Formats the message using bech32-encoding. + fn fmt_bech32_str(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + bech32::encode_without_checksum_to_fmt(f, Self::BECH32_HRP, self.as_ref().to_base32()) + .expect("HRP is invalid").unwrap(); - Ok(()) + Ok(()) + } } -} -// Used to avoid copying a bech32 string not containing the continuation character (+). -enum Bech32String<'a> { - Borrowed(&'a str), - Owned(String), -} + // Used to avoid copying a bech32 string not containing the continuation character (+). + enum Bech32String<'a> { + Borrowed(&'a str), + Owned(String), + } -impl<'a> AsRef for Bech32String<'a> { - fn as_ref(&self) -> &str { - match self { - Bech32String::Borrowed(s) => s, - Bech32String::Owned(s) => s, + impl<'a> AsRef for Bech32String<'a> { + fn as_ref(&self) -> &str { + match self { + Bech32String::Borrowed(s) => s, + Bech32String::Owned(s) => s, + } } } } @@ -102,8 +116,8 @@ impl TryFrom> for ParsedMessage { } /// Error when parsing a bech32 encoded message using [`str::parse`]. -#[derive(Debug, PartialEq)] -pub enum ParseError { +#[derive(Clone, Debug, PartialEq)] +pub enum Bolt12ParseError { /// The bech32 encoding does not conform to the BOLT 12 requirements for continuing messages /// across multiple parts (i.e., '+' followed by whitespace). InvalidContinuation, @@ -115,14 +129,14 @@ pub enum ParseError { /// The bech32 decoded string could not be decoded as the expected message type. Decode(DecodeError), /// The parsed message has invalid semantics. - InvalidSemantics(SemanticError), + InvalidSemantics(Bolt12SemanticError), /// The parsed message has an invalid signature. InvalidSignature(secp256k1::Error), } /// Error when interpreting a TLV stream as a specific type. -#[derive(Debug, PartialEq)] -pub enum SemanticError { +#[derive(Clone, Debug, PartialEq)] +pub enum Bolt12SemanticError { /// The current [`std::time::SystemTime`] is past the offer or invoice's expiration. AlreadyExpired, /// The provided chain hash does not correspond to a supported chain. @@ -157,6 +171,8 @@ pub enum SemanticError { InvalidQuantity, /// A quantity or quantity bounds was provided but was not expected. UnexpectedQuantity, + /// Metadata could not be used to verify the offers message. + InvalidMetadata, /// Metadata was provided but was not expected. UnexpectedMetadata, /// Payer metadata was expected but was missing. @@ -175,25 +191,25 @@ pub enum SemanticError { MissingSignature, } -impl From for ParseError { +impl From for Bolt12ParseError { fn from(error: bech32::Error) -> Self { Self::Bech32(error) } } -impl From for ParseError { +impl From for Bolt12ParseError { fn from(error: DecodeError) -> Self { Self::Decode(error) } } -impl From for ParseError { - fn from(error: SemanticError) -> Self { +impl From for Bolt12ParseError { + fn from(error: Bolt12SemanticError) -> Self { Self::InvalidSemantics(error) } } -impl From for ParseError { +impl From for Bolt12ParseError { fn from(error: secp256k1::Error) -> Self { Self::InvalidSignature(error) }