Skip to content

Instantly share code, notes, and snippets.

@gorgos
Created May 27, 2024 20:53
Show Gist options
  • Save gorgos/ecaadd863ad37f983de23a4176939b38 to your computer and use it in GitHub Desktop.
Save gorgos/ecaadd863ad37f983de23a4176939b38 to your computer and use it in GitHub Desktop.
fn instantiate2_address_impl(
checksum: &[u8],
creator: &CanonicalAddr,
salt: &[u8],
msg: &[u8],
) -> Result<CanonicalAddr, Instantiate2AddressError> {
if checksum.len() != 32 {
return Err(Instantiate2AddressError::InvalidChecksumLength);
}
if salt.is_empty() || salt.len() > 64 {
return Err(Instantiate2AddressError::InvalidSaltLength);
};
// Length-prefix each part of the key
let mut key = Vec::<u8>::new();
key.extend_from_slice(b"wasm\0");
key.extend_from_slice(&(checksum.len() as u64).to_be_bytes());
key.extend_from_slice(checksum);
key.extend_from_slice(&(creator.len() as u64).to_be_bytes());
key.extend_from_slice(creator);
key.extend_from_slice(&(salt.len() as u64).to_be_bytes());
key.extend_from_slice(salt);
key.extend_from_slice(&(msg.len() as u64).to_be_bytes());
key.extend_from_slice(msg);
let address_data = hash("module", &key);
Ok(CanonicalAddr::from(&address_data[0..20]))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment