diff --git a/src/lib.ts b/src/lib.ts index 33d5e1a..32479a9 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -25,6 +25,7 @@ import { expect, headerToMap, hexToArray, + replaceZeroBytesWithSymbol, } from './utils'; import { ParsedTranscriptData, PresentationJSON } from './types'; @@ -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') { @@ -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 = '*') => { diff --git a/src/utils.ts b/src/utils.ts index 21c902b..3817cf3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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; @@ -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 = {