I have a complex struct that I hash on-chain for certain computation. Is there any off-chain way via the TS SDK to hash that struct? I see keccak256 in the TS SDK, but this doesn’t serve my purpose (takes in a Uint8Array-like object).
“Complex” struct:
struct Key {
one: u256,
two: u64,
three: bool,
}
impl Hash for Key {
fn hash(self, ref mut state: Hasher) {
self.one.hash(state);
self.two.hash(state);
self.three.hash(state);
}
}
// hashed on-chain via:
keccak256(Key {
one: 69,
two: 420,
three: true
})
Minimal contract reproduction:
contract;
use std::hash::*;
abi TestHash {
fn hash_key() -> b256;
}
struct Key {
one: u256,
two: u64,
three: bool,
}
impl Hash for Key {
fn hash(self, ref mut state: Hasher) {
self.one.hash(state);
self.two.hash(state);
self.three.hash(state);
}
}
impl TestHash for Contract {
fn hash_key() -> b256 {
keccak256(Key {
one: 69,
two: 420,
three: true
})
}
}
#[test]
fn test_from() {
let contr = abi(TestHash, CONTRACT_ID);
log(contr.hash_key());
}
running with forc test --logs --decode
yield:
Decoded log value: Bits256([29, 85, 43, 160, 236, 142, 226, 39, 227, 220, 79, 104, 17, 212, 102, 24, 162, 131, 124, 238, 200, 233, 17, 235, 223, 27, 251, 89, 44, 57, 100, 246]), log rb: 8961848586872524460
Raw logs:
[{"LogData":{"data":"1d552ba0ec8ee227e3dc4f6811d46618a2837ceec8e911ebdf1bfb592c3964f6","digest":"6b06c13e935681097361fd4505c7256fc8cf4ad603a35546b875958da41fd1ff","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":32,"pc":12632,"ptr":67103622,"ra":0,"rb":8961848586872524460}}]