Writing the DocsApp smart contract 

Let's write our DocsApp chaincode:

  1. Create a new file called DocsApp.js. We start writing the contract by declaring the dependent fabric-contract-api library, which is used to define our contract object:
const { Contract } = require('fabric-contract-api');
class docsapp extends Contract {
  1. Next, we'll write the addDocHash method. The addDocHash method will add a new document's hash signature to the Hyperledger Fabric network. It takes PathHash (the hash of the file path of the document) and the DocHash (the hash of the document). The ctx object is the context object that is used to index information in the ledger. We also set a console.info statement to print a checkpoint to the console indicating the start of the DocHash method:
async addDocHash(ctx, PathHash, DocHash) {
console.info('============= START : Register Document Hash ===========');

The document object is defined next. It takes  DocHash and PathHash as input parameters. It also sets the docType parameter equal to the document flag:

const document = {
DocHash,
PathHash,
docType: 'document'
};
  1. Lastly, we use putState to add our document object to the blockchain ledger as a JSON object and print a message on the console to indicate the end of the addDocHash method:
await ctx.stub.putState(PathHash,Buffer.from(JSON.stringify(document)));
console.info('============= END : Register Document Hash===========');
}
  1. The addParamHash method adds the MTH (the hash of the array of the last-modified time of all the documents in a secured directory) and the FTH (the hash of the file tree path of all the secured documents in the secured document). It takes the context object, the MTH, the FTH, and the timestamp of when the hash was calculated:
async addPathHash(ctx, Time, MTimeHash, TreeHash) {
console.info('============= START : Register M Time Hash ===========');

const TH = {
Time,
MTimeHash,
TreeHash,
docType: 'TimeHash'
};

The TH object captures the time, the MTH, and the FTH. The method then calls putState to write the TH object in the blockchain ledger. The timestamp acts as the context for the ledger entry; it'll be used to retrieve entries from the blockchain ledger:

await ctx.stub.putState(Time,Buffer.from(JSON.stringify(TH)));
console.info('============= END : Register Time Hash ===========');
}

The queryDocHash method queries and retrieves document hashes stored in the blockchain and indexed by  PathHash. It takes the file path hash as input and returns the last recorded DocHash from the blockchain state database:

 async queryDocHash(ctx, PathHash) {
const DocHashBytes = await ctx.stub.getState(PathHash); // get the document hash from chaincode state

We use the getState API to get the document hash linked to PathHash from the blockchain state database.

The method will return the marshaled JSON string received from the state database as a string object if it finds a linked document object to the input PathHash:

if (!DocHashBytes || DocHashBytes.length === 0) {
throw new Error('Invalid ${PathHash}.Document not registered');
}
console.log(DocHashBytes.toString());
return DocHashBytes.toString();
}
  1. Similarly, we write the queryParamHash method, which will retrieve the last MTH and the FTH stored at the blockchain state database using the timestamp:
async queryPathHash(ctx, Time) {
const TimeHashBytes = await ctx.stub.getState(Time); // get the document hash from chaincode state

We use the getState API to get the MTH and FTH hash linked to a timestamp from the blockchain state database.

The method will return the marshaled JSON string received from the state database as a string object if it finds a linked document object to the input timestamp:

if (!TimeHashBytes || TimeHashBytes.length === 0) {
throw new Error('Invalid ${Time}. Time not registered');
}
console.log(TimeHashBytes.toString());
return TimeHashBytes.toString();
}
}

module.exports = docsapp;

This brings us to the end of our DocsApp chaincode contract. Now, let's install it in the nodes and deploy it.

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

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