Skip to content

Commit

Permalink
Merge pull request #297 from PeggyJV/bolten/replace-num256
Browse files Browse the repository at this point in the history
Replace web30 and clarity with ethers-rs
  • Loading branch information
zmanian authored Dec 1, 2021
2 parents 21c5745 + 8830a8b commit 4bf240f
Show file tree
Hide file tree
Showing 73 changed files with 6,684 additions and 2,764 deletions.
1,807 changes: 1,553 additions & 254 deletions orchestrator/Cargo.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion orchestrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ members = [
"relayer",
"register_delegate_keys",
"gorc",
"abi_build",
"gravity_abi",
]

[dependencies]
Expand All @@ -36,4 +38,6 @@ test_runner = { path = "./test_runner" }
gravity_proto = { path = "./gravity_proto" }
register_delegate_keys = { path = "./register_delegate_keys" }
gorc = { path = "./gorc" }
relayer = { path = "./relayer" }
relayer = { path = "./relayer" }
abi_build = { path = "./abi_build" }
gravity_abi = { path = "./gravity_abi" }
12 changes: 12 additions & 0 deletions orchestrator/abi_build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "abi_build"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ethers = { version = "0.6.1", features = ["abigen"] }
serde_derive = "1.0"
serde_json = "1.0.69"
serde = "1.0"
58 changes: 58 additions & 0 deletions orchestrator/abi_build/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use ethers::contract::Abigen;
use std::process;

fn main() {
// Gravity contract

let abigen = match Abigen::new("Gravity", "../gravity_abi/Gravity.json") {
Ok(abigen) => abigen,
Err(e) => {
println!("Could not open Gravity.json: {}", e);
process::exit(1);
}
};

let abi = match abigen
.add_event_derive("serde::Deserialize")
.add_event_derive("serde::Serialize")
.generate()
{
Ok(abi) => abi,
Err(e) => {
println!("Could not generate abi from Gravity.json: {}", e);
process::exit(1);
}
};

match abi.write_to_file("../gravity_abi/src/gravity.rs") {
Ok(_) => (),
Err(e) => println!("Error writing gravity.rs: {}", e),
}

// OpenZeppelin ERC20 contract

let abigen = match Abigen::new("ERC20", "../gravity_abi/ERC20.json") {
Ok(abigen) => abigen,
Err(e) => {
println!("Could not open ERC20.json: {}", e);
process::exit(1);
}
};

let abi = match abigen
.add_event_derive("serde::Deserialize")
.add_event_derive("serde::Serialize")
.generate()
{
Ok(abi) => abi,
Err(e) => {
println!("Could not generate abi from ERC20.json: {}", e);
process::exit(1);
}
};

match abi.write_to_file("../gravity_abi/src/erc20.rs") {
Ok(_) => (),
Err(e) => println!("Error writing erc20.rs: {}", e),
}
}
4 changes: 2 additions & 2 deletions orchestrator/cosmos_gravity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ ethereum_gravity = {path = "../ethereum_gravity"}
gravity_proto = {path = "../gravity_proto/"}

deep_space ={git="https://github.com/iqlusioninc/deep_space/", branch="master"}
ethers = "0.6.1"
clarity = "0.4.11"
serde = "1.0"
num256 = "0.3"
log = "0.4"
sha3 = "0.9"
tokio = "1.4"
Expand All @@ -28,4 +28,4 @@ bytes = "1"
[dev-dependencies]
env_logger = "0.8"
rand = "0.8"
actix = "0.11"
actix = "0.12"
99 changes: 52 additions & 47 deletions orchestrator/cosmos_gravity/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
use std::collections::BTreeMap;

use clarity::PrivateKey as EthPrivateKey;
use deep_space::private_key::PrivateKey as CosmosPrivateKey;
use deep_space::utils::bytes_to_hex_str;
use deep_space::Contact;
use deep_space::Msg;
use ethereum_gravity::utils::downcast_uint256;
use ethereum_gravity::types::EthClient;
use ethers::prelude::*;
use ethers::utils::keccak256;
use gravity_proto::gravity as proto;
use gravity_proto::ToAny;
use gravity_utils::ethereum::{downcast_to_u64, format_eth_address};
use gravity_utils::message_signatures::{
encode_logic_call_confirm, encode_tx_batch_confirm, encode_valset_confirm,
};
use gravity_utils::types::*;
use std::collections::BTreeMap;

pub fn signer_set_tx_confirmation_messages(
pub async fn signer_set_tx_confirmation_messages(
contact: &Contact,
ethereum_key: EthPrivateKey,
eth_client: EthClient,
valsets: Vec<Valset>,
cosmos_key: CosmosPrivateKey,
gravity_id: String,
) -> Vec<Msg> {
let cosmos_address = cosmos_key.to_address(&contact.get_prefix()).unwrap();
let ethereum_address = ethereum_key.to_public_key().unwrap();
let ethereum_address = eth_client.address();

let mut msgs = Vec::new();
for valset in valsets {
let data = encode_valset_confirm(gravity_id.clone(), valset.clone());
let signature = ethereum_key.sign_ethereum_msg(&data);
let data = keccak256(encode_valset_confirm(gravity_id.clone(), valset.clone()).as_slice());
// Signer trait responds with a Result, but we use a LocalWallet and it
// will never throw an error
let signature = eth_client.signer().sign_message(data).await.unwrap();
let confirmation = proto::SignerSetTxConfirmation {
ethereum_signer: ethereum_address.to_string(),
ethereum_signer: format_eth_address(ethereum_address),
signer_set_nonce: valset.nonce,
signature: signature.to_bytes().to_vec(),
signature: signature.into(),
};
let msg = proto::MsgSubmitEthereumTxConfirmation {
signer: cosmos_address.to_string(),
Expand All @@ -42,25 +44,27 @@ pub fn signer_set_tx_confirmation_messages(
msgs
}

pub fn batch_tx_confirmation_messages(
pub async fn batch_tx_confirmation_messages(
contact: &Contact,
ethereum_key: EthPrivateKey,
eth_client: EthClient,
batches: Vec<TransactionBatch>,
cosmos_key: CosmosPrivateKey,
gravity_id: String,
) -> Vec<Msg> {
let cosmos_address = cosmos_key.to_address(&contact.get_prefix()).unwrap();
let ethereum_address = ethereum_key.to_public_key().unwrap();
let ethereum_address = eth_client.address();

let mut msgs = Vec::new();
for batch in batches {
let data = encode_tx_batch_confirm(gravity_id.clone(), batch.clone());
let signature = ethereum_key.sign_ethereum_msg(&data);
let data = keccak256(encode_tx_batch_confirm(gravity_id.clone(), batch.clone()).as_slice());
// Signer trait responds with a Result, but we use a LocalWallet and it
// will never throw an error
let signature = eth_client.signer().sign_message(data).await.unwrap();
let confirmation = proto::BatchTxConfirmation {
token_contract: batch.token_contract.to_string(),
token_contract: format_eth_address(batch.token_contract),
batch_nonce: batch.nonce,
ethereum_signer: ethereum_address.to_string(),
signature: signature.to_bytes().to_vec(),
ethereum_signer: format_eth_address(ethereum_address),
signature: signature.into(),
};
let msg = proto::MsgSubmitEthereumEvent {
signer: cosmos_address.to_string(),
Expand All @@ -72,26 +76,27 @@ pub fn batch_tx_confirmation_messages(
msgs
}

pub fn contract_call_tx_confirmation_messages(
pub async fn contract_call_tx_confirmation_messages(
contact: &Contact,
ethereum_key: EthPrivateKey,
eth_client: EthClient,
logic_calls: Vec<LogicCall>,
cosmos_key: CosmosPrivateKey,
gravity_id: String,
) -> Vec<Msg> {
let cosmos_address = cosmos_key.to_address(&contact.get_prefix()).unwrap();
let ethereum_address = ethereum_key.to_public_key().unwrap();
let ethereum_address = eth_client.address();

let mut msgs = Vec::new();
for logic_call in logic_calls {
let data = encode_logic_call_confirm(gravity_id.clone(), logic_call.clone());
let signature = ethereum_key.sign_ethereum_msg(&data);
let data =
keccak256(encode_logic_call_confirm(gravity_id.clone(), logic_call.clone()).as_slice());
// Signer trait responds with a Result, but we use a LocalWallet and it
// will never throw an error
let signature = eth_client.signer().sign_message(data).await.unwrap();
let confirmation = proto::ContractCallTxConfirmation {
ethereum_signer: ethereum_address.to_string(),
signature: signature.to_bytes().to_vec(),
invalidation_scope: bytes_to_hex_str(&logic_call.invalidation_id)
.as_bytes()
.to_vec(),
ethereum_signer: format_eth_address(ethereum_address),
signature: signature.into(),
invalidation_scope: logic_call.invalidation_id,
invalidation_nonce: logic_call.invalidation_nonce,
};
let msg = proto::MsgSubmitEthereumTxConfirmation {
Expand Down Expand Up @@ -126,12 +131,12 @@ pub fn ethereum_event_messages(
let mut unordered_msgs = BTreeMap::new();
for deposit in deposits {
let event = proto::SendToCosmosEvent {
event_nonce: downcast_uint256(deposit.event_nonce.clone()).unwrap(),
ethereum_height: downcast_uint256(deposit.block_height).unwrap(),
token_contract: deposit.erc20.to_string(),
event_nonce: downcast_to_u64(deposit.event_nonce.clone()).unwrap(),
ethereum_height: downcast_to_u64(deposit.block_height).unwrap(),
token_contract: format_eth_address(deposit.erc20),
amount: deposit.amount.to_string(),
cosmos_receiver: deposit.destination.to_string(),
ethereum_sender: deposit.sender.to_string(),
ethereum_sender: format_eth_address(deposit.sender),
};
let msg = proto::MsgSubmitEthereumEvent {
signer: cosmos_address.to_string(),
Expand All @@ -142,10 +147,10 @@ pub fn ethereum_event_messages(
}
for batch in batches {
let event = proto::BatchExecutedEvent {
event_nonce: downcast_uint256(batch.event_nonce.clone()).unwrap(),
batch_nonce: downcast_uint256(batch.batch_nonce.clone()).unwrap(),
ethereum_height: downcast_uint256(batch.block_height).unwrap(),
token_contract: batch.erc20.to_string(),
event_nonce: downcast_to_u64(batch.event_nonce.clone()).unwrap(),
batch_nonce: downcast_to_u64(batch.batch_nonce.clone()).unwrap(),
ethereum_height: downcast_to_u64(batch.block_height).unwrap(),
token_contract: format_eth_address(batch.erc20),
};
let msg = proto::MsgSubmitEthereumEvent {
signer: cosmos_address.to_string(),
Expand All @@ -156,10 +161,10 @@ pub fn ethereum_event_messages(
}
for deploy in erc20_deploys {
let event = proto::Erc20DeployedEvent {
event_nonce: downcast_uint256(deploy.event_nonce.clone()).unwrap(),
ethereum_height: downcast_uint256(deploy.block_height).unwrap(),
event_nonce: downcast_to_u64(deploy.event_nonce.clone()).unwrap(),
ethereum_height: downcast_to_u64(deploy.block_height).unwrap(),
cosmos_denom: deploy.cosmos_denom,
token_contract: deploy.erc20_address.to_string(),
token_contract: format_eth_address(deploy.erc20_address),
erc20_name: deploy.name,
erc20_symbol: deploy.symbol,
erc20_decimals: deploy.decimals as u64,
Expand All @@ -173,10 +178,10 @@ pub fn ethereum_event_messages(
}
for logic_call in logic_calls {
let event = proto::ContractCallExecutedEvent {
event_nonce: downcast_uint256(logic_call.event_nonce.clone()).unwrap(),
ethereum_height: downcast_uint256(logic_call.block_height).unwrap(),
event_nonce: downcast_to_u64(logic_call.event_nonce.clone()).unwrap(),
ethereum_height: downcast_to_u64(logic_call.block_height).unwrap(),
invalidation_id: logic_call.invalidation_id,
invalidation_nonce: downcast_uint256(logic_call.invalidation_nonce).unwrap(),
invalidation_nonce: downcast_to_u64(logic_call.invalidation_nonce).unwrap(),
};
let msg = proto::MsgSubmitEthereumEvent {
signer: cosmos_address.to_string(),
Expand All @@ -187,9 +192,9 @@ pub fn ethereum_event_messages(
}
for valset in valsets {
let event = proto::SignerSetTxExecutedEvent {
event_nonce: downcast_uint256(valset.event_nonce.clone()).unwrap(),
signer_set_tx_nonce: downcast_uint256(valset.valset_nonce.clone()).unwrap(),
ethereum_height: downcast_uint256(valset.block_height).unwrap(),
event_nonce: downcast_to_u64(valset.event_nonce.clone()).unwrap(),
signer_set_tx_nonce: downcast_to_u64(valset.valset_nonce.clone()).unwrap(),
ethereum_height: downcast_to_u64(valset.block_height).unwrap(),
members: valset.members.iter().map(|v| v.into()).collect(),
};
let msg = proto::MsgSubmitEthereumEvent {
Expand Down
5 changes: 3 additions & 2 deletions orchestrator/cosmos_gravity/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use clarity::Address as EthAddress;
use deep_space::address::Address;
use ethers::types::Address as EthAddress;
use gravity_proto::gravity::query_client::QueryClient as GravityQueryClient;
use gravity_proto::gravity::*;
use gravity_utils::error::GravityError;
use gravity_utils::ethereum::format_eth_address;
use gravity_utils::types::*;
use tonic::transport::Channel;

Expand Down Expand Up @@ -109,7 +110,7 @@ pub async fn get_transaction_batch_signatures(
let request = client
.batch_tx_confirmations(BatchTxConfirmationsRequest {
batch_nonce: nonce,
token_contract: contract_address.to_string(),
token_contract: format_eth_address(contract_address),
})
.await?;
let batch_confirms = request.into_inner().signatures;
Expand Down
Loading

0 comments on commit 4bf240f

Please sign in to comment.