src/blockchain/merkle.js
function recursive (node, depth, index) {
    let hashLeft
    let hashRight
    let summingBuffer

    // case with single node in tree
    if (depth === 0 && node.val !== undefined) {
      return getHash(node.val, depth, index * 2)
    }

    if (node.left === undefined) {
      throw new Error('Left node is missed.')
    }
    if (typeof node.left === 'string') {
      if (!validateHexadecimal(node.left)) {
        throw new TypeError('Tree element of wrong type is passed. Hexadecimal expected.')
      }
      hashLeft = node.left
    } else {
      if (!isObject(node.left)) {
        throw new TypeError('Invalid type of left node.')
      }
      if (node.left.val !== undefined) {
        hashLeft = getHash(node.left.val, depth, index * 2)
      } else {
        hashLeft = recursive(node.left, depth + 1, index * 2)
      }
    }

    if (depth === 0) {
      rootBranch = 'right'
    }

    if (node.right !== undefined) {
      if (typeof node.right === 'string') {
        if (!validateHexadecimal(node.right)) {
          throw new TypeError('Tree element of wrong type is passed. Hexadecimal expected.')
        }
        hashRight = node.right
      } else {
        if (!isObject(node.right)) {
          throw new TypeError('Invalid type of right node.')
        }

        if (node.right.val !== undefined) {
          hashRight = getHash(node.right.val, depth, index * 2 + 1)
        } else {
          hashRight = recursive(node.right, depth + 1, index * 2 + 1)
        }
      }

      summingBuffer = new Uint8Array(64)
      summingBuffer.set(hexadecimalToUint8Array(hashLeft))
      summingBuffer.set(hexadecimalToUint8Array(hashRight), 32)
      return hash(summingBuffer)
    }

    if (depth === 0 || rootBranch === 'left') {
      throw new Error('Right leaf is missed in left branch of tree.')
    }
    summingBuffer = hexadecimalToUint8Array(hashLeft)
    return hash(summingBuffer)
  }
function recursive (node, depth, index) {
    let hashLeft
    let hashRight
    let summingBuffer

    // case with single node in tree
    if (depth === 0 && node.val !== undefined) {
      return getHash(node.val, depth, index * 2)
    }

    if (node.left === undefined) {
      throw new Error('Left node is missed.')
    }
    if (typeof node.left === 'string') {
      if (!validateHexadecimal(node.left)) {
        throw new TypeError('Tree element of wrong type is passed. Hexadecimal expected.')
      }
      hashLeft = node.left
    } else {
      if (!isObject(node.left)) {
        throw new TypeError('Invalid type of left node.')
      }
      if (node.left.val !== undefined) {
        hashLeft = getHash(node.left.val, depth, index * 2)
      } else {
        hashLeft = recursive(node.left, depth + 1, index * 2)
      }
    }

    if (depth === 0) {
      rootBranch = 'right'
    }

    if (node.right !== undefined) {
      if (typeof node.right === 'string') {
        if (!validateHexadecimal(node.right)) {
          throw new TypeError('Tree element of wrong type is passed. Hexadecimal expected.')
        }
        hashRight = node.right
      } else {
        if (!isObject(node.right)) {
          throw new TypeError('Invalid type of right node.')
        }

        if (node.right.val !== undefined) {
          hashRight = getHash(node.right.val, depth, index * 2 + 1)
        } else {
          hashRight = recursive(node.right, depth + 1, index * 2 + 1)
        }
      }

      summingBuffer = new Uint8Array(64)
      summingBuffer.set(hexadecimalToUint8Array(hashLeft))
      summingBuffer.set(hexadecimalToUint8Array(hashRight), 32)
      return hash(summingBuffer)
    }

    if (depth === 0 || rootBranch === 'left') {
      throw new Error('Right leaf is missed in left branch of tree.')
    }
    summingBuffer = hexadecimalToUint8Array(hashLeft)
    return hash(summingBuffer)
  }
function recursive ( node , depth , index ) { let hashLeft let hashRight let summingBuffer // case with single node in tree if ( depth === 0 && node . val !== undefined ) { return getHash ( node . val , depth , index * 2 ) } if ( node . left === undefined ) { throw new Error ( 'Left node is missed.' ) } if ( typeof node . left === 'string' ) { if ( ! validateHexadecimal ( node . left ) ) { throw new TypeError ( 'Tree element of wrong type is passed. Hexadecimal expected.' ) } hashLeft = node . left } else { if ( ! isObject ( node . left ) ) { throw new TypeError ( 'Invalid type of left node.' ) } if ( node . left . val !== undefined ) { hashLeft = getHash ( node . left . val , depth , index * 2 ) } else { hashLeft = recursive ( node . left , depth + 1 , index * 2 ) } } if ( depth === 0 ) { rootBranch = 'right' } if ( node . right !== undefined ) { if ( typeof node . right === 'string' ) { if ( ! validateHexadecimal ( node . right ) ) { throw new TypeError ( 'Tree element of wrong type is passed. Hexadecimal expected.' ) } hashRight = node . right } else { if ( ! isObject ( node . right ) ) { throw new TypeError ( 'Invalid type of right node.' ) } if ( node . right . val !== undefined ) { hashRight = getHash ( node . right . val , depth , index * 2 + 1 ) } else { hashRight = recursive ( node . right , depth + 1 , index * 2 + 1 ) } } summingBuffer = new Uint8Array ( 64 ) summingBuffer . set ( hexadecimalToUint8Array ( hashLeft ) ) summingBuffer . set ( hexadecimalToUint8Array ( hashRight ) , 32 ) return hash ( summingBuffer ) } if ( depth === 0 || rootBranch === 'left' ) { throw new Error ( 'Right leaf is missed in left branch of tree.' ) } summingBuffer = hexadecimalToUint8Array ( hashLeft ) return hash ( summingBuffer ) }
Recursive tree traversal function
@param {Object} node
@param {number} depth
@param {number} index
@returns {string}
Recursive tree traversal function