1- use common:: tls:: Config ;
21use stage_service:: stage_service_client:: StageServiceClient ;
32use stage_service:: { GenerateProofRequest , GetStatusRequest } ;
43
4+ use std:: path:: Path ;
55use std:: time:: Instant ;
66use tonic:: transport:: Endpoint ;
7+ use tonic:: transport:: { Certificate , Identity } ;
78use tonic:: transport:: { Channel , ClientTlsConfig } ;
89
910use crate :: prover:: { ClientCfg , Prover , ProverInput , ProverResult } ;
@@ -13,6 +14,12 @@ use tokio::time::Duration;
1314
1415use async_trait:: async_trait;
1516
17+ #[ derive( Clone ) ]
18+ pub struct Config {
19+ pub ca_cert : Option < Certificate > ,
20+ pub identity : Option < Identity > ,
21+ }
22+
1623pub mod stage_service {
1724 tonic:: include_proto!( "stage.v1" ) ;
1825}
@@ -32,7 +39,9 @@ impl NetworkProver {
3239 let ssl_config = if ca_cert_path. is_empty ( ) {
3340 None
3441 } 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 } )
3645 } ;
3746 let endpoint_para = client_config. endpoint . to_owned ( ) . expect ( "ENDPOINT must be set" ) ;
3847 let endpoint = match ssl_config {
@@ -217,3 +226,35 @@ impl Prover for NetworkProver {
217226 self . wait_proof ( & proof_id, timeout) . await
218227 }
219228}
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