bdk_chain::bitcoin::bech32::primitives::decode

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>

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>

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

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 hrp(&self) -> Hrp

Returns the human-readable part.

pub fn witness_version(&self) -> Fe32

Returns the witness version.

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<'_>

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§

§

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

§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'s> Freeze for SegwitHrpstring<'s>

§

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>,

Source§

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>,

Source§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V