import { cloneDeep , pick , unset } from "lodash" ; import { flatten } from "../shared/serialize/flatten" ; import { toBuffer } from "../shared/utils" ; import { OpenAttestationDocument } from "../__generated__/schema.2.0" ; import { WrappedDocument } from "./types" ; export const obfuscateData = ( _data , fields ) => { const data = cloneDeep ( _data ) ; const fieldsToRemove = Array . isArray ( fields ) ? fields : [ fields ] ; const dataToObfuscate = flatten ( pick ( data , fieldsToRemove ) ) ; const obfuscatedData = Object . keys ( dataToObfuscate ) . map ( ( k ) => { const obj = { } ; obj [ k ] = dataToObfuscate [ k ] ; return toBuffer ( obj ) . toString ( "hex" ) ; } ) ; fieldsToRemove . forEach ( ( path ) => { unset ( data , path ) ; } ) ; return { data , obfuscatedData , } ; } ; export const obfuscateDocument = < T extends OpenAttestationDocument = OpenAttestationDocument > ( document , fields ) : WrappedDocument < T > => { const existingData = document . data ; const { data , obfuscatedData } = obfuscateData ( existingData , fields ) ; const currentObfuscatedData = document ?. privacy ?. obfuscatedData ?? [ ] ; const newObfuscatedData = currentObfuscatedData . concat ( obfuscatedData ) ; return { ... document , data , privacy : { ... document . privacy , obfuscatedData : newObfuscatedData , } , } ; } ;