How to calculate "Data Hash" before submit to the ledger in Hyperledger Fabric (SDK Node)?

300 Views Asked by At

I'm researching Hyperledger Fabric v2.2 with SDK Node v2.2 and reach a trouble that I don't known how to calculate the "data hash" before submit it to the ledger, I need it for verify between client data and ledger data.

My submit transaction chaincode function:

file: chaincode.js
...
async addNewDataToLedger(ctx, dataId, data){
    return await ctx.stub.putState(dataId, Buffer.from(data));
}

Then in my application, I have a JSON data such as

const dataSample = {a : 1, b : 2};
const transaction = contract.createTransaction('addNewDataToLedger');
const result = await transaction.submit('number1', JSON.stringify(dataSample));

After submit it to the ledger. I will receiver a "Data Hash" such as e2c07a0541f0b90ef0cea2174365719abeda50fd024468f9d7639b76cb1878fb

I guess it hash my Buffer.from(data) and something secret, because I realized that, "Data Hash" is always different even though the same input data.

And if data_hash is encrypt by data: {...} section like below block, therefore how to verify the data of client with data in the ledger is matched?

{
  header: {
    number: {}, 
    previous_hash: "e4617a6446d30628b723206d3f0a0e61308e08dc5eef502bb4c87d228c1c4d10", 
    data_hash: "38afae3941a400149c8508f7d8e4a26bf938cf60dd3a6f4be602ec829f7115fd"
  }, 
  data: { //IS 'data_hash' HASHED THIS SECTION OBJECT?
    data: [...]
  }, 
  metadata: {
    metadata: [...]
  }
}

I searched everywhere but couldn't find what "Data hash" is encoded by, and how to verify data integrity, hope you can help me, many thanks!!

1

There are 1 best solutions below

0
Dave Enyeart On

The block data_hash is not the hash over a single data key or even a single transaction.

The block data_hash is a SHA 256 hash over all the transactions in the block, as seen by the code here.