Implement resolver for node address

Fixes services.
This commit is contained in:
Rene Vergara 2021-10-21 10:23:58 -05:00
parent 30a83414ad
commit 4214dac887
11 changed files with 144 additions and 93 deletions

View file

@ -3,9 +3,10 @@ import { RouterModule, Routes } from '@angular/router';
import { ViewerComponent } from './viewer/viewer.component'; import { ViewerComponent } from './viewer/viewer.component';
import { LoginComponent } from './login/login.component'; import { LoginComponent } from './login/login.component';
import { AuthGuardService } from './auth-guard.service'; import { AuthGuardService } from './auth-guard.service';
import { NodeResolverService } from './node-resolver.service';
const routes: Routes = [ const routes: Routes = [
{ path: '', component: LoginComponent }, { path: '', component: LoginComponent, resolve: { response: NodeResolverService} },
//{ path: 'create', component: PostCreateComponent, canActivate: [AuthGuardService]}, //{ path: 'create', component: PostCreateComponent, canActivate: [AuthGuardService]},
{ path: 'view', component: ViewerComponent, canActivate: [AuthGuardService]}, { path: 'view', component: ViewerComponent, canActivate: [AuthGuardService]},
{ path: 'login', component: LoginComponent} { path: 'login', component: LoginComponent}

View file

@ -15,7 +15,7 @@ import { AppComponent } from './app.component';
//import { HeaderComponent } from './header/header.component'; //import { HeaderComponent } from './header/header.component';
//import { PostListComponent } from './posts/post-list/post-list.component'; //import { PostListComponent } from './posts/post-list/post-list.component';
//import { PostService } from './posts/posts.service'; //import { PostService } from './posts/posts.service';
//import { ViewerComponent } from './viewer/viewer.component'; import { ViewerComponent } from './viewer/viewer.component';
import { LoginComponent } from './login/login.component'; import { LoginComponent } from './login/login.component';
//import { NameDialogComponent } from './namedialog/namedialog.component'; //import { NameDialogComponent } from './namedialog/namedialog.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@ -25,7 +25,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
AppComponent, AppComponent,
//PostCreateComponent, //PostCreateComponent,
//HeaderComponent, //HeaderComponent,
//ViewerComponent, ViewerComponent,
LoginComponent, LoginComponent,
//NameDialogComponent, //NameDialogComponent,
//PostListComponent //PostListComponent

View file

@ -19,8 +19,7 @@ export class AuthGuardService implements CanActivate {
const token = localStorage.getItem('s4z_token'); const token = localStorage.getItem('s4z_token');
if(token != null){ if(token != null){
this.userService.getUser(token); this.userService.uZaddrUpdate.
this.UserSub = this.userService.getZaddrUpdateListener().
subscribe((addr) => { subscribe((addr) => {
if (addr != null) { if (addr != null) {
console.log(addr); console.log(addr);
@ -29,10 +28,10 @@ export class AuthGuardService implements CanActivate {
console.log("No record for that token"); console.log("No record for that token");
} }
}); });
this.UserSub.unsubscribe();
if (this.addr != null) { if (this.addr != null) {
return true; return true;
} else { } else {
this.router.navigate(['/login']);
return false; return false;
} }
} else { } else {

View file

@ -7,25 +7,26 @@ import {UserService} from './user.service';
@Injectable({providedIn: 'root'}) @Injectable({providedIn: 'root'})
export class FullnodeService{ export class FullnodeService{
private height = 0; private dataStore: { height: number, memoList: string[], addr: string } = { height: 0, memoList: [], addr: '' };
private memoList: string[] = []; private _heightUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.height);
private addr: string = ''; private _memoUpdated: BehaviorSubject<string[]> = new BehaviorSubject(this.dataStore.memoList);
private _heightUpdated: BehaviorSubject<number> = new BehaviorSubject(this.height); private _addrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.dataStore.addr);
private _memoUpdated: BehaviorSubject<string[]> = new BehaviorSubject(this.memoList);
private _addrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.addr);
public readonly addrUpdate: Observable<string> = this._addrUpdated.asObservable(); public readonly addrUpdate: Observable<string> = this._addrUpdated.asObservable();
public readonly heightUpdate: Observable<number> = this._heightUpdated.asObservable(); public readonly heightUpdate: Observable<number> = this._heightUpdated.asObservable();
public readonly memoUpdate: Observable<string[]> = this._memoUpdated.asObservable(); public readonly memoUpdate: Observable<string[]> = this._memoUpdated.asObservable();
private UserSub: Subscription = new Subscription(); private UserSub: Subscription = new Subscription();
constructor(private http: HttpClient, public userService: UserService){ constructor(private http: HttpClient, public userService: UserService){
this.getAddr();
this.getHeight();
this.getMemos();
} }
getHeight(){ getHeight(){
let obs = this.http.get<{message: string, height: number}>('http://localhost:3000/api/blockheight'); let obs = this.http.get<{message: string, height: number}>('http://localhost:3000/api/blockheight');
obs.subscribe((BlockData) => { obs.subscribe((BlockData) => {
this.height = BlockData.height; this.dataStore.height = BlockData.height;
this._heightUpdated.next(this.height); this._heightUpdated.next(Object.assign({}, this.dataStore).height);
}); });
return obs; return obs;
@ -61,21 +62,22 @@ export class FullnodeService{
var blocktime = TxData.txs[i].blocktime; var blocktime = TxData.txs[i].blocktime;
console.log(memo); console.log(memo);
console.log(`Searching for user for ${session}`); console.log(`Searching for user for ${session}`);
this.userService.getUser(session); const params = new HttpParams().append('session', session);
this.UserSub = this.userService.getZaddrUpdateListener(). this.http.get<{message: string, user: any}>('http://localhost:3000/api/getuser', { headers:{}, params: params, observe: 'response'}).
subscribe((zAddr) => { subscribe((UserDataResponse) => {
if (zAddr.length == 0) { console.log(UserDataResponse.status);
console.log('FS: getMemos new adding user'); if (UserDataResponse.status == 200){
this.userService.addUser(address, session, blocktime); console.log(`FS: Found user`);
} else { } else {
console.log('FS: getMemos found user'); console.log('FS: Did not find user');
this.userService.addUser(address, session, blocktime);
} }
}); });
} }
} }
} }
this.memoList = memos; this.dataStore.memoList = memos;
this._memoUpdated.next(this.memoList); this._memoUpdated.next(Object.assign({},this.dataStore).memoList);
}); });
} }
@ -84,11 +86,14 @@ export class FullnodeService{
//} //}
getAddr() { getAddr() {
this.http.get<{message: string, addr: string}>('http://localhost:3000/api/getaddr'). let obs = this.http.get<{message: string, addr: string}>('http://localhost:3000/api/getaddr');
subscribe((AddrData) => {
this.addr = AddrData.addr; obs.subscribe((AddrData) => {
this._addrUpdated.next(this.addr); this.dataStore.addr = AddrData.addr;
this._addrUpdated.next(Object.assign({}, this.dataStore).addr);
}); });
return obs;
} }
//getAddrUpdateListener() { //getAddrUpdateListener() {

View file

@ -1,5 +1,5 @@
import { Component, OnInit, OnDestroy, Injectable } from '@angular/core'; import { Component, OnInit, OnDestroy, Injectable } from '@angular/core';
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router'; import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot, ActivatedRoute } from '@angular/router';
import { UserService } from '../user.service'; import { UserService } from '../user.service';
import { FullnodeService } from '../fullnode.service'; import { FullnodeService } from '../fullnode.service';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
@ -17,19 +17,24 @@ var Buffer = require('buffer/').Buffer;
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
memos: string[] = []; memos: string[] = [];
nodeAddress: string = '';
private FullnodeSub: Subscription = new Subscription(); private FullnodeSub: Subscription = new Subscription();
private UserSub: Subscription = new Subscription(); private UserSub: Subscription = new Subscription();
constructor( constructor(
private activatedRoute: ActivatedRoute,
public fullnodeService: FullnodeService, public fullnodeService: FullnodeService,
private router: Router, private router: Router,
public userService: UserService public userService: UserService
){ ){
//this.fullnodeService.getAddr();
} }
ngOnInit(){ ngOnInit(){
this.fullnodeService.getMemos(); console.log('Activated route data in Component:::', this.activatedRoute.data);
this.fullnodeService.getAddr(); this.activatedRoute.data.subscribe((addrData) => {
var nodeAddr = this.fullnodeService.addrUpdate; console.log('FETCH ADDRESS', addrData);
this.nodeAddress = addrData.response.addr;
console.log('Node addres ', this.nodeAddress);
var localToken = localStorage.getItem('s4z_token'); var localToken = localStorage.getItem('s4z_token');
console.log(localToken); console.log(localToken);
if(localToken == null){ if(localToken == null){
@ -37,7 +42,7 @@ export class LoginComponent implements OnInit {
localStorage.setItem('s4z_token', token); localStorage.setItem('s4z_token', token);
console.log('Showing QR code for login'); console.log('Showing QR code for login');
console.log(URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(token)))); console.log(URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(token))));
var codeString = `zcash:${nodeAddr}?amount=0.001&memo=${URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(token)))}`; var codeString = `zcash:${this.nodeAddress}?amount=0.001&memo=${URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(token)))}`;
console.log(codeString); console.log(codeString);
var qrcode = new QRCode(document.getElementById("qrcode"), { var qrcode = new QRCode(document.getElementById("qrcode"), {
text: codeString, text: codeString,
@ -46,17 +51,17 @@ export class LoginComponent implements OnInit {
logoHeight: 80 logoHeight: 80
}); });
} else { } else {
this.userService.getUser(localToken); //this.userService.getUser(localToken);
this.userService.getZaddrUpdateListener(). this.userService.uZaddrUpdate.
subscribe((userAddr: string) => { subscribe((userAddr: string) => {
if (userAddr.length != 0) { if (userAddr.length != 0) {
console.log('Log in found!'); console.log('Log in found!');
//this.router.navigate(['/view']); this.router.navigate(['/view']);
} else { } else {
console.log('No login for existing token found'); console.log('No login for existing token found');
console.log('Showing QR code for login'); console.log('Showing QR code for login');
//console.log(URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(localToken)))); //console.log(URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(localToken))));
var codeString = `zcash:${nodeAddr}?amount=0.001&memo=${URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(localToken!)))}`; var codeString = `zcash:${this.nodeAddress}?amount=0.001&memo=${URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(localToken!)))}`;
console.log(codeString); console.log(codeString);
var qrcode = new QRCode(document.getElementById("qrcode"), { var qrcode = new QRCode(document.getElementById("qrcode"), {
text: codeString, text: codeString,
@ -67,6 +72,7 @@ export class LoginComponent implements OnInit {
} }
}); });
} }
});
} }
ngOnDestroy(){ ngOnDestroy(){

View file

@ -1,5 +1,7 @@
<!-- TODO: get this dialog working right-->
<h2 mat-dialog-title>Set the name of your business</h2> <h2 mat-dialog-title>Set the name of your business</h2>
<form (submit)="save(nameForm)" #nameForm="NgForm"> <main>
<form (submit)="save(nameForm)" #nameForm="ngForm">
<mat-dialog-content> <mat-dialog-content>
<mat-form-field> <mat-form-field>
<input matInput type="text" name="bizname" placeholder="Bubba's Bakery" ngModel required #bizname="ngModel"> <input matInput type="text" name="bizname" placeholder="Bubba's Bakery" ngModel required #bizname="ngModel">
@ -12,3 +14,4 @@
<button class="mat-raised-button mat-primary" type="submit">Save</button> <button class="mat-raised-button mat-primary" type="submit">Save</button>
</mat-dialog-actions> </mat-dialog-actions>
</form> </form>
</main>

View file

@ -0,0 +1,23 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { FullnodeService} from './fullnode.service';
@Injectable({
providedIn: 'root'
})
export class NodeResolverService implements Resolve<any> {
constructor(private fullnode: FullnodeService) {}
resolve(route: ActivatedRouteSnapshot): Observable<any> {
console.log('Called getAddr in resolver...', route);
return this.fullnode.getAddr().pipe(
catchError(error => {
return of('No data');
})
);
}
}

View file

@ -1,5 +1,5 @@
export interface User { export interface User {
_id: string; _id?: string;
address: string; address: string;
session: string; session: string;
blocktime: number; blocktime: number;

View file

@ -1,5 +1,5 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {Subject} from 'rxjs'; import {Subject, BehaviorSubject, Observable} from 'rxjs';
import {HttpClient, HttpParams} from '@angular/common/http'; import {HttpClient, HttpParams} from '@angular/common/http';
import {User} from './user.model'; import {User} from './user.model';
import {Owner} from './owner.model'; import {Owner} from './owner.model';
@ -7,16 +7,39 @@ import {Owner} from './owner.model';
@Injectable({providedIn: 'root'}) @Injectable({providedIn: 'root'})
export class UserService{ export class UserService{
private dataStore: { user: User} = { user: { address: '', session: '', blocktime: 0 }};
private uZaddr = ''; private uZaddr = '';
private oZaddr = ''; private oZaddr = '';
private uName = ''; private uName = '';
private session: string | null = ''; private session: string | null = '';
private uZaddrUpdated = new Subject<string>(); private _uZaddrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.uZaddr);
private uNameUpdated = new Subject<string>(); private uNameUpdated = new Subject<string>();
private ownerUpdated = new Subject<Owner>(); private ownerUpdated = new Subject<Owner>();
public readonly uZaddrUpdate: Observable<string> = this._uZaddrUpdated.asObservable();
constructor(private http: HttpClient){ constructor(private http: HttpClient){
this.session = localStorage.getItem('s4z_token'); this.session = localStorage.getItem('s4z_token');
if (this.session != null) {
this.findUser(this.session);
}
}
findUser(session: string) {
const params = new HttpParams().append('session', session);
let obs = this.http.get<{message: string, user: any}>('http://localhost:3000/api/getuser', { headers:{}, params: params, observe: 'response'});
obs.subscribe((UserDataResponse) => {
console.log(UserDataResponse.status);
if (UserDataResponse.status == 200){
this.dataStore.user = UserDataResponse.body!.user[0];
console.log(`US: Found user, returning it`);
this._uZaddrUpdated.next(Object.assign({},this.dataStore).user.address);
} else {
console.log('US: Did not find user');
}
});
return obs;
} }
addUser(address: string, session: string, blocktime: number) { addUser(address: string, session: string, blocktime: number) {
@ -55,10 +78,10 @@ export class UserService{
if (UserDataResponse.status == 200){ if (UserDataResponse.status == 200){
this.uZaddr = UserDataResponse.body!.user[0].address; this.uZaddr = UserDataResponse.body!.user[0].address;
console.log(`US: Found user, returning zaddr ${this.uZaddr}`); console.log(`US: Found user, returning zaddr ${this.uZaddr}`);
this.uZaddrUpdated.next(this.uZaddr); this._uZaddrUpdated.next(this.uZaddr);
} else { } else {
console.log('US: Did not find user'); console.log('US: Did not find user');
this.uZaddrUpdated.next(''); this._uZaddrUpdated.next('');
} }
}); });
@ -84,7 +107,7 @@ export class UserService{
getName() { getName() {
if (this.session != null) { if (this.session != null) {
this.getUser(this.session); this.getUser(this.session);
this.uZaddrUpdated.subscribe((addr: string) => { this._uZaddrUpdated.subscribe((addr: string) => {
console.log(` US: ${addr}`); console.log(` US: ${addr}`);
this.getOwner(addr); this.getOwner(addr);
}); });
@ -95,9 +118,9 @@ export class UserService{
return this.uNameUpdated; return this.uNameUpdated;
} }
getZaddrUpdateListener() { //getZaddrUpdateListener() {
return this.uZaddrUpdated; //return this.uZaddrUpdated;
} //}
getOwnerUpdateListener() { getOwnerUpdateListener() {
return this.ownerUpdated; return this.ownerUpdated;

View file

@ -1,4 +1,6 @@
<!--<app-header></app-header>--> <!--<app-header></app-header>-->
<div align="center"> <div align="center">
<h1>{{message}}</h1> <h1>{{message}}</h1>
<br>
{{addrUpdate | async}}
</div> </div>

View file

@ -3,8 +3,8 @@ import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot } from
import { MatDialog, MatDialogConfig } from '@angular/material/dialog'; import { MatDialog, MatDialogConfig } from '@angular/material/dialog';
import { UserService } from '../user.service'; import { UserService } from '../user.service';
import { FullnodeService } from '../fullnode.service'; import { FullnodeService } from '../fullnode.service';
//import { NameDialogComponent } from '../namedialog/namedialog.component'; //import { NameDialogComponent } from '../namedialog/namedialog.component'; // TODO: connect dialog
import { Subscription } from 'rxjs'; import { Subscription, Observable } from 'rxjs';
import {Owner} from '../owner.model'; import {Owner} from '../owner.model';
//import { v4 as uuidv4 } from 'uuid'; //import { v4 as uuidv4 } from 'uuid';
@ -23,6 +23,7 @@ export class ViewerComponent implements OnInit {
public message: string = "Welcome to the inside!"; public message: string = "Welcome to the inside!";
private UserSub: Subscription = new Subscription(); private UserSub: Subscription = new Subscription();
private OwnerSub: Subscription = new Subscription(); private OwnerSub: Subscription = new Subscription();
public addrUpdate: Observable<string>;
constructor( constructor(
public fullnodeService: FullnodeService, public fullnodeService: FullnodeService,
@ -30,23 +31,11 @@ export class ViewerComponent implements OnInit {
public userService: UserService, public userService: UserService,
private dialog: MatDialog private dialog: MatDialog
){ ){
this.addrUpdate = fullnodeService.addrUpdate;
} }
ngOnInit(){ ngOnInit(){
this.userService.getName();
this.UserSub = this.userService.getNameUpdateListener().
subscribe((name) => {
if (name.length == 0) {
console.log(name);
this.OwnerSub = this.userService.getZaddrUpdateListener().
subscribe((addr) => {
//this.openDialog(addr);
});
} else {
this.message = name;
}
});
} }
ngOnDestroy(){ ngOnDestroy(){