From f13ae6dd2a12a835fbd0b7989d7501918b78a056 Mon Sep 17 00:00:00 2001 From: Blair Vanderlugt Date: Tue, 22 Jun 2021 20:49:25 +0000 Subject: [PATCH] refactor registry service --- src/app/_eth/accountIndex.ts | 3 + src/app/_guards/auth.guard.ts | 2 +- .../_interceptors/http-config.interceptor.ts | 13 ++-- src/app/_services/registry.service.ts | 63 ++++++++++++++----- src/app/_services/user.service.ts | 23 ++++--- src/environments/environment.dev.ts | 2 +- src/environments/environment.prod.ts | 2 +- src/environments/environment.ts | 2 +- 8 files changed, 80 insertions(+), 30 deletions(-) diff --git a/src/app/_eth/accountIndex.ts b/src/app/_eth/accountIndex.ts index c2fe057..42b115d 100644 --- a/src/app/_eth/accountIndex.ts +++ b/src/app/_eth/accountIndex.ts @@ -34,6 +34,9 @@ export class AccountIndex { constructor(contractAddress: string, signerAddress?: string) { this.contractAddress = contractAddress; this.contract = new web3.eth.Contract(abi, this.contractAddress); + // TODO this signer logic should be part of the web3service + // if signer address is not passed (for example in user service) then + // this fallsback to a web3 wallet that is not even connected??? if (signerAddress) { this.signerAddress = signerAddress; } else { diff --git a/src/app/_guards/auth.guard.ts b/src/app/_guards/auth.guard.ts index 395d37b..f2383ec 100644 --- a/src/app/_guards/auth.guard.ts +++ b/src/app/_guards/auth.guard.ts @@ -39,7 +39,7 @@ export class AuthGuard implements CanActivate { route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable | Promise | boolean | UrlTree { - if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) { + if (sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'))) { return true; } this.router.navigate(['/auth']); diff --git a/src/app/_interceptors/http-config.interceptor.ts b/src/app/_interceptors/http-config.interceptor.ts index 0bce86a..cb4431c 100644 --- a/src/app/_interceptors/http-config.interceptor.ts +++ b/src/app/_interceptors/http-config.interceptor.ts @@ -4,6 +4,7 @@ import { Injectable } from '@angular/core'; // Third party imports import { Observable } from 'rxjs'; +import { environment } from '@src/environments/environment'; /** Intercepts and handles setting of configurations to outgoing HTTP request. */ @Injectable() @@ -19,11 +20,15 @@ export class HttpConfigInterceptor implements HttpInterceptor { * @returns The forwarded request. */ intercept(request: HttpRequest, next: HttpHandler): Observable> { - // const token: string = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')); + if (request.url.startsWith(environment.cicMetaUrl)) { + const token: string = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')); - // 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/registry.service.ts b/src/app/_services/registry.service.ts index 7abfd98..5a6a879 100644 --- a/src/app/_services/registry.service.ts +++ b/src/app/_services/registry.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@angular/core'; import { environment } from '@src/environments/environment'; import { CICRegistry, FileGetter } from '@cicnet/cic-client'; +import { TokenRegistry, AccountIndex } from '@app/_eth'; import { HttpGetter } from '@app/_helpers'; import { Web3Service } from '@app/_services/web3.service'; @@ -10,21 +11,55 @@ import { Web3Service } from '@app/_services/web3.service'; export class RegistryService { static fileGetter: FileGetter = new HttpGetter(); private static registry: CICRegistry; - - constructor() {} + private static tokenRegistry: TokenRegistry; + private static accountRegistry: AccountIndex; public static async getRegistry(): Promise { - if (!RegistryService.registry) { - RegistryService.registry = new CICRegistry( - Web3Service.getInstance(), - environment.registryAddress, - 'Registry', - RegistryService.fileGetter, - ['../../assets/js/block-sync/data'] - ); - RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); - await RegistryService.registry.load(); - } - return RegistryService.registry; + return new Promise(async (resolve, reject) => { + if (!RegistryService.registry) { + RegistryService.registry = new CICRegistry( + Web3Service.getInstance(), + environment.registryAddress, + 'Registry', + RegistryService.fileGetter, + ['../../assets/js/block-sync/data'] + ); + RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); + await RegistryService.registry.load(); + return resolve(RegistryService.registry); + } + return resolve(RegistryService.registry); + }); + } + + public static async getTokenRegistry(): Promise { + return new Promise(async (resolve, reject) => { + if (!RegistryService.tokenRegistry) { + const registry = await RegistryService.getRegistry(); + const tokenRegistryAddress = await registry.getContractAddressByName('TokenRegistry'); + if (!tokenRegistryAddress) { + return reject('Unable to initialize Token Registry'); + } + RegistryService.tokenRegistry = new TokenRegistry(tokenRegistryAddress); + return resolve(RegistryService.tokenRegistry); + } + return resolve(RegistryService.tokenRegistry); + }); + } + + public static async getAccountRegistry(): Promise { + return new Promise(async (resolve, reject) => { + if (!RegistryService.accountRegistry) { + const registry = await RegistryService.getRegistry(); + const accountRegistryAddress = await registry.getContractAddressByName('AccountRegistry'); + + if (!accountRegistryAddress) { + return reject('Unable to initialize Account Registry'); + } + RegistryService.accountRegistry = new AccountIndex(accountRegistryAddress); + return resolve(RegistryService.accountRegistry); + } + return resolve(RegistryService.accountRegistry); + }); } } diff --git a/src/app/_services/user.service.ts b/src/app/_services/user.service.ts index 1844e78..914e73f 100644 --- a/src/app/_services/user.service.ts +++ b/src/app/_services/user.service.ts @@ -179,14 +179,21 @@ export class UserService { async loadAccounts(limit: number = 100, offset: number = 0): Promise { this.resetAccountsList(); - const accountIndexAddress: string = await this.registry.getContractAddressByName( - 'AccountRegistry' - ); - const accountIndexQuery = new AccountIndex(accountIndexAddress); - const accountAddresses: Array = await accountIndexQuery.last(limit); - this.loggingService.sendInfoLevelMessage(accountAddresses); - for (const accountAddress of accountAddresses.slice(offset, offset + limit)) { - await this.getAccountByAddress(accountAddress, limit); + // const accountIndexAddress: string = await this.registry.getContractAddressByName( + // 'AccountRegistry' + // ); + // const accountIndexQuery = new AccountIndex(accountIndexAddress); + // const accountAddresses: Array = await accountIndexQuery.last(limit); + try { + const accountRegistry = await RegistryService.getAccountRegistry(); + const accountAddresses: Array = await accountRegistry.last(limit); + this.loggingService.sendInfoLevelMessage(accountAddresses); + for (const accountAddress of accountAddresses.slice(offset, offset + limit)) { + await this.getAccountByAddress(accountAddress, limit); + } + } catch (error) { + this.loggingService.sendErrorLevelMessage('Unable to load accounts.', 'user.service', error); + throw error; } } diff --git a/src/environments/environment.dev.ts b/src/environments/environment.dev.ts index 978f71f..cde24f3 100644 --- a/src/environments/environment.dev.ts +++ b/src/environments/environment.dev.ts @@ -6,7 +6,7 @@ export const environment = { logLevel: NgxLoggerLevel.ERROR, serverLogLevel: NgxLoggerLevel.OFF, loggingUrl: '', - 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', diff --git a/src/environments/environment.prod.ts b/src/environments/environment.prod.ts index f4786c1..32cc50b 100644 --- a/src/environments/environment.prod.ts +++ b/src/environments/environment.prod.ts @@ -6,7 +6,7 @@ export const environment = { logLevel: NgxLoggerLevel.ERROR, serverLogLevel: NgxLoggerLevel.OFF, loggingUrl: '', - 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', 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',