bdk_chain::bitcoin::bech32::primitives

Module encode

Expand description

Bech32 address encoding.

This module provides types and iterators that can be used to encode data as a bech32 address in a variety of ways without any allocations, generating, verifying, and appending checksums, prepending HRP strings etc.

In general, directly using these adaptors is not very ergonomic, and users are recommended to instead use the crate level API.

WARNING: This module does not enforce the maximum length of an encoded bech32 string (90 chars).

§Examples

use bech32::{Bech32, ByteIterExt, Fe32IterExt, Fe32, Hrp};

let witness_prog = [
    0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4,
    0x54, 0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23,
    0xf1, 0x43, 0x3b, 0xd6,
];

// Get a stream of characters representing the bech32 encoded
// address using "bc" for the human-readable part.
let hrp = Hrp::parse("bc").expect("bc is valid hrp string");
let chars = witness_prog
    .iter()
    .copied()
    .bytes_to_fes()
    .with_checksum::<Bech32>(&hrp)
    .with_witness_version(Fe32::Q) // Optionally add witness version.
    .chars();

#[cfg(feature = "alloc")]
{
    let addr = chars.collect::<String>();
    assert_eq!(addr.to_uppercase(), "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4");
}

Structs§

  • Iterator adaptor which takes a stream of ASCII field elements (an encoded string) and yields a stream of bytes.
  • Iterator adaptor which takes a stream of field elements, converts it to characters prefixed by an HRP (and separator), and suffixed by the checksum i.e., converts the data in a stream of field elements into stream of characters representing the encoded bech32 string.
  • The Encoder builds iterators that can be used to encode field elements into a bech32 address.
  • Iterator adaptor for a checksummed iterator that inputs the HRP into the checksum algorithm before yielding the HRP as field elements followed by the data then checksum.
  • Iterator adaptor that just prepends a single character to a field element stream.