question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SolidityKeccak256 without tightly packing

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ricmoocommented, Aug 12, 2020

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 normal utils.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. 😃

1reaction
jtrembackcommented, Sep 4, 2020

Ah, that works.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found