internxt-crypto 1.3.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -107,9 +107,8 @@ const password = 'your password';
107
107
  const { key, salt } = await getKeyFromPassword(password);
108
108
 
109
109
  // Hybrid email encryption
110
- const email: EmailBody = {
110
+ const email: Email = {
111
111
  text: 'email text',
112
- subject: 'email subject',
113
112
  };
114
113
  const { secretKey: bobPrivateKeys, publicKey: bobPublicKeys } = await generateEmailKeys();
115
114
  const bobWithPublicKeys = {
@@ -119,8 +118,18 @@ const bobWithPublicKeys = {
119
118
  const encryptedEmail = await encryptEmailHybrid(email, bobWithPublicKeys);
120
119
  const decryptedEmail = await decryptEmailHybrid(encryptedEmail, bobPrivateKeys);
121
120
 
122
- expect(encryptedEmail.encEmailBody.encSubject).not.toBe(email.subject);
123
- expect(decryptedEmailBody).toStrictEqual(email);
121
+ expect(decryptedEmail).toStrictEqual(email);
122
+
123
+ // Hybrid email and subject encryption
124
+ const emailAndSubject: EmailAndSubject = {
125
+ text: 'email text',
126
+ subject: 'email subject'
127
+ };
128
+ const encryptedEmailAndSubject = await encryptEmailAndSubjectHybrid(emailAndSubject, bobWithPublicKeys);
129
+ const decryptedEmailAndSubject = await decryptEmailAndSubjectHybrid(encryptedEmailAndSubject, bobPrivateKeys);
130
+
131
+ expect(encryptedEmailAndSubject.encEmail.encSubject).not.toBe(emailAndSubject.subject);
132
+ expect(decryptedEmailAndSubject).toStrictEqual(emailAndSubject);
124
133
 
125
134
 
126
135
  // password-protected email
@@ -141,73 +150,4 @@ const resultRec = await openRecoveryKeystore(recoveryCodes, recoveryKeystore);
141
150
 
142
151
  expect(resultEnc).toStrictEqual(resultRec);
143
152
 
144
- // Email storage and search
145
-
146
- // Between sessions emails are stored encrypted in IndexedDB. The encryption key is derived from user's mnemonic
147
- // During the session, all emails are decrypted and stored in the cache (up to 600 MB, if excides - we delete oldests emails)
148
- // For search, we build a search index from cache, then use Flexsearch for the search.
149
- // The search is doen separately for email content, subject, sender and recivers.
150
-
151
- // Open IndexedDB database
152
- const userID = 'user ID';
153
- const db = await openDatabase(userID);
154
- const mnemonic = genMnemonic();
155
-
156
- // Derive key for encrypting emails before storing them in a local database
157
- const key = await deriveDatabaseKey(mnemonic);
158
-
159
- // Derive key for encrypting email draft
160
- const key = await deriveDatabaseKey(mnemonic);
161
-
162
- // Encrypt and store one or several emails
163
- await encryptAndStoreEmail(email, key, db);
164
- await encryptAndStoreManyEmail(emails, key, db);
165
-
166
- // Delete given email by its ID
167
- await deleteEmail(emailID, db);
168
-
169
- // Delete oldests emails
170
- const number = 5;
171
- await deleteOldestEmails(number, db);
172
-
173
- // Get all emails with or without sorting
174
- const allEmails = await getAndDecryptAllEmails(key, db);
175
- const newestFirst = await getAllEmailsSortedNewestFirst(db, key);
176
- const oldestFirst = await getAllEmailsSortedOldestFirst(db, key);
177
-
178
- // Get the number of stored emails
179
- const count = await getEmailCount(db);
180
-
181
- // Close IndexedDB database
182
- closeDatabase(db);
183
-
184
- // Delete IndexedDB database
185
- await deleteDatabase(userID);
186
-
187
- // Create email cache
188
- const esCache = await createCacheFromDB(key, db);
189
-
190
- // Add one or multiple emails to cache
191
- const result = addEmailToCache(email, esCache);
192
- expect(result.success).toBe(true);
193
-
194
- const result = addEmailsToCache(emails, esCache);
195
- expect(result.success).toBe(true);
196
-
197
- // Get email from cache by its ID
198
- const email = await getEmailFromCache(emailID, esCache);
199
-
200
- // Delete email from cache by its ID
201
- await deleteEmailFromCache(emailID, esCache);
202
-
203
- // Create search index and search by query
204
- const searchIndex = await buildSearchIndexFromCache(esCache);
205
- const query = 'keywords to search';
206
- const options = {
207
- fields: ['subject'], // in which fields to search, all by deafult (subject, body, from, to)
208
- limit: 5, // result limit, 50 by default
209
- boost: { subject: 3, body: 1, from: 2, to: 2 }, // custom waights for matches in different email parts
210
- };
211
- const result: EmailSearchResult = await searchEmails(query, esCache, searchIndex);
212
-
213
153
  ```