Building a method to read MTH and FTH from the blockchain

The pRead() method fetches the last MTH and FTH from the blockchain and compares them with the current MTH (ModTH) and FTH (FileTH): 

  1. It takes the time at which the hash has recorded on the blockchain (dateTime), the current MTH of the directory (ModTH), and the current FTH of the directory (FileTH) as input parameters.
  2. We start the pRead() method by first defining the wallet object and connecting to the gateway in a similar way to the last two functions:
async function pRead (dateTime,ModTH,FileTH) {

try {

const wallet = new FileSystemWallet('/home/ishan/docsapp/wallet');
const userExists = await wallet.exists('user1');
if (!userExists) {
console.log('An identity for the user "user1" does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
return;
}

const gateway = new Gateway();
await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });
const network = await gateway.getNetwork('mychannel');
const contract = network.getContract('docsapp');

The identity that's used for connecting is user 1. The wallet object is set to the wallet we created for DocsApp. The gateway connects to the mychannel channel and the docsapp chaincode.

  1. Next, we use the evaluateTransaction method to call the read-only queryPathHash method in our docsapp chaincode. You might remember from the previous section that the queryPathHash method in the docsapp chaincode fetches the MTH and the FTH, which are indexed by the timestamp at which they were recorded:
const resultFT = await contract.evaluateTransaction('queryPathHash',dateTime);
  1. The MTH and FTH returned by the queryPathHash method are captured in the resultFT variable:
var MTH = resultFT.toString('ascii',14,78);
var FTH = resultFT.toString('ascii',115,179);
console.log("Transaction has been evaluated");
await gateway.disconnect();
  1. The MTH and FTH are fetched from the blockchain and stored in the MTH and FTH variables. Since the result is returned in marshaled JSON format, we need to slice the result and separate the MTH and the FTH from the resultFT variable. The gateway is disconnected and a message is printed to the console.
  2. Next, we verify whether the hashes fetched from the blockchain match the hash that is calculated and sent as an input parameter to the pRead() method:
var result;

if(MTH != ModTH || FTH != FileTH)
{
result = 'Tampered'
}
else
{
result = 'Not Tampered'
}

return result;
  1. If the hashes match, then this indicates that the directory has not been tampered with. If they do not match, then this indicates that the file directory has been tampered with. The result (tampered/not tampered) is returned to the requester.
  1. A catch statement is added to catch any errors during transaction evaluation:
} catch (error) {
console.error(`Failed to submit transaction: ${error}`);
return error;
process.exit(1);
}
};

This brings us to the end of our walkthrough of the pRead() method. The pRead() method is called when our application is verifying whether the directory under observation has been tampered with. We'll look at the bRead() method next, which fetches individual document hashes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.253.170