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 { LoginComponent } from './login/login.component';
import { AuthGuardService } from './auth-guard.service';
import { NodeResolverService } from './node-resolver.service';
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: '', component: LoginComponent, resolve: { response: NodeResolverService} },
//{ path: 'create', component: PostCreateComponent, canActivate: [AuthGuardService]},
{ path: 'view', component: ViewerComponent, canActivate: [AuthGuardService]},
{ path: 'login', component: LoginComponent}

View file

@ -15,7 +15,7 @@ import { AppComponent } from './app.component';
//import { HeaderComponent } from './header/header.component';
//import { PostListComponent } from './posts/post-list/post-list.component';
//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 { NameDialogComponent } from './namedialog/namedialog.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@ -25,7 +25,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
AppComponent,
//PostCreateComponent,
//HeaderComponent,
//ViewerComponent,
ViewerComponent,
LoginComponent,
//NameDialogComponent,
//PostListComponent

View file

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

View file

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

View file

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

View file

@ -1,5 +1,7 @@
<!-- TODO: get this dialog working right-->
<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-form-field>
<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>
</mat-dialog-actions>
</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 {
_id: string;
_id?: string;
address: string;
session: string;
blocktime: number;

View file

@ -1,5 +1,5 @@
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs';
import {Subject, BehaviorSubject, Observable} from 'rxjs';
import {HttpClient, HttpParams} from '@angular/common/http';
import {User} from './user.model';
import {Owner} from './owner.model';
@ -7,16 +7,39 @@ import {Owner} from './owner.model';
@Injectable({providedIn: 'root'})
export class UserService{
private dataStore: { user: User} = { user: { address: '', session: '', blocktime: 0 }};
private uZaddr = '';
private oZaddr = '';
private uName = '';
private session: string | null = '';
private uZaddrUpdated = new Subject<string>();
private _uZaddrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.uZaddr);
private uNameUpdated = new Subject<string>();
private ownerUpdated = new Subject<Owner>();
public readonly uZaddrUpdate: Observable<string> = this._uZaddrUpdated.asObservable();
constructor(private http: HttpClient){
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) {
@ -55,10 +78,10 @@ export class UserService{
if (UserDataResponse.status == 200){
this.uZaddr = UserDataResponse.body!.user[0].address;
console.log(`US: Found user, returning zaddr ${this.uZaddr}`);
this.uZaddrUpdated.next(this.uZaddr);
this._uZaddrUpdated.next(this.uZaddr);
} else {
console.log('US: Did not find user');
this.uZaddrUpdated.next('');
this._uZaddrUpdated.next('');
}
});
@ -84,7 +107,7 @@ export class UserService{
getName() {
if (this.session != null) {
this.getUser(this.session);
this.uZaddrUpdated.subscribe((addr: string) => {
this._uZaddrUpdated.subscribe((addr: string) => {
console.log(` US: ${addr}`);
this.getOwner(addr);
});
@ -95,9 +118,9 @@ export class UserService{
return this.uNameUpdated;
}
getZaddrUpdateListener() {
return this.uZaddrUpdated;
}
//getZaddrUpdateListener() {
//return this.uZaddrUpdated;
//}
getOwnerUpdateListener() {
return this.ownerUpdated;

View file

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

View file

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