Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix-single-broadcast-deadlock' i…
Browse files Browse the repository at this point in the history
…nto fix-single-broadcast-deadlock
  • Loading branch information
cgorenflo committed Sep 11, 2024
2 parents a74a1b4 + 171c524 commit e93ecb0
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 18 deletions.
17 changes: 15 additions & 2 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ service-registry = { version = "^1.0.0", path = "contracts/service-registry" }
sha3 = { version = "0.10.8", default-features = false, features = [] }
signature-verifier-api = { version = "^1.0.0", path = "packages/signature-verifier-api" }
stellar = { version = "^1.0.0", path = "external-gateways/stellar" }
stellar-xdr = { version = "21.2.0" }
strum = { version = "0.25", default-features = false, features = ["derive"] }
sui-gateway = { version = "^1.0.0", path = "packages/sui-gateway" }
sui-types = { version = "^1.0.0", path = "packages/sui-types" }
Expand Down
4 changes: 2 additions & 2 deletions external-gateways/stellar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ router-api = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
sha3 = { workspace = true }
stellar-strkey = { version = "0.0.8" }
stellar-xdr = { version = "21.2.0" }
stellar-strkey = { version = "0.0.10" }
stellar-xdr = { workspace = true }
thiserror = { workspace = true }

[lints]
Expand Down
2 changes: 2 additions & 0 deletions packages/axelar-wasm-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ schemars = "0.8.10"
serde = { version = "1.0.145", default-features = false, features = ["derive"] }
serde_json = "1.0.89"
sha3 = { workspace = true }
stellar-xdr = { workspace = true }
strum = { workspace = true }
sui-types = { workspace = true }
thiserror = { workspace = true }
valuable = { version = "0.1.0", features = ["derive"] }

[dev-dependencies]
assert_ok = { workspace = true }
cw-multi-test = "0.15.1"
hex = { version = "0.4.3", default-features = false }
rand = "0.8.5"
Expand Down
111 changes: 97 additions & 14 deletions packages/axelar-wasm-std/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::str::FromStr;
use alloy_primitives::Address;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, Api};
use error_stack::{Result, ResultExt};
use error_stack::{bail, Result, ResultExt};
use stellar_xdr::curr::ScAddress;
use sui_types::SuiAddress;

#[derive(thiserror::Error)]
Expand All @@ -17,6 +18,7 @@ pub enum Error {
pub enum AddressFormat {
Eip55,
Sui,
Stellar,
}

pub fn validate_address(address: &str, format: &AddressFormat) -> Result<(), Error> {
Expand All @@ -29,6 +31,13 @@ pub fn validate_address(address: &str, format: &AddressFormat) -> Result<(), Err
SuiAddress::from_str(address)
.change_context(Error::InvalidAddress(address.to_string()))?;
}
AddressFormat::Stellar => {
if address != address.to_uppercase() {
bail!(Error::InvalidAddress(address.to_string()))
}
ScAddress::from_str(address)
.change_context(Error::InvalidAddress(address.to_string()))?;
}
}

Ok(())
Expand All @@ -41,37 +50,64 @@ pub fn validate_cosmwasm_address(api: &dyn Api, addr: &str) -> Result<Addr, Erro

#[cfg(test)]
mod tests {
use assert_ok::assert_ok;
use cosmwasm_std::testing::MockApi;

use crate::{address, err_contains};
use crate::{address, assert_err_contains};

#[test]
fn validate_eip55_address() {
let addr = "0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5";

assert!(address::validate_address(addr, &address::AddressFormat::Eip55).is_ok());
assert_ok!(address::validate_address(
addr,
&address::AddressFormat::Eip55
));

let without_prefix = addr.strip_prefix("0x").unwrap();
assert!(address::validate_address(without_prefix, &address::AddressFormat::Eip55).is_err());
assert_err_contains!(
address::validate_address(without_prefix, &address::AddressFormat::Eip55),
address::Error,
address::Error::InvalidAddress(..)
);

let lower_case = addr.to_lowercase();
assert!(address::validate_address(&lower_case, &address::AddressFormat::Eip55).is_err());
assert_err_contains!(
address::validate_address(&lower_case, &address::AddressFormat::Eip55),
address::Error,
address::Error::InvalidAddress(..)
);

let upper_case = addr.to_uppercase();
assert!(address::validate_address(&upper_case, &address::AddressFormat::Eip55).is_err());
assert_err_contains!(
address::validate_address(&upper_case, &address::AddressFormat::Eip55),
address::Error,
address::Error::InvalidAddress(..)
);
}

#[test]
fn validate_sui_address() {
let addr = "0x8cc8d18733a4bf98de8f861d356e2191918733e3afff29f327a01b5ba2997a4d";

assert!(address::validate_address(addr, &address::AddressFormat::Sui).is_ok());
assert_ok!(address::validate_address(
addr,
&address::AddressFormat::Sui
));

let without_prefix = addr.strip_prefix("0x").unwrap();
assert!(address::validate_address(without_prefix, &address::AddressFormat::Sui).is_err());
assert_err_contains!(
address::validate_address(without_prefix, &address::AddressFormat::Sui),
address::Error,
address::Error::InvalidAddress(..)
);

let upper_case = addr.to_uppercase();
assert!(address::validate_address(&upper_case, &address::AddressFormat::Sui).is_err());
assert_err_contains!(
address::validate_address(&upper_case, &address::AddressFormat::Sui),
address::Error,
address::Error::InvalidAddress(..)
);

let mixed_case = addr
.chars()
Expand All @@ -85,23 +121,70 @@ mod tests {
.to_string()
})
.collect::<String>();
assert!(address::validate_address(&mixed_case, &address::AddressFormat::Sui).is_err());
assert_err_contains!(
address::validate_address(&mixed_case, &address::AddressFormat::Sui),
address::Error,
address::Error::InvalidAddress(..)
);

let invalid_length = format!("{}5f", addr);
assert!(address::validate_address(&invalid_length, &address::AddressFormat::Sui).is_err());
assert_err_contains!(
address::validate_address(&invalid_length, &address::AddressFormat::Sui),
address::Error,
address::Error::InvalidAddress(..)
);
}

#[test]
fn validate_cosmwasm_address() {
let api = MockApi::default();
let addr = "axelar1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v";
assert!(address::validate_cosmwasm_address(&api, addr).is_ok());
assert_ok!(address::validate_cosmwasm_address(&api, addr));

let upper_case = addr.to_uppercase();
assert!(err_contains!(
address::validate_cosmwasm_address(&api, &upper_case).unwrap_err(),
assert_err_contains!(
address::validate_cosmwasm_address(&api, &upper_case),
address::Error,
address::Error::InvalidAddress(..)
);
}

#[test]
fn validate_stellar_address() {
// account id
let addr = "GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGZ";
assert_ok!(address::validate_address(
addr,
&address::AddressFormat::Stellar
));

let lower_case = addr.to_lowercase();
assert_err_contains!(
address::validate_address(&lower_case, &address::AddressFormat::Stellar),
address::Error,
address::Error::InvalidAddress(..)
);

// contract
let addr = "CA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUWDA";
assert_ok!(address::validate_address(
addr,
&address::AddressFormat::Stellar
));

let lower_case = addr.to_lowercase();
assert_err_contains!(
address::validate_address(&lower_case, &address::AddressFormat::Stellar),
address::Error,
address::Error::InvalidAddress(..)
);

// invalid
let invalid = "MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVAAAAAAAAAAAAAJLK";
assert_err_contains!(
address::validate_address(invalid, &address::AddressFormat::Stellar),
address::Error,
address::Error::InvalidAddress(..)
);
}
}

0 comments on commit e93ecb0

Please sign in to comment.