| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- 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<any> {
- if (this._requestInProcess) {
- return;
- }
- this.userBalance = await this.getUserBalanceFromIroha(this.user, "ecb7f22887b0b45d1923fcd147d34bb6fd79f56eb54ed5af42c9d70c7808a9d8", this.ASSET);
- }
- async pay(): Promise<any> {
- 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<number> {
- 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<string> {
- 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<Array<number>>) : Promise<number>{
- 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<string>{
- let response = await this.httpClient.post(this.CHAIN4ALL_SERVICE_URL + '/buy', { txHash: paymentHash, user }, { headers: this.basicAuthHeaders}).toPromise();
- console.debug(response);
- return response["dataUrl"];
- }
- }
|