@haex-space/vault-sdk 2.3.10 → 2.3.12

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.
File without changes
package/dist/index.d.mts CHANGED
@@ -190,6 +190,67 @@ declare function hexToBytes(hex: string): ArrayBuffer;
190
190
  */
191
191
  declare function verifyExtensionSignature(files: ZipFileEntry[], manifest: ExtensionManifest): Promise<VerifyResult>;
192
192
 
193
+ /**
194
+ * Crypto utilities for Vault Key Management
195
+ * Implements PBKDF2 + AES-GCM encryption/decryption
196
+ *
197
+ * Used for:
198
+ * - Encrypting/decrypting vault names with server password
199
+ * - Encrypting/decrypting vault keys with vault password
200
+ * - Encrypting/decrypting CRDT data with vault key
201
+ */
202
+ /**
203
+ * Derives a cryptographic key from a password using PBKDF2
204
+ */
205
+ declare function deriveKeyFromPassword(password: string, salt: Uint8Array): Promise<CryptoKey>;
206
+ /**
207
+ * Generates a random vault key (32 bytes)
208
+ */
209
+ declare function generateVaultKey(): Uint8Array;
210
+ /**
211
+ * Encrypts a string (like vault name) with a password-derived key
212
+ * Returns: { encryptedData, nonce } as Base64 strings
213
+ */
214
+ declare function encryptString(data: string, derivedKey: CryptoKey): Promise<{
215
+ encryptedData: string;
216
+ nonce: string;
217
+ }>;
218
+ /**
219
+ * Decrypts a string (like vault name) with a password-derived key
220
+ */
221
+ declare function decryptString(encryptedData: string, nonce: string, derivedKey: CryptoKey): Promise<string>;
222
+ /**
223
+ * Encrypts the vault key with a password-derived key
224
+ * Returns: { encryptedVaultKey, salt, vaultKeyNonce } all as Base64 strings
225
+ */
226
+ declare function encryptVaultKey(vaultKey: Uint8Array, password: string): Promise<{
227
+ encryptedVaultKey: string;
228
+ salt: string;
229
+ vaultKeyNonce: string;
230
+ }>;
231
+ /**
232
+ * Decrypts the vault key using the password
233
+ */
234
+ declare function decryptVaultKey(encryptedVaultKey: string, salt: string, vaultKeyNonce: string, password: string): Promise<Uint8Array>;
235
+ /**
236
+ * Decrypts a vault name using the server password
237
+ * Convenience function that combines key derivation and decryption
238
+ */
239
+ declare function decryptVaultName(encryptedVaultName: string, vaultNameNonce: string, vaultNameSalt: string, password: string): Promise<string>;
240
+ /**
241
+ * Encrypts CRDT log data with the vault key
242
+ */
243
+ declare function encryptCrdtData(data: object, vaultKey: Uint8Array): Promise<{
244
+ encryptedData: string;
245
+ nonce: string;
246
+ }>;
247
+ /**
248
+ * Decrypts CRDT log data with the vault key
249
+ */
250
+ declare function decryptCrdtData<T = object>(encryptedData: string, nonce: string, vaultKey: Uint8Array): Promise<T>;
251
+ declare function arrayBufferToBase64(buffer: ArrayBuffer | Uint8Array): string;
252
+ declare function base64ToArrayBuffer(base64: string): Uint8Array;
253
+
193
254
  declare function createHaexVaultClient(config?: HaexHubConfig): HaexVaultClient;
194
255
 
195
- export { ExtensionManifest, HAEXSPACE_MESSAGE_TYPES, HAEXTENSION_METHODS, HaexHubConfig, HaexVaultClient, type HaexspaceMessageType, type HaextensionMethod, type VerifyResult, type ZipFileEntry, createHaexVaultClient, hexToBytes, installBaseTag, installCookiePolyfill, installHistoryPolyfill, installLocalStoragePolyfill, installPolyfills, installSessionStoragePolyfill, sortObjectKeysRecursively, verifyExtensionSignature };
256
+ export { ExtensionManifest, HAEXSPACE_MESSAGE_TYPES, HAEXTENSION_METHODS, HaexHubConfig, HaexVaultClient, type HaexspaceMessageType, type HaextensionMethod, type VerifyResult, type ZipFileEntry, arrayBufferToBase64, base64ToArrayBuffer, createHaexVaultClient, decryptCrdtData, decryptString, decryptVaultKey, decryptVaultName, deriveKeyFromPassword, encryptCrdtData, encryptString, encryptVaultKey, generateVaultKey, hexToBytes, installBaseTag, installCookiePolyfill, installHistoryPolyfill, installLocalStoragePolyfill, installPolyfills, installSessionStoragePolyfill, sortObjectKeysRecursively, verifyExtensionSignature };
package/dist/index.d.ts CHANGED
@@ -190,6 +190,67 @@ declare function hexToBytes(hex: string): ArrayBuffer;
190
190
  */
191
191
  declare function verifyExtensionSignature(files: ZipFileEntry[], manifest: ExtensionManifest): Promise<VerifyResult>;
192
192
 
193
+ /**
194
+ * Crypto utilities for Vault Key Management
195
+ * Implements PBKDF2 + AES-GCM encryption/decryption
196
+ *
197
+ * Used for:
198
+ * - Encrypting/decrypting vault names with server password
199
+ * - Encrypting/decrypting vault keys with vault password
200
+ * - Encrypting/decrypting CRDT data with vault key
201
+ */
202
+ /**
203
+ * Derives a cryptographic key from a password using PBKDF2
204
+ */
205
+ declare function deriveKeyFromPassword(password: string, salt: Uint8Array): Promise<CryptoKey>;
206
+ /**
207
+ * Generates a random vault key (32 bytes)
208
+ */
209
+ declare function generateVaultKey(): Uint8Array;
210
+ /**
211
+ * Encrypts a string (like vault name) with a password-derived key
212
+ * Returns: { encryptedData, nonce } as Base64 strings
213
+ */
214
+ declare function encryptString(data: string, derivedKey: CryptoKey): Promise<{
215
+ encryptedData: string;
216
+ nonce: string;
217
+ }>;
218
+ /**
219
+ * Decrypts a string (like vault name) with a password-derived key
220
+ */
221
+ declare function decryptString(encryptedData: string, nonce: string, derivedKey: CryptoKey): Promise<string>;
222
+ /**
223
+ * Encrypts the vault key with a password-derived key
224
+ * Returns: { encryptedVaultKey, salt, vaultKeyNonce } all as Base64 strings
225
+ */
226
+ declare function encryptVaultKey(vaultKey: Uint8Array, password: string): Promise<{
227
+ encryptedVaultKey: string;
228
+ salt: string;
229
+ vaultKeyNonce: string;
230
+ }>;
231
+ /**
232
+ * Decrypts the vault key using the password
233
+ */
234
+ declare function decryptVaultKey(encryptedVaultKey: string, salt: string, vaultKeyNonce: string, password: string): Promise<Uint8Array>;
235
+ /**
236
+ * Decrypts a vault name using the server password
237
+ * Convenience function that combines key derivation and decryption
238
+ */
239
+ declare function decryptVaultName(encryptedVaultName: string, vaultNameNonce: string, vaultNameSalt: string, password: string): Promise<string>;
240
+ /**
241
+ * Encrypts CRDT log data with the vault key
242
+ */
243
+ declare function encryptCrdtData(data: object, vaultKey: Uint8Array): Promise<{
244
+ encryptedData: string;
245
+ nonce: string;
246
+ }>;
247
+ /**
248
+ * Decrypts CRDT log data with the vault key
249
+ */
250
+ declare function decryptCrdtData<T = object>(encryptedData: string, nonce: string, vaultKey: Uint8Array): Promise<T>;
251
+ declare function arrayBufferToBase64(buffer: ArrayBuffer | Uint8Array): string;
252
+ declare function base64ToArrayBuffer(base64: string): Uint8Array;
253
+
193
254
  declare function createHaexVaultClient(config?: HaexHubConfig): HaexVaultClient;
194
255
 
195
- export { ExtensionManifest, HAEXSPACE_MESSAGE_TYPES, HAEXTENSION_METHODS, HaexHubConfig, HaexVaultClient, type HaexspaceMessageType, type HaextensionMethod, type VerifyResult, type ZipFileEntry, createHaexVaultClient, hexToBytes, installBaseTag, installCookiePolyfill, installHistoryPolyfill, installLocalStoragePolyfill, installPolyfills, installSessionStoragePolyfill, sortObjectKeysRecursively, verifyExtensionSignature };
256
+ export { ExtensionManifest, HAEXSPACE_MESSAGE_TYPES, HAEXTENSION_METHODS, HaexHubConfig, HaexVaultClient, type HaexspaceMessageType, type HaextensionMethod, type VerifyResult, type ZipFileEntry, arrayBufferToBase64, base64ToArrayBuffer, createHaexVaultClient, decryptCrdtData, decryptString, decryptVaultKey, decryptVaultName, deriveKeyFromPassword, encryptCrdtData, encryptString, encryptVaultKey, generateVaultKey, hexToBytes, installBaseTag, installCookiePolyfill, installHistoryPolyfill, installLocalStoragePolyfill, installPolyfills, installSessionStoragePolyfill, sortObjectKeysRecursively, verifyExtensionSignature };
package/dist/index.js CHANGED
@@ -1618,6 +1618,188 @@ async function verifyExtensionSignature(files, manifest) {
1618
1618
  }
1619
1619
  }
1620
1620
 
1621
+ // src/crypto/vaultKey.ts
1622
+ var PBKDF2_ITERATIONS = 6e5;
1623
+ var KEY_LENGTH = 256;
1624
+ var ALGORITHM = "AES-GCM";
1625
+ async function deriveKeyFromPassword(password, salt) {
1626
+ const encoder = new TextEncoder();
1627
+ const passwordBuffer = encoder.encode(password);
1628
+ const saltBuffer = new Uint8Array(salt);
1629
+ const keyMaterial = await crypto.subtle.importKey(
1630
+ "raw",
1631
+ passwordBuffer,
1632
+ "PBKDF2",
1633
+ false,
1634
+ ["deriveKey"]
1635
+ );
1636
+ return await crypto.subtle.deriveKey(
1637
+ {
1638
+ name: "PBKDF2",
1639
+ salt: saltBuffer,
1640
+ iterations: PBKDF2_ITERATIONS,
1641
+ hash: "SHA-256"
1642
+ },
1643
+ keyMaterial,
1644
+ { name: ALGORITHM, length: KEY_LENGTH },
1645
+ false,
1646
+ // not extractable
1647
+ ["encrypt", "decrypt"]
1648
+ );
1649
+ }
1650
+ function generateVaultKey() {
1651
+ return crypto.getRandomValues(new Uint8Array(32));
1652
+ }
1653
+ async function encryptString(data, derivedKey) {
1654
+ const nonce = crypto.getRandomValues(new Uint8Array(12));
1655
+ const encoder = new TextEncoder();
1656
+ const dataBuffer = encoder.encode(data);
1657
+ const encryptedBuffer = await crypto.subtle.encrypt(
1658
+ {
1659
+ name: ALGORITHM,
1660
+ iv: nonce
1661
+ },
1662
+ derivedKey,
1663
+ dataBuffer
1664
+ );
1665
+ return {
1666
+ encryptedData: arrayBufferToBase64(encryptedBuffer),
1667
+ nonce: arrayBufferToBase64(nonce)
1668
+ };
1669
+ }
1670
+ async function decryptString(encryptedData, nonce, derivedKey) {
1671
+ const encryptedBuffer = base64ToArrayBuffer(encryptedData);
1672
+ const nonceBuffer = base64ToArrayBuffer(nonce);
1673
+ const encryptedDataBuffer = new Uint8Array(encryptedBuffer);
1674
+ const iv = new Uint8Array(nonceBuffer);
1675
+ const decryptedBuffer = await crypto.subtle.decrypt(
1676
+ {
1677
+ name: ALGORITHM,
1678
+ iv
1679
+ },
1680
+ derivedKey,
1681
+ encryptedDataBuffer
1682
+ );
1683
+ const decoder = new TextDecoder();
1684
+ return decoder.decode(decryptedBuffer);
1685
+ }
1686
+ async function encryptVaultKey(vaultKey, password) {
1687
+ const salt = crypto.getRandomValues(new Uint8Array(32));
1688
+ const derivedKey = await deriveKeyFromPassword(password, salt);
1689
+ const nonce = crypto.getRandomValues(new Uint8Array(12));
1690
+ const vaultKeyBuffer = new Uint8Array(vaultKey);
1691
+ const encryptedBuffer = await crypto.subtle.encrypt(
1692
+ {
1693
+ name: ALGORITHM,
1694
+ iv: nonce
1695
+ },
1696
+ derivedKey,
1697
+ vaultKeyBuffer
1698
+ );
1699
+ return {
1700
+ encryptedVaultKey: arrayBufferToBase64(encryptedBuffer),
1701
+ salt: arrayBufferToBase64(salt),
1702
+ vaultKeyNonce: arrayBufferToBase64(nonce)
1703
+ };
1704
+ }
1705
+ async function decryptVaultKey(encryptedVaultKey, salt, vaultKeyNonce, password) {
1706
+ const encryptedBuffer = base64ToArrayBuffer(encryptedVaultKey);
1707
+ const saltBuffer = base64ToArrayBuffer(salt);
1708
+ const nonceBuffer = base64ToArrayBuffer(vaultKeyNonce);
1709
+ const derivedKey = await deriveKeyFromPassword(password, saltBuffer);
1710
+ const encryptedData = new Uint8Array(encryptedBuffer);
1711
+ const iv = new Uint8Array(nonceBuffer);
1712
+ const decryptedBuffer = await crypto.subtle.decrypt(
1713
+ {
1714
+ name: ALGORITHM,
1715
+ iv
1716
+ },
1717
+ derivedKey,
1718
+ encryptedData
1719
+ );
1720
+ return new Uint8Array(decryptedBuffer);
1721
+ }
1722
+ async function decryptVaultName(encryptedVaultName, vaultNameNonce, vaultNameSalt, password) {
1723
+ const saltBuffer = base64ToArrayBuffer(vaultNameSalt);
1724
+ const derivedKey = await deriveKeyFromPassword(password, saltBuffer);
1725
+ return decryptString(encryptedVaultName, vaultNameNonce, derivedKey);
1726
+ }
1727
+ async function encryptCrdtData(data, vaultKey) {
1728
+ const vaultKeyBuffer = new Uint8Array(vaultKey);
1729
+ const cryptoKey = await crypto.subtle.importKey(
1730
+ "raw",
1731
+ vaultKeyBuffer,
1732
+ { name: ALGORITHM },
1733
+ false,
1734
+ ["encrypt"]
1735
+ );
1736
+ const nonce = crypto.getRandomValues(new Uint8Array(12));
1737
+ const encoder = new TextEncoder();
1738
+ const dataBuffer = encoder.encode(JSON.stringify(data));
1739
+ const encryptedBuffer = await crypto.subtle.encrypt(
1740
+ {
1741
+ name: ALGORITHM,
1742
+ iv: nonce
1743
+ },
1744
+ cryptoKey,
1745
+ dataBuffer
1746
+ );
1747
+ return {
1748
+ encryptedData: arrayBufferToBase64(encryptedBuffer),
1749
+ nonce: arrayBufferToBase64(nonce)
1750
+ };
1751
+ }
1752
+ async function decryptCrdtData(encryptedData, nonce, vaultKey) {
1753
+ const vaultKeyBuffer = new Uint8Array(vaultKey);
1754
+ const cryptoKey = await crypto.subtle.importKey(
1755
+ "raw",
1756
+ vaultKeyBuffer,
1757
+ { name: ALGORITHM },
1758
+ false,
1759
+ ["decrypt"]
1760
+ );
1761
+ const encryptedBuffer = base64ToArrayBuffer(encryptedData);
1762
+ const nonceBuffer = base64ToArrayBuffer(nonce);
1763
+ const encryptedDataBuffer = new Uint8Array(encryptedBuffer);
1764
+ const iv = new Uint8Array(nonceBuffer);
1765
+ const decryptedBuffer = await crypto.subtle.decrypt(
1766
+ {
1767
+ name: ALGORITHM,
1768
+ iv
1769
+ },
1770
+ cryptoKey,
1771
+ encryptedDataBuffer
1772
+ );
1773
+ const decoder = new TextDecoder();
1774
+ const jsonString = decoder.decode(decryptedBuffer);
1775
+ return JSON.parse(jsonString);
1776
+ }
1777
+ function arrayBufferToBase64(buffer) {
1778
+ const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
1779
+ if (typeof Buffer !== "undefined") {
1780
+ return Buffer.from(bytes).toString("base64");
1781
+ }
1782
+ let binary = "";
1783
+ for (let i = 0; i < bytes.length; i++) {
1784
+ const byte = bytes[i];
1785
+ if (byte !== void 0) {
1786
+ binary += String.fromCharCode(byte);
1787
+ }
1788
+ }
1789
+ return btoa(binary);
1790
+ }
1791
+ function base64ToArrayBuffer(base64) {
1792
+ if (typeof Buffer !== "undefined") {
1793
+ return new Uint8Array(Buffer.from(base64, "base64"));
1794
+ }
1795
+ const binary = atob(base64);
1796
+ const bytes = new Uint8Array(binary.length);
1797
+ for (let i = 0; i < binary.length; i++) {
1798
+ bytes[i] = binary.charCodeAt(i);
1799
+ }
1800
+ return bytes;
1801
+ }
1802
+
1621
1803
  // src/index.ts
1622
1804
  function createHaexVaultClient(config = {}) {
1623
1805
  return new HaexVaultClient(config);
@@ -1636,7 +1818,18 @@ exports.PermissionStatus = PermissionStatus;
1636
1818
  exports.PermissionsAPI = PermissionsAPI;
1637
1819
  exports.TABLE_SEPARATOR = TABLE_SEPARATOR;
1638
1820
  exports.WebAPI = WebAPI;
1821
+ exports.arrayBufferToBase64 = arrayBufferToBase64;
1822
+ exports.base64ToArrayBuffer = base64ToArrayBuffer;
1639
1823
  exports.createHaexVaultClient = createHaexVaultClient;
1824
+ exports.decryptCrdtData = decryptCrdtData;
1825
+ exports.decryptString = decryptString;
1826
+ exports.decryptVaultKey = decryptVaultKey;
1827
+ exports.decryptVaultName = decryptVaultName;
1828
+ exports.deriveKeyFromPassword = deriveKeyFromPassword;
1829
+ exports.encryptCrdtData = encryptCrdtData;
1830
+ exports.encryptString = encryptString;
1831
+ exports.encryptVaultKey = encryptVaultKey;
1832
+ exports.generateVaultKey = generateVaultKey;
1640
1833
  exports.getTableName = getTableName;
1641
1834
  exports.hexToBytes = hexToBytes;
1642
1835
  exports.installBaseTag = installBaseTag;