Skip to content

Commit

Permalink
Merge pull request #663 from public-awesome/humanalgorithm/fix-lowerc…
Browse files Browse the repository at this point in the history
…ase-conversion

Fix Lowercase Conversion for Eth Addresses
  • Loading branch information
humanalgorithm authored Mar 19, 2024
2 parents 3b43da4 + 37f5b01 commit 290761e
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 5 deletions.
16 changes: 14 additions & 2 deletions contracts/dydx-airdrop/src/claim_airdrop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ pub fn claim_airdrop(
eth_sig: String,
) -> Result<Response, ContractError> {
let config = CONFIG.load(deps.storage)?;
let eth_address_lower = eth_address.to_ascii_lowercase();
validation::assert_lower_case(eth_address_lower.clone())?;
validate_claim(
&deps,
info.clone(),
eth_address.clone(),
eth_address_lower.clone(),
eth_sig,
config.clone(),
)?;
let res = claim_and_whitelist_add(&deps, info, config.airdrop_amount)?;
increment_local_mint_count_for_address(deps, eth_address)?;
increment_local_mint_count_for_address(deps, eth_address_lower)?;

Ok(res.add_attribute("claimed_amount", config.airdrop_amount.to_string()))
}
Expand Down Expand Up @@ -100,13 +102,23 @@ mod validation {
)
}

pub fn assert_lower_case(eth_address: String) -> Result<(), ContractError> {
match eth_address.to_lowercase() == eth_address {
true => Ok(()),
false => Err(ContractError::EthAddressShouldBeLower {
address: eth_address,
}),
}
}

pub fn validate_claim(
deps: &DepsMut,
info: MessageInfo,
eth_address: String,
eth_sig: String,
config: Config,
) -> Result<(), ContractError> {
assert_lower_case(eth_address.clone())?;
validate_is_eligible(deps, eth_address.clone())?;
validate_eth_sig(deps, info, eth_address.clone(), eth_sig, config)?;
validate_mints_remaining(deps, &eth_address)?;
Expand Down
3 changes: 3 additions & 0 deletions contracts/dydx-airdrop/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ pub enum ContractError {

#[error("Plaintext message is too long")]
PlaintextTooLong {},

#[error("Address {address} should be converted to lowercase")]
EthAddressShouldBeLower { address: String },
}
16 changes: 14 additions & 2 deletions contracts/rekt-airdrop/src/claim_airdrop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ pub fn claim_airdrop(
eth_sig: String,
) -> Result<Response, ContractError> {
let config = CONFIG.load(deps.storage)?;
let eth_address_lower = eth_address.to_ascii_lowercase();
validation::assert_lower_case(eth_address_lower.clone())?;
validate_claim(
&deps,
info.clone(),
eth_address.clone(),
eth_address_lower.clone(),
eth_sig,
config.clone(),
)?;
let res = claim_and_whitelist_add(&deps, info, config.airdrop_amount)?;
increment_local_mint_count_for_address(deps, eth_address)?;
increment_local_mint_count_for_address(deps, eth_address_lower)?;

Ok(res.add_attribute("claimed_amount", config.airdrop_amount.to_string()))
}
Expand Down Expand Up @@ -100,13 +102,23 @@ mod validation {
)
}

pub fn assert_lower_case(eth_address: String) -> Result<(), ContractError> {
match eth_address.to_lowercase() == eth_address {
true => Ok(()),
false => Err(ContractError::EthAddressShouldBeLower {
address: eth_address,
}),
}
}

pub fn validate_claim(
deps: &DepsMut,
info: MessageInfo,
eth_address: String,
eth_sig: String,
config: Config,
) -> Result<(), ContractError> {
assert_lower_case(eth_address.clone())?;
validate_is_eligible(deps, eth_address.clone())?;
validate_eth_sig(deps, info, eth_address.clone(), eth_sig, config)?;
validate_mints_remaining(deps, &eth_address)?;
Expand Down
3 changes: 3 additions & 0 deletions contracts/rekt-airdrop/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ pub enum ContractError {

#[error("Plaintext message is too long")]
PlaintextTooLong {},

#[error("Address {address} should be converted to lowercase")]
EthAddressShouldBeLower { address: String },
}
3 changes: 2 additions & 1 deletion contracts/whitelists/whitelist-immutable/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ pub fn instantiate(
fn update_whitelist(deps: &mut DepsMut, msg: InstantiateMsg) -> Result<u64, ContractError> {
let mut count = 0u64;
for address in msg.addresses.into_iter() {
WHITELIST.save(deps.storage, &address, &true)?;
let address_lower = address.clone().to_ascii_lowercase();
WHITELIST.save(deps.storage, &address_lower, &true)?;
count += 1;
}
Ok(count)
Expand Down
62 changes: 62 additions & 0 deletions test-suite/src/dydx_airdrop/tests/test_claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,65 @@ fn test_two_claims_allowed_success() {
}];
assert_eq!(balances, expected_balance)
}

#[test]
fn test_claim_converts_to_lowercase() {
let claim_plaintext = &get_msg_plaintext(STARGAZE_WALLET_01.to_string());
let (_, eth_sig_str, _, eth_addr_str) = get_wallet_and_sig(claim_plaintext.clone());

let stargaze_wallet_01 = Addr::unchecked(STARGAZE_WALLET_01);

let mut app = custom_mock_app();
configure_mock_minter_with_mock_whitelist(&mut app);
let minter_addr = Addr::unchecked(MOCK_MINTER_ADDR_STR);
let airdrop_contract = Addr::unchecked(MOCK_AIRDROP_ADDR_STR);

let mut mixed_case = vec![];
let mut to_upper = true;
for char in eth_addr_str.chars() {
if !to_upper {
mixed_case.extend([char.to_string().to_lowercase()]);
} else {
mixed_case.extend([char.to_string().to_uppercase()]);
}
to_upper = !to_upper;
}
let mixed_case_eth_addr: String = mixed_case.into_iter().collect();

let params = InstantiateParams {
addresses: vec![mixed_case_eth_addr.clone()],
funds_amount: WHITELIST_AMOUNT + INSTANTIATION_FEE,
expected_airdrop_contract_id: 4,
minter_address: minter_addr.clone(),
admin_account: Addr::unchecked(OWNER),
app: &mut app,
per_address_limit: 1,
claim_msg_plaintext: CONFIG_PLAINTEXT.to_string(),
};
instantiate_contract(params).unwrap();
query_minter_as_expected(&mut app, airdrop_contract.clone(), minter_addr);
let balances = app
.wrap()
.query_all_balances(stargaze_wallet_01.clone())
.unwrap();
assert_eq!(balances, []);

let claim_message = ExecuteMsg::ClaimAirdrop {
eth_address: mixed_case_eth_addr,
eth_sig: eth_sig_str,
};
let _ = execute_contract_with_msg(
claim_message,
&mut app,
stargaze_wallet_01.clone(),
airdrop_contract,
)
.unwrap();

let balances = app.wrap().query_all_balances(stargaze_wallet_01).unwrap();
let expected_balance = [Coin {
denom: NATIVE_DENOM.to_string(),
amount: Uint128::new(WHITELIST_AMOUNT),
}];
assert_eq!(balances, expected_balance)
}
63 changes: 63 additions & 0 deletions test-suite/src/rekt_airdrop/tests/test_claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn test_instantiate() {
let minter_address = Addr::unchecked("contract1");
let claim_plaintext = &get_msg_plaintext(STARGAZE_WALLET_01.to_string());
let (_, _, _, eth_addr_str) = get_wallet_and_sig(claim_plaintext.clone());

let params = InstantiateParams {
addresses: vec![eth_addr_str],
funds_amount: WHITELIST_AMOUNT + INSTANTIATION_FEE,
Expand Down Expand Up @@ -593,3 +594,65 @@ fn test_two_claims_allowed_success() {
}];
assert_eq!(balances, expected_balance)
}

#[test]
fn test_claim_converts_to_lowercase() {
let claim_plaintext = &get_msg_plaintext(STARGAZE_WALLET_01.to_string());
let (_, eth_sig_str, _, eth_addr_str) = get_wallet_and_sig(claim_plaintext.clone());

let stargaze_wallet_01 = Addr::unchecked(STARGAZE_WALLET_01);

let mut app = custom_mock_app();
configure_mock_minter_with_mock_whitelist(&mut app);
let minter_addr = Addr::unchecked(MOCK_MINTER_ADDR_STR);
let airdrop_contract = Addr::unchecked(MOCK_AIRDROP_ADDR_STR);

let mut mixed_case = vec![];
let mut to_upper = true;
for char in eth_addr_str.chars() {
if !to_upper {
mixed_case.extend([char.to_string().to_lowercase()]);
} else {
mixed_case.extend([char.to_string().to_uppercase()]);
}
to_upper = !to_upper;
}
let mixed_case_eth_addr: String = mixed_case.into_iter().collect();

let params = InstantiateParams {
addresses: vec![mixed_case_eth_addr.clone()],
funds_amount: WHITELIST_AMOUNT + INSTANTIATION_FEE,
expected_airdrop_contract_id: 4,
minter_address: minter_addr.clone(),
admin_account: Addr::unchecked(OWNER),
app: &mut app,
per_address_limit: 1,
claim_msg_plaintext: CONFIG_PLAINTEXT.to_string(),
};
instantiate_contract(params).unwrap();
query_minter_as_expected(&mut app, airdrop_contract.clone(), minter_addr);
let balances = app
.wrap()
.query_all_balances(stargaze_wallet_01.clone())
.unwrap();
assert_eq!(balances, []);

let claim_message = ExecuteMsg::ClaimAirdrop {
eth_address: mixed_case_eth_addr,
eth_sig: eth_sig_str,
};
let _ = execute_contract_with_msg(
claim_message,
&mut app,
stargaze_wallet_01.clone(),
airdrop_contract,
)
.unwrap();

let balances = app.wrap().query_all_balances(stargaze_wallet_01).unwrap();
let expected_balance = [Coin {
denom: NATIVE_DENOM.to_string(),
amount: Uint128::new(WHITELIST_AMOUNT),
}];
assert_eq!(balances, expected_balance)
}

0 comments on commit 290761e

Please sign in to comment.