|
38 | 38 | //! use bech32::{hrp, segwit, Hrp, Bech32m}; |
39 | 39 | //! |
40 | 40 | //! const DATA: [u8; 20] = [0xab; 20]; // Arbitrary data to be encoded. |
41 | | -//! const ADDR: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu"; |
| 41 | +//! const STRING: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu"; |
42 | 42 | //! const TAP_ADDR: &str = "bc1p4w46h2at4w46h2at4w46h2at4w46h2at5kreae"; |
43 | 43 | //! |
44 | 44 | //! // Encode arbitrary data using "abc" as the human-readable part and append a bech32m checksum. |
45 | 45 | //! let hrp = Hrp::parse("abc").expect("valid hrp"); |
46 | | -//! let address = bech32::encode::<Bech32m>(hrp, &DATA).expect("failed to encode address"); |
47 | | -//! assert_eq!(address, ADDR); |
| 46 | +//! let string = bech32::encode::<Bech32m>(hrp, &DATA).expect("failed to encode string"); |
| 47 | +//! assert_eq!(string, STRING); |
48 | 48 | //! |
49 | 49 | //! // Encode arbitrary data as a Bitcoin taproot address. |
50 | 50 | //! let taproot_address = segwit::encode(hrp::BC, segwit::VERSION_1, &DATA).expect("valid witness version and program"); |
|
53 | 53 | //! // No-alloc: Encode without allocating (ignoring that String::new() allocates :). |
54 | 54 | //! let mut buf = String::new(); |
55 | 55 | //! bech32::encode_to_fmt::<Bech32m, String>(&mut buf, hrp, &DATA).expect("failed to encode to buffer"); |
56 | | -//! assert_eq!(buf, ADDR); |
| 56 | +//! assert_eq!(buf, STRING); |
57 | 57 | //! # } |
58 | 58 | //! ``` |
59 | 59 | //! |
|
65 | 65 | //! use bech32::{hrp, segwit, Hrp, Bech32m}; |
66 | 66 | //! |
67 | 67 | //! const DATA: [u8; 20] = [0xab; 20]; // Arbitrary data to be encoded. |
68 | | -//! const ADDR: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu"; |
| 68 | +//! const STRING: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu"; |
69 | 69 | //! const TAP_ADDR: &str = "bc1p4w46h2at4w46h2at4w46h2at4w46h2at5kreae"; |
70 | 70 | //! |
71 | 71 | //! // Decode a bech32 encoded string that includes a bech32/bech32m checksum. |
72 | 72 | //! // |
73 | 73 | //! // The input address MUST include a valid bech32 or bech32m checksum, for individual specific |
74 | 74 | //! // checksum algorithms see [`decode_bech32`], [`decode_bech32m`], [`decode_no_checksum`] or use |
75 | 75 | //! // the [`primitives::decode::CheckedHrpstring`] type directly. |
76 | | -//! let (hrp, data) = bech32::decode(&ADDR).expect("failed to decode"); |
| 76 | +//! let (hrp, data) = bech32::decode(&STRING).expect("failed to decode"); |
77 | 77 | //! assert_eq!(hrp, Hrp::parse("abc").unwrap()); |
78 | 78 | //! assert_eq!(data, DATA); |
79 | 79 | //! |
|
82 | 82 | //! assert_eq!(program, DATA); |
83 | 83 | //! |
84 | 84 | //! // No-alloc: Decode a bech32m checksummed address without allocating. |
85 | | -//! let p = CheckedHrpstring::new::<Bech32m>(&ADDR).expect("failed to parse address"); |
| 85 | +//! let p = CheckedHrpstring::new::<Bech32m>(&STRING).expect("failed to parse string"); |
86 | 86 | //! assert_eq!(hrp, p.hrp()); |
87 | 87 | //! assert!(p.byte_iter().eq(DATA.iter().map(|&b| b))); // We yield bytes not references. |
88 | 88 | //! |
@@ -197,14 +197,14 @@ pub use { |
197 | 197 | /// const BECH32M: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu"; |
198 | 198 | /// const NO_CHECKSUM: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at"; |
199 | 199 | /// |
200 | | -/// let (hrp, data) = decode(&BECH32).expect("valid address with valid bech32 checksum"); |
201 | | -/// let (hrp, data) = decode(&BECH32M).expect("valid address with valid bech32m checksum"); |
| 200 | +/// let (hrp, data) = decode(&BECH32).expect("valid bech32 string with valid bech32 checksum"); |
| 201 | +/// let (hrp, data) = decode(&BECH32M).expect("valid bech32 string with valid bech32m checksum"); |
202 | 202 | /// assert!(decode(&NO_CHECKSUM).is_err()); |
203 | 203 | /// |
204 | 204 | /// // You can control the checksum algorithm directly by using the [`CheckedHrpstring`] type. |
205 | | -/// let p = CheckedHrpstring::new::<Bech32>(&BECH32).expect("valid address with valid bech32 checksum"); |
206 | | -/// let p = CheckedHrpstring::new::<Bech32m>(&BECH32M).expect("valid address with valid bech32 checksum"); |
207 | | -/// let p = CheckedHrpstring::new::<NoChecksum>(&NO_CHECKSUM).expect("valid address with no checksum"); |
| 205 | +/// let p = CheckedHrpstring::new::<Bech32>(&BECH32).expect("valid bech32 string with valid bech32 checksum"); |
| 206 | +/// let p = CheckedHrpstring::new::<Bech32m>(&BECH32M).expect("valid bech32 string with valid bech32 checksum"); |
| 207 | +/// let p = CheckedHrpstring::new::<NoChecksum>(&NO_CHECKSUM).expect("valid bech32 string with no checksum"); |
208 | 208 | /// # } |
209 | 209 | /// ``` |
210 | 210 | #[cfg(feature = "alloc")] |
@@ -406,7 +406,7 @@ pub fn encoded_length<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> usize { |
406 | 406 | hrp.len() + 1 + iter.len() + Ck::CHECKSUM_LENGTH // +1 for separator |
407 | 407 | } |
408 | 408 |
|
409 | | -/// An error while decoding an address. |
| 409 | +/// An error while decoding a bech32 string. |
410 | 410 | #[cfg(feature = "alloc")] |
411 | 411 | #[derive(Debug, Clone, PartialEq, Eq)] |
412 | 412 | #[non_exhaustive] |
@@ -622,7 +622,7 @@ mod tests { |
622 | 622 | #[test] |
623 | 623 | fn encoded_length_works() { |
624 | 624 | let s = "test1lu08d6qejxtdg4y5r3zarvary0c5xw7kmz4lky"; |
625 | | - let (hrp, data) = decode(s).expect("failed to decode valid address"); |
| 625 | + let (hrp, data) = decode(s).expect("failed to decode valid bech32 string"); |
626 | 626 |
|
627 | 627 | let encoded = encode::<Bech32m>(hrp, &data).expect("failed to encode string"); |
628 | 628 | let want = encoded.len(); |
|
0 commit comments