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§
Sourcefn try_from_bech32m(&self, expected_hrp: &str) -> Result<Vec<u8>, Bech32Error>
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
Bech32Error::OperationFailed— invalid checksum or malformed string.Bech32Error::UnexpectedHrp— decoded HRP does not matchexpected_hrp.
§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());Sourcefn try_from_bech32m_unchecked(&self) -> Result<(String, Vec<u8>), Bech32Error>
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
Bech32Error::OperationFailed— invalid checksum or malformed string.Bech32Error::ConversionFailed— bit-conversion failure.
§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());