1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use super::super::{
serializable_to_json, try_response_from_json, NashProtocol, ResponseOrError, State,
};
use super::response;
use crate::errors::{ProtocolError, Result};
use crate::graphql::dh_fill_pool;
use crate::types::Blockchain;
#[cfg(feature = "secp256k1")]
use nash_mpc::curves::secp256_k1::{Secp256k1Point, Secp256k1Scalar};
#[cfg(feature = "k256")]
use nash_mpc::curves::secp256_k1_rust::{Secp256k1Point, Secp256k1Scalar};
use nash_mpc::curves::secp256_r1::{Secp256r1Point, Secp256r1Scalar};
use async_trait::async_trait;
use futures::lock::Mutex;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub enum DhFillPoolRequest {
Bitcoin(K1FillPool),
Ethereum(K1FillPool),
NEO(R1FillPool),
}
impl DhFillPoolRequest {
pub fn new(chain: Blockchain) -> Result<Self> {
match chain {
Blockchain::Ethereum => Ok(Self::Ethereum(K1FillPool::new()?)),
Blockchain::Bitcoin => Ok(Self::Bitcoin(K1FillPool::new()?)),
Blockchain::NEO => Ok(Self::NEO(R1FillPool::new()?)),
}
}
pub fn blockchain(&self) -> Blockchain {
match self {
Self::Bitcoin(_) => Blockchain::Bitcoin,
Self::Ethereum(_) => Blockchain::Ethereum,
Self::NEO(_) => Blockchain::NEO,
}
}
}
#[derive(Clone, Debug)]
pub struct K1FillPool {
pub publics: Vec<Secp256k1Point>,
pub secrets: Vec<Secp256k1Scalar>,
}
impl K1FillPool {
pub fn new() -> Result<Self> {
let (secrets, publics) = nash_mpc::common::dh_init_secp256k1(100)
.map_err(|_| ProtocolError("Could not initialize k1 values"))?;
Ok(Self { publics, secrets })
}
}
#[derive(Clone, Debug)]
pub struct R1FillPool {
pub publics: Vec<Secp256r1Point>,
pub secrets: Vec<Secp256r1Scalar>,
}
impl R1FillPool {
pub fn new() -> Result<Self> {
let (secrets, publics) = nash_mpc::common::dh_init_secp256r1(100)
.map_err(|_| ProtocolError("Could not initialize r1 values"))?;
Ok(Self { publics, secrets })
}
}
#[derive(Clone, Debug)]
pub struct DhFillPoolResponse {
pub server_publics: Vec<String>,
}
pub enum ServerPublics {
Bitcoin(Vec<Secp256k1Point>),
Ethereum(Vec<Secp256k1Point>),
NEO(Vec<Secp256r1Point>),
}
#[async_trait]
impl NashProtocol for DhFillPoolRequest {
type Response = DhFillPoolResponse;
async fn graphql(&self, _state: Arc<Mutex<State>>) -> Result<serde_json::Value> {
let query = self.make_query();
serializable_to_json(&query)
}
async fn response_from_json(
&self,
response: serde_json::Value,
_state: Arc<Mutex<State>>
) -> Result<ResponseOrError<Self::Response>> {
try_response_from_json::<DhFillPoolResponse, dh_fill_pool::ResponseData>(response)
}
async fn process_response(
&self,
response: &Self::Response,
state: Arc<Mutex<State>>,
) -> Result<()> {
let server_publics = ServerPublics::from_hexstrings(self.blockchain(), response)?;
response::fill_pool(self, server_publics, state.clone()).await?;
let mut state = state.lock().await;
state.signer()?.fill_r_vals(self.blockchain(), 100);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::{Blockchain, DhFillPoolRequest, NashProtocol};
use crate::protocol::State;
use futures::executor;
use futures::lock::Mutex;
use std::sync::Arc;
#[test]
fn serialize_dh_fill_pool() {
let state = Arc::new(Mutex::new(
State::new(Some("../nash-native-client/test_data/keyfile.json")).unwrap(),
));
let async_block = async {
println!(
"{:?}",
DhFillPoolRequest::new(Blockchain::Ethereum)
.unwrap()
.graphql(state)
.await
.unwrap()
);
};
executor::block_on(async_block);
}
}