SolidityKeccak256 without tightly packing
See original GitHub issueI have an ABIEncoderV2
struct that holds 3 arrays as such
struct Update {
address[] target;
uint256[] value;
bytes[] data;
}
We hash this struct but these types can’t be packed by the ABI, but I still need to get the hash of said struct in JS.
bytes32 updateHash = keccak256(abi.encode(_update));
since solidityKeccak256(types, values)
tightly packs the data I can’t use that , is there an alternative that doesn’t pack the data?
i.e. utils.solidityKeccak256(["address[], uint256[], bytes[]"], [update.target, update.value, update.data])
would not work due to reasons mentioned. I think it would even throw an error IIUC.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Hashing Algorithms - ethers
Returns the non-standard encoded values packed according to their respective type in types. ethers.utils.solidityKeccak256( types , values ) ...
Read more >How to mimic abi.encodePacked in ethers?
function, a non-standard tightly packed version of encoding is used. ... encodePacked simply concats together the variables with no padding ...
Read more >ethereum/solidity - Gitter
"Tightly packed" in Solidity usually refers to padding the values to be hashed with zero bytes to fit a single word (32-bytes).
Read more >Hashing Algorithms - Hethers - Hedera
Returns the non-standard encoded values packed according to their respective type in types. hethers.utils.solidityKeccak256( types ...
Read more >How to Hash Data Without Errors (part 3) - Coder's Errand
Final post of the series on Hash Functions Without Pitfalls, ... by ABI encoding and then tightly packing the bytes before hashing.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You need to use the normal encoder. You are correct, the
solidity*
functions are only for tightly packed.To hash normally, use
utils.defaultAbiCoder.encode([ "tuple(address[] target, uint[] value, bytes[] data)" ], [ objectHere ])
and you can then just use the normalutils.keccak256
on that. This should match the abi.encode in Solidity. 😃One quick note about your API, if you allow calls that use ecrecover to establish the from, you likely want to include a nonce as well to prevent replaying. You may already have other protection from this, but just in case; for delegated transactions that is often overlooked. 😃
Ah, that works.