Skip to content

Commit

Permalink
Merge pull request #1876 from CosmWasm/1456-private-newtypes
Browse files Browse the repository at this point in the history
[2.0] Privatize CanonicalAddr, Binary and Size inner values
  • Loading branch information
webmaster128 authored Nov 6, 2023
2 parents ffeb6d3 + 656813b commit 3d56455
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 100 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ and this project adheres to
`ConversionOverflowError` and `DivideByZeroError`. ([#1896])
- cosmwasm-std: Add `SubMsg:reply_never` constructor ([#1929])
- cosmwasm-std: Add optional memo field to `IbcMsg::Transfer`. ([#1878])
- cosmwasm-std: Make inner values of `CanonicalAddr` and `Binary` private and
add constructor for `Binary`. ([#1876])
- cosmwasm-vm: Make inner value of `Size` private and add constructor. ([#1876])

[#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874
[#1876]: https://github.com/CosmWasm/cosmwasm/pull/1876
[#1878]: https://github.com/CosmWasm/cosmwasm/pull/1878
[#1879]: https://github.com/CosmWasm/cosmwasm/pull/1879
[#1890]: https://github.com/CosmWasm/cosmwasm/pull/1890
Expand Down
16 changes: 16 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ major releases of `cosmwasm`. Note that you can also view the
+Coin::new(1234u128, "uatom")
```

- When creating a `Binary` or `Size` instance from an inner value, you now have
to explicitly call `new`:

```diff
-Binary(vec![1u8])
+Binary::new(vec![1u8])
```

- When accessing the inner value of a `CanonicalAddr` or `Binary`, use
`as_slice` instead:

```diff
-&canonical_addr.0
+canonical_addr.as_slice()
```

## 1.4.x -> 1.5.0

- Update `cosmwasm-*` dependencies in Cargo.toml (skip the ones you don't use):
Expand Down
82 changes: 42 additions & 40 deletions contracts/crypto-verify/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
public_key,
} => to_json_binary(&query_verify_cosmos(
deps,
&message.0,
&signature.0,
&public_key.0,
message.as_slice(),
signature.as_slice(),
public_key.as_slice(),
)?),
QueryMsg::VerifyEthereumText {
message,
Expand Down Expand Up @@ -69,9 +69,9 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
public_key,
} => to_json_binary(&query_verify_tendermint(
deps,
&message.0,
&signature.0,
&public_key.0,
message.as_slice(),
signature.as_slice(),
public_key.as_slice(),
)?),
QueryMsg::VerifyTendermintBatch {
messages,
Expand Down Expand Up @@ -268,9 +268,9 @@ mod tests {
let public_key = hex::decode(SECP256K1_PUBLIC_KEY_HEX).unwrap();

let verify_msg = QueryMsg::VerifyCosmosSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
Expand All @@ -290,9 +290,9 @@ mod tests {
let public_key = hex::decode(SECP256K1_PUBLIC_KEY_HEX).unwrap();

let verify_msg = QueryMsg::VerifyCosmosSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
Expand All @@ -310,9 +310,9 @@ mod tests {
let public_key = vec![];

let verify_msg = QueryMsg::VerifyCosmosSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let res = query(deps.as_ref(), mock_env(), verify_msg);
Expand Down Expand Up @@ -457,15 +457,15 @@ mod tests {

let messages = [ED25519_MESSAGE_HEX, ED25519_MESSAGE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
let public_keys = [ED25519_PUBLIC_KEY_HEX, ED25519_PUBLIC_KEY2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -487,18 +487,18 @@ mod tests {
// One message
let messages = [ED25519_MESSAGE_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// Multiple signatures
//FIXME: Use different signatures / pubkeys
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// Multiple pubkeys
let public_keys = [ED25519_PUBLIC_KEY_HEX, ED25519_PUBLIC_KEY_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -521,18 +521,18 @@ mod tests {
//FIXME: Use different messages
let messages = [ED25519_MESSAGE_HEX, ED25519_MESSAGE_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// Multiple signatures
//FIXME: Use different signatures
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// One pubkey
let public_keys = [ED25519_PUBLIC_KEY_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -553,17 +553,19 @@ mod tests {

let mut messages: Vec<Binary> = [ED25519_MESSAGE_HEX, ED25519_MESSAGE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// Alter one of the messages
messages[0].0[0] ^= 0x01;
let mut b: Vec<_> = messages.pop().unwrap().into();
b[0] ^= 0x01;
messages.push(Binary::new(b));
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
let public_keys = [ED25519_PUBLIC_KEY_HEX, ED25519_PUBLIC_KEY2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -584,16 +586,16 @@ mod tests {

let messages = [ED25519_MESSAGE_HEX, ED25519_MESSAGE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// One of the public keys is empty
let public_keys = ["", ED25519_PUBLIC_KEY_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -620,9 +622,9 @@ mod tests {
let public_key = hex::decode(ED25519_PUBLIC_KEY_HEX).unwrap();

let verify_msg = QueryMsg::VerifyTendermintSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
Expand All @@ -642,9 +644,9 @@ mod tests {
let public_key = hex::decode(ED25519_PUBLIC_KEY_HEX).unwrap();

let verify_msg = QueryMsg::VerifyTendermintSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
Expand All @@ -662,9 +664,9 @@ mod tests {
let public_key = vec![];

let verify_msg = QueryMsg::VerifyTendermintSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};
let res = query(deps.as_ref(), mock_env(), verify_msg);
assert!(res.is_err());
Expand Down
Loading

0 comments on commit 3d56455

Please sign in to comment.