whisky_pallas/converter/
address.rs1use bech32::{self};
2use whisky_common::WError;
3
4pub fn bytes_from_bech32(bech32_str: &str) -> Result<String, WError> {
5 let (_hrp, data) = bech32::decode(bech32_str)
6 .map_err(|e| WError::new("Bech32 decode error", &format!("{}", e)))?;
7 Ok(hex::encode(data))
8}
9
10pub fn bech32_from_bytes(bytes_hex: &str) -> Result<String, WError> {
11 let bytes = hex::decode(bytes_hex)
12 .map_err(|e| WError::new("Address bytes decode error", &format!("{}", e)))?;
13
14 let header_byte = bytes
15 .first()
16 .ok_or_else(|| WError::new("Bech32 encode error", "Empty bytes"))?;
17 let hrp = if header_byte & 0b1 == 0 {
19 "addr_test"
20 } else {
21 "addr"
22 };
23 let bech32_str = bech32::encode::<bech32::Bech32>(bech32::Hrp::parse(hrp).unwrap(), &bytes)
24 .map_err(|e| WError::new("Bech32 encode error", &format!("{}", e)))?;
25 Ok(bech32_str)
26}