I am going to get the same value that is produced by keccak256 in solidity.

This is the code in my solidity file and I want to get the same value in the javascript file using ethers or web3.

bytes32 node = keccak256(abi.encodePacked(nodeString));

I got the same value of abi.encodePacked(nodeString)) by using ethers.utils.solidityPack.

const abiEncodedPackedString = ethers.utils.solidityPack(['string'], [nodeString]);

But when I tried ethers.utils.solidityKeccak256, the result wasn't the same as node in solidity.

const nodeInJavascript = ethers.utils.solidityKeccak256(['string], [abiEncodePackedString]);

I have also tried ethers.utils.keccak256(abiEncodePackedString) but I couldn't get the result either.

  • Does this ethereum.stackexchange.com/questions/30024/… answer your question?
    – Ismael
    Commented Mar 25, 2022 at 15:09
  • Thanks, @Ismael for your kind comment. I have tried all methods mentioned in the question you linked, but I didn't find a proper method.
    – Bikas Lin
    Commented Mar 26, 2022 at 1:37

1 Answer 1


There are two ways to achieve this from the documentation https://docs.ethers.io/v5/api/utils/hashing/.

  • ethers.utils.keccak256: It accepts a bytes like sequence so you have to convert the string to a byte sequence with ethers.utils.toUtf8Bytes



  • ethers.utils.id




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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