From b6d46f0a92d3bdd14652183ca10955ab89b70267 Mon Sep 17 00:00:00 2001 From: npty <78221556+npty@users.noreply.github.com> Date: Fri, 15 Sep 2023 14:24:59 +0700 Subject: [PATCH] chore: move getBytecodeHash function to axelar-chains-config (#51) * 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 --- axelar-chains-config/package-lock.json | 66 +++++++++++++++++-- axelar-chains-config/package.json | 3 +- .../src/utils/getBytecodeHash.js | 45 +++++++++++++ axelar-chains-config/src/utils/index.js | 1 + 4 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 axelar-chains-config/src/utils/getBytecodeHash.js diff --git a/axelar-chains-config/package-lock.json b/axelar-chains-config/package-lock.json index 13c174e4..00ba20dd 100644 --- a/axelar-chains-config/package-lock.json +++ b/axelar-chains-config/package-lock.json @@ -1,14 +1,15 @@ { - "name": "npty-axelar-chains-config", - "version": "0.1.1", + "name": "@axelar-network/axelar-chains-config", + "version": "0.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "npty-axelar-chains-config", - "version": "0.1.1", + "name": "@axelar-network/axelar-chains-config", + "version": "0.1.2", "license": "MIT", "dependencies": { + "@ethersproject/keccak256": "^5.7.0", "fs-extra": "^11.1.1" }, "devDependencies": { @@ -436,6 +437,58 @@ "node": "^12.22.0 || ^14.17.0 || >=16.0.0" } }, + "node_modules/@ethersproject/bytes": { + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/@ethersproject/bytes/-/bytes-5.7.0.tgz", + "integrity": "sha512-nsbxwgFXWh9NyYWo+U8atvmMsSdKJprTcICAkvbBffT75qDocbuggBU0SJiVK2MuTrp0q+xvLkTnGMPK1+uA9A==", + "funding": [ + { + "type": "individual", + "url": "https://gitcoin.co/grants/13/ethersjs-complete-simple-and-tiny-2" + }, + { + "type": "individual", + "url": "https://www.buymeacoffee.com/ricmoo" + } + ], + "dependencies": { + "@ethersproject/logger": "^5.7.0" + } + }, + "node_modules/@ethersproject/keccak256": { + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/@ethersproject/keccak256/-/keccak256-5.7.0.tgz", + "integrity": "sha512-2UcPboeL/iW+pSg6vZ6ydF8tCnv3Iu/8tUmLLzWWGzxWKFFqOBQFLo6uLUv6BDrLgCDfN28RJ/wtByx+jZ4KBg==", + "funding": [ + { + "type": "individual", + "url": "https://gitcoin.co/grants/13/ethersjs-complete-simple-and-tiny-2" + }, + { + "type": "individual", + "url": "https://www.buymeacoffee.com/ricmoo" + } + ], + "dependencies": { + "@ethersproject/bytes": "^5.7.0", + "js-sha3": "0.8.0" + } + }, + "node_modules/@ethersproject/logger": { + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/@ethersproject/logger/-/logger-5.7.0.tgz", + "integrity": "sha512-0odtFdXu/XHtjQXJYA3u9G0G8btm0ND5Cu8M7i5vhEcE8/HmF4Lbdqanwyv4uQTr2tx6b7fQRmgLrsnpQlmnig==", + "funding": [ + { + "type": "individual", + "url": "https://gitcoin.co/grants/13/ethersjs-complete-simple-and-tiny-2" + }, + { + "type": "individual", + "url": "https://www.buymeacoffee.com/ricmoo" + } + ] + }, "node_modules/@humanwhocodes/config-array": { "version": "0.11.11", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.11.tgz", @@ -1360,6 +1413,11 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, + "node_modules/js-sha3": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.8.0.tgz", + "integrity": "sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==" + }, "node_modules/js-yaml": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", diff --git a/axelar-chains-config/package.json b/axelar-chains-config/package.json index 0921280a..cdfe34ac 100644 --- a/axelar-chains-config/package.json +++ b/axelar-chains-config/package.json @@ -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", @@ -24,6 +24,7 @@ "vitest": "^0.34.4" }, "dependencies": { + "@ethersproject/keccak256": "^5.7.0", "fs-extra": "^11.1.1" } } diff --git a/axelar-chains-config/src/utils/getBytecodeHash.js b/axelar-chains-config/src/utils/getBytecodeHash.js new file mode 100644 index 00000000..dd0a6fdd --- /dev/null +++ b/axelar-chains-config/src/utils/getBytecodeHash.js @@ -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} - 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, +}; diff --git a/axelar-chains-config/src/utils/index.js b/axelar-chains-config/src/utils/index.js index 74c038af..a253951f 100644 --- a/axelar-chains-config/src/utils/index.js +++ b/axelar-chains-config/src/utils/index.js @@ -3,4 +3,5 @@ module.exports = { ...require('./readJSON'), ...require('./importNetworks'), ...require('./verifyContract'), + ...require('./getBytecodeHash'), };