Skip to content

Commit

Permalink
inject localdbservice into sigchainservice
Browse files Browse the repository at this point in the history
  • Loading branch information
adrastaea committed Nov 26, 2024
1 parent 6dd9e22 commit b63039d
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 21 deletions.
11 changes: 1 addition & 10 deletions packages/backend/src/nest/auth/services/crypto/crypto.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@ import * as bs58 from 'bs58'
import { EncryptedAndSignedPayload, EncryptedPayload, EncryptionScope, EncryptionScopeType } from './types'
import { ChainServiceBase } from '../chainServiceBase'
import { SigChain } from '../../sigchain'
import {
asymmetric,
Base58,
Keyset,
KeysetWithSecrets,
LocalUserContext,
Member,
SignedEnvelope,
} from '@localfirst/auth'
import { asymmetric, Base58, Keyset, LocalUserContext, Member, SignedEnvelope } from '@localfirst/auth'
import { DEFAULT_SEARCH_OPTIONS, MemberSearchOptions } from '../members/types'
import { ChannelService } from '../roles/channel.service'
import { createLogger } from '../../../common/logger'

const logger = createLogger('auth:cryptoService')
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/nest/auth/sigchain.service.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Module } from '@nestjs/common'
import { SigChainService } from './sigchain.service'
import { LocalDbModule } from '../local-db/local-db.module'

@Module({
providers: [SigChainService],
exports: [SigChainService],
imports: [LocalDbModule],
})
export class SigChainModule {}
24 changes: 22 additions & 2 deletions packages/backend/src/nest/auth/sigchain.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ describe('SigChainManager', () => {
sigChainManager = await module.resolve(SigChainService)
localDbService = await module.resolve(LocalDbService)
})

beforeEach(async () => {
if (localDbService.getStatus() === 'closed') {
await localDbService.open()
}
})

afterAll(async () => {
await localDbService.close()
await module.close()
})

it('should throw an error when trying to get an active chain without setting one', () => {
expect(() => sigChainManager.getActiveChain()).toThrowError()
})
Expand Down Expand Up @@ -56,8 +68,8 @@ describe('SigChainManager', () => {
expect(retrievedChain).toBeDefined()
expect(retrievedChain?.context.user.userName).toBe('user')
sigChainManager.deleteChain('test')
const loadedSigChain = sigChainManager.loadChain(
serializedChain,
const loadedSigChain = sigChainManager.rehydrateSigChain(
retrievedChain!.serializedTeam,
retrievedChain!.context,
retrievedChain!.teamKeyRing,
false
Expand All @@ -66,4 +78,12 @@ describe('SigChainManager', () => {
expect(loadedSigChain.context.user.userName).toBe('user')
expect(loadedSigChain.team.teamName).toBe('test')
})
it('should save and load sigchain using nestjs service', async () => {
const sigChain = sigChainManager.createChain('test3', 'user', true)
sigChainManager.saveChain(sigChain.team.teamName)
sigChainManager.deleteChain(sigChain.team.teamName)
const loadedSigChain = await sigChainManager.loadChain('test3', false)
expect(loadedSigChain).toBeDefined()
expect(sigChainManager.getActiveChain()).toBe(loadedSigChain)
})
})
25 changes: 24 additions & 1 deletion packages/backend/src/nest/auth/sigchain.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Injectable, Logger, OnModuleInit } from '@nestjs/common'
import { SigChain } from './sigchain'
import { Keyring, LocalUserContext } from '3rd-party/auth/packages/auth/dist'
import { LocalDbService } from '../local-db/local-db.service'

@Injectable()
export class SigChainService implements OnModuleInit {
private readonly logger = new Logger(SigChainService.name)
private chains: Map<string, SigChain> = new Map()
private activeChainTeamName: string | undefined
private static _instance: SigChainService | undefined
private readonly localDbService: LocalDbService

onModuleInit() {
if (SigChainService._instance) {
Expand Down Expand Up @@ -62,12 +64,33 @@ export class SigChainService implements OnModuleInit {
return sigChain
}

loadChain(serializedTeam: Uint8Array, context: LocalUserContext, teamKeyRing: Keyring, setActive: boolean): SigChain {
rehydrateSigChain(
serializedTeam: Uint8Array,
context: LocalUserContext,
teamKeyRing: Keyring,
setActive: boolean
): SigChain {
const sigChain = SigChain.load(serializedTeam, context, teamKeyRing)
this.addChain(sigChain, setActive)
return sigChain
}

async loadChain(teamName: string, setActive: boolean): Promise<SigChain> {
const chain = await this.localDbService.getSigChain(teamName)
if (this.localDbService.getStatus() !== 'open') {
throw new Error('LocalDB not open!')
}
return this.rehydrateSigChain(chain!.serializedTeam, chain!.context, chain!.teamKeyRing, setActive)
}

saveChain(teamName: string): void {
if (this.localDbService.getStatus() !== 'open') {
throw new Error('LocalDB not open!')
}
const chain = this.getChainByTeamName(teamName)
this.localDbService.setSigChain(chain)
}

getChainByTeamName(teamName: string): SigChain {
if (!this.chains.has(teamName)) {
throw new Error(`No chain found for team ${teamName}!`)
Expand Down
8 changes: 7 additions & 1 deletion packages/backend/src/nest/auth/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Keyring, LocalUserContext } from '@localfirst/auth'

export type SigChainBlob = {
export type SigChainSaveData = {
serializedTeam: string
context: LocalUserContext
teamKeyRing: Keyring
}

export type SerializedSigChain = {
serializedTeam: Uint8Array
context: LocalUserContext
teamKeyRing: Keyring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
const sigChainBlob = await this.localDbService.getSigChain(community.name)
if (sigChainBlob) {
try {
this.sigChainService.loadChain(
this.sigChainService.rehydrateSigChain(
sigChainBlob.serializedTeam,
sigChainBlob.context,
sigChainBlob.teamKeyRing,
Expand Down
20 changes: 14 additions & 6 deletions packages/backend/src/nest/local-db/local-db.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Buffer } from 'buffer'
import { Inject, Injectable } from '@nestjs/common'
import { Level } from 'level'
import { type Community, type NetworkInfo, NetworkStats, Identity, IdentityUpdatePayload } from '@quiet/types'
import { createLibp2pAddress, filterAndSortPeers } from '@quiet/common'
import { LEVEL_DB } from '../const'
import { LocalDBKeys, LocalDbStatus } from './local-db.types'
import { createLogger } from '../common/logger'
import { SigChainBlob } from '../auth/types'
import { SerializedSigChain, SigChainSaveData } from '../auth/types'
import { SigChain } from '../auth/sigchain'

@Injectable()
Expand Down Expand Up @@ -170,18 +171,25 @@ export class LocalDbService {
if (!sigChains) {
sigChains = {}
}
const sigChainBlob: SigChainBlob = {
serializedTeam: sigChain.save(),
const serializedSigChain: SigChainSaveData = {
serializedTeam: Buffer.from(sigChain.save()).toString('base64'),
context: sigChain.context,
teamKeyRing: sigChain.team.teamKeyring(),
}
sigChains[teamName] = sigChainBlob
sigChains[teamName] = serializedSigChain
await this.put(LocalDBKeys.SIGCHAINS, sigChains)
}

public async getSigChain(teamName: string): Promise<SigChainBlob | undefined> {
public async getSigChain(teamName: string): Promise<SerializedSigChain | undefined> {
const sigChains = await this.get(LocalDBKeys.SIGCHAINS)
this.logger.info('Getting sigchain', teamName)
return sigChains?.[teamName]
const sigChainBlob = sigChains?.[teamName]
if (sigChainBlob) {
return {
serializedTeam: Buffer.from(sigChainBlob.serializedTeam, 'base64'),
context: sigChainBlob.context,
teamKeyRing: sigChainBlob.teamKeyRing,
}
}
}
}

0 comments on commit b63039d

Please sign in to comment.