Skip to content

Commit

Permalink
Added small fixes to instantiate2 algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszDepta committed Jan 13, 2025
1 parent bc2a4ac commit 652ec3f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
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>

0 comments on commit 652ec3f

Please sign in to comment.