app.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import 'dotenv/config'
  2. import grpc from "grpc"
  3. import express from "express"
  4. import bodyParser from "body-parser"
  5. import basicAuth from "express-basic-auth"
  6. import {
  7. CommandService_v1Client as CommandService,
  8. QueryService_v1Client as QueryService
  9. } from 'iroha-helpers/lib/proto/endpoint_grpc_pb.js'
  10. import iroha from 'iroha-helpers'
  11. const app = express();
  12. app.use(bodyParser.json());
  13. app.use(basicAuth({
  14. users: { 'admin': 'superPasswd' },
  15. challange: true
  16. }));
  17. const IROHA_ADMIN_PRIV = "f101537e319568c765b2cc89698325604991dca57b9716b58016b253506cab70";
  18. const IROHA_ADMIN = "admin@test";
  19. const IROHA_ADDRESS = "localhost:50051";
  20. const commandService = new CommandService(IROHA_ADDRESS, grpc.credentials.createInsecure());
  21. const queryService = new QueryService(IROHA_ADDRESS, grpc.credentials.createInsecure());
  22. const CHAIN4ALL_SERVICE_PORT = process.env.CHAIN4ALL_SERVICE_PORT || 3000;
  23. const IROHA_API_HOST = process.env.IROHA_API_HOST || "http://localhost";
  24. const IROHA_API_PORT = process.env.IROHA_API_PORT || 5000;
  25. const IROHA_DOMAIN = process.env.IROHA_DOMAIN || "test";
  26. const IROHA_ASSET = process.env.IROHA_ASSET || "coin";
  27. const DATA_OWNER = process.env.DATA_OWNER || "admin"
  28. const PRICE_MODIFIER = process.env.PRICE_MODIFIER || 0.5;
  29. app.get("/", (req, res) => {
  30. res.send("Chain4All Blockchain service");
  31. });
  32. app.post("/price", (req, res) => {
  33. if (req.body && req.body.extent) {
  34. res.send({ price: getPrice(req.body.extent) });
  35. }
  36. else {
  37. res.status(400);
  38. throw Error(JSON.stringify({ error: { name: "Error, request has no body with \"extent\" property!" } }));
  39. }
  40. });
  41. app.post("/buy", async (req, res, next) => {
  42. try {
  43. if (!req.body) {
  44. res.status(400);
  45. throw Error(JSON.stringify({ error: { name: "Error, request has no body!" } }));
  46. }
  47. if (!req.body.txHash) {
  48. res.status(400);
  49. throw Error(JSON.stringify({ error: { name: "Error, request body has no \"txHash\" property!" } }));
  50. }
  51. //TODO load from headers, or something like that??
  52. if (!req.body.user) {
  53. res.status(400);
  54. throw Error(JSON.stringify({ error: { name: "Error, request body has no \"user\" property!" } }));
  55. }
  56. let txDetail = await getTransactionDetail(req.body.txHash, "kunicykd@test");
  57. if()
  58. }
  59. catch (err) {
  60. next(err);
  61. }
  62. });
  63. function isTransacationValid(){
  64. }
  65. async function getTransactionDetail(txHash, user) {
  66. let quer = await iroha.queries.getAccountTransactions({
  67. privateKey: IROHA_ADMIN_PRIV,
  68. creatorAccountId: 'admin@test',
  69. queryService,
  70. timeoutLimit: 5000
  71. }, {
  72. accountId: user,
  73. pageSize: 10,
  74. firstTxHash: txHash,
  75. ordering: {
  76. field: undefined,
  77. direction: undefined
  78. },
  79. firstTxTime: undefined,
  80. lastTxTime: undefined,
  81. firstTxHeight: 1,
  82. lastTxHeight: 3
  83. });
  84. //TODO find better way to look for transferAssets command in transaction
  85. return quer.transactionsList[0].payload.reducedPayload.commandsList[0].transferAsset;
  86. }
  87. function getArea(extent) {
  88. let y1 = extent[0][1];
  89. let y4 = extent[3][1];
  90. let x1 = extent[0][0];
  91. let x2 = extent[1][0];
  92. let height = Math.abs(y1 - y4);
  93. let width = Math.abs(x1 - x2);
  94. return height * width;
  95. }
  96. function getPrice(extent) {
  97. return getArea(extent) * PRICE_MODIFIER;
  98. }
  99. function errorMiddleware(err, req, res, next) { //TODO: add custom Exception class
  100. console.log(err);
  101. res.status(500);
  102. res.send(err.message);
  103. }
  104. app.use(errorMiddleware);
  105. app.listen(CHAIN4ALL_SERVICE_PORT, () => {
  106. console.log(`Listening at http://localhost:${CHAIN4ALL_SERVICE_PORT}`)
  107. });