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

Displays LtHash's Checksum as base58 #3142

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lattice-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = { workspace = true }
[dependencies]
base64 = { workspace = true }
blake3 = { workspace = true }
bs58 = { workspace = true }
bytemuck = { workspace = true, features = ["must_cast"] }

[dev-dependencies]
Expand Down
20 changes: 17 additions & 3 deletions lattice-hash/src/lt_hash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
base64::{display::Base64Display, prelude::BASE64_STANDARD},
std::fmt,
std::{fmt, str},
};

/// A 16-bit, 1024 element lattice-based incremental hash based on blake3
Expand Down Expand Up @@ -77,8 +77,14 @@ impl Checksum {

impl fmt::Display for Checksum {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let base64 = Base64Display::new(&self.0, &BASE64_STANDARD);
write!(f, "{base64}")
/// Maximum string length of a base58 encoded Checksum.
const MAX_BASE58_LEN: usize = 44;
let mut buf = [0u8; MAX_BASE58_LEN];
// SAFETY: The only error is if the buffer is too small
let len = bs58::encode(&self.0).onto(buf.as_mut_slice()).unwrap();
// SAFETY: The base58 alphabet is utf8
let str = str::from_utf8(&buf[..len]).unwrap();
write!(f, "{str}")
}
}

Expand Down Expand Up @@ -377,4 +383,12 @@ mod tests {
assert_eq!(actual_checksum, expected_checksum);
}
}

#[test]
fn test_checksum_display() {
let lt_hash = LtHash::identity();
let checksum = lt_hash.checksum();
let str = checksum.to_string();
assert_eq!(str.as_str(), "DoL6fvKuTpTQCyUh83NxQw2ewKzWYtq9gsTKp1eQiGC2");
}
}
1 change: 1 addition & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading