Function bech32::decode

source ·
pub fn decode(s: &str) -> Result<(Hrp, Vec<u8>), DecodeError>
Expand description

Decodes a bech32 encoded string.

If this function succeeds the input string was found to be well formed (hrp, separator, bech32 characters), and to have either a valid bech32m checksum or a valid bech32 checksum.

If your input string has no checksum use the CheckedHrpstring constructor, which allows selecting the checksum algorithm explicitly.

Returns

The human-readable part and the encoded data with the checksum removed.

Examples

use bech32::{decode, Bech32, Bech32m, NoChecksum};
use bech32::primitives::decode::CheckedHrpstring;

const BECH32: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2atsghld7";
const BECH32M: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at958ngu";
const NO_CHECKSUM: &str = "abc14w46h2at4w46h2at4w46h2at4w46h2at";

let (hrp, data) = decode(&BECH32).expect("valid address with valid bech32 checksum");
let (hrp, data) = decode(&BECH32M).expect("valid address with valid bech32m checksum");
assert!(decode(&NO_CHECKSUM).is_err());

// You can control the checksum algorithm directly by using the [`CheckedHrpstring`] type.
let p = CheckedHrpstring::new::<Bech32>(&BECH32).expect("valid address with valid bech32 checksum");
let p = CheckedHrpstring::new::<Bech32m>(&BECH32M).expect("valid address with valid bech32 checksum");
let p = CheckedHrpstring::new::<NoChecksum>(&NO_CHECKSUM).expect("valid address with no checksum");