Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(katana-primitives): remove base64 genesis prefix #1518

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading