Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor: import/export use cases #2397

Merged
merged 9 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/services/src/Domain/Application/ApplicationInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import {
StatusServiceInterface,
MfaServiceInterface,
GenerateUuid,
CreateDecryptedBackupFile,
} from '@standardnotes/services'
import { VaultLockServiceInterface } from './../VaultLock/VaultLockServiceInterface'
import { HistoryServiceInterface } from './../History/HistoryServiceInterface'
import { InternalEventBusInterface } from './../Internal/InternalEventBusInterface'
import { PreferenceServiceInterface } from './../Preferences/PreferenceServiceInterface'
import { AsymmetricMessageServiceInterface } from './../AsymmetricMessage/AsymmetricMessageServiceInterface'
import { ImportDataReturnType } from './../Mutator/ImportDataUseCase'
import { ImportDataResult } from '../Import/ImportDataResult'
import { ChallengeServiceInterface } from './../Challenge/ChallengeServiceInterface'
import { VaultServiceInterface } from '../Vault/VaultServiceInterface'
import { ApplicationIdentifier } from '@standardnotes/common'
Expand All @@ -42,6 +43,8 @@ import { UserServiceInterface } from '../User/UserServiceInterface'
import { SessionsClientInterface } from '../Session/SessionsClientInterface'
import { HomeServerServiceInterface } from '../HomeServer/HomeServerServiceInterface'
import { EncryptionProviderInterface } from '../Encryption/EncryptionProviderInterface'
import { Result } from '@standardnotes/domain-core'
import { CreateEncryptedBackupFile } from '../Import/CreateEncryptedBackupFile'

export interface ApplicationInterface {
deinit(mode: DeinitMode, source: DeinitSource): void
Expand All @@ -51,9 +54,6 @@ export interface ApplicationInterface {
addEventObserver(callback: ApplicationEventCallback, singleEvent?: ApplicationEvent): () => void
addSingleEventObserver(event: ApplicationEvent, callback: ApplicationEventCallback): () => void
hasProtectionSources(): boolean
createEncryptedBackupFileForAutomatedDesktopBackups(): Promise<BackupFile | undefined>
createEncryptedBackupFile(): Promise<BackupFile | undefined>
createDecryptedBackupFile(): Promise<BackupFile | undefined>
hasPasscode(): boolean
lock(): Promise<void>
setValue(key: string, value: unknown, mode?: StorageValueModes): void
Expand All @@ -68,12 +68,17 @@ export interface ApplicationInterface {
setCustomHost(host: string): Promise<void>
isUsingHomeServer(): Promise<boolean>

importData(data: BackupFile, awaitSync?: boolean): Promise<ImportDataReturnType>
importData(data: BackupFile, awaitSync?: boolean): Promise<Result<ImportDataResult>>

// Use cases
get changeAndSaveItem(): ChangeAndSaveItem
get createDecryptedBackupFile(): CreateDecryptedBackupFile
get createEncryptedBackupFile(): CreateEncryptedBackupFile
get generateUuid(): GenerateUuid
get getHost(): GetHost
get setHost(): SetHost

// Services
get alerts(): AlertService
get asymmetric(): AsymmetricMessageServiceInterface
get challenges(): ChallengeServiceInterface
Expand All @@ -85,7 +90,6 @@ export interface ApplicationInterface {
get files(): FilesClientInterface
get history(): HistoryServiceInterface
get homeServer(): HomeServerServiceInterface | undefined
get generateUuid(): GenerateUuid
get items(): ItemManagerInterface
get legacyApi(): LegacyApiServiceInterface
get mfa(): MfaServiceInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from '@standardnotes/encryption'
import { KeyParamsOrigination, ProtocolVersion } from '@standardnotes/common'
import {
BackupFile,
DecryptedPayloadInterface,
EncryptedPayloadInterface,
ItemContent,
Expand Down Expand Up @@ -54,9 +53,6 @@ export interface EncryptionProviderInterface {
getEncryptionDisplayName(): Promise<string>
upgradeAvailable(): Promise<boolean>

createEncryptedBackupFile(): Promise<BackupFile>
createDecryptedBackupFile(): BackupFile

getUserVersion(): ProtocolVersion | undefined
hasAccount(): boolean
hasPasscode(): boolean
Expand All @@ -75,6 +71,11 @@ export interface EncryptionProviderInterface {

deleteWorkspaceSpecificKeyStateFromDevice(): Promise<void>

itemsKeyForEncryptedPayload(
payload: EncryptedPayloadInterface,
): ItemsKeyInterface | KeySystemItemsKeyInterface | undefined
defaultItemsKeyForItemVersion(version: ProtocolVersion, fromKeys?: ItemsKeyInterface[]): ItemsKeyInterface | undefined

unwrapRootKey(wrappingKey: RootKeyInterface): Promise<void>
computeRootKey(password: string, keyParams: SNRootKeyParams): Promise<RootKeyInterface>
computeWrappingKey(passcode: string): Promise<RootKeyInterface>
Expand Down
50 changes: 0 additions & 50 deletions packages/services/src/Domain/Encryption/EncryptionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { InternalEventHandlerInterface } from './../Internal/InternalEventHandle
import { MutatorClientInterface } from './../Mutator/MutatorClientInterface'
import {
CreateAnyKeyParams,
CreateEncryptionSplitWithKeyLookup,
encryptedInputParametersFromPayload,
ErrorDecryptingParameters,
FindPayloadInDecryptionSplit,
Expand All @@ -16,7 +15,6 @@ import {
KeyedEncryptionSplit,
LegacyAttachedData,
RootKeyEncryptedAuthenticatedData,
SplitPayloadsByEncryptionType,
V001Algorithm,
V002Algorithm,
EncryptedOutputParameters,
Expand All @@ -25,15 +23,10 @@ import {
EncryptionOperatorsInterface,
} from '@standardnotes/encryption'
import {
BackupFile,
CreateDecryptedBackupFileContextPayload,
CreateEncryptedBackupFileContextPayload,
DecryptedPayload,
DecryptedPayloadInterface,
EncryptedPayload,
EncryptedPayloadInterface,
isDecryptedPayload,
isEncryptedPayload,
ItemContent,
ItemsKeyInterface,
RootKeyInterface,
Expand All @@ -47,7 +40,6 @@ import {
import { PureCryptoInterface } from '@standardnotes/sncrypto-common'
import {
extendArray,
isNotUndefined,
isNullOrUndefined,
isReactNativeEnvironment,
isWebCryptoAvailable,
Expand Down Expand Up @@ -81,7 +73,6 @@ import { EncryptTypeAPayloadWithKeyLookup } from './UseCase/TypeA/EncryptPayload
import { EncryptTypeAPayload } from './UseCase/TypeA/EncryptPayload'
import { ValidateAccountPasswordResult } from '../RootKeyManager/ValidateAccountPasswordResult'
import { ValidatePasscodeResult } from '../RootKeyManager/ValidatePasscodeResult'
import { ContentType } from '@standardnotes/domain-core'
import { EncryptionProviderInterface } from './EncryptionProviderInterface'
import { KeyMode } from '../RootKeyManager/KeyMode'

Expand Down Expand Up @@ -578,47 +569,6 @@ export class EncryptionService
return CreateAnyKeyParams(keyParams)
}

public async createEncryptedBackupFile(): Promise<BackupFile> {
const payloads = this.items.items.map((item) => item.payload)

const split = SplitPayloadsByEncryptionType(payloads)

const keyLookupSplit = CreateEncryptionSplitWithKeyLookup(split)

const result = await this.encryptSplit(keyLookupSplit)

const ejected = result.map((payload) => CreateEncryptedBackupFileContextPayload(payload))

const data: BackupFile = {
version: ProtocolVersionLatest,
items: ejected,
}

const keyParams = this.getRootKeyParams()
data.keyParams = keyParams?.getPortableValue()
return data
}

public createDecryptedBackupFile(): BackupFile {
const payloads = this.payloads.nonDeletedItems.filter((item) => item.content_type !== ContentType.TYPES.ItemsKey)

const data: BackupFile = {
version: ProtocolVersionLatest,
items: payloads
.map((payload) => {
if (isDecryptedPayload(payload)) {
return CreateDecryptedBackupFileContextPayload(payload)
} else if (isEncryptedPayload(payload)) {
return CreateEncryptedBackupFileContextPayload(payload)
}
return undefined
})
.filter(isNotUndefined),
}

return data
}

public hasPasscode(): boolean {
return this.rootKeyManager.hasPasscode()
}
Expand Down