2023-08-21 13:32:06 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
|
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-09 22:12:44 +05:00
|
|
|
import {
|
2025-10-13 14:02:32 +05:00
|
|
|
AsymmetricCipher,
|
2025-09-09 22:12:44 +05:00
|
|
|
Cipher,
|
|
|
|
|
DataFormat,
|
|
|
|
|
SerializedKey,
|
|
|
|
|
SerializedKeyPair
|
|
|
|
|
} from "@notesnook/crypto";
|
2024-09-23 15:08:57 +05:00
|
|
|
import { KVStorage } from "./database/kv.js";
|
|
|
|
|
import { ConfigStorage } from "./database/config.js";
|
2023-08-21 13:32:06 +05:00
|
|
|
|
|
|
|
|
export type Output<TOutputFormat extends DataFormat> =
|
2024-03-18 08:15:42 +05:00
|
|
|
TOutputFormat extends "uint8array" ? Uint8Array : string;
|
2023-08-21 13:32:06 +05:00
|
|
|
export type FileEncryptionMetadata = {
|
|
|
|
|
chunkSize: number;
|
|
|
|
|
iv: string;
|
2023-10-02 09:40:10 +05:00
|
|
|
size: number;
|
2023-08-21 13:32:06 +05:00
|
|
|
salt: string;
|
|
|
|
|
alg: string;
|
|
|
|
|
};
|
|
|
|
|
export type FileEncryptionMetadataWithOutputType<
|
|
|
|
|
TOutputFormat extends DataFormat
|
|
|
|
|
> = FileEncryptionMetadata & { outputType: TOutputFormat };
|
|
|
|
|
export type FileEncryptionMetadataWithHash = FileEncryptionMetadata & {
|
|
|
|
|
hash: string;
|
|
|
|
|
hashType: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export interface IStorage {
|
|
|
|
|
write<T>(key: string, data: T): Promise<void>;
|
|
|
|
|
writeMulti<T>(entries: [string, T][]): Promise<void>;
|
|
|
|
|
readMulti<T>(keys: string[]): Promise<[string, T][]>;
|
|
|
|
|
read<T>(key: string, isArray?: boolean): Promise<T | undefined>;
|
|
|
|
|
remove(key: string): Promise<void>;
|
2023-11-27 10:36:16 +05:00
|
|
|
removeMulti(keys: string[]): Promise<void>;
|
2023-08-21 13:32:06 +05:00
|
|
|
clear(): Promise<void>;
|
|
|
|
|
getAllKeys(): Promise<string[]>;
|
|
|
|
|
encrypt(key: SerializedKey, plainText: string): Promise<Cipher<"base64">>;
|
|
|
|
|
encryptMulti(
|
|
|
|
|
key: SerializedKey,
|
|
|
|
|
items: string[]
|
|
|
|
|
): Promise<Cipher<"base64">[]>;
|
|
|
|
|
decrypt(key: SerializedKey, cipherData: Cipher<"base64">): Promise<string>;
|
|
|
|
|
decryptMulti(
|
|
|
|
|
key: SerializedKey,
|
|
|
|
|
items: Cipher<"base64">[]
|
|
|
|
|
): Promise<string[]>;
|
2025-10-13 14:02:32 +05:00
|
|
|
decryptAsymmetric(
|
|
|
|
|
keyPair: SerializedKeyPair,
|
|
|
|
|
cipherData: AsymmetricCipher<"base64">
|
|
|
|
|
): Promise<string>;
|
2024-01-12 15:41:33 +05:00
|
|
|
deriveCryptoKey(credentials: SerializedKey): Promise<void>;
|
2024-12-11 14:49:26 +05:00
|
|
|
hash(
|
|
|
|
|
password: string,
|
|
|
|
|
email: string,
|
|
|
|
|
options?: { usesFallback?: boolean }
|
|
|
|
|
): Promise<string>;
|
2024-01-12 15:41:33 +05:00
|
|
|
getCryptoKey(): Promise<string | undefined>;
|
2023-08-21 13:32:06 +05:00
|
|
|
generateCryptoKey(password: string, salt?: string): Promise<SerializedKey>;
|
2025-09-09 22:12:44 +05:00
|
|
|
generateCryptoKeyPair(): Promise<SerializedKeyPair>;
|
2023-08-21 13:32:06 +05:00
|
|
|
|
2024-12-23 13:35:36 +05:00
|
|
|
generateCryptoKeyFallback(
|
|
|
|
|
password: string,
|
|
|
|
|
salt?: string
|
|
|
|
|
): Promise<SerializedKey>;
|
2024-12-11 14:49:26 +05:00
|
|
|
deriveCryptoKeyFallback(credentials: SerializedKey): Promise<void>;
|
|
|
|
|
|
2023-08-21 13:32:06 +05:00
|
|
|
// async generateRandomKey() {
|
|
|
|
|
// const passwordBytes = randomBytes(124);
|
|
|
|
|
// const password = passwordBytes.toString("base64");
|
|
|
|
|
// return await this.storage.generateCryptoKey(password);
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ICompressor {
|
|
|
|
|
compress(data: string): Promise<string>;
|
|
|
|
|
decompress(data: string): Promise<string>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type RequestOptions = {
|
|
|
|
|
url: string;
|
2023-10-02 09:40:10 +05:00
|
|
|
// metadata?: AttachmentMetadata;
|
2023-08-21 13:32:06 +05:00
|
|
|
chunkSize: number;
|
|
|
|
|
headers: { Authorization: string };
|
|
|
|
|
};
|
|
|
|
|
type Cancellable<T> = {
|
|
|
|
|
execute(): Promise<T>;
|
|
|
|
|
cancel(reason?: string): Promise<void>;
|
|
|
|
|
};
|
|
|
|
|
export interface IFileStorage {
|
|
|
|
|
downloadFile(
|
|
|
|
|
filename: string,
|
|
|
|
|
requestOptions: RequestOptions
|
|
|
|
|
): Cancellable<boolean>;
|
|
|
|
|
uploadFile(
|
|
|
|
|
filename: string,
|
|
|
|
|
requestOptions: RequestOptions
|
|
|
|
|
): Cancellable<boolean>;
|
|
|
|
|
readEncrypted<TOutputFormat extends DataFormat>(
|
|
|
|
|
filename: string,
|
|
|
|
|
encryptionKey: SerializedKey,
|
|
|
|
|
cipherData: FileEncryptionMetadataWithOutputType<TOutputFormat>
|
|
|
|
|
): Promise<Output<TOutputFormat> | undefined>;
|
|
|
|
|
writeEncryptedBase64(
|
|
|
|
|
data: string,
|
|
|
|
|
encryptionKey: SerializedKey,
|
|
|
|
|
mimeType: string
|
|
|
|
|
): Promise<FileEncryptionMetadataWithHash>;
|
|
|
|
|
deleteFile(
|
|
|
|
|
filename: string,
|
|
|
|
|
requestOptions?: RequestOptions
|
|
|
|
|
): Promise<boolean>;
|
|
|
|
|
exists(filename: string): Promise<boolean>;
|
2024-08-02 15:14:06 +05:00
|
|
|
bulkExists(filenames: string[]): Promise<string[]>;
|
2024-07-26 12:47:17 +05:00
|
|
|
getUploadedFileSize(filename: string): Promise<number>;
|
2023-08-21 13:32:06 +05:00
|
|
|
clearFileStorage(): Promise<void>;
|
|
|
|
|
hashBase64(data: string): Promise<{ hash: string; type: string }>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type StorageAccessor = () => IStorage;
|
2024-02-05 22:05:55 +05:00
|
|
|
export type KVStorageAccessor = () => KVStorage;
|
2024-07-16 14:26:06 +05:00
|
|
|
export type ConfigStorageAccessor = () => ConfigStorage;
|
2024-11-12 13:13:46 +05:00
|
|
|
export type CompressorAccessor = () => Promise<ICompressor>;
|