import { hash } from '../../../lisk-cryptography/dist-node' ; import { LEAF_PREFIX } from './constants' ; import { calculatePathNodes , ROOT_INDEX } from './utils' ; export const calculateRootFromUpdateData = ( updateData , proof ) : Buffer => { const { indexes , size , siblingHashes } = proof ; if ( size === 0 || indexes . length === 0 ) { throw new Error ( 'Invalid proof.' ) ; } if ( updateData . length !== indexes . length ) { throw new Error ( "Amount of update data doesn't match amount of indexes" ) ; } const updateHashes = [ ] ; for ( const data of updateData ) { const leafValueWithoutNodeIndex = Buffer . concat ( [ LEAF_PREFIX , data ] , LEAF_PREFIX . length + data . length ) ; const leafHash = hash ( leafValueWithoutNodeIndex ) ; updateHashes . push ( leafHash ) ; } const calculatedTree = calculatePathNodes ( updateHashes , size , indexes , siblingHashes ) ; const calculatedRoot = calculatedTree . get ( ROOT_INDEX ) ; return calculatedRoot as Buffer ; } ;