1- use alloy:: signers:: Signer ;
1+ use alloy:: { hex , signers:: Signer } ;
22use reqwest:: { multipart, Client } ;
33use serde:: de:: DeserializeOwned ;
4+ use sp1_sdk:: { SP1ProofWithPublicValues , SP1VerifyingKey } ;
45
56use crate :: {
6- aggregation_layer :: gateway:: types:: {
7- GatewayResponse , NonceResponse , Receipt , ReceiptsQuery , ReceiptsResponse ,
7+ gateway:: types:: {
8+ GatewayResponse , NonceResponse , Receipt , ReceiptsQueryParams , ReceiptsResponse ,
89 SubmitProofResponse , SubmitSP1ProofMessage ,
910 } ,
10- common :: types:: Network ,
11+ types:: Network ,
1112} ;
1213
1314pub struct AggregationModeGatewayProvider < S : Signer > {
@@ -18,37 +19,27 @@ pub struct AggregationModeGatewayProvider<S: Signer> {
1819}
1920
2021#[ derive( Debug ) ]
21- pub enum AggregationModeError {
22- UnsupportedNetwork ,
22+ pub enum GatewayError {
2323 Request ( String ) ,
2424 Api { status : u16 , message : String } ,
2525 SignerNotConfigured ,
26+ ProofSerialization ( String ) ,
27+ MessageSignature ( String ) ,
2628}
2729
2830impl < S : Signer > AggregationModeGatewayProvider < S > {
29- pub fn new ( network : Network ) -> Result < Self , AggregationModeError > {
30- let gateway_url = match network {
31- Network :: Devnet => "http://127.0.0.1:8089" . into ( ) ,
32-
33- _ => return Err ( AggregationModeError :: UnsupportedNetwork ) ,
34- } ;
35-
31+ pub fn new ( network : Network ) -> Result < Self , GatewayError > {
3632 Ok ( Self {
37- gateway_url,
33+ gateway_url : network . gateway_url ( ) ,
3834 http_client : Client :: new ( ) ,
3935 signer : None ,
4036 network,
4137 } )
4238 }
4339
44- pub fn new_with_signer ( network : Network , signer : S ) -> Result < Self , AggregationModeError > {
45- let gateway_url = match network {
46- Network :: Devnet => "http://127.0.0.1:8089" . into ( ) ,
47- _ => return Err ( AggregationModeError :: UnsupportedNetwork ) ,
48- } ;
49-
40+ pub fn new_with_signer ( network : Network , signer : S ) -> Result < Self , GatewayError > {
5041 Ok ( Self {
51- gateway_url,
42+ gateway_url : network . gateway_url ( ) ,
5243 http_client : Client :: new ( ) ,
5344 signer : Some ( signer) ,
5445 network,
@@ -65,7 +56,7 @@ impl<S: Signer> AggregationModeGatewayProvider<S> {
6556 & self . gateway_url
6657 }
6758
68- pub async fn get_nonce_for ( & self , address : String ) -> Result < u64 , AggregationModeError > {
59+ pub async fn get_nonce_for ( & self , address : String ) -> Result < u64 , GatewayError > {
6960 let url = format ! ( "{}/nonce/{}" , self . gateway_url, address) ;
7061 let response: NonceResponse = self . send_request ( self . http_client . get ( url) ) . await ?;
7162
@@ -76,8 +67,8 @@ impl<S: Signer> AggregationModeGatewayProvider<S> {
7667 & self ,
7768 address : String ,
7869 nonce : Option < u64 > ,
79- ) -> Result < Vec < Receipt > , AggregationModeError > {
80- let query = ReceiptsQuery {
70+ ) -> Result < Vec < Receipt > , GatewayError > {
71+ let query = ReceiptsQueryParams {
8172 address : address,
8273 nonce,
8374 } ;
@@ -94,18 +85,24 @@ impl<S: Signer> AggregationModeGatewayProvider<S> {
9485
9586 pub async fn submit_sp1_proof (
9687 & self ,
97- serialized_proof : Vec < u8 > ,
98- serialized_vk : Vec < u8 > ,
99- ) -> Result < SubmitProofResponse , AggregationModeError > {
88+ proof : & SP1ProofWithPublicValues ,
89+ vk : & SP1VerifyingKey ,
90+ ) -> Result < SubmitProofResponse , GatewayError > {
91+ let serialized_proof = bincode:: serialize ( proof)
92+ . map_err ( |e| GatewayError :: ProofSerialization ( e. to_string ( ) ) ) ?;
93+ let serialized_vk =
94+ bincode:: serialize ( vk) . map_err ( |e| GatewayError :: ProofSerialization ( e. to_string ( ) ) ) ?;
95+
10096 let Some ( signer) = & self . signer else {
101- return Err ( AggregationModeError :: SignerNotConfigured ) ;
97+ return Err ( GatewayError :: SignerNotConfigured ) ;
10298 } ;
10399 let signer_address = signer. address ( ) . to_string ( ) ;
104-
105100 let nonce = self . get_nonce_for ( signer_address) . await ?;
106101 let message = SubmitSP1ProofMessage :: new ( nonce, serialized_proof, serialized_vk)
107102 . sign ( signer, & self . network )
108- . await ;
103+ . await
104+ . map_err ( |e| GatewayError :: MessageSignature ( e) ) ?;
105+
109106 let form = multipart:: Form :: new ( )
110107 . text ( "nonce" , message. nonce . to_string ( ) )
111108 . part (
@@ -131,19 +128,19 @@ impl<S: Signer> AggregationModeGatewayProvider<S> {
131128 async fn send_request < T : DeserializeOwned > (
132129 & self ,
133130 request : reqwest:: RequestBuilder ,
134- ) -> Result < T , AggregationModeError > {
131+ ) -> Result < T , GatewayError > {
135132 let response = request
136133 . send ( )
137134 . await
138- . map_err ( |e| AggregationModeError :: Request ( e. to_string ( ) ) ) ?;
135+ . map_err ( |e| GatewayError :: Request ( e. to_string ( ) ) ) ?;
139136
140137 let payload: GatewayResponse < T > = response
141138 . json ( )
142139 . await
143- . map_err ( |e| AggregationModeError :: Request ( e. to_string ( ) ) ) ?;
140+ . map_err ( |e| GatewayError :: Request ( e. to_string ( ) ) ) ?;
144141
145142 if payload. status != 200 {
146- return Err ( AggregationModeError :: Api {
143+ return Err ( GatewayError :: Api {
147144 status : payload. status ,
148145 message : payload. message ,
149146 } ) ;
0 commit comments