Skip to content

Commit bb0e9ac

Browse files
committed
refactor: move sdk to aggregation_mode dir
1 parent 5152746 commit bb0e9ac

File tree

12 files changed

+133
-241
lines changed

12 files changed

+133
-241
lines changed

aggregation_mode/Cargo.lock

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aggregation_mode/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
resolver = "2"
3-
members = ["./batcher", "./proof_aggregator", "./db"]
3+
members = ["./batcher", "./proof_aggregator", "./db", "./sdk"]
44

55
[workspace.package]
66
version = "0.1.0"

aggregation_mode/sdk/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "agg_mode_sdk"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
serde = { workspace = true }
8+
tracing = { version = "0.1", features = ["log"] }
9+
tracing-subscriber = { version = "0.3.0", features = ["env-filter"] }
10+
bincode = "1.3.3"
11+
reqwest = { version = "0.12", features = ["json", "multipart"] }
12+
tokio = { version = "1", features = ["time"]}
13+
alloy = { workspace = true }
14+
sp1-sdk = { workspace = true }
File renamed without changes.

crates/sdk/src/aggregation_layer/gateway/provider.rs renamed to aggregation_mode/sdk/src/gateway/provider.rs

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
use alloy::signers::Signer;
1+
use alloy::{hex, signers::Signer};
22
use reqwest::{multipart, Client};
33
use serde::de::DeserializeOwned;
4+
use sp1_sdk::{SP1ProofWithPublicValues, SP1VerifyingKey};
45

56
use 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

1314
pub 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

2830
impl<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
});

crates/sdk/src/aggregation_layer/gateway/types.rs renamed to aggregation_mode/sdk/src/gateway/types.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use alloy::{
55
};
66
use serde::{Deserialize, Serialize};
77

8-
use crate::common::types::Network;
8+
use crate::types::Network;
99

1010
#[derive(Debug, Deserialize)]
1111
pub(super) struct GatewayResponse<T> {
@@ -20,7 +20,7 @@ pub(super) struct NonceResponse {
2020
}
2121

2222
#[derive(Debug, Serialize)]
23-
pub struct ReceiptsQuery {
23+
pub struct ReceiptsQueryParams {
2424
pub address: String,
2525
#[serde(skip_serializing_if = "Option::is_none")]
2626
pub nonce: Option<u64>,
@@ -96,14 +96,14 @@ impl SubmitSP1ProofMessage {
9696
keccak256([&[0x19, 0x01], &domain_separator[..], &message_hash[..]].concat()).0
9797
}
9898

99-
pub async fn sign<S: Signer>(mut self, signer: &S, network: &Network) -> Self {
99+
pub async fn sign<S: Signer>(mut self, signer: &S, network: &Network) -> Result<Self, String> {
100100
let signature = signer
101101
.sign_hash(&self.eip712_hash(network).into())
102102
.await
103-
.unwrap();
103+
.map_err(|e| e.to_string())?;
104104

105105
self.signature = signature.as_bytes().to_vec();
106106

107-
self
107+
Ok(self)
108108
}
109109
}

aggregation_mode/sdk/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod gateway;
2+
pub mod types;
3+
// TODO: move the rest of the sdk in crates/sdk/aggregation_layer to here

aggregation_mode/sdk/src/types.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::str::FromStr;
2+
3+
#[derive(Debug, Clone)]
4+
pub enum Network {
5+
Devnet,
6+
}
7+
8+
#[derive(Debug, Clone)]
9+
pub enum NetworkError {
10+
InvalidNetwork,
11+
}
12+
13+
impl FromStr for Network {
14+
type Err = NetworkError;
15+
fn from_str(s: &str) -> Result<Self, Self::Err> {
16+
match s.to_lowercase().as_str() {
17+
"devnet" => Ok(Self::Devnet),
18+
_ => Err(NetworkError::InvalidNetwork),
19+
}
20+
}
21+
}
22+
23+
impl Network {
24+
pub fn chain_id(&self) -> u64 {
25+
match self {
26+
Self::Devnet => 31_337,
27+
}
28+
}
29+
30+
pub fn gateway_url(&self) -> String {
31+
match self {
32+
Self::Devnet => "http://127.0.0.1:8089".to_string(),
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)