Building a function to compare the current hash signature of a file with the hash recorded in the blockchain

The bRead() function is called to compare the hash of a file in the monitored directory with the hash signature recorded previously in the blockchain. It takes the hash of the directory path of a file (p) and the current hash of the file (d) as input parameters:

  1. Let's take a look at the function. Like the pRead methods, we start bRead by defining our wallet object and gateway connection. We point the connection to the docsapp smart contract and mychannel network channel. We'll be using the   user1, to submit our evaluation transaction:
async function bRead (p,d) {

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');
  1. We'll be connecting and submitting an evaluateTransaction request to the docsapp method, queryDocHash. The queryDocHash method returns the file hash signature recorded in the blockchain. It takes the hash of the directory path of the file as input and returns the hash of the file recorded in the blockchain:
const result = await contract.evaluateTransaction('queryDocHash',p);
  1. The response is returned in marshaled JSON format. We convert the response in to a string and splice the string to get the file hash from the response:
const data = result.toString('ascii',12,76);
console.log(`Transaction has been evaluated, result is: ${result.toString('ascii',12,76)}`);

  1. Next, the method checks whether the hash we receive in the response (data) is the same as the current file hash (d) that is sent as an input parameter. If there is a mismatch, it means that the file has been tampered with. If there is not a mismatch, then this means that the file has not been tampered with:
var status; 
if (data == d)
{
console.log("File not tampered");
status = 'Not Tampered';
return status;
}
else
{
console.log("File tampered");
status= 'Tampered';

return status;
}
  1. The result (Tampered/Not Tampered) is returned to the requester in the output parameter, status.
  2. A catch statement catches any errors during transaction evaluation:
} catch (error) {
console.error(`Failed to submit transaction: ${error}`);
process.exit(1); }
};

That ends our walkthrough of the bRead() method. The bRead() method is called when we realize that our directory has been tampered with from the result of the pRead() method. It is called for each file member in the directory to identify which files have been tampered with.

Next, let's take a look at the services that our backend server will expose for our frontend React interface to consume.

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

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