Merge branch 'bvander/refactor-registry-service' into 'master'

refactor registry service

See merge request grassrootseconomics/cic-staff-client!34
This commit is contained in:
Blair Vanderlugt 2021-06-22 20:49:25 +00:00
commit a69213dab2
5 changed files with 77 additions and 27 deletions

View File

@ -34,6 +34,9 @@ export class AccountIndex {
constructor(contractAddress: string, signerAddress?: string) { constructor(contractAddress: string, signerAddress?: string) {
this.contractAddress = contractAddress; this.contractAddress = contractAddress;
this.contract = new web3.eth.Contract(abi, this.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) { if (signerAddress) {
this.signerAddress = signerAddress; this.signerAddress = signerAddress;
} else { } else {

View File

@ -39,7 +39,7 @@ export class AuthGuard implements CanActivate {
route: ActivatedRouteSnapshot, route: ActivatedRouteSnapshot,
state: RouterStateSnapshot state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) { if (sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'))) {
return true; return true;
} }
this.router.navigate(['/auth']); this.router.navigate(['/auth']);

View File

@ -4,6 +4,7 @@ import { Injectable } from '@angular/core';
// Third party imports // Third party imports
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { environment } from '@src/environments/environment';
/** Intercepts and handles setting of configurations to outgoing HTTP request. */ /** Intercepts and handles setting of configurations to outgoing HTTP request. */
@Injectable() @Injectable()
@ -19,11 +20,15 @@ export class HttpConfigInterceptor implements HttpInterceptor {
* @returns The forwarded request. * @returns The forwarded request.
*/ */
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')); if (request.url.startsWith(environment.cicMetaUrl)) {
const token: string = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
// 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

@ -1,6 +1,7 @@
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 '@cicnet/cic-client'; import { CICRegistry, FileGetter } from '@cicnet/cic-client';
import { TokenRegistry, AccountIndex } 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';
@ -10,10 +11,11 @@ import { Web3Service } from '@app/_services/web3.service';
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 accountRegistry: AccountIndex;
public static async getRegistry(): Promise<CICRegistry> { public static async getRegistry(): Promise<CICRegistry> {
return new Promise(async (resolve, reject) => {
if (!RegistryService.registry) { if (!RegistryService.registry) {
RegistryService.registry = new CICRegistry( RegistryService.registry = new CICRegistry(
Web3Service.getInstance(), Web3Service.getInstance(),
@ -24,7 +26,40 @@ export class RegistryService {
); );
RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress);
await RegistryService.registry.load(); await RegistryService.registry.load();
return resolve(RegistryService.registry);
} }
return RegistryService.registry; return resolve(RegistryService.registry);
});
}
public static async getTokenRegistry(): Promise<TokenRegistry> {
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<AccountIndex> {
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);
});
} }
} }

View File

@ -180,15 +180,22 @@ export class UserService {
async loadAccounts(limit: number = 100, offset: number = 0): Promise<void> { async loadAccounts(limit: number = 100, offset: number = 0): Promise<void> {
this.resetAccountsList(); this.resetAccountsList();
const accountIndexAddress: string = await this.registry.getContractAddressByName( // const accountIndexAddress: string = await this.registry.getContractAddressByName(
'AccountRegistry' // 'AccountRegistry'
); // );
const accountIndexQuery = new AccountIndex(accountIndexAddress); // const accountIndexQuery = new AccountIndex(accountIndexAddress);
const accountAddresses: Array<string> = await accountIndexQuery.last(limit); // const accountAddresses: Array<string> = await accountIndexQuery.last(limit);
try {
const accountRegistry = await RegistryService.getAccountRegistry();
const accountAddresses: Array<string> = await accountRegistry.last(limit);
this.loggingService.sendInfoLevelMessage(accountAddresses); this.loggingService.sendInfoLevelMessage(accountAddresses);
for (const accountAddress of accountAddresses.slice(offset, offset + limit)) { for (const accountAddress of accountAddresses.slice(offset, offset + limit)) {
await this.getAccountByAddress(accountAddress, limit); await this.getAccountByAddress(accountAddress, limit);
} }
} catch (error) {
this.loggingService.sendErrorLevelMessage('Unable to load accounts.', 'user.service', error);
throw error;
}
} }
async getAccountByAddress( async getAccountByAddress(