refactor token service to use the token registry

This commit is contained in:
Blair Vanderlugt 2021-06-10 13:56:47 -07:00
parent 060c47f840
commit 61bf336789
8 changed files with 50 additions and 23 deletions

View File

@ -1,3 +1,4 @@
import { AuthService } from '@app/_services';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http'; import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
@ -7,11 +8,12 @@ export class HttpConfigInterceptor implements HttpInterceptor {
constructor() {} constructor() {}
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
// const token: string = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')); //const token: string = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
const token: string = AuthService.getSessionToken()
// if (token) { if (token) {
// request = request.clone({headers: request.headers.set('Authorization', 'Bearer ' + token)}); request = request.clone({headers: request.headers.set('Authorization', 'Bearer ' + token)});
// } }
return next.handle(request); return next.handle(request);
} }

View File

@ -36,7 +36,7 @@ export class AuthService {
} }
} }
getSessionToken(): string { public static getSessionToken(): string {
return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')); return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
} }
@ -50,7 +50,7 @@ export class AuthService {
getWithToken(): Promise<boolean> { getWithToken(): Promise<boolean> {
const headers = { const headers = {
Authorization: 'Bearer ' + this.getSessionToken, Authorization: 'Bearer ' + AuthService.getSessionToken,
'Content-Type': 'application/json;charset=utf-8', 'Content-Type': 'application/json;charset=utf-8',
'x-cic-automerge': 'none', 'x-cic-automerge': 'none',
}; };
@ -93,7 +93,7 @@ export class AuthService {
} }
async login(): Promise<boolean> { async login(): Promise<boolean> {
if (this.getSessionToken()) { if (AuthService.getSessionToken()) {
sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN')); sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN'));
} else { } else {
const o = await this.getChallenge(); const o = await this.getChallenge();

View File

@ -1,6 +1,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Settings } from '@app/_models'; import { Settings } from '@app/_models';
import { TransactionHelper } from 'cic-client'; import { TransactionHelper } from '@cicnet/cic-client';
import { first } from 'rxjs/operators'; import { first } from 'rxjs/operators';
import { TransactionService } from '@app/_services/transaction.service'; import { TransactionService } from '@app/_services/transaction.service';
import { environment } from '@src/environments/environment'; import { environment } from '@src/environments/environment';

View File

@ -1,17 +1,24 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { environment } from '@src/environments/environment'; import { environment } from '@src/environments/environment';
import { CICRegistry, FileGetter } from 'cic-client'; import { CICRegistry, FileGetter } from '@cicnet/cic-client';
import { TokenRegistry } from '@app/_eth';
import { HttpGetter } from '@app/_helpers'; import { HttpGetter } from '@app/_helpers';
import { Web3Service } from '@app/_services/web3.service'; import { Web3Service } from '@app/_services/web3.service';
//export interface RegistryCollection {
// cicRegistry: CICRegistry,
// tokenRegistry: TokenRegistry
//}
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class RegistryService { export class RegistryService {
static fileGetter: FileGetter = new HttpGetter(); static fileGetter: FileGetter = new HttpGetter();
private static registry: CICRegistry; private static registry: CICRegistry;
private static tokenRegistry: TokenRegistry;
constructor() {} //private static registries: RegistryCollection;
public static async getRegistry(): Promise<CICRegistry> { public static async getRegistry(): Promise<CICRegistry> {
if (!RegistryService.registry) { if (!RegistryService.registry) {
@ -23,8 +30,25 @@ export class RegistryService {
['../../assets/js/block-sync/data'] ['../../assets/js/block-sync/data']
); );
RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress);
await RegistryService.registry.load(); await RegistryService.registry.load()
} }
return RegistryService.registry; return RegistryService.registry;
} }
public static async getTokenRegistry(): Promise<TokenRegistry> {
if (!RegistryService.tokenRegistry) {
//then initial it
const registry = await RegistryService.getRegistry()
const tokenRegistryAddress = await RegistryService.registry.getContractAddressByName('TokenRegistry')
RegistryService.tokenRegistry = new TokenRegistry(tokenRegistryAddress);
return new Promise((resolve, reject) => {
resolve(RegistryService.tokenRegistry)
})
}
return new Promise((resolve, reject) => {
resolve(RegistryService.tokenRegistry);
})
}
} }

View File

@ -1,5 +1,5 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CICRegistry } from 'cic-client'; import { CICRegistry } from '@cicnet/cic-client';
import { TokenRegistry } from '@app/_eth'; import { TokenRegistry } from '@app/_eth';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { RegistryService } from '@app/_services/registry.service'; import { RegistryService } from '@app/_services/registry.service';
@ -21,12 +21,13 @@ export class TokenService {
async init(): Promise<void> { async init(): Promise<void> {
this.registry = await RegistryService.getRegistry(); this.registry = await RegistryService.getRegistry();
this.registry.onload = async (address: string): Promise<void> => { this.tokenRegistry = await RegistryService.getTokenRegistry();
this.tokenRegistry = new TokenRegistry( //this.registry.onload = async (address: string): Promise<void> => {
await this.registry.getContractAddressByName('TokenRegistry') // this.tokenRegistry = new TokenRegistry(
); // await this.registry.getContractAddressByName('TokenRegistry')
this.onload(this.tokenRegistry !== undefined); // );
}; // this.onload(this.tokenRegistry !== undefined);
//};
} }
addToken(token: Token): void { addToken(token: Token): void {

View File

@ -14,7 +14,7 @@ import { AuthService } from '@app/_services/auth.service';
import { defaultAccount } from '@app/_models'; import { defaultAccount } from '@app/_models';
import { LoggingService } from '@app/_services/logging.service'; import { LoggingService } from '@app/_services/logging.service';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { CICRegistry } from 'cic-client'; import { CICRegistry } from '@cicnet/cic-client';
import { RegistryService } from '@app/_services/registry.service'; import { RegistryService } from '@app/_services/registry.service';
import Web3 from 'web3'; import Web3 from 'web3';
import { Web3Service } from '@app/_services/web3.service'; import { Web3Service } from '@app/_services/web3.service';

View File

@ -10,7 +10,7 @@ import { TokenService } from '@app/_services/token.service';
import { AccountIndex } from '@app/_eth'; import { AccountIndex } from '@app/_eth';
import { MutableKeyStore, PGPSigner, Signer } from '@app/_pgp'; import { MutableKeyStore, PGPSigner, Signer } from '@app/_pgp';
import { RegistryService } from '@app/_services/registry.service'; import { RegistryService } from '@app/_services/registry.service';
import { CICRegistry } from 'cic-client'; import { CICRegistry } from '@cicnet/cic-client';
import { AuthService } from '@app/_services/auth.service'; import { AuthService } from '@app/_services/auth.service';
import { personValidation, vcardValidation } from '@app/_helpers'; import { personValidation, vcardValidation } from '@app/_helpers';
import { add0x } from '@src/assets/js/ethtx/dist/hex'; import { add0x } from '@src/assets/js/ethtx/dist/hex';
@ -43,7 +43,7 @@ export class UserService {
) {} ) {}
async init(): Promise<void> { async init(): Promise<void> {
await this.authService.init(); // await this.authService.init();
await this.tokenService.init(); await this.tokenService.init();
this.keystore = this.authService.mutableKeyStore; this.keystore = this.authService.mutableKeyStore;
this.signer = new PGPSigner(this.keystore); this.signer = new PGPSigner(this.keystore);

View File

@ -6,7 +6,7 @@ export const environment = {
logLevel: NgxLoggerLevel.ERROR, logLevel: NgxLoggerLevel.ERROR,
serverLogLevel: NgxLoggerLevel.OFF, serverLogLevel: NgxLoggerLevel.OFF,
loggingUrl: 'http://localhost:8000', loggingUrl: 'http://localhost:8000',
cicMetaUrl: 'https://meta.dev.grassrootseconomics.net', cicMetaUrl: 'https://meta-auth.dev.grassrootseconomics.net',
publicKeysUrl: 'https://dev.grassrootseconomics.net/.well-known/publickeys/', publicKeysUrl: 'https://dev.grassrootseconomics.net/.well-known/publickeys/',
cicCacheUrl: 'https://cache.dev.grassrootseconomics.net', cicCacheUrl: 'https://cache.dev.grassrootseconomics.net',
web3Provider: 'wss://bloxberg-ws.dev.grassrootseconomics.net', web3Provider: 'wss://bloxberg-ws.dev.grassrootseconomics.net',