mirror of
https://github.com/serai-dex/serai.git
synced 2025-03-12 09:26:51 +00:00
Update to work against the latest foundry
Links the Ethereum contract tests as well
This commit is contained in:
parent
91f62672d3
commit
8d56c43f5e
2 changed files with 36 additions and 29 deletions
|
@ -1,60 +1,67 @@
|
|||
use ethereum_serai::contract::{call_verify, deploy_schnorr_verifier_contract};
|
||||
use ethers::{prelude::*, utils::Anvil};
|
||||
use std::{convert::TryFrom, sync::Arc, time::Duration};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_deploy_contract() {
|
||||
use rand_core::OsRng;
|
||||
|
||||
use k256::{elliptic_curve::bigint::ArrayEncoding, U256};
|
||||
|
||||
use ethers::{
|
||||
prelude::*,
|
||||
utils::{keccak256, Anvil, AnvilInstance},
|
||||
};
|
||||
|
||||
use frost::{
|
||||
curve::Secp256k1,
|
||||
algorithm::Schnorr as Algo,
|
||||
tests::{key_gen, algorithm_machines, sign},
|
||||
};
|
||||
|
||||
use ethereum_serai::{
|
||||
crypto,
|
||||
contract::{Schnorr, call_verify, deploy_schnorr_verifier_contract},
|
||||
};
|
||||
|
||||
async fn deploy_test_contract(
|
||||
) -> (u32, AnvilInstance, Schnorr<SignerMiddleware<Provider<Http>, LocalWallet>>) {
|
||||
let anvil = Anvil::new().spawn();
|
||||
|
||||
let wallet: LocalWallet = anvil.keys()[0].clone().into();
|
||||
let provider =
|
||||
Provider::<Http>::try_from(anvil.endpoint()).unwrap().interval(Duration::from_millis(10u64));
|
||||
let client = Arc::new(SignerMiddleware::new(provider, wallet));
|
||||
let chain_id = provider.get_chainid().await.unwrap().as_u32();
|
||||
let client = Arc::new(SignerMiddleware::new_with_provider_chain(provider, wallet).await.unwrap());
|
||||
|
||||
let _contract = deploy_schnorr_verifier_contract(client).await.unwrap();
|
||||
(chain_id, anvil, deploy_schnorr_verifier_contract(client).await.unwrap())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_deploy_contract() {
|
||||
deploy_test_contract().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_ecrecover_hack() {
|
||||
use ethereum_serai::crypto;
|
||||
use ethers::utils::keccak256;
|
||||
use frost::{
|
||||
algorithm::Schnorr,
|
||||
curve::Secp256k1,
|
||||
tests::{algorithm_machines, key_gen, sign},
|
||||
};
|
||||
use k256::elliptic_curve::bigint::ArrayEncoding;
|
||||
use k256::{Scalar, U256};
|
||||
use rand_core::OsRng;
|
||||
|
||||
let anvil = Anvil::new().spawn();
|
||||
let wallet: LocalWallet = anvil.keys()[0].clone().into();
|
||||
let provider =
|
||||
Provider::<Http>::try_from(anvil.endpoint()).unwrap().interval(Duration::from_millis(10u64));
|
||||
let chain_id = provider.get_chainid().await.unwrap();
|
||||
let client = Arc::new(SignerMiddleware::new(provider, wallet));
|
||||
let (chain_id, _anvil, contract) = deploy_test_contract().await;
|
||||
let chain_id = U256::from(chain_id);
|
||||
|
||||
let keys = key_gen::<_, Secp256k1>(&mut OsRng);
|
||||
let group_key = keys[&1].group_key();
|
||||
|
||||
const MESSAGE: &'static [u8] = b"Hello, World!";
|
||||
let hashed_message = keccak256(MESSAGE);
|
||||
let chain_id = U256::from(Scalar::from(chain_id.as_u32()));
|
||||
|
||||
let full_message = &[chain_id.to_be_byte_array().as_slice(), &hashed_message].concat();
|
||||
|
||||
let sig = sign(
|
||||
&mut OsRng,
|
||||
algorithm_machines(&mut OsRng, Schnorr::<Secp256k1, crypto::EthereumHram>::new(), &keys),
|
||||
algorithm_machines(&mut OsRng, Algo::<Secp256k1, crypto::EthereumHram>::new(), &keys),
|
||||
full_message,
|
||||
);
|
||||
let mut processed_sig =
|
||||
crypto::process_signature_for_contract(hashed_message, &sig.R, sig.s, &group_key, chain_id);
|
||||
|
||||
let contract = deploy_schnorr_verifier_contract(client).await.unwrap();
|
||||
call_verify(&contract, &processed_sig).await.unwrap();
|
||||
|
||||
// test invalid signature fails
|
||||
processed_sig.message[0] = 0;
|
||||
let res = call_verify(&contract, &processed_sig).await;
|
||||
assert!(res.is_err());
|
||||
assert!(call_verify(&contract, &processed_sig).await.is_err());
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ fn test_ecrecover_hack() {
|
|||
|
||||
const MESSAGE: &'static [u8] = b"Hello, World!";
|
||||
let hashed_message = keccak256(MESSAGE);
|
||||
let chain_id = U256::from(Scalar::ONE);
|
||||
let chain_id = U256::ONE;
|
||||
|
||||
let full_message = &[chain_id.to_be_byte_array().as_slice(), &hashed_message].concat();
|
||||
|
||||
|
|
Loading…
Reference in a new issue