Skip to main content

Module bech32m

Module bech32m 

Source
Expand description

Bech32m decoding trait.

Import path: use secure_gate::FromBech32mStr;

This trait provides secure, explicit decoding of Bech32m strings (BIP-350 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-bech32m feature (distinct from classic Bech32).

§Security Notes

  • Treat all input as untrusted: validate Bech32m strings upstream before wrapping in secrets. HRP validation prevents cross-protocol confusion attacks.
  • HRP validation: use try_from_bech32m as the default; use try_from_bech32m_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.
  • BIP-350 checksum: Enhanced error detection over BIP-173 Bech32.
  • Standard 90-byte payload limit (by design): decodes only spec-compliant Bech32m strings intended for Bitcoin address formats. Strings produced by the extended ToBech32 / Bech32Large variant are a distinct format — decode those with FromBech32Str.

§Example

use secure_gate::FromBech32mStr;

// BIP-350 minimal valid Bech32m test vector
let bech32m = "A1LQFN3A";

let data = bech32m.try_from_bech32m("A").expect("HRP matches");
assert!(data.is_empty());

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

// Error on invalid input
assert!("not-bech32m".try_from_bech32m("a").is_err());

Traits§

FromBech32mStr
Extension trait for decoding Bech32m (BIP-350) strings into byte vectors.