import { get , omitBy , sortBy } from "lodash" ; import { keccak256 } from "js-sha3" ; import { flatten } from "../shared/serialize/flatten" ; import { OpenAttestationDocument } from "../__generated__/schema.2.0" ; import { SchematisedDocument } from "./types" ; const isKeyOrValueUndefined = ( value , key ) => value === undefined || key === undefined ; export const flattenHashArray = ( data ) => { const flattenedData = omitBy ( flatten ( data ) , isKeyOrValueUndefined ) ; return Object . keys ( flattenedData ) . map ( ( k ) => { const obj = { } ; obj [ k ] = flattenedData [ k ] ; return keccak256 ( JSON . stringify ( obj ) ) ; } ) ; } ; export const digestDocument = < T extends OpenAttestationDocument = OpenAttestationDocument > ( document ) => { const hashedDataArray = get ( document , "privacy.obfuscatedData" , [ ] ) ; const unhashedData = get ( document , "data" ) ; const hashedUnhashedDataArray = flattenHashArray ( unhashedData ) ; const combinedHashes = hashedDataArray . concat ( hashedUnhashedDataArray ) ; const sortedHashes = sortBy ( combinedHashes ) ; return keccak256 ( JSON . stringify ( sortedHashes ) ) ; } ;