@thezelijah/majik-message 1.0.21 → 1.0.22

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.
@@ -10,6 +10,6 @@ interface MajikAutosaveSchema {
10
10
  export declare function initDB(name?: string): Promise<IDBPDatabase<MajikAutosaveSchema>>;
11
11
  export declare function idbSaveBlob(id: string, data: Blob, name?: string): Promise<void>;
12
12
  export declare function idbLoadBlob(id: string, name?: string): Promise<MajikIDBSaveData | undefined>;
13
- export declare function deleteBlob(id: string): Promise<void>;
14
- export declare function clearAllBlobs(): Promise<void>;
13
+ export declare function deleteBlob(id: string, name?: string): Promise<void>;
14
+ export declare function clearAllBlobs(name?: string): Promise<void>;
15
15
  export {};
@@ -28,11 +28,17 @@ export async function idbLoadBlob(id, name = "default") {
28
28
  return undefined;
29
29
  }
30
30
  }
31
- export async function deleteBlob(id) {
32
- const db = await initDB();
33
- return db.delete("majikdata", id);
31
+ export async function deleteBlob(id, name = "default") {
32
+ try {
33
+ const db = await initDB(name);
34
+ return db.delete("majikdata", id);
35
+ }
36
+ catch (err) {
37
+ console.error(`Failed to delete blob with id "${id}":`, err);
38
+ return undefined;
39
+ }
34
40
  }
35
- export async function clearAllBlobs() {
36
- const db = await initDB();
41
+ export async function clearAllBlobs(name = "default") {
42
+ const db = await initDB(name);
37
43
  return db.clear("majikdata");
38
44
  }
@@ -237,5 +237,11 @@ export declare class MajikMessage {
237
237
  * Try to load an existing state from IDB; if none exists, create a fresh instance and save it.
238
238
  */
239
239
  static loadOrCreate<T extends MajikMessage>(this: MajikMessageStatic<T>, config: MajikMessageConfig, userProfile?: string): Promise<T>;
240
+ /**
241
+ * Reset all data to a fresh state.
242
+ * Clears cache, own accounts, contact directory, keystore, and saved data.
243
+ * WARNING: This operation is irreversible and will delete all user data.
244
+ */
245
+ resetData(userProfile?: string): Promise<void>;
240
246
  }
241
247
  export {};
@@ -10,7 +10,7 @@ import { MajikContactDirectory, } from "./core/contacts/majik-contact-directory"
10
10
  import { arrayBufferToBase64, arrayToBase64, base64ToArrayBuffer, base64ToUtf8, utf8ToBase64, } from "./core/utils/utilities";
11
11
  import { autoSaveMajikFileData, loadSavedMajikFileData, } from "./core/utils/majik-file-utils";
12
12
  import { randomBytes } from "@stablelib/random";
13
- import { idbLoadBlob, idbSaveBlob } from "./core/utils/idb-majik-system";
13
+ import { clearAllBlobs, idbLoadBlob, idbSaveBlob, } from "./core/utils/idb-majik-system";
14
14
  import { MajikMessageChat } from "./core/database/chat/majik-message-chat";
15
15
  import { MajikCompressor } from "./core/compressor/majik-compressor";
16
16
  export class MajikMessage {
@@ -1154,4 +1154,58 @@ export class MajikMessage {
1154
1154
  created.attachAutosaveHandlers();
1155
1155
  return created;
1156
1156
  }
1157
+ /**
1158
+ * Reset all data to a fresh state.
1159
+ * Clears cache, own accounts, contact directory, keystore, and saved data.
1160
+ * WARNING: This operation is irreversible and will delete all user data.
1161
+ */
1162
+ async resetData(userProfile = "default") {
1163
+ try {
1164
+ // 1. Clear envelope cache
1165
+ await this.clearCachedEnvelopes();
1166
+ // 2. Clear all own accounts from keystore
1167
+ const accountIds = [...this.ownAccountsOrder];
1168
+ for (const id of accountIds) {
1169
+ try {
1170
+ // Delete from KeyStore storage
1171
+ await KeyStore.deleteIdentity?.(id).catch(() => { });
1172
+ }
1173
+ catch (e) {
1174
+ console.warn(`Failed to delete identity ${id} from KeyStore:`, e);
1175
+ }
1176
+ }
1177
+ // 3. Clear own accounts map and order
1178
+ this.ownAccounts.clear();
1179
+ this.ownAccountsOrder = [];
1180
+ // 4. Clear contact directory
1181
+ try {
1182
+ this.contactDirectory.clear();
1183
+ }
1184
+ catch (e) {
1185
+ console.warn(`Failed to clear contacts directory: `, e);
1186
+ }
1187
+ // 5. Clear PIN hash
1188
+ this.pinHash = null;
1189
+ // 6. Reset unlocked state
1190
+ this.unlocked = false;
1191
+ // 7. Clear saved state from IndexedDB
1192
+ try {
1193
+ await clearAllBlobs(userProfile);
1194
+ }
1195
+ catch (e) {
1196
+ console.warn("Failed to clear saved state from IndexedDB:", e);
1197
+ }
1198
+ // 8. Generate new ID for fresh instance
1199
+ this.id = arrayToBase64(randomBytes(32));
1200
+ // 9. Stop and restart autosave to ensure clean state
1201
+ this.stopAutosave();
1202
+ this.startAutosave();
1203
+ this.emit("active-account-change", null);
1204
+ console.log("MajikMessage data reset successfully");
1205
+ }
1206
+ catch (err) {
1207
+ console.error("Error during resetData:", err);
1208
+ throw new Error(`Failed to reset data: ${err instanceof Error ? err.message : err}`);
1209
+ }
1210
+ }
1157
1211
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@thezelijah/majik-message",
3
3
  "type": "module",
4
4
  "description": "Encrypt and decrypt messages on any website. Secure chats with keypairs and seed-based accounts. Open source.",
5
- "version": "1.0.21",
5
+ "version": "1.0.22",
6
6
  "license": "Apache-2.0",
7
7
  "author": "Zelijah",
8
8
  "main": "./dist/index.js",