Storage – Storing SHA-256 64 Byte String in Solidity

contract-designcontract-developmentsoliditystorage

I am wondering what the best way to store a SHA-256 hash is in solidity. The SHA-256 output is a 64 byte hex string when I pass the hash from my web app to my contract. The issue I run into is that I start getting errors such as inaccessible dynamic type when I use strings – since strings are actually just dynamically sized arrays. Any ideas?

Best Answer

I would chop it up into two bytes32 client-side and re-assemble it client-side. The simplest form might be:

bytes32 part1;
bytes32 part2;

This is as economical as it gets, and can be passed around between contracts if needed.

Hope it helps.

p.s. If you don't need to recall them and only need to validate their existence (from some other off-chain source or claim), hashing them into a single bytes32 would provide enough precision and 50% gas reduction.

Related Topic