codec_multibase/
base58btc.rs1use bs58::decode::Error as Bs58DecodeError;
6use thiserror::Error;
7
8#[derive(Debug, Error)]
10pub enum Base58Error {
11 #[error("base58btc output buffer too small")]
13 BufferTooSmall,
14 #[error("base58btc decode failed")]
16 DecodeFailed,
17 #[error("invalid base58btc character")]
19 InvalidCharacter,
20 #[error("non-ascii base58btc character")]
22 NonAsciiCharacter,
23}
24
25impl From<Bs58DecodeError> for Base58Error {
26 fn from(value: Bs58DecodeError) -> Self {
27 match value {
28 Bs58DecodeError::BufferTooSmall => Self::BufferTooSmall,
29 Bs58DecodeError::InvalidCharacter { .. } => Self::InvalidCharacter,
30 Bs58DecodeError::NonAsciiCharacter { .. } => Self::NonAsciiCharacter,
31 _ => Self::DecodeFailed,
32 }
33 }
34}
35
36pub fn base58btc_encode(bytes: &[u8]) -> String {
38 bs58::encode(bytes).into_string()
39}
40
41pub fn base58btc_decode(s: &str) -> Result<Vec<u8>, Base58Error> {
45 bs58::decode(s).into_vec().map_err(Base58Error::from)
46}