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.

Solidity packing doesn't support arrays

See original GitHub issue

I have been trying to reproduce Solidity’s sha3 in Javascript, and have gotten everything to work with my own code except arrays. Once I found out about this library, I tried to use it for arrays, but I haven’t been able to get the same hash as Solidity produces yet. It would be great if you might be able to help me understand this better.

How would I generate the same sha3 hash using ethereumjs-abi as in this Solidity example?

contract Contract {
    function Test() constant returns(bytes32) {
        uint[] memory amounts = new uint[](2);
        amounts[0] = 1;
        amounts[1] = 2;

        bytes8[] memory tickers = new bytes8[](2);
        tickers[0] = "BTC";
        tickers[1] = "LTC";

        // 0x4282aea708b799612aa9d311309ef4b8eb1374cf0740c3e9d5914cb5ce9b93f2
        return sha3(amounts, tickers);
    }
}

My attempt gives an error of Cannot read property 1 of undefined:

abi.soliditySHA3(['uint[]', 'bytes8[]'], [[1,2], ['BTC', 'LTC']])

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
holgerd77commented, Aug 1, 2019

@alex-forshtat-tbk thanks, very helpful! I will try to use the “Update branch” function from GitHub and manually resolve conflicts in the next couple of days, since the original author of the PR is not responsive anymore (which is no wonder 😛).

1reaction
tommyz7commented, Dec 30, 2018

For the time being (as @djrtwo explained) you can do the following:

contract GenHash {
    function getHash(string title, uint256 num, address[] addr, uint256 num2) public pure returns (bytes32) {
        return keccak256(abi.encodePacked(title, num, addr, num2));
    }
}

and in JS…

function getHash(title, num, arr, num2) {
  let args = []
  arr.map((addr, index) => {
    args[index] = {t: 'bytes', v: web3.utils.leftPad(addr, 64)}
  })
  return web3.utils.soliditySha3(
    {t: 'string', v: title},
    {t: 'uint256', v: num},
    ...args,
    {t: 'uint256', v: num2}
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Do the tight variable packing rules apply to arrays in structs?
According to the docs, struct types and array types will always pack the elements tightly to save storage space. You can read about...
Read more >
Layout of State Variables in Storage - Solidity
Structs and array data always start a new slot and their items are packed tightly according to these rules.
Read more >
How an array is "tightly packed" in Solidity - Stack Overflow
I am trying to create a hash (sha256) of an array but I don't understand how it works. for example : pragma solidity...
Read more >
Gas Optimization in Solidity Part I: Variables - Medium
Variable packing only occurs in storage — memory and call data does not get packed. You will not save space trying to pack...
Read more >
Tight Variable Packing | solidity-patterns - GitHub Pages
Function parameters or dynamically-sized arrays do not benefit from it. On the contrary, as seen in the contract creation costs in the Gas...
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