Skip to content

Commit

Permalink
Add eth_address_to_cosmos_address function
Browse files Browse the repository at this point in the history
Just a simple utility function that is the reverse of the one we already
have.
  • Loading branch information
jkilpatr committed Feb 28, 2024
1 parent 4fe1628 commit 48f8561
Showing 1 changed file with 20 additions and 0 deletions.
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();
}

0 comments on commit 48f8561

Please sign in to comment.