Creating a method to write the hashes to the blockchain

Next, let's look at the hashFile method. The hashFile method is called after the user submits the directory path to be secured for hashing and writing the directory components and parameters to the blockchain:

  1. We first capture the local state and methods of the app from the this object. We also fetch the directory path submitted by the user and store it in the DirPath variable:
hashFile = () => {

let app = this;
var DirPath = this.state.fields.DirPath;
  1. Next, we call the hashwrite API in our backend server with the directory path (DirPath) as the input parameter:
fetch('http://localhost:3600/api/hashwrite', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
DirPath: DirPath})
}).then(function(response,error) {
  1. Next, we check for a successful response from the hashwrite services:

if(response)
{
return response.json();
}
else
{
console.log(error);
}
}).then(function(data){
  1. The data in the response JSON object is parsed to fetch the DirPath, the list of monitored files, the MTH, the FTH, and the time when the hash was captured (modtime):
app.setState({
DirPath: DirPath,
status: 'Idle',
files : data.files,
MTH : data.MTH,
FTH: data.FTH,
modtime: data.modtime
});
});
}

The following app state variables are set:

    • DirPath: Directory root path
    • status: Current state of the app
    • files: Array with the list of files
    • MTH: MTH of the files in the directory path
    • FTH: FTH of the directory
    • modtime: Time when hashes were recorded in the blockchain

That wraps up our hashFile method. Let's move on to the next method, which we will use to compare the current last modified time and the file tree structure of the secured directory with the values stored in the blockchain.

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

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