I'm coming from Ethereum, and in Solidity we can pack data then hash it like keccak256(abi.encodePacked(typed_data1, typed_data2, ....))
, and there is a corresponding approach in nodejs ethers.utils.keccak256(ethers.utils.solidityPack(types, values))
.
Using this, the hash of the off-chain data can be signed, then submitted to a smart contract for verification.
I'm trying to figure out how to accomplish this in Solana and am having trouble finding resources. I was looking at anchor_lang::solana_program::keccak::hashv
but this seems to be limited to arrays of u8 data, whereas I need to pack any sequence of data types.
Any tips and suggestions appreciated.
Edit:
Thanks to @arowana, I've manage to hash arbitary data using the following:
#[derive(BorshSerialize)]
struct OffChainData {
a: u8,
b: u64
}
let data = OffChainData { a: 1, b: 100213123 };
let serialized_data: Vec<u8> = data.try_to_vec()?;
let hash = anchor_lang::solana_program::keccak::hash(&serialized_data[..]);
msg!("keccak::hash {:?}", hash);
My concern now is that the hash doesn't appear to be keccak-256, at least as I am used to seeing it (I would expect the above hash to be 0xa2bd9883818ee366d2793fc267127e1b2b6df28041c8a0d65fdaab5928e4d26e
). However, the above code is giving the following as a result: Fg3EvJQ1r2FiKr7ReRUAFmn35LPGy1dVrmgVJCtf2h5B
My remaining question is how to get this result in nodejs, ideally via @project-serum/anchor