Skip to content

Commit 4b412f7

Browse files
xJonathanLEIincrypto32
authored andcommitted
refactor: remove unsafe in starknet_keccak
1 parent 3a9c18d commit 4b412f7

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Diff for: chain/starknet/src/data_source.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,21 @@ fn starknet_keccak(data: &[u8]) -> [u8; 32] {
380380
// Remove the first 6 bits
381381
hash[0] &= 0b00000011;
382382

383-
// Because we know hash is always 32 bytes
384-
*unsafe { &*(hash[..].as_ptr() as *const [u8; 32]) }
383+
hash.into()
384+
}
385+
386+
#[cfg(test)]
387+
mod tests {
388+
use super::*;
389+
390+
#[test]
391+
fn test_starknet_keccak() {
392+
let expected_hash: [u8; 32] =
393+
hex::decode("016c11b0b5b808960df26f5bfc471d04c1995b0ffd2055925ad1be28d6baadfd")
394+
.unwrap()
395+
.try_into()
396+
.unwrap();
397+
398+
assert_eq!(starknet_keccak("Hello world".as_bytes()), expected_hash);
399+
}
385400
}

0 commit comments

Comments
 (0)