Struct SegwitHrpstring
pub struct SegwitHrpstring<'s> { /* private fields */ }
Expand description
An valid length HRP string that has been parsed, had the checksum validated, had the witness version validated, had the witness data length checked, and the had witness version and checksum removed.
§Examples
use bech32::primitives::decode::SegwitHrpstring;
// Parse a segwit V0 address.
let address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
let segwit = SegwitHrpstring::new(address).expect("valid segwit address");
// Do something with the encoded data.
let _ = segwit.byte_iter();
Implementations§
§impl<'s> SegwitHrpstring<'s>
impl<'s> SegwitHrpstring<'s>
pub fn new(s: &'s str) -> Result<SegwitHrpstring<'s>, SegwitHrpstringError>
pub fn new(s: &'s str) -> Result<SegwitHrpstring<'s>, SegwitHrpstringError>
Parses an HRP string, treating the first data character as a witness version.
The version byte does not appear in the extracted binary data, but is covered by the
checksum. It can be accessed with Self::witness_version
.
NOTE: We do not enforce any restrictions on the HRP, use SegwitHrpstring::has_valid_hrp
to get strict BIP conformance (also Hrp::is_valid_on_mainnet
and friends).
pub fn new_bech32(
s: &'s str,
) -> Result<SegwitHrpstring<'s>, SegwitHrpstringError>
pub fn new_bech32( s: &'s str, ) -> Result<SegwitHrpstring<'s>, SegwitHrpstringError>
Parses an HRP string, treating the first data character as a witness version.
§WARNING
You almost certainly do not want to use this function.
It is provided for backwards comparability to parse addresses that have an non-zero witness version because BIP-173 explicitly allows using the bech32 checksum with any witness version however BIP-350 specifies all witness version > 0 now MUST use bech32m.
pub fn has_valid_hrp(&self) -> bool
pub fn has_valid_hrp(&self) -> bool
Returns true
if the HRP is “bc” or “tb”.
BIP-173 requires that the HRP is “bc” or “tb” but software in the Bitcoin ecosystem uses
other HRPs, specifically “bcrt” for regtest addresses. We provide this function in order to
be BIP-173 compliant but their are no restrictions on the HRP of SegwitHrpstring
.
pub fn witness_version(&self) -> Fe32
pub fn witness_version(&self) -> Fe32
Returns the witness version.
pub fn data_part_ascii_no_witver_no_checksum(&self) -> &'s [u8] ⓘ
pub fn data_part_ascii_no_witver_no_checksum(&self) -> &'s [u8] ⓘ
Returns a partial slice of the data part, as ASCII bytes, everything after the witness version and before the checksum.
The byte values are guaranteed to be valid bech32 characters.
§Examples
use bech32::{Bech32, primitives::decode::SegwitHrpstring};
let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
let ascii = "ar0srrr7xfkvy5l643lydnw9re59gtzz";
let segwit = SegwitHrpstring::new(&addr).unwrap();
assert!(segwit.data_part_ascii_no_witver_no_checksum().iter().eq(ascii.as_bytes().iter()))
pub fn byte_iter(&self) -> ByteIter<'_> ⓘ
pub fn byte_iter(&self) -> ByteIter<'_> ⓘ
Returns an iterator that yields the data part, excluding the witness version, of the parsed bech32 encoded string.
Converts the ASCII bytes representing field elements to the respective field elements, then converts the stream of field elements to a stream of bytes.
Use self.witness_version()
to get the witness version.