Merge branch 'bvander/refactor-registry-service' into 'master'
refactor registry service See merge request grassrootseconomics/cic-staff-client!34
This commit is contained in:
commit
a69213dab2
@ -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 {
|
||||||
|
@ -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']);
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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,21 +11,55 @@ 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> {
|
||||||
if (!RegistryService.registry) {
|
return new Promise(async (resolve, reject) => {
|
||||||
RegistryService.registry = new CICRegistry(
|
if (!RegistryService.registry) {
|
||||||
Web3Service.getInstance(),
|
RegistryService.registry = new CICRegistry(
|
||||||
environment.registryAddress,
|
Web3Service.getInstance(),
|
||||||
'Registry',
|
environment.registryAddress,
|
||||||
RegistryService.fileGetter,
|
'Registry',
|
||||||
['../../assets/js/block-sync/data']
|
RegistryService.fileGetter,
|
||||||
);
|
['../../assets/js/block-sync/data']
|
||||||
RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress);
|
);
|
||||||
await RegistryService.registry.load();
|
RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress);
|
||||||
}
|
await RegistryService.registry.load();
|
||||||
return RegistryService.registry;
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -180,14 +180,21 @@ 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);
|
||||||
this.loggingService.sendInfoLevelMessage(accountAddresses);
|
try {
|
||||||
for (const accountAddress of accountAddresses.slice(offset, offset + limit)) {
|
const accountRegistry = await RegistryService.getAccountRegistry();
|
||||||
await this.getAccountByAddress(accountAddress, limit);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user