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

Add make_addr function to MockApi #1905

Merged
merged 13 commits into from
Oct 6, 2023
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions contracts/burner/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions contracts/crypto-verify/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions contracts/cyberpunk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions contracts/floaty/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions contracts/hackatom/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions contracts/ibc-reflect-send/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions contracts/ibc-reflect/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions contracts/queue/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions contracts/reflect/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions contracts/staking/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions contracts/virus/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ bnum = "0.8.0"
static_assertions = "1.1.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
bech32 = "0.9.1"
cosmwasm-crypto = { path = "../crypto", version = "1.4.0" }

[dev-dependencies]
Expand Down
83 changes: 83 additions & 0 deletions packages/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use alloc::collections::BTreeMap;
use bech32::{encode, ToBase32, Variant};
use core::marker::PhantomData;
#[cfg(feature = "cosmwasm_1_3")]
use core::ops::Bound;
use serde::de::DeserializeOwned;
#[cfg(feature = "stargate")]
use serde::Serialize;
use sha2::{Digest, Sha256};
#[cfg(feature = "cosmwasm_1_3")]
use std::collections::BTreeSet;
use std::collections::HashMap;
Expand Down Expand Up @@ -106,6 +108,11 @@ const CANONICAL_LENGTH: usize = 90; // n = 45
const SHUFFLES_ENCODE: usize = 10;
const SHUFFLES_DECODE: usize = 2;

/// Default prefix used when creating Bech32 encoded address.
///
/// Prefix should not be empty.
DariuszDepta marked this conversation as resolved.
Show resolved Hide resolved
const BECH32_PREFIX: &str = "cosmwasm";

// MockApi zero pads all human addresses to make them fit the canonical_length
// it trims off zeros for the reverse operation.
// not really smart, but allows us to see a difference (and consistent length for canonical addresses)
Expand All @@ -114,12 +121,15 @@ pub struct MockApi {
/// Length of canonical addresses created with this API. Contracts should not make any assumptions
/// what this value is.
canonical_length: usize,
/// Prefix used for creating addresses in Bech32 encoding.
bech32_prefix: &'static str,
}

impl Default for MockApi {
fn default() -> Self {
MockApi {
canonical_length: CANONICAL_LENGTH,
bech32_prefix: BECH32_PREFIX,
}
}
}
Expand Down Expand Up @@ -244,6 +254,52 @@ impl Api for MockApi {
}
}

impl MockApi {
/// Returns [MockApi] with Bech32 prefix set to provided value.
///
/// # Example
///
/// ```
/// # use cosmwasm_std::Addr;
/// # use cosmwasm_std::testing::MockApi;
/// #
/// let mock_api = MockApi::default().with_prefix("juno");
/// let addr = mock_api.addr_make("creator").to_string();
///
/// assert_eq!("juno1h34lmpywh4upnjdg90cjf4j70aee6z8qqfspugamjp42e4q28kqsksmtyp", addr);
/// ```
pub fn with_prefix(self, bech32_prefix: &'static str) -> Self {
let MockApi {
canonical_length, ..
} = self;
Self {
canonical_length,
bech32_prefix,
}
DariuszDepta marked this conversation as resolved.
Show resolved Hide resolved
}
DariuszDepta marked this conversation as resolved.
Show resolved Hide resolved

/// Returns an address built from provided input string.
///
/// # Example
///
/// ```
/// # use cosmwasm_std::Addr;
/// # use cosmwasm_std::testing::MockApi;
/// #
/// let mock_api = MockApi::default();
/// let addr = mock_api.addr_make("creator").to_string();
DariuszDepta marked this conversation as resolved.
Show resolved Hide resolved
///
/// assert_eq!("cosmwasm1h34lmpywh4upnjdg90cjf4j70aee6z8qqfspugamjp42e4q28kqs8s7vcp", addr);
DariuszDepta marked this conversation as resolved.
Show resolved Hide resolved
/// ```
pub fn addr_make(&self, input: &str) -> Addr {
let digest = Sha256::digest(input).to_vec();
match encode(self.bech32_prefix, digest.to_base32(), Variant::Bech32) {
Ok(address) => Addr::unchecked(address),
Err(reason) => panic!("Generating address failed with reason: {reason}"),
}
}
}

/// Returns a default enviroment with height, time, chain_id, and contract address
/// You can submit as is to most contracts, or modify height/time if you want to
/// test for expiration.
Expand Down Expand Up @@ -2264,4 +2320,31 @@ mod tests {

assert_eq!(digit_sum(&[255, 1]), 256);
}

#[test]
fn making_an_address_works() {
let mock_api = MockApi::default();

assert_eq!(
"cosmwasm1h34lmpywh4upnjdg90cjf4j70aee6z8qqfspugamjp42e4q28kqs8s7vcp",
mock_api.addr_make("creator").to_string()
);

assert_eq!(
"cosmwasm1uwcvgs5clswpfxhm7nyfjmaeysn6us0yvjdexn9yjkv3k7zjhp2sly4xh9",
mock_api.addr_make("").to_string()
);

let mock_api = MockApi::default().with_prefix("juno");
assert_eq!(
"juno1h34lmpywh4upnjdg90cjf4j70aee6z8qqfspugamjp42e4q28kqsksmtyp",
mock_api.addr_make("creator").to_string()
);
DariuszDepta marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
#[should_panic(expected = "Generating address failed with reason: invalid length")]
fn making_an_address_with_empty_prefix_should_panic() {
MockApi::default().with_prefix("").addr_make("creator");
}
}
Loading