1
- use common:: tls:: Config ;
2
1
use stage_service:: stage_service_client:: StageServiceClient ;
3
2
use stage_service:: { GenerateProofRequest , GetStatusRequest } ;
4
3
4
+ use std:: path:: Path ;
5
5
use std:: time:: Instant ;
6
6
use tonic:: transport:: Endpoint ;
7
+ use tonic:: transport:: { Certificate , Identity } ;
7
8
use tonic:: transport:: { Channel , ClientTlsConfig } ;
8
9
9
10
use crate :: prover:: { ClientCfg , Prover , ProverInput , ProverResult } ;
@@ -13,6 +14,12 @@ use tokio::time::Duration;
13
14
14
15
use async_trait:: async_trait;
15
16
17
+ #[ derive( Clone ) ]
18
+ pub struct Config {
19
+ pub ca_cert : Option < Certificate > ,
20
+ pub identity : Option < Identity > ,
21
+ }
22
+
16
23
pub mod stage_service {
17
24
tonic:: include_proto!( "stage.v1" ) ;
18
25
}
@@ -32,7 +39,9 @@ impl NetworkProver {
32
39
let ssl_config = if ca_cert_path. is_empty ( ) {
33
40
None
34
41
} else {
35
- Some ( Config :: new ( ca_cert_path, cert_path, key_path) . await ?)
42
+ let ( ca_cert, identity) =
43
+ get_cert_and_identity ( ca_cert_path, cert_path, key_path) . await ?;
44
+ Some ( Config { ca_cert, identity } )
36
45
} ;
37
46
let endpoint_para = client_config. endpoint . to_owned ( ) . expect ( "ENDPOINT must be set" ) ;
38
47
let endpoint = match ssl_config {
@@ -217,3 +226,35 @@ impl Prover for NetworkProver {
217
226
self . wait_proof ( & proof_id, timeout) . await
218
227
}
219
228
}
229
+
230
+ async fn get_cert_and_identity (
231
+ ca_cert_path : String ,
232
+ cert_path : String ,
233
+ key_path : String ,
234
+ ) -> anyhow:: Result < ( Option < Certificate > , Option < Identity > ) > {
235
+ let ca_cert_path = Path :: new ( & ca_cert_path) ;
236
+ let cert_path = Path :: new ( & cert_path) ;
237
+ let key_path = Path :: new ( & key_path) ;
238
+ // if !ca_cert_path.is_file() || !cert_path.is_file() || !key_path.is_file() {
239
+ // bail!("both ca_cert_path, cert_path and key_path should be valid file")
240
+ // }
241
+ let mut ca: Option < Certificate > = None ;
242
+ let mut identity: Option < Identity > = None ;
243
+ if ca_cert_path. is_file ( ) {
244
+ let ca_cert = tokio:: fs:: read ( ca_cert_path)
245
+ . await
246
+ . unwrap_or_else ( |err| panic ! ( "Failed to read {:?}, err: {:?}" , ca_cert_path, err) ) ;
247
+ ca = Some ( Certificate :: from_pem ( ca_cert) ) ;
248
+ }
249
+
250
+ if cert_path. is_file ( ) && key_path. is_file ( ) {
251
+ let cert = tokio:: fs:: read ( cert_path)
252
+ . await
253
+ . unwrap_or_else ( |err| panic ! ( "Failed to read {:?}, err: {:?}" , cert_path, err) ) ;
254
+ let key = tokio:: fs:: read ( key_path)
255
+ . await
256
+ . unwrap_or_else ( |err| panic ! ( "Failed to read {:?}, err: {:?}" , key_path, err) ) ;
257
+ identity = Some ( Identity :: from_pem ( cert, key) ) ;
258
+ }
259
+ Ok ( ( ca, identity) )
260
+ }
0 commit comments