Skip to content

Commit

Permalink
Merge pull request #14 from zippiehq/feat/nitro_attestation
Browse files Browse the repository at this point in the history
feat: add nitro signing feature
  • Loading branch information
stskeeps authored Nov 5, 2024
2 parents 6850ef6 + 14a73ca commit 08f4d42
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 36 deletions.
71 changes: 37 additions & 34 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async-std = { version = "1", features = ["attributes", "tokio1"] }
advance_runner = {git = "https://github.com/zippiehq/cartesi-advance-runner", rev = "d56ca465282b258e6850fafb411926e90e9e7ff9"}
hyper = { version = "0.14", features = ["full"] }
hex = "0.4"
serde_json = "1.0.107"
serde_json = "1.0.132"
regex = "1.11.0"
dotenv = "0.15.0"

Expand All @@ -21,8 +21,12 @@ futures = "0.3"
cid = "0.8.6"
alloy-primitives = "0.8.7"
rayon = "1.10.0"
serde = "1.0.214"
base64 = "0.22.1"
hyper-tls = "0.6.0"
[features]
default = []
bls_signing = [
"signer-eigen",
]
nitro_attestation = []
43 changes: 42 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ use alloy_primitives::utils::{keccak256, Keccak256};
use alloy_primitives::B256;
use async_std::channel::bounded;
use async_std::fs::OpenOptions;
use base64::{prelude::BASE64_STANDARD, Engine};
use cid::Cid;
use futures::TryStreamExt;
use hyper::body::to_bytes;
use hyper::header::HeaderValue;
use hyper::service::{make_service_fn, service_fn};
use hyper::Method;
use hyper::{Body, Client, Request, Response, Server, StatusCode};
use hyper_tls::HttpsConnector;
use ipfs_api_backend_hyper::IpfsApi;
use regex::Regex;
use rs_car_ipfs::single_file::read_single_file_seek;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::OpenOptions as StdOpenOptions;
use std::io::Error;
Expand Down Expand Up @@ -181,6 +186,21 @@ async fn main() {
"outputs_callback_vector": *outputs_vector,
"reports_callback_vector": *reports_vector,
});

#[cfg(feature = "nitro_attestation")]
{
let data = hyper::body::to_bytes(
serde_json::to_string(&json_response).unwrap(),
)
.await
.unwrap()
.to_vec();

let attestation_doc = get_attestation(data.clone()).await;
json_response["attestation_doc"] =
serde_json::from_slice(&attestation_doc).unwrap();
}

#[cfg(feature = "bls_signing")]
if signing_requested {
let bls_private_key_str = std::env::var("BLS_PRIVATE_KEY")
Expand Down Expand Up @@ -215,7 +235,6 @@ async fn main() {
json_response["signature"] =
serde_json::Value::String(signature_hex);
}

let json_response = serde_json::to_string(&json_response).unwrap();

let response = Response::builder()
Expand Down Expand Up @@ -447,6 +466,28 @@ async fn main() {
println!("Server is listening on {}", addr);
server.await.unwrap();
}

async fn get_attestation<T: AsRef<[u8]>>(user_data: T) -> Vec<u8> {
let client = Client::new();

let uri = "http://localhost:7777/v1/attestation".parse().unwrap();

let req_data = AttestationUserData {
user_data: BASE64_STANDARD.encode(user_data),
};

let mut req = Request::new(Body::from(serde_json::json!(req_data).to_string()));

*req.uri_mut() = uri;
*req.method_mut() = Method::POST;

let response = client.request(req).await.unwrap();
to_bytes(response.into_body()).await.unwrap().to_vec()
}
#[derive(Debug, Serialize, Deserialize)]
struct AttestationUserData {
user_data: String,
}
fn check_hash_format(hash: &str, error_message: &str) -> Result<(), Response<Body>> {
let hash_regex = Regex::new(r"^[a-f0-9]{64}$").unwrap();

Expand Down

0 comments on commit 08f4d42

Please sign in to comment.