github.com/simranvc/fabric-ca@v0.0.0-20191030094829-acc364294dde/tmp1/hyperledger/org1/peer1/assets/chaincode/contract-api/lib/contractApi.js (about) 1 /* 2 * SPDX-License-Identifier: Apache-2.0 3 */ 4 5 'use strict'; 6 7 const shim = require('fabric-shim'); 8 const util = require('util'); 9 10 let contractApi = class { 11 async Init(stub) { 12 return stub.putState('dummyKey', Buffer.from('dummyValue')) 13 .then(() => { 14 console.info('Chaincode instantiation is successful'); 15 return shim.success(); 16 }, () => { 17 return shim.error(); 18 }); 19 } 20 21 async Invoke(stub) { 22 console.info('Transaction ID: ' + stub.getTxID()); 23 console.info(util.format('Args: %j', stub.getArgs())); 24 25 let ret = stub.getFunctionAndParameters(); 26 console.info(ret); 27 28 let method = this[ret.fcn]; 29 if (!method) { 30 console.log('no function of name:' + ret.fcn + ' found'); 31 throw new Error('Received unknown function ' + ret.fcn + ' invocation'); 32 } 33 try { 34 console.info('Calling function: ' + ret.fcn); 35 let payload = await method(stub, ret.params, this); 36 return shim.success(payload); 37 } catch (err) { 38 console.log(err); 39 return shim.error(err); 40 } 41 } 42 43 async createContract(stub, args) { 44 let uniqueID = args[0]; 45 let Transaction = { 46 encryptionKey: args[1], 47 docType: 'EdexaContract', 48 hash: args[2], 49 name: args[3], 50 note: args[4], 51 fileType: args[5] 52 }; 53 54 return stub.putState(uniqueID, Buffer.from(JSON.stringify(Transaction))) 55 .then(() => { 56 console.info('Chaincode instantiation is successful'); 57 }, () => { 58 throw new Error('Error while commiting record into ledger'); 59 }); 60 } 61 }; 62 63 shim.start(new contractApi());