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 :

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')

Leave a Reply