4

I'm trying to verify a document from QLDB using nodejs. I have been following the Java verification example as much as I can, but I'm unable to calculate the same digest as stored on the ledger.

This is the code I have come up with. I query the proof and block hash from QLDB and then try to calculate digest in the same way as in Java example. But after concatinating the two hashes and calculating the new hash from the result I get the wrong output from crypto.createHash('sha256').update(c).digest("base64"). I have also tried using "base64" instead of "hex" with different, but also wrong result.

  const rBlock = makeReader(res.Revision.IonText);

  var block = [];

  rBlock.next();
  rBlock.stepIn();
  rBlock.next();
  while (rBlock.next() != null) {
    if (rBlock.fieldName() == 'hash') {
      block.push(Buffer.from(rBlock.byteValue()).toString('hex'));
    }
  }

  console.log(block);

  var proof = [];

  const rProof = makeReader(res.Proof.IonText);
  rProof.next();

  rProof.stepIn();
  while (rProof.next() != null) {
    proof.push(Buffer.from(rProof.byteValue()).toString('hex'));
  }

  var ph = block[0];
  var c;
  for (var i = 0; i < proof.length; i++) {
    console.log(proof[i])

    for (var j = 0; j < ph.length; j++) {
      if (parseInt(ph[j]) > parseInt(proof[i][j])){
        c = ph + proof[i];
        break;
      }
      if (parseInt(ph[j]) < parseInt(proof[i][j])){
        c = proof[i] + ph;
        break;
      }
    }

    ph = crypto.createHash('sha256').update(c).digest("hex");
    console.log(ph);
    console.log();
  }
Alko
  • 672
  • 10
  • 21

1 Answers1

2

I have figure it out. The problem was that I was converting the blobs to hex strings and hash them instead of the raw values. For anyone wanting to verify data in node, here is the bare solution:

ledgerInfo.getRevision(params).then(res => {
  console.log(res);

  const rBlock = makeReader(res.Revision.IonText);

  var ph;

  rBlock.next();
  rBlock.stepIn();
  rBlock.next();
  while (rBlock.next() != null) {
    if (rBlock.fieldName() == 'hash') {
      ph = rBlock.byteValue()
    }
  }

  var proof = [];

  const rProof = makeReader(res.Proof.IonText);
  rProof.next();

  rProof.stepIn();
  while (rProof.next() != null) {
    proof.push(rProof.byteValue());
  }

  for (var i = 0; i < proof.length; i++) {
    var c;

    if (hashComparator(ph, proof[i]) < 0) {
      c = concatTypedArrays(ph, proof[i]);
    }
    else {
      c = concatTypedArrays(proof[i], ph);
    }

    var buff = crypto.createHash('sha256').update(c).digest("hex");
    ph = Uint8Array.from(Buffer.from(buff, 'hex'));
  }

  console.log(Buffer.from(ph).toString('base64'));

}).catch(err => {
  console.log(err, err.stack)
});

function concatTypedArrays(a, b) {
  var c = new (a.constructor)(a.length + b.length);
  c.set(a, 0);
  c.set(b, a.length);
  return c;
}

function hashComparator(h1, h2) {
  for (var i = h1.length - 1; i >= 0; i--) {
    var diff = (h1[i]<<24>>24) - (h2[i]<<24>>24);
    if (diff != 0)
      return diff;
  }
  return 0;
}
Alko
  • 672
  • 10
  • 21
  • I used the exact code and I'm guessing console.log(Buffer.from(ph).toString('base64')); is the calculated digest ? – Sachin S Nov 04 '20 at 03:34