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) {
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 {

View File

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

View File

@ -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<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) {
// 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);
}

View File

@ -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<CICRegistry> {
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<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,14 +180,21 @@ export class UserService {
async loadAccounts(limit: number = 100, offset: number = 0): Promise<void> {
this.resetAccountsList();
const accountIndexAddress: string = await this.registry.getContractAddressByName(
'AccountRegistry'
);
const accountIndexQuery = new AccountIndex(accountIndexAddress);
const accountAddresses: Array<string> = 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<string> = await accountIndexQuery.last(limit);
try {
const accountRegistry = await RegistryService.getAccountRegistry();
const accountAddresses: Array<string> = 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;
}
}