use bs58::decode::Error as Bs58DecodeError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Base58Error {
#[error("base58btc output buffer too small")]
BufferTooSmall,
#[error("base58btc decode failed")]
DecodeFailed,
#[error("invalid base58btc character")]
InvalidCharacter,
#[error("non-ascii base58btc character")]
NonAsciiCharacter,
}
impl From<Bs58DecodeError> for Base58Error {
fn from(value: Bs58DecodeError) -> Self {
match value {
Bs58DecodeError::BufferTooSmall => Self::BufferTooSmall,
Bs58DecodeError::InvalidCharacter { .. } => Self::InvalidCharacter,
Bs58DecodeError::NonAsciiCharacter { .. } => Self::NonAsciiCharacter,
_ => Self::DecodeFailed,
}
}
}
pub fn base58btc_encode(bytes: &[u8]) -> String {
bs58::encode(bytes).into_string()
}
pub fn base58btc_decode(s: &str) -> Result<Vec<u8>, Base58Error> {
bs58::decode(s).into_vec().map_err(Base58Error::from)
}