1use {
2 five8::DecodeError,
3 solana_pubkey::{ParsePubkeyError, Pubkey},
4 solana_signature::{ParseSignatureError, Signature},
5};
6
7pub fn pubkey_decode<I: AsRef<[u8]>>(encoded: I) -> Result<Pubkey, ParsePubkeyError> {
8 let mut out = [0; 32];
9 match five8::decode_32(encoded, &mut out) {
10 Ok(()) => Ok(Pubkey::new_from_array(out)),
11 Err(DecodeError::InvalidChar(_)) => Err(ParsePubkeyError::Invalid),
12 Err(DecodeError::TooLong) => Err(ParsePubkeyError::WrongSize),
13 Err(DecodeError::TooShort) => Err(ParsePubkeyError::WrongSize),
14 Err(DecodeError::LargestTermTooHigh) => Err(ParsePubkeyError::WrongSize),
15 Err(DecodeError::OutputTooLong) => Err(ParsePubkeyError::WrongSize),
16 }
17}
18
19pub fn pubkey_encode(bytes: &[u8; 32]) -> String {
20 let mut out = [0; 44];
21 let len = five8::encode_32(bytes, &mut out) as usize;
22 out[0..len].iter().copied().map(char::from).collect()
23}
24
25pub fn signature_decode<I: AsRef<[u8]>>(encoded: I) -> Result<Signature, ParseSignatureError> {
26 let mut out = [0; 64];
27 match five8::decode_64(encoded, &mut out) {
28 Ok(()) => Ok(Signature::from(out)),
29 Err(DecodeError::InvalidChar(_)) => Err(ParseSignatureError::Invalid),
30 Err(DecodeError::TooLong) => Err(ParseSignatureError::WrongSize),
31 Err(DecodeError::TooShort) => Err(ParseSignatureError::WrongSize),
32 Err(DecodeError::LargestTermTooHigh) => Err(ParseSignatureError::WrongSize),
33 Err(DecodeError::OutputTooLong) => Err(ParseSignatureError::WrongSize),
34 }
35}
36
37pub fn signature_encode(bytes: &[u8; 64]) -> String {
38 let mut out = [0; 88];
39 let len = five8::encode_64(bytes, &mut out) as usize;
40 out[0..len].iter().copied().map(char::from).collect()
41}