@haex-space/vault-sdk 2.3.10 → 2.3.14

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
@@ -1616,11 +1616,193 @@ async function verifyExtensionSignature(files, manifest) {
1616
1616
  }
1617
1617
  }
1618
1618
 
1619
+ // src/crypto/vaultKey.ts
1620
+ var PBKDF2_ITERATIONS = 6e5;
1621
+ var KEY_LENGTH = 256;
1622
+ var ALGORITHM = "AES-GCM";
1623
+ async function deriveKeyFromPassword(password, salt) {
1624
+ const encoder = new TextEncoder();
1625
+ const passwordBuffer = encoder.encode(password);
1626
+ const saltBuffer = new Uint8Array(salt);
1627
+ const keyMaterial = await crypto.subtle.importKey(
1628
+ "raw",
1629
+ passwordBuffer,
1630
+ "PBKDF2",
1631
+ false,
1632
+ ["deriveKey"]
1633
+ );
1634
+ return await crypto.subtle.deriveKey(
1635
+ {
1636
+ name: "PBKDF2",
1637
+ salt: saltBuffer,
1638
+ iterations: PBKDF2_ITERATIONS,
1639
+ hash: "SHA-256"
1640
+ },
1641
+ keyMaterial,
1642
+ { name: ALGORITHM, length: KEY_LENGTH },
1643
+ false,
1644
+ // not extractable
1645
+ ["encrypt", "decrypt"]
1646
+ );
1647
+ }
1648
+ function generateVaultKey() {
1649
+ return crypto.getRandomValues(new Uint8Array(32));
1650
+ }
1651
+ async function encryptString(data, derivedKey) {
1652
+ const nonce = crypto.getRandomValues(new Uint8Array(12));
1653
+ const encoder = new TextEncoder();
1654
+ const dataBuffer = encoder.encode(data);
1655
+ const encryptedBuffer = await crypto.subtle.encrypt(
1656
+ {
1657
+ name: ALGORITHM,
1658
+ iv: nonce
1659
+ },
1660
+ derivedKey,
1661
+ dataBuffer
1662
+ );
1663
+ return {
1664
+ encryptedData: arrayBufferToBase64(encryptedBuffer),
1665
+ nonce: arrayBufferToBase64(nonce)
1666
+ };
1667
+ }
1668
+ async function decryptString(encryptedData, nonce, derivedKey) {
1669
+ const encryptedBuffer = base64ToArrayBuffer(encryptedData);
1670
+ const nonceBuffer = base64ToArrayBuffer(nonce);
1671
+ const encryptedDataBuffer = new Uint8Array(encryptedBuffer);
1672
+ const iv = new Uint8Array(nonceBuffer);
1673
+ const decryptedBuffer = await crypto.subtle.decrypt(
1674
+ {
1675
+ name: ALGORITHM,
1676
+ iv
1677
+ },
1678
+ derivedKey,
1679
+ encryptedDataBuffer
1680
+ );
1681
+ const decoder = new TextDecoder();
1682
+ return decoder.decode(decryptedBuffer);
1683
+ }
1684
+ async function encryptVaultKey(vaultKey, password) {
1685
+ const salt = crypto.getRandomValues(new Uint8Array(32));
1686
+ const derivedKey = await deriveKeyFromPassword(password, salt);
1687
+ const nonce = crypto.getRandomValues(new Uint8Array(12));
1688
+ const vaultKeyBuffer = new Uint8Array(vaultKey);
1689
+ const encryptedBuffer = await crypto.subtle.encrypt(
1690
+ {
1691
+ name: ALGORITHM,
1692
+ iv: nonce
1693
+ },
1694
+ derivedKey,
1695
+ vaultKeyBuffer
1696
+ );
1697
+ return {
1698
+ encryptedVaultKey: arrayBufferToBase64(encryptedBuffer),
1699
+ salt: arrayBufferToBase64(salt),
1700
+ vaultKeyNonce: arrayBufferToBase64(nonce)
1701
+ };
1702
+ }
1703
+ async function decryptVaultKey(encryptedVaultKey, salt, vaultKeyNonce, password) {
1704
+ const encryptedBuffer = base64ToArrayBuffer(encryptedVaultKey);
1705
+ const saltBuffer = base64ToArrayBuffer(salt);
1706
+ const nonceBuffer = base64ToArrayBuffer(vaultKeyNonce);
1707
+ const derivedKey = await deriveKeyFromPassword(password, saltBuffer);
1708
+ const encryptedData = new Uint8Array(encryptedBuffer);
1709
+ const iv = new Uint8Array(nonceBuffer);
1710
+ const decryptedBuffer = await crypto.subtle.decrypt(
1711
+ {
1712
+ name: ALGORITHM,
1713
+ iv
1714
+ },
1715
+ derivedKey,
1716
+ encryptedData
1717
+ );
1718
+ return new Uint8Array(decryptedBuffer);
1719
+ }
1720
+ async function decryptVaultName(encryptedVaultName, vaultNameNonce, vaultNameSalt, password) {
1721
+ const saltBuffer = base64ToArrayBuffer(vaultNameSalt);
1722
+ const derivedKey = await deriveKeyFromPassword(password, saltBuffer);
1723
+ return decryptString(encryptedVaultName, vaultNameNonce, derivedKey);
1724
+ }
1725
+ async function encryptCrdtData(data, vaultKey) {
1726
+ const vaultKeyBuffer = new Uint8Array(vaultKey);
1727
+ const cryptoKey = await crypto.subtle.importKey(
1728
+ "raw",
1729
+ vaultKeyBuffer,
1730
+ { name: ALGORITHM },
1731
+ false,
1732
+ ["encrypt"]
1733
+ );
1734
+ const nonce = crypto.getRandomValues(new Uint8Array(12));
1735
+ const encoder = new TextEncoder();
1736
+ const dataBuffer = encoder.encode(JSON.stringify(data));
1737
+ const encryptedBuffer = await crypto.subtle.encrypt(
1738
+ {
1739
+ name: ALGORITHM,
1740
+ iv: nonce
1741
+ },
1742
+ cryptoKey,
1743
+ dataBuffer
1744
+ );
1745
+ return {
1746
+ encryptedData: arrayBufferToBase64(encryptedBuffer),
1747
+ nonce: arrayBufferToBase64(nonce)
1748
+ };
1749
+ }
1750
+ async function decryptCrdtData(encryptedData, nonce, vaultKey) {
1751
+ const vaultKeyBuffer = new Uint8Array(vaultKey);
1752
+ const cryptoKey = await crypto.subtle.importKey(
1753
+ "raw",
1754
+ vaultKeyBuffer,
1755
+ { name: ALGORITHM },
1756
+ false,
1757
+ ["decrypt"]
1758
+ );
1759
+ const encryptedBuffer = base64ToArrayBuffer(encryptedData);
1760
+ const nonceBuffer = base64ToArrayBuffer(nonce);
1761
+ const encryptedDataBuffer = new Uint8Array(encryptedBuffer);
1762
+ const iv = new Uint8Array(nonceBuffer);
1763
+ const decryptedBuffer = await crypto.subtle.decrypt(
1764
+ {
1765
+ name: ALGORITHM,
1766
+ iv
1767
+ },
1768
+ cryptoKey,
1769
+ encryptedDataBuffer
1770
+ );
1771
+ const decoder = new TextDecoder();
1772
+ const jsonString = decoder.decode(decryptedBuffer);
1773
+ return JSON.parse(jsonString);
1774
+ }
1775
+ function arrayBufferToBase64(buffer) {
1776
+ const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
1777
+ if (typeof Buffer !== "undefined") {
1778
+ return Buffer.from(bytes).toString("base64");
1779
+ }
1780
+ let binary = "";
1781
+ for (let i = 0; i < bytes.length; i++) {
1782
+ const byte = bytes[i];
1783
+ if (byte !== void 0) {
1784
+ binary += String.fromCharCode(byte);
1785
+ }
1786
+ }
1787
+ return btoa(binary);
1788
+ }
1789
+ function base64ToArrayBuffer(base64) {
1790
+ if (typeof Buffer !== "undefined") {
1791
+ return new Uint8Array(Buffer.from(base64, "base64"));
1792
+ }
1793
+ const binary = atob(base64);
1794
+ const bytes = new Uint8Array(binary.length);
1795
+ for (let i = 0; i < binary.length; i++) {
1796
+ bytes[i] = binary.charCodeAt(i);
1797
+ }
1798
+ return bytes;
1799
+ }
1800
+
1619
1801
  // src/index.ts
1620
1802
  function createHaexVaultClient(config = {}) {
1621
1803
  return new HaexVaultClient(config);
1622
1804
  }
1623
1805
 
1624
- export { DEFAULT_TIMEOUT, DatabaseAPI, ErrorCode, FilesystemAPI, HAEXSPACE_MESSAGE_TYPES, HAEXTENSION_EVENTS, HAEXTENSION_METHODS, HaexHubError, HaexVaultClient, PermissionStatus, PermissionsAPI, TABLE_SEPARATOR, WebAPI, createHaexVaultClient, getTableName, hexToBytes, installBaseTag, installCookiePolyfill, installHistoryPolyfill, installLocalStoragePolyfill, installPolyfills, installSessionStoragePolyfill, sortObjectKeysRecursively, verifyExtensionSignature };
1806
+ export { DEFAULT_TIMEOUT, DatabaseAPI, ErrorCode, FilesystemAPI, HAEXSPACE_MESSAGE_TYPES, HAEXTENSION_EVENTS, HAEXTENSION_METHODS, HaexHubError, HaexVaultClient, PermissionStatus, PermissionsAPI, TABLE_SEPARATOR, WebAPI, arrayBufferToBase64, base64ToArrayBuffer, createHaexVaultClient, decryptCrdtData, decryptString, decryptVaultKey, decryptVaultName, deriveKeyFromPassword, encryptCrdtData, encryptString, encryptVaultKey, generateVaultKey, getTableName, hexToBytes, installBaseTag, installCookiePolyfill, installHistoryPolyfill, installLocalStoragePolyfill, installPolyfills, installSessionStoragePolyfill, sortObjectKeysRecursively, verifyExtensionSignature };
1625
1807
  //# sourceMappingURL=index.mjs.map
1626
1808
  //# sourceMappingURL=index.mjs.map