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 eth_address_to_cosmos_address function #27

Merged
merged 2 commits into from
Feb 28, 2024
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
20 changes: 20 additions & 0 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ pub fn cosmos_address_to_eth_address(
EthAddress::from_slice(address.get_bytes())
}

#[cfg(feature = "ethermint")]
// Swaps the byte interpretation of an address from EthAddress to CosmosAddress
pub fn eth_address_to_cosmos_address(
address: EthAddress,
prefix: Option<&str>,
) -> Result<Address, AddressError> {
let prefix = prefix.unwrap_or(DEFAULT_PREFIX);
Address::from_slice(address.as_bytes(), prefix)
}

#[test]
fn test_bech32() {
let address = Address::from_slice(&[0; 20], "cosmos").unwrap();
Expand Down Expand Up @@ -276,3 +286,13 @@ fn test_parse() {
.parse()
.unwrap();
}

#[cfg(feature = "ethermint")]
#[test]
fn test_address_conversion() {
let test: Address = "cosmos1vlms2r8f6x7yxjh3ynyzc7ckarqd8a96ckjvrp"
.parse()
.unwrap();
let eth_address = cosmos_address_to_eth_address(test).unwrap();
let _cosmos_address = eth_address_to_cosmos_address(eth_address, None).unwrap();
}
2 changes: 1 addition & 1 deletion src/mnemonic/language/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ mod tests {

for &(sum, lang) in &checksums {
let mut hasher = Sha256::new();
for (_idx, word) in lang.word_list().iter().enumerate() {
for word in lang.word_list().iter() {
assert!(::unicode_normalization::is_nfkd(word));
hasher.update(format!("{word}\n"));
}
Expand Down
Loading