Skip to content

Commit

Permalink
fix(sdk): proof request signatures (#1845)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdvaithNair authored Dec 6, 2024
1 parent 6c09a31 commit aa5d953
Show file tree
Hide file tree
Showing 8 changed files with 397 additions and 42 deletions.
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

0 comments on commit aa5d953

Please sign in to comment.