I've been wondering how I can access a contract's code hash. I see that from ink version 3.0.1, there is a function inside the ink_env crate code_hash but even when updating to the following version, I don't have access to it. Is there any other way of doing this or a similar one, just to retrieve the code hash, i.e from the "metadata.json" of the contract or the ".contract" file?
2 Answers
By looking at the code, it looks like the code has been just landed, maybe that's why you don't have access to it.
Is there any other way of doing this or a similar one, just to retrieve the code hash, i.e from the "metadata.json" of the contract or the ".contract" file?
Assuming you don't need to read the code hash on-chain and you just need to obtain the code hash of a given wasm file, then it should be rather simple.
Per this, the code_hash
is nothing else but the hash of the wasm code according to the specified hasher. This is chain dependent, but more often than not it is blake2-256 hasher (or more specifically, it is the blake2b-256).
As you mentioned, according to the ink! docs, you should have access to code_hash.
Make sure you're using the correct syntax. In an #[ink(constructor)]
use Self::env().code_hash()
or if you're trying to access the code_hash
in an #[ink(message)]
then use self.env().code_hash()
.
You can see a list of all the functions available to you here.
If you're asking from the contracts-pallet
side, you can also access the code hash via:
Chain state > Storage > contracts > codeStorage
-
-
You can do manually on polkadot{.js} UI or you can use JavaScript and call the polkadot{.js} api: polkadot.js.org/docs/substrate/…– Bruno ♦Apr 19, 2022 at 13:16
-
-