-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.rs
More file actions
201 lines (185 loc) · 7.37 KB
/
main.rs
File metadata and controls
201 lines (185 loc) · 7.37 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#![allow(non_snake_case)]
#![feature(proc_macro_hygiene, decl_macro)]
extern crate clap;
extern crate curv;
extern crate hex;
extern crate multi_party_ecdsa;
extern crate paillier;
extern crate reqwest;
extern crate serde_json;
use std::fs;
use clap::{App, AppSettings, Arg, SubCommand};
use curv::cryptographic_primitives::secret_sharing::feldman_vss::VerifiableSS;
use curv::elliptic::curves::traits::*;
use curv::{
BigInt,
elliptic::curves::secp256_k1::{GE},
arithmetic::Converter
};
use curv::elliptic::curves::secp256_k1::FE;
use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2018::party_i::*;
use paillier::*;
use serde_json::json;
use common::{hd_keys, keygen, manager, signer, SignParams};
mod common;
mod test;
fn main() {
let matches = App::new("TSS CLI Utility")
.version("0.1.0")
.author("Kaspars Sprogis <darklow@gmail.com>")
// .about("")
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommands(vec![
SubCommand::with_name("manager").about("Run state manager"),
SubCommand::with_name("keygen").about("Run keygen")
.arg(Arg::with_name("keysfile")
.required(true)
.index(1)
.takes_value(true)
.help("Target keys file"))
.arg(Arg::with_name("params")
.index(2)
.required(true)
.takes_value(true)
.help("Threshold params: threshold/parties (t+1/n). E.g. 1/3 for 2 of 3 schema."))
.arg(Arg::with_name("manager_addr")
.short("a")
.long("addr")
.takes_value(true)
.help("URL to manager. E.g. http://127.0.0.2:8002")),
SubCommand::with_name("pubkey").about("Get X,Y of a pub key")
.arg(Arg::with_name("keysfile")
.required(true)
.index(1)
.takes_value(true)
.help("Keys file"))
.arg(Arg::with_name("path")
.short("p")
.long("path")
.takes_value(true)
.help("Derivation path (Optional)")),
SubCommand::with_name("sign").about("Run signer")
.arg(Arg::with_name("keysfile")
.required(true)
.index(1)
.takes_value(true)
.help("Keys file"))
.arg(Arg::with_name("params")
.index(2)
.required(true)
.takes_value(true)
.help("Threshold params: threshold/parties (t+1/n). E.g. 1/3 for 2 of 3 schema."))
.arg(Arg::with_name("message")
.index(3)
.required(true)
.takes_value(true)
.help("Message to sign in hex format"))
.arg(Arg::with_name("path")
.short("p")
.long("path")
.takes_value(true)
.help("Derivation path"))
.arg(Arg::with_name("manager_addr")
.short("a")
.long("addr")
.takes_value(true)
.help("URL to manager"))
])
.get_matches();
match matches.subcommand() {
("pubkey", Some(sub_matches)) | ("sign", Some(sub_matches)) => {
let keysfile_path = sub_matches.value_of("keysfile").unwrap_or("");
// Read data from keys file
let data = fs::read_to_string(keysfile_path).expect(
format!("Unable to load keys file at location: {}", keysfile_path).as_str(),
);
let (party_keys, shared_keys, party_id, mut vss_scheme_vec, paillier_key_vector, y_sum): (
Keys,
SharedKeys,
u16,
Vec<VerifiableSS<GE>>,
Vec<EncryptionKey>,
GE,
) = serde_json::from_str(&data).unwrap();
// Get root pub key or HD pub key at specified path
let path = sub_matches.value_of("path").unwrap_or("");
let (f_l_new, y_sum) = match path.is_empty() {
true => (ECScalar::zero(), y_sum),
false => call_hd_key(path, y_sum)
};
// Return pub key as x,y
if let Some(_sub_matches) = matches.subcommand_matches("pubkey") {
let ret_dict = json!({
"x": &y_sum.x_coor(),
"y": &y_sum.y_coor(),
"path": path,
});
println!("{}", ret_dict.to_string());
} else if let Some(sub_matches) = matches.subcommand_matches("sign") {
// Parse message to sign
let message_str = sub_matches.value_of("message").unwrap_or("").to_string();
let message = match hex::decode(message_str.clone()) {
Ok(x) => x,
Err(_e) => message_str.as_bytes().to_vec(),
};
let message = &message[..];
let manager_addr = sub_matches
.value_of("manager_addr")
.unwrap_or("http://127.0.0.1:8001")
.to_string();
// Parse threshold params
let params: Vec<&str> = sub_matches
.value_of("params")
.unwrap_or("")
.split("/")
.collect();
// println!("sign me {:?} / {:?} / {:?}", manager_addr, message, params);
let params = SignParams {
threshold: params[0].to_string(),
parties: params[1].to_string(),
x: y_sum.x_coor().unwrap().to_str_radix(10),
y: y_sum.x_coor().unwrap().to_str_radix(10),
message: message_str,
};
signer::sign(
manager_addr,
party_keys,
shared_keys,
party_id,
&mut vss_scheme_vec,
paillier_key_vector,
&y_sum,
¶ms,
&message,
&f_l_new,
!path.is_empty(),
)
}
}
("manager", Some(_matches)) => {
manager::run_manager();
}
("keygen", Some(sub_matches)) => {
let addr = sub_matches
.value_of("manager_addr")
.unwrap_or("http://127.0.0.1:8001")
.to_string();
let keysfile_path = sub_matches.value_of("keysfile").unwrap_or("").to_string();
let params: Vec<&str> = sub_matches
.value_of("params")
.unwrap_or("")
.split("/")
.collect();
keygen::run_keygen(&addr, &keysfile_path, ¶ms);
}
_ => {}
}
}
fn call_hd_key(path: &str, public_key: GE) -> (FE, GE) {
let path_vector: Vec<BigInt> = path
.split('/')
.map(|s| BigInt::from_str_radix(s.trim(), 10).unwrap())
.collect();
let (public_key_child, f_l_new) = hd_keys::get_hd_key(&public_key, path_vector.clone());
(f_l_new, public_key_child.clone())
}