@haex-space/vault-sdk 2.5.50 → 2.5.52

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