zgo/src/app/fullnode.service.ts

91 lines
2.5 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Subject} from 'rxjs';
import {HttpClient, HttpParams} from '@angular/common/http';
import {UserService} from './user.service';
//import {User} from './user.model';
@Injectable({providedIn: 'root'})
export class FullnodeService{
private height = 0;
private memoList: string[] = [];
private addr: string = '';
private heightUpdated = new Subject<number>();
private memoUpdated = new Subject<string[]>();
private addrUpdated = new Subject<string>();
constructor(private http: HttpClient, public userService: UserService){
}
getHeight() {
this.http.get<{message: string, height: number}>('http://localhost:3000/api/blockheight').
subscribe((BlockData) => {
this.height = BlockData.height;
this.heightUpdated.next(this.height);
});
}
getHeightUpdateListener() {
return this.heightUpdated;
}
hexToString(hexString: string) {
var str = '';
for (var n=0; n < hexString.length; n +=2) {
str += String.fromCharCode(parseInt(hexString.substr(n, 2), 16));
}
return str;
}
getMemos() {
this.http.get<{message: string, txs: any}>('http://localhost:3000/api/txs').
subscribe((TxData) => {
var memos: string[] = [];
//this.addr = TxData.addr;
var re = /.*S4ZEC::(.*)\sReply-To:\s(z\w+)/;
for (var i=0; i < TxData.txs.length; i++) {
var memo = this.hexToString(TxData.txs[i].memo);
//console.log(TxData.txs[i].blocktime);
if (re.test(memo)){
memos.push(memo);
var match = re.exec(memo);
if (match != null) {
var address = match[2];
var session = match[1];
var blocktime = TxData.txs[i].blocktime;
const params = new HttpParams().append('address', address).append('session', session);
this.http.get<{message: string, users: any}>('http://localhost:3000/api/users', {headers:{}, params: params}).
subscribe((UserData) => {
console.log(UserData.message);
console.log(UserData.users.length);
if (UserData.users.length == 0){
console.log(`No matching user found! Adding ${address} ${session} ${blocktime}`);
this.userService.addUser(address, session, blocktime);
}
});
}
}
}
this.memoList = memos;
this.memoUpdated.next(this.memoList);
});
}
getMemoUpdateListener() {
return this.memoUpdated;
}
getAddr() {
this.http.get<{message: string, addr: string}>('http://localhost:3000/api/getaddr').
subscribe((AddrData) => {
this.addr = AddrData.addr;
this.addrUpdated.next(this.addr);
});
}
getAddrUpdateListener() {
return this.addrUpdated;
}
}