Skip to content

Commit

Permalink
fix(Transcript): recv and sent hiding support utf-8 bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
ciricc committed Dec 11, 2024
1 parent 2b78361 commit c93ad3c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 61 deletions.
25 changes: 9 additions & 16 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
expect,
headerToMap,
hexToArray,
replaceZeroBytesWithSymbol,
} from './utils';
import { ParsedTranscriptData, PresentationJSON } from './types';

Expand Down Expand Up @@ -305,12 +306,12 @@ export class Presentation {
constructor(
params:
| {
attestationHex: string;
secretsHex: string;
notaryUrl?: string;
websocketProxyUrl?: string;
reveal?: Reveal;
}
attestationHex: string;
secretsHex: string;
notaryUrl?: string;
websocketProxyUrl?: string;
reveal?: Reveal;
}
| string,
) {
if (typeof params === 'string') {
Expand Down Expand Up @@ -496,19 +497,11 @@ export class Transcript {
}

recv(redactedSymbol = '*') {
return this.#recv.reduce((recv: string, num) => {
recv =
recv + (num === 0 ? redactedSymbol : Buffer.from([num]).toString());
return recv;
}, '');
return replaceZeroBytesWithSymbol(this.#recv, redactedSymbol);
}

sent(redactedSymbol = '*') {
return this.#sent.reduce((sent: string, num) => {
sent =
sent + (num === 0 ? redactedSymbol : Buffer.from([num]).toString());
return sent;
}, '');
return replaceZeroBytesWithSymbol(this.#sent, redactedSymbol);
}

text = (redactedSymbol = '*') => {
Expand Down
106 changes: 61 additions & 45 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,56 @@ import { Buffer } from 'buffer';

type Stack =
| {
type: 'object';
symbol: string;
range: [number, number];
id: number;
}
type: 'object';
symbol: string;
range: [number, number];
id: number;
}
| {
type: 'array';
symbol: string;
range: [number, number];
data: string;
id: number;
}
type: 'array';
symbol: string;
range: [number, number];
data: string;
id: number;
}
| {
type: 'object_key';
symbol: string;
range: [number, number];
data: string;
path: string;
id: number;
objectId: number;
}
type: 'object_key';
symbol: string;
range: [number, number];
data: string;
path: string;
id: number;
objectId: number;
}
| {
type: 'object_value';
symbol: string;
range: [number, number];
data: string;
id: number;
keyId: number;
objectId: number;
}
type: 'object_value';
symbol: string;
range: [number, number];
data: string;
id: number;
keyId: number;
objectId: number;
}
| {
type: 'object_value_string';
symbol: string;
range: [number, number];
data: string;
path: string;
id: number;
objectId: number;
valueId: number;
}
type: 'object_value_string';
symbol: string;
range: [number, number];
data: string;
path: string;
id: number;
objectId: number;
valueId: number;
}
| {
type: 'object_value_number';
symbol: string;
range: [number, number];
data: string;
path: string;
id: number;
objectId: number;
valueId: number;
};
type: 'object_value_number';
symbol: string;
range: [number, number];
data: string;
path: string;
id: number;
objectId: number;
valueId: number;
};

type Commitment = {
name?: string;
Expand Down Expand Up @@ -292,6 +292,22 @@ export function processJSON(str: string): Commitment[] {
}));
}

export function replaceZeroBytesWithSymbol(buf: number[], symbol: string) {
const redactedSymbolBytes = [...Buffer.from(symbol, 'utf-8')];

let transformedBuffer: number[] = []

buf.forEach((num) => {
if (num === 0) {
transformedBuffer = transformedBuffer.concat(redactedSymbolBytes);
} else {
transformedBuffer.push(num);
}
})

return transformedBuffer
}

export function processTranscript(transcript: string): ParsedTranscriptData {
// const commitments: Commitment[] = [];
const returnVal: ParsedTranscriptData = {
Expand Down

0 comments on commit c93ad3c

Please sign in to comment.