import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { CommandService_v1Client as CommandService, QueryService_v1Client as QueryService } from 'iroha-helpers/lib/proto/endpoint_pb_service' import { queries, commands } from 'iroha-helpers' @Injectable({ providedIn: 'root' }) export class BcInfoService { BASE_URL = 'https://chain4all.lesprojekt.cz' as const; CHAIN4ALL_SERVICE_URL = 'http://localhost:3000' as const; IROHA_ADDRESS = "http://localhost:8080" as const; ASSET = "testcoin#test" as const; ADMIN_USER = "admin@test" as const; commandService: CommandService; queryService: QueryService; basicAuthHeaders: HttpHeaders; user = 'kunickyd@test'; // userPrivateKey = 'ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8'; userBalance = -1; price = -1; paymentHash = ""; dataUrl = ""; extent = [ //bottom left and top right point should be enough [48.5, 17], [49, 17], [49, 16.5], [48.5, 16.5] ]; private _requestInProcess = false; constructor(public httpClient: HttpClient) { this.commandService = new CommandService(this.IROHA_ADDRESS); this.queryService = new QueryService(this.IROHA_ADDRESS); this.getUserBalance(); this.getPrice(this.extent).then(price => this.price = price); this.basicAuthHeaders = new HttpHeaders(); this.basicAuthHeaders.append("Authorization", "Basic " + Buffer.from("admin:password", 'base64')); } async getUserBalance(): Promise { if (this._requestInProcess) { return; } this.userBalance = await this.getUserBalanceFromIroha(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this.ASSET); } async pay(): Promise { this.paymentHash = await this.transferAssets(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this.ASSET, JSON.stringify({ extent: this.extent }), this.price); this.dataUrl = await this.requestData(this.paymentHash, this.user); } async getUserBalanceFromIroha(user: string, userPrivateKey: string, assetId: string): Promise { this._requestInProcess = true; let queryOptions = { privateKey: userPrivateKey, creatorAccountId: user, queryService: this.queryService, timeoutLimit: 10000 }; try { // For testing /*data = await new Promise((resolve, reject) => { setTimeout(() => { resolve(Math.random()*100); }, 1500); });*/ let data = await queries.getAccountAssets(queryOptions, { accountId: user, firstAssetId: assetId, pageSize: 10 }); console.debug(data); let balance: number = parseFloat(data[0].balance); this._requestInProcess = false; return balance; } catch (err) { this._requestInProcess = false console.error(err) } this._requestInProcess = false } async transferAssets(user: string, userPrivateKey: string, assetId: string, description: string, amount: number): Promise { let commandOptions = { privateKeys: [userPrivateKey], // Array of private keys in hex format creatorAccountId: user, // Account id, ex. admin@test quorum: 1, commandService: this.commandService, timeoutLimit: 5000 // Set timeout limit }; try { let response = await commands.transferAsset(commandOptions, { srcAccountId: user, destAccountId: this.ADMIN_USER, assetId, description, amount: String(amount), }); console.debug(response); return response["txHash"][0]; } catch (err) { console.error(err) } } async getPrice(extent: Array>) : Promise{ let response = await this.httpClient.post(this.CHAIN4ALL_SERVICE_URL + '/price', { extent: extent }, { headers: this.basicAuthHeaders} ).toPromise(); console.debug(response); return response["price"]; } async requestData(paymentHash: string, user: string) : Promise{ let response = await this.httpClient.post(this.CHAIN4ALL_SERVICE_URL + '/buy', { txHash: paymentHash, user }, { headers: this.basicAuthHeaders}).toPromise(); console.debug(response); return response["dataUrl"]; } }