Skip to content

Commit

Permalink
Use string not address in lib.rs
Browse files Browse the repository at this point in the history
The `crate::{encode, decode}` API is for encoding/decoding bech32
strings not specifically addresses (for example miniscript descriptors
are bech32 strings but not addresses).

Use the words "bech32 string" instead of "address" in `lib.rs`.
  • Loading branch information
tcharding committed Oct 24, 2023
1 parent 254202e commit d244386
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
//! use bech32::{hrp, segwit, Hrp, Bech32m};
//!
//! const DATA: [u8; 20] = [0xab; 20]; // Arbitrary data to be encoded.
//! const ADDR: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu";
//! const STRING: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu";
//! const TAP_ADDR: &str = "bc1p4w46h2at4w46h2at4w46h2at4w46h2at5kreae";
//!
//! // Encode arbitrary data using "abc" as the human-readable part and append a bech32m checksum.
//! let hrp = Hrp::parse("abc").expect("valid hrp");
//! let address = bech32::encode::<Bech32m>(hrp, &DATA).expect("failed to encode address");
//! assert_eq!(address, ADDR);
//! let string = bech32::encode::<Bech32m>(hrp, &DATA).expect("failed to encode string");
//! assert_eq!(string, STRING);
//!
//! // Encode arbitrary data as a Bitcoin taproot address.
//! let taproot_address = segwit::encode(hrp::BC, segwit::VERSION_1, &DATA).expect("valid witness version and program");
Expand All @@ -53,7 +53,7 @@
//! // No-alloc: Encode without allocating (ignoring that String::new() allocates :).
//! let mut buf = String::new();
//! bech32::encode_to_fmt::<Bech32m, String>(&mut buf, hrp, &DATA).expect("failed to encode to buffer");
//! assert_eq!(buf, ADDR);
//! assert_eq!(buf, STRING);
//! # }
//! ```
//!
Expand All @@ -65,15 +65,15 @@
//! use bech32::{hrp, segwit, Hrp, Bech32m};
//!
//! const DATA: [u8; 20] = [0xab; 20]; // Arbitrary data to be encoded.
//! const ADDR: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu";
//! const STRING: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu";
//! const TAP_ADDR: &str = "bc1p4w46h2at4w46h2at4w46h2at4w46h2at5kreae";
//!
//! // Decode a bech32 encoded string that includes a bech32/bech32m checksum.
//! //
//! // The input address MUST include a valid bech32 or bech32m checksum, for individual specific
//! // checksum algorithms see [`decode_bech32`], [`decode_bech32m`], [`decode_no_checksum`] or use
//! // the [`primitives::decode::CheckedHrpstring`] type directly.
//! let (hrp, data) = bech32::decode(&ADDR).expect("failed to decode");
//! let (hrp, data) = bech32::decode(&STRING).expect("failed to decode");
//! assert_eq!(hrp, Hrp::parse("abc").unwrap());
//! assert_eq!(data, DATA);
//!
Expand All @@ -82,7 +82,7 @@
//! assert_eq!(program, DATA);
//!
//! // No-alloc: Decode a bech32m checksummed address without allocating.
//! let p = CheckedHrpstring::new::<Bech32m>(&ADDR).expect("failed to parse address");
//! let p = CheckedHrpstring::new::<Bech32m>(&STRING).expect("failed to parse string");
//! assert_eq!(hrp, p.hrp());
//! assert!(p.byte_iter().eq(DATA.iter().map(|&b| b))); // We yield bytes not references.
//!
Expand Down Expand Up @@ -197,14 +197,14 @@ pub use {
/// const BECH32M: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu";
/// const NO_CHECKSUM: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at";
///
/// let (hrp, data) = decode(&BECH32).expect("valid address with valid bech32 checksum");
/// let (hrp, data) = decode(&BECH32M).expect("valid address with valid bech32m checksum");
/// let (hrp, data) = decode(&BECH32).expect("valid bech32 string with valid bech32 checksum");
/// let (hrp, data) = decode(&BECH32M).expect("valid bech32 string with valid bech32m checksum");
/// assert!(decode(&NO_CHECKSUM).is_err());
///
/// // You can control the checksum algorithm directly by using the [`CheckedHrpstring`] type.
/// let p = CheckedHrpstring::new::<Bech32>(&BECH32).expect("valid address with valid bech32 checksum");
/// let p = CheckedHrpstring::new::<Bech32m>(&BECH32M).expect("valid address with valid bech32 checksum");
/// let p = CheckedHrpstring::new::<NoChecksum>(&NO_CHECKSUM).expect("valid address with no checksum");
/// let p = CheckedHrpstring::new::<Bech32>(&BECH32).expect("valid bech32 string with valid bech32 checksum");
/// let p = CheckedHrpstring::new::<Bech32m>(&BECH32M).expect("valid bech32 string with valid bech32 checksum");
/// let p = CheckedHrpstring::new::<NoChecksum>(&NO_CHECKSUM).expect("valid bech32 string with no checksum");
/// # }
/// ```
#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -406,7 +406,7 @@ pub fn encoded_length<Ck: Checksum>(hrp: Hrp, data: &[u8]) -> usize {
hrp.len() + 1 + iter.len() + Ck::CHECKSUM_LENGTH // +1 for separator
}

/// An error while decoding an address.
/// An error while decoding a bech32 string.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
Expand Down Expand Up @@ -622,7 +622,7 @@ mod tests {
#[test]
fn encoded_length_works() {
let s = "test1lu08d6qejxtdg4y5r3zarvary0c5xw7kmz4lky";
let (hrp, data) = decode(s).expect("failed to decode valid address");
let (hrp, data) = decode(s).expect("failed to decode valid bech32 string");

let encoded = encode::<Bech32m>(hrp, &data).expect("failed to encode string");
let want = encoded.len();
Expand Down

0 comments on commit d244386

Please sign in to comment.