alephium-web3 2.0.7

A Rust library to interact with the Alephium platform
Documentation
// Copyright 2018 - 2022 The Alephium Authors
// This file is part of the alephium project.

use crate::error::{Error, Result};

/// Encode bytes to base58 string
pub fn bytes_to_base58(bytes: &[u8]) -> String {
    bs58::encode(bytes).into_string()
}

/// Decode base58 string to bytes
pub fn base58_to_bytes(s: &str) -> Result<Vec<u8>> {
    bs58::decode(s)
        .into_vec()
        .map_err(|e| Error::InvalidBase58(format!("{}: {}", s, e)))
}

/// Check if a string is valid base58
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"); // Contains invalid characters
        assert!(result.is_err());
    }
}