@haex-space/vault-sdk 2.5.49 → 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 +275 -232
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +275 -232
- package/dist/index.mjs.map +1 -1
- package/dist/react.js +99 -15
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +99 -15
- package/dist/react.mjs.map +1 -1
- package/dist/runtime/nuxt.plugin.client.js +99 -15
- package/dist/runtime/nuxt.plugin.client.js.map +1 -1
- package/dist/runtime/nuxt.plugin.client.mjs +99 -15
- package/dist/runtime/nuxt.plugin.client.mjs.map +1 -1
- package/dist/svelte.js +99 -15
- package/dist/svelte.js.map +1 -1
- package/dist/svelte.mjs +99 -15
- package/dist/svelte.mjs.map +1 -1
- package/dist/vue.js +99 -15
- package/dist/vue.js.map +1 -1
- package/dist/vue.mjs +99 -15
- package/dist/vue.mjs.map +1 -1
- package/package.json +1 -1
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
|
-
|
|
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 =
|
|
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 =
|
|
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
|
-
|
|
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
|
|
@@ -1455,6 +1663,7 @@ var webHandlers = {
|
|
|
1455
1663
|
|
|
1456
1664
|
// src/transport/handlers/filesystem.ts
|
|
1457
1665
|
var filesystemHandlers = {
|
|
1666
|
+
// Legacy extension-specific operations
|
|
1458
1667
|
[HAEXTENSION_METHODS.filesystem.saveFile]: {
|
|
1459
1668
|
command: TAURI_COMMANDS.filesystem.saveFile,
|
|
1460
1669
|
args: (p) => ({
|
|
@@ -1476,6 +1685,58 @@ var filesystemHandlers = {
|
|
|
1476
1685
|
args: (p) => ({
|
|
1477
1686
|
dataUrl: p.dataUrl
|
|
1478
1687
|
})
|
|
1688
|
+
},
|
|
1689
|
+
// Generic filesystem operations
|
|
1690
|
+
[HAEXTENSION_METHODS.filesystem.readFile]: {
|
|
1691
|
+
command: TAURI_COMMANDS.filesystem.readFile,
|
|
1692
|
+
args: (p) => ({ path: p.path })
|
|
1693
|
+
},
|
|
1694
|
+
[HAEXTENSION_METHODS.filesystem.writeFile]: {
|
|
1695
|
+
command: TAURI_COMMANDS.filesystem.writeFile,
|
|
1696
|
+
args: (p) => ({ path: p.path, data: p.data })
|
|
1697
|
+
},
|
|
1698
|
+
[HAEXTENSION_METHODS.filesystem.readDir]: {
|
|
1699
|
+
command: TAURI_COMMANDS.filesystem.readDir,
|
|
1700
|
+
args: (p) => ({ path: p.path })
|
|
1701
|
+
},
|
|
1702
|
+
[HAEXTENSION_METHODS.filesystem.mkdir]: {
|
|
1703
|
+
command: TAURI_COMMANDS.filesystem.mkdir,
|
|
1704
|
+
args: (p) => ({ path: p.path })
|
|
1705
|
+
},
|
|
1706
|
+
[HAEXTENSION_METHODS.filesystem.remove]: {
|
|
1707
|
+
command: TAURI_COMMANDS.filesystem.remove,
|
|
1708
|
+
args: (p) => ({ path: p.path })
|
|
1709
|
+
},
|
|
1710
|
+
[HAEXTENSION_METHODS.filesystem.exists]: {
|
|
1711
|
+
command: TAURI_COMMANDS.filesystem.exists,
|
|
1712
|
+
args: (p) => ({ path: p.path })
|
|
1713
|
+
},
|
|
1714
|
+
[HAEXTENSION_METHODS.filesystem.stat]: {
|
|
1715
|
+
command: TAURI_COMMANDS.filesystem.stat,
|
|
1716
|
+
args: (p) => ({ path: p.path })
|
|
1717
|
+
},
|
|
1718
|
+
[HAEXTENSION_METHODS.filesystem.selectFolder]: {
|
|
1719
|
+
command: TAURI_COMMANDS.filesystem.selectFolder,
|
|
1720
|
+
args: (p) => ({
|
|
1721
|
+
title: p.title,
|
|
1722
|
+
defaultPath: p.defaultPath
|
|
1723
|
+
})
|
|
1724
|
+
},
|
|
1725
|
+
[HAEXTENSION_METHODS.filesystem.selectFile]: {
|
|
1726
|
+
command: TAURI_COMMANDS.filesystem.selectFile,
|
|
1727
|
+
args: (p) => ({
|
|
1728
|
+
title: p.title,
|
|
1729
|
+
defaultPath: p.defaultPath,
|
|
1730
|
+
filters: p.filters
|
|
1731
|
+
})
|
|
1732
|
+
},
|
|
1733
|
+
[HAEXTENSION_METHODS.filesystem.rename]: {
|
|
1734
|
+
command: TAURI_COMMANDS.filesystem.rename,
|
|
1735
|
+
args: (p) => ({ from: p.from, to: p.to })
|
|
1736
|
+
},
|
|
1737
|
+
[HAEXTENSION_METHODS.filesystem.copy]: {
|
|
1738
|
+
command: TAURI_COMMANDS.filesystem.copy,
|
|
1739
|
+
args: (p) => ({ from: p.from, to: p.to })
|
|
1479
1740
|
}
|
|
1480
1741
|
};
|
|
1481
1742
|
|
|
@@ -2276,224 +2537,6 @@ async function verifyExtensionSignature(files, manifest) {
|
|
|
2276
2537
|
}
|
|
2277
2538
|
}
|
|
2278
2539
|
|
|
2279
|
-
// src/crypto/vaultKey.ts
|
|
2280
|
-
var PBKDF2_ITERATIONS = 6e5;
|
|
2281
|
-
var KEY_LENGTH = 256;
|
|
2282
|
-
var ALGORITHM = "AES-GCM";
|
|
2283
|
-
async function deriveKeyFromPassword(password, salt) {
|
|
2284
|
-
const encoder = new TextEncoder();
|
|
2285
|
-
const passwordBuffer = encoder.encode(password);
|
|
2286
|
-
const saltBuffer = new Uint8Array(salt);
|
|
2287
|
-
const keyMaterial = await crypto.subtle.importKey(
|
|
2288
|
-
"raw",
|
|
2289
|
-
passwordBuffer,
|
|
2290
|
-
"PBKDF2",
|
|
2291
|
-
false,
|
|
2292
|
-
["deriveKey"]
|
|
2293
|
-
);
|
|
2294
|
-
return await crypto.subtle.deriveKey(
|
|
2295
|
-
{
|
|
2296
|
-
name: "PBKDF2",
|
|
2297
|
-
salt: saltBuffer,
|
|
2298
|
-
iterations: PBKDF2_ITERATIONS,
|
|
2299
|
-
hash: "SHA-256"
|
|
2300
|
-
},
|
|
2301
|
-
keyMaterial,
|
|
2302
|
-
{ name: ALGORITHM, length: KEY_LENGTH },
|
|
2303
|
-
false,
|
|
2304
|
-
// not extractable
|
|
2305
|
-
["encrypt", "decrypt"]
|
|
2306
|
-
);
|
|
2307
|
-
}
|
|
2308
|
-
function generateVaultKey() {
|
|
2309
|
-
return crypto.getRandomValues(new Uint8Array(32));
|
|
2310
|
-
}
|
|
2311
|
-
async function encryptString(data, derivedKey) {
|
|
2312
|
-
const nonce = crypto.getRandomValues(new Uint8Array(12));
|
|
2313
|
-
const encoder = new TextEncoder();
|
|
2314
|
-
const dataBuffer = encoder.encode(data);
|
|
2315
|
-
const encryptedBuffer = await crypto.subtle.encrypt(
|
|
2316
|
-
{
|
|
2317
|
-
name: ALGORITHM,
|
|
2318
|
-
iv: nonce
|
|
2319
|
-
},
|
|
2320
|
-
derivedKey,
|
|
2321
|
-
dataBuffer
|
|
2322
|
-
);
|
|
2323
|
-
return {
|
|
2324
|
-
encryptedData: arrayBufferToBase64(encryptedBuffer),
|
|
2325
|
-
nonce: arrayBufferToBase64(nonce)
|
|
2326
|
-
};
|
|
2327
|
-
}
|
|
2328
|
-
async function decryptString(encryptedData, nonce, derivedKey) {
|
|
2329
|
-
const encryptedBuffer = base64ToArrayBuffer(encryptedData);
|
|
2330
|
-
const nonceBuffer = base64ToArrayBuffer(nonce);
|
|
2331
|
-
const encryptedDataBuffer = new Uint8Array(encryptedBuffer);
|
|
2332
|
-
const iv = new Uint8Array(nonceBuffer);
|
|
2333
|
-
const decryptedBuffer = await crypto.subtle.decrypt(
|
|
2334
|
-
{
|
|
2335
|
-
name: ALGORITHM,
|
|
2336
|
-
iv
|
|
2337
|
-
},
|
|
2338
|
-
derivedKey,
|
|
2339
|
-
encryptedDataBuffer
|
|
2340
|
-
);
|
|
2341
|
-
const decoder = new TextDecoder();
|
|
2342
|
-
return decoder.decode(decryptedBuffer);
|
|
2343
|
-
}
|
|
2344
|
-
async function encryptVaultKey(vaultKey, password) {
|
|
2345
|
-
const salt = crypto.getRandomValues(new Uint8Array(32));
|
|
2346
|
-
const derivedKey = await deriveKeyFromPassword(password, salt);
|
|
2347
|
-
const nonce = crypto.getRandomValues(new Uint8Array(12));
|
|
2348
|
-
const vaultKeyBuffer = new Uint8Array(vaultKey);
|
|
2349
|
-
const encryptedBuffer = await crypto.subtle.encrypt(
|
|
2350
|
-
{
|
|
2351
|
-
name: ALGORITHM,
|
|
2352
|
-
iv: nonce
|
|
2353
|
-
},
|
|
2354
|
-
derivedKey,
|
|
2355
|
-
vaultKeyBuffer
|
|
2356
|
-
);
|
|
2357
|
-
return {
|
|
2358
|
-
encryptedVaultKey: arrayBufferToBase64(encryptedBuffer),
|
|
2359
|
-
salt: arrayBufferToBase64(salt),
|
|
2360
|
-
vaultKeyNonce: arrayBufferToBase64(nonce)
|
|
2361
|
-
};
|
|
2362
|
-
}
|
|
2363
|
-
async function decryptVaultKey(encryptedVaultKey, salt, vaultKeyNonce, password) {
|
|
2364
|
-
const encryptedBuffer = base64ToArrayBuffer(encryptedVaultKey);
|
|
2365
|
-
const saltBuffer = base64ToArrayBuffer(salt);
|
|
2366
|
-
const nonceBuffer = base64ToArrayBuffer(vaultKeyNonce);
|
|
2367
|
-
const derivedKey = await deriveKeyFromPassword(password, saltBuffer);
|
|
2368
|
-
const encryptedData = new Uint8Array(encryptedBuffer);
|
|
2369
|
-
const iv = new Uint8Array(nonceBuffer);
|
|
2370
|
-
const decryptedBuffer = await crypto.subtle.decrypt(
|
|
2371
|
-
{
|
|
2372
|
-
name: ALGORITHM,
|
|
2373
|
-
iv
|
|
2374
|
-
},
|
|
2375
|
-
derivedKey,
|
|
2376
|
-
encryptedData
|
|
2377
|
-
);
|
|
2378
|
-
return new Uint8Array(decryptedBuffer);
|
|
2379
|
-
}
|
|
2380
|
-
async function decryptVaultName(encryptedVaultName, vaultNameNonce, vaultNameSalt, password) {
|
|
2381
|
-
const saltBuffer = base64ToArrayBuffer(vaultNameSalt);
|
|
2382
|
-
const derivedKey = await deriveKeyFromPassword(password, saltBuffer);
|
|
2383
|
-
return decryptString(encryptedVaultName, vaultNameNonce, derivedKey);
|
|
2384
|
-
}
|
|
2385
|
-
async function encryptCrdtData(data, vaultKey) {
|
|
2386
|
-
const vaultKeyBuffer = new Uint8Array(vaultKey);
|
|
2387
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
2388
|
-
"raw",
|
|
2389
|
-
vaultKeyBuffer,
|
|
2390
|
-
{ name: ALGORITHM },
|
|
2391
|
-
false,
|
|
2392
|
-
["encrypt"]
|
|
2393
|
-
);
|
|
2394
|
-
const nonce = crypto.getRandomValues(new Uint8Array(12));
|
|
2395
|
-
const encoder = new TextEncoder();
|
|
2396
|
-
const dataBuffer = encoder.encode(JSON.stringify(data));
|
|
2397
|
-
const encryptedBuffer = await crypto.subtle.encrypt(
|
|
2398
|
-
{
|
|
2399
|
-
name: ALGORITHM,
|
|
2400
|
-
iv: nonce
|
|
2401
|
-
},
|
|
2402
|
-
cryptoKey,
|
|
2403
|
-
dataBuffer
|
|
2404
|
-
);
|
|
2405
|
-
return {
|
|
2406
|
-
encryptedData: arrayBufferToBase64(encryptedBuffer),
|
|
2407
|
-
nonce: arrayBufferToBase64(nonce)
|
|
2408
|
-
};
|
|
2409
|
-
}
|
|
2410
|
-
async function wrapKey(keyToWrap, wrappingKey) {
|
|
2411
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
2412
|
-
"raw",
|
|
2413
|
-
new Uint8Array(wrappingKey),
|
|
2414
|
-
{ name: ALGORITHM },
|
|
2415
|
-
false,
|
|
2416
|
-
["encrypt"]
|
|
2417
|
-
);
|
|
2418
|
-
const nonce = crypto.getRandomValues(new Uint8Array(12));
|
|
2419
|
-
const ciphertext = await crypto.subtle.encrypt(
|
|
2420
|
-
{ name: ALGORITHM, iv: nonce },
|
|
2421
|
-
cryptoKey,
|
|
2422
|
-
new Uint8Array(keyToWrap)
|
|
2423
|
-
);
|
|
2424
|
-
const result = new Uint8Array(12 + ciphertext.byteLength);
|
|
2425
|
-
result.set(nonce, 0);
|
|
2426
|
-
result.set(new Uint8Array(ciphertext), 12);
|
|
2427
|
-
return result;
|
|
2428
|
-
}
|
|
2429
|
-
async function unwrapKey(wrappedKey, wrappingKey) {
|
|
2430
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
2431
|
-
"raw",
|
|
2432
|
-
new Uint8Array(wrappingKey),
|
|
2433
|
-
{ name: ALGORITHM },
|
|
2434
|
-
false,
|
|
2435
|
-
["decrypt"]
|
|
2436
|
-
);
|
|
2437
|
-
const nonce = wrappedKey.slice(0, 12);
|
|
2438
|
-
const ciphertext = wrappedKey.slice(12);
|
|
2439
|
-
const plaintext = await crypto.subtle.decrypt(
|
|
2440
|
-
{ name: ALGORITHM, iv: nonce },
|
|
2441
|
-
cryptoKey,
|
|
2442
|
-
ciphertext
|
|
2443
|
-
);
|
|
2444
|
-
return new Uint8Array(plaintext);
|
|
2445
|
-
}
|
|
2446
|
-
async function decryptCrdtData(encryptedData, nonce, vaultKey) {
|
|
2447
|
-
const vaultKeyBuffer = new Uint8Array(vaultKey);
|
|
2448
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
2449
|
-
"raw",
|
|
2450
|
-
vaultKeyBuffer,
|
|
2451
|
-
{ name: ALGORITHM },
|
|
2452
|
-
false,
|
|
2453
|
-
["decrypt"]
|
|
2454
|
-
);
|
|
2455
|
-
const encryptedBuffer = base64ToArrayBuffer(encryptedData);
|
|
2456
|
-
const nonceBuffer = base64ToArrayBuffer(nonce);
|
|
2457
|
-
const encryptedDataBuffer = new Uint8Array(encryptedBuffer);
|
|
2458
|
-
const iv = new Uint8Array(nonceBuffer);
|
|
2459
|
-
const decryptedBuffer = await crypto.subtle.decrypt(
|
|
2460
|
-
{
|
|
2461
|
-
name: ALGORITHM,
|
|
2462
|
-
iv
|
|
2463
|
-
},
|
|
2464
|
-
cryptoKey,
|
|
2465
|
-
encryptedDataBuffer
|
|
2466
|
-
);
|
|
2467
|
-
const decoder = new TextDecoder();
|
|
2468
|
-
const jsonString = decoder.decode(decryptedBuffer);
|
|
2469
|
-
return JSON.parse(jsonString);
|
|
2470
|
-
}
|
|
2471
|
-
function arrayBufferToBase64(buffer) {
|
|
2472
|
-
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
2473
|
-
if (typeof Buffer !== "undefined") {
|
|
2474
|
-
return Buffer.from(bytes).toString("base64");
|
|
2475
|
-
}
|
|
2476
|
-
let binary = "";
|
|
2477
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
2478
|
-
const byte = bytes[i];
|
|
2479
|
-
if (byte !== void 0) {
|
|
2480
|
-
binary += String.fromCharCode(byte);
|
|
2481
|
-
}
|
|
2482
|
-
}
|
|
2483
|
-
return btoa(binary);
|
|
2484
|
-
}
|
|
2485
|
-
function base64ToArrayBuffer(base64) {
|
|
2486
|
-
if (typeof Buffer !== "undefined") {
|
|
2487
|
-
return new Uint8Array(Buffer.from(base64, "base64"));
|
|
2488
|
-
}
|
|
2489
|
-
const binary = atob(base64);
|
|
2490
|
-
const bytes = new Uint8Array(binary.length);
|
|
2491
|
-
for (let i = 0; i < binary.length; i++) {
|
|
2492
|
-
bytes[i] = binary.charCodeAt(i);
|
|
2493
|
-
}
|
|
2494
|
-
return bytes;
|
|
2495
|
-
}
|
|
2496
|
-
|
|
2497
2540
|
// src/index.ts
|
|
2498
2541
|
function createHaexVaultSdk(config = {}) {
|
|
2499
2542
|
return new HaexVaultSdk(config);
|