Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get the init code hash of a Solidity contract in Deno?

I’m building a web3 project in Deno, and I’m having a hard time trying to get the init code hash of a contract from the bytecode, how can I achieve that in Deno?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

To get the init code hash of a contract, you have to do keccak256(bytecode), in Deno you can achieve that with the following code:

import { keccak256 } from "npm:@ethersproject/keccak256";
import { decode } from "https://deno.land/std@0.180.0/encoding/hex.ts";

const bytecode = '...'; // The bytecode as a hex string (without 0x)
const initCodeHash = '0x' + keccak256(decode(new TextEncoder().encode(bytecode)));

This part of the code:

decode(new TextEncoder().encode(bytecode))

Is equivalent to Node’s:

Buffer.from(bytecode, 'hex')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading