2021-10-15 19:14:49 +00:00
|
|
|
import {Injectable} from '@angular/core';
|
2021-10-20 20:51:14 +00:00
|
|
|
import {Subject, Subscription, BehaviorSubject, Observable} from 'rxjs';
|
2021-10-15 19:14:49 +00:00
|
|
|
import {HttpClient, HttpParams} from '@angular/common/http';
|
|
|
|
import {UserService} from './user.service';
|
|
|
|
|
|
|
|
//import {User} from './user.model';
|
|
|
|
|
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
|
export class FullnodeService{
|
2021-10-22 22:20:36 +00:00
|
|
|
private dataStore: { height: number, memoList: string[], addr: string, price: number } = { height: 0, memoList: [], addr: '', price:0 };
|
2021-10-21 15:23:58 +00:00
|
|
|
private _heightUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.height);
|
|
|
|
private _memoUpdated: BehaviorSubject<string[]> = new BehaviorSubject(this.dataStore.memoList);
|
|
|
|
private _addrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.dataStore.addr);
|
2021-10-22 22:20:36 +00:00
|
|
|
private _priceUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.price);
|
2021-10-20 20:51:14 +00:00
|
|
|
public readonly addrUpdate: Observable<string> = this._addrUpdated.asObservable();
|
|
|
|
public readonly heightUpdate: Observable<number> = this._heightUpdated.asObservable();
|
|
|
|
public readonly memoUpdate: Observable<string[]> = this._memoUpdated.asObservable();
|
2021-10-22 22:20:36 +00:00
|
|
|
public readonly priceUpdate: Observable<number> = this._priceUpdated.asObservable();
|
2021-10-20 20:51:14 +00:00
|
|
|
private UserSub: Subscription = new Subscription();
|
2021-10-15 19:14:49 +00:00
|
|
|
|
|
|
|
constructor(private http: HttpClient, public userService: UserService){
|
2021-10-21 15:23:58 +00:00
|
|
|
this.getAddr();
|
|
|
|
this.getHeight();
|
|
|
|
this.getMemos();
|
2021-10-22 22:20:36 +00:00
|
|
|
this.getPrice();
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 20:51:14 +00:00
|
|
|
getHeight(){
|
|
|
|
let obs = this.http.get<{message: string, height: number}>('http://localhost:3000/api/blockheight');
|
|
|
|
obs.subscribe((BlockData) => {
|
2021-10-21 15:23:58 +00:00
|
|
|
this.dataStore.height = BlockData.height;
|
|
|
|
this._heightUpdated.next(Object.assign({}, this.dataStore).height);
|
2021-10-15 19:14:49 +00:00
|
|
|
});
|
|
|
|
|
2021-10-20 20:51:14 +00:00
|
|
|
return obs;
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 22:20:36 +00:00
|
|
|
getPrice(){
|
|
|
|
let obs = this.http.get<{message: string, price: number}>('http://localhost:3000/api/price');
|
|
|
|
obs.subscribe((PriceData) => {
|
|
|
|
this.dataStore.price = PriceData.price;
|
|
|
|
console.log(this.dataStore.price);
|
|
|
|
this._priceUpdated.next(Object.assign({},this.dataStore).price);
|
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
|
|
|
}
|
2021-10-20 20:51:14 +00:00
|
|
|
|
2021-10-15 19:14:49 +00:00
|
|
|
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;
|
2021-10-20 20:51:14 +00:00
|
|
|
console.log(memo);
|
|
|
|
console.log(`Searching for user for ${session}`);
|
2021-10-21 15:23:58 +00:00
|
|
|
const params = new HttpParams().append('session', session);
|
|
|
|
this.http.get<{message: string, user: any}>('http://localhost:3000/api/getuser', { headers:{}, params: params, observe: 'response'}).
|
|
|
|
subscribe((UserDataResponse) => {
|
|
|
|
console.log(UserDataResponse.status);
|
|
|
|
if (UserDataResponse.status == 200){
|
|
|
|
console.log(`FS: Found user`);
|
2021-10-20 20:51:14 +00:00
|
|
|
} else {
|
2021-10-21 15:23:58 +00:00
|
|
|
console.log('FS: Did not find user');
|
|
|
|
this.userService.addUser(address, session, blocktime);
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 15:23:58 +00:00
|
|
|
this.dataStore.memoList = memos;
|
|
|
|
this._memoUpdated.next(Object.assign({},this.dataStore).memoList);
|
2021-10-15 19:14:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getAddr() {
|
2021-10-21 15:23:58 +00:00
|
|
|
let obs = this.http.get<{message: string, addr: string}>('http://localhost:3000/api/getaddr');
|
|
|
|
|
|
|
|
obs.subscribe((AddrData) => {
|
|
|
|
this.dataStore.addr = AddrData.addr;
|
|
|
|
this._addrUpdated.next(Object.assign({}, this.dataStore).addr);
|
2021-10-15 19:14:49 +00:00
|
|
|
});
|
2021-10-21 15:23:58 +00:00
|
|
|
|
|
|
|
return obs;
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
}
|