@@ -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,10 +20,8 @@ use super::{
1820use crate :: {
1921 config:: Config ,
2022 db:: Db ,
21- server:: types:: {
22- GetReceiptsResponse , SubmitProofRequest , SubmitProofRequestMessageRisc0 ,
23- SubmitProofRequestMessageSP1 ,
24- } ,
23+ server:: types:: { GetReceiptsResponse , SubmitProofRequestRisc0 , SubmitProofRequestSP1 } ,
24+ verifiers:: { verify_sp1_proof, VerificationError } ,
2525} ;
2626
2727#[ derive( Clone , Debug ) ]
@@ -79,7 +79,6 @@ impl BatcherServer {
7979 let address = address_raw. to_lowercase ( ) ;
8080
8181 // TODO: validate valid ethereum address
82-
8382 let Some ( state) = req. app_data :: < Data < BatcherServer > > ( ) else {
8483 return HttpResponse :: InternalServerError ( )
8584 . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ;
@@ -100,11 +99,8 @@ impl BatcherServer {
10099 // Posts an SP1 proof to the batcher, recovering the address from the signature
101100 async fn post_proof_sp1 (
102101 req : HttpRequest ,
103- body : web :: Json < SubmitProofRequest < SubmitProofRequestMessageSP1 > > ,
102+ MultipartForm ( data ) : MultipartForm < SubmitProofRequestSP1 > ,
104103 ) -> impl Responder {
105- let data = body. into_inner ( ) ;
106-
107- // TODO: validate signature
108104 let recovered_address = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" . to_lowercase ( ) ;
109105
110106 let Some ( state) = req. app_data :: < Data < BatcherServer > > ( ) else {
@@ -118,7 +114,7 @@ impl BatcherServer {
118114 . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ;
119115 } ;
120116
121- if data. nonce != ( count as u64 ) {
117+ if data. nonce . 0 != ( count as u64 ) {
122118 return HttpResponse :: BadRequest ( ) . json ( AppResponse :: new_unsucessfull (
123119 & format ! ( "Invalid nonce, expected nonce = {count}" ) ,
124120 400 ,
@@ -156,15 +152,42 @@ impl BatcherServer {
156152 ) ) ;
157153 }
158154
159- // TODO: decode proof and validate it
155+ let Ok ( proof_content) = tokio:: fs:: read ( data. proof . file . path ( ) ) . await else {
156+ return HttpResponse :: InternalServerError ( )
157+ . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ;
158+ } ;
159+
160+ let Ok ( proof) = bincode:: deserialize :: < SP1ProofWithPublicValues > ( & proof_content) else {
161+ return HttpResponse :: BadRequest ( )
162+ . json ( AppResponse :: new_unsucessfull ( "Invalid SP1 proof" , 400 ) ) ;
163+ } ;
164+
165+ let Ok ( vk_content) = tokio:: fs:: read ( data. program_vk . file . path ( ) ) . await else {
166+ return HttpResponse :: InternalServerError ( )
167+ . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ;
168+ } ;
169+
170+ let Ok ( vk) = bincode:: deserialize :: < SP1VerifyingKey > ( & vk_content) else {
171+ return HttpResponse :: BadRequest ( )
172+ . json ( AppResponse :: new_unsucessfull ( "Invalid vk" , 400 ) ) ;
173+ } ;
174+
175+ if let Err ( e) = verify_sp1_proof ( & proof, & vk) {
176+ let message = match e {
177+ VerificationError :: InvalidProof => "Proof verification failed" ,
178+ VerificationError :: UnsupportedProof => "Unsupported proof" ,
179+ } ;
180+
181+ return HttpResponse :: BadRequest ( ) . json ( AppResponse :: new_unsucessfull ( message, 400 ) ) ;
182+ } ;
160183
161184 match state
162185 . db
163186 . insert_task (
164187 & recovered_address,
165188 AggregationModeProvingSystem :: SP1 . as_u16 ( ) as i32 ,
166- & data . message . proof ,
167- & data . message . program_vk_commitment ,
189+ & proof_content ,
190+ & vk_content ,
168191 None ,
169192 data. nonce as i64 ,
170193 )
@@ -182,7 +205,7 @@ impl BatcherServer {
182205 // Posts a Risc0 proof to the batcher, recovering the address from the signature
183206 async fn post_proof_risc0 (
184207 _req : HttpRequest ,
185- _body : web :: Json < SubmitProofRequest < SubmitProofRequestMessageRisc0 > > ,
208+ MultipartForm ( _ ) : MultipartForm < SubmitProofRequestRisc0 > ,
186209 ) -> impl Responder {
187210 HttpResponse :: Ok ( ) . json ( AppResponse :: new_sucessfull ( serde_json:: json!( { } ) ) )
188211 }
0 commit comments