Skip to content

Commit

Permalink
Problem: ethers-rs version not updated to 2.0.11 (fix #941)
Browse files Browse the repository at this point in the history
compiles

refactor

simpler

remove unwrap

remove unwrap

remove unwrap

fix wasm compile

fix fmt

fix binding

use firefox

replace delay with task::sleep
  • Loading branch information
leejw51crypto committed Nov 29, 2023
1 parent c4eb3ca commit f9f4aa0
Show file tree
Hide file tree
Showing 14 changed files with 749 additions and 406 deletions.
987 changes: 642 additions & 345 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ python-tests:
wasm-ci-tests:
export WASM_BINDGEN_TEST_TIMEOUT=60
@nix-shell ./integration_tests/shell.nix --run "scripts/chainmain-ctl start"
cd bindings/wasm/ && wasm-pack test --chrome --headless && cd ../..
cd bindings/wasm/ && wasm-pack test --firefox --headless && cd ../..
@nix-shell ./integration_tests/shell.nix --run "scripts/chainmain-ctl stop"
@nix-shell ./integration_tests/shell.nix --run "scripts/chainmain-ctl clear"
@nix-shell ./integration_tests/shell.nix --run "scripts/start-all"
cd bindings/wasm/ && wasm-pack test --chrome --headless -- --features cronos-test,ibc-test --test ibc --test ethereum && cd ../..
cd bindings/wasm/ && wasm-pack test --firefox --headless -- --features cronos-test,ibc-test --test ibc --test ethereum && cd ../..
@nix-shell ./integration_tests/shell.nix --run "scripts/stop-all"

# No ibc test
Expand Down
18 changes: 9 additions & 9 deletions bindings/cpp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ anyhow = "1"
serde="1"
serde_json="1"
siwe = { version = "0.5" }
ethers = { version = "=2.0.0", features = ["rustls"] }
ethers-addressbook = { version = "=2.0.0"}
ethers-contract = { version = "=2.0.0" }
ethers-core = { version = "=2.0.0" }
ethers-etherscan = { version = "=2.0.0" }
ethers-middleware = { version = "=2.0.0" }
ethers-providers = { version = "=2.0.0"}
ethers-signers = { version = "=2.0.0" }
ethers-solc = { version = "=2.0.0"}
ethers = { version = "2.0", features = ["rustls"] }
ethers-addressbook = { version = "2.0"}
ethers-contract = { version = "2.0" }
ethers-core = { version = "2.0" }
ethers-etherscan = { version = "2.0" }
ethers-middleware = { version = "2.0" }
ethers-providers = { version = "2.0"}
ethers-signers = { version = "2.0" }
ethers-solc = { version = "2.0"}
hex = "0.4"
tokio = { version = "1", features = ["rt"] }

Expand Down
6 changes: 4 additions & 2 deletions bindings/cpp/src/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ fn new_signing_eth_contract(
chainid: u64,
) -> Result<Box<EthContract>> {
let client: Provider<Http> = Provider::<Http>::try_from(&rpcserver)?;
let signingkey: SigningKey = SigningKey::from_bytes(&private_key.to_bytes())?;
let wallet: Wallet<SigningKey> = signingkey.into();
let binding = private_key.to_bytes();
let bytes = binding.as_slice(); // 32 bytes
let signing_key = SigningKey::from_bytes(bytes.into())?;
let wallet: Wallet<SigningKey> = signing_key.into();
let wallet = wallet.with_chain_id(chainid);
let signer: SignerMiddleware<Provider<Http>, Wallet<SigningKey>> =
SignerMiddleware::new(client, wallet);
Expand Down
3 changes: 2 additions & 1 deletion bindings/cpp/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ impl U256 {
/// Divides the input by 1e18
pub fn format_ether(&self) -> U256 {
let parsed_units = ParseUnits::U256(self.into());
ethers::utils::format_ether(parsed_units).into()
let formatted_string = ethers::utils::format_ether(parsed_units);
U256::from_dec_str(formatted_string).unwrap()
}

/// Convert to common ethereum unit types: ether, gwei, or wei
Expand Down
19 changes: 10 additions & 9 deletions bindings/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ console_error_panic_hook = { version = "0.1", optional = true }
cosmos-sdk-proto = { git = "https://github.com/crypto-com/cosmos-rust.git", default-features = false, features = ["cosmwasm"] }

tendermint = "0.29"
async-std = "1.12.0"

[dev-dependencies]
wasm-bindgen-test = "0.3"
ethers = { version = "=2.0.0", features = ["rustls"] }
ethers-addressbook = { version = "=2.0.0"}
ethers-contract = { version = "=2.0.0" }
ethers-core = { version = "=2.0.0" }
ethers-etherscan = { version = "=2.0.0" }
ethers-middleware = { version = "=2.0.0" }
ethers-providers = { version = "=2.0.0"}
ethers-signers = { version = "=2.0.0" }
ethers-solc = { version = "=2.0.0"}
ethers = { version = "2.0", features = ["rustls"] }
ethers-addressbook = { version = "2.0"}
ethers-contract = { version = "2.0" }
ethers-core = { version = "2.0" }
ethers-etherscan = { version = "2.0" }
ethers-middleware = { version = "2.0" }
ethers-providers = { version = "2.0"}
ethers-signers = { version = "2.0" }
ethers-solc = { version = "2.0"}
wasm-timer = "0.2"
tendermint-rpc = "0.29"
defi-wallet-core-proto = { version = "0.1", path = "../../proto" }
Expand Down
5 changes: 2 additions & 3 deletions bindings/wasm/tests/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// functions are not all used for each test files.
#![allow(dead_code)]

use async_std::task;
use core::time::Duration;
use defi_wallet_core_common::{Network, RawRpcAccountResponse, RawRpcAccountStatus, RawRpcBalance};
use defi_wallet_core_wasm::{
Expand Down Expand Up @@ -131,7 +132,5 @@ pub(crate) async fn query_cronos_balance(address: &str) -> RawRpcBalance {
}

pub(crate) async fn wait_for_timeout(secs: Option<u64>) {
Delay::new(Duration::from_secs(secs.unwrap_or(DEFAULT_WAITING_SECS)))
.await
.unwrap();
task::sleep(Duration::from_secs(secs.unwrap_or(DEFAULT_WAITING_SECS))).await;
}
18 changes: 9 additions & 9 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ bip39 = { version = "2", default-features = false }
# FIXME: switch to upstream crates.io when released
cosmrs = { git = "https://github.com/crypto-com/cosmos-rust.git" }
eyre = "0.6"
ethers = { version = "=2.0.0", features = ["rustls", "abigen"] }
ethers-addressbook = { version = "=2.0.0"}
ethers-contract = { version = "=2.0.0" }
ethers-core = { version = "=2.0.0" }
ethers-etherscan = { version = "=2.0.0" }
ethers-middleware = { version = "=2.0.0" }
ethers-providers = { version = "=2.0.0"}
ethers-signers = { version = "=2.0.0" }
ethers-solc = { version = "=2.0.0"}
ethers = { version = "2.0", features = ["rustls", "abigen"] }
ethers-addressbook = { version = "2.0"}
ethers-contract = { version = "2.0" }
ethers-core = { version = "2.0" }
ethers-etherscan = { version = "2.0" }
ethers-middleware = { version = "2.0" }
ethers-providers = { version = "2.0"}
ethers-signers = { version = "2.0" }
ethers-solc = { version = "2.0"}
ibc = { version = "0.31", features = ["serde"], default-features = false }
ibc-proto = { version = "0.26", default-features = false }
itertools = "0.10"
Expand Down
12 changes: 6 additions & 6 deletions common/src/node/ethereum/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ use crate::{
construct_simple_eth_transfer_tx, EthAmount, EthError, EthNetwork, SecretKey, WalletCoin,
WalletCoinFunc,
};
use cosmrs::bip32::secp256k1::ecdsa::SigningKey;
use ethers::prelude::{Address, LocalWallet, Middleware, Signer, SignerMiddleware, TxHash};
use ethers::providers::{Http, Provider};
use ethers::types::transaction::eip2718::TypedTransaction;
use std::{str::FromStr, sync::Arc, time::Duration};
// use ethers Http
use ethers::prelude::Wallet;
use ethers::providers::{Http, Provider};

#[cfg(not(target_arch = "wasm32"))]
use ethers::utils::hex::ToHex;
Expand Down Expand Up @@ -482,9 +479,12 @@ fn create_localwallet_client(
key: Arc<SecretKey>,
chain_id: u64,
client: Provider<Http>,
) -> Result<SignerMiddleware<Provider<Http>, Wallet<SigningKey>>, EthError> {
) -> Result<SignerMiddleware<Provider<Http>, LocalWallet>, EthError> {
let provider = client.interval(Duration::from_millis(polling_interval_ms));
let wallet = LocalWallet::from(key.get_signing_key()).with_chain_id(chain_id);
let ethers_key = key
.get_eth_signing_key()
.map_err(|_| EthError::SignatureError)?;
let wallet = LocalWallet::from(ethers_key).with_chain_id(chain_id);
let client = SignerMiddleware::new(provider, wallet);
Ok(client)
}
Expand Down
37 changes: 30 additions & 7 deletions common/src/transaction/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,15 @@ pub fn build_signed_eth_tx(
tx.set_data(data.into());
}
tx.set_chain_id(chain_id);
let wallet = LocalWallet::from(secret_key.get_signing_key()).with_chain_id(chain_id);
let sig: Signature = wallet.sign_transaction_sync(&tx);
let wallet = LocalWallet::from(
secret_key
.get_eth_signing_key()
.map_err(|_| EthError::SignatureError)?,
)
.with_chain_id(chain_id);
let sig: Signature = wallet
.sign_transaction_sync(&tx)
.map_err(|_| EthError::SignatureError)?;
let signed_tx = &tx.rlp_signed(&sig);
Ok(signed_tx.to_vec())
}
Expand Down Expand Up @@ -357,10 +364,19 @@ pub fn eth_sign_transaction(
serde_json::from_str(json_str).map_err(EthError::JsonError)?;
let type_tx: TypedTransaction = tx.to_type_tx();
if type_tx.chain_id().is_some() {
default_chain_id = type_tx.chain_id().unwrap().as_u64();
default_chain_id = type_tx
.chain_id()
.map_or_else(|| 0u64, |chainid| chainid.as_u64());
}
let wallet = LocalWallet::from(secret_key.get_signing_key()).with_chain_id(default_chain_id);
let sig = wallet.sign_transaction_sync(&type_tx);
let wallet = LocalWallet::from(
secret_key
.get_eth_signing_key()
.map_err(|_| EthError::SignatureError)?,
)
.with_chain_id(default_chain_id);
let sig = wallet
.sign_transaction_sync(&type_tx)
.map_err(|_| EthError::SignatureError)?;
let signed_tx = &type_tx.rlp_signed(&sig);
Ok(signed_tx.to_vec())
}
Expand All @@ -377,8 +393,15 @@ pub fn eth_sign_transaction_with_chainid(
tx.chain_id = Some(chain_id.into());
let type_tx: TypedTransaction = tx.to_type_tx();

let wallet = LocalWallet::from(secret_key.get_signing_key()).with_chain_id(chain_id);
let sig = wallet.sign_transaction_sync(&type_tx);
let wallet = LocalWallet::from(
secret_key
.get_eth_signing_key()
.map_err(|_| EthError::SignatureError)?,
)
.with_chain_id(chain_id);
let sig = wallet
.sign_transaction_sync(&type_tx)
.map_err(|_| EthError::SignatureError)?;
let signed_tx = &type_tx.rlp_signed(&sig);
Ok(signed_tx.to_vec())
}
Expand Down
13 changes: 10 additions & 3 deletions common/src/transaction/ethereum/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ impl EthSigner {
/// Create an instance via a secret key.
pub fn new(secret_key: Arc<SecretKey>) -> Self {
Self {
wallet: secret_key.get_signing_key().into(),
// TODO remove unwrap
wallet: secret_key.get_eth_signing_key().unwrap().into(),
}
}

Expand All @@ -33,15 +34,20 @@ impl EthSigner {
pub fn eth_sign_insecure(&self, hash: &str) -> Result<String, EthError> {
let hash = hash.strip_prefix("0x").unwrap_or(hash);
let hash = H256::from_str(hash).map_err(|_| EthError::HexConversion)?;
let signature = self.wallet.sign_hash(hash).to_string();
let signature = self
.wallet
.sign_hash(hash)
.map_err(|_| EthError::SignatureError)?
.to_string();
Ok(format!("0x{signature}"))
}

/// Sign an arbitrary message as per EIP-191.
/// Return a signature of hex string with prefix `0x`.
pub fn personal_sign(&self, message: &str) -> String {
let hash = hash_message(message);
let signature = self.wallet.sign_hash(hash).to_string();
// TODO : remove unwrap
let signature = self.wallet.sign_hash(hash).unwrap().to_string();
format!("0x{signature}")
}

Expand Down Expand Up @@ -92,6 +98,7 @@ impl EthSigner {
let signature = self
.wallet
.sign_hash(H256::from_slice(&encoded_data))
.map_err(|_| EthError::SignatureError)? // TODO: better error handling
.to_string();
Ok(format!("0x{signature}"))
}
Expand Down
29 changes: 21 additions & 8 deletions common/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ethers::core::k256::ecdsa;
use ethers::prelude::{LocalWallet, Signature, Signer, H256};
use ethers::utils::hex::{self, FromHexError, ToHex};
use ethers::utils::{hash_message, secret_key_to_address};
use ethers_core::k256::ecdsa::SigningKey as EthSigningKey;
use rand_core::{OsRng, RngCore};
use secrecy::{ExposeSecret, SecretString, Zeroize};
use std::str::FromStr;
Expand Down Expand Up @@ -66,7 +67,7 @@ impl WalletCoinFunc {
.map_err(HdWrapError::AccountId)
}
WalletCoin::Ethereum { .. } => {
let address = secret_key_to_address(&private_key.get_signing_key());
let address = secret_key_to_address(&private_key.get_eth_signing_key()?);
let address_hex: String = address.encode_hex();
Ok(format!("0x{}", address_hex))
}
Expand Down Expand Up @@ -261,8 +262,8 @@ impl SecretKey {

/// constructs secret key from bytes
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, SecretKeyWrapError> {
let signing_key =
SigningKey::from_bytes(&bytes).map_err(SecretKeyWrapError::InvalidBytes)?;
let signing_key = SigningKey::from_bytes(&bytes)
.map_err(|_| SecretKeyWrapError::InvalidBytes(ecdsa::Error::new()))?;
Ok(Self(signing_key))
}

Expand All @@ -277,22 +278,34 @@ impl SecretKey {
self.0.clone()
}

pub fn get_eth_signing_key(&self) -> Result<EthSigningKey, HdWrapError> {
let binding = self.0.to_bytes();
let bytes = binding.as_slice(); // 32 bytes
let signing_key =
EthSigningKey::from_bytes(bytes.into()).map_err(|_| HdWrapError::InvalidLength)?;
Ok(signing_key)
}

/// signs an arbitrary message as per EIP-191
/// TODO: chain_id may not be necessary?
pub fn eth_sign(&self, message: &[u8], chain_id: u64) -> Result<Signature, HdWrapError> {
let hash = hash_message(message);
let wallet = LocalWallet::from(self.get_signing_key()).with_chain_id(chain_id);
let signature = wallet.sign_hash(hash);
let wallet = LocalWallet::from(self.get_eth_signing_key()?).with_chain_id(chain_id);
let signature = wallet
.sign_hash(hash)
.map_err(|_| HdWrapError::InvalidLength)?;
Ok(signature)
}

// eth sign hash hex string without the 0x prefix
pub fn eth_sign_by_hash(&self, hash: String, chain_id: u64) -> Result<Signature, HdWrapError> {
let vhash = hex_to_bytes(hash);
let bhash: [u8; 32] = vhash.try_into().unwrap();
let bhash: [u8; 32] = vhash.try_into().map_err(|_| HdWrapError::InvalidLength)?;
let uhash: H256 = bhash.into();
let wallet = LocalWallet::from(self.get_signing_key()).with_chain_id(chain_id);
let signature = wallet.sign_hash(uhash);
let wallet = LocalWallet::from(self.get_eth_signing_key()?).with_chain_id(chain_id);
let signature = wallet
.sign_hash(uhash)
.map_err(|_| HdWrapError::InvalidLength)?;
Ok(signature)
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/full-wasm-tests
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
# Test for ibc transfer
./scripts/start-all
cd bindings/wasm/
wasm-pack test --chrome --headless -- --features ibc-test --test ibc
wasm-pack test --firefox --headless -- --features ibc-test --test ibc
cd ../..
./scripts/stop-all
2 changes: 1 addition & 1 deletion scripts/wasm-tests
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export WASM_BINDGEN_TEST_TIMEOUT=60
# Do wasm tests
sleep 5
cd ../bindings/wasm/
wasm-pack test --chrome --headless
wasm-pack test --firefox --headless

cd $script_dir

Expand Down

0 comments on commit f9f4aa0

Please sign in to comment.