-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
1 parent
e48fdb3
commit b6d46f0
Showing
4 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters