@majikah/majik-message 0.1.15 → 0.1.16

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.
@@ -90,7 +90,7 @@ export declare class MajikMessage {
90
90
  exportContactAsJSON(contactId: string): Promise<string | null>;
91
91
  exportContactAsString(contactId: string): Promise<string | null>;
92
92
  importContactFromJSON(jsonStr: string): Promise<MAJIK_API_RESPONSE>;
93
- importContactFromString(base64Str: string): Promise<void>;
93
+ importContactFromString(base64Str: string): Promise<MAJIK_API_RESPONSE>;
94
94
  exportContactCompressed(contact: MajikContact): Promise<string>;
95
95
  importContactCompressed(base64Str: string): Promise<MajikContact>;
96
96
  addContact(contact: MajikContact): void;
@@ -6,7 +6,7 @@ import { MessageEnvelope } from "./core/messages/message-envelope";
6
6
  import { EnvelopeCache, } from "./core/messages/envelope-cache";
7
7
  import { MajikKeyStore } from "./core/crypto/keystore";
8
8
  import { MajikContactDirectory, } from "./core/contacts/majik-contact-directory";
9
- import { arrayBufferToBase64, arrayToBase64, base64ToArrayBuffer, base64ToUint8Array, base64ToUtf8, } from "./core/utils/utilities";
9
+ import { arrayBufferToBase64, arrayToBase64, base64ToArrayBuffer, base64ToUint8Array, } from "./core/utils/utilities";
10
10
  import { autoSaveMajikFileData, loadSavedMajikFileData, } from "./core/utils/majik-file-utils";
11
11
  import { randomBytes } from "@stablelib/random";
12
12
  import { clearAllBlobs, idbLoadBlob, idbSaveBlob, } from "./core/utils/idb-majik-system";
@@ -272,8 +272,6 @@ export class MajikMessage {
272
272
  }, null, 2);
273
273
  }
274
274
  async exportContactAsString(contactId) {
275
- // const json = await this.exportContactAsJSON(contactId);
276
- // return json ? utf8ToBase64(json) : null;
277
275
  const contact = this.contactDirectory.getContact(contactId);
278
276
  if (!contact)
279
277
  return null;
@@ -311,9 +309,17 @@ export class MajikMessage {
311
309
  }
312
310
  }
313
311
  async importContactFromString(base64Str) {
314
- const result = await this.importContactFromJSON(base64ToUtf8(base64Str));
315
- if (!result.success)
316
- throw new Error(result.message);
312
+ try {
313
+ const parsedContact = await this.importContactCompressed(base64Str);
314
+ this.addContact(parsedContact);
315
+ return { success: true, message: "Contact imported successfully" };
316
+ }
317
+ catch (err) {
318
+ return {
319
+ success: false,
320
+ message: err instanceof Error ? err.message : "Unknown error",
321
+ };
322
+ }
317
323
  }
318
324
  async exportContactCompressed(contact) {
319
325
  // Prepare JSON with raw keys
@@ -345,9 +351,14 @@ export class MajikMessage {
345
351
  const decompressed = gunzipSync(new Uint8Array(compressed));
346
352
  const jsonStr = new TextDecoder().decode(decompressed);
347
353
  const data = JSON.parse(jsonStr);
348
- const publicKey = data.publicKey instanceof Array
349
- ? { raw: new Uint8Array(data.publicKey) }
350
- : await crypto.subtle.importKey("raw", base64ToArrayBuffer(data.publicKey), KEY_ALGO, true, []);
354
+ const rawBuffer = base64ToArrayBuffer(data.publicKey);
355
+ let publicKey;
356
+ try {
357
+ publicKey = await crypto.subtle.importKey("raw", rawBuffer, KEY_ALGO, true, []);
358
+ }
359
+ catch {
360
+ publicKey = { raw: new Uint8Array(rawBuffer) };
361
+ }
351
362
  return new MajikContact({
352
363
  id: data.id,
353
364
  publicKey,
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@majikah/majik-message",
3
3
  "type": "module",
4
4
  "description": "Encrypt and decrypt messages on any website or platform. Secure chats with keypairs and seed-based accounts. Open source.",
5
- "version": "0.1.15",
5
+ "version": "0.1.16",
6
6
  "license": "Apache-2.0",
7
7
  "author": "Zelijah",
8
8
  "main": "./dist/index.js",