feat: add helper method and expose wasm types (#63)

This commit is contained in:
tsukino
2024-08-14 06:04:54 -04:00
committed by GitHub
parent 24aa329210
commit 7b78fbca61
4 changed files with 116 additions and 12 deletions

View File

@@ -86,6 +86,31 @@ function App(): ReactElement {
setProofHex(proofHex);
}, [setProofHex, setProcessing]);
const onAltClick = useCallback(async () => {
setProcessing(true);
await init({ loggingLevel: 'Debug' });
const proof = await Prover.notarize({
id: 'test',
notaryUrl: 'http://localhost:7047',
websocketProxyUrl: 'ws://localhost:55688',
url: 'https://swapi.dev/api/people/1',
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
body: {
hello: 'world',
one: 1,
},
commit: {
sent: [{ start: 0, end: 50 }],
recv: [{ start: 0, end: 50 }],
},
});
setProofHex(proof);
}, [setProofHex, setProcessing]);
useEffect(() => {
(async () => {
if (proofHex) {
@@ -104,9 +129,22 @@ function App(): ReactElement {
return (
<div>
<button onClick={!processing ? onClick : undefined} disabled={processing}>
Start demo
</button>
<div>
<button
onClick={!processing ? onClick : undefined}
disabled={processing}
>
Start Demo (Normal config)
</button>
</div>
<div>
<button
onClick={!processing ? onAltClick : undefined}
disabled={processing}
>
Start Demo 2 (With helper method)
</button>
</div>
<div>
<b>Proof: </b>
{!processing && !proofHex ? (

View File

@@ -1,6 +1,6 @@
{
"name": "tlsn-js",
"version": "v0.1.0-alpha.6.1",
"version": "v0.1.0-alpha.6.2",
"description": "",
"repository": "https://github.com/tlsnotary/tlsn-js",
"main": "build/lib.js",
@@ -8,7 +8,7 @@
"files": [
"build/",
"src/",
"wasm/prover/pkg/*",
"wasm/pkg/*",
"readme.md"
],
"scripts": {

View File

@@ -16,7 +16,7 @@ import initWasm, {
VerifierData,
NotaryPublicKey,
} from '../wasm/pkg/tlsn_wasm';
import { arrayToHex, processTranscript, stringToBuffer, expect } from './utils';
import { arrayToHex, processTranscript, expect, headerToMap } from './utils';
import type { ParsedTranscriptData, ProofData } from './types';
let LOGGING_LEVEL: LoggingLevel = 'Info';
@@ -59,6 +59,64 @@ export class Prover {
#prover: WasmProver;
#config: ProverConfig;
static async notarize(options: {
url: string;
notaryUrl: string;
websocketProxyUrl: string;
method?: Method;
headers?: {
[name: string]: string;
};
body?: any;
maxSentData?: number;
maxRecvData?: number;
id: string;
commit?: Commit;
}) {
const {
url,
method = 'GET',
headers = {},
body,
maxSentData,
maxRecvData,
notaryUrl,
websocketProxyUrl,
id,
commit: _commit,
} = options;
const hostname = new URL(url).hostname;
const notary = NotaryServer.from(notaryUrl);
const prover = new WasmProver({
id,
server_dns: hostname,
max_sent_data: maxSentData,
max_recv_data: maxRecvData,
});
await prover.setup(await notary.sessionUrl(maxSentData, maxRecvData));
await prover.send_request(websocketProxyUrl + `?token=${hostname}`, {
uri: url,
method,
headers: headerToMap(headers),
body,
});
const transcript = prover.transcript();
const commit = _commit || {
sent: [{ start: 0, end: transcript.sent.length }],
recv: [{ start: 0, end: transcript.recv.length }],
};
const session = await prover.notarize(commit);
const tlsProof = await session.proof(commit);
return tlsProof.serialize();
}
constructor(config: {
id?: string;
serverDns: string;
@@ -118,12 +176,10 @@ export class Prover {
}> {
const { url, method = 'GET', headers = {}, body } = request;
const hostname = new URL(url).hostname;
const headerMap: Map<string, number[]> = new Map();
headerMap.set('Host', stringToBuffer(hostname));
headerMap.set('Connection', stringToBuffer('close'));
Object.entries(headers).forEach(([key, value]) => {
headerMap.set(key, stringToBuffer(value));
const headerMap = headerToMap({
Host: hostname,
Connection: 'close',
...headers,
});
const resp = await this.#prover.send_request(wsProxyUrl, {

View File

@@ -406,3 +406,13 @@ export function stringToBuffer(str: string): number[] {
export function arrayToHex(uintArr: Uint8Array): string {
return Buffer.from(uintArr).toString('hex');
}
export function headerToMap(headers: {
[name: string]: string;
}): Map<string, number[]> {
const headerMap: Map<string, number[]> = new Map();
Object.entries(headers).forEach(([key, value]) => {
headerMap.set(key, stringToBuffer(value));
});
return headerMap;
}