use crate::error::{Error, Result};
pub fn bytes_to_base58(bytes: &[u8]) -> String {
bs58::encode(bytes).into_string()
}
pub fn base58_to_bytes(s: &str) -> Result<Vec<u8>> {
bs58::decode(s)
.into_vec()
.map_err(|e| Error::InvalidBase58(format!("{}: {}", s, e)))
}
pub fn is_base58(s: &str) -> bool {
bs58::decode(s).into_vec().is_ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_base58_encoding() {
let data = b"hello world";
let encoded = bytes_to_base58(data);
let decoded = base58_to_bytes(&encoded).unwrap();
assert_eq!(decoded, data);
}
#[test]
fn test_is_base58() {
assert!(is_base58("StV1DL6CwTryKyV"));
assert!(!is_base58("This is not base58!"));
}
#[test]
fn test_invalid_base58() {
let result = base58_to_bytes("0OIl"); assert!(result.is_err());
}
}