Skip to main content

FromBech32mStr

Trait FromBech32mStr 

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

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

Requires feature encoding-bech32m.

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

Design note — standard BIP-350 compliance: decodes only standard-length Bech32m strings (Bitcoin Taproot/SegWit v1+ compatible). The Bech32Large variant used by ToBech32 is a distinct non-standard format for large payloads; decode those with FromBech32Str.

Required Methods§

Source

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

Decodes a Bech32m (BIP-350) 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-350 checksum.

§Errors
§Examples
use secure_gate::FromBech32mStr;

// BIP-350 minimal valid test vector
let data = "A1LQFN3A".try_from_bech32m("A")?;
assert!(data.is_empty());

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

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

Decodes a Bech32m (BIP-350) string into (HRP, data_bytes) without validating the HRP.

Validates the BIP-350 checksum.

§Errors
§Examples
use secure_gate::FromBech32mStr;

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

Implementors§

Source§

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

Available on crate feature encoding-bech32m only.