Skip to content

Commit

Permalink
refactor(katana-primitives): remove base64 genesis prefix (#1518)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Feb 5, 2024
1 parent dfe390a commit 4fe87ba
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions crates/katana/primitives/src/genesis/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,30 +495,20 @@ pub fn resolve_artifacts_and_to_base64<P: AsRef<Path>>(
pub fn to_base64(genesis: GenesisJson) -> Result<Vec<u8>, GenesisJsonError> {
let data = serde_json::to_vec(&genesis)?;

let mut buf = vec![b'b', b'a', b's', b'e', b'6', b'4', b':'];
// make sure we'll have a slice big enough for base64 + padding
buf.resize((4 * data.len() / 3 + 4) + buf.len(), 0);
let mut buf = vec![0; (4 * data.len() / 3) + 4];

let bytes_written = BASE64_STANDARD.encode_slice(data, &mut buf[7..])?;
let bytes_written = BASE64_STANDARD.encode_slice(data, &mut buf)?;
// shorten the buffer to the actual length written
buf.truncate(bytes_written + 7);
buf.truncate(bytes_written);

Ok(buf)
}

/// Deserialize the [GenesisJson] from base64 encoded bytes.
pub fn from_base64(data: &[u8]) -> Result<GenesisJson, GenesisJsonError> {
match data {
[b'b', b'a', b's', b'e', b'6', b'4', b':', rest @ ..] => {
let decoded = BASE64_STANDARD.decode(rest)?;
Ok(serde_json::from_slice::<GenesisJson>(&decoded)?)
}

_ => {
let decoded = BASE64_STANDARD.decode(data)?;
Ok(serde_json::from_slice::<GenesisJson>(&decoded)?)
}
}
let decoded = BASE64_STANDARD.decode(data)?;
Ok(serde_json::from_slice::<GenesisJson>(&decoded)?)
}

fn class_artifact_at_path(
Expand Down

0 comments on commit 4fe87ba

Please sign in to comment.