Skip to content

Commit

Permalink
Replace usage of ethers-signers with 11 lines of ECDSA code
Browse files Browse the repository at this point in the history
  • Loading branch information
kayabaNerve committed Nov 9, 2023
1 parent ec51fa2 commit b51204a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 164 deletions.
160 changes: 4 additions & 156 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion coins/ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ k256 = { version = "^0.13.1", default-features = false, features = ["std", "ecds
frost = { package = "modular-frost", path = "../../crypto/frost", features = ["secp256k1", "tests"] }

ethers-core = { version = "2", default-features = false }
ethers-signers = { version = "2", default-features = false }
ethers-providers = { version = "2", default-features = false }
ethers-contract = { version = "2", default-features = false, features = ["abigen", "providers"] }

Expand Down
29 changes: 22 additions & 7 deletions coins/ethereum/tests/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ use std::{convert::TryFrom, sync::Arc, time::Duration, fs::File};

use rand_core::OsRng;

use ::k256::{elliptic_curve::bigint::ArrayEncoding, U256};
use ::k256::{
elliptic_curve::{bigint::ArrayEncoding, PrimeField},
U256,
};

use ethers_core::{
types::Signature,
abi::Abi,
utils::{keccak256, Anvil, AnvilInstance},
};
use ethers_contract::ContractFactory;
use ethers_providers::{Middleware, Provider, Http};
use ethers_signers::{Signer, LocalWallet};

use frost::{
curve::Secp256k1,
Expand Down Expand Up @@ -40,7 +43,7 @@ struct Artifact {
pub async fn deploy_schnorr_verifier_contract(
chain_id: u32,
client: Arc<Provider<Http>>,
wallet: &LocalWallet,
wallet: &k256::ecdsa::SigningKey,
) -> eyre::Result<Schnorr<Provider<Http>>> {
let path = "./artifacts/Schnorr.sol/Schnorr.json";
let artifact: Artifact = serde_json::from_reader(File::open(path).unwrap()).unwrap();
Expand All @@ -57,8 +60,21 @@ pub async fn deploy_schnorr_verifier_contract(
let (max_fee_per_gas, max_priority_fee_per_gas) = client.estimate_eip1559_fees(None).await?;
deployment_tx.as_eip1559_mut().unwrap().max_fee_per_gas = Some(max_fee_per_gas);
deployment_tx.as_eip1559_mut().unwrap().max_priority_fee_per_gas = Some(max_priority_fee_per_gas);
let signature = wallet.sign_transaction_sync(&deployment_tx)?;
let deployment_tx = deployment_tx.rlp_signed(&signature);

let sig_hash = deployment_tx.sighash();
let (sig, rid) = wallet.sign_prehash_recoverable(sig_hash.as_ref()).unwrap();

// EIP-155 v
let mut v = u64::from(rid.to_byte());
assert!((v == 0) || (v == 1));
v += u64::from((chain_id * 2) + 35);

let r = sig.r().to_repr();
let r_ref: &[u8] = r.as_ref();
let s = sig.s().to_repr();
let s_ref: &[u8] = s.as_ref();
let deployment_tx = deployment_tx.rlp_signed(&Signature { r: r_ref.into(), s: s_ref.into(), v });

let pending_tx = client.send_raw_transaction(deployment_tx).await?;

let mut receipt;
Expand All @@ -81,8 +97,7 @@ async fn deploy_test_contract() -> (u32, AnvilInstance, Schnorr<Provider<Http>>)
let provider =
Provider::<Http>::try_from(anvil.endpoint()).unwrap().interval(Duration::from_millis(10u64));
let chain_id = provider.get_chainid().await.unwrap().as_u32();
let wallet: LocalWallet = anvil.keys()[0].clone().into();
let wallet = wallet.with_chain_id(chain_id);
let wallet = anvil.keys()[0].clone().into();
let client = Arc::new(provider);

(chain_id, anvil, deploy_schnorr_verifier_contract(chain_id, client, &wallet).await.unwrap())
Expand Down

0 comments on commit b51204a

Please sign in to comment.