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.

transform from ethereumjs-util keccak256 to ethers.js keccak256

See original GitHub issue
static combinedHash(first: Buffer, second: Buffer): Buffer {
    if (!first) {
      return second
    }
    if (!second) {
      return first
    }
    return keccak256(MerkleTree.sortAndConcat(first, second))
  }

This is what I am using with ethereumjs-util's keccak256. All seems well and it’s working, but the idea is that keccak256 returns Buffer , so that’s why I have Buffer as returned type for combinedHash function. All works.

Now I want to get rid of ethereumjs-utils and use ethers.js’s keccak256. So I do this, but ,the problem arises.

  • The first problem is that i can’t pass Buffer to keccak256
  • The second problem is it returns string instead of Buffer. If I try to transform the returned data into buffer by myself with Buffer.from, it produces the different buffer and my tests start to fail.

How can I fix those ? Any help would be appreciated.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
novaknolecommented, Mar 8, 2021

I fixed it…

I think I was missing arrayify in the middle.

This is what made it work:

return Buffer.from(arrayify(keccak256(MerkleTree.sortAndConcat(first, second))))

Thanks a lot for all the help , especially this quick.

Best of luck.

0reactions
ricmoocommented, Mar 7, 2021

Oh. Is that being returned from a function? I think it’s complaining about the return type from your function, not ethers. You need to either specify your function returns a string, or use Buffer.from(arrayify(v)), maybe?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use ethers keccak256? - Ethereum Stack Exchange
With ethers.js v5 you can use: const { ethers, utils } = require("ethers"); const labelhash = utils.keccak256(utils.toUtf8Bytes("example")).
Read more >
Documentation - Ethers.js
sendTransaction({ to: "ricmoo.firefly.eth", value: ethers.utils. ... and is computed by normalizing the Event signature and taking the keccak256 hash of it.
Read more >
Hashing Algorithms - ethers
The Ethereum Identity function computes the KECCAK256 hash of the text bytes. ... convert strings to bytes first: utils.keccak256(utils.
Read more >
ethereumjs-util keccak256 TypeScript Examples
This page shows TypeScript code examples of ethereumjs-util keccak256. ... 0, 20)); // Fail if bytecode is still not valid hex after transformations...
Read more >
How do I match ecrecover in ethers and in Solidity? · Issue #468
I have the following code in an Angular app: ethers.Wallet. ... payload); let payloadHash = ethers.utils.keccak256(payload); ...
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