Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move getBytecodeHash function to axelar-chains-config #51

Merged
merged 6 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
43 changes: 43 additions & 0 deletions axelar-chains-config/src/utils/getBytecodeHash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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');
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
}

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'),
};