@@ -3,11 +3,13 @@ use std::{
33 time:: { SystemTime , UNIX_EPOCH } ,
44} ;
55
6+ use actix_multipart:: form:: MultipartForm ;
67use actix_web:: {
78 web:: { self , Data } ,
89 App , HttpRequest , HttpResponse , HttpServer , Responder ,
910} ;
1011use aligned_sdk:: aggregation_layer:: AggregationModeProvingSystem ;
12+ use sp1_sdk:: { SP1ProofWithPublicValues , SP1VerifyingKey } ;
1113use sqlx:: types:: BigDecimal ;
1214
1315use super :: {
@@ -18,9 +20,8 @@ use super::{
1820use crate :: {
1921 config:: Config ,
2022 db:: Db ,
21- server:: types:: {
22- SubmitProofRequest , SubmitProofRequestMessageRisc0 , SubmitProofRequestMessageSP1 ,
23- } ,
23+ server:: types:: { SubmitProofRequestRisc0 , SubmitProofRequestSP1 } ,
24+ verifiers:: { verify_sp1_proof, VerificationError } ,
2425} ;
2526
2627#[ derive( Clone , Debug ) ]
@@ -64,7 +65,6 @@ impl BatcherServer {
6465 } ;
6566
6667 // TODO: validate valid ethereum address
67-
6868 let Some ( state) = req. app_data :: < Data < BatcherServer > > ( ) else {
6969 return HttpResponse :: InternalServerError ( )
7070 . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ;
@@ -84,11 +84,8 @@ impl BatcherServer {
8484
8585 async fn post_proof_sp1 (
8686 req : HttpRequest ,
87- body : web :: Json < SubmitProofRequest < SubmitProofRequestMessageSP1 > > ,
87+ MultipartForm ( data ) : MultipartForm < SubmitProofRequestSP1 > ,
8888 ) -> impl Responder {
89- let data = body. into_inner ( ) ;
90-
91- // TODO: validate signature
9289 let recovered_address = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" . to_lowercase ( ) ;
9390
9491 let Some ( state) = req. app_data :: < Data < BatcherServer > > ( ) else {
@@ -102,7 +99,7 @@ impl BatcherServer {
10299 . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ;
103100 } ;
104101
105- if data. nonce != ( count as u64 ) {
102+ if data. nonce . 0 != ( count as u64 ) {
106103 return HttpResponse :: BadRequest ( ) . json ( AppResponse :: new_unsucessfull (
107104 & format ! ( "Invalid nonce, expected nonce = {count}" ) ,
108105 400 ,
@@ -140,15 +137,42 @@ impl BatcherServer {
140137 ) ) ;
141138 }
142139
143- // TODO: decode proof and validate it
140+ let Ok ( proof_content) = tokio:: fs:: read ( data. proof . file . path ( ) ) . await else {
141+ return HttpResponse :: InternalServerError ( )
142+ . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ;
143+ } ;
144+
145+ let Ok ( proof) = bincode:: deserialize :: < SP1ProofWithPublicValues > ( & proof_content) else {
146+ return HttpResponse :: BadRequest ( )
147+ . json ( AppResponse :: new_unsucessfull ( "Invalid SP1 proof" , 400 ) ) ;
148+ } ;
149+
150+ let Ok ( vk_content) = tokio:: fs:: read ( data. program_vk . file . path ( ) ) . await else {
151+ return HttpResponse :: InternalServerError ( )
152+ . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ;
153+ } ;
154+
155+ let Ok ( vk) = bincode:: deserialize :: < SP1VerifyingKey > ( & vk_content) else {
156+ return HttpResponse :: BadRequest ( )
157+ . json ( AppResponse :: new_unsucessfull ( "Invalid vk" , 400 ) ) ;
158+ } ;
159+
160+ if let Err ( e) = verify_sp1_proof ( & proof, & vk) {
161+ let message = match e {
162+ VerificationError :: InvalidProof => "Proof verification failed" ,
163+ VerificationError :: UnsupportedProof => "Unsupported proof" ,
164+ } ;
165+
166+ return HttpResponse :: BadRequest ( ) . json ( AppResponse :: new_unsucessfull ( message, 400 ) ) ;
167+ } ;
144168
145169 match state
146170 . db
147171 . insert_task (
148172 & recovered_address,
149173 AggregationModeProvingSystem :: SP1 . as_u16 ( ) as i32 ,
150- & data . message . proof ,
151- & data . message . program_vk_commitment ,
174+ & proof_content ,
175+ & vk_content ,
152176 None ,
153177 )
154178 . await
@@ -164,7 +188,7 @@ impl BatcherServer {
164188 /// TODO: complete for risc0 (see `post_proof_sp1`)
165189 async fn post_proof_risc0 (
166190 _req : HttpRequest ,
167- _body : web :: Json < SubmitProofRequest < SubmitProofRequestMessageRisc0 > > ,
191+ MultipartForm ( _ ) : MultipartForm < SubmitProofRequestRisc0 > ,
168192 ) -> impl Responder {
169193 HttpResponse :: Ok ( ) . json ( AppResponse :: new_sucessfull ( serde_json:: json!( { } ) ) )
170194 }
0 commit comments