Skip to main content

Module bech32

Module bech32 

Source
Expand description

Bech32 decoding trait.

Import path: use secure_gate::FromBech32Str;

This trait provides secure, explicit decoding of Bech32 strings (BIP-173 checksum) to byte vectors, with HRP validation as the primary path. It is designed for handling untrusted input in cryptographic contexts, such as decoding encoded addresses or keys.

Requires the encoding-bech32 feature (distinct from Bech32m).

§Security Notes

  • Treat all input as untrusted: validate Bech32 strings upstream before wrapping in secrets. HRP validation prevents cross-protocol confusion attacks.
  • HRP validation: use try_from_bech32 as the default; use try_from_bech32_unchecked only when you intentionally need the decoded HRP. Test empty and invalid HRP inputs in security-critical code.
  • Heap allocation: Returns Vec<u8> — wrap in Fixed or Dynamic to store as a secret.

§Example

use secure_gate::FromBech32Str;
{
// BIP-173 minimal valid Bech32 test vector
let bech32 = "A12UEL5L";

let data = bech32.try_from_bech32("A").expect("HRP matches");
assert!(data.is_empty());

let (hrp, data) = bech32.try_from_bech32_unchecked().expect("valid bech32");
assert_eq!(hrp.to_ascii_lowercase(), "a");
assert!(data.is_empty());

// Error on invalid input
assert!("not-bech32".try_from_bech32("a").is_err());
}

Traits§

FromBech32Str
Extension trait for decoding Bech32 (BIP-173) strings into byte vectors.