use super::util::{hex_str_to_bytes, ripemd160_raw, sha256_raw};
use super::Result;
use bs58;
pub fn new(input: &[u8]) -> String {
let raw = ripemd160_raw(&sha256_raw(input));
let mut tmp: Vec<u8> = Vec::with_capacity(1 + raw.len());
tmp.push(0_u8);
tmp.extend(raw);
bs58::encode(&tmp).with_check().into_string()
}
pub fn from_hex(hexstr: &str) -> Result<String> {
hex_str_to_bytes(hexstr).map(|x| new(&x))
}
pub fn is_wallet(wallet: &str) -> bool {
bs58::decode(wallet).with_check(Some(0)).into_vec().is_ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from() {
let result =
from_hex("03511c83916ac338835b07f6b9f7c0aa10b7b427b48e16b5e91360c919c9cf60cb").unwrap();
assert_eq!("1Lpgbz8o24ENRsZD3Rr5fVzJr2Ln4BBi5F", result);
}
#[test]
fn test_to() -> Result<()> {
assert!(is_wallet("1Lpgbz8o24ENRsZD3Rr5fVzJr2Ln4BBi5F"));
assert!(!is_wallet("1Lpgbz8o24ENRsZD3Rr5fVzJr2Ln4BBi5f"));
Ok(())
}
}