Skip to content

Commit

Permalink
Support ethermint mode with features
Browse files Browse the repository at this point in the history
- update deep_space with ethermint feature
- add a PrivateKey which switch to ethermint when ethermint feature is turn on
- change default hd_path if ethermint feature turn on
  • Loading branch information
yihuang authored and thomas-nguy committed Feb 9, 2022
1 parent fef4573 commit d3ae46d
Show file tree
Hide file tree
Showing 24 changed files with 875 additions and 603 deletions.
1,285 changes: 707 additions & 578 deletions orchestrator/Cargo.lock

Large diffs are not rendered by default.

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
2 changes: 1 addition & 1 deletion orchestrator/ethereum_gravity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2018"
gravity_abi = { path = "../gravity_abi" }
gravity_utils = { path = "../gravity_utils" }

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"
web30 = "0.15.4"
Expand Down
7 changes: 5 additions & 2 deletions orchestrator/gorc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ gravity_utils = { path = "../gravity_utils" }
orchestrator = { path = "../orchestrator" }
relayer = { path = "../relayer" }

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.12"
actix-rt = "2.5"
Expand Down Expand Up @@ -50,4 +50,7 @@ version = "0.6.0-rc.0"

[dev-dependencies]
abscissa_core = { version = "0.6.0-rc.0", features = ["testing"] }
once_cell = "1.2"
once_cell = "1.2"

[features]
ethermint = ["cosmos_gravity/ethermint", "orchestrator/ethermint", "relayer/ethermint"]
3 changes: 2 additions & 1 deletion orchestrator/gorc/src/commands/tx/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::{application::APP, prelude::*, utils::*};
use abscissa_core::{clap::Parser, Command, Runnable};
use clarity::Uint256;
use cosmos_gravity::send::send_to_eth;
use deep_space::{coin::Coin, private_key::PrivateKey as CosmosPrivateKey};
use deep_space::coin::Coin;
use ethers::types::Address as EthAddress;
use gravity_proto::gravity::DenomToErc20Request;
use gravity_utils::connection_prep::{check_for_fee_denom, create_rpc_connections};
use cosmos_gravity::crypto::PrivateKey as CosmosPrivateKey;
use regex::Regex;
use std::process::exit;

Expand Down
5 changes: 3 additions & 2 deletions orchestrator/gorc/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cosmos_gravity::crypto::DEFAULT_HD_PATH;
use ethers::signers::LocalWallet as EthWallet;
use serde::{Deserialize, Serialize};
use signatory::FsKeyStore;
Expand Down Expand Up @@ -32,7 +33,7 @@ impl GorcConfig {
EthWallet::from(self.load_secret_key(name))
}

pub fn load_deep_space_key(&self, name: String) -> deep_space::private_key::PrivateKey {
pub fn load_deep_space_key(&self, name: String) -> cosmos_gravity::crypto::PrivateKey {
let key = self.load_secret_key(name).to_bytes();
let key = deep_space::utils::bytes_to_hex_str(&key);
key.parse().expect("Could not parse private key")
Expand Down Expand Up @@ -101,7 +102,7 @@ pub struct CosmosSection {
impl Default for CosmosSection {
fn default() -> Self {
Self {
key_derivation_path: "m/44'/118'/0'/0/0".to_owned(),
key_derivation_path: DEFAULT_HD_PATH.to_owned(),
grpc: "http://localhost:9090".to_owned(),
prefix: "cosmos".to_owned(),
gas_price: GasPrice::default(),
Expand Down
6 changes: 5 additions & 1 deletion orchestrator/gravity_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2018"
gravity_abi = { path = "../gravity_abi" }
gravity_proto = { path = "../gravity_proto/" }
cosmos-sdk-proto = "0.6.3"
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"] }
web30 = "0.15"
clarity = "0.4.11"
Expand All @@ -24,7 +24,11 @@ num-bigint = "0.4"
log = "0.4"
url = "2"
sha3 = "0.9"
tiny-bip39 = "0.8.0"
bitcoin = { version = "=0.27", features = ["use-serde"] }
hdpath = { version = "0.6.0", features = ["with-bitcoin"] }
rustc-hex = "2.1.0"

[dev_dependencies]
rand = "0.8"
actix = "0.12"
5 changes: 4 additions & 1 deletion orchestrator/orchestrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ gravity_abi = { path = "../gravity_abi" }
gravity_utils = { path = "../gravity_utils" }
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"] }
serde_derive = "1.0"
clarity = "0.4.11"
Expand Down Expand Up @@ -45,3 +45,6 @@ prometheus = "0.12.0"
# feature includes it's own OpenSSL version that's compiled on the fly
# If ANY crate in this workspace has this it will work for all of them.
openssl = { version = "0.10", features = ["vendored"] }

[features]
ethermint = ["cosmos_gravity/ethermint", "relayer/ethermint"]
2 changes: 1 addition & 1 deletion orchestrator/orchestrator/src/ethereum_event_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::get_with_retry::get_chain_id_with_retry;
use crate::metrics;
use cosmos_gravity::build;
use cosmos_gravity::query::get_last_event_nonce;
use deep_space::private_key::PrivateKey as CosmosPrivateKey;
use cosmos_gravity::crypto::PrivateKey as CosmosPrivateKey;
use deep_space::{Contact, Msg};
use ethereum_gravity::types::EthClient;
use ethers::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion orchestrator/orchestrator/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use cosmos_gravity::{
};
use deep_space::client::ChainStatus;
use deep_space::error::CosmosGrpcError;
use deep_space::private_key::PrivateKey as CosmosPrivateKey;
use cosmos_gravity::crypto::PrivateKey as CosmosPrivateKey;
use deep_space::{Contact, Msg};
use ethereum_gravity::types::EthClient;
use ethereum_gravity::utils::get_gravity_id;
Expand Down
7 changes: 5 additions & 2 deletions orchestrator/register_delegate_keys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cosmos_gravity = {path = "../cosmos_gravity"}
gravity_utils = {path = "../gravity_utils"}
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"] }
contact = "0.4"
serde_derive = "1.0"
Expand All @@ -28,4 +28,7 @@ web30 = "0.15"
env_logger = "0.8"
log = "0.4.14"
openssl-probe = "0.1"
rand = "0.8"
rand = "0.8"

[features]
ethermint = ["cosmos_gravity/ethermint", "relayer/ethermint"]
3 changes: 2 additions & 1 deletion orchestrator/register_delegate_keys/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ extern crate lazy_static;

use clarity::PrivateKey as EthPrivateKey;
use cosmos_gravity::send::update_gravity_delegate_addresses;
use deep_space::{mnemonic::Mnemonic, private_key::PrivateKey as CosmosPrivateKey};
use deep_space::{coin::Coin, mnemonic::Mnemonic};
use docopt::Docopt;
use ethers::core::k256::ecdsa::SigningKey;
use ethers::prelude::*;
use gravity_utils::connection_prep::check_for_fee_denom;
use gravity_utils::connection_prep::{create_rpc_connections, wait_for_cosmos_node_ready};
use log::error;
use cosmos_gravity::crypto::PrivateKey as CosmosPrivateKey;
use rand::{thread_rng, Rng};
use std::time::Duration;

Expand Down
6 changes: 4 additions & 2 deletions orchestrator/relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ gravity_abi = { path = "../gravity_abi" }
gravity_utils = { path = "../gravity_utils" }
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"] }
serde_derive = "1.0"
clarity = "0.4.11"
Expand All @@ -34,6 +34,8 @@ tokio = "1.4"
tonic = "0.4"
openssl-probe = "0.1"


[dev-dependencies]
actix = "0.12"

[features]
ethermint = ["cosmos_gravity/ethermint"]
5 changes: 4 additions & 1 deletion orchestrator/test_runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ gravity_utils = {path = "../gravity_utils"}
gravity_proto = {path = "../gravity_proto/"}
orchestrator = {path = "../orchestrator/"}

deep_space ={git="https://github.com/iqlusioninc/deep_space/", branch="master"}
deep_space = "2.4.7"
serde_derive = "1.0"
clarity = "0.4.11"
docopt = "1"
Expand All @@ -36,3 +36,6 @@ rand = "0.8"
tonic = "0.4"
futures = "0.3.18"
hex = "0.4.3"

[features]
ethermint = ["cosmos_gravity/ethermint", "orchestrator/ethermint"]
2 changes: 1 addition & 1 deletion orchestrator/test_runner/src/bootstrapping.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::utils::ValidatorKeys;
use deep_space::private_key::PrivateKey as CosmosPrivateKey;
use cosmos_gravity::crypto::PrivateKey as CosmosPrivateKey;
use ethers::core::k256::ecdsa::SigningKey;
use ethers::types::Address as EthAddress;
use std::fs::File;
Expand Down
4 changes: 2 additions & 2 deletions orchestrator/test_runner/src/happy_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use crate::MINER_CLIENT;
use crate::OPERATION_TIMEOUT;
use crate::TOTAL_TIMEOUT;
use clarity::Uint256;
use cosmos_gravity::crypto::PrivateKey as CosmosPrivateKey;
use cosmos_gravity::send::{send_request_batch_tx, send_to_eth};
use cosmos_gravity::{build, query::get_oldest_unsigned_transaction_batch, send};
use deep_space::address::Address as CosmosAddress;
use deep_space::coin::Coin;
use deep_space::private_key::PrivateKey as CosmosPrivateKey;
use deep_space::Contact;
use ethereum_gravity::erc20_utils::get_erc20_balance;
use ethereum_gravity::utils::get_valset_nonce;
Expand Down Expand Up @@ -185,7 +185,7 @@ pub async fn test_valset_update(
delegate_address.parse().unwrap(),
amount.clone(),
get_fee(),
keys_to_change.orch_key,
keys_to_change.orch_key.into(),
Some(OPERATION_TIMEOUT),
)
.await;
Expand Down
Loading

0 comments on commit d3ae46d

Please sign in to comment.