Skip to content

Commit

Permalink
Displays LtHash's Checksum as base58 (anza-xyz#3142)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored Oct 11, 2024
1 parent 8f0465f commit 8e83183
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
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.

0 comments on commit 8e83183

Please sign in to comment.