Skip to content

Commit

Permalink
chore: move getBytecodeHash function to axelar-chains-config (#51)
Browse files Browse the repository at this point in the history
* feat: add getBytecodeHash function

* chore: bump to v0.1.2

* chore: fix import

* chore: add empty line

* Update axelar-chains-config/src/utils/getBytecodeHash.js

* chore: fix string

---------

Co-authored-by: Milap Sheth <[email protected]>
  • Loading branch information
npty and milapsheth authored Sep 15, 2023
1 parent e48fdb3 commit b6d46f0
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
66 changes: 62 additions & 4 deletions axelar-chains-config/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion axelar-chains-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@axelar-network/axelar-chains-config",
"version": "0.1.1",
"version": "0.1.2",
"description": "A utility to get chain information from Axelar",
"main": "src/index.js",
"types": "dist/index.d.ts",
Expand All @@ -24,6 +24,7 @@
"vitest": "^0.34.4"
},
"dependencies": {
"@ethersproject/keccak256": "^5.7.0",
"fs-extra": "^11.1.1"
}
}
45 changes: 45 additions & 0 deletions axelar-chains-config/src/utils/getBytecodeHash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { keccak256 } = require('@ethersproject/keccak256');

/**
* Compute bytecode hash for a deployed contract or contract factory as it would appear on-chain.
* Some chains don't use keccak256 for their state representation, which is taken into account by this function.
* @param {Object} contractObject - An instance of the contract or a contract factory (ethers.js Contract or ContractFactory object)
* @returns {Promise<string>} - The keccak256 hash of the contract bytecode
*/

async function getBytecodeHash(contractObject, chain = '', provider = null) {
let bytecode;

if (isString(contractObject)) {
if (provider === null) {
throw new Error('Provider must be provided for chain');
}

bytecode = await provider.getCode(contractObject);
} else if (contractObject.address) {
// Contract instance
provider = contractObject.provider;
bytecode = await provider.getCode(contractObject.address);
} else if (contractObject.bytecode) {
// Contract factory
bytecode = contractObject.bytecode;
} else {
throw new Error('Invalid contract object. Expected ethers.js Contract or ContractFactory.');
}

if (chain.toLowerCase() === 'polygon-zkevm') {
throw new Error(
"polygon-zkevm is not supported. Use getBytecodeHash from axelar-contract-deployments that handles polygon-zkevm's hash function",
);
}

return keccak256(bytecode);
}

const isString = (arg) => {
return typeof arg === 'string' && arg !== '';
};

module.exports = {
getBytecodeHash,
};
1 change: 1 addition & 0 deletions axelar-chains-config/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ module.exports = {
...require('./readJSON'),
...require('./importNetworks'),
...require('./verifyContract'),
...require('./getBytecodeHash'),
};

0 comments on commit b6d46f0

Please sign in to comment.