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