1

I'm trying to hash a message as shown here:

function constructPaymentMessage(contractAddress, amount) {
  return ethereumjs.ABI.soliditySHA3(
    ["address", "uint256"],
    [contractAddress, amount],
  );
}

how can I use web3.sha3 instead of ABI.soliditySHA3 to get the SHA3 hash correctly, is there any difference anyway? thanks.

| improve this question | | | | |
0

Here maybe this will help https://web3js.readthedocs.io/en/1.0/web3-utils.html#soliditysha3

OR

This is actually implemented in ethereumjs-abi:

var abi = require('ethereumjs-abi')
var BN = require('bn.js')

abi.soliditySHA3(
    [ "address", "address", "uint", "uint" ],
    [ new BN("43989fb883ba8111221e89123897538475893837", 16), 0, 10000, 1448075779 ]
).toString('hex')
| improve this answer | | | | |
0

In web3 version 1.2.6, we have the following:

This method only takes an input as a string and calculates the sha3 equivalent of the input.

In order to achieve what you are trying to accomplish with ethereumjs.ABI.soliditySHA3 you should use web3.utils.soliditySha3

Will calculate the sha3 of given input parameters in the same way solidity would. This means arguments will be ABI converted and tightly packed before being hashed.

function constructPaymentMessage(contractAddress, amount) {
  // the types are auto-detected
  return web3.utils.soliditySHA3(contractAddress, amount);
}
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.