diff --git a/CHANGELOG.md b/CHANGELOG.md index 1116814bc0..5b0bd4db0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ * Self-assign the member role when joining with QSS [#3058](https://github.com/TryQuiet/quiet/issues/3058) * Use LFA-based identity in OrbitDB * Requests iOS notification permissions when app launches [#3079](https://github.com/TryQuiet/quiet/issues/3079) +* Store LFA keys in IOS keychain for notifications [#3091](https://github.com/TryQuiet/quiet/issues/3091) +* Store user metadata in IOS native storage for notifications [#3091](https://github.com/TryQuiet/quiet/issues/3091) ### Fixes diff --git a/packages/backend/src/nest/auth/services/crypto/crypto.service.ts b/packages/backend/src/nest/auth/services/crypto/crypto.service.ts index 63af9863da..495aeb61ef 100644 --- a/packages/backend/src/nest/auth/services/crypto/crypto.service.ts +++ b/packages/backend/src/nest/auth/services/crypto/crypto.service.ts @@ -14,9 +14,10 @@ import { import { ChainServiceBase } from '../chainServiceBase' import { SigChain } from '../../sigchain' import { asymmetric, Keyset, Member, SignedEnvelope, EncryptStreamTeamPayload } from '@localfirst/auth' +import { KeyMap } from '@localfirst/auth/team/selectors' import { DEFAULT_SEARCH_OPTIONS, MemberSearchOptions } from '../members/types' import { createLogger } from '../../../common/logger' -import { KeyMetadata } from '3rd-party/auth/packages/crdx/dist' +import { KeyMetadata } from '@localfirst/crdx' const logger = createLogger('auth:cryptoService') @@ -36,6 +37,22 @@ class CryptoService extends ChainServiceBase { }) } + public getPublicKeysForAllMembers(includeSelf: boolean = false): Keyset[] { + const members = this.sigChain.users.getAllUsers() + const keysByMember = [] + for (const member of members) { + if (member.userId === this.sigChain.context.user.userId && !includeSelf) { + continue + } + keysByMember.push(member.keys) + } + return keysByMember + } + + public getAllKeys(): KeyMap { + return this.sigChain.team!.allKeys() + } + public sign(message: any): SignedEnvelope { return this.sigChain.team!.sign(message) } diff --git a/packages/backend/src/nest/auth/sigchain.service.ts b/packages/backend/src/nest/auth/sigchain.service.ts index dd417ad981..cdaacd35ee 100644 --- a/packages/backend/src/nest/auth/sigchain.service.ts +++ b/packages/backend/src/nest/auth/sigchain.service.ts @@ -1,21 +1,30 @@ -import { Inject, Injectable, OnModuleInit } from '@nestjs/common' +import { Inject, Injectable } from '@nestjs/common' import { SigChain } from './sigchain' -import { Connection, InviteeMemberContext, Keyring, LocalUserContext, MemberContext, Team } from '@localfirst/auth' +import { + Connection, + Hash, + InviteeMemberContext, + Keyring, + LocalUserContext, + MemberContext, + Team, + UserWithSecrets, + DeviceWithSecrets, +} from '@localfirst/auth' +import { KeyMetadata } from '@localfirst/crdx' import { LocalDbService } from '../local-db/local-db.service' import { createLogger } from '../common/logger' -import { SocketService } from '../socket/socket.service' -import { SocketEvents, User } from '@quiet/types' +import { SocketEvents, StorableKey, User } from '@quiet/types' import { type RoleService } from './services/roles/role.service' import { type DeviceService } from './services/members/device.service' import { type InviteService } from './services/invites/invite.service' import { type UserService } from './services/members/user.service' import { type CryptoService } from './services/crypto/crypto.service' -import { type UserWithSecrets } from '@localfirst/auth' -import { type DeviceWithSecrets } from '@localfirst/auth' import { SERVER_IO_PROVIDER } from '../const' import { ServerIoProviderTypes } from '../types' import EventEmitter from 'events' -import { GetChainFilter } from './types' +import { GetChainFilter, StoredKeyType } from './types' +import { KeysUpdatedEvent } from '@quiet/types' @Injectable() export class SigChainService extends EventEmitter { @@ -26,8 +35,7 @@ export class SigChainService extends EventEmitter { constructor( @Inject(SERVER_IO_PROVIDER) public readonly serverIoProvider: ServerIoProviderTypes, - private readonly localDbService: LocalDbService, - private readonly socketService: SocketService + private readonly localDbService: LocalDbService ) { super() } @@ -132,8 +140,19 @@ export class SigChainService extends EventEmitter { this.attachSocketListeners(this.getChain({ teamName })) } - private handleChainUpdate = () => { - const users = this.getActiveChain() + private handleChainUpdate = (teamName: string) => { + this._updateUsersOnChainUpdate(teamName) + this._updateKeysOnChainUpdate(teamName) + this.emit('updated', teamName) + this.saveChain(teamName) + this.logger.info('Chain updated, emitted updated event') + } + + /** + * Send updated list of users to the state manager on chain update + */ + private _updateUsersOnChainUpdate(teamName: string) { + const users = this.getChain({ teamName }) .team?.members() .map(user => ({ userId: user.userId, @@ -141,20 +160,101 @@ export class SigChainService extends EventEmitter { isRegistered: true, isDuplicated: false, })) as User[] - this.socketService.emit(SocketEvents.USERS_UPDATED, { users }) - this.emit('updated') - this.saveChain(this.activeChainTeamName!) - this.logger.info('Chain updated, emitted updated event') + this.serverIoProvider.io.emit(SocketEvents.USERS_UPDATED, { users }) + } + + /** + * Update the IOS keychain with any new keys on chain update + */ + private async _updateKeysOnChainUpdate(teamName: string): Promise { + if ((process.platform as string) !== 'ios') { + this.logger.trace('Skipping key update because we are not on ios, current platform =', process.platform) + return + } + + const generateKeyName = (teamId: string, keyType: string, scope: KeyMetadata): string => { + return `quiet_${teamId}_${scope.type}_${scope.name}_${scope.generation}_${keyType}` + } + + const sigchain = this.getChain({ teamName }) + if (sigchain == null) { + this.logger.error('No chain for name found', teamName) + return + } + + const teamId = sigchain.team!.id + const alreadySentKeys: Set = new Set(await this.localDbService.getKeysStoredInKeychain(teamId)) + const keysToSend: StorableKey[] = [] + const keyNamesSent: string[] = [] + // get all secret keys that this user has that haven't been added to the keychain + const allKeys = sigchain.crypto.getAllKeys() + for (const keyData of Object.values(allKeys)) { + for (const keyTypeData of Object.values(keyData)) { + for (const keyTypeGenData of Object.values(keyTypeData)) { + const keyName = generateKeyName(teamId, StoredKeyType.SECRET, { + name: keyTypeGenData.name, + type: keyTypeGenData.type, + generation: keyTypeGenData.generation, + }) + if (!alreadySentKeys.has(keyName)) { + keysToSend.push({ key: keyTypeGenData.secretKey, keyName }) + keyNamesSent.push(keyName) + } + } + } + } + // TODO: update to pull all generations of user public/sig keys + // get all user public keys that haven't been added to the keychain + const allUserPublicKeys = sigchain.crypto.getPublicKeysForAllMembers(true) + for (const keySet of allUserPublicKeys) { + const publicKeyName = generateKeyName(teamId, StoredKeyType.USER_PUBLIC, { + name: keySet.name, + type: keySet.type, + generation: keySet.generation, + }) + if (!alreadySentKeys.has(publicKeyName)) { + keysToSend.push({ key: keySet.encryption, keyName: publicKeyName }) + keyNamesSent.push(publicKeyName) + } + + const sigKeyName = generateKeyName(teamId, StoredKeyType.USER_SIG, { + name: keySet.name, + type: keySet.type, + generation: keySet.generation, + }) + if (!alreadySentKeys.has(sigKeyName)) { + keysToSend.push({ key: keySet.signature, keyName: sigKeyName }) + keyNamesSent.push(sigKeyName) + } + } + + if (keysToSend.length === 0) { + this.logger.trace('Skipping IOS keychain update, no new keys') + return + } + + // send new keys to the state manager to add to the keychain and update list of key names in + const keyUpdateEvent: KeysUpdatedEvent = { + keys: keysToSend, + } + await this.localDbService.updateKeysStoredInKeychain(teamId, keyNamesSent) + this.serverIoProvider.io.emit(SocketEvents.KEYS_UPDATED, keyUpdateEvent) } private attachSocketListeners(chain: SigChain): void { this.logger.info('Attaching socket listeners') - chain.on('updated', this.handleChainUpdate) + const _onTeamUpdate = (): void => { + this.handleChainUpdate(chain.team!.teamName) + } + chain.on('updated', _onTeamUpdate) } private detachSocketListeners(chain: SigChain): void { this.logger.info('Detaching socket listeners') - chain.removeListener('updated', this.handleChainUpdate) + const _onTeamUpdate = (): void => { + this.handleChainUpdate(chain.team!.teamName) + } + chain.removeListener('updated', _onTeamUpdate) } /** @@ -208,6 +308,7 @@ export class SigChainService extends EventEmitter { const sigChain = SigChain.create(teamName, username) this.addChain(sigChain, setActive, teamName) await this.saveChain(teamName) + this.handleChainUpdate(teamName) return sigChain } diff --git a/packages/backend/src/nest/auth/types.ts b/packages/backend/src/nest/auth/types.ts index 438c6b0d46..42d7eda60a 100644 --- a/packages/backend/src/nest/auth/types.ts +++ b/packages/backend/src/nest/auth/types.ts @@ -17,3 +17,9 @@ export type GetChainFilter = { teamId?: string teamName?: string } + +export enum StoredKeyType { + SECRET = 'secret', + USER_PUBLIC = 'userPublic', + USER_SIG = 'userSig', +} diff --git a/packages/backend/src/nest/connections-manager/connections-manager.service.ts b/packages/backend/src/nest/connections-manager/connections-manager.service.ts index ff03f1a5ec..45aa25c4fc 100644 --- a/packages/backend/src/nest/connections-manager/connections-manager.service.ts +++ b/packages/backend/src/nest/connections-manager/connections-manager.service.ts @@ -50,6 +50,7 @@ import { SetUserProfilePayload, InvitationData, SetUserProfileResponse, + UserProfilesUpdatedPayload, } from '@quiet/types' import { CONFIG_OPTIONS, QSS_ALLOWED, QSS_ENDPOINT, SERVER_IO_PROVIDER, SOCKS_PROXY_AGENT } from '../const' import { Libp2pService, Libp2pState } from '../libp2p/libp2p.service' @@ -809,6 +810,11 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI } ) + this.socketService.on(SocketActions.USER_PROFILES_UPDATED, (payload: UserProfilesUpdatedPayload) => { + this.logger.info(`Forwarding ${SocketActions.USER_PROFILES_UPDATED} back to state manager`) + this.serverIoProvider.io.emit(SocketEvents.USER_PROFILES_UPDATED, payload) + }) + this.socketService.on(SocketActions.TOGGLE_P2P, async (payload: boolean, callback: (response: boolean) => void) => { try { if (payload) { diff --git a/packages/backend/src/nest/local-db/local-db.service.ts b/packages/backend/src/nest/local-db/local-db.service.ts index b1cd84f38d..d1209bcb12 100644 --- a/packages/backend/src/nest/local-db/local-db.service.ts +++ b/packages/backend/src/nest/local-db/local-db.service.ts @@ -605,4 +605,29 @@ export class LocalDbService extends EventEmitter { } return count } + + /** + * Update list of kys for a given team ID that were stored in the IOS keychain + * + * @param teamId LFA team ID + * @param keyNames Names of keys that were added to IOS keychain + */ + public async updateKeysStoredInKeychain(teamId: string, keyNames: string[]): Promise { + const key = `${LocalDBKeys.KEYS_STORED_KEYCHAIN}:${teamId}` + const arr: string[] = (await this.get(key)) || [] + arr.push(...keyNames) + await this.put(key, arr) + } + + /** + * Get the list of key names for a given team ID that have been stored in the IOS keychain + * + * @param teamId LFA team ID + * @returns List of key names + */ + public async getKeysStoredInKeychain(teamId: string): Promise { + const key = `${LocalDBKeys.KEYS_STORED_KEYCHAIN}:${teamId}` + const arr: string[] = (await this.get(key)) || [] + return arr + } } diff --git a/packages/backend/src/nest/local-db/local-db.types.ts b/packages/backend/src/nest/local-db/local-db.types.ts index e0ccf2236b..1ddf242ae5 100644 --- a/packages/backend/src/nest/local-db/local-db.types.ts +++ b/packages/backend/src/nest/local-db/local-db.types.ts @@ -38,6 +38,9 @@ export enum LocalDBKeys { // exists in the Community object. OWNER_ORBIT_DB_IDENTITY = 'ownerOrbitDbIdentity', + // Keys from sigchain that have been stored in keychain + KEYS_STORED_KEYCHAIN = 'keysStoredInKeychain', + SIGCHAINS = 'sigchains:', USER_CONTEXTS = 'userContexts', KEYRINGS = 'keyrings', diff --git a/packages/backend/src/nest/qss/qss.service.spec.ts b/packages/backend/src/nest/qss/qss.service.spec.ts index 7b9e2e451e..31e5dff651 100644 --- a/packages/backend/src/nest/qss/qss.service.spec.ts +++ b/packages/backend/src/nest/qss/qss.service.spec.ts @@ -1035,7 +1035,7 @@ describe('QSSService', () => { const ingestSpy = jest.spyOn(orbitDbService, 'ingestEntries').mockResolvedValue() // Trigger sigchain update which should process DLQ - sigchainService.emit('updated') + sigchainService.emit('updated', sigchainService.activeChainTeamName) // Wait for async processing await waitForExpect(async () => { @@ -1071,10 +1071,10 @@ describe('QSSService', () => { const processSpy = jest.spyOn(qssService, 'processDLQDecrypt') // Trigger first update - sigchainService.emit('updated') + sigchainService.emit('updated', sigchainService.activeChainTeamName) // Immediately trigger second update while first is processing - sigchainService.emit('updated') + sigchainService.emit('updated', sigchainService.activeChainTeamName) await waitForExpect(async () => { const remainingCount = await localDbService.getDLQDecryptCount(teamId) @@ -1097,7 +1097,7 @@ describe('QSSService', () => { const ingestSpy = jest.spyOn(orbitDbService, 'ingestEntries') // Trigger sigchain update - sigchainService.emit('updated') + sigchainService.emit('updated', sigchainService.activeChainTeamName) // Give it time to process await new Promise(resolve => setTimeout(resolve, 100)) diff --git a/packages/backend/src/nest/qss/qss.service.ts b/packages/backend/src/nest/qss/qss.service.ts index 9a06a8b120..0a67374320 100644 --- a/packages/backend/src/nest/qss/qss.service.ts +++ b/packages/backend/src/nest/qss/qss.service.ts @@ -105,7 +105,7 @@ export class QSSService extends EventEmitter implements OnModuleDestroy, OnModul this._deadLetterQueueProcessor = setInterval(this.processDeadLetterQueue, 30_000) this.connect = this.connect.bind(this) this._configureEventHandlers() - this.sigChainService.on('updated', () => void this.processDLQDecrypt()) + this.sigChainService.on('updated', (teamName: string) => void this.processDLQDecrypt(teamName)) } public onModuleDestroy() { @@ -907,14 +907,14 @@ export class QSSService extends EventEmitter implements OnModuleDestroy, OnModul /** * Process the decryption dead letter queue when sigchain updates (new keys arrive) */ - private async processDLQDecrypt(): Promise { + private async processDLQDecrypt(teamName: string): Promise { if (this._dlqDecryptInFlight) { this.logger.debug('DLQ decrypt already in progress, requesting retry') this._dlqDecryptRetryRequested = true return } - const activeChain = this.sigChainService.getActiveChain(false) + const activeChain = this.sigChainService.getChain({ teamName }) if (!activeChain?.team) { return } @@ -981,7 +981,7 @@ export class QSSService extends EventEmitter implements OnModuleDestroy, OnModul // If a sigchain update occurred while processing, retry with new keys if (this._dlqDecryptRetryRequested) { this.logger.debug('Retrying DLQ decrypt after sigchain update during processing') - await this.processDLQDecrypt() + await this.processDLQDecrypt(teamName) } } diff --git a/packages/backend/src/nest/socket/socket.service.ts b/packages/backend/src/nest/socket/socket.service.ts index 18506f70d0..aefd7aeaa8 100644 --- a/packages/backend/src/nest/socket/socket.service.ts +++ b/packages/backend/src/nest/socket/socket.service.ts @@ -24,6 +24,7 @@ import { SetUserProfilePayload, type HCaptchaFormResponse, InviteResultWithSalt, + UserProfilesUpdatedPayload, } from '@quiet/types' import EventEmitter from 'events' import { CONFIG_OPTIONS, SERVER_IO_PROVIDER } from '../const' @@ -199,6 +200,11 @@ export class SocketService extends EventEmitter implements OnModuleInit { } ) + socket.on(SocketActions.USER_PROFILES_UPDATED, (payload: UserProfilesUpdatedPayload) => { + this.logger.info(`Emitting ${SocketActions.USER_PROFILES_UPDATED}`) + this.emit(SocketActions.USER_PROFILES_UPDATED, payload) + }) + // ====== Local First Auth ====== socket.on( diff --git a/packages/mobile/ios/CommunicationBridge.m b/packages/mobile/ios/CommunicationBridge.m index 254cfa1975..9212ba937e 100644 --- a/packages/mobile/ios/CommunicationBridge.m +++ b/packages/mobile/ios/CommunicationBridge.m @@ -5,4 +5,6 @@ @interface RCT_EXTERN_MODULE(CommunicationModule, RCTEventEmitter) RCT_EXTERN_METHOD(handleIncomingEvents:(NSString *)event payload:(NSString *)payload extra:(NSString *)extra) RCT_EXTERN_METHOD(requestNotificationPermission) RCT_EXTERN_METHOD(checkNotificationPermission) +RCT_EXTERN_METHOD(saveKeysInKeychain:(NSArray *)newKeys) +RCT_EXTERN_METHOD(saveUserMetadata:(NSArray *)updatedMetadata) @end diff --git a/packages/mobile/ios/CommunicationModule.swift b/packages/mobile/ios/CommunicationModule.swift index f6961cdbfe..b4eae33805 100644 --- a/packages/mobile/ios/CommunicationModule.swift +++ b/packages/mobile/ios/CommunicationModule.swift @@ -1,4 +1,5 @@ import UserNotifications +import OSLog @objc(CommunicationModule) class CommunicationModule: RCTEventEmitter { @@ -12,6 +13,10 @@ class CommunicationModule: RCTEventEmitter { static let DEVICE_TOKEN_RECEIVED = "deviceTokenReceived" static let WEBSOCKET_CONNECTION_CHANNEL = "_WEBSOCKET_CONNECTION_" + private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "CommunicationModule") + + let keychainHandler = KeychainHandler() + let userMetadataHandler = UserMetadataHandler() @objc func sendDataPort(port: UInt16, socketIOSecret: String) { @@ -55,6 +60,46 @@ class CommunicationModule: RCTEventEmitter { } } } + + @objc + func saveKeysInKeychain(_ newKeys: NSArray) { + let decoder = JSONDecoder() + for keyAsAny in newKeys { + do { + let keyAsString: String = keyAsAny as! String + let data = Data(keyAsString.utf8) + let decodedNamedKey = try decoder.decode(NamedKey.self, from: data) + try self.keychainHandler.addLfaKey(namedKey: decodedNamedKey) + let stored = try self.keychainHandler.getLfaKeyString(keyName: decodedNamedKey.keyName) + CommunicationModule.logger.info("Stored key matches? \(stored == decodedNamedKey.key) \(decodedNamedKey.keyName)") + } catch { + // TODO: send a message to the backend with any keys that weren't stored + CommunicationModule.logger.error("Error while saving key in keychain: \(error)") + } + } + } + + @objc + func saveUserMetadata(_ updatedMetadata: NSArray) { + let decoder = JSONDecoder() + var userMetadata: [UserMetadataStruct] = [] + for metadataAsAny in updatedMetadata { + do { + let metadataAsString: String = metadataAsAny as! String + let data = Data(metadataAsString.utf8) + let decodedMetadata = try decoder.decode(UserMetadataStruct.self, from: data) + userMetadata.append(decodedMetadata) + } catch { + CommunicationModule.logger.error("Error while decoding user metadata: \(error)") + } + } + + do { + try self.userMetadataHandler.saveUserMetadata(updatedMetadata: userMetadata) + } catch { + CommunicationModule.logger.error("Error while saving user metadata: \(error)") + } + } @objc func checkNotificationPermission() { diff --git a/packages/mobile/ios/KeychainHandler.swift b/packages/mobile/ios/KeychainHandler.swift new file mode 100644 index 0000000000..355365f045 --- /dev/null +++ b/packages/mobile/ios/KeychainHandler.swift @@ -0,0 +1,136 @@ +// +// KeychainError.swift +// Quiet +// +// Created by Isla Koenigsknecht on 2/25/26. +// + +import Foundation +import CryptoKit +import Security +import CoreData +import OSLog + +public enum KeychainError: Error { + case noPassword + case unexpectedPasswordData + case unexpectedItemData + case unhandledError(status: OSStatus) +} + +public enum ConversionError: Error { + case stringToBytesError +} + +public enum KeychainHandlerError: Error { + case noKeyFound + case malformedKey + case unhandledError(reason: Any) +} + +public enum KeyAddStatus { + case success + case duplicateScope +} + +public struct NamedKey: Codable { + let keyName: String + let key: String +} + +// TODO: add string to key object conversion (e.g. string to SymmetricKey) +@objc(KeychainHandler) +class KeychainHandler: NSObject { + private let keychainGroupName: String = "com.quietmobile" + + private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "KeychainHandler") + + public func getLfaKeyString(keyName: String) throws -> String { + do { + let password: String = try _getKeyImpl(keyName: keyName) + return password + } catch KeychainError.noPassword { + throw KeychainHandlerError.noKeyFound + } catch KeychainError.unexpectedPasswordData { + throw KeychainHandlerError.malformedKey + } catch ConversionError.stringToBytesError { + throw KeychainHandlerError.malformedKey + } catch { + throw KeychainHandlerError.unhandledError(reason: error) + } + } + + public func addLfaKey(namedKey: NamedKey) throws -> KeyAddStatus { + var existingKey: String? + do { + existingKey = try getLfaKeyString(keyName: namedKey.keyName) + } catch KeychainHandlerError.noKeyFound { + existingKey = nil + } catch KeychainHandlerError.malformedKey { + existingKey = nil + } catch { + KeychainHandler.logger.error("Error while getting existing LFA key for name \(namedKey.keyName): \(error)") + throw error + } + + guard existingKey == nil else { + guard existingKey == namedKey.key else { return KeyAddStatus.duplicateScope } + return KeyAddStatus.success + } + + do { + let keyData: Data = try _stringToBytes(str: namedKey.key) + let addStatus: KeyAddStatus = try _addKeyToKeychainImpl(keyName: namedKey.keyName, keyData: keyData) + return addStatus + } catch { + throw KeychainHandlerError.unhandledError(reason: error) + } + } + + private func _getKeyImpl(keyName: String) throws -> String { + var existingKey: CFTypeRef? + let query: [String: Any] = [ + kSecClass as String: kSecClassGenericPassword, + kSecAttrService as String: keychainGroupName, + kSecAttrAccount as String: keyName, + kSecMatchLimit as String: kSecMatchLimitOne, + kSecReturnAttributes as String: true, + kSecReturnData as String: true + ] + let status: OSStatus = SecItemCopyMatching(query as CFDictionary, &existingKey) + guard status != errSecItemNotFound else { throw KeychainError.noPassword } + guard status == errSecSuccess else { throw KeychainError.unhandledError(status: status) } + guard let existingItem: [String : Any] = existingKey as? [String : Any], + let passwordData = existingItem[kSecValueData as String] as? Data, + let password = String(data: passwordData, encoding: String.Encoding.utf8) + else { + throw KeychainError.unexpectedPasswordData + } + return password + } + + private func _addKeyToKeychainImpl(keyName: String, keyData: Data) throws -> KeyAddStatus { + let query: [String: Any] = [ + kSecClass as String: kSecClassGenericPassword, + kSecAttrAccount as String: keyName, + kSecAttrService as String: keychainGroupName, + kSecValueData as String: keyData, + kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock + ] + + let status: OSStatus = SecItemAdd(query as CFDictionary, nil) + if status == errSecSuccess { + return KeyAddStatus.success + } else if status == errSecDuplicateItem { + return KeyAddStatus.duplicateScope + } else { + throw KeychainError.unhandledError(status: status) + } + } + + private func _stringToBytes(str: String) throws -> Data { + let bytes: Data? = str.data(using: .utf8) + guard bytes != nil else { throw ConversionError.stringToBytesError } + return bytes! + } +} diff --git a/packages/mobile/ios/Quiet.xcodeproj/project.pbxproj b/packages/mobile/ios/Quiet.xcodeproj/project.pbxproj index 79efc9d819..9260b8638c 100644 --- a/packages/mobile/ios/Quiet.xcodeproj/project.pbxproj +++ b/packages/mobile/ios/Quiet.xcodeproj/project.pbxproj @@ -55,10 +55,12 @@ 18FD2A3E296F009E00A2B8C0 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 18FD2A37296F009E00A2B8C0 /* AppDelegate.m */; }; 18FD2A3F296F009E00A2B8C0 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 18FD2A38296F009E00A2B8C0 /* Images.xcassets */; }; 18FD2A40296F009E00A2B8C0 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 18FD2A39296F009E00A2B8C0 /* main.m */; }; - 38AD376324628C6E27E70991 /* libPods-Quiet-QuietTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 02AFA323BA7196A67E7A1133 /* libPods-Quiet-QuietTests.a */; }; + 663DC8C12F621139005D2086 /* UserMetadataHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663DC8C02F621134005D2086 /* UserMetadataHandler.swift */; }; + 665587CA2F4F5ECD005D2086 /* KeychainHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 665587C92F4F5ECD005D2086 /* KeychainHandler.swift */; }; + 85137B01156B661E82184B11 /* libPods-Quiet.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 287A03B12772D68AAF979A77 /* libPods-Quiet.a */; }; 955DC7582BD930B30014725B /* WebsocketSingleton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 955DC7572BD930B30014725B /* WebsocketSingleton.swift */; }; D3239FB5EFA85E780E1AD201 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 84F12DFE2A5B0E05C2C41286 /* PrivacyInfo.xcprivacy */; }; - E479975299F0932FF690F805 /* libPods-Quiet.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 104C0D0FA8EF00192C60CAD7 /* libPods-Quiet.a */; }; + FFD33AE2521CEA2E0D3134ED /* libPods-Quiet-QuietTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CA54D67D8C7A248C6A22C0F8 /* libPods-Quiet-QuietTests.a */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -92,7 +94,6 @@ 00E356EE1AD99517003FC87E /* QuietTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = QuietTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 00E356F11AD99517003FC87E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 00E356F21AD99517003FC87E /* QuietTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QuietTests.m; sourceTree = ""; }; - 02AFA323BA7196A67E7A1133 /* libPods-Quiet-QuietTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Quiet-QuietTests.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 03B673F92E6103DC00A86655 /* Rubik-Black.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; name = "Rubik-Black.ttf"; path = "../src/assets/fonts/Rubik-Black.ttf"; sourceTree = SOURCE_ROOT; }; 03B673FA2E6103DC00A86655 /* Rubik-BlackItalic.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; name = "Rubik-BlackItalic.ttf"; path = "../src/assets/fonts/Rubik-BlackItalic.ttf"; sourceTree = SOURCE_ROOT; }; 03B673FB2E6103DC00A86655 /* Rubik-Bold.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; name = "Rubik-Bold.ttf"; path = "../src/assets/fonts/Rubik-Bold.ttf"; sourceTree = SOURCE_ROOT; }; @@ -107,8 +108,6 @@ 03B674042E6103DC00A86655 /* Rubik-Regular.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; name = "Rubik-Regular.ttf"; path = "../src/assets/fonts/Rubik-Regular.ttf"; sourceTree = SOURCE_ROOT; }; 03B674052E6103DC00A86655 /* Rubik-SemiBold.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; name = "Rubik-SemiBold.ttf"; path = "../src/assets/fonts/Rubik-SemiBold.ttf"; sourceTree = SOURCE_ROOT; }; 03B674062E6103DC00A86655 /* Rubik-SemiBoldItalic.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; name = "Rubik-SemiBoldItalic.ttf"; path = "../src/assets/fonts/Rubik-SemiBoldItalic.ttf"; sourceTree = SOURCE_ROOT; }; - 104C0D0FA8EF00192C60CAD7 /* libPods-Quiet.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Quiet.a"; sourceTree = BUILT_PRODUCTS_DIR; }; - 116749B7D5552021F04BE739 /* Pods-Quiet.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Quiet.release.xcconfig"; path = "Target Support Files/Pods-Quiet/Pods-Quiet.release.xcconfig"; sourceTree = ""; }; 13B07F961A680F5B00A75B9A /* Quiet.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Quiet.app; sourceTree = BUILT_PRODUCTS_DIR; }; 180E120A2AEFB7F900804659 /* Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = ""; }; 1827A9E129783D6E00245FD3 /* classic-level.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = "classic-level.framework"; sourceTree = ""; }; @@ -620,11 +619,16 @@ 18FD2A39296F009E00A2B8C0 /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = Quiet/main.m; sourceTree = ""; }; 18FD2A3A296F009E00A2B8C0 /* Quiet.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; name = Quiet.entitlements; path = Quiet/Quiet.entitlements; sourceTree = ""; }; 18FD2A3B296F009E00A2B8C0 /* QuietDebug.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; name = QuietDebug.entitlements; path = Quiet/QuietDebug.entitlements; sourceTree = ""; }; - 2877B6088D1D81EF869D71D9 /* Pods-Quiet-QuietTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Quiet-QuietTests.release.xcconfig"; path = "Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests.release.xcconfig"; sourceTree = ""; }; - 7F75CE6D1C7D3DA9F5BA6A76 /* Pods-Quiet-QuietTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Quiet-QuietTests.debug.xcconfig"; path = "Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests.debug.xcconfig"; sourceTree = ""; }; + 287A03B12772D68AAF979A77 /* libPods-Quiet.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Quiet.a"; sourceTree = BUILT_PRODUCTS_DIR; }; + 2F63F73503B4A62481768267 /* Pods-Quiet.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Quiet.debug.xcconfig"; path = "Target Support Files/Pods-Quiet/Pods-Quiet.debug.xcconfig"; sourceTree = ""; }; + 663DC8C02F621134005D2086 /* UserMetadataHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserMetadataHandler.swift; sourceTree = ""; }; + 665587C92F4F5ECD005D2086 /* KeychainHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeychainHandler.swift; sourceTree = ""; }; + 677CCEF15F36760C73BC26A6 /* Pods-Quiet.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Quiet.release.xcconfig"; path = "Target Support Files/Pods-Quiet/Pods-Quiet.release.xcconfig"; sourceTree = ""; }; + 70BDAC54548DB3505251D3DB /* Pods-Quiet-QuietTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Quiet-QuietTests.debug.xcconfig"; path = "Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests.debug.xcconfig"; sourceTree = ""; }; 84F12DFE2A5B0E05C2C41286 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xml; name = PrivacyInfo.xcprivacy; path = Quiet/PrivacyInfo.xcprivacy; sourceTree = ""; }; + 88972EF0A58F6401DC66730D /* Pods-Quiet-QuietTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Quiet-QuietTests.release.xcconfig"; path = "Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests.release.xcconfig"; sourceTree = ""; }; 955DC7572BD930B30014725B /* WebsocketSingleton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebsocketSingleton.swift; sourceTree = ""; }; - E727FE0446E9E46CDECEC7FE /* Pods-Quiet.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Quiet.debug.xcconfig"; path = "Target Support Files/Pods-Quiet/Pods-Quiet.debug.xcconfig"; sourceTree = ""; }; + CA54D67D8C7A248C6A22C0F8 /* libPods-Quiet-QuietTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Quiet-QuietTests.a"; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -633,7 +637,7 @@ buildActionMask = 2147483647; files = ( 1827A9E229783D6E00245FD3 /* classic-level.framework in Frameworks */, - 38AD376324628C6E27E70991 /* libPods-Quiet-QuietTests.a in Frameworks */, + FFD33AE2521CEA2E0D3134ED /* libPods-Quiet-QuietTests.a in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -643,7 +647,7 @@ files = ( 00A416342EC2EAA900ACC877 /* NodeMobile.xcframework in Frameworks */, 1827A9E329783D7600245FD3 /* classic-level.framework in Frameworks */, - E479975299F0932FF690F805 /* libPods-Quiet.a in Frameworks */, + 85137B01156B661E82184B11 /* libPods-Quiet.a in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -670,6 +674,8 @@ 13B07FAE1A68108700A75B9A /* Quiet */ = { isa = PBXGroup; children = ( + 663DC8C02F621134005D2086 /* UserMetadataHandler.swift */, + 665587C92F4F5ECD005D2086 /* KeychainHandler.swift */, 180E120A2AEFB7F900804659 /* Utils.swift */, 18FD2A36296F009E00A2B8C0 /* AppDelegate.h */, 18FD2A37296F009E00A2B8C0 /* AppDelegate.m */, @@ -4701,10 +4707,10 @@ 1CEEDB4F07B9978C125775C5 /* Pods */ = { isa = PBXGroup; children = ( - E727FE0446E9E46CDECEC7FE /* Pods-Quiet.debug.xcconfig */, - 116749B7D5552021F04BE739 /* Pods-Quiet.release.xcconfig */, - 7F75CE6D1C7D3DA9F5BA6A76 /* Pods-Quiet-QuietTests.debug.xcconfig */, - 2877B6088D1D81EF869D71D9 /* Pods-Quiet-QuietTests.release.xcconfig */, + 2F63F73503B4A62481768267 /* Pods-Quiet.debug.xcconfig */, + 677CCEF15F36760C73BC26A6 /* Pods-Quiet.release.xcconfig */, + 70BDAC54548DB3505251D3DB /* Pods-Quiet-QuietTests.debug.xcconfig */, + 88972EF0A58F6401DC66730D /* Pods-Quiet-QuietTests.release.xcconfig */, ); path = Pods; sourceTree = ""; @@ -4714,8 +4720,8 @@ children = ( 00A416332EC2EAA900ACC877 /* NodeMobile.xcframework */, 1827A9E129783D6E00245FD3 /* classic-level.framework */, - 104C0D0FA8EF00192C60CAD7 /* libPods-Quiet.a */, - 02AFA323BA7196A67E7A1133 /* libPods-Quiet-QuietTests.a */, + 287A03B12772D68AAF979A77 /* libPods-Quiet.a */, + CA54D67D8C7A248C6A22C0F8 /* libPods-Quiet-QuietTests.a */, ); name = Frameworks; sourceTree = ""; @@ -4781,12 +4787,12 @@ isa = PBXNativeTarget; buildConfigurationList = 00E357021AD99517003FC87E /* Build configuration list for PBXNativeTarget "QuietTests" */; buildPhases = ( - 58352AE7BD90BC0845FC78DE /* [CP] Check Pods Manifest.lock */, + 95F7BA6D16EB3A1B27BAA786 /* [CP] Check Pods Manifest.lock */, 00E356EA1AD99517003FC87E /* Sources */, 00E356EB1AD99517003FC87E /* Frameworks */, 00E356EC1AD99517003FC87E /* Resources */, - EF4E9879B3A5060ADF995D6A /* [CP] Embed Pods Frameworks */, - 4E18C6AEAD8D524EAD836F51 /* [CP] Copy Pods Resources */, + 911E369A1C6D85907F7F0302 /* [CP] Embed Pods Frameworks */, + E7F2776DE3547BE1502BC947 /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -4802,7 +4808,7 @@ isa = PBXNativeTarget; buildConfigurationList = 13B07F931A680F5B00A75B9A /* Build configuration list for PBXNativeTarget "Quiet" */; buildPhases = ( - 715B735C8E7FA602A967EAF0 /* [CP] Check Pods Manifest.lock */, + BB9294C6673D9CCB22CBA0B8 /* [CP] Check Pods Manifest.lock */, FD10A7F022414F080027D42C /* Start Packager */, 13B07F871A680F5B00A75B9A /* Sources */, 13B07F8C1A680F5B00A75B9A /* Frameworks */, @@ -4814,8 +4820,8 @@ 18FD2A32296D736300A2B8C0 /* [CUSTOM NODEJS MOBILE] Remove Python3 Binaries */, 1827A9E0297837FE00245FD3 /* [CUSTOM NODEJS MOBILE] Remove prebuilds */, 1868C095292F8FE2001D6D5E /* Embed Frameworks */, - 7253A52F091FC8122D730B2E /* [CP] Embed Pods Frameworks */, - A1AE2657AE5C40D18941D671 /* [CP] Copy Pods Resources */, + 85BBD2CD345055BC7227A72B /* [CP] Embed Pods Frameworks */, + 00462B5D3156F56526040199 /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -4912,6 +4918,23 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */ + 00462B5D3156F56526040199 /* [CP] Copy Pods Resources */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-Quiet/Pods-Quiet-resources-${CONFIGURATION}-input-files.xcfilelist", + ); + name = "[CP] Copy Pods Resources"; + outputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-Quiet/Pods-Quiet-resources-${CONFIGURATION}-output-files.xcfilelist", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Quiet/Pods-Quiet-resources.sh\"\n"; + showEnvVarsInLog = 0; + }; 00DD1BFF1BD5951E006B06BC /* Bundle React Native code and images */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; @@ -4924,7 +4947,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "set -e\n\n# Fix for machines using nvm\nif [[ -s \"$HOME/.nvm/nvm.sh\" ]]; then\n. \"$HOME/.nvm/nvm.sh\"\nelif [[ -x \"$(command -v brew)\" && -s \"$(brew --prefix nvm)/nvm.sh\" ]]; then\n. \"$(brew --prefix nvm)/nvm.sh\"\nfi\n\nexport NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh\n"; + shellScript = "set -e\n\nexport NODE_BINARY=/Users/isla/.volta/bin/node\n../node_modules/react-native/scripts/react-native-xcode.sh\n"; }; 03B673F82E60FE0000A86655 /* Inject Feature Flags */ = { isa = PBXShellScriptBuildPhase; @@ -5020,46 +5043,41 @@ shellPath = /bin/sh; shellScript = "find \"$CODESIGNING_FOLDER_PATH/nodejs-project/node_modules/\" -name \"python3\" | xargs rm\n"; }; - 4E18C6AEAD8D524EAD836F51 /* [CP] Copy Pods Resources */ = { + 85BBD2CD345055BC7227A72B /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputFileListPaths = ( - "${PODS_ROOT}/Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests-resources-${CONFIGURATION}-input-files.xcfilelist", + "${PODS_ROOT}/Target Support Files/Pods-Quiet/Pods-Quiet-frameworks-${CONFIGURATION}-input-files.xcfilelist", ); - name = "[CP] Copy Pods Resources"; + name = "[CP] Embed Pods Frameworks"; outputFileListPaths = ( - "${PODS_ROOT}/Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests-resources-${CONFIGURATION}-output-files.xcfilelist", + "${PODS_ROOT}/Target Support Files/Pods-Quiet/Pods-Quiet-frameworks-${CONFIGURATION}-output-files.xcfilelist", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests-resources.sh\"\n"; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Quiet/Pods-Quiet-frameworks.sh\"\n"; showEnvVarsInLog = 0; }; - 58352AE7BD90BC0845FC78DE /* [CP] Check Pods Manifest.lock */ = { + 911E369A1C6D85907F7F0302 /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests-frameworks-${CONFIGURATION}-input-files.xcfilelist", ); - inputPaths = ( - "${PODS_PODFILE_DIR_PATH}/Podfile.lock", - "${PODS_ROOT}/Manifest.lock", - ); - name = "[CP] Check Pods Manifest.lock"; + name = "[CP] Embed Pods Frameworks"; outputFileListPaths = ( - ); - outputPaths = ( - "$(DERIVED_FILE_DIR)/Pods-Quiet-QuietTests-checkManifestLockResult.txt", + "${PODS_ROOT}/Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests-frameworks-${CONFIGURATION}-output-files.xcfilelist", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests-frameworks.sh\"\n"; showEnvVarsInLog = 0; }; - 715B735C8E7FA602A967EAF0 /* [CP] Check Pods Manifest.lock */ = { + 95F7BA6D16EB3A1B27BAA786 /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( @@ -5074,62 +5092,50 @@ outputFileListPaths = ( ); outputPaths = ( - "$(DERIVED_FILE_DIR)/Pods-Quiet-checkManifestLockResult.txt", + "$(DERIVED_FILE_DIR)/Pods-Quiet-QuietTests-checkManifestLockResult.txt", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; showEnvVarsInLog = 0; }; - 7253A52F091FC8122D730B2E /* [CP] Embed Pods Frameworks */ = { + BB9294C6673D9CCB22CBA0B8 /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputFileListPaths = ( - "${PODS_ROOT}/Target Support Files/Pods-Quiet/Pods-Quiet-frameworks-${CONFIGURATION}-input-files.xcfilelist", - ); - name = "[CP] Embed Pods Frameworks"; - outputFileListPaths = ( - "${PODS_ROOT}/Target Support Files/Pods-Quiet/Pods-Quiet-frameworks-${CONFIGURATION}-output-files.xcfilelist", ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Quiet/Pods-Quiet-frameworks.sh\"\n"; - showEnvVarsInLog = 0; - }; - A1AE2657AE5C40D18941D671 /* [CP] Copy Pods Resources */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputFileListPaths = ( - "${PODS_ROOT}/Target Support Files/Pods-Quiet/Pods-Quiet-resources-${CONFIGURATION}-input-files.xcfilelist", + inputPaths = ( + "${PODS_PODFILE_DIR_PATH}/Podfile.lock", + "${PODS_ROOT}/Manifest.lock", ); - name = "[CP] Copy Pods Resources"; + name = "[CP] Check Pods Manifest.lock"; outputFileListPaths = ( - "${PODS_ROOT}/Target Support Files/Pods-Quiet/Pods-Quiet-resources-${CONFIGURATION}-output-files.xcfilelist", + ); + outputPaths = ( + "$(DERIVED_FILE_DIR)/Pods-Quiet-checkManifestLockResult.txt", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Quiet/Pods-Quiet-resources.sh\"\n"; + shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; showEnvVarsInLog = 0; }; - EF4E9879B3A5060ADF995D6A /* [CP] Embed Pods Frameworks */ = { + E7F2776DE3547BE1502BC947 /* [CP] Copy Pods Resources */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( ); inputFileListPaths = ( - "${PODS_ROOT}/Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests-frameworks-${CONFIGURATION}-input-files.xcfilelist", + "${PODS_ROOT}/Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests-resources-${CONFIGURATION}-input-files.xcfilelist", ); - name = "[CP] Embed Pods Frameworks"; + name = "[CP] Copy Pods Resources"; outputFileListPaths = ( - "${PODS_ROOT}/Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests-frameworks-${CONFIGURATION}-output-files.xcfilelist", + "${PODS_ROOT}/Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests-resources-${CONFIGURATION}-output-files.xcfilelist", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests-frameworks.sh\"\n"; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Quiet-QuietTests/Pods-Quiet-QuietTests-resources.sh\"\n"; showEnvVarsInLog = 0; }; FD10A7F022414F080027D42C /* Start Packager */ = { @@ -5171,9 +5177,11 @@ 955DC7582BD930B30014725B /* WebsocketSingleton.swift in Sources */, 1889CA4E26E763E1004ECFBD /* Extensions.swift in Sources */, 1898143A2934CF70001F39E7 /* TorHandler.swift in Sources */, + 663DC8C12F621139005D2086 /* UserMetadataHandler.swift in Sources */, 1868BCEE292E9212001D6D5E /* RNNodeJsMobile.m in Sources */, 1868C4382930D7D6001D6D5E /* DataDirectory.swift in Sources */, 1868C43E2930EAEA001D6D5E /* CommunicationBridge.m in Sources */, + 665587CA2F4F5ECD005D2086 /* KeychainHandler.swift in Sources */, 1868C43A2930D859001D6D5E /* FindFreePort.swift in Sources */, 18FD2A3E296F009E00A2B8C0 /* AppDelegate.m in Sources */, 1868BCED292E9212001D6D5E /* NodeRunner.mm in Sources */, @@ -5195,7 +5203,7 @@ /* Begin XCBuildConfiguration section */ 00E356F61AD99517003FC87E /* Debug */ = { isa = XCBuildConfiguration; - baseConfigurationReference = 7F75CE6D1C7D3DA9F5BA6A76 /* Pods-Quiet-QuietTests.debug.xcconfig */; + baseConfigurationReference = 70BDAC54548DB3505251D3DB /* Pods-Quiet-QuietTests.debug.xcconfig */; buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; BUNDLE_LOADER = "$(TEST_HOST)"; @@ -5228,7 +5236,7 @@ }; 00E356F71AD99517003FC87E /* Release */ = { isa = XCBuildConfiguration; - baseConfigurationReference = 2877B6088D1D81EF869D71D9 /* Pods-Quiet-QuietTests.release.xcconfig */; + baseConfigurationReference = 88972EF0A58F6401DC66730D /* Pods-Quiet-QuietTests.release.xcconfig */; buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; BUNDLE_LOADER = "$(TEST_HOST)"; @@ -5258,7 +5266,7 @@ }; 13B07F941A680F5B00A75B9A /* Debug */ = { isa = XCBuildConfiguration; - baseConfigurationReference = E727FE0446E9E46CDECEC7FE /* Pods-Quiet.debug.xcconfig */; + baseConfigurationReference = 2F63F73503B4A62481768267 /* Pods-Quiet.debug.xcconfig */; buildSettings = { ARCHS = "$(ARCHS_STANDARD)"; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; @@ -5359,7 +5367,7 @@ }; 13B07F951A680F5B00A75B9A /* Release */ = { isa = XCBuildConfiguration; - baseConfigurationReference = 116749B7D5552021F04BE739 /* Pods-Quiet.release.xcconfig */; + baseConfigurationReference = 677CCEF15F36760C73BC26A6 /* Pods-Quiet.release.xcconfig */; buildSettings = { ARCHS = "$(ARCHS_STANDARD)"; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; diff --git a/packages/mobile/ios/Quiet/Quiet.entitlements b/packages/mobile/ios/Quiet/Quiet.entitlements index 903def2af5..599333ab05 100644 --- a/packages/mobile/ios/Quiet/Quiet.entitlements +++ b/packages/mobile/ios/Quiet/Quiet.entitlements @@ -4,5 +4,9 @@ aps-environment development + keychain-access-groups + + $(AppIdentifierPrefix)com.quietmobile + diff --git a/packages/mobile/ios/Quiet/QuietDebug.entitlements b/packages/mobile/ios/Quiet/QuietDebug.entitlements index 903def2af5..599333ab05 100644 --- a/packages/mobile/ios/Quiet/QuietDebug.entitlements +++ b/packages/mobile/ios/Quiet/QuietDebug.entitlements @@ -4,5 +4,9 @@ aps-environment development + keychain-access-groups + + $(AppIdentifierPrefix)com.quietmobile + diff --git a/packages/mobile/ios/UserMetadataHandler.swift b/packages/mobile/ios/UserMetadataHandler.swift new file mode 100644 index 0000000000..0f8141858d --- /dev/null +++ b/packages/mobile/ios/UserMetadataHandler.swift @@ -0,0 +1,195 @@ +// +// UserMetadataHandler.swift +// Quiet +// +// Created by Isla Koenigsknecht on 2/25/26. +// + +import Foundation +import CoreData +import OSLog +import SwiftData + +public enum UserMetadataError: Error { + case missingModelContext + case noModelFound(id: String) + case incorrectModelCount(expected: Int, actual: Int) + case unhandledError(reason: Error) +} + +public struct ProfilePhotoStruct: Codable { + let ext: String + let path: String? + let size: Int + let width: Int + let height: Int +} + +public struct UserMetadataStruct: Codable { + let userId: String + let nickname: String + let profilePhoto: ProfilePhotoStruct? +} + +@Model +class ProfilePhoto: Identifiable { + var id: String + var ext: String + var path: String? + var size: Int + var width: Int + var height: Int + var userMetadata: UserMetadata? + var createdAt: Date? + + init(id: String, ext: String, path: String?, size: Int, width: Int, height: Int, createdAt: Date?) { + self.id = id + self.ext = ext + self.path = path + self.size = size + self.width = width + self.height = height + self.createdAt = createdAt + } + + public static func fromStruct(id: String, profilePhoto: ProfilePhotoStruct, createdAt: Date?) -> ProfilePhoto { + return ProfilePhoto(id: id, ext: profilePhoto.ext, path: profilePhoto.path, size: profilePhoto.size, width: profilePhoto.width, height: profilePhoto.height, createdAt: createdAt) + } + + public func toStruct() -> ProfilePhotoStruct { + return ProfilePhotoStruct(ext: self.ext, path: self.path, size: self.size, width: self.width, height: self.height) + } +} + +@Model +class UserMetadata: Identifiable { + var id: String + var nickname: String + var createdAt: Date? = nil + + @Relationship(deleteRule: .cascade, inverse: \ProfilePhoto.userMetadata) + var profilePhoto: ProfilePhoto? + + init(id: String, nickname: String, profilePhoto: ProfilePhotoStruct?, createdAt: Date?) { + var profilePhotoModel: ProfilePhoto? = nil + if let unwrappedProfilePhoto = profilePhoto { + profilePhotoModel = ProfilePhoto.fromStruct(id: id, profilePhoto: unwrappedProfilePhoto, createdAt: createdAt) + } + + self.id = id + self.nickname = nickname + self.profilePhoto = profilePhotoModel + self.createdAt = createdAt + } + + public static func fromStruct(userMetadata: UserMetadataStruct, createdAt: Date?) -> UserMetadata { + return UserMetadata(id: userMetadata.userId, nickname: userMetadata.nickname, profilePhoto: userMetadata.profilePhoto, createdAt: createdAt) + } + + public func toStruct() -> UserMetadataStruct { + return UserMetadataStruct( + userId: self.id, + nickname: self.nickname, + profilePhoto: self.profilePhoto?.toStruct() + ) + } +} + +@objc(UserMetadataHandler) +class UserMetadataHandler: NSObject { + private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "UserMetadataHandler") + private var container: ModelContainer? + private var modelContext: ModelContext? + + public func initContainer() throws -> Void { + if (self.container != nil) { + UserMetadataHandler.logger.debug("Container already initialized, skipping...") + return + } + + do { + self.container = try ModelContainer(for: UserMetadata.self, configurations: .init(isStoredInMemoryOnly: false)) + self.modelContext = ModelContext(self.container!) + } catch { + UserMetadataHandler.logger.error("Error while initializing UserMetadata ModelContainer: \(error)") + throw error + } + } + + public func saveUserMetadata(updatedMetadata: [UserMetadataStruct]) throws -> Void { + do { + try self.initContainer() + } catch { + throw error + } + + guard let context = self.modelContext else { + throw UserMetadataError.missingModelContext + } + + UserMetadataHandler.logger.info("Inserting user metadata") + for metadata in updatedMetadata { + UserMetadataHandler.logger.debug("Inserting data for \(metadata.userId)") + let found = try self.fetchUserMetadataById(userId: metadata.userId) + if let unwrappedFound = found { + UserMetadataHandler.logger.debug("Replacing existing metadata for \(metadata.userId)") + try self.deleteUserMetadata(model: unwrappedFound) + } + let model = UserMetadata.fromStruct(userMetadata: metadata, createdAt: Date.now) + context.insert(model) + } + + UserMetadataHandler.logger.info("Persisting user metadata") + do { + try context.save() + } catch { + UserMetadataHandler.logger.error("Error while persisting UserMetadata model(s) to disk: \(error)") + throw KeychainHandlerError.unhandledError(reason: error) + } + } + + public func fetchUserMetadataById(userId: String) throws -> UserMetadata? { + UserMetadataHandler.logger.info("Fetching UserMetadata by ID: \(userId)") + + guard let context = self.modelContext else { + throw UserMetadataError.missingModelContext + } + + do { + let descriptor = FetchDescriptor( + predicate: #Predicate { $0.id == userId } + ) + let models = try context.fetch(descriptor) + guard models.count > 0 else { + return nil + } + guard models.count == 1 else { + UserMetadataHandler.logger.warning("Found \(models.count) stored metadata records for \(userId)") + for model in models { + UserMetadataHandler.logger.warning("Found \(userId) created at \(model.createdAt?.ISO8601Format() ?? "NO CREATEDAT")") + } + throw UserMetadataError.incorrectModelCount(expected: 1, actual: models.count) + } + return models[0] + } catch { + UserMetadataHandler.logger.error("Error while fetching UserMetadata for ID \(userId): \(error)") + throw UserMetadataError.unhandledError(reason: error) + } + } + + public func deleteUserMetadata(model: UserMetadata) throws -> Void { + UserMetadataHandler.logger.info("Deleting UserMetadata record for \(model.id)") + + guard let context = self.modelContext else { + throw UserMetadataError.missingModelContext + } + + do { + context.delete(model) + try context.save() + } catch { + UserMetadataHandler.logger.error("Error while deleting UserMetadata for \(model.id): \(error)") + throw UserMetadataError.unhandledError(reason: error) + } + } +} diff --git a/packages/mobile/src/store/init/startConnection/startConnection.saga.ts b/packages/mobile/src/store/init/startConnection/startConnection.saga.ts index 10c9b3370f..4c12d91f62 100644 --- a/packages/mobile/src/store/init/startConnection/startConnection.saga.ts +++ b/packages/mobile/src/store/init/startConnection/startConnection.saga.ts @@ -16,9 +16,11 @@ import { PayloadAction } from '@reduxjs/toolkit' import { socket as stateManager, Socket } from '@quiet/state-manager' import { initActions, WebsocketConnectionPayload } from '../init.slice' import { eventChannel } from 'redux-saga' -import { SocketActions } from '@quiet/types' +import { KeysUpdatedEvent, SocketActions, SocketEvents, UserProfilesUpdatedPayload } from '@quiet/types' import { createLogger } from '../../../utils/logger' import { initSelectors } from '../init.selectors' +import { keysActions } from '../../keys/keys.slice' +import { usersMetadataActions } from '../../userMetadata/usersMetadata.slice' const logger = createLogger('startConnection') @@ -76,7 +78,10 @@ function subscribeSocketLifecycle(socket: Socket, socketIOData: WebsocketConnect let socket_id: string | undefined return eventChannel< - ReturnType | ReturnType + | ReturnType + | ReturnType + | ReturnType + | ReturnType >(emit => { socket.on('connect', async () => { socket_id = socket.id @@ -87,6 +92,14 @@ function subscribeSocketLifecycle(socket: Socket, socketIOData: WebsocketConnect logger.warn('client: Closing socket connection', socket_id, reason) emit(initActions.suspendWebsocketConnection()) }) + socket.on(SocketEvents.KEYS_UPDATED, async (payload: KeysUpdatedEvent) => { + logger.info('Keys updated, writing to keychain') + emit(keysActions.saveKeysInKeychain(payload)) + }) + socket.on(SocketEvents.USER_PROFILES_UPDATED, async (payload: UserProfilesUpdatedPayload) => { + logger.info('User profiles updated, saving in ios native storage') + emit(usersMetadataActions.saveUserMetadataNatively(payload)) + }) return () => {} }) } diff --git a/packages/mobile/src/store/keys/keys.master.saga.ts b/packages/mobile/src/store/keys/keys.master.saga.ts new file mode 100644 index 0000000000..a35dff4a2a --- /dev/null +++ b/packages/mobile/src/store/keys/keys.master.saga.ts @@ -0,0 +1,20 @@ +import { takeEvery, cancelled } from 'redux-saga/effects' +import { all } from 'typed-redux-saga' +import { type Socket } from '@quiet/state-manager/src/types' +import { keysActions } from './keys.slice' +import { saveKeysInKeychainSaga } from './saveKeysInKeychain/saveKeysInKeychain.saga' +import { createLogger } from '../../utils/logger' + +const logger = createLogger('keysMasterSaga') + +export function* keysMasterSaga(): Generator { + logger.info('keysMasterSaga starting') + try { + yield all([takeEvery(keysActions.saveKeysInKeychain.type, saveKeysInKeychainSaga)]) + } finally { + logger.info('keysMasterSaga stopping') + if (yield cancelled()) { + logger.info('keysMasterSaga cancelled') + } + } +} diff --git a/packages/mobile/src/store/keys/keys.selectors..ts b/packages/mobile/src/store/keys/keys.selectors..ts new file mode 100644 index 0000000000..647e429649 --- /dev/null +++ b/packages/mobile/src/store/keys/keys.selectors..ts @@ -0,0 +1,6 @@ +import { StoreKeys } from '../store.keys' +import { CreatedSelectors, StoreState } from '../store.types' + +const keysSlice: CreatedSelectors[StoreKeys.Keys] = (state: StoreState) => state[StoreKeys.Keys] + +export const keysSelectors = {} diff --git a/packages/mobile/src/store/keys/keys.slice.ts b/packages/mobile/src/store/keys/keys.slice.ts new file mode 100644 index 0000000000..884b147bcf --- /dev/null +++ b/packages/mobile/src/store/keys/keys.slice.ts @@ -0,0 +1,19 @@ +import { createSlice, type PayloadAction } from '@reduxjs/toolkit' +import { StoreKeys } from '../store.keys' +import { KeysUpdatedEvent } from '@quiet/types' +import { createLogger } from '../../utils/logger' + +const logger = createLogger('keysSlice') + +export class KeysState {} + +export const keysSlice = createSlice({ + initialState: { ...new KeysState() }, + name: StoreKeys.Keys, + reducers: { + saveKeysInKeychain: (state, _action: PayloadAction) => state, + }, +}) + +export const keysActions = keysSlice.actions +export const keysReducer = keysSlice.reducer diff --git a/packages/mobile/src/store/keys/keys.transform.ts b/packages/mobile/src/store/keys/keys.transform.ts new file mode 100644 index 0000000000..797b644871 --- /dev/null +++ b/packages/mobile/src/store/keys/keys.transform.ts @@ -0,0 +1,14 @@ +import { createTransform } from 'redux-persist' +import { StoreKeys } from '../store.keys' +import { KeysState } from './keys.slice' + +export const KeysTransform = createTransform( + (inboundState: KeysState, _key: any) => { + return inboundState + }, + (outboundState: KeysState, _key: any) => { + // TODO: determine if we still need this transform + return outboundState + }, + { whitelist: [StoreKeys.Keys] } +) diff --git a/packages/mobile/src/store/keys/keys.type.ts b/packages/mobile/src/store/keys/keys.type.ts new file mode 100644 index 0000000000..5148b443a9 --- /dev/null +++ b/packages/mobile/src/store/keys/keys.type.ts @@ -0,0 +1,12 @@ +export type ExtendedKeyScope = { + type: string + name: string + generation: number + keyType: string +} + +export interface StorableKey { + scope: ExtendedKeyScope + key: string + teamId: string +} diff --git a/packages/mobile/src/store/keys/saveKeysInKeychain/saveKeysInKeychain.saga.ts b/packages/mobile/src/store/keys/saveKeysInKeychain/saveKeysInKeychain.saga.ts new file mode 100644 index 0000000000..45c6465713 --- /dev/null +++ b/packages/mobile/src/store/keys/saveKeysInKeychain/saveKeysInKeychain.saga.ts @@ -0,0 +1,20 @@ +import { type PayloadAction } from '@reduxjs/toolkit' +import { call } from 'typed-redux-saga' +import { NativeModules } from 'react-native' + +import { KeysUpdatedEvent } from '@quiet/types' +import { createLogger } from '../../../utils/logger' + +const logger = createLogger('saveKeysInKeychainSaga') + +export function* saveKeysInKeychainSaga(action: PayloadAction): Generator { + logger.info('Storing keys in ios keychain', action.payload.keys) + try { + yield* call( + NativeModules.CommunicationModule.saveKeysInKeychain, + action.payload.keys.map(key => JSON.stringify(key)) + ) + } catch (e) { + logger.error('Error while updating keys on keychain', e) + } +} diff --git a/packages/mobile/src/store/root.reducer.ts b/packages/mobile/src/store/root.reducer.ts index f5570fc579..bfc6037d4d 100644 --- a/packages/mobile/src/store/root.reducer.ts +++ b/packages/mobile/src/store/root.reducer.ts @@ -5,6 +5,8 @@ import { initReducer } from './init/init.slice' import { navigationReducer } from './navigation/navigation.slice' import { nativeServicesReducer, nativeServicesActions } from './nativeServices/nativeServices.slice' import { pushNotificationsReducer } from './pushNotifications/pushNotifications.slice' +import { keysReducer } from './keys/keys.slice' +import { usersMetadataReducer } from './userMetadata/usersMetadata.slice' export const reducers = { ...stateManagerReducers.reducers, @@ -12,6 +14,8 @@ export const reducers = { [StoreKeys.Navigation]: navigationReducer, [StoreKeys.NativeServices]: nativeServicesReducer, [StoreKeys.PushNotifications]: pushNotificationsReducer, + [StoreKeys.Keys]: keysReducer, + [StoreKeys.UsersMetadata]: usersMetadataReducer, } export const allReducers = combineReducers(reducers) diff --git a/packages/mobile/src/store/root.saga.ts b/packages/mobile/src/store/root.saga.ts index c17191aa3d..5a818fbf6d 100644 --- a/packages/mobile/src/store/root.saga.ts +++ b/packages/mobile/src/store/root.saga.ts @@ -3,12 +3,14 @@ import { nativeServicesMasterSaga } from './nativeServices/nativeServices.master import { navigationMasterSaga } from './navigation/navigation.master.saga' import { initMasterSaga } from './init/init.master.saga' import { initActions } from './init/init.slice' -import { publicChannels } from '@quiet/state-manager' +import { publicChannels, Socket } from '@quiet/state-manager' import { showNotificationSaga } from './nativeServices/showNotification/showNotification.saga' import { clearReduxStore } from './nativeServices/leaveCommunity/leaveCommunity.saga' import { pushNotificationsMasterSaga } from './pushNotifications/pushNotifications.master.saga' import { setEngine, CryptoEngine } from 'pkijs' import { createLogger } from '../utils/logger' +import { keysMasterSaga } from './keys/keys.master.saga' +import { usersMetadataMasterSaga } from './userMetadata/usersMetadata.master.saga' const logger = createLogger('root') @@ -54,6 +56,8 @@ function* storeReadySaga(): Generator { fork(navigationMasterSaga), fork(nativeServicesMasterSaga), fork(pushNotificationsMasterSaga), + fork(keysMasterSaga), + fork(usersMetadataMasterSaga), // Below line is reponsible for displaying notifications about messages from channels other than currently viewing one takeEvery(publicChannels.actions.markUnreadChannel.type, showNotificationSaga), takeLeading(initActions.canceledRootTask.type, clearReduxStore), diff --git a/packages/mobile/src/store/store.keys.ts b/packages/mobile/src/store/store.keys.ts index fdb67eed97..d015f270f8 100644 --- a/packages/mobile/src/store/store.keys.ts +++ b/packages/mobile/src/store/store.keys.ts @@ -3,4 +3,6 @@ export enum StoreKeys { Navigation = 'Navigation', NativeServices = 'NativeServices', PushNotifications = 'PushNotifications', + Keys = 'Keys', + UsersMetadata = 'UsersMetadata', } diff --git a/packages/mobile/src/store/userMetadata/saveUserMetadataNatively/saveUserMetadataNatively.saga.ts b/packages/mobile/src/store/userMetadata/saveUserMetadataNatively/saveUserMetadataNatively.saga.ts new file mode 100644 index 0000000000..f10b5b9123 --- /dev/null +++ b/packages/mobile/src/store/userMetadata/saveUserMetadataNatively/saveUserMetadataNatively.saga.ts @@ -0,0 +1,23 @@ +import { type PayloadAction } from '@reduxjs/toolkit' +import { call } from 'typed-redux-saga' +import { NativeModules } from 'react-native' + +import { UserProfilesUpdatedPayload } from '@quiet/types' +import { createLogger } from '../../../utils/logger' + +const logger = createLogger('saveUserMetadataNativelySaga') + +export function* saveUserMetadataNativelySaga(action: PayloadAction): Generator { + logger.info( + `Storing user metadata in ios native storage (new count = ${action.payload.new.length}, update count = ${action.payload.updates.length})` + ) + try { + const updates: string[] = [ + ...action.payload.new.map(profile => JSON.stringify(profile)), + ...action.payload.updates.map(profile => JSON.stringify(profile)), + ] + yield* call(NativeModules.CommunicationModule.saveUserMetadata, updates) + } catch (e) { + logger.error('Error while updating user metadata in ios native storage', e) + } +} diff --git a/packages/mobile/src/store/userMetadata/usersMetadata.master.saga.ts b/packages/mobile/src/store/userMetadata/usersMetadata.master.saga.ts new file mode 100644 index 0000000000..1be76b3594 --- /dev/null +++ b/packages/mobile/src/store/userMetadata/usersMetadata.master.saga.ts @@ -0,0 +1,19 @@ +import { takeEvery, cancelled } from 'redux-saga/effects' +import { all } from 'typed-redux-saga' +import { saveUserMetadataNativelySaga } from './saveUserMetadataNatively/saveUserMetadataNatively.saga' +import { createLogger } from '../../utils/logger' +import { usersMetadataActions } from './usersMetadata.slice' + +const logger = createLogger('usersMetadataMasterSaga') + +export function* usersMetadataMasterSaga(): Generator { + logger.info('usersMetadataMasterSaga starting') + try { + yield all([takeEvery(usersMetadataActions.saveUserMetadataNatively.type, saveUserMetadataNativelySaga)]) + } finally { + logger.info('usersMetadataMasterSaga stopping') + if (yield cancelled()) { + logger.info('usersMetadataMasterSaga cancelled') + } + } +} diff --git a/packages/mobile/src/store/userMetadata/usersMetadata.selectors..ts b/packages/mobile/src/store/userMetadata/usersMetadata.selectors..ts new file mode 100644 index 0000000000..513827bdda --- /dev/null +++ b/packages/mobile/src/store/userMetadata/usersMetadata.selectors..ts @@ -0,0 +1,7 @@ +import { StoreKeys } from '../store.keys' +import { CreatedSelectors, StoreState } from '../store.types' + +const usersMetadataSlice: CreatedSelectors[StoreKeys.UsersMetadata] = (state: StoreState) => + state[StoreKeys.UsersMetadata] + +export const usersMetadataSelectors = {} diff --git a/packages/mobile/src/store/userMetadata/usersMetadata.slice.ts b/packages/mobile/src/store/userMetadata/usersMetadata.slice.ts new file mode 100644 index 0000000000..7c415e8c50 --- /dev/null +++ b/packages/mobile/src/store/userMetadata/usersMetadata.slice.ts @@ -0,0 +1,19 @@ +import { createSlice, type PayloadAction } from '@reduxjs/toolkit' +import { StoreKeys } from '../store.keys' +import { UserProfilesUpdatedPayload } from '@quiet/types' +import { createLogger } from '../../utils/logger' + +const logger = createLogger('keysSlice') + +export class UsersMetadataState {} + +export const usersMetadataSlice = createSlice({ + initialState: { ...new UsersMetadataState() }, + name: StoreKeys.Keys, + reducers: { + saveUserMetadataNatively: (state, _action: PayloadAction) => state, + }, +}) + +export const usersMetadataActions = usersMetadataSlice.actions +export const usersMetadataReducer = usersMetadataSlice.reducer diff --git a/packages/mobile/src/store/userMetadata/usersMetadata.transform.ts b/packages/mobile/src/store/userMetadata/usersMetadata.transform.ts new file mode 100644 index 0000000000..8bb98ffe53 --- /dev/null +++ b/packages/mobile/src/store/userMetadata/usersMetadata.transform.ts @@ -0,0 +1,14 @@ +import { createTransform } from 'redux-persist' +import { StoreKeys } from '../store.keys' +import { UsersMetadataState } from './usersMetadata.slice' + +export const UsersMetadataTransform = createTransform( + (inboundState: UsersMetadataState, _key: any) => { + return inboundState + }, + (outboundState: UsersMetadataState, _key: any) => { + // TODO: determine if we still need this transform + return outboundState + }, + { whitelist: [StoreKeys.UsersMetadata] } +) diff --git a/packages/mobile/src/store/userMetadata/usersMetadata.types.ts b/packages/mobile/src/store/userMetadata/usersMetadata.types.ts new file mode 100644 index 0000000000..5148b443a9 --- /dev/null +++ b/packages/mobile/src/store/userMetadata/usersMetadata.types.ts @@ -0,0 +1,12 @@ +export type ExtendedKeyScope = { + type: string + name: string + generation: number + keyType: string +} + +export interface StorableKey { + scope: ExtendedKeyScope + key: string + teamId: string +} diff --git a/packages/state-manager/package-lock.json b/packages/state-manager/package-lock.json index 190d9cfa2d..db39da7acc 100644 --- a/packages/state-manager/package-lock.json +++ b/packages/state-manager/package-lock.json @@ -14,6 +14,7 @@ "@reduxjs/toolkit": "^1.9.1", "factory-girl": "^5.0.4", "get-port": "^5.1.1", + "lodash": "^4.17.23", "luxon": "^2.0.2", "redux": "^4.1.1", "redux-persist": "^6.0.0", @@ -31,6 +32,7 @@ "@peculiar/webcrypto": "1.4.3", "@types/factory-girl": "^5.0.12", "@types/jest": "^26.0.24", + "@types/lodash": "^4.17.24", "@types/luxon": "^2.0.0", "@types/redux-saga": "^0.10.5", "babel-jest": "^29.3.1", @@ -4922,6 +4924,13 @@ "pretty-format": "^26.0.0" } }, + "node_modules/@types/lodash": { + "version": "4.17.24", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.24.tgz", + "integrity": "sha512-gIW7lQLZbue7lRSWEFql49QJJWThrTFFeIMJdp3eH4tKoxm1OvEPg02rm4wCCSHS0cL3/Fizimb35b7k8atwsQ==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/luxon": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-2.0.0.tgz", @@ -11378,10 +11387,10 @@ } }, "node_modules/lodash": { - "version": "4.17.21", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", - "dev": true + "version": "4.17.23", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.23.tgz", + "integrity": "sha512-LgVTMpQtIopCi79SJeDiP0TfWi5CNEc/L/aRdTh3yIvmZXTnheWpKjSZhnvMl8iXbC1tFg9gdHHDMLoV7CnG+w==", + "license": "MIT" }, "node_modules/lodash.isequal": { "version": "4.5.0", @@ -16579,6 +16588,12 @@ "pretty-format": "^26.0.0" } }, + "@types/lodash": { + "version": "4.17.24", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.24.tgz", + "integrity": "sha512-gIW7lQLZbue7lRSWEFql49QJJWThrTFFeIMJdp3eH4tKoxm1OvEPg02rm4wCCSHS0cL3/Fizimb35b7k8atwsQ==", + "dev": true + }, "@types/luxon": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-2.0.0.tgz", @@ -21516,10 +21531,9 @@ } }, "lodash": { - "version": "4.17.21", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", - "dev": true + "version": "4.17.23", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.23.tgz", + "integrity": "sha512-LgVTMpQtIopCi79SJeDiP0TfWi5CNEc/L/aRdTh3yIvmZXTnheWpKjSZhnvMl8iXbC1tFg9gdHHDMLoV7CnG+w==" }, "lodash.isequal": { "version": "4.5.0", diff --git a/packages/state-manager/package.json b/packages/state-manager/package.json index a8279f49d1..6b0cb60265 100644 --- a/packages/state-manager/package.json +++ b/packages/state-manager/package.json @@ -33,6 +33,7 @@ "@reduxjs/toolkit": "^1.9.1", "factory-girl": "^5.0.4", "get-port": "^5.1.1", + "lodash": "^4.17.23", "luxon": "^2.0.2", "redux": "^4.1.1", "redux-persist": "^6.0.0", @@ -54,6 +55,7 @@ "@types/factory-girl": "^5.0.12", "@quiet/node-common": "^4.0.3", "@types/jest": "^26.0.24", + "@types/lodash": "^4.17.24", "@types/luxon": "^2.0.0", "@types/redux-saga": "^0.10.5", "babel-jest": "^29.3.1", diff --git a/packages/state-manager/src/sagas/socket/startConnection/startConnection.saga.ts b/packages/state-manager/src/sagas/socket/startConnection/startConnection.saga.ts index 4215fdf693..e988254ddf 100644 --- a/packages/state-manager/src/sagas/socket/startConnection/startConnection.saga.ts +++ b/packages/state-manager/src/sagas/socket/startConnection/startConnection.saga.ts @@ -41,6 +41,7 @@ import { HCaptchaRequest, HCaptchaChallengeRequest, InviteResultWithSalt, + UserProfilesUpdatedPayload, } from '@quiet/types' import { createLogger } from '../../../utils/logger' diff --git a/packages/state-manager/src/sagas/users/userProfile/updateUserProfiles.saga.test.ts b/packages/state-manager/src/sagas/users/userProfile/updateUserProfiles.saga.test.ts new file mode 100644 index 0000000000..48d176d5be --- /dev/null +++ b/packages/state-manager/src/sagas/users/userProfile/updateUserProfiles.saga.test.ts @@ -0,0 +1,186 @@ +import { expectSaga } from 'redux-saga-test-plan' +import { type FactoryGirl } from 'factory-girl' +import { UserProfile, SocketActions, Identity } from '@quiet/types' + +import { usersActions } from '../users.slice' +import { updateUserProfilesSaga } from './updateUserProfiles.saga' +import { MockedSocket } from '../../../utils/tests/mockedSocket' +import { Socket } from '../../../types' +import { Store } from '../../store.types' +import { prepareStore, testReducers } from '../../../utils/tests/prepareStore' +import { getBaseTypesFactory } from '../../../utils/tests/factories' +import { combineReducers } from 'redux' +import { userProfileSelectors } from './userProfile.selectors' +import { createLogger } from '../../../utils/logger' + +describe('updateUserProfilesSaga', () => { + let store: Store + let baseTypesFactory: FactoryGirl + let socket: MockedSocket + let userProfile: UserProfile + let userId: string + + const logger = createLogger('updateUserProfilesSaga:test') + + beforeEach(async () => { + socket = new MockedSocket() + store = prepareStore().store + baseTypesFactory = await getBaseTypesFactory() + + userProfile = await baseTypesFactory.create('UserProfile') + userId = userProfile.userId + }) + + it('should clear profilePhoto.path if CID changes', async () => { + const newCid = 'new-cid' + + const existingProfiles = { + [userId]: userProfile, + } + + const updatedProfile: UserProfile = { + ...userProfile, + profilePhoto: { + ...userProfile.profilePhoto!, + cid: newCid, + path: null, + }, + } + + let userProfilesSelectCalls = 0 + + await expectSaga( + updateUserProfilesSaga, + socket as unknown as Socket, + // @ts-ignore + usersActions.updateUserProfiles([updatedProfile]) + ) + .withReducer(combineReducers(testReducers)) + .withState(store.getState()) + .provide([ + { + select: ({ selector }: any, next: any) => { + if (selector === userProfileSelectors.userProfiles) { + userProfilesSelectCalls += 1 + return existingProfiles + } + return next() + }, + }, + ]) + .apply.like({ + context: socket, + fn: socket.emit, + args: [ + SocketActions.USER_PROFILES_UPDATED, + { + new: [], + updates: [updatedProfile], + }, + ], + }) + .put.like({ + action: { + type: usersActions.setUserProfiles.type, + payload: [updatedProfile], + }, + }) + .run() + }) + + it('should NOT clear profilePhoto.path if CID is the same', async () => { + const existingProfiles = { + [userId]: userProfile, + } + + const updatedProfile: UserProfile = { + ...userProfile, + profilePhoto: { + ...userProfile.profilePhoto!, + path: null, + }, + } + + let userProfilesSelectCalls = 0 + + await expectSaga( + updateUserProfilesSaga, + socket as unknown as Socket, + // @ts-ignore + usersActions.updateUserProfiles([updatedProfile]) + ) + .withReducer(combineReducers(testReducers)) + .withState(store.getState()) + .provide([ + { + select: ({ selector }: any, next: any) => { + if (selector === userProfileSelectors.userProfiles) { + userProfilesSelectCalls += 1 + return existingProfiles + } + return next() + }, + }, + ]) + // since we aren't updating the profile we aren't sending the socket event to ios + .not.apply.like({ + context: socket, + fn: socket.emit, + }) + .put.like({ + action: { + type: usersActions.setUserProfiles.type, + payload: [userProfile], + }, + }) + .run() + }) + + it('should send new profile via socket to ios', async () => { + const existingProfiles = { + [userId]: userProfile, + } + + const newProfile = await baseTypesFactory.create('UserProfile') + + let userProfilesSelectCalls = 0 + + await expectSaga( + updateUserProfilesSaga, + socket as unknown as Socket, + // @ts-ignore + { payload: [userProfile, newProfile] } + ) + .withReducer(combineReducers(testReducers)) + .withState(store.getState()) + .provide([ + { + select: ({ selector }: any, next: any) => { + if (selector === userProfileSelectors.userProfiles) { + userProfilesSelectCalls += 1 + return existingProfiles + } + return next() + }, + }, + ]) + .apply.like({ + context: socket, + fn: socket.emit, + args: [ + SocketActions.USER_PROFILES_UPDATED, + { + new: [newProfile], + updates: [], + }, + ], + }) + .put.like({ + action: { + type: usersActions.setUserProfiles.type, + payload: [userProfile, newProfile], + }, + }) + .run() + }) +}) diff --git a/packages/state-manager/src/sagas/users/userProfile/updateUserProfiles.saga.ts b/packages/state-manager/src/sagas/users/userProfile/updateUserProfiles.saga.ts new file mode 100644 index 0000000000..1a143ecb0c --- /dev/null +++ b/packages/state-manager/src/sagas/users/userProfile/updateUserProfiles.saga.ts @@ -0,0 +1,69 @@ +import { PayloadAction } from '@reduxjs/toolkit' +import { createLogger } from '../../../utils/logger' +import { apply, call, put, select } from 'typed-redux-saga' +import { userProfileSelectors } from './userProfile.selectors' +import { SocketActions, SocketEvents, SocketEventsMap, UserProfile, UserProfilesUpdatedPayload } from '@quiet/types' +import { applyEmitParams, Socket } from '../../../types' +import { usersActions } from '../users.slice' +import * as _ from 'lodash' + +const logger = createLogger('updateUserProfilesSaga') + +export function* updateUserProfilesSaga(socket: Socket, action: PayloadAction): Generator { + logger.info(`Updating user profiles (profile count = ${action.payload.length})`) + const existingProfiles = yield* select(userProfileSelectors.userProfiles) + const output: UserProfilesUpdatedPayload = { + new: [], + updates: [], + } + const updates = { ...existingProfiles } + for (const userProfile of action.payload) { + if (existingProfiles[userProfile.userId]) { + const existingProfile = existingProfiles[userProfile.userId] + + const updatedProfile = { + ...existingProfile, + ...userProfile, + } + + // If CID is the same, preserve the existing path + if ( + userProfile.profilePhoto?.cid && + existingProfile.profilePhoto?.cid === userProfile.profilePhoto.cid && + existingProfile.profilePhoto?.path + ) { + updatedProfile.profilePhoto = { + ...userProfile.profilePhoto, + path: existingProfile.profilePhoto.path, + } + } + + // If CID changed, ensure path is null (it should be null from userProfile anyway, but let's be explicit) + if (userProfile.profilePhoto?.cid && existingProfile.profilePhoto?.cid !== userProfile.profilePhoto.cid) { + updatedProfile.profilePhoto = { + ...userProfile.profilePhoto, + path: null, + } + } + + updates[userProfile.userId] = updatedProfile + if (!_.isEqual(existingProfile, updatedProfile)) { + output.updates.push(updatedProfile) + } + } else { + updates[userProfile.userId] = userProfile + output.new.push(userProfile) + } + } + const updatedUserProfiles = Object.values(updates) + logger.debug(`Updating user profiles in redux store`) + yield* put(usersActions.setUserProfiles(updatedUserProfiles)) + + if (output.new.length > 0 || output.updates.length > 0) { + logger.debug(`Emitting user profiles updated event`) + yield* apply(socket, socket.emit, applyEmitParams(SocketActions.USER_PROFILES_UPDATED, output)) + } else { + logger.trace('Skipping user profile updated event, no new or updated profiles') + } + logger.info(`Done`) +} diff --git a/packages/state-manager/src/sagas/users/users.master.saga.ts b/packages/state-manager/src/sagas/users/users.master.saga.ts index 533dde98e1..cbbe6e0639 100644 --- a/packages/state-manager/src/sagas/users/users.master.saga.ts +++ b/packages/state-manager/src/sagas/users/users.master.saga.ts @@ -5,6 +5,7 @@ import { usersActions } from './users.slice' import { saveUserProfileSaga } from './userProfile/saveUserProfile.saga' import { downloadProfilePhotosSaga } from './userProfile/downloadProfilePhotos.saga' import { createLogger } from '../../utils/logger' +import { updateUserProfilesSaga } from './userProfile/updateUserProfiles.saga' const logger = createLogger('usersMasterSaga') @@ -13,6 +14,7 @@ export function* usersMasterSaga(socket: Socket): Generator { try { yield all([ takeEvery(usersActions.saveUserProfile.type, saveUserProfileSaga, socket), + takeEvery(usersActions.updateUserProfiles.type, updateUserProfilesSaga, socket), takeEvery(usersActions.updateUserProfiles.type, downloadProfilePhotosSaga), ]) } finally { diff --git a/packages/state-manager/src/sagas/users/users.slice.test.ts b/packages/state-manager/src/sagas/users/users.slice.test.ts deleted file mode 100644 index 915ece4adf..0000000000 --- a/packages/state-manager/src/sagas/users/users.slice.test.ts +++ /dev/null @@ -1,89 +0,0 @@ -import { usersSlice, UsersState } from './users.slice' -import { UserProfile, FileMetadata } from '@quiet/types' - -describe('usersSlice', () => { - describe('updateUserProfiles', () => { - it('should clear profilePhoto.path if CID changes', () => { - const userId = 'user-1' - const oldCid = 'old-cid' - const newCid = 'new-cid' - - const initialState: UsersState = { - userProfiles: { - [userId]: { - userId, - nickname: 'alice', - profilePhoto: { - cid: oldCid, - path: '/path/to/old/photo.png', - name: 'photo', - ext: '.png', - message: { id: 'mid-1', channelId: 'PROFILE_PHOTO_CHANNEL_ID' }, - } as FileMetadata, - } as UserProfile, - }, - users: {}, - saveUserProfileError: null, - } - - const updatedProfile: UserProfile = { - userId, - nickname: 'alice', - profilePhoto: { - cid: newCid, - path: null, // Backend sends null path - name: 'photo', - ext: '.png', - message: { id: 'mid-2', channelId: 'PROFILE_PHOTO_CHANNEL_ID' }, - } as FileMetadata, - } - - const nextState = usersSlice.reducer(initialState, usersSlice.actions.updateUserProfiles([updatedProfile])) - - expect(nextState.userProfiles[userId].profilePhoto?.cid).toBe(newCid) - expect(nextState.userProfiles[userId].profilePhoto?.path).toBeNull() - }) - - it('should NOT clear profilePhoto.path if CID is the same', () => { - const userId = 'user-1' - const cid = 'same-cid' - const path = '/path/to/photo.png' - - const initialState: UsersState = { - userProfiles: { - [userId]: { - userId, - nickname: 'alice', - profilePhoto: { - cid, - path, - name: 'photo', - ext: '.png', - message: { id: 'mid-1', channelId: 'PROFILE_PHOTO_CHANNEL_ID' }, - } as FileMetadata, - } as UserProfile, - }, - users: {}, - saveUserProfileError: null, - } - - const updatedProfile: UserProfile = { - userId, - nickname: 'alice-updated', - profilePhoto: { - cid, - path: null, // Backend might send null path even if we have it locally - name: 'photo', - ext: '.png', - message: { id: 'mid-1', channelId: 'PROFILE_PHOTO_CHANNEL_ID' }, - } as FileMetadata, - } - - const nextState = usersSlice.reducer(initialState, usersSlice.actions.updateUserProfiles([updatedProfile])) - - expect(nextState.userProfiles[userId].nickname).toBe('alice-updated') - expect(nextState.userProfiles[userId].profilePhoto?.cid).toBe(cid) - expect(nextState.userProfiles[userId].profilePhoto?.path).toBe(path) - }) - }) -}) diff --git a/packages/state-manager/src/sagas/users/users.slice.ts b/packages/state-manager/src/sagas/users/users.slice.ts index d253d51f37..54064031b7 100644 --- a/packages/state-manager/src/sagas/users/users.slice.ts +++ b/packages/state-manager/src/sagas/users/users.slice.ts @@ -40,46 +40,7 @@ export const usersSlice = createSlice({ } return state }, - updateUserProfiles: (state, action: PayloadAction) => { - if (!state.userProfiles) { - state.userProfiles = {} - } - for (const userProfile of action.payload) { - if (state.userProfiles[userProfile.userId]) { - const existingProfile = state.userProfiles[userProfile.userId] - - const updatedProfile = { - ...existingProfile, - ...userProfile, - } - - // If CID is the same, preserve the existing path - if ( - userProfile.profilePhoto?.cid && - existingProfile.profilePhoto?.cid === userProfile.profilePhoto.cid && - existingProfile.profilePhoto?.path - ) { - updatedProfile.profilePhoto = { - ...userProfile.profilePhoto, - path: existingProfile.profilePhoto.path, - } - } - - // If CID changed, ensure path is null (it should be null from userProfile anyway, but let's be explicit) - if (userProfile.profilePhoto?.cid && existingProfile.profilePhoto?.cid !== userProfile.profilePhoto.cid) { - updatedProfile.profilePhoto = { - ...userProfile.profilePhoto, - path: null, - } - } - - state.userProfiles[userProfile.userId] = updatedProfile - } else { - state.userProfiles[userProfile.userId] = userProfile - } - } - return state - }, + updateUserProfiles: (state, _action: PayloadAction) => state, // Sets a single user profile, overwriting the existing one setUserProfile: (state, action: PayloadAction) => { // Creating user profiles object for backwards compatibility with 2.0.1 diff --git a/packages/state-manager/src/utils/tests/factories.ts b/packages/state-manager/src/utils/tests/factories.ts index 7659a6c395..74f5bd6f97 100644 --- a/packages/state-manager/src/utils/tests/factories.ts +++ b/packages/state-manager/src/utils/tests/factories.ts @@ -49,6 +49,8 @@ import { HCaptchaFormResponse, HCaptchaRequest, InviteResultWithSalt, + FileMessage, + FileEncryptionMetadata, } from '@quiet/types' import { InviteResult } from '@localfirst/auth' import { createLogger } from '../logger' @@ -66,6 +68,7 @@ import { errorsActions } from '../../sagas/errors/errors.slice' import { errorsSelectors } from '../../sagas/errors/errors.selectors' import { connectionActions } from '../../sagas/appConnection/connection.slice' import { connectionSelectors } from '../../sagas/appConnection/connection.selectors' +import { randomBytes } from 'crypto' const logger = createLogger('factories') @@ -125,11 +128,38 @@ export const getBaseTypesFactory = async () => { bio: factory.sequence('UserProfileDisplayData.bio', (n: number) => `bio_${n}`), }) + factory.define('FileMessage', Object, { + id: factory.sequence('FileMessage.id', (n: number) => `profile-photo-user-profile-photo-cid-${n}-${n}`), + channelId: '__profile-photo__', + }) + + factory.define('FileEncryptionMetadata', Object, { + header: factory.sequence('FileEncryptionMetadata.header', (n: number) => randomBytes(32).toString('base64')), + recipient: { + generation: 0, + type: 'ROLE', + name: 'MEMBER', + }, + }) + + factory.define('FileMetadata', Object, { + cid: factory.sequence('FileMetadata.cid', (n: number) => `user-profile-photo-cid-${n}`), + path: factory.sequence('FileMetadata.path', (n: number) => `/foo/bar/user-profile-photo-cid-${n}.png`), + ext: '.png', + name: factory.sequence('FileMetadata.name', (n: number) => `user-profile-photo-name-${n}`), + message: factory.assoc('FileMessage'), + size: factory.sequence('FileMetadata.size', (n: number) => 1024 + n), + width: factory.sequence('FileMetadata.width', (n: number) => 100 + n), + height: factory.sequence('FileMetadata.height', (n: number) => 100 + n), + enc: factory.assoc('FileEncryptionMetadata'), + }) + factory.define('UserProfile', Object, { userId: factory.sequence('UserProfile.userId', (n: number) => `userId_${n}`), nickname: factory.sequence('UserProfile.nickname', (n: number) => `userProfile.nickname_${n}`), - photo: 'dGVzdAo=', + photo: undefined, bio: factory.sequence('UserProfile.bio', (n: number) => `bio_${n}`), + profilePhoto: factory.assoc('FileMetadata'), }) factory.define('User', Object, { @@ -590,6 +620,21 @@ export const getSocketFactory = async () => { }, }) + factory.define(SocketActions.USER_PROFILES_UPDATED, Object, [ + { + profile: { + userId: 'user-id', + nickname: 'Test User', + photo: 'dGVzdAo=', + bio: 'This is a test user profile', + userData: { + onionAddress: 'test.onion', + peerId: 'peer-id', + }, + }, + }, + ]) + factory.define(`${SocketActions.SET_USER_PROFILE}_response`, Object, { success: true, error: undefined, diff --git a/packages/types/src/files.ts b/packages/types/src/files.ts index 4997a9e7d5..cd7b823f27 100644 --- a/packages/types/src/files.ts +++ b/packages/types/src/files.ts @@ -11,20 +11,22 @@ export interface FilePreviewData { [id: string]: FileContent } +export interface FileEncryptionMetadata { + header: string + recipient: { + generation: number + type: string + name: string + } +} + export interface FileMetadata extends FileContent { cid: string message: FileMessage size?: number width?: number height?: number - enc?: { - header: string - recipient: { - generation: number - type: string - name: string - } - } + enc?: FileEncryptionMetadata } export interface AttachFilePayload { diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 0817d07065..4cdfcdaec1 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -13,4 +13,5 @@ export * from './network' export * from './test' export * from './captcha' export * from './serializer' +export * from './keys' export * from './notification' diff --git a/packages/types/src/keys.ts b/packages/types/src/keys.ts new file mode 100644 index 0000000000..7939aac759 --- /dev/null +++ b/packages/types/src/keys.ts @@ -0,0 +1,10 @@ +import { Base58, KeyMetadata } from '@localfirst/crdx' + +export interface StorableKey { + keyName: string + key: string | Base58 +} + +export interface KeysUpdatedEvent { + keys: StorableKey[] +} diff --git a/packages/types/src/socket.ts b/packages/types/src/socket.ts index f3cead2d8e..9fb324a17a 100644 --- a/packages/types/src/socket.ts +++ b/packages/types/src/socket.ts @@ -2,6 +2,7 @@ import { SetUserProfilePayload, SetUserProfileResponse, UserProfilesStoredEvent, + UserProfilesUpdatedPayload, UsersRemovedEvent, UsersUpdatedEvent, } from './user' @@ -39,6 +40,7 @@ import { } from './community' import { ErrorPayload } from './errors' import { HCaptchaChallengeRequest, HCaptchaFormResponse, HCaptchaRequest } from './captcha' +import { KeysUpdatedEvent } from './keys' // ----------------------------------------------------------------------------- // SocketActions: These are the actions the frontend emits to the backend @@ -78,6 +80,7 @@ export enum SocketActions { // ====== User ====== SET_USER_PROFILE = 'updateUserProfile', + USER_PROFILES_UPDATED = 'userProfilesUpdated', // ====== Files ====== @@ -131,6 +134,8 @@ export enum SocketEvents { USERS_UPDATED = 'usersUpdated', USERS_REMOVED = 'usersRemoved', USER_PROFILES_STORED = 'userProfilesStored', + KEYS_UPDATED = 'keysUpdated', + USER_PROFILES_UPDATED = 'userProfilesUpdatedFwd', // ====== Files ====== FILE_ATTACHED = 'fileUploaded', @@ -191,6 +196,7 @@ export interface SocketActionsMap { // ====== User Profiles ====== [SocketActions.SET_USER_PROFILE]: EmitEvent void> [SocketActions.LOAD_MIGRATION_DATA]: EmitEvent> + [SocketActions.USER_PROFILES_UPDATED]: EmitEvent // ====== Local First Auth ====== [SocketActions.VALIDATE_OR_CREATE_LONG_LIVED_LFA_INVITE]: EmitEvent< @@ -235,6 +241,8 @@ export interface SocketEventsMap { [SocketEvents.USERS_UPDATED]: EmitEvent [SocketEvents.USERS_REMOVED]: EmitEvent [SocketEvents.USER_PROFILES_STORED]: EmitEvent + [SocketEvents.KEYS_UPDATED]: EmitEvent + [SocketEvents.USER_PROFILES_UPDATED]: EmitEvent // ====== Files ====== [SocketEvents.FILE_ATTACHED]: EmitEvent diff --git a/packages/types/src/user.ts b/packages/types/src/user.ts index 4319c7d3af..be8bd1da7b 100644 --- a/packages/types/src/user.ts +++ b/packages/types/src/user.ts @@ -59,6 +59,11 @@ export interface UserProfilesStoredEvent { profiles: UserProfile[] } +export interface UserProfilesUpdatedPayload { + new: UserProfile[] + updates: UserProfile[] +} + export interface UsersUpdatedEvent { users: User[] }