app.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import 'dotenv/config'
  2. import fetch from "node-fetch"
  3. import express from "express"
  4. import bodyParser from "body-parser"
  5. const app = express();
  6. app.use(bodyParser.json());
  7. const CHAIN4ALL_SERVICE_PORT = process.env.CHAIN4ALL_SERVICE_PORT || 3000;
  8. const IROHA_API_HOST = process.env.IROHA_API_HOST || "http://localhost";
  9. const IROHA_API_PORT = process.env.IROHA_API_PORT || 5000;
  10. const IROHA_DOMAIN = process.env.IROHA_DOMAIN || "test";
  11. const IROHA_ASSET = process.env.IROHA_ASSET || "coin";
  12. const DATA_OWNER = process.env.DATA_OWNER || "admin"
  13. const PRICE_MODIFIER = process.env.PRICE_MODIFIER || 0.5;
  14. app.get("/", (req, res) => {
  15. res.send("Chain4All Blockchain service");
  16. });
  17. app.post("/price", (req, res) => {
  18. if (req.body && req.body.extent) {
  19. res.send({ price: getPrice(req.body.extent) });
  20. }
  21. else {
  22. res.status(400);
  23. throw Error(JSON.stringify({ error: { name: "Error, request has no body with \"extent\" property!" } }));
  24. }
  25. });
  26. app.get("/users/:userId/assets", async (req, res) => {
  27. let irohaResponse = await fetchUsersAssets(req.params.userId);
  28. res.status(irohaResponse.status);
  29. if (irohaResponse.headers.get("content-length") < 1) {
  30. res.send();
  31. }
  32. else {
  33. res.send(await irohaResponse.json());
  34. }
  35. });
  36. app.get("/users/:userId/assets/:assetId", async (req, res) => {
  37. let assets = (await fetchUsersAssets(req.params.userId)).assets;
  38. let parsedAssetName = "";
  39. let i = 0;
  40. for (i; i < assets.length; i++) {
  41. parsedAssetName = assets[i].assetId.split('#')[0];
  42. if (parsedAssetName === req.params.assetId) {
  43. res.status(200);
  44. res.send(assets[i]);
  45. return;
  46. }
  47. }
  48. res.status(204);
  49. res.send();
  50. });
  51. app.post("/buy", async (req, res) => {
  52. if (!req.body) {
  53. res.status(400);
  54. throw Error(JSON.stringify({ error: { name: "Error, request has no body!" } }));
  55. }
  56. if (!req.body.extent) {
  57. res.status(400);
  58. throw Error(JSON.stringify({ error: { name: "Error, request body has no \"extent\" property!" } }));
  59. }
  60. if (!req.body.user) {
  61. res.status(400);
  62. throw Error(JSON.stringify({ error: { name: "Error, request body has no \"user\" property!" } }));
  63. }
  64. let price = getPrice(req.body.extent);
  65. let irohaResponse = await forwardBuyRequest(
  66. {
  67. transfers: [
  68. {
  69. asset: IROHA_ASSET + "#" + IROHA_DOMAIN,
  70. source: req.body.user + "@" + IROHA_DOMAIN,
  71. destination: DATA_OWNER + "@" + IROHA_DOMAIN,
  72. description: "test", //TODO: compose uniform description
  73. amount: price
  74. }
  75. ]
  76. }
  77. );
  78. let data = "test - to be implemented"; //TODO implement data extraction
  79. res.status(irohaResponse.status);
  80. if(!irohaResponse.ok){
  81. res.send(irohaResponse);
  82. }
  83. let irohaResponseJson = await irohaResponse.json();
  84. console.log("tx hash: " + irohaResponseJson.transactionHash);
  85. res.send({
  86. data : data,
  87. transactionHash : irohaResponseJson.transactionHash
  88. });
  89. });
  90. async function forwardBuyRequest(body) {
  91. return await fetch(IROHA_API_HOST + ':' + IROHA_API_PORT + '/assets/transfer/',
  92. {
  93. method: 'POST',
  94. body: JSON.stringify(body),
  95. headers: { 'Content-Type': 'application/json' }
  96. }
  97. );
  98. }
  99. async function fetchUsersAssets(userId) {
  100. return await fetch(IROHA_API_HOST + ':' + IROHA_API_PORT + '/accounts/' + userId + '@' + IROHA_DOMAIN + '/assets/');
  101. }
  102. function getArea(extent) {
  103. let y1 = extent[0][1];
  104. let y4 = extent[3][1];
  105. let x1 = extent[0][0];
  106. let x2 = extent[1][0];
  107. let height = Math.abs(y1 - y4);
  108. let width = Math.abs(x1 - x2);
  109. return height * width;
  110. }
  111. function getPrice(extent) {
  112. return getArea(extent) * PRICE_MODIFIER;
  113. }
  114. function error(err, req, res, next) { //TODO: add custom Exception class
  115. console.log(err);
  116. if (!res.status) {
  117. res.status(500);
  118. }
  119. res.send(err.message);
  120. }
  121. app.use(error);
  122. app.listen(CHAIN4ALL_SERVICE_PORT, () => {
  123. console.log(`Listening at http://localhost:${CHAIN4ALL_SERVICE_PORT}`)
  124. });