function buildTree(tree) {
    if (Array.isArray(tree)) {
      const [left, leftH] = buildTree(tree[0]);
      const [right, rightH] = buildTree(tree[1]);
      const ret = [[[left[0], left[1]], rightH], [[right[0], right[1]], leftH]];
      const hWriter = TaggedHash.TAPBRANCH;
      if (leftH.compare(rightH) === 1) {
        hWriter.write(rightH);
        hWriter.write(leftH);
      } else {
        hWriter.write(leftH);
        hWriter.write(rightH);
      }
      return [ret, hWriter.finalize()];
    } else {
      const { leafVersion, script } = tree;
      const scriptBuf = new Script(script).toBuffer();
      const leafWriter = TaggedHash.TAPLEAF;
      leafWriter.writeUInt8(leafVersion);
      leafWriter.writeUInt8(scriptBuf.length);
      leafWriter.write(scriptBuf);
      const h = leafWriter.finalize();
      return [[Buffer.from([leafVersion]), scriptBuf], h];
    }
  }
Build Taproot script output
@param {PublicKey|Address} to recipient's pubKey or address
@param {Array|Object} scriptTree single leaf object OR array of leaves. leaf: { script: String, leafVersion: Integer }
@returns {Script}