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::bech32::{FromBase32, ToBase32};
14 use core::convert::TryFrom;
16 use crate::ln::msgs::DecodeError;
18 use crate::prelude::*;
20 /// Indicates a message can be encoded using bech32.
21 pub(crate) trait Bech32Encode: AsRef<[u8]> + TryFrom<Vec<u8>, Error=ParseError> {
22 /// Human readable part of the message's bech32 encoding.
23 const BECH32_HRP: &'static str;
25 /// Parses a bech32-encoded message into a TLV stream.
26 fn from_bech32_str(s: &str) -> Result<Self, ParseError> {
27 // Offer encoding may be split by '+' followed by optional whitespace.
28 let encoded = match s.split('+').skip(1).next() {
30 for chunk in s.split('+') {
31 let chunk = chunk.trim_start();
32 if chunk.is_empty() || chunk.contains(char::is_whitespace) {
33 return Err(ParseError::InvalidContinuation);
37 let s = s.chars().filter(|c| *c != '+' && !c.is_whitespace()).collect::<String>();
38 Bech32String::Owned(s)
40 None => Bech32String::Borrowed(s),
43 let (hrp, data) = bech32::decode_without_checksum(encoded.as_ref())?;
45 if hrp != Self::BECH32_HRP {
46 return Err(ParseError::InvalidBech32Hrp);
49 let data = Vec::<u8>::from_base32(&data)?;
53 /// Formats the message using bech32-encoding.
54 fn fmt_bech32_str(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
55 bech32::encode_without_checksum_to_fmt(f, Self::BECH32_HRP, self.as_ref().to_base32())
56 .expect("HRP is invalid").unwrap();
62 // Used to avoid copying a bech32 string not containing the continuation character (+).
63 enum Bech32String<'a> {
68 impl<'a> AsRef<str> for Bech32String<'a> {
69 fn as_ref(&self) -> &str {
71 Bech32String::Borrowed(s) => s,
72 Bech32String::Owned(s) => s,
77 /// Error when parsing a bech32 encoded message using [`str::parse`].
78 #[derive(Debug, PartialEq)]
80 /// The bech32 encoding does not conform to the BOLT 12 requirements for continuing messages
81 /// across multiple parts (i.e., '+' followed by whitespace).
83 /// The bech32 encoding's human-readable part does not match what was expected for the message
86 /// The string could not be bech32 decoded.
87 Bech32(bech32::Error),
88 /// The bech32 decoded string could not be decoded as the expected message type.
90 /// The parsed message has invalid semantics.
91 InvalidSemantics(SemanticError),
94 /// Error when interpreting a TLV stream as a specific type.
95 #[derive(Debug, PartialEq)]
96 pub enum SemanticError {
97 /// An amount was expected but was missing.
99 /// The amount exceeded the total bitcoin supply.
101 /// A required description was not provided.
103 /// A signing pubkey was not provided.
104 MissingSigningPubkey,
105 /// An unsupported quantity was provided.
109 impl From<bech32::Error> for ParseError {
110 fn from(error: bech32::Error) -> Self {
115 impl From<DecodeError> for ParseError {
116 fn from(error: DecodeError) -> Self {
121 impl From<SemanticError> for ParseError {
122 fn from(error: SemanticError) -> Self {
123 Self::InvalidSemantics(error)