import { OpenAttestationDocument } from "../__generated__/schema.3.0" ; import { toBuffer } from "../shared/utils" ; import { WrappedDocument } from "./types" ; import { cloneDeep , get , unset , pick } from "lodash" ; import { decodeSalt , encodeSalt } from "./salt" ; import { traverseAndFlatten } from "./traverseAndFlatten" ; const obfuscate = ( _data , fields ) => { const data = cloneDeep ( _data ) ; const fieldsAsArray = ( [ ] as string [ ] ) . concat ( fields ) ; const fieldsToRemove = traverseAndFlatten ( pick ( data , fieldsAsArray ) , { iteratee : ( { path } ) => path , } ) ; const salts = decodeSalt ( data . proof . salts ) ; const obfuscatedData = fieldsToRemove . map ( ( field ) => { const value = get ( data , field ) ; const salt = salts . find ( ( s ) => s . path === field ) ; if ( ! salt ) { throw new Error ( `Salt not found for ${ field }` ) ; } return toBuffer ( { [ salt . path ] : `${ salt . value }:${ value }` } ) . toString ( "hex" ) ; } ) ; fieldsAsArray . forEach ( ( field ) => unset ( data , field ) ) ; data . proof . salts = encodeSalt ( salts . filter ( ( s ) => ! fieldsToRemove . includes ( s . path ) ) ) ; return { data , obfuscatedData , } ; } ; export const obfuscateVerifiableCredential = ( document , fields ) : WrappedDocument < OpenAttestationDocument > => { const { data , obfuscatedData } = obfuscate ( document , fields ) ; const currentObfuscatedData = document . proof . privacy . obfuscated ; const newObfuscatedData = currentObfuscatedData . concat ( obfuscatedData ) ; return { ... data , proof : { ... data . proof , privacy : { ... data . proof . privacy , obfuscated : newObfuscatedData , } , } , } ; } ;