Skip to content

Commit

Permalink
feat: lock concept
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacper-RF committed Nov 2, 2023
1 parent 0622c93 commit 920ce24
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
this.registrationService.on(RegistrationEvents.NEW_USER, async payload => {
await this.storageService?.saveCertificate(payload)
})

this.registrationService.on(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, payload => {
this.storageService.emit(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, { id: payload.id })
})
}
private attachsocketServiceListeners() {
// Community
Expand Down Expand Up @@ -561,7 +565,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
})
this.storageService.on(
StorageEvents.REPLICATED_CSR,
async (payload: { csrs: string[]; certificates: string[] }) => {
async (payload: { csrs: string[]; certificates: string[]; id: string }) => {
console.log(`On ${StorageEvents.REPLICATED_CSR}`)
this.serverIoProvider.io.emit(SocketActionTypes.RESPONSE_GET_CSRS, { csrs: payload.csrs })
this.registrationService.emit(RegistrationEvents.REGISTER_USER_CERTIFICATE, payload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ export class RegistrationService extends EventEmitter implements OnModuleInit {
onModuleInit() {
this.on(
RegistrationEvents.REGISTER_USER_CERTIFICATE,
async (payload: { csrs: string[]; certificates: string[] }) => {
async (payload: { csrs: string[]; certificates: string[]; id: string }) => {
// Lack of permsData means that we are not the owner of the community in the official model of the app, however anyone can modify the source code, put malicious permsData here, issue false certificates and try to trick other users.
await this.issueCertificates(payload)
}
)
}

private async issueCertificates(payload: { csrs: string[]; certificates: string[] }) {
private async issueCertificates(payload: { csrs: string[]; certificates: string[]; id: string }) {
if (!this._permsData) return
const pendingCsrs = await extractPendingCsrs(payload)
pendingCsrs.forEach(async csr => {
await this.registerUserCertificate(csr)
})

this.emit(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, { id: payload.id })
}

public set permsData(perms: PermsData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export enum RegistrationEvents {
ERROR = 'error',
NEW_USER = 'newUser',
REGISTER_USER_CERTIFICATE = 'registerUserCertificate',
FINISHED_ISSUING_CERTIFICATES_FOR_ID = 'FINISHED_ISSUING_CERTIFICATES_FOR_ID',
}
1 change: 1 addition & 0 deletions packages/backend/src/nest/storage/storage.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { LocalDbModule } from '../local-db/local-db.module'
import { LocalDbService } from '../local-db/local-db.service'
import { IPFS_REPO_PATCH, ORBIT_DB_DIR, QUIET_DIR } from '../const'
import { LocalDBKeys } from '../local-db/local-db.types'
import { RegistrationEvents } from '../registration/registration.types'

const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
Expand Down
24 changes: 23 additions & 1 deletion packages/backend/src/nest/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import Logger from '../common/logger'
import { DirectMessagesRepo, PublicChannelsRepo } from '../common/types'
import { removeFiles, removeDirs, createPaths, getUsersAddresses } from '../common/utils'
import { StorageEvents } from './storage.types'
import { RegistrationEvents } from '../registration/registration.types'

interface DBOptions {
replicate: boolean
Expand All @@ -71,6 +72,7 @@ export class StorageService extends EventEmitter {
private filesManager: IpfsFileManagerService
private peerId: PeerId | null = null
private ipfsStarted: boolean
private replicatedPromises: Promise<string | void>[] = []

private readonly logger = Logger(StorageService.name)
constructor(
Expand Down Expand Up @@ -398,13 +400,33 @@ export class StorageService extends EventEmitter {
write: ['*'],
},
})

this.certificatesRequests.events.on('replicated', async () => {
this.logger('REPLICATED: CSRs')

const id = Math.random().toString(36).slice(2, 7)

const promiseForId = new Promise<string>(resolve => {
const listener = async (payload: { id: string }) => {
if (id === payload.id) {
this.removeListener(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, listener)
resolve(id)
}
}

this.on(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, listener)
})

this.replicatedPromises.push(promiseForId)

const filteredCsrs = await this.getCsrs()

const allCertificates = this.getAllEventLogEntries(this.certificates)
this.emit(StorageEvents.REPLICATED_CSR, { csrs: filteredCsrs, certificates: allCertificates })

await Promise.all(this.replicatedPromises)

this.emit(StorageEvents.REPLICATED_CSR, { csrs: filteredCsrs, certificates: allCertificates, id })

await this.updatePeersList()
})
this.certificatesRequests.events.on('write', async (_address, entry) => {
Expand Down

0 comments on commit 920ce24

Please sign in to comment.