How to use the safe-buffer.Buffer.compare function in safe-buffer

To help you get started, we’ve selected a few safe-buffer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github LayerXcom / plasma-mvp-vyper / test / helpers / fixedMerkleTree.js View on Github external
getPlasmaProof(leaf) {
        let index = -1
        for (let i = 0; i < this.leaves.length; i++) {
            if (Buffer.compare(leaf, this.leaves[i]) === 0) {
                index = i
            }
        }

        const proof = []
        if (index <= this.getLeaves().length) {
            let siblingIndex
            for (let i = 0; i < this.layers.length - 1; i++) {
                if (index % 2 === 0) {
                    siblingIndex = index + 1
                } else {
                    siblingIndex = index - 1
                }
                index = parseInt(index / 2)
                proof.push(this.layers[i][siblingIndex])
            }
github Hanul / UPPERCASE / UPPERCASE-DB / node_modules / mongodb / lib / core / topologies / replset_state.js View on Github external
function compareObjectIds(id1, id2) {
  var a = Buffer.from(id1.toHexString(), 'hex');
  var b = Buffer.from(id2.toHexString(), 'hex');

  if (a === b) {
    return 0;
  }

  if (typeof Buffer.compare === 'function') {
    return Buffer.compare(a, b);
  }

  var x = a.length;
  var y = b.length;
  var len = Math.min(x, y);

  for (var i = 0; i < len; i++) {
    if (a[i] !== b[i]) {
      break;
    }
  }

  if (i !== len) {
    x = a[i];
    y = b[i];
  }
github voltairelabs / plasma / src / lib / fixed-merkle-tree.js View on Github external
getPlasmaProof(leaf) {
    let index = -1
    for (let i = 0; i < this.leaves.length; i++) {
      if (Buffer.compare(leaf, this.leaves[i]) === 0) {
        index = i
      }
    }

    const proof = []
    if (index <= this.getLeaves().length) {
      let siblingIndex
      for (let i = 0; i < this.layers.length - 1; i++) {
        if (index % 2 === 0) {
          siblingIndex = index + 1
        } else {
          siblingIndex = index - 1
        }
        index = parseInt(index / 2)
        proof.push(this.layers[i][siblingIndex])
      }
github voltairelabs / plasma / src / chain / block.js View on Github external
async validate(chain) {
    const p = []
    for (let i = 0; i < this.transactions.length; i++) {
      const tx = this.transactions[i]
      p.push(tx.validate(chain))
    }

    const results = await Promise.all(p)
    if (!results.every(r => r)) {
      return false
    }

    const merkleHashes = this.transactions.map(tx => tx.merkleHash())
    const tree = new FixedMerkleTree(16, merkleHashes)
    if (Buffer.compare(tree.getRoot(), this.header.root) !== 0) {
      return false
    }

    return true
  }
github mongodb-js / mongodb-core / lib / topologies / replset_state.js View on Github external
function compareObjectIds(id1, id2) {
  var a = Buffer.from(id1.toHexString(), 'hex');
  var b = Buffer.from(id2.toHexString(), 'hex');

  if (a === b) {
    return 0;
  }

  if (typeof Buffer.compare === 'function') {
    return Buffer.compare(a, b);
  }

  var x = a.length;
  var y = b.length;
  var len = Math.min(x, y);

  for (var i = 0; i < len; i++) {
    if (a[i] !== b[i]) {
      break;
    }
  }

  if (i !== len) {
    x = a[i];
    y = b[i];
  }
github didinj / node-express-mongoose-passport-jwt-rest-api-auth / node_modules / mongodb-core / lib / topologies / replset_state.js View on Github external
function compareObjectIds(id1, id2) {
  var a = Buffer.from(id1.toHexString(), 'hex');
  var b = Buffer.from(id2.toHexString(), 'hex');

  if (a === b) {
    return 0;
  }

  if (typeof Buffer.compare === 'function') {
    return Buffer.compare(a, b);
  }

  var x = a.length;
  var y = b.length;
  var len = Math.min(x, y);

  for (var i = 0; i < len; i++) {
    if (a[i] !== b[i]) {
      break;
    }
  }

  if (i !== len) {
    x = a[i];
    y = b[i];
  }
github voltairelabs / plasma / src / lib / fixed-merkle-tree.js View on Github external
getProof(leaf, index) {
    const proof = []

    if (typeof index !== 'number') {
      index = -1
      for (let i = 0; i < this.leaves.length; i++) {
        if (Buffer.compare(leaf, this.leaves[i]) === 0) {
          index = i
        }
      }
    }

    if (index <= -1) {
      return []
    }

    for (let i = 0; i < this.layers.length; i++) {
      const layer = this.layers[i]
      const isRightNode = index % 2
      const pairIndex = isRightNode ? index - 1 : index + 1

      if (pairIndex < layer.length) {
        proof.push({
github maticnetwork / matic.js / src / helpers / merkle-tree.js View on Github external
return false
    }

    let hash = value
    for (let i = 0; i < proof.length; i++) {
      const node = proof[i]
      if (index % 2 === 0) {
        hash = sha3(Buffer.concat([hash, node]))
      } else {
        hash = sha3(Buffer.concat([node, hash]))
      }

      index = parseInt(index / 2)
    }

    return Buffer.compare(hash, root) === 0
  }
}
github voltairelabs / plasma / src / lib / fixed-merkle-tree.js View on Github external
let hash = targetNode

    if (!Array.isArray(proof) || !proof.length || !targetNode || !root) {
      return false
    }

    for (let i = 0; i < proof.length; i++) {
      const node = proof[i]
      const isLeftNode = node.position === 'left'
      const buffers = []
      buffers.push(hash)
      buffers[isLeftNode ? 'unshift' : 'push'](node.data)
      hash = sha3(Buffer.concat(buffers))
    }

    return Buffer.compare(hash, root) === 0
  }
}