Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small improvements to instantiate2 algorithm description #228

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs-test-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use strum::{AsRefStr, EnumIter, IntoEnumIterator};
static TEMPLATES: phf::Map<&'static str, &'static str> = phf_map! {
"core" => include_str!("../templates/core.tpl"),
"execute" => include_str!("../templates/execute.tpl"),
"instantiate-spec" => include_str!("../templates/instantiate-spec.tpl"),
"instantiate2-spec" => include_str!("../templates/instantiate2-spec.tpl"),
"ibc-channel" => include_str!("../templates/ibc-channel.tpl"),
"ibc-packet" => include_str!("../templates/ibc-packet.tpl"),
"storage" => include_str!("../templates/storage.tpl"),
Expand Down
2 changes: 1 addition & 1 deletion src/pages/core/specification/_meta.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default {
"instantiate2-algo": "Instantiate2 Algorithm",
"instantiate2-algo": "Instantiate2 algorithm",
};
29 changes: 18 additions & 11 deletions src/pages/core/specification/instantiate2-algo.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
---
tags: ["core", "specification"]
tags: ["core", "specification", "instantiate2"]
---

import { Callout } from "nextra/components";

[instantiate2_address]:
https://docs.rs/cosmwasm-std/latest/cosmwasm_std/fn.instantiate2_address.html

# Instantiate2 algorithm

With the instantiate2 algorithm you can create a contract address in a predictable and deterministic
Expand All @@ -13,18 +16,17 @@ 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
- **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,
- **msg**: the initialization message is usually unused; CosmWasm sets this to an empty byte string.

<Callout>
Make sure you convert all the integers into their *big-endian* byte representation!
</Callout>
Assuming that the macro `concat!{:rust}` joins two byte slices and the function `hash_sha256{:rust}`
returns the SHA-256 hash of the provided input, the Instantiate2 algorithm would look like this:

```rust filename="instantiate2.rs" template="instantiate-spec"
```rust filename="instantiate2.rs" template="instantiate2-spec"
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);
Expand All @@ -41,3 +43,8 @@ let canonical_address = hash_sha256(
),
);
```

<Callout>
Please note that an implementation of this [function][instantiate2_address] is already
available for you to use, just add the import: `use cosmwasm_std::instantiate2_address;{:rust}`
</Callout>
Loading