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§

source§

impl<'s> SegwitHrpstring<'s>

source

pub fn new(s: &'s str) -> Result<Self, 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).

source

pub fn new_bech32(s: &'s str) -> Result<Self, 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.

source

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.

source

pub fn hrp(&self) -> Hrp

Returns the human-readable part.

source

pub fn witness_version(&self) -> Fe32

Returns the witness version.

source

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()))
source

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.

Trait Implementations§

source§

impl<'s> Debug for SegwitHrpstring<'s>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'s> RefUnwindSafe for SegwitHrpstring<'s>

§

impl<'s> Send for SegwitHrpstring<'s>

§

impl<'s> Sync for SegwitHrpstring<'s>

§

impl<'s> Unpin for SegwitHrpstring<'s>

§

impl<'s> UnwindSafe for SegwitHrpstring<'s>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.