@@ -3,6 +3,7 @@ use crate::{
33 batch:: await_batch_verification,
44 messaging:: { receive, send_messages, ResponseStream } ,
55 protocol:: check_protocol_version,
6+ serialization:: cbor_serialize,
67 } ,
78 core:: {
89 constants:: {
@@ -34,13 +35,18 @@ use std::{str::FromStr, sync::Arc};
3435use tokio:: { net:: TcpStream , sync:: Mutex } ;
3536use tokio_tungstenite:: { connect_async, tungstenite:: Message , MaybeTlsStream , WebSocketStream } ;
3637
37- use log:: debug;
38+ use log:: { debug, info } ;
3839
3940use futures_util:: {
4041 stream:: { SplitSink , SplitStream } ,
4142 StreamExt , TryStreamExt ,
4243} ;
4344
45+ use std:: fs:: File ;
46+ use std:: io:: Write ;
47+ use std:: path:: PathBuf ;
48+
49+ use serde_json:: json;
4450/// Submits multiple proofs to the batcher to be verified in Aligned and waits for the verification on-chain.
4551/// # Arguments
4652/// * `batcher_url` - The url of the batcher to which the proof will be submitted.
@@ -655,6 +661,91 @@ pub async fn get_balance_in_aligned(
655661 }
656662}
657663
664+ /// Saves AlignedVerificationData in a file.
665+ /// # Arguments
666+ /// * `batch_inclusion_data_directory_path` - The path of the directory where the data will be saved.
667+ /// * `aligned_verification_data` - The aligned verification data to be saved.
668+ /// # Returns
669+ /// * Ok if the data is saved successfully.
670+ /// # Errors
671+ /// * `FileError` if there is an error writing the data to the file.
672+ pub fn save_response (
673+ batch_inclusion_data_directory_path : PathBuf ,
674+ aligned_verification_data : & AlignedVerificationData ,
675+ ) -> Result < ( ) , errors:: FileError > {
676+ save_response_cbor (
677+ batch_inclusion_data_directory_path. clone ( ) ,
678+ & aligned_verification_data. clone ( ) ,
679+ ) ?;
680+ save_response_json (
681+ batch_inclusion_data_directory_path,
682+ aligned_verification_data,
683+ )
684+ }
685+ fn save_response_cbor (
686+ batch_inclusion_data_directory_path : PathBuf ,
687+ aligned_verification_data : & AlignedVerificationData ,
688+ ) -> Result < ( ) , errors:: FileError > {
689+ let batch_merkle_root = & hex:: encode ( aligned_verification_data. batch_merkle_root ) [ ..8 ] ;
690+ let batch_inclusion_data_file_name = batch_merkle_root. to_owned ( )
691+ + "_"
692+ + & aligned_verification_data. index_in_batch . to_string ( )
693+ + ".cbor" ;
694+
695+ let batch_inclusion_data_path =
696+ batch_inclusion_data_directory_path. join ( batch_inclusion_data_file_name) ;
697+
698+ let data = cbor_serialize ( & aligned_verification_data) ?;
699+
700+ let mut file = File :: create ( & batch_inclusion_data_path) ?;
701+ file. write_all ( data. as_slice ( ) ) ?;
702+ info ! (
703+ "Batch inclusion data written into {}" ,
704+ batch_inclusion_data_path. display( )
705+ ) ;
706+
707+ Ok ( ( ) )
708+ }
709+ fn save_response_json (
710+ batch_inclusion_data_directory_path : PathBuf ,
711+ aligned_verification_data : & AlignedVerificationData ,
712+ ) -> Result < ( ) , errors:: FileError > {
713+ let batch_merkle_root = & hex:: encode ( aligned_verification_data. batch_merkle_root ) [ ..8 ] ;
714+ let batch_inclusion_data_file_name = batch_merkle_root. to_owned ( )
715+ + "_"
716+ + & aligned_verification_data. index_in_batch . to_string ( )
717+ + ".json" ;
718+
719+ let batch_inclusion_data_path =
720+ batch_inclusion_data_directory_path. join ( batch_inclusion_data_file_name) ;
721+
722+ let merkle_proof = aligned_verification_data
723+ . batch_inclusion_proof
724+ . merkle_path
725+ . iter ( )
726+ . map ( hex:: encode)
727+ . collect :: < Vec < String > > ( )
728+ . join ( "" ) ;
729+ let data = json ! ( {
730+ "proof_commitment" : hex:: encode( aligned_verification_data. verification_data_commitment. proof_commitment) ,
731+ "pub_input_commitment" : hex:: encode( aligned_verification_data. verification_data_commitment. pub_input_commitment) ,
732+ "program_id_commitment" : hex:: encode( aligned_verification_data. verification_data_commitment. proving_system_aux_data_commitment) ,
733+ "proof_generator_addr" : hex:: encode( aligned_verification_data. verification_data_commitment. proof_generator_addr) ,
734+ "batch_merkle_root" : hex:: encode( aligned_verification_data. batch_merkle_root) ,
735+ "verification_data_batch_index" : aligned_verification_data. index_in_batch,
736+ "merkle_proof" : merkle_proof,
737+ } ) ;
738+ let mut file = File :: create ( & batch_inclusion_data_path) ?;
739+ file. write_all ( serde_json:: to_string_pretty ( & data) . unwrap ( ) . as_bytes ( ) ) ?;
740+
741+ info ! (
742+ "Batch inclusion data written into {}" ,
743+ batch_inclusion_data_path. display( )
744+ ) ;
745+
746+ Ok ( ( ) )
747+ }
748+
658749#[ cfg( test) ]
659750mod test {
660751 //Public constants for convenience
0 commit comments