import type { APIClient } from '@wireapp/api-client' ; import type { LoginData , PreKey } from '@wireapp/api-client/src/auth/' ; import { ClientClassification , ClientType , NewClient , RegisteredClient } from '@wireapp/api-client/src/client/' ; import type { CRUDEngine } from '@wireapp/store-engine' ; import type { CryptographyService } from '../cryptography/' ; import { ClientInfo , ClientBackendRepository , ClientDatabaseRepository } from './' ; export interface MetaClient extends RegisteredClient { domain : string | null ; meta : { is_verified ? : boolean ; primary_key : string ; } ; } export class ClientService { private readonly database : ClientDatabaseRepository ; private readonly backend : ClientBackendRepository ; constructor ( private readonly apiClient , private readonly storeEngine , private readonly cryptographyService ) { this . database = new ClientDatabaseRepository ( this . storeEngine ) ; this . backend = new ClientBackendRepository ( this . apiClient ) ; } public deleteLocalClient ( ) { return this . database . deleteLocalClient ( ) ; } public getClients ( ) { return this . backend . getClients ( ) ; } public getLocalClient ( ) { return this . database . getLocalClient ( ) ; } public createLocalClient ( client , domain ) { return this . database . createLocalClient ( client , domain ) ; } public async synchronizeClients ( ) { const registeredClients = await this . backend . getClients ( ) ; const filteredClients = registeredClients . filter ( client => client . id !== this . apiClient . context ! . clientId ) ; return this . database . createClientList ( this . apiClient . context ! . userId , filteredClients , this . apiClient . context ! . domain || null ) ; } public async register ( loginData , clientInfo = { classification : ClientClassification . DESKTOP , cookieLabel : 'default' , model : '@wireapp/core' , } ) { if ( ! this . apiClient . context ) { throw new Error ( 'Context is not set.' ) ; } if ( loginData . clientType === ClientType . NONE ) { throw new Error ( `Can't register client of type "${ ClientType . NONE }"` ) ; } const serializedPreKeys = await this . cryptographyService . createCryptobox ( ) ; if ( ! this . cryptographyService . cryptobox . lastResortPreKey ) { throw new Error ( 'Cryptobox got initialized without a last resort PreKey.' ) ; } const newClient = { class : clientInfo . classification , cookie : clientInfo . cookieLabel , label : clientInfo . label , lastkey : this . cryptographyService . cryptobox . serialize_prekey ( this . cryptographyService . cryptobox . lastResortPreKey ) , location : clientInfo . location , model : clientInfo . model , password : loginData . password ? String ( loginData . password ) : undefined , prekeys : serializedPreKeys , type : loginData . clientType , } ; const client = await this . backend . postClient ( newClient ) ; await this . createLocalClient ( client , this . apiClient . context . domain || null ) ; await this . cryptographyService . initCryptobox ( ) ; return client ; } }