pub struct UncheckedHrpstring<'s> { /* private fields */ }
Expand description

An HRP string that has been parsed but not yet had the checksum checked.

Parsing an HRP string only checks validity of the characters, it does not validate the checksum in any way.

Unless you are attempting to validate a string with multiple checksums then you likely do not want to use this type directly, instead use [CheckedHrpstring::new(s)].

Examples

use bech32::{Bech32, Bech32m, primitives::decode::UncheckedHrpstring};

let addr = "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4";
let unchecked = UncheckedHrpstring::new(addr).expect("valid bech32 character encoded string");
if unchecked.has_valid_checksum::<Bech32>() {
    // Remove the checksum and do something with the data.
    let checked = unchecked.remove_checksum::<Bech32>();
    let _ = checked.byte_iter();
} else if unchecked.has_valid_checksum::<Bech32m>() {
    // Remove the checksum and do something with the data as above.
} else {
    // Checksum is not valid for either the bech32 or bech32 checksum algorithms.
}

Implementations§

source§

impl<'s> UncheckedHrpstring<'s>

source

pub fn new(s: &'s str) -> Result<Self, UncheckedHrpstringError>

Parses an bech32 encode string and constructs a UncheckedHrpstring object.

Checks for valid ASCII values, does not validate the checksum.

source

pub fn hrp(&self) -> Hrp

Returns the human-readable part.

source

pub fn validate_and_remove_checksum<Ck: Checksum>( self ) -> Result<CheckedHrpstring<'s>, ChecksumError>

Validates that data has a valid checksum for the Ck algorithm and returns a CheckedHrpstring.

source

pub fn has_valid_checksum<Ck: Checksum>(&self) -> bool

Validates that data has a valid checksum for the Ck algorithm (this may mean an empty checksum if NoChecksum is used).

This is useful if you do not know which checksum algorithm was used and wish to validate against multiple algorithms consecutively. If this function returns true then call remove_checksum to get a CheckedHrpstring.

source

pub fn validate_checksum<Ck: Checksum>(&self) -> Result<(), ChecksumError>

Validates that data has a valid checksum for the Ck algorithm (this may mean an empty checksum if NoChecksum is used).

source

pub fn remove_checksum<Ck: Checksum>(self) -> CheckedHrpstring<'s>

Removes the checksum for the Ck algorithm and returns an CheckedHrpstring.

Data must be valid (ie, first call has_valid_checksum or validate_checksum()). This function is typically paired with has_valid_checksum when validating against multiple checksum algorithms consecutively.

Panics

May panic if data is not valid.

Trait Implementations§

source§

impl<'s> Debug for UncheckedHrpstring<'s>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.