Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk): proof request signatures #1845

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ sp1-sdk = { path = "crates/sdk", version = "3.0.0" }
sp1-cuda = { path = "crates/cuda", version = "3.0.0" }
sp1-stark = { path = "crates/stark", version = "3.0.0" }
sp1-lib = { path = "crates/zkvm/lib", version = "3.0.0", default-features = false }

# NOTE: The version in this crate is manually set to 3.0.1 right now. When upgrading SP1 versions,
# make sure to update this crate.
sp1-zkvm = { path = "crates/zkvm/entrypoint", version = "3.0.1", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions crates/sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ categories = { workspace = true }
[dependencies]
prost = { version = "0.13", optional = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
twirp = { package = "twirp-rs", version = "0.13.0-succinct", optional = true }
async-trait = "0.1.81"
reqwest-middleware = { version = "0.3.2", optional = true }
Expand Down
6 changes: 4 additions & 2 deletions crates/sdk/src/network-v2/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use crate::network_v2::proto::network::{
prover_network_client::ProverNetworkClient, CreateProgramRequest, CreateProgramRequestBody,
CreateProgramResponse, FulfillmentStatus, FulfillmentStrategy, GetNonceRequest,
GetProgramRequest, GetProgramResponse, GetProofRequestStatusRequest,
GetProofRequestStatusResponse, ProofMode, RequestProofRequest, RequestProofRequestBody,
RequestProofResponse,
GetProofRequestStatusResponse, MessageFormat, ProofMode, RequestProofRequest,
RequestProofRequestBody, RequestProofResponse,
};
use crate::network_v2::Signable;

Expand Down Expand Up @@ -160,6 +160,7 @@ impl NetworkClient {

Ok(rpc
.create_program(CreateProgramRequest {
format: MessageFormat::Binary.into(),
signature: request_body.sign(&self.signer).into(),
body: Some(request_body),
})
Expand Down Expand Up @@ -232,6 +233,7 @@ impl NetworkClient {
};
let request_response = rpc
.request_proof(RequestProofRequest {
format: MessageFormat::Binary.into(),
signature: request_body.sign(&self.signer).into(),
body: Some(request_body),
})
Expand Down
57 changes: 57 additions & 0 deletions crates/sdk/src/network-v2/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use prost::Message;
#[allow(unused_imports)]
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Errors that can occur during JSON formatting.
#[derive(Error, Debug)]
pub enum JsonFormatError {
#[error("Serialization error: {0}")]
SerializationError(String),
}

/// Formats a Protobuf body into a JSON byte representation.
pub fn format_json_message<T>(body: &T) -> Result<Vec<u8>, JsonFormatError>
where
T: Message + Serialize,
{
match serde_json::to_string(body) {
Ok(json_str) => {
if json_str.starts_with('"') && json_str.ends_with('"') {
let inner = &json_str[1..json_str.len() - 1];
let unescaped = inner.replace("\\\"", "\"");
Ok(unescaped.into_bytes())
} else {
Ok(json_str.into_bytes())
}
}
Err(e) => Err(JsonFormatError::SerializationError(e.to_string())),
}
}

#[cfg(test)]
mod tests {
use super::*;
use prost::Message as ProstMessage;

// Test message for JSON formatting.
#[derive(Clone, ProstMessage, Serialize, Deserialize)]
struct TestMessage {
#[prost(string, tag = 1)]
value: String,
}

#[test]
fn test_format_json_message_simple() {
let msg = TestMessage { value: "hello".to_string() };
let result = format_json_message(&msg).unwrap();
assert_eq!(result, b"{\"value\":\"hello\"}");
}

#[test]
fn test_format_json_message_with_quotes() {
let msg = TestMessage { value: "hello \"world\"".to_string() };
let result = format_json_message(&msg).unwrap();
assert_eq!(result, b"{\"value\":\"hello \\\"world\\\"\"}");
}
}
1 change: 1 addition & 0 deletions crates/sdk/src/network-v2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod client;
mod json;
pub mod prover;
mod sign_message;
#[rustfmt::skip]
Expand Down
Loading
Loading