Skip to main content

FromBech32Str

Trait FromBech32Str 

Source
pub trait FromBech32Str {
    // Required methods
    fn try_from_bech32(
        &self,
        expected_hrp: &str,
    ) -> Result<Vec<u8>, Bech32Error>;
    fn try_from_bech32_unchecked(
        &self,
    ) -> Result<(String, Vec<u8>), Bech32Error>;
}
Expand description

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

Requires feature encoding-bech32.

Blanket-implemented for all AsRef<str> types. Treat all input as untrusted; HRP validation prevents injection attacks and cross-protocol confusion.

Extended payload capacity: Uses the custom Bech32Large variant (8191 Fe32 values, ~5 KB (5,115 bytes maximum payload)) — significantly larger than Bech32m’s standard 90-byte limit. Strings encoded via ToBech32 round-trip correctly here but will fail with FromBech32mStr when they exceed ~90 bytes.

Required Methods§

Source

fn try_from_bech32(&self, expected_hrp: &str) -> Result<Vec<u8>, Bech32Error>

Decodes a Bech32 (BIP-173) string, validating that the HRP matches expected_hrp.

The HRP comparison is case-insensitive. Returns only the data bytes — the HRP is validated and discarded.

Validates the BIP-173 checksum using the extended Bech32Large variant (8191 Fe32 limit) for large-payload compatibility.

§Errors
§Examples
use secure_gate::FromBech32Str;

// BIP-173 minimal valid test vector
let data = "A12UEL5L".try_from_bech32("A")?;
assert!(data.is_empty());

// HRP mismatch returns an error
assert!("A12UEL5L".try_from_bech32("bc").is_err());
Source

fn try_from_bech32_unchecked(&self) -> Result<(String, Vec<u8>), Bech32Error>

Decodes a Bech32 (BIP-173) string into (HRP, data_bytes) without validating the HRP.

Validates the BIP-173 checksum using the extended Bech32Large variant (8191 Fe32 limit) for large-payload compatibility.

§Errors
§Examples
use secure_gate::FromBech32Str;

// BIP-173 minimal valid test vector
let (hrp, data) = "A12UEL5L".try_from_bech32_unchecked()?;
assert_eq!(hrp.to_ascii_lowercase(), "a");
assert!(data.is_empty());

Implementors§

Source§

impl<T: AsRef<str> + ?Sized> FromBech32Str for T

Available on crate feature encoding-bech32 only.