Skip to content

Commit f07ce8a

Browse files
committed
base58 lib instead of handroll
1 parent 4ae8752 commit f07ce8a

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

Cargo.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/pinocchio/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ unexpected_cfgs = { level = "warn", check-cfg = [
1919

2020
[features]
2121
std = []
22+
23+
[dev-dependencies]
24+
bs58 = "0.5.1"

sdk/pinocchio/src/sysvars/slot_hashes/test.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,22 @@ fn test_layout_constants() {
2323
197, 41, 208, 190, 59, 19, 110, 45, 0, 85, 32, 0, 0, 0,
2424
]
2525
);
26-
const BASE_58: &[u8; 58] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
27-
// quick base58 comparison just for test
26+
2827
pub fn check_base58(input_bytes: &[u8], expected_b58: &str) {
29-
let mut b58_digits_rev = std::vec![0u8];
30-
for &byte_val in input_bytes {
31-
let mut carry = byte_val as u32;
32-
for digit_ref in b58_digits_rev.iter_mut() {
33-
let temp_val = ((*digit_ref as u32) << 8) | carry;
34-
*digit_ref = (temp_val % 58) as u8;
35-
carry = temp_val / 58;
36-
}
37-
while carry > 0 {
38-
b58_digits_rev.push((carry % 58) as u8);
39-
carry /= 58;
28+
match bs58::decode(expected_b58).into_vec() {
29+
Ok(decoded) => {
30+
assert_eq!(
31+
input_bytes,
32+
decoded.as_slice(),
33+
"Base58 decode mismatch: expected {:?}, got {:?}",
34+
input_bytes,
35+
decoded
36+
);
4037
}
41-
}
42-
for &byte_val in input_bytes {
43-
if byte_val == 0 {
44-
b58_digits_rev.push(0)
45-
} else {
46-
break;
38+
Err(e) => {
39+
panic!("Failed to decode base58 string '{}': {}", expected_b58, e);
4740
}
4841
}
49-
let mut output_chars = Vec::with_capacity(b58_digits_rev.len());
50-
for &digit_val in b58_digits_rev.iter().rev() {
51-
output_chars.push(BASE_58[digit_val as usize]);
52-
}
53-
assert_eq!(expected_b58.as_bytes(), output_chars.as_slice());
5442
}
5543
check_base58(
5644
&SLOTHASHES_ID,

0 commit comments

Comments
 (0)