Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unify crates with private #92

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ jsonwebtoken = "8"
hex = "0.4"
secp256k1 = {version = "0.21.0", features = ["global-context"]}
rand = "0.8"
two-party-ecdsa = { git = "https://github.com/ZenGo-X/two-party-ecdsa.git", branch="compatibility_gotham_engine" }
gotham-engine = { git = "https://github.com/ZenGo-X/gotham-engine.git" }
two-party-ecdsa = { git = "https://github.com/ZenGo-X/two-party-ecdsa.git", branch="serde-type-name" }
gotham-engine = { git = "https://github.com/ZenGo-X/gotham-engine.git", branch="serde-type-name" }
4 changes: 3 additions & 1 deletion gotham-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ impl Client for reqwest::Client {
bearer_token: Option<String>,
body: T,
) -> Option<V> {
let mut b = self.post(&format!("{}/{}", endpoint, uri));
let mut b = self
.post(&format!("{}/{}", endpoint, uri))
.header("x-customer-id", "xxx");
if let Some(token) = bearer_token {
b = b.bearer_auth(token);
}
Expand Down
13 changes: 10 additions & 3 deletions gotham-server/src/public_gotham.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rocket::async_trait;
use std::collections::HashMap;
use std::string::String;

use two_party_ecdsa::party_one::Value;
use two_party_ecdsa::party_one::{v, Value};

use gotham_engine::keygen::KeyGen;
use gotham_engine::sign::Sign;
Expand Down Expand Up @@ -73,7 +73,7 @@ impl Db for PublicGotham {
table_name: &dyn MPCStruct,
) -> Result<Option<Box<dyn Value>>, DatabaseError> {
let identifier = idify(key.clone().customerId, key.clone().id, table_name);
// debug!("Getting from db ({})", identifier);
println!("Getting from db ({})", identifier);
let result = self.rocksdb_client.get(identifier.clone()).unwrap();
let vec_option: Option<Vec<u8>> = result.map(|v| v.to_vec());
match vec_option {
Expand All @@ -86,7 +86,14 @@ impl Db for PublicGotham {
.unwrap();
Ok(Option::from(final_val))
}
None => Ok(None),
None => {
println! {"ok none"}
let value = v {
value: "false".parse().unwrap(),
};
let final_val: Box<dyn Value> = Box::new(value);
Ok(Option::from(final_val))
}
}
}
/// the granted function implements the logic of tx authorization. If no tx authorization is needed the function returns always true
Expand Down
13 changes: 13 additions & 0 deletions gotham-server/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ mod tests {
use two_party_ecdsa::kms::chain_code::two_party::party2::ChainCode2;
use two_party_ecdsa::kms::ecdsa;
use gotham_engine::types::SignSecondMsgRequest;
use rocket::http::Header;
use two_party_ecdsa::party_one::Converter;

fn key_gen(client: &Client) -> (String, MasterKey2) {
let x_customer_header = Header::new("x-customer-id", "xxx");

let response = client
.post("/ecdsa/keygen/first")
.header(ContentType::JSON)
.header(x_customer_header.clone())
.dispatch();
assert_eq!(response.status(), Status::Ok);
let res_body = response.into_string().unwrap();
Expand All @@ -36,6 +40,7 @@ mod tests {
.post(format!("/ecdsa/keygen/{}/second", id))
.body(body)
.header(ContentType::JSON)
.header(x_customer_header.clone())
.dispatch();
assert_eq!(response.status(), Status::Ok);

Expand All @@ -61,6 +66,7 @@ mod tests {
.post(format!("/ecdsa/keygen/{}/third", id))
.body(body)
.header(ContentType::JSON)
.header(x_customer_header.clone())
.dispatch();
assert_eq!(response.status(), Status::Ok);

Expand All @@ -84,6 +90,7 @@ mod tests {
.post(format!("/ecdsa/keygen/{}/fourth", id))
.body(body)
.header(ContentType::JSON)
.header(x_customer_header.clone())
.dispatch();
assert_eq!(response.status(), Status::Ok);

Expand All @@ -103,6 +110,7 @@ mod tests {
let response = client
.post(format!("/ecdsa/keygen/{}/chaincode/first", id))
.header(ContentType::JSON)
.header(x_customer_header.clone())
.dispatch();
assert_eq!(response.status(), Status::Ok);

Expand All @@ -121,6 +129,7 @@ mod tests {
.post(format!("/ecdsa/keygen/{}/chaincode/second", id))
.body(body)
.header(ContentType::JSON)
.header(x_customer_header.clone())
.dispatch();
assert_eq!(response.status(), Status::Ok);

Expand Down Expand Up @@ -164,6 +173,8 @@ mod tests {
master_key_2: MasterKey2,
message: BigInt,
) -> party_one::SignatureRecid {
let x_customer_header = Header::new("x-customer-id", "xxx");

let (eph_key_gen_first_message_party_two, eph_comm_witness, eph_ec_key_pair_party2) =
MasterKey2::sign_first_message();

Expand All @@ -175,6 +186,7 @@ mod tests {
.post(format!("/ecdsa/sign/{}/first", id))
.body(body)
.header(ContentType::JSON)
.header(x_customer_header.clone())
.dispatch();
assert_eq!(response.status(), Status::Ok);

Expand Down Expand Up @@ -209,6 +221,7 @@ mod tests {
.post(format!("/ecdsa/sign/{}/second", id))
.body(body)
.header(ContentType::JSON)
.header(x_customer_header.clone())
.dispatch();
assert_eq!(response.status(), Status::Ok);

Expand Down
141 changes: 4 additions & 137 deletions integration-tests/tests/ecdsa.rs
Original file line number Diff line number Diff line change
@@ -1,109 +1,16 @@
use client_lib::{ecdsa, ClientShim};
use rand::rngs::mock::StepRng;
use rand::Rng;
use rocket::http::Header;
use rocket::serde::{DeserializeOwned, Serialize};
use rocket::Rocket;
use rocket::{Config, Ignite, Rocket};
use secp256k1::{ecdsa::Signature, Message, SECP256K1};
use server_lib::server;
use std::collections::HashMap;
use two_party_ecdsa::curv::arithmetic::big_gmp::BigInt;
use two_party_ecdsa::curv::arithmetic::traits::Converter;
use two_party_ecdsa::curv::elliptic::curves::traits::ECPoint;

// #[rocket::async_test]
// async fn test_ecdsa_network() {
// let mut rng = StepRng::new(0, 1);
// rocket::tokio::spawn(spawn_server(8000, "ecdsa"));
//
// let client_shim = ClientShim::new("http://localhost:8000".to_string(), None);
//
// let two_seconds = time::Duration::from_millis(2000);
// thread::sleep(two_seconds);
//
// let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);
//
// for y in 0..50i32 {
// let x_pos = BigInt::from(y * 2 + 1);
// let y_pos = BigInt::from(y);
//
// let child_master_key = ps.master_key.get_child(vec![x_pos.clone(), y_pos.clone()]);
// let pk = child_master_key.public.q.get_element();
//
// let mut msg_buf = [0u8; 32];
// rng.fill(&mut msg_buf);
// let msg: BigInt = BigInt::from(&msg_buf[..]);
//
// let signature = ecdsa::sign(
// &client_shim,
// msg.clone(),
// &child_master_key,
// x_pos,
// y_pos,
// &ps.id,
// )
// .expect("ECDSA signature failed");
//
// let r = BigInt::to_vec(&signature.r);
// let s = BigInt::to_vec(&signature.s);
// let msg = Message::from_slice(&msg_buf).unwrap();
//
// let mut sig = [0u8; 64];
// sig[32 - r.len()..32].copy_from_slice(&r);
// sig[32 + 32 - s.len()..].copy_from_slice(&s);
//
// let sig = Signature::from_compact(&sig).unwrap();
//
// SECP256K1.verify_ecdsa(&msg, &sig, &pk).unwrap();
// }
// }

// async fn spawn_server(port: u32, db_name: &str) -> Rocket<Ignite> {
// let settings = HashMap::<String, String>::from([
// ("db".to_string(), "local".to_string()),
// ("db_name".to_string(), db_name.to_string()),
// ]);
// let rocket = server::get_server(settings);
// let figment = rocket.figment().clone().merge((Config::PORT, port));
// rocket.configure(figment).launch().await.unwrap()
// }

// #[test]
// fn test_ecdsa_keygen() {
// let settings = HashMap::<String, String>::from([
// ("db".into(), "local".into()),
// ("db_name".into(), "testEcdsaKeygen".into()),
// ]);
// let rocket = server::get_server(settings);
// let client = RocketClient::new(rocket);
//
// let client_shim =
// ClientShim::new_with_client("http://localhost:8009".to_string(), None, client);
// for _ in 0..10 {
// let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);
// let _ = ps.master_key.public.q.get_element();
// }
// }

// #[test]
// fn test_ecdsa_key_derivation() {
// let settings = HashMap::<String, String>::from([
// ("db".into(), "local".into()),
// ("db_name".into(), "testEcdsaDerivation".into()),
// ]);
// let rocket = server::get_server(settings);
// let client = RocketClient::new(rocket);
//
// let client_shim =
// ClientShim::new_with_client("http://localhost:8009".to_string(), None, client);
// let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);
// for y in 0..1 {
// let x_pos = BigInt::from(y * 2 + 1);
// let y_pos = BigInt::from(y);
// let child_master_key = ps.master_key.get_child(vec![x_pos.clone(), y_pos.clone()]);
// let _ = child_master_key.public.q.get_element();
// }
// }

#[test]
// #[rocket::async_test]
fn integration_test_ecdsa_key_signing() {
Expand Down Expand Up @@ -150,48 +57,6 @@ fn integration_test_ecdsa_key_signing() {
}
}

// #[test]
// fn integration_test_ecdsa_long() {
// let mut rng = StepRng::new(0, 1);
// let settings = HashMap::<String, String>::from([
// ("db_name".into(), "testEcdsaLong".into()),
// ]);
// let rocket = server::get_server(settings);
// let client = RocketClient::new(rocket);
//
// let client_shim =
// ClientShim::new_with_client("http://localhost:8009".to_string(), None, client);
//
// let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);
//
// for y in 0..1 {
// let x_pos = BigInt::from(y * 2 + 1);
// let y_pos = BigInt::from(y);
//
// let child_master_key = ps.master_key.get_child(vec![x_pos.clone(), y_pos.clone()]);
// let pk = child_master_key.public.q.get_element();
//
// let mut msg_buf = [0u8; 32];
// rng.fill(&mut msg_buf);
// let msg: BigInt = BigInt::from(&msg_buf[..]);
//
// let signature = ecdsa::sign(&client_shim, msg, &child_master_key, x_pos, y_pos, &ps.id)
// .expect("ECDSA signature failed");
//
// let r = BigInt::to_vec(&signature.r);
// let s = BigInt::to_vec(&signature.s);
// let msg = Message::from_slice(&msg_buf).unwrap();
//
// let mut sig = [0u8; 64];
// sig[32 - r.len()..32].copy_from_slice(&r);
// sig[32 + 32 - s.len()..].copy_from_slice(&s);
//
// let sig = Signature::from_compact(&sig).unwrap();
//
// SECP256K1.verify_ecdsa(&msg, &sig, &pk).unwrap();
// }
// }

struct RocketClient(pub rocket::local::blocking::Client);

impl RocketClient {
Expand All @@ -208,8 +73,10 @@ impl client_lib::Client for RocketClient {
_: Option<String>,
body: T,
) -> Option<V> {
let x_customer_header = Header::new("x-customer-id", "xxx");
self.0
.post(["/", uri].concat())
.header(x_customer_header.clone())
.json(&body)
.dispatch()
.into_string()
Expand Down
Loading