Skip to content

Commit 65ff097

Browse files
committed
feat: gateway send requests
1 parent 6ad8b6f commit 65ff097

4 files changed

Lines changed: 164 additions & 64 deletions

File tree

crates/sdk/src/aggregation_layer/gateway.rs

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod provider;
2+
pub mod types;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
use reqwest::Client;
2+
use serde::de::DeserializeOwned;
3+
4+
use crate::{
5+
aggregation_layer::gateway::types::{
6+
GatewayResponse, NonceResponse, Receipt, ReceiptsQuery, ReceiptsResponse,
7+
},
8+
common::types::Network,
9+
};
10+
11+
pub struct AggregationModeGatewayProvider {
12+
gateway_url: String,
13+
http_client: Client,
14+
}
15+
16+
#[derive(Debug)]
17+
pub enum AggregationModeError {
18+
UnsupportedNetwork,
19+
Request(String),
20+
Api { status: u16, message: String },
21+
}
22+
23+
impl AggregationModeGatewayProvider {
24+
pub fn new(network: Network) -> Result<Self, AggregationModeError> {
25+
let gateway_url = match network {
26+
Network::Devnet => "http://127.0.0.1:8089".into(),
27+
28+
_ => return Err(AggregationModeError::UnsupportedNetwork),
29+
};
30+
31+
Ok(Self {
32+
gateway_url,
33+
http_client: Client::new(),
34+
})
35+
}
36+
37+
pub fn new_with_signer() {}
38+
39+
pub fn signer() {}
40+
}
41+
42+
impl AggregationModeGatewayProvider {
43+
pub async fn gateway_url(&self) -> &str {
44+
&self.gateway_url
45+
}
46+
47+
pub async fn get_nonce_for(&self, address: String) -> Result<u64, AggregationModeError> {
48+
let url = format!("{}/nonce/{}", self.gateway_url, address);
49+
let response: NonceResponse = self.send_request(self.http_client.get(url)).await?;
50+
51+
Ok(response.nonce)
52+
}
53+
54+
pub async fn get_receipts_for(
55+
&self,
56+
address: String,
57+
nonce: Option<u64>,
58+
) -> Result<Vec<Receipt>, AggregationModeError> {
59+
let query = ReceiptsQuery {
60+
address: address,
61+
nonce,
62+
};
63+
64+
let request = self
65+
.http_client
66+
.get(format!("{}/receipts", self.gateway_url))
67+
.query(&query);
68+
69+
let response: ReceiptsResponse = self.send_request(request).await?;
70+
71+
Ok(response.receipts)
72+
}
73+
74+
pub async fn submit_sp1_proof(&self, serialized_proof: Vec<u8>, serialized_vk: Vec<u8>) {}
75+
76+
// TODO: verify proof from receipt merkle path
77+
78+
async fn send_request<T: DeserializeOwned>(
79+
&self,
80+
request: reqwest::RequestBuilder,
81+
) -> Result<T, AggregationModeError> {
82+
let response = request
83+
.send()
84+
.await
85+
.map_err(|e| AggregationModeError::Request(e.to_string()))?;
86+
87+
let payload: GatewayResponse<T> = response
88+
.json()
89+
.await
90+
.map_err(|e| AggregationModeError::Request(e.to_string()))?;
91+
92+
if payload.status != 200 {
93+
return Err(AggregationModeError::Api {
94+
status: payload.status,
95+
message: payload.message,
96+
});
97+
}
98+
99+
Ok(payload.data)
100+
}
101+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Debug, Deserialize)]
4+
pub(super) struct GatewayResponse<T> {
5+
pub status: u16,
6+
pub message: String,
7+
pub data: T,
8+
}
9+
10+
#[derive(Debug, Deserialize)]
11+
pub(super) struct NonceResponse {
12+
pub nonce: u64,
13+
}
14+
15+
#[derive(Debug, Serialize)]
16+
pub struct ReceiptsQuery {
17+
pub address: String,
18+
#[serde(skip_serializing_if = "Option::is_none")]
19+
pub nonce: Option<u64>,
20+
}
21+
22+
#[derive(Debug, Clone, Deserialize)]
23+
pub struct Receipt {
24+
pub status: String,
25+
pub merkle_path: Vec<String>,
26+
pub nonce: i64,
27+
pub address: String,
28+
}
29+
30+
#[derive(Debug, Deserialize)]
31+
pub(super) struct ReceiptsResponse {
32+
pub receipts: Vec<Receipt>,
33+
}
34+
35+
#[derive(Debug, Clone, Deserialize)]
36+
pub struct SubmitSP1ProofMessage {
37+
nonce: u64,
38+
proof: Vec<u8>,
39+
program_vk: Vec<u8>,
40+
signature: Vec<u8>,
41+
}
42+
43+
#[derive(Debug, Clone, Deserialize)]
44+
pub struct SubmitProofResponse {
45+
pub task_id: String,
46+
}
47+
48+
impl SubmitSP1ProofMessage {
49+
pub fn new(nonce: u64, serialized_proof: Vec<u8>, serialized_vk: Vec<u8>) -> Self {
50+
Self {
51+
nonce,
52+
proof: serialized_proof,
53+
program_vk: serialized_vk,
54+
signature: vec![],
55+
}
56+
}
57+
58+
pub fn sign(mut self) -> Self {
59+
self
60+
}
61+
}

0 commit comments

Comments
 (0)