@@ -80,7 +80,8 @@ pub fn decode(s: &str) -> Result<(Hrp, Fe32, Vec<u8>), DecodeError> {
80
80
81
81
/// Encodes a segwit address.
82
82
///
83
- /// Does validity checks on the `witness_version` and length checks on the `witness_program`.
83
+ /// Does validity checks on the `witness_version`, length checks on the `witness_program`, and
84
+ /// checks the total encoded string length.
84
85
///
85
86
/// As specified by [`BIP-350`] we use the [`Bech32m`] checksum algorithm for witness versions 1 and
86
87
/// above, and for witness version 0 we use the original ([`BIP-173`]) [`Bech32`] checksum
@@ -101,20 +102,27 @@ pub fn encode(
101
102
) -> Result < String , EncodeError > {
102
103
segwit:: validate_witness_version ( witness_version) ?;
103
104
segwit:: validate_witness_program_length ( witness_program. len ( ) , witness_version) ?;
105
+ let _ = encoded_length ( hrp, witness_version, witness_program) ?;
104
106
105
107
let mut buf = String :: new ( ) ;
106
108
encode_to_fmt_unchecked ( & mut buf, hrp, witness_version, witness_program) ?;
107
109
Ok ( buf)
108
110
}
109
111
110
112
/// Encodes a segwit version 0 address.
113
+ ///
114
+ /// Does validity checks on the `witness_version`, length checks on the `witness_program`, and
115
+ /// checks the total encoded string length.
111
116
#[ cfg( feature = "alloc" ) ]
112
117
#[ inline]
113
118
pub fn encode_v0 ( hrp : & Hrp , witness_program : & [ u8 ] ) -> Result < String , EncodeError > {
114
119
encode ( hrp, VERSION_0 , witness_program)
115
120
}
116
121
117
122
/// Encodes a segwit version 1 address.
123
+ ///
124
+ /// Does validity checks on the `witness_version`, length checks on the `witness_program`, and
125
+ /// checks the total encoded string length.
118
126
#[ cfg( feature = "alloc" ) ]
119
127
#[ inline]
120
128
pub fn encode_v1 ( hrp : & Hrp , witness_program : & [ u8 ] ) -> Result < String , EncodeError > {
@@ -123,8 +131,9 @@ pub fn encode_v1(hrp: &Hrp, witness_program: &[u8]) -> Result<String, EncodeErro
123
131
124
132
/// Encodes a segwit address to a writer ([`fmt::Write`]) using lowercase characters.
125
133
///
126
- /// Does not check the validity of the witness version and witness program lengths (see
127
- /// the [`crate::primitives::segwit`] module for validation functions).
134
+ /// Does not check the validity of the witness version, the witness program length, or the total
135
+ /// encoded string length (see [`encoded_length`] and the [`crate::primitives::segwit`] module for
136
+ /// validation functions).
128
137
#[ inline]
129
138
pub fn encode_to_fmt_unchecked < W : fmt:: Write > (
130
139
fmt : & mut W ,
@@ -137,8 +146,9 @@ pub fn encode_to_fmt_unchecked<W: fmt::Write>(
137
146
138
147
/// Encodes a segwit address to a writer ([`fmt::Write`]) using lowercase characters.
139
148
///
140
- /// Does not check the validity of the witness version and witness program lengths (see
141
- /// the [`crate::primitives::segwit`] module for validation functions).
149
+ /// Does not check the validity of the witness version, the witness program length, or the total
150
+ /// encoded string length (see [`encoded_length`] and the [`crate::primitives::segwit`] module for
151
+ /// validation functions).
142
152
pub fn encode_lower_to_fmt_unchecked < W : fmt:: Write > (
143
153
fmt : & mut W ,
144
154
hrp : & Hrp ,
@@ -165,8 +175,9 @@ pub fn encode_lower_to_fmt_unchecked<W: fmt::Write>(
165
175
///
166
176
/// This is provided for use when creating QR codes.
167
177
///
168
- /// Does not check the validity of the witness version and witness program lengths (see
169
- /// the [`crate::primitives::segwit`] module for validation functions).
178
+ /// Does not check the validity of the witness version, the witness program length, or the total
179
+ /// encoded string length (see [`encoded_length`] and the [`crate::primitives::segwit`] module for
180
+ /// validation functions).
170
181
#[ inline]
171
182
pub fn encode_upper_to_fmt_unchecked < W : fmt:: Write > (
172
183
fmt : & mut W ,
@@ -193,8 +204,9 @@ pub fn encode_upper_to_fmt_unchecked<W: fmt::Write>(
193
204
194
205
/// Encodes a segwit address to a writer ([`std::io::Write`]) using lowercase characters.
195
206
///
196
- /// Does not check the validity of the witness version and witness program lengths (see
197
- /// the [`crate::primitives::segwit`] module for validation functions).
207
+ /// Does not check the validity of the witness version, the witness program length, or the total
208
+ /// encoded string length (see [`encoded_length`] and the [`crate::primitives::segwit`] module for
209
+ /// validation functions).
198
210
#[ cfg( feature = "std" ) ]
199
211
#[ inline]
200
212
pub fn encode_to_writer_unchecked < W : std:: io:: Write > (
@@ -208,8 +220,9 @@ pub fn encode_to_writer_unchecked<W: std::io::Write>(
208
220
209
221
/// Encodes a segwit address to a writer ([`std::io::Write`]) using lowercase characters.
210
222
///
211
- /// Does not check the validity of the witness version and witness program lengths (see
212
- /// the [`crate::primitives::segwit`] module for validation functions).
223
+ /// Does not check the validity of the witness version, the witness program length, or the total
224
+ /// encoded string length (see [`encoded_length`] and the [`crate::primitives::segwit`] module for
225
+ /// validation functions).
213
226
#[ cfg( feature = "std" ) ]
214
227
#[ inline]
215
228
pub fn encode_lower_to_writer_unchecked < W : std:: io:: Write > (
@@ -238,8 +251,9 @@ pub fn encode_lower_to_writer_unchecked<W: std::io::Write>(
238
251
///
239
252
/// This is provided for use when creating QR codes.
240
253
///
241
- /// Does not check the validity of the witness version and witness program lengths (see
242
- /// the [`crate::primitives::segwit`] module for validation functions).
254
+ /// Does not check the validity of the witness version, the witness program length, or the total
255
+ /// encoded string length (see [`encoded_length`] and the [`crate::primitives::segwit`] module for
256
+ /// validation functions).
243
257
#[ cfg( feature = "std" ) ]
244
258
#[ inline]
245
259
pub fn encode_upper_to_writer_unchecked < W : std:: io:: Write > (
@@ -364,6 +378,8 @@ pub enum EncodeError {
364
378
WitnessVersion ( InvalidWitnessVersionError ) ,
365
379
/// Invalid witness length.
366
380
WitnessLength ( WitnessLengthError ) ,
381
+ /// Encoding HRP, witver, and program into a bech32 string exceeds limit of 90 characters.
382
+ TooLong ( EncodedLengthError ) ,
367
383
/// Writing to formatter failed.
368
384
Write ( fmt:: Error ) ,
369
385
}
@@ -375,6 +391,7 @@ impl fmt::Display for EncodeError {
375
391
match * self {
376
392
WitnessVersion ( ref e) => write_err ! ( f, "witness version" ; e) ,
377
393
WitnessLength ( ref e) => write_err ! ( f, "witness length" ; e) ,
394
+ TooLong ( ref e) => write_err ! ( f, "encoded string too long" ; e) ,
378
395
Write ( ref e) => write_err ! ( f, "writing to formatter failed" ; e) ,
379
396
}
380
397
}
@@ -388,6 +405,7 @@ impl std::error::Error for EncodeError {
388
405
match * self {
389
406
WitnessVersion ( ref e) => Some ( e) ,
390
407
WitnessLength ( ref e) => Some ( e) ,
408
+ TooLong ( ref e) => Some ( e) ,
391
409
Write ( ref e) => Some ( e) ,
392
410
}
393
411
}
@@ -403,6 +421,11 @@ impl From<WitnessLengthError> for EncodeError {
403
421
fn from ( e : WitnessLengthError ) -> Self { Self :: WitnessLength ( e) }
404
422
}
405
423
424
+ impl From < EncodedLengthError > for EncodeError {
425
+ #[ inline]
426
+ fn from ( e : EncodedLengthError ) -> Self { Self :: TooLong ( e) }
427
+ }
428
+
406
429
impl From < fmt:: Error > for EncodeError {
407
430
#[ inline]
408
431
fn from ( e : fmt:: Error ) -> Self { Self :: Write ( e) }
0 commit comments