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

Remove msg argument from instantiate2_address #1554

Merged
merged 3 commits into from
Dec 21, 2022
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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ and this project adheres to
dependency. This makes the contract incompatible with chains running versions
of CosmWasm earlier than 1.2.0 ([#1481]).
- cosmwasm-std: Add `instantiate2_address` which allows calculating the
predictable addresses for `MsgInstantiateContract2` ([#1437]).
predictable addresses for `MsgInstantiateContract2` ([#1437], [#1554]).
- cosmwasm-std: Add `WasmMsg::Instantiate2` (requires `cosmwasm_1_2`, see
`GovMsg::VoteWeighted` above) to instantiate contracts at a predictable
address ([#1436]).
address ([#1436], [#1554])).
- cosmwasm-schema: In contracts, `cosmwasm schema` will now output a separate
JSON Schema file for each entrypoint in the `raw` subdirectory ([#1478],
[#1533]).
Expand All @@ -30,6 +30,7 @@ and this project adheres to
[#1478]: https://github.com/CosmWasm/cosmwasm/pull/1478
[#1533]: https://github.com/CosmWasm/cosmwasm/pull/1533
[#1550]: https://github.com/CosmWasm/cosmwasm/issues/1550
[#1554]: https://github.com/CosmWasm/cosmwasm/pull/1554

### Changed

Expand Down
52 changes: 31 additions & 21 deletions packages/std/src/addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub enum Instantiate2AddressError {
/// let canonical_creator = deps.api.addr_canonicalize(env.contract.address.as_str())?;
/// let checksum = HexBinary::from_hex("9af782a3a1bcbcd22dbb6a45c751551d9af782a3a1bcbcd22dbb6a45c751551d")?;
/// let salt = b"instance 1231";
/// let canonical_addr = instantiate2_address(&checksum, &canonical_creator, salt, None)
/// let canonical_addr = instantiate2_address(&checksum, &canonical_creator, salt)
/// .map_err(|_| StdError::generic_err("Could not calculate addr"))?;
/// let addr = deps.api.addr_humanize(&canonical_addr)?;
///
Expand All @@ -326,7 +326,19 @@ pub fn instantiate2_address(
checksum: &[u8],
creator: &CanonicalAddr,
salt: &[u8],
msg: Option<&[u8]>,
) -> Result<CanonicalAddr, Instantiate2AddressError> {
instantiate2_address_impl(checksum, creator, salt, b"")
}

/// The instantiate2 address derivation implementation. This API is used for
/// testing puposes only. The `msg` field is discouraged and should not be used.
/// Use [`instantiate2_address`].
#[doc(hidden)]
fn instantiate2_address_impl(
checksum: &[u8],
creator: &CanonicalAddr,
salt: &[u8],
msg: &[u8],
) -> Result<CanonicalAddr, Instantiate2AddressError> {
if checksum.len() != 32 {
return Err(Instantiate2AddressError::InvalidChecksumLength);
Expand All @@ -336,8 +348,6 @@ pub fn instantiate2_address(
return Err(Instantiate2AddressError::InvalidSaltLength);
};

let msg = msg.unwrap_or_default();

let mut key = Vec::<u8>::new();
key.extend_from_slice(b"wasm\0");
key.extend_from_slice(&(checksum.len() as u64).to_be_bytes());
Expand Down Expand Up @@ -659,23 +669,23 @@ mod tests {
}

#[test]
fn instantiate2_address_works() {
fn instantiate2_address_impl_works() {
let checksum1 =
HexBinary::from_hex("13a1fc994cc6d1c81b746ee0c0ff6f90043875e0bf1d9be6b7d779fc978dc2a5")
.unwrap();
let creator1 = CanonicalAddr::from(hex!("9999999999aaaaaaaaaabbbbbbbbbbcccccccccc"));
let salt1 = hex!("61");
let salt2 = hex!("aabbccddeeffffeeddbbccddaa66551155aaaabbcc787878789900aabbccddeeffffeeddbbccddaa66551155aaaabbcc787878789900aabbbbcc221100acadae");
let msg1: Option<&[u8]> = None;
let msg2: Option<&[u8]> = Some(b"{}");
let msg3: Option<&[u8]> = Some(b"{\"some\":123,\"structure\":{\"nested\":[\"ok\",true]}}");
let msg1: &[u8] = b"";
let msg2: &[u8] = b"{}";
let msg3: &[u8] = b"{\"some\":123,\"structure\":{\"nested\":[\"ok\",true]}}";

// No msg
let expected = CanonicalAddr::from(hex!(
"5e865d3e45ad3e961f77fd77d46543417ced44d924dc3e079b5415ff6775f847"
));
assert_eq!(
instantiate2_address(&checksum1, &creator1, &salt1, msg1).unwrap(),
instantiate2_address_impl(&checksum1, &creator1, &salt1, msg1).unwrap(),
expected
);

Expand All @@ -684,7 +694,7 @@ mod tests {
"0995499608947a5281e2c7ebd71bdb26a1ad981946dad57f6c4d3ee35de77835"
));
assert_eq!(
instantiate2_address(&checksum1, &creator1, &salt1, msg2).unwrap(),
instantiate2_address_impl(&checksum1, &creator1, &salt1, msg2).unwrap(),
expected
);

Expand All @@ -693,7 +703,7 @@ mod tests {
"83326e554723b15bac664ceabc8a5887e27003abe9fbd992af8c7bcea4745167"
));
assert_eq!(
instantiate2_address(&checksum1, &creator1, &salt1, msg3).unwrap(),
instantiate2_address_impl(&checksum1, &creator1, &salt1, msg3).unwrap(),
expected
);

Expand All @@ -702,42 +712,42 @@ mod tests {
"9384c6248c0bb171e306fd7da0993ec1e20eba006452a3a9e078883eb3594564"
));
assert_eq!(
instantiate2_address(&checksum1, &creator1, &salt2, None).unwrap(),
instantiate2_address_impl(&checksum1, &creator1, &salt2, b"").unwrap(),
expected
);

// Salt too short or too long
let empty = Vec::<u8>::new();
assert!(matches!(
instantiate2_address(&checksum1, &creator1, &empty, None).unwrap_err(),
instantiate2_address_impl(&checksum1, &creator1, &empty, b"").unwrap_err(),
Instantiate2AddressError::InvalidSaltLength
));
let too_long = vec![0x11; 65];
assert!(matches!(
instantiate2_address(&checksum1, &creator1, &too_long, None).unwrap_err(),
instantiate2_address_impl(&checksum1, &creator1, &too_long, b"").unwrap_err(),
Instantiate2AddressError::InvalidSaltLength
));

// invalid checksum length
let broken_cs = hex!("13a1fc994cc6d1c81b746ee0c0ff6f90043875e0bf1d9be6b7d779fc978dc2");
assert!(matches!(
instantiate2_address(&broken_cs, &creator1, &salt1, None).unwrap_err(),
instantiate2_address_impl(&broken_cs, &creator1, &salt1, b"").unwrap_err(),
Instantiate2AddressError::InvalidChecksumLength
));
let broken_cs = hex!("");
assert!(matches!(
instantiate2_address(&broken_cs, &creator1, &salt1, None).unwrap_err(),
instantiate2_address_impl(&broken_cs, &creator1, &salt1, b"").unwrap_err(),
Instantiate2AddressError::InvalidChecksumLength
));
let broken_cs = hex!("13a1fc994cc6d1c81b746ee0c0ff6f90043875e0bf1d9be6b7d779fc978dc2aaaa");
assert!(matches!(
instantiate2_address(&broken_cs, &creator1, &salt1, None).unwrap_err(),
instantiate2_address_impl(&broken_cs, &creator1, &salt1, b"").unwrap_err(),
Instantiate2AddressError::InvalidChecksumLength
));
}

#[test]
fn instantiate2_address_works_for_cosmjs_testvectors() {
fn instantiate2_address_impl_works_for_cosmjs_testvectors() {
// Test data from https://github.com/cosmos/cosmjs/pull/1253
const COSMOS_ED25519_TESTS_JSON: &str = "./testdata/instantiate2_addresses.json";

Expand Down Expand Up @@ -793,12 +803,12 @@ mod tests {
out: _,
} in read_tests()
{
let msg = input.msg.map(|msg| msg.into_bytes());
let addr = instantiate2_address(
let msg = input.msg.map(|msg| msg.into_bytes()).unwrap_or_default();
let addr = instantiate2_address_impl(
&input.checksum,
&input.creator_data.into(),
&input.salt,
msg.as_deref(),
&msg,
)
.unwrap();
assert_eq!(addr, intermediate.address_data);
Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/results/cosmos_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub enum WasmMsg {
///
/// This is translated to a [MsgInstantiateContract2](https://github.com/CosmWasm/wasmd/blob/v0.29.2/proto/cosmwasm/wasm/v1/tx.proto#L73-L96).
/// `sender` is automatically filled with the current contract's address.
/// `fix_msg` is automatically set to false.
#[cfg(feature = "cosmwasm_1_2")]
Instantiate2 {
admin: Option<String>,
Expand All @@ -175,7 +176,6 @@ pub enum WasmMsg {
msg: Binary,
funds: Vec<Coin>,
salt: Binary,
fix_msg: bool,
},
/// Migrates a given contracts to use new wasm code. Passes a MigrateMsg to allow us to
/// customize behavior.
Expand Down