1

In Solidity, I am encoding a string and some empty bytes like so:

bytes memory _queryData = abi.encode("TwitterContestV1", abi.encode(bytes("")));
bytes32 _queryId = keccak256(_queryData);

I haven't found a solution that produces the same queryId in JavaScript. I've tried solutions using ethers.js & web3.js. For example, this:

const {ethers} = require("hardhat");
const {keccak256} = require("@ethersproject/keccak256");
...
const abiCoder = new ethers.utils.AbiCoder;
const queryData = abiCoder.encode(["string", "bytes"], ["TwitterContestV1", "0x"]);
const queryId = keccak256(queryData);
console.log(queryId)

Produces: 0xfa582019f544379a24648958ea92b04b85fb4dc8c8d379de150096012d02d8da But the Solidity queryId (hashed queryData) is: 0x7848658e5249a88a2cd61c63cb7114555cc79c9a491a7d2edf3b203236917a21

  • I've tried all solutions posted here: https://ethereum.stackexchange.com/questions/122398/how-to-create-an-empty-bytes-or-bytes32-in-ethersjs None worked – user12706407 Dec 23 '22 at 02:18

1 Answers1

1

oh, this is how:

const queryData = abiCoder.encode(["string", "bytes"], ["TwitterContestV1", abiCoder.encode(["bytes"], ["0x"])]);