Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/Duplicate/Unregistered users mixed up - change certificatesRequests DS from EventStore to KeyValueStore #1988

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions packages/backend/src/nest/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
verifyUserCert,
parseCertificationRequest,
getReqFieldValue,
loadCSR,
} from '@quiet/identity'
import type { IPFS } from 'ipfs-core'
import OrbitDB from 'orbit-db'
Expand Down Expand Up @@ -59,7 +60,7 @@ interface DBOptions {
export class StorageService extends EventEmitter {
public channels: KeyValueStore<PublicChannel>
private certificates: EventStore<string>
private certificatesRequests: EventStore<string>
private certificatesRequests: KeyValueStore<string>
public publicChannelsRepos: Map<string, PublicChannelsRepo> = new Map()
public directMessagesRepos: Map<string, DirectMessagesRepo> = new Map()
private publicKeysMap: Map<string, CryptoKey> = new Map()
Expand Down Expand Up @@ -319,7 +320,7 @@ export class StorageService extends EventEmitter {
}

public async updatePeersList() {
const allUsers = this.getAllUsers()
const allUsers = await this.getAllUsers()
const registeredUsers = this.getAllRegisteredUsers()
const peers = [...new Set(await getUsersAddresses(allUsers.concat(registeredUsers)))]
console.log('updatePeersList, peers count:', peers.length)
Expand Down Expand Up @@ -391,21 +392,23 @@ export class StorageService extends EventEmitter {

public async createDbForCertificatesRequests() {
this.logger('certificatesRequests db init')
this.certificatesRequests = await this.orbitDb.log<string>('csrs', {
this.certificatesRequests = await this.orbitDb.kvstore<string>('csrs', {
replicate: false,
accessController: {
write: ['*'],
},
})
this.certificatesRequests.events.on('replicated', async () => {
this.logger('REPLICATED: CSRs')
const allCsrs = this.getAllEventLogEntries(this.certificatesRequests)
await this.certificatesRequests.load()
const allCsrs = Object.values(this.certificatesRequests.all)
const allCertificates = this.getAllEventLogEntries(this.certificates)
this.emit(StorageEvents.REPLICATED_CSR, { csrs: allCsrs, certificates: allCertificates })
await this.updatePeersList()
})
this.certificatesRequests.events.on('write', async (_address, entry) => {
const csr: string = entry.payload.value

this.logger('Saved CSR locally')
const allCertificates = this.getAllEventLogEntries(this.certificates)
this.emit(StorageEvents.REPLICATED_CSR, { csrs: [csr], certificates: allCertificates })
Expand All @@ -414,8 +417,8 @@ export class StorageService extends EventEmitter {

// @ts-expect-error - OrbitDB's type declaration of `load` lacks 'options'
await this.certificatesRequests.load({ fetchEntryTimeout: 15000 })
const allcsrs = this.getAllEventLogEntries(this.certificatesRequests)
this.logger('ALL Certificates COUNT:', allcsrs.length)
const allCsrs = Object.values(this.certificatesRequests)
this.logger('ALL Certificates COUNT:', allCsrs.length)
this.logger('STORAGE: Finished creating certificatesRequests db')
}

Expand Down Expand Up @@ -827,6 +830,7 @@ export class StorageService extends EventEmitter {

public async saveCertificate(payload: SaveCertificatePayload): Promise<boolean> {
this.logger('About to save certificate...')
this.logger('payload.certificate', payload.certificate)
if (!payload.certificate) {
this.logger('Certificate is either null or undefined, not saving to db')
return false
Expand All @@ -851,13 +855,14 @@ export class StorageService extends EventEmitter {
}

await this.certificatesRequests.load()

const csrs = this.getAllEventLogEntries(this.certificatesRequests)
const csrs = Object.values(this.certificatesRequests.all)

if (csrs.includes(payload.csr)) return false

this.logger('Saving csr...')
await this.certificatesRequests.add(payload.csr)
const parsedCsr = await loadCSR(payload.csr)
const pubKey = keyFromCertificate(parsedCsr)
await this.certificatesRequests.put(pubKey, payload.csr)
return true
}

Expand All @@ -876,8 +881,9 @@ export class StorageService extends EventEmitter {
return allUsers
}

public getAllUsers(): UserData[] {
const csrs = this.getAllEventLogEntries(this.certificatesRequests)
public async getAllUsers(): Promise<UserData[]> {
await this.certificatesRequests.load()
const csrs = Object.values(this.certificatesRequests.all)
console.log('csrs count:', csrs.length)
const allUsers: UserData[] = []
for (const csr of csrs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ describe('Search Modal', () => {

const factory = await getFactory(store)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

const alice = await factory.create<ReturnType<typeof identity.actions.addNewIdentity>['payload']>('Identity', {
id: community.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ describe('Channels panel', () => {

const factory = await getFactory(store)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)
const generalChannel = publicChannels.selectors.generalChannel(store.getState())
expect(generalChannel).not.toBeUndefined()
const alice = await factory.create<ReturnType<typeof identity.actions.addNewIdentity>['payload']>('Identity', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ describe('IdentityPanel', () => {

const factory = await getFactory(store)

const community: Community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community: Community = await factory.create<
ReturnType<typeof communities.actions.addNewCommunity>['payload']
>('Community')

const result = renderComponent(
<IdentityPanel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface TextWithLinkProps {
tag: string
label: string
action: () => void
},
}
]
}

Expand Down
11 changes: 6 additions & 5 deletions packages/desktop/src/renderer/storybook/decorators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { ThemeProvider, StyledEngineProvider } from '@mui/material/styles'
import theme from '../theme'
import { Store } from '../sagas/store.types'

export const withStore = (store: Store) => (Story: React.FC) => (
<Provider store={store}>
<Story />
</Provider>
)
export const withStore = (store: Store) => (Story: React.FC) =>
(
<Provider store={store}>
<Story />
</Provider>
)

export const withTheme = (Story: React.FC) => (
<StyledEngineProvider injectFirst>
Expand Down
5 changes: 3 additions & 2 deletions packages/desktop/src/rtl-tests/app.restart.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ describe('Restart app works correctly', () => {

const factory = await getFactory(store)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

await factory.create<ReturnType<typeof identityActions.addNewIdentity>['payload']>('Identity', {
id: community.id,
Expand Down
5 changes: 3 additions & 2 deletions packages/desktop/src/rtl-tests/channel.add.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ describe('Add new channel', () => {
)
const factory = await getFactory(store)

const channel =
await factory.create<ReturnType<typeof publicChannels.actions.addChannel>['payload']>('PublicChannel')
const channel = await factory.create<ReturnType<typeof publicChannels.actions.addChannel>['payload']>(
'PublicChannel'
)

renderComponent(<CreateChannel />, store)

Expand Down
65 changes: 39 additions & 26 deletions packages/desktop/src/rtl-tests/channel.main.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ describe('Channel', () => {

const factory = await getFactory(store)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

const entities = store.getState().PublicChannels.channels.entities

Expand Down Expand Up @@ -263,8 +264,9 @@ describe('Channel', () => {

const factory = await getFactory(store)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

const entities = store.getState().PublicChannels.channels.entities

Expand Down Expand Up @@ -327,8 +329,9 @@ describe('Channel', () => {

const factory = await getFactory(store)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)
await factory.create<ReturnType<typeof identity.actions.addNewIdentity>['payload']>('Identity', {
id: community.id,
nickname: 'john',
Expand Down Expand Up @@ -358,8 +361,9 @@ describe('Channel', () => {

const factory = await getFactory(store)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

const entities = store.getState().PublicChannels.channels.entities

Expand Down Expand Up @@ -407,8 +411,9 @@ describe('Channel', () => {

const factory = await getFactory(store)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

const alice = await factory.create<ReturnType<typeof identity.actions.addNewIdentity>['payload']>('Identity', {
id: community.id,
Expand Down Expand Up @@ -482,8 +487,9 @@ describe('Channel', () => {

const factory = await getFactory(store)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

const entities = store.getState().PublicChannels.channels.entities

Expand Down Expand Up @@ -571,8 +577,9 @@ describe('Channel', () => {

const factory = await getFactory(store)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

const alice = await factory.create<ReturnType<typeof identity.actions.addNewIdentity>['payload']>('Identity', {
id: community.id,
Expand Down Expand Up @@ -700,8 +707,9 @@ describe('Channel', () => {

const factory = await getFactory(store)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

const alice = await factory.create<ReturnType<typeof identity.actions.addNewIdentity>['payload']>('Identity', {
id: community.id,
Expand Down Expand Up @@ -747,8 +755,9 @@ describe('Channel', () => {

const factory = await getFactory(initialState)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

const alice = await factory.create<ReturnType<typeof identity.actions.addNewIdentity>['payload']>('Identity', {
id: community.id,
Expand Down Expand Up @@ -1019,8 +1028,9 @@ describe('Channel', () => {

const factory = await getFactory(initialState)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

await factory.create<ReturnType<typeof identity.actions.addNewIdentity>['payload']>('Identity', {
id: community.id,
Expand Down Expand Up @@ -1115,8 +1125,9 @@ describe('Channel', () => {

const factory = await getFactory(initialState)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

const alice = await factory.create<ReturnType<typeof identity.actions.addNewIdentity>['payload']>('Identity', {
id: community.id,
Expand Down Expand Up @@ -1231,8 +1242,9 @@ describe('Channel', () => {

const factory = await getFactory(initialState)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

const alice = await factory.create<ReturnType<typeof identity.actions.addNewIdentity>['payload']>('Identity', {
id: community.id,
Expand Down Expand Up @@ -1349,8 +1361,9 @@ describe('Channel', () => {

const factory = await getFactory(initialState)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

const alice = await factory.create<ReturnType<typeof identity.actions.addNewIdentity>['payload']>('Identity', {
id: community.id,
Expand Down
5 changes: 3 additions & 2 deletions packages/desktop/src/rtl-tests/channel.menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ describe('Channel menu', () => {

const factory = await getFactory(store)

const community =
await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>('Community')
const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
'Community'
)

await factory.create<ReturnType<typeof identity.actions.addNewIdentity>['payload']>('Identity', {
id: community.id,
Expand Down
Loading