bc-info.service.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpHeaders } from '@angular/common/http';
  3. import {
  4. CommandService_v1Client as CommandService,
  5. QueryService_v1Client as QueryService
  6. } from 'iroha-helpers/lib/proto/endpoint_pb_service'
  7. import { queries, commands } from 'iroha-helpers'
  8. @Injectable({ providedIn: 'root' })
  9. export class BcInfoService {
  10. BASE_URL = 'https://chain4all.lesprojekt.cz' as const;
  11. CHAIN4ALL_SERVICE_URL = 'http://localhost:3000' as const;
  12. IROHA_ADDRESS = "http://localhost:8080" as const;
  13. ASSET = "testcoin#test" as const;
  14. ADMIN_USER = "admin@test" as const;
  15. commandService: CommandService;
  16. queryService: QueryService;
  17. basicAuthHeaders: HttpHeaders;
  18. user = 'kunickyd@test';
  19. // userPrivateKey = 'ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8';
  20. userBalance = -1;
  21. price = -1;
  22. paymentHash = "";
  23. dataUrl = "";
  24. extent = [ //bottom left and top right point should be enough
  25. [48.5, 17],
  26. [49, 17],
  27. [49, 16.5],
  28. [48.5, 16.5]
  29. ];
  30. private _requestInProcess = false;
  31. constructor(public httpClient: HttpClient) {
  32. this.commandService = new CommandService(this.IROHA_ADDRESS);
  33. this.queryService = new QueryService(this.IROHA_ADDRESS);
  34. this.getUserBalance();
  35. this.getPrice(this.extent).then(price => this.price = price);
  36. this.basicAuthHeaders = new HttpHeaders();
  37. this.basicAuthHeaders.append("Authorization", "Basic " + Buffer.from("admin:password", 'base64'));
  38. }
  39. async getUserBalance(): Promise<any> {
  40. if (this._requestInProcess) {
  41. return;
  42. }
  43. this.userBalance = await this.getUserBalanceFromIroha(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this.ASSET);
  44. }
  45. async pay(): Promise<any> {
  46. this.paymentHash = await this.transferAssets(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this.ASSET, JSON.stringify({ extent: this.extent }), this.price);
  47. this.dataUrl = await this.requestData(this.paymentHash, this.user);
  48. }
  49. async getUserBalanceFromIroha(user: string, userPrivateKey: string, assetId: string): Promise<number> {
  50. this._requestInProcess = true;
  51. let queryOptions = {
  52. privateKey: userPrivateKey,
  53. creatorAccountId: user,
  54. queryService: this.queryService,
  55. timeoutLimit: 10000
  56. };
  57. try {
  58. // For testing
  59. /*data = await new Promise((resolve, reject) => {
  60. setTimeout(() => {
  61. resolve(Math.random()*100);
  62. }, 1500);
  63. });*/
  64. let data = await queries.getAccountAssets(queryOptions, {
  65. accountId: user,
  66. firstAssetId: assetId,
  67. pageSize: 10
  68. });
  69. console.debug(data);
  70. let balance: number = parseFloat(data[0].balance);
  71. this._requestInProcess = false;
  72. return balance;
  73. } catch (err) {
  74. this._requestInProcess = false
  75. console.error(err)
  76. }
  77. this._requestInProcess = false
  78. }
  79. async transferAssets(user: string, userPrivateKey: string, assetId: string, description: string, amount: number): Promise<string> {
  80. let commandOptions = {
  81. privateKeys: [userPrivateKey], // Array of private keys in hex format
  82. creatorAccountId: user, // Account id, ex. admin@test
  83. quorum: 1,
  84. commandService: this.commandService,
  85. timeoutLimit: 5000 // Set timeout limit
  86. };
  87. try {
  88. let response = await commands.transferAsset(commandOptions, {
  89. srcAccountId: user,
  90. destAccountId: this.ADMIN_USER,
  91. assetId,
  92. description,
  93. amount: String(amount),
  94. });
  95. console.debug(response);
  96. return response["txHash"][0];
  97. } catch (err) {
  98. console.error(err)
  99. }
  100. }
  101. async getPrice(extent: Array<Array<number>>) : Promise<number>{
  102. let response = await this.httpClient.post(this.CHAIN4ALL_SERVICE_URL + '/price', { extent: extent }, { headers: this.basicAuthHeaders} ).toPromise();
  103. console.debug(response);
  104. return response["price"];
  105. }
  106. async requestData(paymentHash: string, user: string) : Promise<string>{
  107. let response = await this.httpClient.post(this.CHAIN4ALL_SERVICE_URL + '/buy', { txHash: paymentHash, user }, { headers: this.basicAuthHeaders}).toPromise();
  108. console.debug(response);
  109. return response["dataUrl"];
  110. }
  111. }