bluetape_rs_codec/
base58.rs1use std::error::Error;
4use std::fmt;
5
6use crate::base_n::{BaseNDecodeError, decode_base_n, encode_base_n};
7
8const BITCOIN_BASE58_ALPHABET: &[u8] =
9 b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13#[non_exhaustive]
14pub enum Base58DecodeError {
15 InvalidCharacter {
17 index: usize,
19 byte: u8,
21 },
22}
23
24impl fmt::Display for Base58DecodeError {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 match self {
27 Self::InvalidCharacter { index, byte } => {
28 write!(
29 f,
30 "base58 input contains invalid byte 0x{byte:02x} at position {index}"
31 )
32 }
33 }
34 }
35}
36
37impl Error for Base58DecodeError {}
38
39impl From<BaseNDecodeError> for Base58DecodeError {
40 fn from(error: BaseNDecodeError) -> Self {
41 match error {
42 BaseNDecodeError::InvalidCharacter { index, byte } => {
43 Self::InvalidCharacter { index, byte }
44 }
45 }
46 }
47}
48
49#[must_use]
61pub fn encode_base58(bytes: impl AsRef<[u8]>) -> String {
62 encode_base_n(bytes.as_ref(), BITCOIN_BASE58_ALPHABET)
63}
64
65pub fn decode_base58(encoded: impl AsRef<str>) -> Result<Vec<u8>, Base58DecodeError> {
81 decode_base_n(encoded.as_ref(), BITCOIN_BASE58_ALPHABET).map_err(Into::into)
82}