Merge branch 'spencer/keystore-singleton' into 'master'

Refactor keystore into singleton service.

See merge request grassrootseconomics/cic-staff-client!31
This commit is contained in:
Spencer Ofwiti 2021-06-15 16:22:02 +00:00
commit 5295e47cc1
9 changed files with 52 additions and 10 deletions

View File

@ -3,12 +3,13 @@ import { hobaParseChallengeHeader } from '@src/assets/js/hoba.js';
import { signChallenge } from '@src/assets/js/hoba-pgp.js'; import { signChallenge } from '@src/assets/js/hoba-pgp.js';
import { environment } from '@src/environments/environment'; import { environment } from '@src/environments/environment';
import { LoggingService } from '@app/_services/logging.service'; import { LoggingService } from '@app/_services/logging.service';
import { MutableKeyStore, MutablePgpKeyStore } from '@app/_pgp'; import { MutableKeyStore } from '@app/_pgp';
import { ErrorDialogService } from '@app/_services/error-dialog.service'; import { ErrorDialogService } from '@app/_services/error-dialog.service';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { HttpError, rejectBody } from '@app/_helpers/global-error-handler'; import { HttpError, rejectBody } from '@app/_helpers/global-error-handler';
import { Staff } from '@app/_models'; import { Staff } from '@app/_models';
import { BehaviorSubject, Observable } from 'rxjs'; import { BehaviorSubject, Observable } from 'rxjs';
import { KeystoreService } from '@app/_services/keystore.service';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -25,12 +26,10 @@ export class AuthService {
private httpClient: HttpClient, private httpClient: HttpClient,
private loggingService: LoggingService, private loggingService: LoggingService,
private errorDialogService: ErrorDialogService private errorDialogService: ErrorDialogService
) { ) {}
this.mutableKeyStore = new MutablePgpKeyStore();
}
async init(): Promise<void> { async init(): Promise<void> {
await this.mutableKeyStore.loadKeyring(); this.mutableKeyStore = await KeystoreService.getKeystore();
if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) { if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) {
await this.mutableKeyStore.importPrivateKey(localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))); await this.mutableKeyStore.importPrivateKey(localStorage.getItem(btoa('CICADA_PRIVATE_KEY')));
} }

View File

@ -7,3 +7,5 @@ export * from '@app/_services/location.service';
export * from '@app/_services/logging.service'; export * from '@app/_services/logging.service';
export * from '@app/_services/error-dialog.service'; export * from '@app/_services/error-dialog.service';
export * from '@app/_services/web3.service'; export * from '@app/_services/web3.service';
export * from '@app/_services/keystore.service';
export * from '@app/_services/registry.service';

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { KeystoreService } from './keystore.service';
describe('KeystoreService', () => {
let service: KeystoreService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(KeystoreService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,22 @@
import { Injectable } from '@angular/core';
import { MutableKeyStore, MutablePgpKeyStore } from '@app/_pgp';
@Injectable({
providedIn: 'root',
})
export class KeystoreService {
private static mutableKeyStore: MutableKeyStore;
constructor() {}
public static async getKeystore(): Promise<MutableKeyStore> {
return new Promise(async (resolve, reject) => {
if (!KeystoreService.mutableKeyStore) {
this.mutableKeyStore = new MutablePgpKeyStore();
await this.mutableKeyStore.loadKeyring();
return resolve(KeystoreService.mutableKeyStore);
}
return resolve(KeystoreService.mutableKeyStore);
});
}
}

View File

@ -18,6 +18,7 @@ import { CICRegistry } from '@cicnet/cic-client';
import { RegistryService } from '@app/_services/registry.service'; import { RegistryService } from '@app/_services/registry.service';
import Web3 from 'web3'; import Web3 from 'web3';
import { Web3Service } from '@app/_services/web3.service'; import { Web3Service } from '@app/_services/web3.service';
import { KeystoreService } from '@app/_services/keystore.service';
const vCard = require('vcard-parser'); const vCard = require('vcard-parser');
@Injectable({ @Injectable({
@ -169,7 +170,8 @@ export class TransactionService {
tx.value = toValue(value); tx.value = toValue(value);
tx.data = data; tx.data = data;
const txMsg = tx.message(); const txMsg = tx.message();
const privateKey = this.authService.mutableKeyStore.getPrivateKey(); const keystore = await KeystoreService.getKeystore();
const privateKey = keystore.getPrivateKey();
if (!privateKey.isDecrypted()) { if (!privateKey.isDecrypted()) {
const password = window.prompt('password'); const password = window.prompt('password');
await privateKey.decrypt(password); await privateKey.decrypt(password);

View File

@ -14,6 +14,7 @@ import { CICRegistry } from '@cicnet/cic-client';
import { AuthService } from '@app/_services/auth.service'; import { AuthService } from '@app/_services/auth.service';
import { personValidation, vcardValidation } from '@app/_helpers'; import { personValidation, vcardValidation } from '@app/_helpers';
import { add0x } from '@src/assets/js/ethtx/dist/hex'; import { add0x } from '@src/assets/js/ethtx/dist/hex';
import { KeystoreService } from '@app/_services/keystore.service';
const vCard = require('vcard-parser'); const vCard = require('vcard-parser');
@Injectable({ @Injectable({
@ -45,7 +46,7 @@ export class UserService {
async init(): Promise<void> { async init(): Promise<void> {
await this.authService.init(); await this.authService.init();
await this.tokenService.init(); await this.tokenService.init();
this.keystore = this.authService.mutableKeyStore; this.keystore = await KeystoreService.getKeystore();
this.signer = new PGPSigner(this.keystore); this.signer = new PGPSigner(this.keystore);
this.registry = await RegistryService.getRegistry(); this.registry = await RegistryService.getRegistry();
} }

View File

@ -6,7 +6,7 @@ export const environment = {
logLevel: NgxLoggerLevel.ERROR, logLevel: NgxLoggerLevel.ERROR,
serverLogLevel: NgxLoggerLevel.OFF, serverLogLevel: NgxLoggerLevel.OFF,
loggingUrl: '', loggingUrl: '',
cicMetaUrl: 'https://meta.dev.grassrootseconomics.net', cicMetaUrl: 'https://meta-auth.dev.grassrootseconomics.net',
publicKeysUrl: 'https://dev.grassrootseconomics.net/.well-known/publickeys/', publicKeysUrl: 'https://dev.grassrootseconomics.net/.well-known/publickeys/',
cicCacheUrl: 'https://cache.dev.grassrootseconomics.net', cicCacheUrl: 'https://cache.dev.grassrootseconomics.net',
web3Provider: 'wss://bloxberg-ws.dev.grassrootseconomics.net', web3Provider: 'wss://bloxberg-ws.dev.grassrootseconomics.net',

View File

@ -6,7 +6,7 @@ export const environment = {
logLevel: NgxLoggerLevel.ERROR, logLevel: NgxLoggerLevel.ERROR,
serverLogLevel: NgxLoggerLevel.OFF, serverLogLevel: NgxLoggerLevel.OFF,
loggingUrl: '', loggingUrl: '',
cicMetaUrl: 'https://meta.dev.grassrootseconomics.net', cicMetaUrl: 'https://meta-auth.dev.grassrootseconomics.net',
publicKeysUrl: 'https://dev.grassrootseconomics.net/.well-known/publickeys/', publicKeysUrl: 'https://dev.grassrootseconomics.net/.well-known/publickeys/',
cicCacheUrl: 'https://cache.dev.grassrootseconomics.net', cicCacheUrl: 'https://cache.dev.grassrootseconomics.net',
web3Provider: 'wss://bloxberg-ws.dev.grassrootseconomics.net', web3Provider: 'wss://bloxberg-ws.dev.grassrootseconomics.net',

View File

@ -6,7 +6,7 @@ export const environment = {
logLevel: NgxLoggerLevel.ERROR, logLevel: NgxLoggerLevel.ERROR,
serverLogLevel: NgxLoggerLevel.OFF, serverLogLevel: NgxLoggerLevel.OFF,
loggingUrl: 'http://localhost:8000', 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/', publicKeysUrl: 'https://dev.grassrootseconomics.net/.well-known/publickeys/',
cicCacheUrl: 'https://cache.dev.grassrootseconomics.net', cicCacheUrl: 'https://cache.dev.grassrootseconomics.net',
web3Provider: 'wss://bloxberg-ws.dev.grassrootseconomics.net', web3Provider: 'wss://bloxberg-ws.dev.grassrootseconomics.net',