mirror of
https://github.com/tlsnotary/tlsn-js.git
synced 2026-04-02 03:00:18 -04:00
fix: add new http parser
This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
import {
|
||||
Prover as _Prover,
|
||||
NotaryServer,
|
||||
Presentation as _Presentation,
|
||||
} from '../../src/lib';
|
||||
import * as Comlink from 'comlink';
|
||||
import { Transcript } from '../../src/lib';
|
||||
import { assert } from '../utils';
|
||||
|
||||
const { init, Prover, Presentation }: any = Comlink.wrap(
|
||||
// @ts-ignore
|
||||
new Worker(new URL('../worker.ts', import.meta.url)),
|
||||
);
|
||||
|
||||
(async function () {
|
||||
try {
|
||||
await init({ loggingLevel: 'Debug' });
|
||||
// @ts-ignore
|
||||
console.log('test start');
|
||||
console.time('prove');
|
||||
const prover = (await new Prover({
|
||||
id: 'test',
|
||||
serverDns: 'swapi.dev',
|
||||
})) as _Prover;
|
||||
const notary = NotaryServer.from('http://localhost:7047');
|
||||
await prover.setup(await notary.sessionUrl());
|
||||
await prover.sendRequest('wss://notary.pse.dev/proxy?token=swapi.dev', {
|
||||
url: 'https://swapi.dev/api/people/1',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
secret: 'test_secret',
|
||||
},
|
||||
});
|
||||
const transcript = await prover.transcript();
|
||||
console.log({ transcript });
|
||||
const commit = {
|
||||
sent: [
|
||||
...Object.entries(transcript.ranges.sent.headers)
|
||||
.filter(([k]) => k !== 'secret')
|
||||
.map(([, v]) => v),
|
||||
transcript.ranges.sent.info,
|
||||
...transcript.ranges.sent.lineBreaks,
|
||||
],
|
||||
recv: [
|
||||
...Object.entries(transcript.ranges.recv.headers).map(([, v]) => v),
|
||||
transcript.ranges.recv.info,
|
||||
...transcript.ranges.recv.lineBreaks,
|
||||
transcript.ranges.recv.json!['name'],
|
||||
transcript.ranges.recv.json!['hair_color'],
|
||||
transcript.ranges.recv.json!['skin_color'],
|
||||
],
|
||||
};
|
||||
console.log(commit);
|
||||
const notarizationOutput = await prover.notarize(commit);
|
||||
const presentation = (await new Presentation({
|
||||
attestationHex: notarizationOutput.attestation,
|
||||
secretsHex: notarizationOutput.secrets,
|
||||
reveal: commit,
|
||||
})) as _Presentation;
|
||||
console.log('presentation:', await presentation.serialize());
|
||||
console.timeEnd('prove');
|
||||
|
||||
console.time('verify');
|
||||
const { transcript: partialTranscript, server_name } =
|
||||
await presentation.verify();
|
||||
const verifyingKey = await presentation.verifyingKey();
|
||||
console.timeEnd('verify');
|
||||
|
||||
console.log('verifyingKey', verifyingKey);
|
||||
const t = new Transcript({
|
||||
sent: partialTranscript.sent,
|
||||
recv: partialTranscript.recv,
|
||||
});
|
||||
const sent = t.sent();
|
||||
const recv = t.recv();
|
||||
assert(sent.includes('host: swapi.dev'));
|
||||
assert(!sent.includes('secret: test_secret'));
|
||||
assert(recv.includes('"name":"Luke Skywalker"'));
|
||||
assert(recv.includes('"hair_color":"blond"'));
|
||||
assert(recv.includes('"skin_color":"fair"'));
|
||||
assert(server_name === 'swapi.dev');
|
||||
|
||||
// @ts-ignore
|
||||
document.getElementById('full-integration-swapi').textContent = 'OK';
|
||||
} catch (err) {
|
||||
console.log('caught error from wasm');
|
||||
console.error(err);
|
||||
|
||||
// @ts-ignore
|
||||
document.getElementById('full-integration-swapi').textContent = err.message;
|
||||
}
|
||||
})();
|
||||
File diff suppressed because one or more lines are too long
99
test/specs/transcript.ts
Normal file
99
test/specs/transcript.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { describe, it } from 'mocha';
|
||||
import * as assert from 'assert';
|
||||
import Transcript from '../../src/transcript';
|
||||
import { HTTPParser } from 'http-parser-js';
|
||||
describe('Transcript parsing', () => {
|
||||
it('should parse transcript correctly', async () => {
|
||||
const transcript = new Transcript({ sent: swapiSent, recv: swapiRecv });
|
||||
const parser = new HTTPParser(HTTPParser.REQUEST);
|
||||
const bodyChunks: Buffer[] = [];
|
||||
let headerInfo = null;
|
||||
|
||||
parser.onBody = (t) => {
|
||||
bodyChunks.push(t);
|
||||
};
|
||||
|
||||
parser.onHeadersComplete = (req) => {
|
||||
headerInfo = req;
|
||||
};
|
||||
|
||||
parser.execute(Buffer.from(swapiSent));
|
||||
parser.finish();
|
||||
console.log(bodyChunks, headerInfo);
|
||||
console.log(transcript.sent());
|
||||
assert.strictEqual(true, true, 'test');
|
||||
});
|
||||
});
|
||||
|
||||
const swapiRecv = [
|
||||
72, 84, 84, 80, 47, 49, 46, 49, 32, 50, 48, 48, 32, 79, 75, 13, 10, 83, 101,
|
||||
114, 118, 101, 114, 58, 32, 110, 103, 105, 110, 120, 47, 49, 46, 49, 54, 46,
|
||||
49, 13, 10, 68, 97, 116, 101, 58, 32, 70, 114, 105, 44, 32, 48, 55, 32, 70,
|
||||
101, 98, 32, 50, 48, 50, 53, 32, 48, 55, 58, 51, 55, 58, 49, 49, 32, 71, 77,
|
||||
84, 13, 10, 67, 111, 110, 116, 101, 110, 116, 45, 84, 121, 112, 101, 58, 32,
|
||||
97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 106, 115, 111, 110,
|
||||
13, 10, 84, 114, 97, 110, 115, 102, 101, 114, 45, 69, 110, 99, 111, 100, 105,
|
||||
110, 103, 58, 32, 99, 104, 117, 110, 107, 101, 100, 13, 10, 67, 111, 110, 110,
|
||||
101, 99, 116, 105, 111, 110, 58, 32, 99, 108, 111, 115, 101, 13, 10, 86, 97,
|
||||
114, 121, 58, 32, 65, 99, 99, 101, 112, 116, 44, 32, 67, 111, 111, 107, 105,
|
||||
101, 13, 10, 88, 45, 70, 114, 97, 109, 101, 45, 79, 112, 116, 105, 111, 110,
|
||||
115, 58, 32, 83, 65, 77, 69, 79, 82, 73, 71, 73, 78, 13, 10, 69, 84, 97, 103,
|
||||
58, 32, 34, 101, 101, 51, 57, 56, 54, 49, 48, 52, 51, 53, 99, 51, 50, 56, 102,
|
||||
52, 100, 48, 97, 52, 101, 49, 98, 48, 100, 50, 102, 55, 98, 98, 99, 34, 13,
|
||||
10, 65, 108, 108, 111, 119, 58, 32, 71, 69, 84, 44, 32, 72, 69, 65, 68, 44,
|
||||
32, 79, 80, 84, 73, 79, 78, 83, 13, 10, 83, 116, 114, 105, 99, 116, 45, 84,
|
||||
114, 97, 110, 115, 112, 111, 114, 116, 45, 83, 101, 99, 117, 114, 105, 116,
|
||||
121, 58, 32, 109, 97, 120, 45, 97, 103, 101, 61, 49, 53, 55, 54, 56, 48, 48,
|
||||
48, 13, 10, 13, 10, 50, 56, 55, 13, 10, 123, 34, 110, 97, 109, 101, 34, 58,
|
||||
34, 76, 117, 107, 101, 32, 83, 107, 121, 119, 97, 108, 107, 101, 114, 34, 44,
|
||||
34, 104, 101, 105, 103, 104, 116, 34, 58, 34, 49, 55, 50, 34, 44, 34, 109, 97,
|
||||
115, 115, 34, 58, 34, 55, 55, 34, 44, 34, 104, 97, 105, 114, 95, 99, 111, 108,
|
||||
111, 114, 34, 58, 34, 98, 108, 111, 110, 100, 34, 44, 34, 115, 107, 105, 110,
|
||||
95, 99, 111, 108, 111, 114, 34, 58, 34, 102, 97, 105, 114, 34, 44, 34, 101,
|
||||
121, 101, 95, 99, 111, 108, 111, 114, 34, 58, 34, 98, 108, 117, 101, 34, 44,
|
||||
34, 98, 105, 114, 116, 104, 95, 121, 101, 97, 114, 34, 58, 34, 49, 57, 66, 66,
|
||||
89, 34, 44, 34, 103, 101, 110, 100, 101, 114, 34, 58, 34, 109, 97, 108, 101,
|
||||
34, 44, 34, 104, 111, 109, 101, 119, 111, 114, 108, 100, 34, 58, 34, 104, 116,
|
||||
116, 112, 115, 58, 47, 47, 115, 119, 97, 112, 105, 46, 100, 101, 118, 47, 97,
|
||||
112, 105, 47, 112, 108, 97, 110, 101, 116, 115, 47, 49, 47, 34, 44, 34, 102,
|
||||
105, 108, 109, 115, 34, 58, 91, 34, 104, 116, 116, 112, 115, 58, 47, 47, 115,
|
||||
119, 97, 112, 105, 46, 100, 101, 118, 47, 97, 112, 105, 47, 102, 105, 108,
|
||||
109, 115, 47, 49, 47, 34, 44, 34, 104, 116, 116, 112, 115, 58, 47, 47, 115,
|
||||
119, 97, 112, 105, 46, 100, 101, 118, 47, 97, 112, 105, 47, 102, 105, 108,
|
||||
109, 115, 47, 50, 47, 34, 44, 34, 104, 116, 116, 112, 115, 58, 47, 47, 115,
|
||||
119, 97, 112, 105, 46, 100, 101, 118, 47, 97, 112, 105, 47, 102, 105, 108,
|
||||
109, 115, 47, 51, 47, 34, 44, 34, 104, 116, 116, 112, 115, 58, 47, 47, 115,
|
||||
119, 97, 112, 105, 46, 100, 101, 118, 47, 97, 112, 105, 47, 102, 105, 108,
|
||||
109, 115, 47, 54, 47, 34, 93, 44, 34, 115, 112, 101, 99, 105, 101, 115, 34,
|
||||
58, 91, 93, 44, 34, 118, 101, 104, 105, 99, 108, 101, 115, 34, 58, 91, 34,
|
||||
104, 116, 116, 112, 115, 58, 47, 47, 115, 119, 97, 112, 105, 46, 100, 101,
|
||||
118, 47, 97, 112, 105, 47, 118, 101, 104, 105, 99, 108, 101, 115, 47, 49, 52,
|
||||
47, 34, 44, 34, 104, 116, 116, 112, 115, 58, 47, 47, 115, 119, 97, 112, 105,
|
||||
46, 100, 101, 118, 47, 97, 112, 105, 47, 118, 101, 104, 105, 99, 108, 101,
|
||||
115, 47, 51, 48, 47, 34, 93, 44, 34, 115, 116, 97, 114, 115, 104, 105, 112,
|
||||
115, 34, 58, 91, 34, 104, 116, 116, 112, 115, 58, 47, 47, 115, 119, 97, 112,
|
||||
105, 46, 100, 101, 118, 47, 97, 112, 105, 47, 115, 116, 97, 114, 115, 104,
|
||||
105, 112, 115, 47, 49, 50, 47, 34, 44, 34, 104, 116, 116, 112, 115, 58, 47,
|
||||
47, 115, 119, 97, 112, 105, 46, 100, 101, 118, 47, 97, 112, 105, 47, 115, 116,
|
||||
97, 114, 115, 104, 105, 112, 115, 47, 50, 50, 47, 34, 93, 44, 34, 99, 114,
|
||||
101, 97, 116, 101, 100, 34, 58, 34, 50, 48, 49, 52, 45, 49, 50, 45, 48, 57,
|
||||
84, 49, 51, 58, 53, 48, 58, 53, 49, 46, 54, 52, 52, 48, 48, 48, 90, 34, 44,
|
||||
34, 101, 100, 105, 116, 101, 100, 34, 58, 34, 50, 48, 49, 52, 45, 49, 50, 45,
|
||||
50, 48, 84, 50, 49, 58, 49, 55, 58, 53, 54, 46, 56, 57, 49, 48, 48, 48, 90,
|
||||
34, 44, 34, 117, 114, 108, 34, 58, 34, 104, 116, 116, 112, 115, 58, 47, 47,
|
||||
115, 119, 97, 112, 105, 46, 100, 101, 118, 47, 97, 112, 105, 47, 112, 101,
|
||||
111, 112, 108, 101, 47, 49, 47, 34, 125, 13, 10, 48, 13, 10, 13, 10,
|
||||
];
|
||||
|
||||
const swapiSent = [
|
||||
71, 69, 84, 32, 104, 116, 116, 112, 115, 58, 47, 47, 115, 119, 97, 112, 105,
|
||||
46, 100, 101, 118, 47, 97, 112, 105, 47, 112, 101, 111, 112, 108, 101, 47, 49,
|
||||
32, 72, 84, 84, 80, 47, 49, 46, 49, 13, 10, 99, 111, 110, 110, 101, 99, 116,
|
||||
105, 111, 110, 58, 32, 99, 108, 111, 115, 101, 13, 10, 99, 111, 110, 116, 101,
|
||||
110, 116, 45, 108, 101, 110, 103, 116, 104, 58, 32, 50, 53, 13, 10, 99, 111,
|
||||
110, 116, 101, 110, 116, 45, 116, 121, 112, 101, 58, 32, 97, 112, 112, 108,
|
||||
105, 99, 97, 116, 105, 111, 110, 47, 106, 115, 111, 110, 13, 10, 104, 111,
|
||||
115, 116, 58, 32, 115, 119, 97, 112, 105, 46, 100, 101, 118, 13, 10, 13, 10,
|
||||
123, 34, 104, 101, 108, 108, 111, 34, 58, 34, 119, 111, 114, 108, 100, 34, 44,
|
||||
34, 111, 110, 101, 34, 58, 49, 125,
|
||||
];
|
||||
Reference in New Issue
Block a user