2021-11-02 21:13:24 +00:00
|
|
|
import { Component, OnInit, OnDestroy, Injectable, ChangeDetectorRef } from '@angular/core';
|
2021-10-21 15:23:58 +00:00
|
|
|
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot, ActivatedRoute } from '@angular/router';
|
2021-10-15 19:14:49 +00:00
|
|
|
import { UserService } from '../user.service';
|
|
|
|
import { FullnodeService } from '../fullnode.service';
|
2021-11-02 21:13:24 +00:00
|
|
|
import { Subscription, Observable } from 'rxjs';
|
2021-10-15 19:14:49 +00:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
var QRCode = require('easyqrcodejs');
|
|
|
|
var URLSafeBase64 = require('urlsafe-base64');
|
|
|
|
var Buffer = require('buffer/').Buffer;
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-login',
|
|
|
|
templateUrl: './login.component.html',
|
|
|
|
styleUrls: ['./login.component.css']
|
|
|
|
})
|
|
|
|
|
|
|
|
export class LoginComponent implements OnInit {
|
2021-11-02 21:13:24 +00:00
|
|
|
intervalHolder: any;
|
2021-10-21 15:23:58 +00:00
|
|
|
nodeAddress: string = '';
|
2021-10-15 19:14:49 +00:00
|
|
|
private FullnodeSub: Subscription = new Subscription();
|
|
|
|
private UserSub: Subscription = new Subscription();
|
2021-11-02 21:13:24 +00:00
|
|
|
public heightUpdate: Observable<number>;
|
|
|
|
public uZaddrUpdate: Observable<string>;
|
2021-10-15 19:14:49 +00:00
|
|
|
constructor(
|
2021-10-21 15:23:58 +00:00
|
|
|
private activatedRoute: ActivatedRoute,
|
2021-10-15 19:14:49 +00:00
|
|
|
public fullnodeService: FullnodeService,
|
|
|
|
private router: Router,
|
2021-11-02 21:13:24 +00:00
|
|
|
public userService: UserService,
|
|
|
|
private _changeDetectorRef: ChangeDetectorRef
|
2021-10-15 19:14:49 +00:00
|
|
|
){
|
2021-10-21 15:23:58 +00:00
|
|
|
//this.fullnodeService.getAddr();
|
2021-11-02 21:13:24 +00:00
|
|
|
this.heightUpdate = fullnodeService.heightUpdate;
|
|
|
|
this.uZaddrUpdate = userService.uZaddrUpdate;
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit(){
|
2021-11-02 21:13:24 +00:00
|
|
|
//console.log('Activated route data in Component:::', this.activatedRoute.data);
|
2021-10-21 15:23:58 +00:00
|
|
|
this.activatedRoute.data.subscribe((addrData) => {
|
2021-11-02 21:13:24 +00:00
|
|
|
//console.log('FETCH ADDRESS', addrData);
|
2021-10-21 15:23:58 +00:00
|
|
|
this.nodeAddress = addrData.response.addr;
|
2021-11-02 21:13:24 +00:00
|
|
|
//console.log('Node addres ', this.nodeAddress);
|
2021-10-21 15:23:58 +00:00
|
|
|
var localToken = localStorage.getItem('s4z_token');
|
2021-11-02 21:13:24 +00:00
|
|
|
//console.log(localToken);
|
2021-10-21 15:23:58 +00:00
|
|
|
if(localToken == null){
|
|
|
|
var token = uuidv4();
|
|
|
|
localStorage.setItem('s4z_token', token);
|
2021-11-04 12:49:09 +00:00
|
|
|
localToken = token;
|
2021-10-21 15:23:58 +00:00
|
|
|
}
|
2021-11-04 12:49:09 +00:00
|
|
|
this.userService.findUser();
|
|
|
|
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('ZGO::'.concat(localToken!)))}`;
|
|
|
|
console.log(codeString);
|
|
|
|
var qrcode = new QRCode(document.getElementById("qrcode"), {
|
|
|
|
text: codeString,
|
|
|
|
logo: "/assets/zcash.png",
|
|
|
|
logoWidth: 80,
|
|
|
|
logoHeight: 80
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2021-10-21 15:23:58 +00:00
|
|
|
});
|
2021-11-02 21:13:24 +00:00
|
|
|
this.intervalHolder = setInterval(() => {
|
|
|
|
this.fullnodeService.getHeight();
|
2021-11-04 12:49:09 +00:00
|
|
|
//this.userService.findUser();
|
|
|
|
this.loginCheck();
|
2021-11-02 21:13:24 +00:00
|
|
|
this._changeDetectorRef.markForCheck();
|
|
|
|
}, 1000 * 75);
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 12:49:09 +00:00
|
|
|
loginCheck(){
|
|
|
|
this.userService.findUser();
|
|
|
|
this.uZaddrUpdate.subscribe((userAddr: string) => {
|
|
|
|
if (userAddr.length != 0) {
|
|
|
|
console.log('Log in found in blockchain!');
|
|
|
|
this.router.navigate(['/view']);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-15 19:14:49 +00:00
|
|
|
ngOnDestroy(){
|
|
|
|
this.FullnodeSub.unsubscribe();
|
|
|
|
this.UserSub.unsubscribe();
|
2021-11-02 21:13:24 +00:00
|
|
|
clearInterval(this.intervalHolder);
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
}
|