9
9
//! a data part. A checksum at the end of the string provides error detection to prevent mistakes
10
10
//! when the string is written off or read out loud.
11
11
//!
12
+ //! Please note, so as to support lighting ([BOLT-11]) we explicitly do not do string length checks
13
+ //! in the top level API. We do however enforce the 90 character limit within the `segwit` modules.
14
+ //!
12
15
//! # Usage
13
16
//!
14
17
//! - If you are doing segwit stuff you likely want to use the [`segwit`] API.
113
116
//!
114
117
//! # }
115
118
//! ```
119
+ //!
120
+ //! [BOLT-11]: <https://github.com/lightning/bolts/blob/master/11-payment-encoding.md>
116
121
117
122
#![ cfg_attr( all( not( feature = "std" ) , not( test) ) , no_std) ]
118
123
// Experimental features we need.
@@ -169,6 +174,8 @@ pub use {
169
174
/// If your input string has no checksum use the [`CheckedHrpstring`] constructor, which allows
170
175
/// selecting the checksum algorithm explicitly.
171
176
///
177
+ /// Note: this function does not enforce any restrictions on the total length of the input string.
178
+ ///
172
179
/// # Returns
173
180
///
174
181
/// The human-readable part and the encoded data with the checksum removed.
@@ -214,6 +221,15 @@ pub fn decode(s: &str) -> Result<(Hrp, Vec<u8>), DecodeError> {
214
221
///
215
222
/// Encoded string will be prefixed with the `hrp` and have a checksum appended as specified by the
216
223
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
224
+ ///
225
+ /// ## Deviation from spec (BIP-173)
226
+ ///
227
+ /// In order to support [BOLT-11] this function does not restrict the total length of the returned
228
+ /// string. To encode [BIP-173] / [BIP-350] compliant segwit addresses use [`segwit::encode`].
229
+ ///
230
+ /// [BIP-173]: <https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki>
231
+ /// [BIP-350]: <https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki>
232
+ /// [BOLT-11]: <https://github.com/lightning/bolts/blob/master/11-payment-encoding.md>
217
233
#[ cfg( feature = "alloc" ) ]
218
234
#[ inline]
219
235
pub fn encode < Ck : Checksum > ( hrp : Hrp , data : & [ u8 ] ) -> Result < String , fmt:: Error > {
@@ -224,6 +240,15 @@ pub fn encode<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> Result<String, fmt::Error>
224
240
///
225
241
/// Encoded string will be prefixed with the `hrp` and have a checksum appended as specified by the
226
242
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
243
+ ///
244
+ /// ## Deviation from spec (BIP-173)
245
+ ///
246
+ /// In order to support [BOLT-11] this function does not restrict the total length of the returned
247
+ /// string. To encode [BIP-173] / [BIP-350] compliant segwit addresses use [`segwit::encode`].
248
+ ///
249
+ /// [BIP-173]: <https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki>
250
+ /// [BIP-350]: <https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki>
251
+ /// [BOLT-11]: <https://github.com/lightning/bolts/blob/master/11-payment-encoding.md>
227
252
#[ cfg( feature = "alloc" ) ]
228
253
#[ inline]
229
254
pub fn encode_lower < Ck : Checksum > ( hrp : Hrp , data : & [ u8 ] ) -> Result < String , fmt:: Error > {
@@ -236,6 +261,15 @@ pub fn encode_lower<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> Result<String, fmt::
236
261
///
237
262
/// Encoded string will be prefixed with the `hrp` and have a checksum appended as specified by the
238
263
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
264
+ ///
265
+ /// ## Deviation from spec (BIP-173)
266
+ ///
267
+ /// In order to support [BOLT-11] this function does not restrict the total length of the returned
268
+ /// string. To encode [BIP-173] / [BIP-350] compliant segwit addresses use [`segwit::encode`].
269
+ ///
270
+ /// [BIP-173]: <https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki>
271
+ /// [BIP-350]: <https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki>
272
+ /// [BOLT-11]: <https://github.com/lightning/bolts/blob/master/11-payment-encoding.md>
239
273
#[ cfg( feature = "alloc" ) ]
240
274
#[ inline]
241
275
pub fn encode_upper < Ck : Checksum > ( hrp : Hrp , data : & [ u8 ] ) -> Result < String , fmt:: Error > {
@@ -248,6 +282,15 @@ pub fn encode_upper<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> Result<String, fmt::
248
282
///
249
283
/// Encoded string will be prefixed with the `hrp` and have a checksum appended as specified by the
250
284
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
285
+ ///
286
+ /// ## Deviation from spec (BIP-173)
287
+ ///
288
+ /// In order to support [BOLT-11] this function does not restrict the total length of the returned
289
+ /// string. To encode [BIP-173] / [BIP-350] compliant segwit addresses use [`segwit::encode`].
290
+ ///
291
+ /// [BIP-173]: <https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki>
292
+ /// [BIP-350]: <https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki>
293
+ /// [BOLT-11]: <https://github.com/lightning/bolts/blob/master/11-payment-encoding.md>
251
294
#[ inline]
252
295
pub fn encode_to_fmt < Ck : Checksum , W : fmt:: Write > (
253
296
fmt : & mut W ,
@@ -261,6 +304,15 @@ pub fn encode_to_fmt<Ck: Checksum, W: fmt::Write>(
261
304
///
262
305
/// Encoded string will be prefixed with the `hrp` and have a checksum appended as specified by the
263
306
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
307
+ ///
308
+ /// ## Deviation from spec (BIP-173)
309
+ ///
310
+ /// In order to support [BOLT-11] this function does not restrict the total length of the returned
311
+ /// string. To encode [BIP-173] / [BIP-350] compliant segwit addresses use [`segwit::encode`].
312
+ ///
313
+ /// [BIP-173]: <https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki>
314
+ /// [BIP-350]: <https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki>
315
+ /// [BOLT-11]: <https://github.com/lightning/bolts/blob/master/11-payment-encoding.md>
264
316
#[ inline]
265
317
pub fn encode_lower_to_fmt < Ck : Checksum , W : fmt:: Write > (
266
318
fmt : & mut W ,
@@ -279,6 +331,15 @@ pub fn encode_lower_to_fmt<Ck: Checksum, W: fmt::Write>(
279
331
///
280
332
/// Encoded string will be prefixed with the `hrp` and have a checksum appended as specified by the
281
333
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
334
+ ///
335
+ /// ## Deviation from spec (BIP-173)
336
+ ///
337
+ /// In order to support [BOLT-11] this function does not restrict the total length of the returned
338
+ /// string. To encode [BIP-173] / [BIP-350] compliant segwit addresses use [`segwit::encode`].
339
+ ///
340
+ /// [BIP-173]: <https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki>
341
+ /// [BIP-350]: <https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki>
342
+ /// [BOLT-11]: <https://github.com/lightning/bolts/blob/master/11-payment-encoding.md>
282
343
#[ inline]
283
344
pub fn encode_upper_to_fmt < Ck : Checksum , W : fmt:: Write > (
284
345
fmt : & mut W ,
@@ -297,6 +358,15 @@ pub fn encode_upper_to_fmt<Ck: Checksum, W: fmt::Write>(
297
358
///
298
359
/// Encoded string will be prefixed with the `hrp` and have a checksum appended as specified by the
299
360
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
361
+ ///
362
+ /// ## Deviation from spec (BIP-173)
363
+ ///
364
+ /// In order to support [BOLT-11] this function does not restrict the total length of the returned
365
+ /// string. To encode [BIP-173] / [BIP-350] compliant segwit addresses use [`segwit::encode`].
366
+ ///
367
+ /// [BIP-173]: <https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki>
368
+ /// [BIP-350]: <https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki>
369
+ /// [BOLT-11]: <https://github.com/lightning/bolts/blob/master/11-payment-encoding.md>
300
370
#[ cfg( feature = "std" ) ]
301
371
#[ inline]
302
372
pub fn encode_to_writer < Ck : Checksum , W : std:: io:: Write > (
@@ -311,6 +381,15 @@ pub fn encode_to_writer<Ck: Checksum, W: std::io::Write>(
311
381
///
312
382
/// Encoded string will be prefixed with the `hrp` and have a checksum appended as specified by the
313
383
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
384
+ ///
385
+ /// ## Deviation from spec (BIP-173)
386
+ ///
387
+ /// In order to support [BOLT-11] this function does not restrict the total length of the returned
388
+ /// string. To encode [BIP-173] / [BIP-350] compliant segwit addresses use [`segwit::encode`].
389
+ ///
390
+ /// [BIP-173]: <https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki>
391
+ /// [BIP-350]: <https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki>
392
+ /// [BOLT-11]: <https://github.com/lightning/bolts/blob/master/11-payment-encoding.md>
314
393
#[ cfg( feature = "std" ) ]
315
394
#[ inline]
316
395
pub fn encode_lower_to_writer < Ck : Checksum , W : std:: io:: Write > (
@@ -330,6 +409,15 @@ pub fn encode_lower_to_writer<Ck: Checksum, W: std::io::Write>(
330
409
///
331
410
/// Encoded string will be prefixed with the `hrp` and have a checksum appended as specified by the
332
411
/// `Ck` algorithm (`NoChecksum` to exclude checksum all together).
412
+ ///
413
+ /// ## Deviation from spec (BIP-173)
414
+ ///
415
+ /// In order to support [BOLT-11] this function does not restrict the total length of the returned
416
+ /// string. To encode [BIP-173] / [BIP-350] compliant segwit addresses use [`segwit::encode`].
417
+ ///
418
+ /// [BIP-173]: <https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki>
419
+ /// [BIP-350]: <https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki>
420
+ /// [BOLT-11]: <https://github.com/lightning/bolts/blob/master/11-payment-encoding.md>
333
421
#[ cfg( feature = "std" ) ]
334
422
#[ inline]
335
423
pub fn encode_upper_to_writer < Ck : Checksum , W : std:: io:: Write > (
@@ -493,4 +581,29 @@ mod tests {
493
581
494
582
assert_eq ! ( got, want) ;
495
583
}
584
+
585
+ #[ test]
586
+ fn can_encode_really_long_string ( ) {
587
+ // Encode around the bech32 limit, mainly here as documentation.
588
+ let tcs = vec ! [
589
+ // Also shows there are no limit checks on the data slice (since segwit limits this to 40 bytes).
590
+ ( [ 0_u8 ; 50 ] , Hrp :: parse_unchecked( "abc" ) ) , // Encodes to 90 characters.
591
+ ( [ 0_u8 ; 50 ] , Hrp :: parse_unchecked( "abcd" ) ) , // Encodes to 91 characters.
592
+ ] ;
593
+ for ( data, hrp) in tcs {
594
+ assert ! ( encode:: <Bech32 >( & hrp, & data) . is_ok( ) ) ;
595
+ }
596
+
597
+ // Encode something arbitrarily long.
598
+ let data = [ 0_u8 ; 1024 ] ;
599
+ let hrp = Hrp :: parse_unchecked ( "abc" ) ;
600
+ assert ! ( encode:: <Bech32m >( & hrp, & data) . is_ok( ) ) ;
601
+ }
602
+
603
+ #[ test]
604
+ fn can_decode_long_string ( ) {
605
+ // A 91 character long string, greater than the segwit enforced maximum of 90.
606
+ let s = "abcd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqrw9z3s" ;
607
+ assert ! ( decode( s) . is_ok( ) ) ;
608
+ }
496
609
}
0 commit comments