From dc658680ed6b02f421c251172517221a7b1b47c9 Mon Sep 17 00:00:00 2001 From: Hendrik Eeckhaut Date: Tue, 14 Jan 2025 09:42:07 +0100 Subject: [PATCH] Improved React demo --- demo/react-ts-webpack/src/app.tsx | 131 ++++++++++++++++++++---------- 1 file changed, 90 insertions(+), 41 deletions(-) diff --git a/demo/react-ts-webpack/src/app.tsx b/demo/react-ts-webpack/src/app.tsx index 9810d2e..bb1e907 100644 --- a/demo/react-ts-webpack/src/app.tsx +++ b/demo/react-ts-webpack/src/app.tsx @@ -21,6 +21,14 @@ const root = createRoot(container!); root.render(); +const local = true; // Toggle between local and remote notary +const notaryUrl = local ? 'http://localhost:7047' : 'https://notary.pse.dev/v0.1.0-alpha.7'; +const websocketProxyUrl = local ? 'ws://localhost:55688' : 'wss://notary.pse.dev/proxy?token=swapi.dev'; +const loggingLevel = 'Info'; // https://github.com/tlsnotary/tlsn/blob/main/crates/wasm/src/log.rs#L8 + +const serverUrl = 'https://swapi.dev/api/people/1'; +const serverDns = 'swapi.dev'; + function App(): ReactElement { const [initialized, setInitialized] = useState(false); const [processing, setProcessing] = useState(false); @@ -30,22 +38,24 @@ function App(): ReactElement { useEffect(() => { (async () => { - await init({ loggingLevel: 'Info' }); + await init({ loggingLevel: loggingLevel }); setInitialized(true); })(); }, []); const onClick = useCallback(async () => { setProcessing(true); - const notary = NotaryServer.from(`http://localhost:7047`); + const notary = NotaryServer.from(notaryUrl); console.time('submit'); const prover = (await new Prover({ - serverDns: 'swapi.dev', + serverDns: serverDns, + maxRecvData: 2048, })) as TProver; await prover.setup(await notary.sessionUrl()); - const resp = await prover.sendRequest('ws://localhost:55688', { - url: 'https://swapi.dev/api/people/1', + + const resp = await prover.sendRequest(websocketProxyUrl, { + url: serverUrl, method: 'GET', headers: { 'Content-Type': 'application/json', @@ -99,13 +109,14 @@ function App(): ReactElement { const onAltClick = useCallback(async () => { setProcessing(true); const proof = await (Prover.notarize as typeof TProver.notarize)({ - notaryUrl: 'http://localhost:7047', - websocketProxyUrl: 'ws://localhost:55688', - url: 'https://swapi.dev/api/people/1', + notaryUrl: notaryUrl, + websocketProxyUrl: websocketProxyUrl, + url: serverUrl, method: 'GET', headers: { 'Content-Type': 'application/json', }, + body: { hello: 'world', one: 1, @@ -125,7 +136,7 @@ function App(): ReactElement { const proof = (await new Presentation( presentationJSON.data, )) as TPresentation; - const notary = NotaryServer.from(`http://localhost:7047`); + const notary = NotaryServer.from(notaryUrl); const notaryKey = await notary.publicKey('hex'); const verifierOutput = await proof.verify(); const transcript = new Transcript({ @@ -151,26 +162,78 @@ function App(): ReactElement {

TLSNotary React TypeScript Demo{' '}

-
- +
+

+ This demo showcases how to use TLSNotary in a React/TypeScript app with the tlsn-js library. + We will fetch JSON data from the Star Wars API, notarize the TLS request using TLSNotary, + and verify the proof. The demo runs entirely in the browser. +

+

+ + More info + +

+ + + + + + + + + + + + + + + + + + + + + +
Demo SettingsURL
Server{serverUrl}
Notary Server{notaryUrl}
WebSocket Proxy{websocketProxyUrl}
+
- +

+ There are two versions of the demo: one with a normal config and one with a helper method. +

+
+ + +
+ {processing && ( +
+ +
+ )}
Proof: @@ -178,7 +241,7 @@ function App(): ReactElement { not started ) : !presentationJSON ? (
- Proving data from swapi... + Proving data from {serverDns}... Open Developer tools to follow progress @@ -207,20 +270,6 @@ function App(): ReactElement { )}
- {processing && ( -
- -
- )}
); }