-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: delegate keypair encoding to cosmrs
- Loading branch information
1 parent
284c400
commit 19d6ed3
Showing
20 changed files
with
120 additions
and
227 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// 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 { | ||
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.