@haex-space/vault-sdk 2.5.50 → 2.5.54

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/dist/index.mjs CHANGED
@@ -643,6 +643,224 @@ var DatabaseAPI = class {
643
643
  }
644
644
  };
645
645
 
646
+ // src/crypto/vaultKey.ts
647
+ var PBKDF2_ITERATIONS = 6e5;
648
+ var KEY_LENGTH = 256;
649
+ var ALGORITHM = "AES-GCM";
650
+ async function deriveKeyFromPassword(password, salt) {
651
+ const encoder = new TextEncoder();
652
+ const passwordBuffer = encoder.encode(password);
653
+ const saltBuffer = new Uint8Array(salt);
654
+ const keyMaterial = await crypto.subtle.importKey(
655
+ "raw",
656
+ passwordBuffer,
657
+ "PBKDF2",
658
+ false,
659
+ ["deriveKey"]
660
+ );
661
+ return await crypto.subtle.deriveKey(
662
+ {
663
+ name: "PBKDF2",
664
+ salt: saltBuffer,
665
+ iterations: PBKDF2_ITERATIONS,
666
+ hash: "SHA-256"
667
+ },
668
+ keyMaterial,
669
+ { name: ALGORITHM, length: KEY_LENGTH },
670
+ false,
671
+ // not extractable
672
+ ["encrypt", "decrypt"]
673
+ );
674
+ }
675
+ function generateVaultKey() {
676
+ return crypto.getRandomValues(new Uint8Array(32));
677
+ }
678
+ async function encryptString(data, derivedKey) {
679
+ const nonce = crypto.getRandomValues(new Uint8Array(12));
680
+ const encoder = new TextEncoder();
681
+ const dataBuffer = encoder.encode(data);
682
+ const encryptedBuffer = await crypto.subtle.encrypt(
683
+ {
684
+ name: ALGORITHM,
685
+ iv: nonce
686
+ },
687
+ derivedKey,
688
+ dataBuffer
689
+ );
690
+ return {
691
+ encryptedData: arrayBufferToBase64(encryptedBuffer),
692
+ nonce: arrayBufferToBase64(nonce)
693
+ };
694
+ }
695
+ async function decryptString(encryptedData, nonce, derivedKey) {
696
+ const encryptedBuffer = base64ToArrayBuffer(encryptedData);
697
+ const nonceBuffer = base64ToArrayBuffer(nonce);
698
+ const encryptedDataBuffer = new Uint8Array(encryptedBuffer);
699
+ const iv = new Uint8Array(nonceBuffer);
700
+ const decryptedBuffer = await crypto.subtle.decrypt(
701
+ {
702
+ name: ALGORITHM,
703
+ iv
704
+ },
705
+ derivedKey,
706
+ encryptedDataBuffer
707
+ );
708
+ const decoder = new TextDecoder();
709
+ return decoder.decode(decryptedBuffer);
710
+ }
711
+ async function encryptVaultKey(vaultKey, password) {
712
+ const salt = crypto.getRandomValues(new Uint8Array(32));
713
+ const derivedKey = await deriveKeyFromPassword(password, salt);
714
+ const nonce = crypto.getRandomValues(new Uint8Array(12));
715
+ const vaultKeyBuffer = new Uint8Array(vaultKey);
716
+ const encryptedBuffer = await crypto.subtle.encrypt(
717
+ {
718
+ name: ALGORITHM,
719
+ iv: nonce
720
+ },
721
+ derivedKey,
722
+ vaultKeyBuffer
723
+ );
724
+ return {
725
+ encryptedVaultKey: arrayBufferToBase64(encryptedBuffer),
726
+ salt: arrayBufferToBase64(salt),
727
+ vaultKeyNonce: arrayBufferToBase64(nonce)
728
+ };
729
+ }
730
+ async function decryptVaultKey(encryptedVaultKey, salt, vaultKeyNonce, password) {
731
+ const encryptedBuffer = base64ToArrayBuffer(encryptedVaultKey);
732
+ const saltBuffer = base64ToArrayBuffer(salt);
733
+ const nonceBuffer = base64ToArrayBuffer(vaultKeyNonce);
734
+ const derivedKey = await deriveKeyFromPassword(password, saltBuffer);
735
+ const encryptedData = new Uint8Array(encryptedBuffer);
736
+ const iv = new Uint8Array(nonceBuffer);
737
+ const decryptedBuffer = await crypto.subtle.decrypt(
738
+ {
739
+ name: ALGORITHM,
740
+ iv
741
+ },
742
+ derivedKey,
743
+ encryptedData
744
+ );
745
+ return new Uint8Array(decryptedBuffer);
746
+ }
747
+ async function decryptVaultName(encryptedVaultName, vaultNameNonce, vaultNameSalt, password) {
748
+ const saltBuffer = base64ToArrayBuffer(vaultNameSalt);
749
+ const derivedKey = await deriveKeyFromPassword(password, saltBuffer);
750
+ return decryptString(encryptedVaultName, vaultNameNonce, derivedKey);
751
+ }
752
+ async function encryptCrdtData(data, vaultKey) {
753
+ const vaultKeyBuffer = new Uint8Array(vaultKey);
754
+ const cryptoKey = await crypto.subtle.importKey(
755
+ "raw",
756
+ vaultKeyBuffer,
757
+ { name: ALGORITHM },
758
+ false,
759
+ ["encrypt"]
760
+ );
761
+ const nonce = crypto.getRandomValues(new Uint8Array(12));
762
+ const encoder = new TextEncoder();
763
+ const dataBuffer = encoder.encode(JSON.stringify(data));
764
+ const encryptedBuffer = await crypto.subtle.encrypt(
765
+ {
766
+ name: ALGORITHM,
767
+ iv: nonce
768
+ },
769
+ cryptoKey,
770
+ dataBuffer
771
+ );
772
+ return {
773
+ encryptedData: arrayBufferToBase64(encryptedBuffer),
774
+ nonce: arrayBufferToBase64(nonce)
775
+ };
776
+ }
777
+ async function wrapKey(keyToWrap, wrappingKey) {
778
+ const cryptoKey = await crypto.subtle.importKey(
779
+ "raw",
780
+ new Uint8Array(wrappingKey),
781
+ { name: ALGORITHM },
782
+ false,
783
+ ["encrypt"]
784
+ );
785
+ const nonce = crypto.getRandomValues(new Uint8Array(12));
786
+ const ciphertext = await crypto.subtle.encrypt(
787
+ { name: ALGORITHM, iv: nonce },
788
+ cryptoKey,
789
+ new Uint8Array(keyToWrap)
790
+ );
791
+ const result = new Uint8Array(12 + ciphertext.byteLength);
792
+ result.set(nonce, 0);
793
+ result.set(new Uint8Array(ciphertext), 12);
794
+ return result;
795
+ }
796
+ async function unwrapKey(wrappedKey, wrappingKey) {
797
+ const cryptoKey = await crypto.subtle.importKey(
798
+ "raw",
799
+ new Uint8Array(wrappingKey),
800
+ { name: ALGORITHM },
801
+ false,
802
+ ["decrypt"]
803
+ );
804
+ const nonce = wrappedKey.slice(0, 12);
805
+ const ciphertext = wrappedKey.slice(12);
806
+ const plaintext = await crypto.subtle.decrypt(
807
+ { name: ALGORITHM, iv: nonce },
808
+ cryptoKey,
809
+ ciphertext
810
+ );
811
+ return new Uint8Array(plaintext);
812
+ }
813
+ async function decryptCrdtData(encryptedData, nonce, vaultKey) {
814
+ const vaultKeyBuffer = new Uint8Array(vaultKey);
815
+ const cryptoKey = await crypto.subtle.importKey(
816
+ "raw",
817
+ vaultKeyBuffer,
818
+ { name: ALGORITHM },
819
+ false,
820
+ ["decrypt"]
821
+ );
822
+ const encryptedBuffer = base64ToArrayBuffer(encryptedData);
823
+ const nonceBuffer = base64ToArrayBuffer(nonce);
824
+ const encryptedDataBuffer = new Uint8Array(encryptedBuffer);
825
+ const iv = new Uint8Array(nonceBuffer);
826
+ const decryptedBuffer = await crypto.subtle.decrypt(
827
+ {
828
+ name: ALGORITHM,
829
+ iv
830
+ },
831
+ cryptoKey,
832
+ encryptedDataBuffer
833
+ );
834
+ const decoder = new TextDecoder();
835
+ const jsonString = decoder.decode(decryptedBuffer);
836
+ return JSON.parse(jsonString);
837
+ }
838
+ function arrayBufferToBase64(buffer) {
839
+ const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
840
+ if (typeof Buffer !== "undefined") {
841
+ return Buffer.from(bytes).toString("base64");
842
+ }
843
+ let binary = "";
844
+ for (let i = 0; i < bytes.length; i++) {
845
+ const byte = bytes[i];
846
+ if (byte !== void 0) {
847
+ binary += String.fromCharCode(byte);
848
+ }
849
+ }
850
+ return btoa(binary);
851
+ }
852
+ function base64ToArrayBuffer(base64) {
853
+ if (typeof Buffer !== "undefined") {
854
+ return new Uint8Array(Buffer.from(base64, "base64"));
855
+ }
856
+ const binary = atob(base64);
857
+ const bytes = new Uint8Array(binary.length);
858
+ for (let i = 0; i < binary.length; i++) {
859
+ bytes[i] = binary.charCodeAt(i);
860
+ }
861
+ return bytes;
862
+ }
863
+
646
864
  // src/api/filesystem.ts
647
865
  var FilesystemAPI = class {
648
866
  constructor(client) {
@@ -714,12 +932,7 @@ var FilesystemAPI = class {
714
932
  HAEXTENSION_METHODS.filesystem.readFile,
715
933
  { path }
716
934
  );
717
- const binary = atob(base64);
718
- const bytes = new Uint8Array(binary.length);
719
- for (let i = 0; i < binary.length; i++) {
720
- bytes[i] = binary.charCodeAt(i);
721
- }
722
- return bytes;
935
+ return base64ToArrayBuffer(base64);
723
936
  }
724
937
  /**
725
938
  * Write file contents
@@ -727,7 +940,7 @@ var FilesystemAPI = class {
727
940
  * @param data File contents as Uint8Array
728
941
  */
729
942
  async writeFile(path, data) {
730
- const base64 = btoa(String.fromCharCode(...data));
943
+ const base64 = arrayBufferToBase64(data);
731
944
  await this.client.request(
732
945
  HAEXTENSION_METHODS.filesystem.writeFile,
733
946
  { path, data: base64 }
@@ -1013,7 +1226,7 @@ var RemoteStorageAPI = class {
1013
1226
  * @param data - Data to upload
1014
1227
  */
1015
1228
  async upload(backendId, key, data) {
1016
- const base64 = btoa(String.fromCharCode(...data));
1229
+ const base64 = arrayBufferToBase64(data);
1017
1230
  await this.client.request(HAEXTENSION_METHODS.remoteStorage.upload, {
1018
1231
  backendId,
1019
1232
  key,
@@ -1031,12 +1244,7 @@ var RemoteStorageAPI = class {
1031
1244
  HAEXTENSION_METHODS.remoteStorage.download,
1032
1245
  { backendId, key }
1033
1246
  );
1034
- const binary = atob(base64);
1035
- const bytes = new Uint8Array(binary.length);
1036
- for (let i = 0; i < binary.length; i++) {
1037
- bytes[i] = binary.charCodeAt(i);
1038
- }
1039
- return bytes;
1247
+ return base64ToArrayBuffer(base64);
1040
1248
  }
1041
1249
  /**
1042
1250
  * Delete an object from a storage backend
@@ -2327,224 +2535,6 @@ async function verifyExtensionSignature(files, manifest) {
2327
2535
  }
2328
2536
  }
2329
2537
 
2330
- // src/crypto/vaultKey.ts
2331
- var PBKDF2_ITERATIONS = 6e5;
2332
- var KEY_LENGTH = 256;
2333
- var ALGORITHM = "AES-GCM";
2334
- async function deriveKeyFromPassword(password, salt) {
2335
- const encoder = new TextEncoder();
2336
- const passwordBuffer = encoder.encode(password);
2337
- const saltBuffer = new Uint8Array(salt);
2338
- const keyMaterial = await crypto.subtle.importKey(
2339
- "raw",
2340
- passwordBuffer,
2341
- "PBKDF2",
2342
- false,
2343
- ["deriveKey"]
2344
- );
2345
- return await crypto.subtle.deriveKey(
2346
- {
2347
- name: "PBKDF2",
2348
- salt: saltBuffer,
2349
- iterations: PBKDF2_ITERATIONS,
2350
- hash: "SHA-256"
2351
- },
2352
- keyMaterial,
2353
- { name: ALGORITHM, length: KEY_LENGTH },
2354
- false,
2355
- // not extractable
2356
- ["encrypt", "decrypt"]
2357
- );
2358
- }
2359
- function generateVaultKey() {
2360
- return crypto.getRandomValues(new Uint8Array(32));
2361
- }
2362
- async function encryptString(data, derivedKey) {
2363
- const nonce = crypto.getRandomValues(new Uint8Array(12));
2364
- const encoder = new TextEncoder();
2365
- const dataBuffer = encoder.encode(data);
2366
- const encryptedBuffer = await crypto.subtle.encrypt(
2367
- {
2368
- name: ALGORITHM,
2369
- iv: nonce
2370
- },
2371
- derivedKey,
2372
- dataBuffer
2373
- );
2374
- return {
2375
- encryptedData: arrayBufferToBase64(encryptedBuffer),
2376
- nonce: arrayBufferToBase64(nonce)
2377
- };
2378
- }
2379
- async function decryptString(encryptedData, nonce, derivedKey) {
2380
- const encryptedBuffer = base64ToArrayBuffer(encryptedData);
2381
- const nonceBuffer = base64ToArrayBuffer(nonce);
2382
- const encryptedDataBuffer = new Uint8Array(encryptedBuffer);
2383
- const iv = new Uint8Array(nonceBuffer);
2384
- const decryptedBuffer = await crypto.subtle.decrypt(
2385
- {
2386
- name: ALGORITHM,
2387
- iv
2388
- },
2389
- derivedKey,
2390
- encryptedDataBuffer
2391
- );
2392
- const decoder = new TextDecoder();
2393
- return decoder.decode(decryptedBuffer);
2394
- }
2395
- async function encryptVaultKey(vaultKey, password) {
2396
- const salt = crypto.getRandomValues(new Uint8Array(32));
2397
- const derivedKey = await deriveKeyFromPassword(password, salt);
2398
- const nonce = crypto.getRandomValues(new Uint8Array(12));
2399
- const vaultKeyBuffer = new Uint8Array(vaultKey);
2400
- const encryptedBuffer = await crypto.subtle.encrypt(
2401
- {
2402
- name: ALGORITHM,
2403
- iv: nonce
2404
- },
2405
- derivedKey,
2406
- vaultKeyBuffer
2407
- );
2408
- return {
2409
- encryptedVaultKey: arrayBufferToBase64(encryptedBuffer),
2410
- salt: arrayBufferToBase64(salt),
2411
- vaultKeyNonce: arrayBufferToBase64(nonce)
2412
- };
2413
- }
2414
- async function decryptVaultKey(encryptedVaultKey, salt, vaultKeyNonce, password) {
2415
- const encryptedBuffer = base64ToArrayBuffer(encryptedVaultKey);
2416
- const saltBuffer = base64ToArrayBuffer(salt);
2417
- const nonceBuffer = base64ToArrayBuffer(vaultKeyNonce);
2418
- const derivedKey = await deriveKeyFromPassword(password, saltBuffer);
2419
- const encryptedData = new Uint8Array(encryptedBuffer);
2420
- const iv = new Uint8Array(nonceBuffer);
2421
- const decryptedBuffer = await crypto.subtle.decrypt(
2422
- {
2423
- name: ALGORITHM,
2424
- iv
2425
- },
2426
- derivedKey,
2427
- encryptedData
2428
- );
2429
- return new Uint8Array(decryptedBuffer);
2430
- }
2431
- async function decryptVaultName(encryptedVaultName, vaultNameNonce, vaultNameSalt, password) {
2432
- const saltBuffer = base64ToArrayBuffer(vaultNameSalt);
2433
- const derivedKey = await deriveKeyFromPassword(password, saltBuffer);
2434
- return decryptString(encryptedVaultName, vaultNameNonce, derivedKey);
2435
- }
2436
- async function encryptCrdtData(data, vaultKey) {
2437
- const vaultKeyBuffer = new Uint8Array(vaultKey);
2438
- const cryptoKey = await crypto.subtle.importKey(
2439
- "raw",
2440
- vaultKeyBuffer,
2441
- { name: ALGORITHM },
2442
- false,
2443
- ["encrypt"]
2444
- );
2445
- const nonce = crypto.getRandomValues(new Uint8Array(12));
2446
- const encoder = new TextEncoder();
2447
- const dataBuffer = encoder.encode(JSON.stringify(data));
2448
- const encryptedBuffer = await crypto.subtle.encrypt(
2449
- {
2450
- name: ALGORITHM,
2451
- iv: nonce
2452
- },
2453
- cryptoKey,
2454
- dataBuffer
2455
- );
2456
- return {
2457
- encryptedData: arrayBufferToBase64(encryptedBuffer),
2458
- nonce: arrayBufferToBase64(nonce)
2459
- };
2460
- }
2461
- async function wrapKey(keyToWrap, wrappingKey) {
2462
- const cryptoKey = await crypto.subtle.importKey(
2463
- "raw",
2464
- new Uint8Array(wrappingKey),
2465
- { name: ALGORITHM },
2466
- false,
2467
- ["encrypt"]
2468
- );
2469
- const nonce = crypto.getRandomValues(new Uint8Array(12));
2470
- const ciphertext = await crypto.subtle.encrypt(
2471
- { name: ALGORITHM, iv: nonce },
2472
- cryptoKey,
2473
- new Uint8Array(keyToWrap)
2474
- );
2475
- const result = new Uint8Array(12 + ciphertext.byteLength);
2476
- result.set(nonce, 0);
2477
- result.set(new Uint8Array(ciphertext), 12);
2478
- return result;
2479
- }
2480
- async function unwrapKey(wrappedKey, wrappingKey) {
2481
- const cryptoKey = await crypto.subtle.importKey(
2482
- "raw",
2483
- new Uint8Array(wrappingKey),
2484
- { name: ALGORITHM },
2485
- false,
2486
- ["decrypt"]
2487
- );
2488
- const nonce = wrappedKey.slice(0, 12);
2489
- const ciphertext = wrappedKey.slice(12);
2490
- const plaintext = await crypto.subtle.decrypt(
2491
- { name: ALGORITHM, iv: nonce },
2492
- cryptoKey,
2493
- ciphertext
2494
- );
2495
- return new Uint8Array(plaintext);
2496
- }
2497
- async function decryptCrdtData(encryptedData, nonce, vaultKey) {
2498
- const vaultKeyBuffer = new Uint8Array(vaultKey);
2499
- const cryptoKey = await crypto.subtle.importKey(
2500
- "raw",
2501
- vaultKeyBuffer,
2502
- { name: ALGORITHM },
2503
- false,
2504
- ["decrypt"]
2505
- );
2506
- const encryptedBuffer = base64ToArrayBuffer(encryptedData);
2507
- const nonceBuffer = base64ToArrayBuffer(nonce);
2508
- const encryptedDataBuffer = new Uint8Array(encryptedBuffer);
2509
- const iv = new Uint8Array(nonceBuffer);
2510
- const decryptedBuffer = await crypto.subtle.decrypt(
2511
- {
2512
- name: ALGORITHM,
2513
- iv
2514
- },
2515
- cryptoKey,
2516
- encryptedDataBuffer
2517
- );
2518
- const decoder = new TextDecoder();
2519
- const jsonString = decoder.decode(decryptedBuffer);
2520
- return JSON.parse(jsonString);
2521
- }
2522
- function arrayBufferToBase64(buffer) {
2523
- const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
2524
- if (typeof Buffer !== "undefined") {
2525
- return Buffer.from(bytes).toString("base64");
2526
- }
2527
- let binary = "";
2528
- for (let i = 0; i < bytes.length; i++) {
2529
- const byte = bytes[i];
2530
- if (byte !== void 0) {
2531
- binary += String.fromCharCode(byte);
2532
- }
2533
- }
2534
- return btoa(binary);
2535
- }
2536
- function base64ToArrayBuffer(base64) {
2537
- if (typeof Buffer !== "undefined") {
2538
- return new Uint8Array(Buffer.from(base64, "base64"));
2539
- }
2540
- const binary = atob(base64);
2541
- const bytes = new Uint8Array(binary.length);
2542
- for (let i = 0; i < binary.length; i++) {
2543
- bytes[i] = binary.charCodeAt(i);
2544
- }
2545
- return bytes;
2546
- }
2547
-
2548
2538
  // src/index.ts
2549
2539
  function createHaexVaultSdk(config = {}) {
2550
2540
  return new HaexVaultSdk(config);