src/app/_services/transaction.service.ts
Properties |
Methods |
constructor(httpClient: HttpClient, authService: AuthService, userService: UserService, loggingService: LoggingService, registryService: RegistryService)
|
||||||||||||||||||
Defined in src/app/_services/transaction.service.ts:31
|
||||||||||||||||||
Parameters :
|
addTransaction | |||||||||
addTransaction(transaction, cacheSize: number)
|
|||||||||
Defined in src/app/_services/transaction.service.ts:89
|
|||||||||
Parameters :
Returns :
void
|
getAccountInfo | ||||||
getAccountInfo(account: string)
|
||||||
Defined in src/app/_services/transaction.service.ts:102
|
||||||
Parameters :
Returns :
any
|
getAddressTransactions |
getAddressTransactions(address: string, offset: number, limit: number)
|
Defined in src/app/_services/transaction.service.ts:49
|
Returns :
Observable<any>
|
getAllTransactions |
getAllTransactions(offset: number, limit: number)
|
Defined in src/app/_services/transaction.service.ts:45
|
Returns :
Observable<any>
|
resetTransactionsList |
resetTransactionsList()
|
Defined in src/app/_services/transaction.service.ts:97
|
Returns :
void
|
Async setConversion | ||||||
setConversion(conversion, cacheSize)
|
||||||
Defined in src/app/_services/transaction.service.ts:73
|
||||||
Parameters :
Returns :
Promise<void>
|
Async setTransaction | |||||||||
setTransaction(transaction, cacheSize: number)
|
|||||||||
Defined in src/app/_services/transaction.service.ts:53
|
|||||||||
Parameters :
Returns :
Promise<void>
|
Async transferRequest | |||||||||||||||
transferRequest(tokenAddress: string, senderAddress: string, recipientAddress: string, value: number)
|
|||||||||||||||
Defined in src/app/_services/transaction.service.ts:108
|
|||||||||||||||
Parameters :
Returns :
Promise<any>
|
registry |
Type : CICRegistry
|
Defined in src/app/_services/transaction.service.ts:31
|
Private transactionList |
Default value : new BehaviorSubject<any[]>(this.transactions)
|
Defined in src/app/_services/transaction.service.ts:27
|
transactions |
Type : any[]
|
Default value : []
|
Defined in src/app/_services/transaction.service.ts:26
|
transactionsSubject |
Default value : this.transactionList.asObservable()
|
Defined in src/app/_services/transaction.service.ts:28
|
userInfo |
Type : any
|
Defined in src/app/_services/transaction.service.ts:29
|
web3 |
Type : Web3
|
Defined in src/app/_services/transaction.service.ts:30
|
import { Injectable } from '@angular/core';
import {first} from 'rxjs/operators';
import {BehaviorSubject, Observable} from 'rxjs';
import {environment} from '@src/environments/environment';
import {Envelope, User} from 'cic-client-meta';
import {UserService} from '@app/_services/user.service';
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';
import {defaultAccount} from '@app/_models';
import {LoggingService} from '@app/_services/logging.service';
import {HttpClient} from '@angular/common/http';
import {CICRegistry} from 'cic-client';
import {RegistryService} from '@app/_services/registry.service';
import Web3 from 'web3';
const vCard = require('vcard-parser');
@Injectable({
providedIn: 'root'
})
export class TransactionService {
transactions: any[] = [];
private transactionList = new BehaviorSubject<any[]>(this.transactions);
transactionsSubject = this.transactionList.asObservable();
userInfo: any;
web3: Web3;
registry: CICRegistry;
constructor(
private httpClient: HttpClient,
private authService: AuthService,
private userService: UserService,
private loggingService: LoggingService,
private registryService: RegistryService,
) {
this.web3 = this.registryService.getWeb3();
this.registry = registryService.getRegistry();
this.registry.load();
}
getAllTransactions(offset: number, limit: number): Observable<any> {
return this.httpClient.get(`${environment.cicCacheUrl}/tx/${offset}/${limit}`);
}
getAddressTransactions(address: string, offset: number, limit: number): Observable<any> {
return this.httpClient.get(`${environment.cicCacheUrl}/tx/${address}/${offset}/${limit}`);
}
async setTransaction(transaction, cacheSize: number): Promise<void> {
if (this.transactions.find(cachedTx => cachedTx.tx.txHash === transaction.tx.txHash)) { return; }
transaction.value = Number(transaction.value);
transaction.type = 'transaction';
try {
this.userService.getAccountDetailsFromMeta(await User.toKey(transaction.from)).pipe(first()).subscribe((res) => {
transaction.sender = this.getAccountInfo(res.body);
}, error => {
transaction.sender = defaultAccount;
});
this.userService.getAccountDetailsFromMeta(await User.toKey(transaction.to)).pipe(first()).subscribe((res) => {
transaction.recipient = this.getAccountInfo(res.body);
}, error => {
transaction.recipient = defaultAccount;
});
} finally {
this.addTransaction(transaction, cacheSize);
}
}
async setConversion(conversion, cacheSize): Promise<void> {
if (this.transactions.find(cachedTx => cachedTx.tx.txHash === conversion.tx.txHash)) { return; }
conversion.type = 'conversion';
conversion.fromValue = Number(conversion.fromValue);
conversion.toValue = Number(conversion.toValue);
try {
this.userService.getAccountDetailsFromMeta(await User.toKey(conversion.trader)).pipe(first()).subscribe((res) => {
conversion.sender = conversion.recipient = this.getAccountInfo(res.body);
}, error => {
conversion.sender = conversion.recipient = defaultAccount;
});
} finally {
this.addTransaction(conversion, cacheSize);
}
}
addTransaction(transaction, cacheSize: number): void {
this.transactions.unshift(transaction);
if (this.transactions.length > cacheSize) {
this.transactions.length = cacheSize;
}
this.transactionList.next(this.transactions);
}
resetTransactionsList(): void {
this.transactions = [];
this.transactionList.next(this.transactions);
}
getAccountInfo(account: string): any {
let accountInfo = Envelope.fromJSON(JSON.stringify(account)).unwrap().m.data;
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
return accountInfo;
}
async transferRequest(tokenAddress: string, senderAddress: string, recipientAddress: string, value: number): Promise<any> {
const transferAuthAddress = await this.registry.getContractAddressByName('TransferAuthorization');
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 = Number(await this.web3.eth.getGasPrice());
tx.gasLimit = 8000000;
tx.to = fromHex(strip0x(transferAuthAddress));
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);
this.loggingService.sendInfoLevelMessage(`Result: ${result}`);
const transaction = await this.web3.eth.getTransaction(result.transactionHash);
this.loggingService.sendInfoLevelMessage(`Transaction: ${transaction}`);
}
}