2020-11-04 13:35:20 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import {HttpClient} from '@angular/common/http';
|
2021-03-04 16:56:14 +01:00
|
|
|
import {first, map} from 'rxjs/operators';
|
2020-11-25 08:51:15 +01:00
|
|
|
import {BehaviorSubject, Observable} from 'rxjs';
|
2021-02-17 13:49:04 +01:00
|
|
|
import {environment} from '@src/environments/environment';
|
2021-03-04 16:56:14 +01:00
|
|
|
import {Envelope, User} from 'cic-client-meta';
|
2021-02-17 13:49:04 +01:00
|
|
|
import {UserService} from '@app/_services/user.service';
|
2021-03-03 14:43:33 +01:00
|
|
|
import { Keccak } from 'sha3';
|
|
|
|
import { utils } from 'ethers';
|
|
|
|
import {add0x, fromHex, strip0x, toHex} from '@src/assets/js/ethtx/dist/hex';
|
|
|
|
import {Tx} from '@src/assets/js/ethtx/dist';
|
|
|
|
import {toValue} from '@src/assets/js/ethtx/dist/tx';
|
|
|
|
import * as secp256k1 from 'secp256k1';
|
|
|
|
import {AuthService} from '@app/_services/auth.service';
|
2021-03-06 07:28:29 +01:00
|
|
|
import {Registry} from '@app/_helpers';
|
2021-03-03 14:43:33 +01:00
|
|
|
const Web3 = require('web3');
|
2021-03-04 16:56:14 +01:00
|
|
|
const vCard = require('vcard-parser');
|
2020-11-04 13:35:20 +01:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class TransactionService {
|
2020-12-05 07:29:18 +01:00
|
|
|
transactions: any[] = [];
|
|
|
|
private transactionList = new BehaviorSubject<any[]>(this.transactions);
|
2020-11-25 08:51:15 +01:00
|
|
|
transactionsSubject = this.transactionList.asObservable();
|
|
|
|
userInfo: any;
|
2021-03-03 14:43:33 +01:00
|
|
|
web3 = new Web3(environment.web3Provider);
|
2021-03-06 07:28:29 +01:00
|
|
|
registry = new Registry(environment.registryAddress);
|
2020-11-04 13:35:20 +01:00
|
|
|
|
2020-11-25 08:51:15 +01:00
|
|
|
constructor(
|
|
|
|
private http: HttpClient,
|
2021-03-03 14:43:33 +01:00
|
|
|
private authService: AuthService,
|
2020-11-25 08:51:15 +01:00
|
|
|
private userService: UserService
|
|
|
|
) { }
|
2020-11-04 13:35:20 +01:00
|
|
|
|
|
|
|
getAllTransactions(offset: number, limit: number): Observable<any> {
|
|
|
|
return this.http.get(`${environment.cicCacheUrl}/tx/${offset}/${limit}`)
|
|
|
|
.pipe(map(response => {
|
|
|
|
return response;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
getAddressTransactions(address: string, offset: number, limit: number): Observable<any> {
|
|
|
|
return this.http.get(`${environment.cicCacheUrl}/tx/${address}/${offset}/${limit}`)
|
|
|
|
.pipe(map(response => {
|
|
|
|
return response;
|
|
|
|
}));
|
|
|
|
}
|
2020-11-08 07:31:52 +01:00
|
|
|
|
2021-03-04 16:56:14 +01:00
|
|
|
async setTransaction(transaction, cacheSize: number): Promise<void> {
|
2020-11-25 08:51:15 +01:00
|
|
|
const cachedTransaction = this.transactions.find(cachedTx => cachedTx.tx.txHash === transaction.tx.txHash);
|
|
|
|
if (cachedTransaction) { return; }
|
2020-12-05 07:29:18 +01:00
|
|
|
transaction.type = 'transaction';
|
2021-03-04 16:56:14 +01:00
|
|
|
transaction.sender = await this.getUser(transaction.from);
|
|
|
|
transaction.recipient = await this.getUser(transaction.to);
|
|
|
|
await this.addTransaction(transaction, cacheSize);
|
2020-11-08 07:31:52 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 16:56:14 +01:00
|
|
|
async setConversion(conversion, cacheSize): Promise<void> {
|
2020-12-05 07:29:18 +01:00
|
|
|
const cachedConversion = this.transactions.find(cachedTx => cachedTx.tx.txHash === conversion.tx.txHash);
|
2020-11-25 08:51:15 +01:00
|
|
|
if (cachedConversion) { return; }
|
2020-12-05 07:29:18 +01:00
|
|
|
conversion.type = 'conversion';
|
2021-03-04 16:56:14 +01:00
|
|
|
conversion.sender = conversion.recipient = await this.getUser(conversion.trader);
|
|
|
|
await this.addTransaction(conversion, cacheSize);
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 05:30:19 +01:00
|
|
|
async addTransaction(transaction, cacheSize: number): Promise<void> {
|
2020-12-05 07:29:18 +01:00
|
|
|
this.transactions.unshift(transaction);
|
|
|
|
if (this.transactions.length > cacheSize) {
|
|
|
|
this.transactions.length = cacheSize;
|
|
|
|
}
|
|
|
|
this.transactionList.next(this.transactions);
|
|
|
|
}
|
|
|
|
|
2021-03-04 16:56:14 +01:00
|
|
|
async getUser(address: string): Promise<any> {
|
|
|
|
return this.userService.getAccountDetailsFromMeta(await User.toKey(address)).pipe(first()).subscribe(res => {
|
|
|
|
const account = Envelope.fromJSON(JSON.stringify(res)).unwrap();
|
|
|
|
const accountInfo = account.m.data;
|
|
|
|
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
|
|
|
|
this.userInfo = accountInfo;
|
|
|
|
return accountInfo;
|
|
|
|
});
|
2020-11-08 07:31:52 +01:00
|
|
|
}
|
2021-02-08 12:47:07 +01:00
|
|
|
|
2021-03-03 14:43:33 +01:00
|
|
|
async transferRequest(tokenAddress: string, senderAddress: string, recipientAddress: string, value: number): Promise<any> {
|
2021-03-06 07:28:29 +01:00
|
|
|
const transferAuthAddress = await this.registry.addressOf('TransferAuthorization');
|
2021-03-03 14:43:33 +01:00
|
|
|
const hashFunction = new Keccak(256);
|
|
|
|
hashFunction.update('createRequest(address,address,address,uint256)');
|
|
|
|
const hash = hashFunction.digest();
|
|
|
|
const methodSignature = hash.toString('hex').substring(0, 8);
|
|
|
|
const abiCoder = new utils.AbiCoder();
|
|
|
|
const abi = await abiCoder.encode(['address', 'address', 'address', 'uint256'], [senderAddress, recipientAddress, tokenAddress, value]);
|
|
|
|
const data = fromHex(methodSignature + strip0x(abi));
|
|
|
|
const tx = new Tx(environment.bloxbergChainId);
|
|
|
|
tx.nonce = await this.web3.eth.getTransactionCount(senderAddress);
|
|
|
|
tx.gasPrice = await this.web3.eth.getGasPrice();
|
|
|
|
tx.gasLimit = 8000000;
|
2021-03-06 07:28:29 +01:00
|
|
|
tx.to = fromHex(strip0x(transferAuthAddress));
|
2021-03-03 14:43:33 +01:00
|
|
|
tx.value = toValue(value);
|
|
|
|
tx.data = data;
|
|
|
|
const txMsg = tx.message();
|
|
|
|
const privateKey = this.authService.mutableKeyStore.getPrivateKey();
|
|
|
|
if (!privateKey.isDecrypted()) {
|
|
|
|
const password = window.prompt('password');
|
|
|
|
await privateKey.decrypt(password);
|
|
|
|
}
|
|
|
|
const signatureObject = secp256k1.ecdsaSign(txMsg, privateKey.keyPacket.privateParams.d);
|
|
|
|
const r = signatureObject.signature.slice(0, 32);
|
|
|
|
const s = signatureObject.signature.slice(32);
|
|
|
|
const v = signatureObject.recid;
|
|
|
|
tx.setSignature(r, s, v);
|
|
|
|
const txWire = add0x(toHex(tx.serializeRLP()));
|
|
|
|
const result = await this.web3.eth.sendSignedTransaction(txWire);
|
|
|
|
console.log('Result', result);
|
|
|
|
const transaction = await this.web3.eth.getTransaction(result.transactionHash);
|
|
|
|
console.log('Transaction', transaction);
|
2021-02-08 12:47:07 +01:00
|
|
|
}
|
2020-11-04 13:35:20 +01:00
|
|
|
}
|