From edeccf0fe6e9628f52e6243de1ef6dceba46eed0 Mon Sep 17 00:00:00 2001 From: Joy Wang <108701016+joyqvq@users.noreply.github.com> Date: Tue, 19 Dec 2023 10:40:06 -0500 Subject: [PATCH] add spec and hrp --- fastcrypto/src/encoding.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/fastcrypto/src/encoding.rs b/fastcrypto/src/encoding.rs index 1f7d35da59..32688da729 100644 --- a/fastcrypto/src/encoding.rs +++ b/fastcrypto/src/encoding.rs @@ -272,6 +272,12 @@ impl<'de, const N: usize> DeserializeAs<'de, [u8; N]> for Base58 { pub struct Bech32(String); impl Bech32 { + /// Decodes the Bech32 string to bytes, validating the given human readable part (hrp). See spec: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki + /// # Example: + /// ``` + /// let bytes = Bech32::decode("split1qqqqsk5gh5","split").unwrap(); + /// assert_eq!(bytes, vec![0, 0]); + /// ``` pub fn decode(s: &str, hrp: &str) -> Result, eyre::Report> { let (parsed, data, variant) = bech32::decode(s).map_err(|e| eyre::eyre!(e))?; if parsed != hrp || variant != Variant::Bech32 { @@ -281,6 +287,12 @@ impl Bech32 { } } + /// Encodes bytes into a Bech32 encoded string, with the given human readable part (hrp). See spec: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki + /// # Example: + /// ``` + /// let str = Bech32::encode(vec![0, 0],"split").unwrap(); + /// assert_eq!(str, "split1qqqqsk5gh5".to_string()); + /// ``` pub fn encode>(data: T, hrp: &str) -> Result { use bech32::ToBase32; bech32::encode(hrp, data.to_base32(), Variant::Bech32).map_err(|e| eyre::eyre!(e))