Skip to content

Commit

Permalink
Merge pull request #359 from thomas-nguy/thomas/add-ethermint-support
Browse files Browse the repository at this point in the history
Support ethermint mode with features
  • Loading branch information
zmanian authored Feb 22, 2022
2 parents 76b32bb + d438d94 commit 9bf8d5d
Show file tree
Hide file tree
Showing 24 changed files with 258 additions and 33 deletions.
100 changes: 91 additions & 9 deletions orchestrator/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions orchestrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,15 @@ gorc = { path = "./gorc" }
relayer = { path = "./relayer" }
gravity_abi_build = { path = "./gravity_abi_build" }
gravity_abi = { path = "./gravity_abi" }

[features]
ethermint = [
"orchestrator/ethermint",
"cosmos_gravity/ethermint",
"relayer/ethermint",
"gorc/ethermint",
"register_delegate_keys/ethermint",
]

[patch.crates-io]
deep_space = { git = "https://github.com/iqlusioninc/deep_space/", branch = "master" }
5 changes: 4 additions & 1 deletion orchestrator/cosmos_gravity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ gravity_utils = {path = "../gravity_utils"}
ethereum_gravity = {path = "../ethereum_gravity"}
gravity_proto = {path = "../gravity_proto/"}

deep_space ={git="https://github.com/iqlusioninc/deep_space/", branch="master"}
deep_space = "2.4.7"
ethers = { git = "https://github.com/iqlusioninc/ethers-rs.git", branch="zaki/error_abi_support", features = ["abigen"] }
clarity = "0.4.11"
serde = "1.0"
Expand All @@ -29,3 +29,6 @@ bytes = "1"
env_logger = "0.8"
rand = "0.8"
actix = "0.12"

[features]
ethermint = []
3 changes: 2 additions & 1 deletion orchestrator/cosmos_gravity/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use deep_space::private_key::PrivateKey as CosmosPrivateKey;
use deep_space::Contact;
use deep_space::Msg;
use ethereum_gravity::types::EthClient;
Expand All @@ -13,6 +12,8 @@ use gravity_utils::message_signatures::{
use gravity_utils::types::*;
use std::collections::BTreeMap;

use crate::crypto::PrivateKey as CosmosPrivateKey;

pub async fn signer_set_tx_confirmation_messages(
contact: &Contact,
eth_client: EthClient,
Expand Down
104 changes: 104 additions & 0 deletions orchestrator/cosmos_gravity/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use deep_space::private_key::TxParts;
use std::str::FromStr;

#[cfg(not(feature = "ethermint"))]
use deep_space::public_key::COSMOS_PUBKEY_URL;
use deep_space::{
error::{Bip39Error, HdWalletError, PrivateKeyError},
private_key::{PrivateKey as InnerPrivateKey, SignType},
Address, MessageArgs, Msg,
};

#[cfg(feature = "ethermint")]
pub const DEFAULT_HD_PATH: &str = "m/44'/60'/0'/0/0";
#[cfg(not(feature = "ethermint"))]
pub const DEFAULT_HD_PATH: &str = "m/44'/118'/0'/0/0";

/// PrivateKey wraps cosmos private key, switch between cosmos and ethermint behavior according to cargo features.
#[derive(Debug, Copy, Clone)]
pub struct PrivateKey(InnerPrivateKey);

impl FromStr for PrivateKey {
type Err = PrivateKeyError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
InnerPrivateKey::from_str(s).map(Self)
}
}

impl Into<InnerPrivateKey> for PrivateKey {
fn into(self) -> InnerPrivateKey {
self.0
}
}

impl PrivateKey {
pub fn from_hd_wallet_path(
hd_path: &str,
phrase: &str,
passphrase: &str,
) -> Result<Self, PrivateKeyError> {
InnerPrivateKey::from_hd_wallet_path(hd_path, phrase, passphrase).map(Self)
}

pub fn from_phrase(phrase: &str, passphrase: &str) -> Result<Self, PrivateKeyError> {
if phrase.is_empty() {
return Err(HdWalletError::Bip39Error(Bip39Error::BadWordCount(0)).into());
}
Self::from_hd_wallet_path(DEFAULT_HD_PATH, phrase, passphrase)
}

pub fn from_secret(secret: &[u8]) -> Self {
Self(InnerPrivateKey::from_secret(secret))
}

pub fn to_address(&self, prefix: &str) -> Result<Address, PrivateKeyError> {
#[cfg(feature = "ethermint")]
let result = {
let pubkey = self.0.to_public_key("")?;
Ok(pubkey.to_ethermint_address_with_prefix(prefix)?)
};
#[cfg(not(feature = "ethermint"))]
let result = self.0.to_address(prefix);

result
}

pub fn sign_std_msg(
&self,
messages: &[Msg],
args: MessageArgs,
memo: impl Into<String>,
) -> Result<Vec<u8>, PrivateKeyError> {
#[cfg(feature = "ethermint")]
let result = self.0.sign_std_msg_ethermint(
messages,
args,
memo,
"/ethermint.crypto.v1.ethsecp256k1.PubKey",
);
#[cfg(not(feature = "ethermint"))]
let result = self.0.sign_std_msg(messages, args, memo);

result
}

pub fn build_tx(
&self,
messages: &[Msg],
args: MessageArgs,
memo: impl Into<String>,
) -> Result<TxParts, PrivateKeyError> {
#[cfg(feature = "ethermint")]
return self.0.build_tx(
messages,
args,
memo,
"/ethermint.crypto.v1.ethsecp256k1.PubKey",
SignType::Ethermint,
);
#[cfg(not(feature = "ethermint"))]
return self
.0
.build_tx(messages, args, memo, COSMOS_PUBKEY_URL, SignType::Cosmos);
}
}
1 change: 1 addition & 0 deletions orchestrator/cosmos_gravity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
extern crate log;

pub mod build;
pub mod crypto;
pub mod query;
pub mod send;
pub mod utils;
3 changes: 2 additions & 1 deletion orchestrator/cosmos_gravity/src/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use bytes::BytesMut;
use deep_space::address::Address;
use deep_space::coin::Coin;
use deep_space::error::CosmosGrpcError;
use deep_space::private_key::PrivateKey as CosmosPrivateKey;
use deep_space::Contact;
use deep_space::Fee;
use deep_space::Msg;
Expand All @@ -19,6 +18,8 @@ use std::cmp;
use std::collections::HashSet;
use std::{result::Result, time::Duration};

use crate::crypto::PrivateKey as CosmosPrivateKey;

pub const MEMO: &str = "Sent using Gravity Bridge Orchestrator";
pub const TIMEOUT: Duration = Duration::from_secs(60);

Expand Down
Loading

0 comments on commit 9bf8d5d

Please sign in to comment.