-
Notifications
You must be signed in to change notification settings - Fork 400
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
Changes from 1 commit
19d6ed3
d7bf02d
c283034
a366115
f5b19b3
f8d4c7a
ad875bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::str::FromStr; | ||
|
||
use cosmrs::{crypto::secp256k1::SigningKey, AccountId}; | ||
use derive_new::new; | ||
use hyperlane_core::{ChainResult, H256}; | ||
|
||
/// decode bech32 address to H256 | ||
pub fn bech32_decode(addr: String) -> ChainResult<H256> { | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unit tests broke this, good point |
||
// bech32 | ||
Ok(H256::from_slice(&account_id.to_bytes())) | ||
} | ||
|
||
/// Wrapper around the cosmrs AccountId type that abstracts keypair conversions and | ||
/// bech32 encoding | ||
#[derive(new)] | ||
pub struct CosmosAddress { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
account_id: AccountId, | ||
} | ||
|
||
impl CosmosAddress { | ||
/// Returns a Bitcoin style address: RIPEMD160(SHA256(pubkey)) | ||
/// Source: https://github.com/cosmos/cosmos-sdk/blob/177e7f45959215b0b4e85babb7c8264eaceae052/crypto/keys/secp256k1/secp256k1.go#L154 | ||
pub fn from_pubkey(pub_key: &[u8], prefix: &str) -> ChainResult<Self> { | ||
let account_id = AccountId::new(prefix, pub_key)?; | ||
Ok(Self { account_id }) | ||
} | ||
|
||
/// Creates a wrapper arround a cosmrs AccountId from a private key byte array | ||
pub fn from_privkey(priv_key: &[u8], prefix: &str) -> ChainResult<Self> { | ||
let pubkey = SigningKey::from_slice(priv_key)?.public_key().to_bytes(); | ||
Self::from_pubkey(&pubkey, prefix) | ||
} | ||
|
||
/// Creates a wrapper arround a cosmrs AccountId from a H256 digest | ||
pub fn from_h256(digest: H256, prefix: &str) -> ChainResult<Self> { | ||
let bytes = digest.as_bytes(); | ||
CosmosAddress::from_pubkey(bytes, prefix) | ||
} | ||
|
||
/// String representation of a cosmos AccountId | ||
pub fn address(&self) -> String { | ||
self.account_id.to_string() | ||
} | ||
} | ||
|
||
/// encode H256 to bech32 address | ||
pub fn pub_to_addr(pub_key: Vec<u8>, prefix: &str) -> ChainResult<String> { | ||
Ok(CosmosAddress::from_pubkey(&pub_key, prefix)?.address()) | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,2 @@ | ||
/// This module contains all the verification variables the libraries used by the Hyperlane Cosmos chain. | ||
pub mod verify; | ||
|
||
/// This module contains all the Binary variables used by the Hyperlane Cosmos chain. | ||
pub mod binary; | ||
pub mod address; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😎