From 61bf336789a3cfc8acd08a1d749abffde12e810c Mon Sep 17 00:00:00 2001 From: Blair Vanderlugt Date: Thu, 10 Jun 2021 13:56:47 -0700 Subject: [PATCH] refactor token service to use the token registry --- .../_interceptors/http-config.interceptor.ts | 10 +++--- src/app/_services/auth.service.ts | 6 ++-- src/app/_services/block-sync.service.ts | 2 +- src/app/_services/registry.service.ts | 32 ++++++++++++++++--- src/app/_services/token.service.ts | 15 +++++---- src/app/_services/transaction.service.ts | 2 +- src/app/_services/user.service.ts | 4 +-- src/environments/environment.ts | 2 +- 8 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/app/_interceptors/http-config.interceptor.ts b/src/app/_interceptors/http-config.interceptor.ts index fe9e74e..c0c6db4 100644 --- a/src/app/_interceptors/http-config.interceptor.ts +++ b/src/app/_interceptors/http-config.interceptor.ts @@ -1,3 +1,4 @@ +import { AuthService } from '@app/_services'; import { Injectable } from '@angular/core'; import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http'; import { Observable } from 'rxjs'; @@ -7,11 +8,12 @@ export class HttpConfigInterceptor implements HttpInterceptor { constructor() {} intercept(request: HttpRequest, next: HttpHandler): Observable> { - // 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) { - // request = request.clone({headers: request.headers.set('Authorization', 'Bearer ' + token)}); - // } + if (token) { + request = request.clone({headers: request.headers.set('Authorization', 'Bearer ' + token)}); + } return next.handle(request); } diff --git a/src/app/_services/auth.service.ts b/src/app/_services/auth.service.ts index 937f2ce..da8ec6c 100644 --- a/src/app/_services/auth.service.ts +++ b/src/app/_services/auth.service.ts @@ -36,7 +36,7 @@ export class AuthService { } } - getSessionToken(): string { + public static getSessionToken(): string { return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')); } @@ -50,7 +50,7 @@ export class AuthService { getWithToken(): Promise { const headers = { - Authorization: 'Bearer ' + this.getSessionToken, + Authorization: 'Bearer ' + AuthService.getSessionToken, 'Content-Type': 'application/json;charset=utf-8', 'x-cic-automerge': 'none', }; @@ -93,7 +93,7 @@ export class AuthService { } async login(): Promise { - if (this.getSessionToken()) { + if (AuthService.getSessionToken()) { sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN')); } else { const o = await this.getChallenge(); diff --git a/src/app/_services/block-sync.service.ts b/src/app/_services/block-sync.service.ts index 55b03e6..f983a6d 100644 --- a/src/app/_services/block-sync.service.ts +++ b/src/app/_services/block-sync.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; import { Settings } from '@app/_models'; -import { TransactionHelper } from 'cic-client'; +import { TransactionHelper } from '@cicnet/cic-client'; import { first } from 'rxjs/operators'; import { TransactionService } from '@app/_services/transaction.service'; import { environment } from '@src/environments/environment'; diff --git a/src/app/_services/registry.service.ts b/src/app/_services/registry.service.ts index 3aa990c..0f652fb 100644 --- a/src/app/_services/registry.service.ts +++ b/src/app/_services/registry.service.ts @@ -1,17 +1,24 @@ import { Injectable } from '@angular/core'; 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 { Web3Service } from '@app/_services/web3.service'; + +//export interface RegistryCollection { +// cicRegistry: CICRegistry, +// tokenRegistry: TokenRegistry +//} + @Injectable({ providedIn: 'root', }) export class RegistryService { static fileGetter: FileGetter = new HttpGetter(); private static registry: CICRegistry; - - constructor() {} + private static tokenRegistry: TokenRegistry; + //private static registries: RegistryCollection; public static async getRegistry(): Promise { if (!RegistryService.registry) { @@ -23,8 +30,25 @@ export class RegistryService { ['../../assets/js/block-sync/data'] ); RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); - await RegistryService.registry.load(); + await RegistryService.registry.load() + } return RegistryService.registry; } + + public static async getTokenRegistry(): Promise { + 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); + }) + } + } diff --git a/src/app/_services/token.service.ts b/src/app/_services/token.service.ts index d9ffed8..c52bfab 100644 --- a/src/app/_services/token.service.ts +++ b/src/app/_services/token.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core'; -import { CICRegistry } from 'cic-client'; +import { CICRegistry } from '@cicnet/cic-client'; import { TokenRegistry } from '@app/_eth'; import { HttpClient } from '@angular/common/http'; import { RegistryService } from '@app/_services/registry.service'; @@ -21,12 +21,13 @@ export class TokenService { async init(): Promise { this.registry = await RegistryService.getRegistry(); - this.registry.onload = async (address: string): Promise => { - this.tokenRegistry = new TokenRegistry( - await this.registry.getContractAddressByName('TokenRegistry') - ); - this.onload(this.tokenRegistry !== undefined); - }; + this.tokenRegistry = await RegistryService.getTokenRegistry(); + //this.registry.onload = async (address: string): Promise => { + // this.tokenRegistry = new TokenRegistry( + // await this.registry.getContractAddressByName('TokenRegistry') + // ); + // this.onload(this.tokenRegistry !== undefined); + //}; } addToken(token: Token): void { diff --git a/src/app/_services/transaction.service.ts b/src/app/_services/transaction.service.ts index 5a6c7d8..63937b0 100644 --- a/src/app/_services/transaction.service.ts +++ b/src/app/_services/transaction.service.ts @@ -14,7 +14,7 @@ import { AuthService } from '@app/_services/auth.service'; import { defaultAccount } from '@app/_models'; import { LoggingService } from '@app/_services/logging.service'; 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 Web3 from 'web3'; import { Web3Service } from '@app/_services/web3.service'; diff --git a/src/app/_services/user.service.ts b/src/app/_services/user.service.ts index e1db2d5..82b6551 100644 --- a/src/app/_services/user.service.ts +++ b/src/app/_services/user.service.ts @@ -10,7 +10,7 @@ import { TokenService } from '@app/_services/token.service'; import { AccountIndex } from '@app/_eth'; import { MutableKeyStore, PGPSigner, Signer } from '@app/_pgp'; 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 { personValidation, vcardValidation } from '@app/_helpers'; import { add0x } from '@src/assets/js/ethtx/dist/hex'; @@ -43,7 +43,7 @@ export class UserService { ) {} async init(): Promise { - await this.authService.init(); + // await this.authService.init(); await this.tokenService.init(); this.keystore = this.authService.mutableKeyStore; this.signer = new PGPSigner(this.keystore); diff --git a/src/environments/environment.ts b/src/environments/environment.ts index df02af7..f2d7c4f 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -6,7 +6,7 @@ export const environment = { logLevel: NgxLoggerLevel.ERROR, serverLogLevel: NgxLoggerLevel.OFF, 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/', cicCacheUrl: 'https://cache.dev.grassrootseconomics.net', web3Provider: 'wss://bloxberg-ws.dev.grassrootseconomics.net',