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

refactor: delegate keypair encoding to cosmrs #2887

Merged
merged 7 commits into from
Nov 7, 2023

Conversation

daniel-savu
Copy link
Contributor

@daniel-savu daniel-savu commented Nov 2, 2023

Description

Delegates bech32 encoding, decoding, and cosmos address building from a private key to cosmrs/tendermint upstream crates. The only place where this wasn't doable was the tests, because of a cyclic dependency - left a TODO comment there.

As part of this, renamed rust/chains/hyperlane-cosmos/src/libs/verify.rs -> rust/chains/hyperlane-cosmos/src/libs/address.rs

Warning: We should re-enable e2e and make sure everything still works locally, since the actual encode/decode functions that end up being called are different

Testing

Tested manually EVM <> Duality (both ways)

Copy link
Collaborator

@tkporter tkporter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few spots that I think may cause issues, if we don't have e2e maybe you could stand up a testnet relayer locally and send a message on testnet or something? Or even mainnet

(_, Ok(v)) => ism_type_to_module_type(v.typ),
_ => ModuleType::Null,
})
let module_type_response =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😎

let account_id = AccountId::from_str(&addr)?;

// although `from_slice` can panic if the slice is not 32 bytes long,
// we know that we're passing in a value that is 32 bytes long because it was decoded from
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe bech32 is named that because it's the encoding is done in base 32 and not because it's 32 bytes long. I think the pattern in Cosmos is that EOA addresses are 20 bytes and smart contracts are 32 bytes. So we should probably be more defensive here

Looks like account IDs can get up to 255 bytes https://docs.rs/cosmrs/latest/cosmrs/struct.AccountId.html#associatedconstant.MAX_LENGTH

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unit tests broke this, good point

/// Wrapper around the cosmrs AccountId type that abstracts keypair conversions and
/// bech32 encoding
#[derive(new)]
pub struct CosmosAddress {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be a good place to have some quick unit tests if we're uncertain about the encoding / decoding like iiuc you flag in the PR description

@@ -1,28 +0,0 @@
use hyperlane_core::{H160, H256, H512};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@@ -1,132 +0,0 @@
use std::cmp;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪦

@@ -78,7 +77,7 @@ impl MultisigIsm for CosmosMultisigIsm {
let validators: ChainResult<Vec<H256>> = response
.validators
.iter()
.map(|v| H160::from_str(v).map(h160_to_h256).map_err(Into::into))
.map(|v| H256::from_str(v).map_err(Into::into))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe H256::from_str panics if the input length isn't 32 bytes, so we'll probably need to keep doing H160::from_str(v) and then .into() to H256

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great point

@daniel-savu
Copy link
Contributor Author

Fixed the address generation, just need to add a couple more unit tests

Copy link
Collaborator

@tkporter tkporter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! nice job

@@ -1,3 +1,7 @@
// TODO: this file can be removed if `CosmosAddress` can be imported from `hyperlane-cosmos`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is what you're currently looking into?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually no but I'll look into this as well

@daniel-savu daniel-savu merged commit 2a7d6d6 into trevor/new-featv3-cosmos-oct-28 Nov 7, 2023
2 of 4 checks passed
@daniel-savu daniel-savu deleted the dan/cosmrs-keypair branch November 7, 2023 17:56
daniel-savu added a commit that referenced this pull request Nov 8, 2023
Removes cosmos signer unwraps by making the signer optional. Depends on
#2887
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants