Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
siepra committed Nov 16, 2023
1 parent 00180bf commit 877a038
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 140 deletions.
239 changes: 121 additions & 118 deletions packages/backend/src/nest/storage/certificates/certificates.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { StorageEvents } from '../storage.types'
import EventStore from 'orbit-db-eventstore'
import OrbitDB from 'orbit-db'

import { keyFromCertificate, CertFieldsTypes, parseCertificate, getReqFieldValue, getCertFieldValue } from '@quiet/identity'
import {
keyFromCertificate,
CertFieldsTypes,
parseCertificate,
getReqFieldValue,
getCertFieldValue,
} from '@quiet/identity'

import { ConnectionProcessInfo, SocketActionTypes, UserData } from '@quiet/types'

Expand All @@ -13,128 +19,125 @@ import createLogger from '../../common/logger'
const logger = createLogger('CertificatesStore')

export class CertificatesStore {
public orbitDb: OrbitDB
public store: EventStore<string>

private filteredCertificatesMapping: Map<string, Partial<UserData>>
private usernameMapping: Map<string, string>

constructor(orbitDb: OrbitDB) {
this.orbitDb = orbitDb
this.filteredCertificatesMapping = new Map()
this.usernameMapping = new Map()
public orbitDb: OrbitDB
public store: EventStore<string>

private filteredCertificatesMapping: Map<string, Partial<UserData>>
private usernameMapping: Map<string, string>

constructor(orbitDb: OrbitDB) {
this.orbitDb = orbitDb
this.filteredCertificatesMapping = new Map()
this.usernameMapping = new Map()
}

public async init(emitter: EventEmitter) {
logger('Initializing certificates log store')

this.store = await this.orbitDb.log<string>('certificates', {
replicate: false,
accessController: {
write: ['*'],
},
})

this.store.events.on('ready', async () => {
logger('Loaded certificates to memory')
emitter.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CERTIFICATES_REPLICATED)
})

this.store.events.on('write', async () => {
logger('Saved certificate locally')
await loadedCertificates()
})

this.store.events.on('replicated', async () => {
logger('REPLICATED: Certificates')
emitter.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CERTIFICATES_REPLICATED)
await loadedCertificates()
})

const loadedCertificates = async () => {
console.log('dupa')
emitter.emit(StorageEvents.LOADED_CERTIFICATES, {
certificates: await this.getCertificates(),
})
}

public async init(emitter: EventEmitter) {
logger('Initializing certificates log store')

this.store = await this.orbitDb.log<string>('certificates', {
replicate: false,
accessController: {
write: ['*'],
},
})

this.store.events.on('ready', async () => {
logger('Loaded certificates to memory')
emitter.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CERTIFICATES_REPLICATED)
})

this.store.events.on('write', async () => {
logger('Saved certificate locally')
await loadedCertificates()
})

this.store.events.on('replicated', async () => {
logger('REPLICATED: Certificates')
emitter.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CERTIFICATES_REPLICATED)
await loadedCertificates()
})

const loadedCertificates = async () => {
console.log('dupa')
emitter.emit(StorageEvents.LOADED_CERTIFICATES, {
certificates: await this.getCertificates(),
})
// @ts-expect-error - OrbitDB's type declaration of `load` lacks 'options'
await this.store.load({ fetchEntryTimeout: 15000 })

logger('Initialized')
}

public async close() {
await this.store?.close()
}

public getAddress() {
return this.store?.address
}

public async addCertificate(certificate: string) {
logger('Adding user certificate')
await this.store.add(certificate)
return true
}

public async loadAllCertificates() {
return this.getCertificates()
}

/*
* In order to split up the work scope, we mock validating function,
* until we move the certificates section into it's own store
*/
private async validateCertificate(_certificate: string): Promise<boolean> {
logger('Validating certificate')
return true
}

/*
* Method returning store entries, filtered by validation result
* as specified in the comment section of
* https://github.com/TryQuiet/quiet/issues/1899
*/
protected async getCertificates() {
const allCertificates = this.store
.iterator({ limit: -1 })
.collect()
.map(e => e.payload.value)

const validCertificates = allCertificates.map(async certificate => {
if (this.filteredCertificatesMapping.has(certificate)) {
return certificate // Only validate certificates
}

const validation = await this.validateCertificate(certificate)
if (validation) {
const parsedCertificate = parseCertificate(certificate)
const pubkey = keyFromCertificate(parsedCertificate)

const username = getCertFieldValue(parsedCertificate, CertFieldsTypes.nickName)

// @ts-expect-error
this.usernameMapping.set(pubkey, username)

const data: Partial<UserData> = {
// @ts-expect-error
username: username,
}

// @ts-expect-error - OrbitDB's type declaration of `load` lacks 'options'
await this.store.load({ fetchEntryTimeout: 15000 })

logger('Initialized')
}

public async close() {
await this.store?.close()
}

public getAddress() {
return this.store?.address
}
this.filteredCertificatesMapping.set(certificate, data)

public async addCertificate(certificate: string) {
logger('Adding user certificate')
await this.store.add(certificate)
return true
}
return certificate
}
})

public async loadAllCertificates() {
return this.getCertificates()
}

/*
* In order to split up the work scope, we mock validating function,
* until we move the certificates section into it's own store
*/
private async validateCertificate(_certificate: string): Promise<boolean> {
logger('Validating certificate')
return true
}

/*
* Method returning store entries, filtered by validation result
* as specified in the comment section of
* https://github.com/TryQuiet/quiet/issues/1899
*/
protected async getCertificates() {

const allCertificates = this.store
.iterator({ limit: -1 })
.collect()
.map(e => e.payload.value)

const validCertificates = allCertificates.map(async (certificate) => {
if (this.filteredCertificatesMapping.has(certificate)) {
return certificate // Only validate certificates
}

const validation = await this.validateCertificate(certificate)
if (validation) {

const parsedCertificate = parseCertificate(certificate)
const pubkey = keyFromCertificate(parsedCertificate)

const username = getCertFieldValue(parsedCertificate, CertFieldsTypes.nickName)

// @ts-expect-error
this.usernameMapping.set(pubkey, username)

const data: Partial<UserData> = {
// @ts-expect-error
username: username
}

this.filteredCertificatesMapping.set(certificate, data)

return certificate
}
})

return validCertificates.filter(i => Boolean(i)) // Filter out undefineds
}

public getCertificateUsername(pubkey: string) {
return this.usernameMapping.get(pubkey)
}
return validCertificates.filter(i => Boolean(i)) // Filter out undefineds
}

public getCertificateUsername(pubkey: string) {
return this.usernameMapping.get(pubkey)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ describe('CertificatesRequestsStore csr validation methods', () => {

// expect(spyOnUpdatePeersList).toBeCalledTimes(3)
// })
// })
// })
32 changes: 17 additions & 15 deletions packages/backend/src/nest/storage/certificatesRequestsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,23 @@ export class CertificatesRequestsStore {
.collect()
.map(e => e.payload.value)
await Promise.all(
allCsrs.filter(async csr => {
// const parsedCsr = await loadCSR(csr)
const validation = await CertificatesRequestsStore.validateUserCsr(csr)
if (validation) return true
return false
}).map(async csr => {
const parsedCsr = await loadCSR(csr)
const pubKey = keyFromCertificate(parsedCsr)

if (filteredCsrsMap.has(pubKey)) {
filteredCsrsMap.delete(pubKey)
}
filteredCsrsMap.set(pubKey, csr)
})
allCsrs
.filter(async csr => {
// const parsedCsr = await loadCSR(csr)
const validation = await CertificatesRequestsStore.validateUserCsr(csr)
if (validation) return true
return false
})
.map(async csr => {
const parsedCsr = await loadCSR(csr)
const pubKey = keyFromCertificate(parsedCsr)

if (filteredCsrsMap.has(pubKey)) {
filteredCsrsMap.delete(pubKey)
}
filteredCsrsMap.set(pubKey, csr)
})
)
return [...filteredCsrsMap.values()]
}
}
}
9 changes: 6 additions & 3 deletions packages/backend/src/nest/storage/storage.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,11 @@ describe('StorageService', () => {

storageService.certificatesStore.store.events.emit('replicated')

expect(eventSpy).toBeCalledWith(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CERTIFICATES_REPLICATED)

expect(eventSpy).toBeCalledWith(
SocketActionTypes.CONNECTION_PROCESS_INFO,
ConnectionProcessInfo.CERTIFICATES_REPLICATED
)

await waitForExpect(() => {
expect(eventSpy).toBeCalledWith(StorageEvents.REPLICATED_CERTIFICATES, { certificates: [] })
expect(spyOnUpdatePeersList).toBeCalled()
Expand Down Expand Up @@ -681,4 +684,4 @@ describe('StorageService', () => {
expect(spyOnUpdatePeersList).toBeCalledTimes(3)
})
})
})
})
6 changes: 3 additions & 3 deletions packages/backend/src/nest/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,14 @@ export class StorageService extends EventEmitter {
}

public async attachCertificatesStoreListeners() {
this.on(StorageEvents.LOADED_CERTIFICATES, async (payload) => {
this.on(StorageEvents.LOADED_CERTIFICATES, async payload => {
this.emit(StorageEvents.REPLICATED_CERTIFICATES, payload)
await this.updatePeersList()
})
}

public async attachCsrsStoreListeners() {
this.on(StorageEvents.LOADED_USER_CSRS, async (payload) => {
this.on(StorageEvents.LOADED_USER_CSRS, async payload => {
const allCertificates = this.getAllEventLogEntries(this.certificatesStore.store)
this.emit(StorageEvents.REPLICATED_CSR, { csrs: payload.csrs, certificates: allCertificates, id: payload.id })
// TODO
Expand Down Expand Up @@ -834,4 +834,4 @@ export class StorageService extends EventEmitter {
this.certificatesRequestsStore.resetCsrReplicatedMapAndId()
}
}
}
}

0 comments on commit 877a038

Please sign in to comment.