-
Notifications
You must be signed in to change notification settings - Fork 388
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: finish removing unwraps #2881
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,22 @@ | ||
// use bech32::Error; | ||
use hyperlane_core::ChainCommunicationError; | ||
|
||
/// Errors from the crates specific to the hyperlane-cosmos | ||
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. nice |
||
/// implementation. | ||
/// This error can then be converted into the broader error type | ||
/// in hyperlane-core using the `From` trait impl | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum HyperlaneCosmosError { | ||
/// bech32 error | ||
#[error("{0}")] | ||
Bech32(#[from] bech32::Error), | ||
/// gRPC error | ||
#[error("{0}")] | ||
GrpcError(#[from] tonic::Status), | ||
} | ||
|
||
impl From<HyperlaneCosmosError> for ChainCommunicationError { | ||
fn from(value: HyperlaneCosmosError) -> Self { | ||
ChainCommunicationError::from_other(value) | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,33 +1,59 @@ | ||
use cosmrs::crypto::secp256k1::SigningKey; | ||
use cosmrs::crypto::{secp256k1::SigningKey, PublicKey}; | ||
use hyperlane_core::ChainResult; | ||
|
||
use crate::verify; | ||
|
||
#[derive(Clone, Debug)] | ||
/// Signer for cosmos chain | ||
pub struct Signer { | ||
/// prefix for signer address | ||
/// public key | ||
pub public_key: PublicKey, | ||
/// precomputed address, because computing it is a fallible operation | ||
/// and we want to avoid returning `Result` | ||
pub address: String, | ||
/// address prefix | ||
pub prefix: String, | ||
pub(crate) private_key: Vec<u8>, | ||
private_key: Vec<u8>, | ||
} | ||
|
||
impl Signer { | ||
/// create new signer | ||
pub fn new(private_key: Vec<u8>, prefix: String) -> Self { | ||
Self { | ||
/// | ||
/// # Arguments | ||
/// * `private_key` - private key for signer | ||
/// * `prefix` - prefix for signer address | ||
pub fn new(private_key: Vec<u8>, prefix: String) -> ChainResult<Self> { | ||
let address = Self::address(&private_key, &prefix)?; | ||
|
||
let signing_key = Self::build_signing_key(&private_key)?; | ||
SigningKey::from_slice(&private_key)?; | ||
let public_key = signing_key.public_key(); | ||
Ok(Self { | ||
public_key, | ||
private_key, | ||
address, | ||
prefix, | ||
} | ||
}) | ||
} | ||
|
||
/// get bech32 address | ||
pub fn address(&self) -> String { | ||
verify::pub_to_addr( | ||
SigningKey::from_slice(self.private_key.as_slice()) | ||
.unwrap() | ||
fn address(private_key: &Vec<u8>, prefix: &str) -> ChainResult<String> { | ||
let address = verify::pub_to_addr( | ||
SigningKey::from_slice(private_key.as_slice())? | ||
.public_key() | ||
.to_bytes(), | ||
self.prefix.as_str(), | ||
) | ||
.unwrap() | ||
prefix, | ||
)?; | ||
Ok(address) | ||
} | ||
|
||
/// Build a SigningKey from a private key. This cannot be | ||
/// precompiled and stored in `Signer`, because `SigningKey` is not `Sync`. | ||
pub fn signing_key(&self) -> ChainResult<SigningKey> { | ||
Self::build_signing_key(&self.private_key) | ||
} | ||
|
||
fn build_signing_key(private_key: &Vec<u8>) -> ChainResult<SigningKey> { | ||
Ok(SigningKey::from_slice(private_key.as_slice())?) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ use std::ops::Deref; | |
use crate::config::StrOrIntParseError; | ||
use cosmrs::proto::prost; | ||
use cosmrs::Error as CosmrsError; | ||
// use fixed_hash::rustc_hex::FromHexError; | ||
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. nit: rm |
||
use std::string::FromUtf8Error; | ||
|
||
use crate::HyperlaneProviderError; | ||
|
@@ -124,6 +125,9 @@ pub enum ChainCommunicationError { | |
/// Int string parsing error | ||
#[error("{0}")] | ||
ParseIntError(#[from] std::num::ParseIntError), | ||
/// Hash string parsing error | ||
#[error("{0}")] | ||
HashParsingError(#[from] fixed_hash::rustc_hex::FromHexError), | ||
/// Invalid Request | ||
#[error("Invalid Request: {msg:?}")] | ||
InvalidRequest { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: rm