1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Parsing and formatting for bech32 message encoding.
13 use bitcoin::secp256k1;
14 use core::convert::TryFrom;
16 use crate::ln::msgs::DecodeError;
17 use crate::util::ser::SeekReadable;
19 use crate::prelude::*;
22 pub(super) use sealed::Bech32Encode;
25 pub use sealed::Bech32Encode;
29 use bitcoin::bech32::{FromBase32, ToBase32};
30 use core::convert::TryFrom;
32 use super::ParseError;
34 use crate::prelude::*;
36 /// Indicates a message can be encoded using bech32.
37 pub trait Bech32Encode: AsRef<[u8]> + TryFrom<Vec<u8>, Error=ParseError> {
38 /// Human readable part of the message's bech32 encoding.
39 const BECH32_HRP: &'static str;
41 /// Parses a bech32-encoded message into a TLV stream.
42 fn from_bech32_str(s: &str) -> Result<Self, ParseError> {
43 // Offer encoding may be split by '+' followed by optional whitespace.
44 let encoded = match s.split('+').skip(1).next() {
46 for chunk in s.split('+') {
47 let chunk = chunk.trim_start();
48 if chunk.is_empty() || chunk.contains(char::is_whitespace) {
49 return Err(ParseError::InvalidContinuation);
53 let s: String = s.chars().filter(|c| *c != '+' && !c.is_whitespace()).collect();
54 Bech32String::Owned(s)
56 None => Bech32String::Borrowed(s),
59 let (hrp, data) = bech32::decode_without_checksum(encoded.as_ref())?;
61 if hrp != Self::BECH32_HRP {
62 return Err(ParseError::InvalidBech32Hrp);
65 let data = Vec::<u8>::from_base32(&data)?;
69 /// Formats the message using bech32-encoding.
70 fn fmt_bech32_str(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
71 bech32::encode_without_checksum_to_fmt(f, Self::BECH32_HRP, self.as_ref().to_base32())
72 .expect("HRP is invalid").unwrap();
78 // Used to avoid copying a bech32 string not containing the continuation character (+).
79 enum Bech32String<'a> {
84 impl<'a> AsRef<str> for Bech32String<'a> {
85 fn as_ref(&self) -> &str {
87 Bech32String::Borrowed(s) => s,
88 Bech32String::Owned(s) => s,
94 /// A wrapper for reading a message as a TLV stream `T` from a byte sequence, while still
95 /// maintaining ownership of the bytes for later use.
96 pub(super) struct ParsedMessage<T: SeekReadable> {
101 impl<T: SeekReadable> TryFrom<Vec<u8>> for ParsedMessage<T> {
102 type Error = DecodeError;
104 fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
105 let mut cursor = io::Cursor::new(bytes);
106 let tlv_stream: T = SeekReadable::read(&mut cursor)?;
108 // Ensure that there are no more TLV records left to parse.
109 if cursor.position() < cursor.get_ref().len() as u64 {
110 return Err(DecodeError::InvalidValue);
113 let bytes = cursor.into_inner();
114 Ok(Self { bytes, tlv_stream })
118 /// Error when parsing a bech32 encoded message using [`str::parse`].
120 /// This is not exported to bindings users as its name conflicts with the BOLT 11 ParseError type.
121 #[derive(Debug, PartialEq)]
122 pub enum ParseError {
123 /// The bech32 encoding does not conform to the BOLT 12 requirements for continuing messages
124 /// across multiple parts (i.e., '+' followed by whitespace).
126 /// The bech32 encoding's human-readable part does not match what was expected for the message
129 /// The string could not be bech32 decoded.
130 Bech32(bech32::Error),
131 /// The bech32 decoded string could not be decoded as the expected message type.
133 /// The parsed message has invalid semantics.
134 InvalidSemantics(SemanticError),
135 /// The parsed message has an invalid signature.
136 InvalidSignature(secp256k1::Error),
139 /// Error when interpreting a TLV stream as a specific type.
141 /// This is not exported to bindings users as its name conflicts with the BOLT 11 SemanticError type.
142 #[derive(Debug, PartialEq)]
143 pub enum SemanticError {
144 /// The current [`std::time::SystemTime`] is past the offer or invoice's expiration.
146 /// The provided chain hash does not correspond to a supported chain.
148 /// A chain was provided but was not expected.
150 /// An amount was expected but was missing.
152 /// The amount exceeded the total bitcoin supply.
154 /// An amount was provided but was not sufficient in value.
156 /// An amount was provided but was not expected.
158 /// A currency was provided that is not supported.
160 /// A feature was required but is unknown.
161 UnknownRequiredFeatures,
162 /// Features were provided but were not expected.
164 /// A required description was not provided.
166 /// A signing pubkey was not provided.
167 MissingSigningPubkey,
168 /// A signing pubkey was provided but a different one was expected.
169 InvalidSigningPubkey,
170 /// A signing pubkey was provided but was not expected.
171 UnexpectedSigningPubkey,
172 /// A quantity was expected but was missing.
174 /// An unsupported quantity was provided.
176 /// A quantity or quantity bounds was provided but was not expected.
178 /// Metadata could not be used to verify the offers message.
180 /// Metadata was provided but was not expected.
182 /// Payer metadata was expected but was missing.
183 MissingPayerMetadata,
184 /// A payer id was expected but was missing.
186 /// Blinded paths were expected but were missing.
188 /// The blinded payinfo given does not match the number of blinded path hops.
190 /// An invoice creation time was expected but was missing.
192 /// An invoice payment hash was expected but was missing.
194 /// A signature was expected but was missing.
198 impl From<bech32::Error> for ParseError {
199 fn from(error: bech32::Error) -> Self {
204 impl From<DecodeError> for ParseError {
205 fn from(error: DecodeError) -> Self {
210 impl From<SemanticError> for ParseError {
211 fn from(error: SemanticError) -> Self {
212 Self::InvalidSemantics(error)
216 impl From<secp256k1::Error> for ParseError {
217 fn from(error: secp256k1::Error) -> Self {
218 Self::InvalidSignature(error)