Instantiate2 algorithm
With the instantiate2 algorithm you can create a contract address in a predictable and deterministic way. The underlying algorithm is rather simple.
We use SHA-256 as the underlying hashing algorithm.
You need to provide the following inputs:
- Checksum: This is the checksum of the contract code (the Wasm module, for example). This has to be a SHA-256 hash
- Creator: This is the canonicalized address of the user instantiating the contract
- Salt: This is some byte string allowing you to distinguish multiple instantiations of the same contract by the same creator; this parameter has to be under 64 bytes in length
- Message: This is usually unused. CosmWasm sets this to an empty byte string
💡
Make sure you convert all the integers into their big-endian byte representation!
instantiate2.rs
let c_checksum = concat!((checksum.len() as u64).to_be_bytes(), checksum);
let c_creator = concat!((creator.len() as u64).to_be_bytes(), creator);
let c_salt = concat!((salt.len() as u64).to_be_bytes(), salt);
let c_msg = concat!((msg.len() as u64).to_be_bytes(), msg);
let canonical_address = hash_sha256(
concat!(
hash_sha256(b"module"),
b"wasm\0",
c_checksum,
c_creator,
c_salt,
c_msg,
),
);