chore: impl decode for UiAccountData (#33632)

This commit is contained in:
Sylwester Rąpała 2023-11-06 13:24:13 +01:00 committed by GitHub
parent e840b9759a
commit 63fd5cf049
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 18 deletions

View File

@ -56,6 +56,30 @@ pub enum UiAccountData {
Binary(String, UiAccountEncoding),
}
impl UiAccountData {
/// Returns decoded account data in binary format if possible
pub fn decode(&self) -> Option<Vec<u8>> {
match self {
UiAccountData::Json(_) => None,
UiAccountData::LegacyBinary(blob) => bs58::decode(blob).into_vec().ok(),
UiAccountData::Binary(blob, encoding) => match encoding {
UiAccountEncoding::Base58 => bs58::decode(blob).into_vec().ok(),
UiAccountEncoding::Base64 => BASE64_STANDARD.decode(blob).ok(),
UiAccountEncoding::Base64Zstd => {
BASE64_STANDARD.decode(blob).ok().and_then(|zstd_data| {
let mut data = vec![];
zstd::stream::read::Decoder::new(zstd_data.as_slice())
.and_then(|mut reader| reader.read_to_end(&mut data))
.map(|_| data)
.ok()
})
}
UiAccountEncoding::Binary | UiAccountEncoding::JsonParsed => None,
},
}
}
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub enum UiAccountEncoding {
@ -139,24 +163,7 @@ impl UiAccount {
}
pub fn decode<T: WritableAccount>(&self) -> Option<T> {
let data = match &self.data {
UiAccountData::Json(_) => None,
UiAccountData::LegacyBinary(blob) => bs58::decode(blob).into_vec().ok(),
UiAccountData::Binary(blob, encoding) => match encoding {
UiAccountEncoding::Base58 => bs58::decode(blob).into_vec().ok(),
UiAccountEncoding::Base64 => BASE64_STANDARD.decode(blob).ok(),
UiAccountEncoding::Base64Zstd => {
BASE64_STANDARD.decode(blob).ok().and_then(|zstd_data| {
let mut data = vec![];
zstd::stream::read::Decoder::new(zstd_data.as_slice())
.and_then(|mut reader| reader.read_to_end(&mut data))
.map(|_| data)
.ok()
})
}
UiAccountEncoding::Binary | UiAccountEncoding::JsonParsed => None,
},
}?;
let data = self.data.decode()?;
Some(T::create(
self.lamports,
data,