Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. var express = require('express');
  2. var bodyParser = require('body-parser');
  3.  
  4. var app = express();
  5. app.use(bodyParser.json());
  6.  
  7. // Setting for Hyperledger Fabric
  8. const { FileSystemWallet, Gateway } = require('fabric-network');
  9. const path = require('path');
  10. const ccpPath = path.resolve(__dirname, '.', 'connection-org1.json');
  11.  
  12.  
  13. app.get('/api/queryallcars', async function (req, res) {
  14. try {
  15.  
  16. // Create a new file system based wallet for managing identities.
  17. const walletPath = path.join(process.cwd(), 'wallet');
  18. const wallet = new FileSystemWallet(walletPath);
  19. console.log(`Wallet path: ${walletPath}`);
  20.  
  21. // Check to see if we've already enrolled the user.
  22. const userExists = await wallet.exists('user1');
  23. if (!userExists) {
  24. console.log('An identity for the user "user1" does not exist in the wallet');
  25. console.log('Run the registerUser.js application before retrying');
  26. return;
  27. }
  28.  
  29. // Create a new gateway for connecting to our peer node.
  30. const gateway = new Gateway();
  31. await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: false } });
  32.  
  33. // Get the network (channel) our contract is deployed to.
  34. const network = await gateway.getNetwork('mychannel');
  35.  
  36. // Get the contract from the network.
  37. const contract = network.getContract('fabcar');
  38.  
  39. // Evaluate the specified transaction.
  40. // queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4')
  41. // queryAllCars transaction - requires no arguments, ex: ('queryAllCars')
  42. const result = await contract.evaluateTransaction('queryAllCars');
  43. console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
  44. res.status(200).json({response: result.toString()});
  45.  
  46. } catch (error) {
  47. console.error(`Failed to evaluate transaction: ${error}`);
  48. res.status(500).json({error: error});
  49. process.exit(1);
  50. }
  51. });
  52.  
  53.  
  54. app.get('/api/query/:car_index', async function (req, res) {
  55. try {
  56.  
  57. // Create a new file system based wallet for managing identities.
  58. const walletPath = path.join(process.cwd(), 'wallet');
  59. const wallet = new FileSystemWallet(walletPath);
  60. console.log(`Wallet path: ${walletPath}`);
  61.  
  62. // Check to see if we've already enrolled the user.
  63. const userExists = await wallet.exists('user1');
  64. if (!userExists) {
  65. console.log('An identity for the user "user1" does not exist in the wallet');
  66. console.log('Run the registerUser.js application before retrying');
  67. return;
  68. }
  69.  
  70. // Create a new gateway for connecting to our peer node.
  71. const gateway = new Gateway();
  72. await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: false } });
  73.  
  74. // Get the network (channel) our contract is deployed to.
  75. const network = await gateway.getNetwork('mychannel');
  76.  
  77. // Get the contract from the network.
  78. const contract = network.getContract('fabcar');
  79.  
  80. // Evaluate the specified transaction.
  81. // queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4')
  82. // queryAllCars transaction - requires no arguments, ex: ('queryAllCars')
  83. const result = await contract.evaluateTransaction('queryCar', req.params.car_index);
  84. console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
  85. res.status(200).json({response: result.toString()});
  86.  
  87. } catch (error) {
  88. console.error(`Failed to evaluate transaction: ${error}`);
  89. res.status(500).json({error: error});
  90. process.exit(1);
  91. }
  92. });
  93.  
  94. app.post('/api/addcar/', async function (req, res) {
  95. try {
  96.  
  97. // Create a new file system based wallet for managing identities.
  98. const walletPath = path.join(process.cwd(), 'wallet');
  99. const wallet = new FileSystemWallet(walletPath);
  100. console.log(`Wallet path: ${walletPath}`);
  101.  
  102. // Check to see if we've already enrolled the user.
  103. const userExists = await wallet.exists('user1');
  104. if (!userExists) {
  105. console.log('An identity for the user "user1" does not exist in the wallet');
  106. console.log('Run the registerUser.js application before retrying');
  107. return;
  108. }
  109.  
  110. // Create a new gateway for connecting to our peer node.
  111. const gateway = new Gateway();
  112. await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: false } });
  113.  
  114. // Get the network (channel) our contract is deployed to.
  115. const network = await gateway.getNetwork('mychannel');
  116.  
  117. // Get the contract from the network.
  118. const contract = network.getContract('fabcar');
  119.  
  120. // Submit the specified transaction.
  121. // createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom')
  122. // changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR10', 'Dave')
  123. await contract.submitTransaction('createCar', req.body.carid, req.body.make, req.body.model, req.body.colour, req.body.owner);
  124. console.log('Transaction has been submitted');
  125. res.send('Transaction has been submitted');
  126.  
  127. // Disconnect from the gateway.
  128. await gateway.disconnect();
  129.  
  130. } catch (error) {
  131. console.error(`Failed to submit transaction: ${error}`);
  132. process.exit(1);
  133. }
  134. })
  135.  
  136. app.put('/api/changeowner/:car_index', async function (req, res) {
  137. try {
  138.  
  139. // Create a new file system based wallet for managing identities.
  140. const walletPath = path.join(process.cwd(), 'wallet');
  141. const wallet = new FileSystemWallet(walletPath);
  142. console.log(`Wallet path: ${walletPath}`);
  143.  
  144. // Check to see if we've already enrolled the user.
  145. const userExists = await wallet.exists('user1');
  146. if (!userExists) {
  147. console.log('An identity for the user "user1" does not exist in the wallet');
  148. console.log('Run the registerUser.js application before retrying');
  149. return;
  150. }
  151.  
  152. // Create a new gateway for connecting to our peer node.
  153. const gateway = new Gateway();
  154. await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: false } });
  155.  
  156. // Get the network (channel) our contract is deployed to.
  157. const network = await gateway.getNetwork('mychannel');
  158.  
  159. // Get the contract from the network.
  160. const contract = network.getContract('fabcar');
  161.  
  162. // Submit the specified transaction.
  163. // createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom')
  164. // changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR10', 'Dave')
  165. await contract.submitTransaction('changeCarOwner', req.params.car_index, req.body.owner);
  166. console.log('Transaction has been submitted');
  167. res.send('Transaction has been submitted');
  168.  
  169. // Disconnect from the gateway.
  170. await gateway.disconnect();
  171.  
  172. } catch (error) {
  173. console.error(`Failed to submit transaction: ${error}`);
  174. process.exit(1);
  175. }
  176. })
  177.  
  178. app.listen(8080);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement