Skip to content

Commit

Permalink
fix: final rc test before release
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtsukino committed Oct 4, 2024
1 parent b478b51 commit 99902d0
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 41 deletions.
48 changes: 21 additions & 27 deletions demo/react-ts-webpack/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
NotaryServer,
Transcript,
} from 'tlsn-js';
import { PresentationJSON } from '../../../build/types';

const { init, Prover, Presentation }: any = Comlink.wrap(
new Worker(new URL('./worker.ts', import.meta.url)),
Expand All @@ -23,7 +24,8 @@ function App(): ReactElement {
const [initialized, setInitialized] = useState(false);
const [processing, setProcessing] = useState(false);
const [result, setResult] = useState<any | null>(null);
const [proofHex, setProofHex] = useState<null | string>(null);
const [presentationJSON, setPresentationJSON] =
useState<null | PresentationJSON>(null);

useEffect(() => {
(async () => {
Expand Down Expand Up @@ -84,19 +86,18 @@ function App(): ReactElement {
const presentation = (await new Presentation({
attestationHex: notarizationOutputs.attestation,
secretsHex: notarizationOutputs.secrets,
notaryUrl: notarizationOutputs.notaryUrl,
websocketProxyUrl: notarizationOutputs.websocketProxyUrl,
reveal: commit,
})) as TPresentation;

const presentationHex = await presentation.serialize();

setPresentationJSON(await presentation.json());
console.timeEnd('proof');
setProofHex(presentationHex);
}, [setProofHex, setProcessing]);
}, [setPresentationJSON, setProcessing]);

const onAltClick = useCallback(async () => {
setProcessing(true);
const proof = await Prover.notarize({
id: 'test',
const proof = await (Prover.notarize as typeof TProver.notarize)({
notaryUrl: 'http://localhost:7047',
websocketProxyUrl: 'ws://localhost:55688',
url: 'https://swapi.dev/api/people/1',
Expand All @@ -114,17 +115,18 @@ function App(): ReactElement {
},
});

setProofHex(proof);
}, [setProofHex, setProcessing]);
setPresentationJSON(proof);
}, [setPresentationJSON, setProcessing]);

useEffect(() => {
(async () => {
if (proofHex) {
const proof = (await new Presentation(proofHex)) as TPresentation;
if (presentationJSON) {
const proof = (await new Presentation(
presentationJSON.data,
)) as TPresentation;
const notary = NotaryServer.from(`http://localhost:7047`);
const notaryKey = await notary.publicKey();
const notaryKey = await notary.publicKey('hex');
const verifierOutput = await proof.verify();
console.log(verifierOutput, notaryKey);
const transcript = new Transcript({
sent: verifierOutput.transcript.sent,
recv: verifierOutput.transcript.recv,
Expand All @@ -133,23 +135,15 @@ function App(): ReactElement {
setResult({
time: verifierOutput.connection_info.time,
verifyingKey: Buffer.from(vk.data).toString('hex'),
notaryKey: Buffer.from(
notaryKey
.replace('-----BEGIN PUBLIC KEY-----', '')
.replace('-----END PUBLIC KEY-----', '')
.replace(/\n/g, ''),
'base64',
)
.slice(23)
.toString('hex'),
notaryKey: notaryKey,
serverName: verifierOutput.server_name,
sent: transcript.sent(),
recv: transcript.recv(),
});
setProcessing(false);
}
})();
}, [proofHex, setResult]);
}, [presentationJSON, setResult]);

return (
<div>
Expand All @@ -171,9 +165,9 @@ function App(): ReactElement {
</div>
<div>
<b>Proof: </b>
{!processing && !proofHex ? (
{!processing && !presentationJSON ? (
<i>not started</i>
) : !proofHex ? (
) : !presentationJSON ? (
<>
Proving data from swapi...
<Watch
Expand All @@ -192,14 +186,14 @@ function App(): ReactElement {
<>
<details>
<summary>View Proof</summary>
<pre>{JSON.stringify(proofHex, null, 2)}</pre>
<pre>{JSON.stringify(presentationJSON, null, 2)}</pre>
</details>
</>
)}
</div>
<div>
<b>Verification: </b>
{!proofHex ? (
{!presentationJSON ? (
<i>not started</i>
) : !result ? (
<i>verifying</i>
Expand Down
17 changes: 15 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tlsn-js",
"version": "0.1.0-a7.rc.3",
"version": "0.1.0-a7.rc.5",
"description": "",
"repository": "https://github.com/tlsnotary/tlsn-js",
"main": "build/lib.js",
Expand Down Expand Up @@ -55,6 +55,7 @@
"serve": "14.2.1",
"serve-handler": "^6.1.5",
"stream-browserify": "^3.0.0",
"tlsn-wasm": "0.1.0-alpha.7.1",
"ts-loader": "^6.2.1",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.2",
Expand Down
64 changes: 55 additions & 9 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import initWasm, {
build_presentation,
ConnectionInfo,
PartialTranscript,
} from '../wasm/pkg/tlsn_wasm';
} from 'tlsn-wasm';
import {
arrayToHex,
processTranscript,
expect,
headerToMap,
hexToArray,
} from './utils';
import { ParsedTranscriptData } from './types';
import { ParsedTranscriptData, PresentationJSON } from './types';

let LOGGING_LEVEL: LoggingLevel = 'Info';

Expand Down Expand Up @@ -67,6 +67,8 @@ export default async function init(config?: {
export class Prover {
#prover: WasmProver;
#config: ProverConfig;
#verifierUrl?: string;
#websocketProxyUrl?: string;

static async notarize(options: {
url: string;
Expand All @@ -82,7 +84,7 @@ export class Prover {
maxRecvDataOnline?: number;
deferDecryptionFromStart?: boolean;
commit?: Commit;
}) {
}): Promise<PresentationJSON> {
const {
url,
method = 'GET',
Expand Down Expand Up @@ -128,7 +130,14 @@ export class Prover {

const presentation = build_presentation(attestation, secrets, commit);

return arrayToHex(presentation.serialize());
return {
version: '0.1.0-alpha.7',
data: arrayToHex(presentation.serialize()),
meta: {
notaryUrl: notaryUrl,
websocketProxyUrl: websocketProxyUrl,
},
};
}

constructor(config: {
Expand All @@ -153,6 +162,7 @@ export class Prover {
}

async setup(verifierUrl: string): Promise<void> {
this.#verifierUrl = verifierUrl;
return this.#prover.setup(verifierUrl);
}

Expand Down Expand Up @@ -213,6 +223,7 @@ export class Prover {
status: number;
headers: { [key: string]: string };
}> {
this.#websocketProxyUrl = wsProxyUrl;
const { url, method = 'GET', headers = {}, body } = request;

const headerMap = Prover.getHeaderMap(url, body, headers);
Expand All @@ -238,9 +249,12 @@ export class Prover {
};
}

async notarize(
commit?: Commit,
): Promise<{ attestation: string; secrets: string }> {
async notarize(commit?: Commit): Promise<{
attestation: string;
secrets: string;
notaryUrl?: string;
websocketProxyUrl?: string;
}> {
const transcript = await this.transcript();
const output = await this.#prover.notarize(
commit || {
Expand All @@ -251,6 +265,8 @@ export class Prover {
return {
attestation: arrayToHex(output.attestation.serialize()),
secrets: arrayToHex(output.secrets.serialize()),
notaryUrl: this.#verifierUrl,
websocketProxyUrl: this.#websocketProxyUrl,
};
}

Expand Down Expand Up @@ -282,12 +298,16 @@ export class Verifier {

export class Presentation {
#presentation: WasmPresentation;
#notaryUrl?: string;
#websocketProxyUrl?: string;

constructor(
params:
| {
attestationHex: string;
secretsHex: string;
notaryUrl?: string;
websocketProxyUrl?: string;
reveal?: Reveal;
}
| string,
Expand All @@ -308,6 +328,8 @@ export class Presentation {
recv: [{ start: 0, end: transcript.recv.length }],
},
);
this.#websocketProxyUrl = params.websocketProxyUrl;
this.#notaryUrl = params.notaryUrl;
}
}

Expand All @@ -323,6 +345,17 @@ export class Presentation {
return this.#presentation.verifying_key();
}

async json(): Promise<PresentationJSON> {
return {
version: '0.1.0-alpha.7',
data: await this.serialize(),
meta: {
notaryUrl: this.#notaryUrl,
websocketProxyUrl: this.#websocketProxyUrl,
},
};
}

async verify(): Promise<VerifierOutput> {
const {
server_name = '',
Expand Down Expand Up @@ -398,14 +431,27 @@ export class NotaryServer {
return this.#url;
}

async publicKey(): Promise<string> {
async publicKey(encoding: 'pem' | 'hex' = 'hex'): Promise<string> {
const res = await fetch(this.#url + '/info');
const { publicKey } = await res.json();
expect(
typeof publicKey === 'string' && !!publicKey.length,
'invalid public key',
);
return publicKey!;

if (encoding === 'pem') {
return publicKey!;
}

return Buffer.from(
publicKey!
.replace('-----BEGIN PUBLIC KEY-----', '')
.replace('-----END PUBLIC KEY-----', '')
.replace(/\n/g, ''),
'base64',
)
.slice(23)
.toString('hex');
}

async sessionUrl(
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ export type ParsedTranscriptData = {
json?: { [path: string]: CommitData };
lineBreaks: CommitData[];
};

export type PresentationJSON = {
version: '0.1.0-alpha.7';
data: string;
meta: {
notaryUrl?: string;
websocketProxyUrl?: string;
pluginUrl?: string;
};
};
6 changes: 4 additions & 2 deletions wasm/pkg/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"name": "tlsn-wasm",
"type": "module",
"version": "0.1.0-alpha.7",
"version": "0.1.0-alpha.7.1",
"files": [
"tlsn_wasm_bg.wasm",
"tlsn_wasm_bg.wasm.d.ts",
"tlsn_wasm.js",
"tlsn_wasm.d.ts"
"tlsn_wasm.d.ts",
"snippets/"
],
"main": "tlsn_wasm.js",
"types": "tlsn_wasm.d.ts",
Expand Down

0 comments on commit 99902d0

Please sign in to comment.