1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use bitcoin::bech32;
use bitcoin::secp256k1;
use core::convert::TryFrom;
use crate::io;
use crate::ln::msgs::DecodeError;
use crate::util::ser::SeekReadable;
use crate::prelude::*;
#[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::ParseError;
use crate::prelude::*;
pub trait Bech32Encode: AsRef<[u8]> + TryFrom<Vec<u8>, Error=ParseError> {
const BECH32_HRP: &'static str;
fn from_bech32_str(s: &str) -> Result<Self, ParseError> {
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);
}
}
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())?;
if hrp != Self::BECH32_HRP {
return Err(ParseError::InvalidBech32Hrp);
}
let data = Vec::<u8>::from_base32(&data)?;
Self::try_from(data)
}
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(())
}
}
enum Bech32String<'a> {
Borrowed(&'a str),
Owned(String),
}
impl<'a> AsRef<str> for Bech32String<'a> {
fn as_ref(&self) -> &str {
match self {
Bech32String::Borrowed(s) => s,
Bech32String::Owned(s) => s,
}
}
}
}
pub(super) struct ParsedMessage<T: SeekReadable> {
pub bytes: Vec<u8>,
pub tlv_stream: T,
}
impl<T: SeekReadable> TryFrom<Vec<u8>> for ParsedMessage<T> {
type Error = DecodeError;
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
let mut cursor = io::Cursor::new(bytes);
let tlv_stream: T = SeekReadable::read(&mut cursor)?;
if cursor.position() < cursor.get_ref().len() as u64 {
return Err(DecodeError::InvalidValue);
}
let bytes = cursor.into_inner();
Ok(Self { bytes, tlv_stream })
}
}
#[derive(Debug, PartialEq)]
pub enum ParseError {
InvalidContinuation,
InvalidBech32Hrp,
Bech32(bech32::Error),
Decode(DecodeError),
InvalidSemantics(SemanticError),
InvalidSignature(secp256k1::Error),
}
#[derive(Debug, PartialEq)]
pub enum SemanticError {
AlreadyExpired,
UnsupportedChain,
UnexpectedChain,
MissingAmount,
InvalidAmount,
InsufficientAmount,
UnexpectedAmount,
UnsupportedCurrency,
UnknownRequiredFeatures,
UnexpectedFeatures,
MissingDescription,
MissingSigningPubkey,
InvalidSigningPubkey,
UnexpectedSigningPubkey,
MissingQuantity,
InvalidQuantity,
UnexpectedQuantity,
UnexpectedMetadata,
MissingPayerMetadata,
MissingPayerId,
MissingPaths,
InvalidPayInfo,
MissingCreationTime,
MissingPaymentHash,
MissingSignature,
}
impl From<bech32::Error> for ParseError {
fn from(error: bech32::Error) -> Self {
Self::Bech32(error)
}
}
impl From<DecodeError> for ParseError {
fn from(error: DecodeError) -> Self {
Self::Decode(error)
}
}
impl From<SemanticError> for ParseError {
fn from(error: SemanticError) -> Self {
Self::InvalidSemantics(error)
}
}
impl From<secp256k1::Error> for ParseError {
fn from(error: secp256k1::Error) -> Self {
Self::InvalidSignature(error)
}
}