Support passing additional HTTP headers into interactive_prove(). (#54)

This commit is contained in:
Pete Thomas
2024-06-05 07:27:00 -07:00
committed by GitHub
parent 3c7d6f837b
commit 6dbc409663
5 changed files with 83 additions and 43 deletions

View File

@@ -15,11 +15,15 @@ function App(): ReactElement {
const onClick = useCallback(async () => {
setProcessing(true);
const result = await interactive_prove(
'wss://notary.pse.dev/proxy?token=swapi.dev', //'ws://localhost:55688',
'ws://localhost:9816',
"https://swapi.dev/api/people/1",
"interactive-verifier-demo");
const result = await interactive_prove('https://swapi.dev/api/people/1', {
headers: {
RTT: '125',
'Sec-GPC': '1',
},
id: "interactive-verifier-demo",
verifierProxyUrl: 'ws://localhost:9816',
websocketProxyUrl: 'wss://notary.pse.dev/proxy?token=swapi.dev', //'ws://localhost:55688',
});
setResult(result);
setProcessing(false);

View File

@@ -58,19 +58,30 @@ export const prove = async (
export const interactive_prove = async (
websocket_proxy_url: string,
verifier_proxy_url: string,
uri: string,
id: string
url: string,
options: {
headers?: { [key: string]: string };
id: string;
verifierProxyUrl: string;
websocketProxyUrl: string;
},
): Promise<string> => {
const {
headers,
id,
verifierProxyUrl,
websocketProxyUrl,
} = options;
const tlsn = await getTLSN();
const proof = await tlsn.interactive_prove(
websocket_proxy_url,
verifier_proxy_url,
uri,
id);
const proof = await tlsn.interactive_prove(url, {
headers,
websocketProxyUrl,
verifierProxyUrl,
id,
});
return proof
};

View File

@@ -75,17 +75,23 @@ export default class TLSN {
}
async interactive_prove(
websocket_proxy_url: string,
verifier_proxy_url: string,
uri: string,
id: string,
url: string,
options?: {
headers?: { [key: string]: string };
id?: string;
verifierProxyUrl?: string;
websocketProxyUrl?: string;
},
) {
await this.waitForStart();
const resProver = await interactive_prover(
websocket_proxy_url,
verifier_proxy_url,
uri,
id
url,
{
...options,
id: options?.id,
verifierProxyUrl: options?.verifierProxyUrl,
websocketProxyUrl: options?.websocketProxyUrl,
}
);
const resJSON = JSON.parse(resProver);
// console.log('!@# resProver,resJSON=', { resProver, resJSON });

View File

@@ -1,5 +1,5 @@
use crate::hyper_io::FuturesIo;
use crate::request_opt::RequestOptions;
use crate::request_opt::InteractiveRequestOptions;
pub use crate::request_opt::VerifyResult;
use crate::requests::{ClientType, NotarizationSessionRequest, NotarizationSessionResponse};
use crate::{fetch_as_json_string, setup_tracing_web};
@@ -30,21 +30,22 @@ const SECRET: &str = "TLSNotary's private key 🤡";
#[tracing::instrument]
#[wasm_bindgen]
pub async fn interactive_prover(
websocket_proxy_url: String,
verifier_proxy_url: String,
uri: String,
id: String,
url: String,
val: JsValue,
) -> Result<String, JsValue> {
let uri = uri.parse::<Uri>().unwrap();
assert_eq!(uri.scheme().unwrap().as_str(), "https");
let server_domain = uri.authority().unwrap().host();
let url = url.parse::<Uri>().unwrap();
assert_eq!(url.scheme().unwrap().as_str(), "https");
let server_domain = url.authority().unwrap().host();
let options: InteractiveRequestOptions = serde_wasm_bindgen::from_value(val)
.map_err(|e| JsValue::from_str(&format!("Could not deserialize options: {:?}", e)))?;
info!(
"Interactive proof: {}, {}, {} ,{} ",
websocket_proxy_url, verifier_proxy_url, uri, id
options.websocket_proxy_url, options.verifier_proxy_url, url, options.id
);
let test = format!("{}/verify", verifier_proxy_url);
let test = format!("{}/verify", options.verifier_proxy_url);
let (_, verifier_ws_stream) = WsMeta::connect(test, None)
.await
.expect_throw("assume the verifier ws connection succeeds");
@@ -53,7 +54,7 @@ pub async fn interactive_prover(
// Create prover and connect to verifier.
let prover = Prover::new(
ProverConfig::builder()
.id(id)
.id(options.id)
.server_dns(server_domain)
.build()
.unwrap(),
@@ -63,8 +64,8 @@ pub async fn interactive_prover(
.unwrap();
// Connect to TLS Server.
debug!("Connect to websocket proxy {}", websocket_proxy_url);
let (_, client_ws_stream) = WsMeta::connect(websocket_proxy_url, None)
debug!("Connect to websocket proxy {}", options.websocket_proxy_url);
let (_, client_ws_stream) = WsMeta::connect(options.websocket_proxy_url, None)
.await
.expect_throw("assume the client ws connection succeeds");
let (mpc_tls_connection, prover_fut) =
@@ -93,15 +94,24 @@ pub async fn interactive_prover(
spawn_local(handled_connection_fut);
// MPC-TLS: Send Request and wait for Response.
let request = Request::builder()
.uri(uri.clone())
let mut req = Request::builder()
.uri(url.clone())
.method("GET")
.header("Host", server_domain)
.header("Connection", "close")
.header("Secret", SECRET)
.method("GET")
.body(Empty::<Bytes>::new())
.unwrap();
let response = request_sender.send_request(request).await.unwrap();
.header("Secret", SECRET);
for (key, value) in options.headers {
info!("adding header: {} - {}", key.as_str(), value.as_str());
req = req.header(key.as_str(), value.as_str());
}
let req_with_body = req.body(Full::new(Bytes::default()));
let unwrapped_request = req_with_body
.map_err(|e| JsValue::from_str(&format!("Could not build request: {:?}", e)))?;
let response = request_sender.send_request(unwrapped_request).await.unwrap();
assert!(response.status() == StatusCode::OK);

View File

@@ -27,3 +27,12 @@ pub struct VerifyResult {
pub sent: String,
pub recv: String,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InteractiveRequestOptions {
pub headers: HashMap<String, String>,
pub id: String,
pub verifier_proxy_url: String,
pub websocket_proxy_url: String,
}