@opendatalabs/vana-sdk 2.2.1 → 2.2.3
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/config/features.cjs +1 -24
- package/dist/config/features.cjs.map +1 -1
- package/dist/config/features.d.ts +13 -44
- package/dist/config/features.js +1 -24
- package/dist/config/features.js.map +1 -1
- package/dist/controllers/permissions.cjs +19 -14
- package/dist/controllers/permissions.cjs.map +1 -1
- package/dist/controllers/permissions.js +19 -14
- package/dist/controllers/permissions.js.map +1 -1
- package/dist/crypto/ecies/index.cjs.map +1 -1
- package/dist/crypto/ecies/index.d.ts +10 -2
- package/dist/crypto/ecies/index.js.map +1 -1
- package/dist/index.browser.d.ts +2 -0
- package/dist/index.browser.js +10 -0
- package/dist/index.browser.js.map +1 -1
- package/dist/index.node.cjs +12 -0
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.d.ts +3 -0
- package/dist/index.node.js +12 -0
- package/dist/index.node.js.map +1 -1
- package/dist/platform/browser.cjs +40 -119
- package/dist/platform/browser.cjs.map +1 -1
- package/dist/platform/browser.d.ts +7 -7
- package/dist/platform/browser.js +40 -119
- package/dist/platform/browser.js.map +1 -1
- package/dist/platform/node.cjs +51 -129
- package/dist/platform/node.cjs.map +1 -1
- package/dist/platform/node.d.ts +5 -5
- package/dist/platform/node.js +51 -129
- package/dist/platform/node.js.map +1 -1
- package/dist/types/permissions.cjs.map +1 -1
- package/dist/types/permissions.d.ts +2 -0
- package/package.json +1 -2
- package/dist/types/eccrypto-js.d.cjs +0 -2
- package/dist/types/eccrypto-js.d.cjs.map +0 -1
- package/dist/types/eccrypto-js.d.js +0 -1
- package/dist/types/eccrypto-js.d.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/platform/browser.ts"],"sourcesContent":["/**\n * Provides browser-specific implementations of platform abstraction interfaces.\n *\n * @remarks\n * This module implements all platform-specific operations for browser environments,\n * using native Web APIs and browser-compatible libraries. It avoids Node.js-specific\n * dependencies like Buffer, using Uint8Array and viem utilities instead. Supports\n * both standard eccrypto and custom ECIES implementations based on feature flags.\n *\n * @example\n * ```typescript\n * // Use the browser platform adapter\n * import { BrowserPlatformAdapter } from '@vana-sdk/platform/browser';\n *\n * const adapter = new BrowserPlatformAdapter();\n *\n * // Encrypt data with public key\n * const encrypted = await adapter.crypto.encryptWithPublicKey(\n * 'sensitive data',\n * '0x04...' // Public key hex\n * );\n *\n * // Use sessionStorage-backed cache\n * adapter.cache.set('temp_key', 'cached_value');\n * ```\n *\n * @category Platform\n * @module platform/browser\n */\n\nimport type {\n VanaPlatformAdapter,\n VanaCryptoAdapter,\n VanaPGPAdapter,\n VanaHttpAdapter,\n VanaCacheAdapter,\n} from \"./interface\";\nimport { getPGPKeyGenParams } from \"./shared/pgp-utils\";\nimport { wrapCryptoError } from \"./shared/error-utils\";\nimport { lazyImport } from \"../utils/lazy-import\";\nimport { WalletKeyEncryptionService } from \"../crypto/services/WalletKeyEncryptionService\";\nimport { parseEncryptedDataBuffer } from \"../utils/crypto-utils\";\nimport { toHex, fromHex, stringToBytes, bytesToString, concat } from \"viem\";\nimport * as secp256k1 from \"@noble/secp256k1\";\nimport { features } from \"../config/features\";\n\n// Import browser ECIES provider\nimport { BrowserECIESUint8Provider } from \"../crypto/ecies/browser\";\n\n// Lazy-loaded dependencies to avoid Turbopack TDZ issues\nconst getOpenPGP = lazyImport(() => import(\"openpgp\"));\nconst getEccryptoJS = lazyImport(() => import(\"eccrypto-js\"));\n\n/**\n * Implements cryptographic operations for browser environments.\n *\n * @remarks\n * Provides ECIES encryption/decryption, key generation, and password-based\n * encryption using either eccrypto-js or a custom ECIES implementation.\n * Uses Uint8Array and viem utilities for browser compatibility.\n *\n * @internal\n */\nclass BrowserCryptoAdapter implements VanaCryptoAdapter {\n // Initialize both providers - only one will be used based on feature flag\n private customEciesProvider = new BrowserECIESUint8Provider();\n private customWalletService = new WalletKeyEncryptionService({\n eciesProvider: this.customEciesProvider,\n });\n\n /**\n * Encrypts data using ECIES with a public key.\n *\n * @param data - The plaintext string to encrypt.\n * Typically user data or sensitive information.\n * @param publicKeyHex - The recipient's public key in hex format.\n * Can include or omit the '0x' prefix.\n * @returns Encrypted data as a hex string without '0x' prefix\n *\n * @throws {Error} If encryption fails or public key is invalid\n */\n async encryptWithPublicKey(\n data: string,\n publicKeyHex: string,\n ): Promise<string> {\n try {\n if (features.useCustomECIES) {\n // Use custom ECIES implementation\n const prefixedHex = publicKeyHex.startsWith(\"0x\")\n ? publicKeyHex\n : `0x${publicKeyHex}`;\n const publicKeyBytes = fromHex(prefixedHex as `0x${string}`, \"bytes\");\n\n // Encrypt data using ECIES\n const encrypted = await this.customEciesProvider.encrypt(\n publicKeyBytes,\n stringToBytes(data),\n );\n\n // Concatenate all components and return as hex string\n const result = concat([\n encrypted.iv,\n encrypted.ephemPublicKey,\n encrypted.ciphertext,\n encrypted.mac,\n ]);\n\n return toHex(result).slice(2); // Remove '0x' prefix for backward compatibility\n } else {\n // Use eccrypto-js (default)\n const eccryptojs = await getEccryptoJS();\n\n // Remove 0x prefix if present\n const cleanKey = publicKeyHex.startsWith(\"0x\")\n ? publicKeyHex.slice(2)\n : publicKeyHex;\n const publicKeyBytes = Buffer.from(cleanKey, \"hex\");\n\n // Normalize to uncompressed format using the ECIES provider\n // This handles both compressed (33 bytes) and uncompressed (65 bytes) keys\n const uncompressed = this.customEciesProvider.normalizeToUncompressed(\n new Uint8Array(publicKeyBytes),\n );\n const publicKeyBuffer = Buffer.from(uncompressed);\n\n const message = Buffer.from(data, \"utf8\");\n\n const encrypted = await eccryptojs.encrypt(publicKeyBuffer, message);\n\n // Concatenate all components and return as hex string\n const result = Buffer.concat([\n encrypted.iv,\n encrypted.ephemPublicKey,\n encrypted.ciphertext,\n encrypted.mac,\n ]);\n\n return result.toString(\"hex\");\n }\n } catch (error) {\n throw wrapCryptoError(\"encryptWithPublicKey\", error);\n }\n }\n\n /**\n * Decrypts ECIES-encrypted data using a private key.\n *\n * @param encryptedData - Hex string containing encrypted data.\n * Can include or omit the '0x' prefix.\n * @param privateKeyHex - The private key in hex format.\n * Must correspond to the public key used for encryption.\n * @returns The decrypted plaintext string\n *\n * @throws {Error} If decryption fails or MAC verification fails\n */\n async decryptWithPrivateKey(\n encryptedData: string,\n privateKeyHex: string,\n ): Promise<string> {\n try {\n if (features.useCustomECIES) {\n // Use custom ECIES implementation\n const encryptedHex = encryptedData.startsWith(\"0x\")\n ? encryptedData\n : `0x${encryptedData}`;\n const privateHex = privateKeyHex.startsWith(\"0x\")\n ? privateKeyHex\n : `0x${privateKeyHex}`;\n const encryptedBytes = fromHex(encryptedHex as `0x${string}`, \"bytes\");\n const privateKeyBytes = fromHex(privateHex as `0x${string}`, \"bytes\");\n\n // Parse the encrypted data into components\n const encrypted = parseEncryptedDataBuffer(encryptedBytes);\n\n // Decrypt using ECIES\n const decrypted = await this.customEciesProvider.decrypt(\n privateKeyBytes,\n encrypted,\n );\n\n return bytesToString(decrypted);\n } else {\n // Use eccrypto-js (default)\n const eccryptojs = await getEccryptoJS();\n const privateKey = Buffer.from(privateKeyHex, \"hex\");\n const encryptedBuffer = Buffer.from(encryptedData, \"hex\");\n\n // Parse the encrypted data\n const { iv, ephemPublicKey, ciphertext, mac } =\n parseEncryptedDataBuffer(encryptedBuffer);\n\n const decrypted = await eccryptojs.decrypt(privateKey, {\n iv: Buffer.from(iv),\n ephemPublicKey: Buffer.from(ephemPublicKey),\n ciphertext: Buffer.from(ciphertext),\n mac: Buffer.from(mac),\n });\n\n return decrypted.toString(\"utf8\");\n }\n } catch (error) {\n throw wrapCryptoError(\"decryptWithPrivateKey\", error);\n }\n }\n\n /**\n * Encrypts data using a wallet's public key.\n *\n * @param data - The plaintext string to encrypt.\n * Typically permission data or DLP metadata.\n * @param publicKey - The wallet's public key.\n * Automatically handles compressed/uncompressed formats.\n * @returns Encrypted data as a hex string\n *\n * @throws {Error} If encryption fails or key processing fails\n */\n async encryptWithWalletPublicKey(\n data: string,\n publicKey: string,\n ): Promise<string> {\n try {\n if (features.useCustomECIES) {\n // Use custom ECIES implementation via WalletKeyEncryptionService\n return await this.customWalletService.encryptWithWalletPublicKey(\n data,\n publicKey,\n );\n } else {\n // Use eccrypto-js directly for wallet encryption\n const eccryptojs = await getEccryptoJS();\n\n // Remove 0x prefix if present\n const cleanKey = publicKey.startsWith(\"0x\")\n ? publicKey.slice(2)\n : publicKey;\n const publicKeyBytes = Buffer.from(cleanKey, \"hex\");\n\n // Normalize to uncompressed format using the ECIES provider\n // This handles both compressed (33 bytes) and uncompressed (65 bytes) keys\n const uncompressed = this.customEciesProvider.normalizeToUncompressed(\n new Uint8Array(publicKeyBytes),\n );\n const publicKeyBuffer = Buffer.from(uncompressed);\n\n const message = Buffer.from(data, \"utf8\");\n\n const encrypted = await eccryptojs.encrypt(publicKeyBuffer, message);\n\n // Concatenate all components and return as hex string\n const result = Buffer.concat([\n encrypted.iv,\n encrypted.ephemPublicKey,\n encrypted.ciphertext,\n encrypted.mac,\n ]);\n\n return result.toString(\"hex\");\n }\n } catch (error) {\n throw wrapCryptoError(\"encryptWithWalletPublicKey\", error);\n }\n }\n\n /**\n * Decrypts data using a wallet's private key.\n *\n * @param encryptedData - Hex string containing encrypted data.\n * Must be encrypted with corresponding wallet public key.\n * @param privateKey - The wallet's private key.\n * Obtain from wallet connection (handle with care).\n * @returns The decrypted plaintext string\n *\n * @throws {Error} If decryption fails or key is invalid\n */\n async decryptWithWalletPrivateKey(\n encryptedData: string,\n privateKey: string,\n ): Promise<string> {\n try {\n if (features.useCustomECIES) {\n // Use custom ECIES implementation via WalletKeyEncryptionService\n return await this.customWalletService.decryptWithWalletPrivateKey(\n encryptedData,\n privateKey,\n );\n } else {\n // Use eccrypto-js directly for wallet decryption\n const eccryptojs = await getEccryptoJS();\n const privateKeyBuffer = Buffer.from(privateKey, \"hex\");\n const encryptedBuffer = Buffer.from(encryptedData, \"hex\");\n\n // Parse the encrypted data\n const { iv, ephemPublicKey, ciphertext, mac } =\n parseEncryptedDataBuffer(encryptedBuffer);\n\n const decrypted = await eccryptojs.decrypt(privateKeyBuffer, {\n iv: Buffer.from(iv),\n ephemPublicKey: Buffer.from(ephemPublicKey),\n ciphertext: Buffer.from(ciphertext),\n mac: Buffer.from(mac),\n });\n\n return decrypted.toString(\"utf8\");\n }\n } catch (error) {\n throw wrapCryptoError(\"decryptWithWalletPrivateKey\", error);\n }\n }\n\n /**\n * Generates a new secp256k1 key pair for ECIES operations.\n *\n * @returns Object containing hex-encoded public and private keys\n * @returns returns.privateKey - Private key in hex format without '0x'\n * @returns returns.publicKey - Compressed public key in hex format without '0x'\n *\n * @throws {Error} If key generation fails\n */\n async generateKeyPair(): Promise<{\n privateKey: string;\n publicKey: string;\n }> {\n try {\n if (features.useCustomECIES) {\n // Use custom implementation with @noble/secp256k1\n // Generate random private key\n const privateKeyBytes = secp256k1.utils.randomPrivateKey();\n\n // Generate public key (compressed for consistency with Node implementation)\n const publicKeyBytes = secp256k1.getPublicKey(privateKeyBytes, true);\n\n return {\n privateKey: toHex(privateKeyBytes).slice(2),\n publicKey: toHex(publicKeyBytes).slice(2),\n };\n } else {\n // Use eccrypto-js (default)\n const eccryptojs = await getEccryptoJS();\n const privateKey = eccryptojs.generatePrivate();\n const publicKey = eccryptojs.getPublic(privateKey);\n\n return {\n privateKey: privateKey.toString(\"hex\"),\n publicKey: publicKey.toString(\"hex\"),\n };\n }\n } catch (error) {\n throw wrapCryptoError(\"generateKeyPair\", error);\n }\n }\n\n /**\n * Encrypts binary data using password-based encryption.\n *\n * @param data - Binary data to encrypt.\n * Typically file contents or serialized objects.\n * @param password - Password for encryption.\n * Often derived from wallet signatures.\n * @returns Encrypted data as Uint8Array\n *\n * @remarks\n * Uses OpenPGP for password-based encryption with automatic\n * salt generation for security.\n *\n * @throws {Error} If encryption fails\n */\n async encryptWithPassword(\n data: Uint8Array,\n password: string,\n ): Promise<Uint8Array> {\n try {\n const openpgp = await getOpenPGP();\n\n // Create a message from the data\n const message = await openpgp.createMessage({ binary: data });\n\n // Encrypt with password\n const encrypted = await openpgp.encrypt({\n message,\n passwords: [password],\n format: \"binary\",\n });\n\n return new Uint8Array(encrypted as ArrayBuffer);\n } catch (error) {\n throw wrapCryptoError(\"encryptWithPassword\", error);\n }\n }\n\n /**\n * Decrypts password-encrypted binary data.\n *\n * @param encryptedData - Password-encrypted data as Uint8Array.\n * Must be encrypted with the same password.\n * @param password - Password for decryption.\n * Must match the encryption password.\n * @returns Decrypted data as Uint8Array\n *\n * @throws {Error} If decryption fails or password is incorrect\n */\n async decryptWithPassword(\n encryptedData: Uint8Array,\n password: string,\n ): Promise<Uint8Array> {\n try {\n const openpgp = await getOpenPGP();\n\n // Read the encrypted message\n const message = await openpgp.readMessage({\n binaryMessage: encryptedData,\n });\n\n // Decrypt with password\n const { data } = await openpgp.decrypt({\n message,\n passwords: [password],\n format: \"binary\",\n });\n\n return new Uint8Array(data as ArrayBuffer);\n } catch (error) {\n throw wrapCryptoError(\"decryptWithPassword\", error);\n }\n }\n}\n\n/**\n * Implements PGP operations for browser environments.\n *\n * @remarks\n * Provides PGP encryption, decryption, and key generation using the OpenPGP.js\n * library with browser-optimized configuration.\n *\n * @internal\n */\nclass BrowserPGPAdapter implements VanaPGPAdapter {\n /**\n * Encrypts data using PGP public key encryption.\n *\n * @param data - The plaintext string to encrypt.\n * Typically messages or structured data.\n * @param publicKeyArmored - ASCII-armored PGP public key.\n * Obtain from PGP key generation or key servers.\n * @returns ASCII-armored encrypted message\n *\n * @throws {Error} If encryption fails or public key is invalid\n */\n async encrypt(data: string, publicKeyArmored: string): Promise<string> {\n try {\n const openpgp = await getOpenPGP();\n const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });\n\n const encrypted = await openpgp.encrypt({\n message: await openpgp.createMessage({ text: data }),\n encryptionKeys: publicKey,\n config: {\n preferredCompressionAlgorithm: openpgp.enums.compression.zlib,\n },\n });\n\n return encrypted as string;\n } catch (error) {\n throw new Error(`PGP encryption failed: ${String(error)}`);\n }\n }\n\n /**\n * Decrypts PGP-encrypted data using a private key.\n *\n * @param encryptedData - ASCII-armored encrypted message.\n * Must be encrypted with corresponding public key.\n * @param privateKeyArmored - ASCII-armored PGP private key.\n * Must correspond to the public key used for encryption.\n * @returns The decrypted plaintext string\n *\n * @throws {Error} If decryption fails or private key is invalid\n */\n async decrypt(\n encryptedData: string,\n privateKeyArmored: string,\n ): Promise<string> {\n try {\n const openpgp = await getOpenPGP();\n const privateKey = await openpgp.readPrivateKey({\n armoredKey: privateKeyArmored,\n });\n const message = await openpgp.readMessage({\n armoredMessage: encryptedData,\n });\n\n const { data: decrypted } = await openpgp.decrypt({\n message,\n decryptionKeys: privateKey,\n });\n\n return decrypted as string;\n } catch (error) {\n throw new Error(`PGP decryption failed: ${String(error)}`);\n }\n }\n\n /**\n * Generates a new PGP key pair.\n *\n * @param options - Key generation options\n * @param options.name - Name for the key identity.\n * Defaults to 'Vana User'.\n * @param options.email - Email for the key identity.\n * Defaults to 'user@vana.com'.\n * @param options.passphrase - Passphrase to protect the private key.\n * If not provided, key is unprotected.\n * @returns ASCII-armored public and private keys\n *\n * @throws {Error} If key generation fails\n */\n async generateKeyPair(options?: {\n name?: string;\n email?: string;\n passphrase?: string;\n }): Promise<{ publicKey: string; privateKey: string }> {\n try {\n const openpgp = await getOpenPGP();\n // Use shared utility to get standardized parameters\n const keyGenParams = getPGPKeyGenParams(options);\n\n const { privateKey, publicKey } = await openpgp.generateKey(keyGenParams);\n\n return { publicKey, privateKey };\n } catch (error) {\n throw wrapCryptoError(\"PGP key generation\", error);\n }\n }\n}\n\n/**\n * Implements HTTP operations for browser environments.\n *\n * @remarks\n * Uses the native Fetch API available in all modern browsers.\n *\n * @internal\n */\nclass BrowserHttpAdapter implements VanaHttpAdapter {\n /**\n * Performs an HTTP request using the Fetch API.\n *\n * @param url - The URL to fetch.\n * Must be a valid HTTP/HTTPS URL.\n * @param options - Standard fetch options.\n * See MDN fetch documentation for details.\n * @returns Standard fetch Response object\n */\n async fetch(url: string, options?: RequestInit): Promise<Response> {\n return fetch(url, options);\n }\n}\n\n/**\n * Implements secure caching for browser environments.\n *\n * @remarks\n * Uses sessionStorage for temporary caching that's automatically cleared\n * when the browser tab closes. This provides better security for sensitive\n * data like signatures compared to localStorage. All keys are prefixed to\n * avoid conflicts with other applications.\n *\n * @internal\n */\nclass BrowserCacheAdapter implements VanaCacheAdapter {\n private readonly prefix = \"vana_cache_\";\n\n /**\n * Retrieves a cached value by key.\n *\n * @param key - The cache key to look up.\n * Automatically prefixed to avoid conflicts.\n * @returns The cached value or null if not found\n */\n get(key: string): string | null {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return null;\n }\n return sessionStorage.getItem(this.prefix + key);\n } catch {\n return null;\n }\n }\n\n /**\n * Stores a value in sessionStorage.\n *\n * @param key - The cache key.\n * Automatically prefixed with 'vana_cache_'.\n * @param value - The value to cache.\n * Will be cleared when tab closes.\n */\n set(key: string, value: string): void {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return;\n }\n sessionStorage.setItem(this.prefix + key, value);\n } catch {\n // Ignore storage errors (quota exceeded, etc.)\n }\n }\n\n /**\n * Removes a specific key from the cache.\n *\n * @param key - The cache key to remove.\n * Only removes the prefixed key.\n */\n delete(key: string): void {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return;\n }\n sessionStorage.removeItem(this.prefix + key);\n } catch {\n // Ignore storage errors\n }\n }\n\n /**\n * Clears all Vana-prefixed cache entries.\n *\n * @remarks\n * Only removes entries with the 'vana_cache_' prefix,\n * preserving other sessionStorage data.\n */\n clear(): void {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return;\n }\n // Only clear our prefixed keys to avoid affecting other data\n const keysToRemove: string[] = [];\n for (let i = 0; i < sessionStorage.length; i++) {\n const key = sessionStorage.key(i);\n if (key?.startsWith(this.prefix)) {\n keysToRemove.push(key);\n }\n }\n keysToRemove.forEach((key) => {\n sessionStorage.removeItem(key);\n });\n } catch {\n // Ignore storage errors\n }\n }\n}\n\n/**\n * Provides complete platform abstraction for browser environments.\n *\n * @remarks\n * This adapter aggregates all browser-specific implementations of platform\n * operations. It uses native Web APIs where possible and browser-compatible\n * libraries for crypto and PGP operations. The adapter automatically selects\n * appropriate cryptographic implementations based on feature flags.\n *\n * @example\n * ```typescript\n * // Create a browser adapter instance\n * const adapter = new BrowserPlatformAdapter();\n *\n * // All platform operations are available\n * const encrypted = await adapter.crypto.encryptWithPublicKey(\n * 'secret',\n * publicKey\n * );\n *\n * const response = await adapter.http.fetch('/api/data');\n *\n * adapter.cache.set('key', 'value'); // Uses sessionStorage\n * ```\n *\n * @category Platform\n */\nexport class BrowserPlatformAdapter implements VanaPlatformAdapter {\n public readonly crypto = new BrowserCryptoAdapter();\n public readonly pgp = new BrowserPGPAdapter();\n public readonly http = new BrowserHttpAdapter();\n public readonly cache = new BrowserCacheAdapter();\n public readonly platform = \"browser\" as const;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAqCA,uBAAmC;AACnC,yBAAgC;AAChC,yBAA2B;AAC3B,wCAA2C;AAC3C,0BAAyC;AACzC,kBAAqE;AACrE,gBAA2B;AAC3B,sBAAyB;AAGzB,qBAA0C;AAG1C,MAAM,iBAAa,+BAAW,MAAM,OAAO,SAAS,CAAC;AACrD,MAAM,oBAAgB,+BAAW,MAAM,OAAO,aAAa,CAAC;AAY5D,MAAM,qBAAkD;AAAA;AAAA,EAE9C,sBAAsB,IAAI,yCAA0B;AAAA,EACpD,sBAAsB,IAAI,6DAA2B;AAAA,IAC3D,eAAe,KAAK;AAAA,EACtB,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaD,MAAM,qBACJ,MACA,cACiB;AACjB,QAAI;AACF,UAAI,yBAAS,gBAAgB;AAE3B,cAAM,cAAc,aAAa,WAAW,IAAI,IAC5C,eACA,K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AA;AAAA,EASA,QAAc;AACZ,QAAI;AACF,UAAI,OAAO,mBAAmB,aAAa;AACzC;AAAA,MACF;AAEA,YAAM,eAAyB,CAAC;AAChC,eAAS,IAAI,GAAG,IAAI,eAAe,QAAQ,KAAK;AAC9C,cAAM,MAAM,eAAe,IAAI,CAAC;AAChC,YAAI,KAAK,WAAW,KAAK,MAAM,GAAG;AAChC,uBAAa,KAAK,GAAG;AAAA,QACvB;AAAA,MACF;AACA,mBAAa,QAAQ,CAAC,QAAQ;AAC5B,uBAAe,WAAW,GAAG;AAAA,MAC/B,CAAC;AAAA,IACH,QAAQ;AAAA,IAER;AAAA,EACF;AACF;AA6BO,MAAM,uBAAsD;AAAA,EACjD,SAAS,IAAI,qBAAqB;AAAA,EAClC,MAAM,IAAI,kBAAkB;AAAA,EAC5B,OAAO,IAAI,mBAAmB;AAAA,EAC9B,QAAQ,IAAI,oBAAoB;AAAA,EAChC,WAAW;AAC7B;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/platform/browser.ts"],"sourcesContent":["/**\n * Provides browser-specific implementations of platform abstraction interfaces.\n *\n * @remarks\n * This module implements all platform-specific operations for browser environments,\n * using native Web APIs and browser-compatible libraries. It avoids Node.js-specific\n * dependencies like Buffer, using Uint8Array and viem utilities instead. Uses a\n * custom ECIES implementation with @noble/secp256k1 for pure JavaScript crypto.\n *\n * @example\n * ```typescript\n * // Use the browser platform adapter\n * import { BrowserPlatformAdapter } from '@vana-sdk/platform/browser';\n *\n * const adapter = new BrowserPlatformAdapter();\n *\n * // Encrypt data with public key\n * const encrypted = await adapter.crypto.encryptWithPublicKey(\n * 'sensitive data',\n * '0x04...' // Public key hex\n * );\n *\n * // Use sessionStorage-backed cache\n * adapter.cache.set('temp_key', 'cached_value');\n * ```\n *\n * @category Platform\n * @module platform/browser\n */\n\nimport type {\n VanaPlatformAdapter,\n VanaCryptoAdapter,\n VanaPGPAdapter,\n VanaHttpAdapter,\n VanaCacheAdapter,\n} from \"./interface\";\nimport { getPGPKeyGenParams } from \"./shared/pgp-utils\";\nimport { wrapCryptoError } from \"./shared/error-utils\";\nimport { lazyImport } from \"../utils/lazy-import\";\nimport { WalletKeyEncryptionService } from \"../crypto/services/WalletKeyEncryptionService\";\nimport { parseEncryptedDataBuffer } from \"../utils/crypto-utils\";\nimport { toHex, fromHex, stringToBytes, bytesToString, concat } from \"viem\";\nimport * as secp256k1 from \"@noble/secp256k1\";\n\n// Import browser ECIES provider\nimport { BrowserECIESUint8Provider } from \"../crypto/ecies/browser\";\n\n// Lazy-loaded dependencies to avoid Turbopack TDZ issues\nconst getOpenPGP = lazyImport(() => import(\"openpgp\"));\n\n/**\n * Implements cryptographic operations for browser environments.\n *\n * @remarks\n * Provides ECIES encryption/decryption, key generation, and password-based\n * encryption using a custom ECIES implementation with @noble/secp256k1.\n * Uses Uint8Array and viem utilities for browser compatibility.\n *\n * @internal\n */\nclass BrowserCryptoAdapter implements VanaCryptoAdapter {\n private eciesProvider = new BrowserECIESUint8Provider();\n private walletService = new WalletKeyEncryptionService({\n eciesProvider: this.eciesProvider,\n });\n\n /**\n * Encrypts data using ECIES with a public key.\n *\n * @param data - The plaintext string to encrypt.\n * Typically user data or sensitive information.\n * @param publicKeyHex - The recipient's public key in hex format.\n * Can include or omit the '0x' prefix.\n * @returns Encrypted data as a hex string without '0x' prefix\n *\n * @throws {Error} If encryption fails or public key is invalid\n */\n async encryptWithPublicKey(\n data: string,\n publicKeyHex: string,\n ): Promise<string> {\n try {\n const prefixedHex = publicKeyHex.startsWith(\"0x\")\n ? publicKeyHex\n : `0x${publicKeyHex}`;\n const publicKeyBytes = fromHex(prefixedHex as `0x${string}`, \"bytes\");\n\n // Encrypt data using ECIES\n const encrypted = await this.eciesProvider.encrypt(\n publicKeyBytes,\n stringToBytes(data),\n );\n\n // Concatenate all components and return as hex string\n const result = concat([\n encrypted.iv,\n encrypted.ephemPublicKey,\n encrypted.ciphertext,\n encrypted.mac,\n ]);\n\n return toHex(result).slice(2); // Remove '0x' prefix for backward compatibility\n } catch (error) {\n throw wrapCryptoError(\"encryptWithPublicKey\", error);\n }\n }\n\n /**\n * Decrypts ECIES-encrypted data using a private key.\n *\n * @param encryptedData - Hex string containing encrypted data.\n * Can include or omit the '0x' prefix.\n * @param privateKeyHex - The private key in hex format.\n * Must correspond to the public key used for encryption.\n * @returns The decrypted plaintext string\n *\n * @throws {Error} If decryption fails or MAC verification fails\n */\n async decryptWithPrivateKey(\n encryptedData: string,\n privateKeyHex: string,\n ): Promise<string> {\n try {\n const encryptedHex = encryptedData.startsWith(\"0x\")\n ? encryptedData\n : `0x${encryptedData}`;\n const privateHex = privateKeyHex.startsWith(\"0x\")\n ? privateKeyHex\n : `0x${privateKeyHex}`;\n const encryptedBytes = fromHex(encryptedHex as `0x${string}`, \"bytes\");\n const privateKeyBytes = fromHex(privateHex as `0x${string}`, \"bytes\");\n\n // Parse the encrypted data into components\n const encrypted = parseEncryptedDataBuffer(encryptedBytes);\n\n // Decrypt using ECIES\n const decrypted = await this.eciesProvider.decrypt(\n privateKeyBytes,\n encrypted,\n );\n\n return bytesToString(decrypted);\n } catch (error) {\n throw wrapCryptoError(\"decryptWithPrivateKey\", error);\n }\n }\n\n /**\n * Encrypts data using a wallet's public key.\n *\n * @param data - The plaintext string to encrypt.\n * Typically permission data or DLP metadata.\n * @param publicKey - The wallet's public key.\n * Automatically handles compressed/uncompressed formats.\n * @returns Encrypted data as a hex string\n *\n * @throws {Error} If encryption fails or key processing fails\n */\n async encryptWithWalletPublicKey(\n data: string,\n publicKey: string,\n ): Promise<string> {\n try {\n return await this.walletService.encryptWithWalletPublicKey(\n data,\n publicKey,\n );\n } catch (error) {\n throw wrapCryptoError(\"encryptWithWalletPublicKey\", error);\n }\n }\n\n /**\n * Decrypts data using a wallet's private key.\n *\n * @param encryptedData - Hex string containing encrypted data.\n * Must be encrypted with corresponding wallet public key.\n * @param privateKey - The wallet's private key.\n * Obtain from wallet connection (handle with care).\n * @returns The decrypted plaintext string\n *\n * @throws {Error} If decryption fails or key is invalid\n */\n async decryptWithWalletPrivateKey(\n encryptedData: string,\n privateKey: string,\n ): Promise<string> {\n try {\n return await this.walletService.decryptWithWalletPrivateKey(\n encryptedData,\n privateKey,\n );\n } catch (error) {\n throw wrapCryptoError(\"decryptWithWalletPrivateKey\", error);\n }\n }\n\n /**\n * Generates a new secp256k1 key pair for ECIES operations.\n *\n * @returns Object containing hex-encoded public and private keys\n * @returns returns.privateKey - Private key in hex format without '0x'\n * @returns returns.publicKey - Compressed public key in hex format without '0x'\n *\n * @throws {Error} If key generation fails\n */\n async generateKeyPair(): Promise<{\n privateKey: string;\n publicKey: string;\n }> {\n try {\n // Generate random private key\n const privateKeyBytes = secp256k1.utils.randomPrivateKey();\n\n // Generate public key (compressed for consistency with Node implementation)\n const publicKeyBytes = secp256k1.getPublicKey(privateKeyBytes, true);\n\n return {\n privateKey: toHex(privateKeyBytes).slice(2),\n publicKey: toHex(publicKeyBytes).slice(2),\n };\n } catch (error) {\n throw wrapCryptoError(\"generateKeyPair\", error);\n }\n }\n\n /**\n * Encrypts binary data using password-based encryption.\n *\n * @param data - Binary data to encrypt.\n * Typically file contents or serialized objects.\n * @param password - Password for encryption.\n * Often derived from wallet signatures.\n * @returns Encrypted data as Uint8Array\n *\n * @remarks\n * Uses OpenPGP for password-based encryption with automatic\n * salt generation for security.\n *\n * @throws {Error} If encryption fails\n */\n async encryptWithPassword(\n data: Uint8Array,\n password: string,\n ): Promise<Uint8Array> {\n try {\n const openpgp = await getOpenPGP();\n\n // Create a message from the data\n const message = await openpgp.createMessage({ binary: data });\n\n // Encrypt with password\n const encrypted = await openpgp.encrypt({\n message,\n passwords: [password],\n format: \"binary\",\n });\n\n return new Uint8Array(encrypted as ArrayBuffer);\n } catch (error) {\n throw wrapCryptoError(\"encryptWithPassword\", error);\n }\n }\n\n /**\n * Decrypts password-encrypted binary data.\n *\n * @param encryptedData - Password-encrypted data as Uint8Array.\n * Must be encrypted with the same password.\n * @param password - Password for decryption.\n * Must match the encryption password.\n * @returns Decrypted data as Uint8Array\n *\n * @throws {Error} If decryption fails or password is incorrect\n */\n async decryptWithPassword(\n encryptedData: Uint8Array,\n password: string,\n ): Promise<Uint8Array> {\n try {\n const openpgp = await getOpenPGP();\n\n // Read the encrypted message\n const message = await openpgp.readMessage({\n binaryMessage: encryptedData,\n });\n\n // Decrypt with password\n const { data } = await openpgp.decrypt({\n message,\n passwords: [password],\n format: \"binary\",\n });\n\n return new Uint8Array(data as ArrayBuffer);\n } catch (error) {\n throw wrapCryptoError(\"decryptWithPassword\", error);\n }\n }\n}\n\n/**\n * Implements PGP operations for browser environments.\n *\n * @remarks\n * Provides PGP encryption, decryption, and key generation using the OpenPGP.js\n * library with browser-optimized configuration.\n *\n * @internal\n */\nclass BrowserPGPAdapter implements VanaPGPAdapter {\n /**\n * Encrypts data using PGP public key encryption.\n *\n * @param data - The plaintext string to encrypt.\n * Typically messages or structured data.\n * @param publicKeyArmored - ASCII-armored PGP public key.\n * Obtain from PGP key generation or key servers.\n * @returns ASCII-armored encrypted message\n *\n * @throws {Error} If encryption fails or public key is invalid\n */\n async encrypt(data: string, publicKeyArmored: string): Promise<string> {\n try {\n const openpgp = await getOpenPGP();\n const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });\n\n const encrypted = await openpgp.encrypt({\n message: await openpgp.createMessage({ text: data }),\n encryptionKeys: publicKey,\n config: {\n preferredCompressionAlgorithm: openpgp.enums.compression.zlib,\n },\n });\n\n return encrypted as string;\n } catch (error) {\n throw new Error(`PGP encryption failed: ${String(error)}`);\n }\n }\n\n /**\n * Decrypts PGP-encrypted data using a private key.\n *\n * @param encryptedData - ASCII-armored encrypted message.\n * Must be encrypted with corresponding public key.\n * @param privateKeyArmored - ASCII-armored PGP private key.\n * Must correspond to the public key used for encryption.\n * @returns The decrypted plaintext string\n *\n * @throws {Error} If decryption fails or private key is invalid\n */\n async decrypt(\n encryptedData: string,\n privateKeyArmored: string,\n ): Promise<string> {\n try {\n const openpgp = await getOpenPGP();\n const privateKey = await openpgp.readPrivateKey({\n armoredKey: privateKeyArmored,\n });\n const message = await openpgp.readMessage({\n armoredMessage: encryptedData,\n });\n\n const { data: decrypted } = await openpgp.decrypt({\n message,\n decryptionKeys: privateKey,\n });\n\n return decrypted as string;\n } catch (error) {\n throw new Error(`PGP decryption failed: ${String(error)}`);\n }\n }\n\n /**\n * Generates a new PGP key pair.\n *\n * @param options - Key generation options\n * @param options.name - Name for the key identity.\n * Defaults to 'Vana User'.\n * @param options.email - Email for the key identity.\n * Defaults to 'user@vana.com'.\n * @param options.passphrase - Passphrase to protect the private key.\n * If not provided, key is unprotected.\n * @returns ASCII-armored public and private keys\n *\n * @throws {Error} If key generation fails\n */\n async generateKeyPair(options?: {\n name?: string;\n email?: string;\n passphrase?: string;\n }): Promise<{ publicKey: string; privateKey: string }> {\n try {\n const openpgp = await getOpenPGP();\n // Use shared utility to get standardized parameters\n const keyGenParams = getPGPKeyGenParams(options);\n\n const { privateKey, publicKey } = await openpgp.generateKey(keyGenParams);\n\n return { publicKey, privateKey };\n } catch (error) {\n throw wrapCryptoError(\"PGP key generation\", error);\n }\n }\n}\n\n/**\n * Implements HTTP operations for browser environments.\n *\n * @remarks\n * Uses the native Fetch API available in all modern browsers.\n *\n * @internal\n */\nclass BrowserHttpAdapter implements VanaHttpAdapter {\n /**\n * Performs an HTTP request using the Fetch API.\n *\n * @param url - The URL to fetch.\n * Must be a valid HTTP/HTTPS URL.\n * @param options - Standard fetch options.\n * See MDN fetch documentation for details.\n * @returns Standard fetch Response object\n */\n async fetch(url: string, options?: RequestInit): Promise<Response> {\n return fetch(url, options);\n }\n}\n\n/**\n * Implements secure caching for browser environments.\n *\n * @remarks\n * Uses sessionStorage for temporary caching that's automatically cleared\n * when the browser tab closes. This provides better security for sensitive\n * data like signatures compared to localStorage. All keys are prefixed to\n * avoid conflicts with other applications.\n *\n * @internal\n */\nclass BrowserCacheAdapter implements VanaCacheAdapter {\n private readonly prefix = \"vana_cache_\";\n\n /**\n * Retrieves a cached value by key.\n *\n * @param key - The cache key to look up.\n * Automatically prefixed to avoid conflicts.\n * @returns The cached value or null if not found\n */\n get(key: string): string | null {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return null;\n }\n return sessionStorage.getItem(this.prefix + key);\n } catch {\n return null;\n }\n }\n\n /**\n * Stores a value in sessionStorage.\n *\n * @param key - The cache key.\n * Automatically prefixed with 'vana_cache_'.\n * @param value - The value to cache.\n * Will be cleared when tab closes.\n */\n set(key: string, value: string): void {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return;\n }\n sessionStorage.setItem(this.prefix + key, value);\n } catch {\n // Ignore storage errors (quota exceeded, etc.)\n }\n }\n\n /**\n * Removes a specific key from the cache.\n *\n * @param key - The cache key to remove.\n * Only removes the prefixed key.\n */\n delete(key: string): void {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return;\n }\n sessionStorage.removeItem(this.prefix + key);\n } catch {\n // Ignore storage errors\n }\n }\n\n /**\n * Clears all Vana-prefixed cache entries.\n *\n * @remarks\n * Only removes entries with the 'vana_cache_' prefix,\n * preserving other sessionStorage data.\n */\n clear(): void {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return;\n }\n // Only clear our prefixed keys to avoid affecting other data\n const keysToRemove: string[] = [];\n for (let i = 0; i < sessionStorage.length; i++) {\n const key = sessionStorage.key(i);\n if (key?.startsWith(this.prefix)) {\n keysToRemove.push(key);\n }\n }\n keysToRemove.forEach((key) => {\n sessionStorage.removeItem(key);\n });\n } catch {\n // Ignore storage errors\n }\n }\n}\n\n/**\n * Provides complete platform abstraction for browser environments.\n *\n * @remarks\n * This adapter aggregates all browser-specific implementations of platform\n * operations. It uses native Web APIs where possible and browser-compatible\n * libraries for crypto and PGP operations using a custom ECIES implementation\n * with @noble/secp256k1 for pure JavaScript cryptography.\n *\n * @example\n * ```typescript\n * // Create a browser adapter instance\n * const adapter = new BrowserPlatformAdapter();\n *\n * // All platform operations are available\n * const encrypted = await adapter.crypto.encryptWithPublicKey(\n * 'secret',\n * publicKey\n * );\n *\n * const response = await adapter.http.fetch('/api/data');\n *\n * adapter.cache.set('key', 'value'); // Uses sessionStorage\n * ```\n *\n * @category Platform\n */\nexport class BrowserPlatformAdapter implements VanaPlatformAdapter {\n public readonly crypto = new BrowserCryptoAdapter();\n public readonly pgp = new BrowserPGPAdapter();\n public readonly http = new BrowserHttpAdapter();\n public readonly cache = new BrowserCacheAdapter();\n public readonly platform = \"browser\" as const;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAqCA,uBAAmC;AACnC,yBAAgC;AAChC,yBAA2B;AAC3B,wCAA2C;AAC3C,0BAAyC;AACzC,kBAAqE;AACrE,gBAA2B;AAG3B,qBAA0C;AAG1C,MAAM,iBAAa,+BAAW,MAAM,OAAO,SAAS,CAAC;AAYrD,MAAM,qBAAkD;AAAA,EAC9C,gBAAgB,IAAI,yCAA0B;AAAA,EAC9C,gBAAgB,IAAI,6DAA2B;AAAA,IACrD,eAAe,KAAK;AAAA,EACtB,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaD,MAAM,qBACJ,MACA,cACiB;AACjB,QAAI;AACF,YAAM,cAAc,aAAa,WAAW,IAAI,IAC5C,eACA,KAAK,YAAY;AACrB,YAAM,qBAAiB,qBAAQ,aAA8B,OAAO;AAGpE,YAAM,YAAY,MAAM,KAAK,cAAc;AAAA,QACzC;AAAA,YACA,2BAAc,IAAI;AAAA,MACpB;AAGA,YAAM,aAAS,oBAAO;AAAA,QACpB,UAAU;AAAA,QACV,UAAU;AAAA,QACV,UAAU;AAAA,QACV,UAAU;AAAA,MACZ,CAAC;AAED,iBAAO,mBAAM,MAAM,EAAE,MAAM,CAAC;AAAA,IAC9B,SAAS,OAAO;AACd,gBAAM,oCAAgB,wBAAwB,KAAK;AAAA,IACrD;AAAA,EACF;AAAA;AAAA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AAa,KAAK,GAAG;AAAA,QACvB;AAAA,MACF;AACA,mBAAa,QAAQ,CAAC,QAAQ;AAC5B,uBAAe,WAAW,GAAG;AAAA,MAC/B,CAAC;AAAA,IACH,QAAQ;AAAA,IAER;AAAA,EACF;AACF;AA6BO,MAAM,uBAAsD;AAAA,EACjD,SAAS,IAAI,qBAAqB;AAAA,EAClC,MAAM,IAAI,kBAAkB;AAAA,EAC5B,OAAO,IAAI,mBAAmB;AAAA,EAC9B,QAAQ,IAAI,oBAAoB;AAAA,EAChC,WAAW;AAC7B;","names":[]}
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* @remarks
|
|
5
5
|
* This module implements all platform-specific operations for browser environments,
|
|
6
6
|
* using native Web APIs and browser-compatible libraries. It avoids Node.js-specific
|
|
7
|
-
* dependencies like Buffer, using Uint8Array and viem utilities instead.
|
|
8
|
-
*
|
|
7
|
+
* dependencies like Buffer, using Uint8Array and viem utilities instead. Uses a
|
|
8
|
+
* custom ECIES implementation with @noble/secp256k1 for pure JavaScript crypto.
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* ```typescript
|
|
@@ -33,14 +33,14 @@ import type { VanaPlatformAdapter, VanaCryptoAdapter, VanaPGPAdapter, VanaHttpAd
|
|
|
33
33
|
*
|
|
34
34
|
* @remarks
|
|
35
35
|
* Provides ECIES encryption/decryption, key generation, and password-based
|
|
36
|
-
* encryption using
|
|
36
|
+
* encryption using a custom ECIES implementation with @noble/secp256k1.
|
|
37
37
|
* Uses Uint8Array and viem utilities for browser compatibility.
|
|
38
38
|
*
|
|
39
39
|
* @internal
|
|
40
40
|
*/
|
|
41
41
|
declare class BrowserCryptoAdapter implements VanaCryptoAdapter {
|
|
42
|
-
private
|
|
43
|
-
private
|
|
42
|
+
private eciesProvider;
|
|
43
|
+
private walletService;
|
|
44
44
|
/**
|
|
45
45
|
* Encrypts data using ECIES with a public key.
|
|
46
46
|
*
|
|
@@ -260,8 +260,8 @@ declare class BrowserCacheAdapter implements VanaCacheAdapter {
|
|
|
260
260
|
* @remarks
|
|
261
261
|
* This adapter aggregates all browser-specific implementations of platform
|
|
262
262
|
* operations. It uses native Web APIs where possible and browser-compatible
|
|
263
|
-
* libraries for crypto and PGP operations
|
|
264
|
-
*
|
|
263
|
+
* libraries for crypto and PGP operations using a custom ECIES implementation
|
|
264
|
+
* with @noble/secp256k1 for pure JavaScript cryptography.
|
|
265
265
|
*
|
|
266
266
|
* @example
|
|
267
267
|
* ```typescript
|
package/dist/platform/browser.js
CHANGED
|
@@ -5,15 +5,12 @@ import { WalletKeyEncryptionService } from "../crypto/services/WalletKeyEncrypti
|
|
|
5
5
|
import { parseEncryptedDataBuffer } from "../utils/crypto-utils";
|
|
6
6
|
import { toHex, fromHex, stringToBytes, bytesToString, concat } from "viem";
|
|
7
7
|
import * as secp256k1 from "@noble/secp256k1";
|
|
8
|
-
import { features } from "../config/features";
|
|
9
8
|
import { BrowserECIESUint8Provider } from "../crypto/ecies/browser";
|
|
10
9
|
const getOpenPGP = lazyImport(() => import("openpgp"));
|
|
11
|
-
const getEccryptoJS = lazyImport(() => import("eccrypto-js"));
|
|
12
10
|
class BrowserCryptoAdapter {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
eciesProvider: this.customEciesProvider
|
|
11
|
+
eciesProvider = new BrowserECIESUint8Provider();
|
|
12
|
+
walletService = new WalletKeyEncryptionService({
|
|
13
|
+
eciesProvider: this.eciesProvider
|
|
17
14
|
});
|
|
18
15
|
/**
|
|
19
16
|
* Encrypts data using ECIES with a public key.
|
|
@@ -28,38 +25,19 @@ class BrowserCryptoAdapter {
|
|
|
28
25
|
*/
|
|
29
26
|
async encryptWithPublicKey(data, publicKeyHex) {
|
|
30
27
|
try {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return toHex(result).slice(2);
|
|
45
|
-
} else {
|
|
46
|
-
const eccryptojs = await getEccryptoJS();
|
|
47
|
-
const cleanKey = publicKeyHex.startsWith("0x") ? publicKeyHex.slice(2) : publicKeyHex;
|
|
48
|
-
const publicKeyBytes = Buffer.from(cleanKey, "hex");
|
|
49
|
-
const uncompressed = this.customEciesProvider.normalizeToUncompressed(
|
|
50
|
-
new Uint8Array(publicKeyBytes)
|
|
51
|
-
);
|
|
52
|
-
const publicKeyBuffer = Buffer.from(uncompressed);
|
|
53
|
-
const message = Buffer.from(data, "utf8");
|
|
54
|
-
const encrypted = await eccryptojs.encrypt(publicKeyBuffer, message);
|
|
55
|
-
const result = Buffer.concat([
|
|
56
|
-
encrypted.iv,
|
|
57
|
-
encrypted.ephemPublicKey,
|
|
58
|
-
encrypted.ciphertext,
|
|
59
|
-
encrypted.mac
|
|
60
|
-
]);
|
|
61
|
-
return result.toString("hex");
|
|
62
|
-
}
|
|
28
|
+
const prefixedHex = publicKeyHex.startsWith("0x") ? publicKeyHex : `0x${publicKeyHex}`;
|
|
29
|
+
const publicKeyBytes = fromHex(prefixedHex, "bytes");
|
|
30
|
+
const encrypted = await this.eciesProvider.encrypt(
|
|
31
|
+
publicKeyBytes,
|
|
32
|
+
stringToBytes(data)
|
|
33
|
+
);
|
|
34
|
+
const result = concat([
|
|
35
|
+
encrypted.iv,
|
|
36
|
+
encrypted.ephemPublicKey,
|
|
37
|
+
encrypted.ciphertext,
|
|
38
|
+
encrypted.mac
|
|
39
|
+
]);
|
|
40
|
+
return toHex(result).slice(2);
|
|
63
41
|
} catch (error) {
|
|
64
42
|
throw wrapCryptoError("encryptWithPublicKey", error);
|
|
65
43
|
}
|
|
@@ -77,30 +55,16 @@ class BrowserCryptoAdapter {
|
|
|
77
55
|
*/
|
|
78
56
|
async decryptWithPrivateKey(encryptedData, privateKeyHex) {
|
|
79
57
|
try {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return bytesToString(decrypted);
|
|
91
|
-
} else {
|
|
92
|
-
const eccryptojs = await getEccryptoJS();
|
|
93
|
-
const privateKey = Buffer.from(privateKeyHex, "hex");
|
|
94
|
-
const encryptedBuffer = Buffer.from(encryptedData, "hex");
|
|
95
|
-
const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
|
|
96
|
-
const decrypted = await eccryptojs.decrypt(privateKey, {
|
|
97
|
-
iv: Buffer.from(iv),
|
|
98
|
-
ephemPublicKey: Buffer.from(ephemPublicKey),
|
|
99
|
-
ciphertext: Buffer.from(ciphertext),
|
|
100
|
-
mac: Buffer.from(mac)
|
|
101
|
-
});
|
|
102
|
-
return decrypted.toString("utf8");
|
|
103
|
-
}
|
|
58
|
+
const encryptedHex = encryptedData.startsWith("0x") ? encryptedData : `0x${encryptedData}`;
|
|
59
|
+
const privateHex = privateKeyHex.startsWith("0x") ? privateKeyHex : `0x${privateKeyHex}`;
|
|
60
|
+
const encryptedBytes = fromHex(encryptedHex, "bytes");
|
|
61
|
+
const privateKeyBytes = fromHex(privateHex, "bytes");
|
|
62
|
+
const encrypted = parseEncryptedDataBuffer(encryptedBytes);
|
|
63
|
+
const decrypted = await this.eciesProvider.decrypt(
|
|
64
|
+
privateKeyBytes,
|
|
65
|
+
encrypted
|
|
66
|
+
);
|
|
67
|
+
return bytesToString(decrypted);
|
|
104
68
|
} catch (error) {
|
|
105
69
|
throw wrapCryptoError("decryptWithPrivateKey", error);
|
|
106
70
|
}
|
|
@@ -118,29 +82,10 @@ class BrowserCryptoAdapter {
|
|
|
118
82
|
*/
|
|
119
83
|
async encryptWithWalletPublicKey(data, publicKey) {
|
|
120
84
|
try {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
);
|
|
126
|
-
} else {
|
|
127
|
-
const eccryptojs = await getEccryptoJS();
|
|
128
|
-
const cleanKey = publicKey.startsWith("0x") ? publicKey.slice(2) : publicKey;
|
|
129
|
-
const publicKeyBytes = Buffer.from(cleanKey, "hex");
|
|
130
|
-
const uncompressed = this.customEciesProvider.normalizeToUncompressed(
|
|
131
|
-
new Uint8Array(publicKeyBytes)
|
|
132
|
-
);
|
|
133
|
-
const publicKeyBuffer = Buffer.from(uncompressed);
|
|
134
|
-
const message = Buffer.from(data, "utf8");
|
|
135
|
-
const encrypted = await eccryptojs.encrypt(publicKeyBuffer, message);
|
|
136
|
-
const result = Buffer.concat([
|
|
137
|
-
encrypted.iv,
|
|
138
|
-
encrypted.ephemPublicKey,
|
|
139
|
-
encrypted.ciphertext,
|
|
140
|
-
encrypted.mac
|
|
141
|
-
]);
|
|
142
|
-
return result.toString("hex");
|
|
143
|
-
}
|
|
85
|
+
return await this.walletService.encryptWithWalletPublicKey(
|
|
86
|
+
data,
|
|
87
|
+
publicKey
|
|
88
|
+
);
|
|
144
89
|
} catch (error) {
|
|
145
90
|
throw wrapCryptoError("encryptWithWalletPublicKey", error);
|
|
146
91
|
}
|
|
@@ -158,24 +103,10 @@ class BrowserCryptoAdapter {
|
|
|
158
103
|
*/
|
|
159
104
|
async decryptWithWalletPrivateKey(encryptedData, privateKey) {
|
|
160
105
|
try {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
);
|
|
166
|
-
} else {
|
|
167
|
-
const eccryptojs = await getEccryptoJS();
|
|
168
|
-
const privateKeyBuffer = Buffer.from(privateKey, "hex");
|
|
169
|
-
const encryptedBuffer = Buffer.from(encryptedData, "hex");
|
|
170
|
-
const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
|
|
171
|
-
const decrypted = await eccryptojs.decrypt(privateKeyBuffer, {
|
|
172
|
-
iv: Buffer.from(iv),
|
|
173
|
-
ephemPublicKey: Buffer.from(ephemPublicKey),
|
|
174
|
-
ciphertext: Buffer.from(ciphertext),
|
|
175
|
-
mac: Buffer.from(mac)
|
|
176
|
-
});
|
|
177
|
-
return decrypted.toString("utf8");
|
|
178
|
-
}
|
|
106
|
+
return await this.walletService.decryptWithWalletPrivateKey(
|
|
107
|
+
encryptedData,
|
|
108
|
+
privateKey
|
|
109
|
+
);
|
|
179
110
|
} catch (error) {
|
|
180
111
|
throw wrapCryptoError("decryptWithWalletPrivateKey", error);
|
|
181
112
|
}
|
|
@@ -191,22 +122,12 @@ class BrowserCryptoAdapter {
|
|
|
191
122
|
*/
|
|
192
123
|
async generateKeyPair() {
|
|
193
124
|
try {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
};
|
|
201
|
-
} else {
|
|
202
|
-
const eccryptojs = await getEccryptoJS();
|
|
203
|
-
const privateKey = eccryptojs.generatePrivate();
|
|
204
|
-
const publicKey = eccryptojs.getPublic(privateKey);
|
|
205
|
-
return {
|
|
206
|
-
privateKey: privateKey.toString("hex"),
|
|
207
|
-
publicKey: publicKey.toString("hex")
|
|
208
|
-
};
|
|
209
|
-
}
|
|
125
|
+
const privateKeyBytes = secp256k1.utils.randomPrivateKey();
|
|
126
|
+
const publicKeyBytes = secp256k1.getPublicKey(privateKeyBytes, true);
|
|
127
|
+
return {
|
|
128
|
+
privateKey: toHex(privateKeyBytes).slice(2),
|
|
129
|
+
publicKey: toHex(publicKeyBytes).slice(2)
|
|
130
|
+
};
|
|
210
131
|
} catch (error) {
|
|
211
132
|
throw wrapCryptoError("generateKeyPair", error);
|
|
212
133
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/platform/browser.ts"],"sourcesContent":["/**\n * Provides browser-specific implementations of platform abstraction interfaces.\n *\n * @remarks\n * This module implements all platform-specific operations for browser environments,\n * using native Web APIs and browser-compatible libraries. It avoids Node.js-specific\n * dependencies like Buffer, using Uint8Array and viem utilities instead. Supports\n * both standard eccrypto and custom ECIES implementations based on feature flags.\n *\n * @example\n * ```typescript\n * // Use the browser platform adapter\n * import { BrowserPlatformAdapter } from '@vana-sdk/platform/browser';\n *\n * const adapter = new BrowserPlatformAdapter();\n *\n * // Encrypt data with public key\n * const encrypted = await adapter.crypto.encryptWithPublicKey(\n * 'sensitive data',\n * '0x04...' // Public key hex\n * );\n *\n * // Use sessionStorage-backed cache\n * adapter.cache.set('temp_key', 'cached_value');\n * ```\n *\n * @category Platform\n * @module platform/browser\n */\n\nimport type {\n VanaPlatformAdapter,\n VanaCryptoAdapter,\n VanaPGPAdapter,\n VanaHttpAdapter,\n VanaCacheAdapter,\n} from \"./interface\";\nimport { getPGPKeyGenParams } from \"./shared/pgp-utils\";\nimport { wrapCryptoError } from \"./shared/error-utils\";\nimport { lazyImport } from \"../utils/lazy-import\";\nimport { WalletKeyEncryptionService } from \"../crypto/services/WalletKeyEncryptionService\";\nimport { parseEncryptedDataBuffer } from \"../utils/crypto-utils\";\nimport { toHex, fromHex, stringToBytes, bytesToString, concat } from \"viem\";\nimport * as secp256k1 from \"@noble/secp256k1\";\nimport { features } from \"../config/features\";\n\n// Import browser ECIES provider\nimport { BrowserECIESUint8Provider } from \"../crypto/ecies/browser\";\n\n// Lazy-loaded dependencies to avoid Turbopack TDZ issues\nconst getOpenPGP = lazyImport(() => import(\"openpgp\"));\nconst getEccryptoJS = lazyImport(() => import(\"eccrypto-js\"));\n\n/**\n * Implements cryptographic operations for browser environments.\n *\n * @remarks\n * Provides ECIES encryption/decryption, key generation, and password-based\n * encryption using either eccrypto-js or a custom ECIES implementation.\n * Uses Uint8Array and viem utilities for browser compatibility.\n *\n * @internal\n */\nclass BrowserCryptoAdapter implements VanaCryptoAdapter {\n // Initialize both providers - only one will be used based on feature flag\n private customEciesProvider = new BrowserECIESUint8Provider();\n private customWalletService = new WalletKeyEncryptionService({\n eciesProvider: this.customEciesProvider,\n });\n\n /**\n * Encrypts data using ECIES with a public key.\n *\n * @param data - The plaintext string to encrypt.\n * Typically user data or sensitive information.\n * @param publicKeyHex - The recipient's public key in hex format.\n * Can include or omit the '0x' prefix.\n * @returns Encrypted data as a hex string without '0x' prefix\n *\n * @throws {Error} If encryption fails or public key is invalid\n */\n async encryptWithPublicKey(\n data: string,\n publicKeyHex: string,\n ): Promise<string> {\n try {\n if (features.useCustomECIES) {\n // Use custom ECIES implementation\n const prefixedHex = publicKeyHex.startsWith(\"0x\")\n ? publicKeyHex\n : `0x${publicKeyHex}`;\n const publicKeyBytes = fromHex(prefixedHex as `0x${string}`, \"bytes\");\n\n // Encrypt data using ECIES\n const encrypted = await this.customEciesProvider.encrypt(\n publicKeyBytes,\n stringToBytes(data),\n );\n\n // Concatenate all components and return as hex string\n const result = concat([\n encrypted.iv,\n encrypted.ephemPublicKey,\n encrypted.ciphertext,\n encrypted.mac,\n ]);\n\n return toHex(result).slice(2); // Remove '0x' prefix for backward compatibility\n } else {\n // Use eccrypto-js (default)\n const eccryptojs = await getEccryptoJS();\n\n // Remove 0x prefix if present\n const cleanKey = publicKeyHex.startsWith(\"0x\")\n ? publicKeyHex.slice(2)\n : publicKeyHex;\n const publicKeyBytes = Buffer.from(cleanKey, \"hex\");\n\n // Normalize to uncompressed format using the ECIES provider\n // This handles both compressed (33 bytes) and uncompressed (65 bytes) keys\n const uncompressed = this.customEciesProvider.normalizeToUncompressed(\n new Uint8Array(publicKeyBytes),\n );\n const publicKeyBuffer = Buffer.from(uncompressed);\n\n const message = Buffer.from(data, \"utf8\");\n\n const encrypted = await eccryptojs.encrypt(publicKeyBuffer, message);\n\n // Concatenate all components and return as hex string\n const result = Buffer.concat([\n encrypted.iv,\n encrypted.ephemPublicKey,\n encrypted.ciphertext,\n encrypted.mac,\n ]);\n\n return result.toString(\"hex\");\n }\n } catch (error) {\n throw wrapCryptoError(\"encryptWithPublicKey\", error);\n }\n }\n\n /**\n * Decrypts ECIES-encrypted data using a private key.\n *\n * @param encryptedData - Hex string containing encrypted data.\n * Can include or omit the '0x' prefix.\n * @param privateKeyHex - The private key in hex format.\n * Must correspond to the public key used for encryption.\n * @returns The decrypted plaintext string\n *\n * @throws {Error} If decryption fails or MAC verification fails\n */\n async decryptWithPrivateKey(\n encryptedData: string,\n privateKeyHex: string,\n ): Promise<string> {\n try {\n if (features.useCustomECIES) {\n // Use custom ECIES implementation\n const encryptedHex = encryptedData.startsWith(\"0x\")\n ? encryptedData\n : `0x${encryptedData}`;\n const privateHex = privateKeyHex.startsWith(\"0x\")\n ? privateKeyHex\n : `0x${privateKeyHex}`;\n const encryptedBytes = fromHex(encryptedHex as `0x${string}`, \"bytes\");\n const privateKeyBytes = fromHex(privateHex as `0x${string}`, \"bytes\");\n\n // Parse the encrypted data into components\n const encrypted = parseEncryptedDataBuffer(encryptedBytes);\n\n // Decrypt using ECIES\n const decrypted = await this.customEciesProvider.decrypt(\n privateKeyBytes,\n encrypted,\n );\n\n return bytesToString(decrypted);\n } else {\n // Use eccrypto-js (default)\n const eccryptojs = await getEccryptoJS();\n const privateKey = Buffer.from(privateKeyHex, \"hex\");\n const encryptedBuffer = Buffer.from(encryptedData, \"hex\");\n\n // Parse the encrypted data\n const { iv, ephemPublicKey, ciphertext, mac } =\n parseEncryptedDataBuffer(encryptedBuffer);\n\n const decrypted = await eccryptojs.decrypt(privateKey, {\n iv: Buffer.from(iv),\n ephemPublicKey: Buffer.from(ephemPublicKey),\n ciphertext: Buffer.from(ciphertext),\n mac: Buffer.from(mac),\n });\n\n return decrypted.toString(\"utf8\");\n }\n } catch (error) {\n throw wrapCryptoError(\"decryptWithPrivateKey\", error);\n }\n }\n\n /**\n * Encrypts data using a wallet's public key.\n *\n * @param data - The plaintext string to encrypt.\n * Typically permission data or DLP metadata.\n * @param publicKey - The wallet's public key.\n * Automatically handles compressed/uncompressed formats.\n * @returns Encrypted data as a hex string\n *\n * @throws {Error} If encryption fails or key processing fails\n */\n async encryptWithWalletPublicKey(\n data: string,\n publicKey: string,\n ): Promise<string> {\n try {\n if (features.useCustomECIES) {\n // Use custom ECIES implementation via WalletKeyEncryptionService\n return await this.customWalletService.encryptWithWalletPublicKey(\n data,\n publicKey,\n );\n } else {\n // Use eccrypto-js directly for wallet encryption\n const eccryptojs = await getEccryptoJS();\n\n // Remove 0x prefix if present\n const cleanKey = publicKey.startsWith(\"0x\")\n ? publicKey.slice(2)\n : publicKey;\n const publicKeyBytes = Buffer.from(cleanKey, \"hex\");\n\n // Normalize to uncompressed format using the ECIES provider\n // This handles both compressed (33 bytes) and uncompressed (65 bytes) keys\n const uncompressed = this.customEciesProvider.normalizeToUncompressed(\n new Uint8Array(publicKeyBytes),\n );\n const publicKeyBuffer = Buffer.from(uncompressed);\n\n const message = Buffer.from(data, \"utf8\");\n\n const encrypted = await eccryptojs.encrypt(publicKeyBuffer, message);\n\n // Concatenate all components and return as hex string\n const result = Buffer.concat([\n encrypted.iv,\n encrypted.ephemPublicKey,\n encrypted.ciphertext,\n encrypted.mac,\n ]);\n\n return result.toString(\"hex\");\n }\n } catch (error) {\n throw wrapCryptoError(\"encryptWithWalletPublicKey\", error);\n }\n }\n\n /**\n * Decrypts data using a wallet's private key.\n *\n * @param encryptedData - Hex string containing encrypted data.\n * Must be encrypted with corresponding wallet public key.\n * @param privateKey - The wallet's private key.\n * Obtain from wallet connection (handle with care).\n * @returns The decrypted plaintext string\n *\n * @throws {Error} If decryption fails or key is invalid\n */\n async decryptWithWalletPrivateKey(\n encryptedData: string,\n privateKey: string,\n ): Promise<string> {\n try {\n if (features.useCustomECIES) {\n // Use custom ECIES implementation via WalletKeyEncryptionService\n return await this.customWalletService.decryptWithWalletPrivateKey(\n encryptedData,\n privateKey,\n );\n } else {\n // Use eccrypto-js directly for wallet decryption\n const eccryptojs = await getEccryptoJS();\n const privateKeyBuffer = Buffer.from(privateKey, \"hex\");\n const encryptedBuffer = Buffer.from(encryptedData, \"hex\");\n\n // Parse the encrypted data\n const { iv, ephemPublicKey, ciphertext, mac } =\n parseEncryptedDataBuffer(encryptedBuffer);\n\n const decrypted = await eccryptojs.decrypt(privateKeyBuffer, {\n iv: Buffer.from(iv),\n ephemPublicKey: Buffer.from(ephemPublicKey),\n ciphertext: Buffer.from(ciphertext),\n mac: Buffer.from(mac),\n });\n\n return decrypted.toString(\"utf8\");\n }\n } catch (error) {\n throw wrapCryptoError(\"decryptWithWalletPrivateKey\", error);\n }\n }\n\n /**\n * Generates a new secp256k1 key pair for ECIES operations.\n *\n * @returns Object containing hex-encoded public and private keys\n * @returns returns.privateKey - Private key in hex format without '0x'\n * @returns returns.publicKey - Compressed public key in hex format without '0x'\n *\n * @throws {Error} If key generation fails\n */\n async generateKeyPair(): Promise<{\n privateKey: string;\n publicKey: string;\n }> {\n try {\n if (features.useCustomECIES) {\n // Use custom implementation with @noble/secp256k1\n // Generate random private key\n const privateKeyBytes = secp256k1.utils.randomPrivateKey();\n\n // Generate public key (compressed for consistency with Node implementation)\n const publicKeyBytes = secp256k1.getPublicKey(privateKeyBytes, true);\n\n return {\n privateKey: toHex(privateKeyBytes).slice(2),\n publicKey: toHex(publicKeyBytes).slice(2),\n };\n } else {\n // Use eccrypto-js (default)\n const eccryptojs = await getEccryptoJS();\n const privateKey = eccryptojs.generatePrivate();\n const publicKey = eccryptojs.getPublic(privateKey);\n\n return {\n privateKey: privateKey.toString(\"hex\"),\n publicKey: publicKey.toString(\"hex\"),\n };\n }\n } catch (error) {\n throw wrapCryptoError(\"generateKeyPair\", error);\n }\n }\n\n /**\n * Encrypts binary data using password-based encryption.\n *\n * @param data - Binary data to encrypt.\n * Typically file contents or serialized objects.\n * @param password - Password for encryption.\n * Often derived from wallet signatures.\n * @returns Encrypted data as Uint8Array\n *\n * @remarks\n * Uses OpenPGP for password-based encryption with automatic\n * salt generation for security.\n *\n * @throws {Error} If encryption fails\n */\n async encryptWithPassword(\n data: Uint8Array,\n password: string,\n ): Promise<Uint8Array> {\n try {\n const openpgp = await getOpenPGP();\n\n // Create a message from the data\n const message = await openpgp.createMessage({ binary: data });\n\n // Encrypt with password\n const encrypted = await openpgp.encrypt({\n message,\n passwords: [password],\n format: \"binary\",\n });\n\n return new Uint8Array(encrypted as ArrayBuffer);\n } catch (error) {\n throw wrapCryptoError(\"encryptWithPassword\", error);\n }\n }\n\n /**\n * Decrypts password-encrypted binary data.\n *\n * @param encryptedData - Password-encrypted data as Uint8Array.\n * Must be encrypted with the same password.\n * @param password - Password for decryption.\n * Must match the encryption password.\n * @returns Decrypted data as Uint8Array\n *\n * @throws {Error} If decryption fails or password is incorrect\n */\n async decryptWithPassword(\n encryptedData: Uint8Array,\n password: string,\n ): Promise<Uint8Array> {\n try {\n const openpgp = await getOpenPGP();\n\n // Read the encrypted message\n const message = await openpgp.readMessage({\n binaryMessage: encryptedData,\n });\n\n // Decrypt with password\n const { data } = await openpgp.decrypt({\n message,\n passwords: [password],\n format: \"binary\",\n });\n\n return new Uint8Array(data as ArrayBuffer);\n } catch (error) {\n throw wrapCryptoError(\"decryptWithPassword\", error);\n }\n }\n}\n\n/**\n * Implements PGP operations for browser environments.\n *\n * @remarks\n * Provides PGP encryption, decryption, and key generation using the OpenPGP.js\n * library with browser-optimized configuration.\n *\n * @internal\n */\nclass BrowserPGPAdapter implements VanaPGPAdapter {\n /**\n * Encrypts data using PGP public key encryption.\n *\n * @param data - The plaintext string to encrypt.\n * Typically messages or structured data.\n * @param publicKeyArmored - ASCII-armored PGP public key.\n * Obtain from PGP key generation or key servers.\n * @returns ASCII-armored encrypted message\n *\n * @throws {Error} If encryption fails or public key is invalid\n */\n async encrypt(data: string, publicKeyArmored: string): Promise<string> {\n try {\n const openpgp = await getOpenPGP();\n const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });\n\n const encrypted = await openpgp.encrypt({\n message: await openpgp.createMessage({ text: data }),\n encryptionKeys: publicKey,\n config: {\n preferredCompressionAlgorithm: openpgp.enums.compression.zlib,\n },\n });\n\n return encrypted as string;\n } catch (error) {\n throw new Error(`PGP encryption failed: ${String(error)}`);\n }\n }\n\n /**\n * Decrypts PGP-encrypted data using a private key.\n *\n * @param encryptedData - ASCII-armored encrypted message.\n * Must be encrypted with corresponding public key.\n * @param privateKeyArmored - ASCII-armored PGP private key.\n * Must correspond to the public key used for encryption.\n * @returns The decrypted plaintext string\n *\n * @throws {Error} If decryption fails or private key is invalid\n */\n async decrypt(\n encryptedData: string,\n privateKeyArmored: string,\n ): Promise<string> {\n try {\n const openpgp = await getOpenPGP();\n const privateKey = await openpgp.readPrivateKey({\n armoredKey: privateKeyArmored,\n });\n const message = await openpgp.readMessage({\n armoredMessage: encryptedData,\n });\n\n const { data: decrypted } = await openpgp.decrypt({\n message,\n decryptionKeys: privateKey,\n });\n\n return decrypted as string;\n } catch (error) {\n throw new Error(`PGP decryption failed: ${String(error)}`);\n }\n }\n\n /**\n * Generates a new PGP key pair.\n *\n * @param options - Key generation options\n * @param options.name - Name for the key identity.\n * Defaults to 'Vana User'.\n * @param options.email - Email for the key identity.\n * Defaults to 'user@vana.com'.\n * @param options.passphrase - Passphrase to protect the private key.\n * If not provided, key is unprotected.\n * @returns ASCII-armored public and private keys\n *\n * @throws {Error} If key generation fails\n */\n async generateKeyPair(options?: {\n name?: string;\n email?: string;\n passphrase?: string;\n }): Promise<{ publicKey: string; privateKey: string }> {\n try {\n const openpgp = await getOpenPGP();\n // Use shared utility to get standardized parameters\n const keyGenParams = getPGPKeyGenParams(options);\n\n const { privateKey, publicKey } = await openpgp.generateKey(keyGenParams);\n\n return { publicKey, privateKey };\n } catch (error) {\n throw wrapCryptoError(\"PGP key generation\", error);\n }\n }\n}\n\n/**\n * Implements HTTP operations for browser environments.\n *\n * @remarks\n * Uses the native Fetch API available in all modern browsers.\n *\n * @internal\n */\nclass BrowserHttpAdapter implements VanaHttpAdapter {\n /**\n * Performs an HTTP request using the Fetch API.\n *\n * @param url - The URL to fetch.\n * Must be a valid HTTP/HTTPS URL.\n * @param options - Standard fetch options.\n * See MDN fetch documentation for details.\n * @returns Standard fetch Response object\n */\n async fetch(url: string, options?: RequestInit): Promise<Response> {\n return fetch(url, options);\n }\n}\n\n/**\n * Implements secure caching for browser environments.\n *\n * @remarks\n * Uses sessionStorage for temporary caching that's automatically cleared\n * when the browser tab closes. This provides better security for sensitive\n * data like signatures compared to localStorage. All keys are prefixed to\n * avoid conflicts with other applications.\n *\n * @internal\n */\nclass BrowserCacheAdapter implements VanaCacheAdapter {\n private readonly prefix = \"vana_cache_\";\n\n /**\n * Retrieves a cached value by key.\n *\n * @param key - The cache key to look up.\n * Automatically prefixed to avoid conflicts.\n * @returns The cached value or null if not found\n */\n get(key: string): string | null {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return null;\n }\n return sessionStorage.getItem(this.prefix + key);\n } catch {\n return null;\n }\n }\n\n /**\n * Stores a value in sessionStorage.\n *\n * @param key - The cache key.\n * Automatically prefixed with 'vana_cache_'.\n * @param value - The value to cache.\n * Will be cleared when tab closes.\n */\n set(key: string, value: string): void {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return;\n }\n sessionStorage.setItem(this.prefix + key, value);\n } catch {\n // Ignore storage errors (quota exceeded, etc.)\n }\n }\n\n /**\n * Removes a specific key from the cache.\n *\n * @param key - The cache key to remove.\n * Only removes the prefixed key.\n */\n delete(key: string): void {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return;\n }\n sessionStorage.removeItem(this.prefix + key);\n } catch {\n // Ignore storage errors\n }\n }\n\n /**\n * Clears all Vana-prefixed cache entries.\n *\n * @remarks\n * Only removes entries with the 'vana_cache_' prefix,\n * preserving other sessionStorage data.\n */\n clear(): void {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return;\n }\n // Only clear our prefixed keys to avoid affecting other data\n const keysToRemove: string[] = [];\n for (let i = 0; i < sessionStorage.length; i++) {\n const key = sessionStorage.key(i);\n if (key?.startsWith(this.prefix)) {\n keysToRemove.push(key);\n }\n }\n keysToRemove.forEach((key) => {\n sessionStorage.removeItem(key);\n });\n } catch {\n // Ignore storage errors\n }\n }\n}\n\n/**\n * Provides complete platform abstraction for browser environments.\n *\n * @remarks\n * This adapter aggregates all browser-specific implementations of platform\n * operations. It uses native Web APIs where possible and browser-compatible\n * libraries for crypto and PGP operations. The adapter automatically selects\n * appropriate cryptographic implementations based on feature flags.\n *\n * @example\n * ```typescript\n * // Create a browser adapter instance\n * const adapter = new BrowserPlatformAdapter();\n *\n * // All platform operations are available\n * const encrypted = await adapter.crypto.encryptWithPublicKey(\n * 'secret',\n * publicKey\n * );\n *\n * const response = await adapter.http.fetch('/api/data');\n *\n * adapter.cache.set('key', 'value'); // Uses sessionStorage\n * ```\n *\n * @category Platform\n */\nexport class BrowserPlatformAdapter implements VanaPlatformAdapter {\n public readonly crypto = new BrowserCryptoAdapter();\n public readonly pgp = new BrowserPGPAdapter();\n public readonly http = new BrowserHttpAdapter();\n public readonly cache = new BrowserCacheAdapter();\n public readonly platform = \"browser\" as const;\n}\n"],"mappings":"AAqCA,SAAS,0BAA0B;AACnC,SAAS,uBAAuB;AAChC,SAAS,kBAAkB;AAC3B,SAAS,kCAAkC;AAC3C,SAAS,gCAAgC;AACzC,SAAS,OAAO,SAAS,eAAe,eAAe,cAAc;AACrE,YAAY,eAAe;AAC3B,SAAS,gBAAgB;AAGzB,SAAS,iCAAiC;AAG1C,MAAM,aAAa,WAAW,MAAM,OAAO,SAAS,CAAC;AACrD,MAAM,gBAAgB,WAAW,MAAM,OAAO,aAAa,CAAC;AAY5D,MAAM,qBAAkD;AAAA;AAAA,EAE9C,sBAAsB,IAAI,0BAA0B;AAAA,EACpD,sBAAsB,IAAI,2BAA2B;AAAA,IAC3D,eAAe,KAAK;AAAA,EACtB,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaD,MAAM,qBACJ,MACA,cACiB;AACjB,QAAI;AACF,UAAI,SAAS,gBAAgB;AAE3B,cAAM,cAAc,aAAa,WAAW,IAAI,IAC5C,eACA,KAAK,YAAY;AACrB,cAAM,iBAAiB,QAAQ,aAA8B,OAAO;AAGpE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AM,eAAyB,CAAC;AAChC,eAAS,IAAI,GAAG,IAAI,eAAe,QAAQ,KAAK;AAC9C,cAAM,MAAM,eAAe,IAAI,CAAC;AAChC,YAAI,KAAK,WAAW,KAAK,MAAM,GAAG;AAChC,uBAAa,KAAK,GAAG;AAAA,QACvB;AAAA,MACF;AACA,mBAAa,QAAQ,CAAC,QAAQ;AAC5B,uBAAe,WAAW,GAAG;AAAA,MAC/B,CAAC;AAAA,IACH,QAAQ;AAAA,IAER;AAAA,EACF;AACF;AA6BO,MAAM,uBAAsD;AAAA,EACjD,SAAS,IAAI,qBAAqB;AAAA,EAClC,MAAM,IAAI,kBAAkB;AAAA,EAC5B,OAAO,IAAI,mBAAmB;AAAA,EAC9B,QAAQ,IAAI,oBAAoB;AAAA,EAChC,WAAW;AAC7B;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/platform/browser.ts"],"sourcesContent":["/**\n * Provides browser-specific implementations of platform abstraction interfaces.\n *\n * @remarks\n * This module implements all platform-specific operations for browser environments,\n * using native Web APIs and browser-compatible libraries. It avoids Node.js-specific\n * dependencies like Buffer, using Uint8Array and viem utilities instead. Uses a\n * custom ECIES implementation with @noble/secp256k1 for pure JavaScript crypto.\n *\n * @example\n * ```typescript\n * // Use the browser platform adapter\n * import { BrowserPlatformAdapter } from '@vana-sdk/platform/browser';\n *\n * const adapter = new BrowserPlatformAdapter();\n *\n * // Encrypt data with public key\n * const encrypted = await adapter.crypto.encryptWithPublicKey(\n * 'sensitive data',\n * '0x04...' // Public key hex\n * );\n *\n * // Use sessionStorage-backed cache\n * adapter.cache.set('temp_key', 'cached_value');\n * ```\n *\n * @category Platform\n * @module platform/browser\n */\n\nimport type {\n VanaPlatformAdapter,\n VanaCryptoAdapter,\n VanaPGPAdapter,\n VanaHttpAdapter,\n VanaCacheAdapter,\n} from \"./interface\";\nimport { getPGPKeyGenParams } from \"./shared/pgp-utils\";\nimport { wrapCryptoError } from \"./shared/error-utils\";\nimport { lazyImport } from \"../utils/lazy-import\";\nimport { WalletKeyEncryptionService } from \"../crypto/services/WalletKeyEncryptionService\";\nimport { parseEncryptedDataBuffer } from \"../utils/crypto-utils\";\nimport { toHex, fromHex, stringToBytes, bytesToString, concat } from \"viem\";\nimport * as secp256k1 from \"@noble/secp256k1\";\n\n// Import browser ECIES provider\nimport { BrowserECIESUint8Provider } from \"../crypto/ecies/browser\";\n\n// Lazy-loaded dependencies to avoid Turbopack TDZ issues\nconst getOpenPGP = lazyImport(() => import(\"openpgp\"));\n\n/**\n * Implements cryptographic operations for browser environments.\n *\n * @remarks\n * Provides ECIES encryption/decryption, key generation, and password-based\n * encryption using a custom ECIES implementation with @noble/secp256k1.\n * Uses Uint8Array and viem utilities for browser compatibility.\n *\n * @internal\n */\nclass BrowserCryptoAdapter implements VanaCryptoAdapter {\n private eciesProvider = new BrowserECIESUint8Provider();\n private walletService = new WalletKeyEncryptionService({\n eciesProvider: this.eciesProvider,\n });\n\n /**\n * Encrypts data using ECIES with a public key.\n *\n * @param data - The plaintext string to encrypt.\n * Typically user data or sensitive information.\n * @param publicKeyHex - The recipient's public key in hex format.\n * Can include or omit the '0x' prefix.\n * @returns Encrypted data as a hex string without '0x' prefix\n *\n * @throws {Error} If encryption fails or public key is invalid\n */\n async encryptWithPublicKey(\n data: string,\n publicKeyHex: string,\n ): Promise<string> {\n try {\n const prefixedHex = publicKeyHex.startsWith(\"0x\")\n ? publicKeyHex\n : `0x${publicKeyHex}`;\n const publicKeyBytes = fromHex(prefixedHex as `0x${string}`, \"bytes\");\n\n // Encrypt data using ECIES\n const encrypted = await this.eciesProvider.encrypt(\n publicKeyBytes,\n stringToBytes(data),\n );\n\n // Concatenate all components and return as hex string\n const result = concat([\n encrypted.iv,\n encrypted.ephemPublicKey,\n encrypted.ciphertext,\n encrypted.mac,\n ]);\n\n return toHex(result).slice(2); // Remove '0x' prefix for backward compatibility\n } catch (error) {\n throw wrapCryptoError(\"encryptWithPublicKey\", error);\n }\n }\n\n /**\n * Decrypts ECIES-encrypted data using a private key.\n *\n * @param encryptedData - Hex string containing encrypted data.\n * Can include or omit the '0x' prefix.\n * @param privateKeyHex - The private key in hex format.\n * Must correspond to the public key used for encryption.\n * @returns The decrypted plaintext string\n *\n * @throws {Error} If decryption fails or MAC verification fails\n */\n async decryptWithPrivateKey(\n encryptedData: string,\n privateKeyHex: string,\n ): Promise<string> {\n try {\n const encryptedHex = encryptedData.startsWith(\"0x\")\n ? encryptedData\n : `0x${encryptedData}`;\n const privateHex = privateKeyHex.startsWith(\"0x\")\n ? privateKeyHex\n : `0x${privateKeyHex}`;\n const encryptedBytes = fromHex(encryptedHex as `0x${string}`, \"bytes\");\n const privateKeyBytes = fromHex(privateHex as `0x${string}`, \"bytes\");\n\n // Parse the encrypted data into components\n const encrypted = parseEncryptedDataBuffer(encryptedBytes);\n\n // Decrypt using ECIES\n const decrypted = await this.eciesProvider.decrypt(\n privateKeyBytes,\n encrypted,\n );\n\n return bytesToString(decrypted);\n } catch (error) {\n throw wrapCryptoError(\"decryptWithPrivateKey\", error);\n }\n }\n\n /**\n * Encrypts data using a wallet's public key.\n *\n * @param data - The plaintext string to encrypt.\n * Typically permission data or DLP metadata.\n * @param publicKey - The wallet's public key.\n * Automatically handles compressed/uncompressed formats.\n * @returns Encrypted data as a hex string\n *\n * @throws {Error} If encryption fails or key processing fails\n */\n async encryptWithWalletPublicKey(\n data: string,\n publicKey: string,\n ): Promise<string> {\n try {\n return await this.walletService.encryptWithWalletPublicKey(\n data,\n publicKey,\n );\n } catch (error) {\n throw wrapCryptoError(\"encryptWithWalletPublicKey\", error);\n }\n }\n\n /**\n * Decrypts data using a wallet's private key.\n *\n * @param encryptedData - Hex string containing encrypted data.\n * Must be encrypted with corresponding wallet public key.\n * @param privateKey - The wallet's private key.\n * Obtain from wallet connection (handle with care).\n * @returns The decrypted plaintext string\n *\n * @throws {Error} If decryption fails or key is invalid\n */\n async decryptWithWalletPrivateKey(\n encryptedData: string,\n privateKey: string,\n ): Promise<string> {\n try {\n return await this.walletService.decryptWithWalletPrivateKey(\n encryptedData,\n privateKey,\n );\n } catch (error) {\n throw wrapCryptoError(\"decryptWithWalletPrivateKey\", error);\n }\n }\n\n /**\n * Generates a new secp256k1 key pair for ECIES operations.\n *\n * @returns Object containing hex-encoded public and private keys\n * @returns returns.privateKey - Private key in hex format without '0x'\n * @returns returns.publicKey - Compressed public key in hex format without '0x'\n *\n * @throws {Error} If key generation fails\n */\n async generateKeyPair(): Promise<{\n privateKey: string;\n publicKey: string;\n }> {\n try {\n // Generate random private key\n const privateKeyBytes = secp256k1.utils.randomPrivateKey();\n\n // Generate public key (compressed for consistency with Node implementation)\n const publicKeyBytes = secp256k1.getPublicKey(privateKeyBytes, true);\n\n return {\n privateKey: toHex(privateKeyBytes).slice(2),\n publicKey: toHex(publicKeyBytes).slice(2),\n };\n } catch (error) {\n throw wrapCryptoError(\"generateKeyPair\", error);\n }\n }\n\n /**\n * Encrypts binary data using password-based encryption.\n *\n * @param data - Binary data to encrypt.\n * Typically file contents or serialized objects.\n * @param password - Password for encryption.\n * Often derived from wallet signatures.\n * @returns Encrypted data as Uint8Array\n *\n * @remarks\n * Uses OpenPGP for password-based encryption with automatic\n * salt generation for security.\n *\n * @throws {Error} If encryption fails\n */\n async encryptWithPassword(\n data: Uint8Array,\n password: string,\n ): Promise<Uint8Array> {\n try {\n const openpgp = await getOpenPGP();\n\n // Create a message from the data\n const message = await openpgp.createMessage({ binary: data });\n\n // Encrypt with password\n const encrypted = await openpgp.encrypt({\n message,\n passwords: [password],\n format: \"binary\",\n });\n\n return new Uint8Array(encrypted as ArrayBuffer);\n } catch (error) {\n throw wrapCryptoError(\"encryptWithPassword\", error);\n }\n }\n\n /**\n * Decrypts password-encrypted binary data.\n *\n * @param encryptedData - Password-encrypted data as Uint8Array.\n * Must be encrypted with the same password.\n * @param password - Password for decryption.\n * Must match the encryption password.\n * @returns Decrypted data as Uint8Array\n *\n * @throws {Error} If decryption fails or password is incorrect\n */\n async decryptWithPassword(\n encryptedData: Uint8Array,\n password: string,\n ): Promise<Uint8Array> {\n try {\n const openpgp = await getOpenPGP();\n\n // Read the encrypted message\n const message = await openpgp.readMessage({\n binaryMessage: encryptedData,\n });\n\n // Decrypt with password\n const { data } = await openpgp.decrypt({\n message,\n passwords: [password],\n format: \"binary\",\n });\n\n return new Uint8Array(data as ArrayBuffer);\n } catch (error) {\n throw wrapCryptoError(\"decryptWithPassword\", error);\n }\n }\n}\n\n/**\n * Implements PGP operations for browser environments.\n *\n * @remarks\n * Provides PGP encryption, decryption, and key generation using the OpenPGP.js\n * library with browser-optimized configuration.\n *\n * @internal\n */\nclass BrowserPGPAdapter implements VanaPGPAdapter {\n /**\n * Encrypts data using PGP public key encryption.\n *\n * @param data - The plaintext string to encrypt.\n * Typically messages or structured data.\n * @param publicKeyArmored - ASCII-armored PGP public key.\n * Obtain from PGP key generation or key servers.\n * @returns ASCII-armored encrypted message\n *\n * @throws {Error} If encryption fails or public key is invalid\n */\n async encrypt(data: string, publicKeyArmored: string): Promise<string> {\n try {\n const openpgp = await getOpenPGP();\n const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });\n\n const encrypted = await openpgp.encrypt({\n message: await openpgp.createMessage({ text: data }),\n encryptionKeys: publicKey,\n config: {\n preferredCompressionAlgorithm: openpgp.enums.compression.zlib,\n },\n });\n\n return encrypted as string;\n } catch (error) {\n throw new Error(`PGP encryption failed: ${String(error)}`);\n }\n }\n\n /**\n * Decrypts PGP-encrypted data using a private key.\n *\n * @param encryptedData - ASCII-armored encrypted message.\n * Must be encrypted with corresponding public key.\n * @param privateKeyArmored - ASCII-armored PGP private key.\n * Must correspond to the public key used for encryption.\n * @returns The decrypted plaintext string\n *\n * @throws {Error} If decryption fails or private key is invalid\n */\n async decrypt(\n encryptedData: string,\n privateKeyArmored: string,\n ): Promise<string> {\n try {\n const openpgp = await getOpenPGP();\n const privateKey = await openpgp.readPrivateKey({\n armoredKey: privateKeyArmored,\n });\n const message = await openpgp.readMessage({\n armoredMessage: encryptedData,\n });\n\n const { data: decrypted } = await openpgp.decrypt({\n message,\n decryptionKeys: privateKey,\n });\n\n return decrypted as string;\n } catch (error) {\n throw new Error(`PGP decryption failed: ${String(error)}`);\n }\n }\n\n /**\n * Generates a new PGP key pair.\n *\n * @param options - Key generation options\n * @param options.name - Name for the key identity.\n * Defaults to 'Vana User'.\n * @param options.email - Email for the key identity.\n * Defaults to 'user@vana.com'.\n * @param options.passphrase - Passphrase to protect the private key.\n * If not provided, key is unprotected.\n * @returns ASCII-armored public and private keys\n *\n * @throws {Error} If key generation fails\n */\n async generateKeyPair(options?: {\n name?: string;\n email?: string;\n passphrase?: string;\n }): Promise<{ publicKey: string; privateKey: string }> {\n try {\n const openpgp = await getOpenPGP();\n // Use shared utility to get standardized parameters\n const keyGenParams = getPGPKeyGenParams(options);\n\n const { privateKey, publicKey } = await openpgp.generateKey(keyGenParams);\n\n return { publicKey, privateKey };\n } catch (error) {\n throw wrapCryptoError(\"PGP key generation\", error);\n }\n }\n}\n\n/**\n * Implements HTTP operations for browser environments.\n *\n * @remarks\n * Uses the native Fetch API available in all modern browsers.\n *\n * @internal\n */\nclass BrowserHttpAdapter implements VanaHttpAdapter {\n /**\n * Performs an HTTP request using the Fetch API.\n *\n * @param url - The URL to fetch.\n * Must be a valid HTTP/HTTPS URL.\n * @param options - Standard fetch options.\n * See MDN fetch documentation for details.\n * @returns Standard fetch Response object\n */\n async fetch(url: string, options?: RequestInit): Promise<Response> {\n return fetch(url, options);\n }\n}\n\n/**\n * Implements secure caching for browser environments.\n *\n * @remarks\n * Uses sessionStorage for temporary caching that's automatically cleared\n * when the browser tab closes. This provides better security for sensitive\n * data like signatures compared to localStorage. All keys are prefixed to\n * avoid conflicts with other applications.\n *\n * @internal\n */\nclass BrowserCacheAdapter implements VanaCacheAdapter {\n private readonly prefix = \"vana_cache_\";\n\n /**\n * Retrieves a cached value by key.\n *\n * @param key - The cache key to look up.\n * Automatically prefixed to avoid conflicts.\n * @returns The cached value or null if not found\n */\n get(key: string): string | null {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return null;\n }\n return sessionStorage.getItem(this.prefix + key);\n } catch {\n return null;\n }\n }\n\n /**\n * Stores a value in sessionStorage.\n *\n * @param key - The cache key.\n * Automatically prefixed with 'vana_cache_'.\n * @param value - The value to cache.\n * Will be cleared when tab closes.\n */\n set(key: string, value: string): void {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return;\n }\n sessionStorage.setItem(this.prefix + key, value);\n } catch {\n // Ignore storage errors (quota exceeded, etc.)\n }\n }\n\n /**\n * Removes a specific key from the cache.\n *\n * @param key - The cache key to remove.\n * Only removes the prefixed key.\n */\n delete(key: string): void {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return;\n }\n sessionStorage.removeItem(this.prefix + key);\n } catch {\n // Ignore storage errors\n }\n }\n\n /**\n * Clears all Vana-prefixed cache entries.\n *\n * @remarks\n * Only removes entries with the 'vana_cache_' prefix,\n * preserving other sessionStorage data.\n */\n clear(): void {\n try {\n if (typeof sessionStorage === \"undefined\") {\n return;\n }\n // Only clear our prefixed keys to avoid affecting other data\n const keysToRemove: string[] = [];\n for (let i = 0; i < sessionStorage.length; i++) {\n const key = sessionStorage.key(i);\n if (key?.startsWith(this.prefix)) {\n keysToRemove.push(key);\n }\n }\n keysToRemove.forEach((key) => {\n sessionStorage.removeItem(key);\n });\n } catch {\n // Ignore storage errors\n }\n }\n}\n\n/**\n * Provides complete platform abstraction for browser environments.\n *\n * @remarks\n * This adapter aggregates all browser-specific implementations of platform\n * operations. It uses native Web APIs where possible and browser-compatible\n * libraries for crypto and PGP operations using a custom ECIES implementation\n * with @noble/secp256k1 for pure JavaScript cryptography.\n *\n * @example\n * ```typescript\n * // Create a browser adapter instance\n * const adapter = new BrowserPlatformAdapter();\n *\n * // All platform operations are available\n * const encrypted = await adapter.crypto.encryptWithPublicKey(\n * 'secret',\n * publicKey\n * );\n *\n * const response = await adapter.http.fetch('/api/data');\n *\n * adapter.cache.set('key', 'value'); // Uses sessionStorage\n * ```\n *\n * @category Platform\n */\nexport class BrowserPlatformAdapter implements VanaPlatformAdapter {\n public readonly crypto = new BrowserCryptoAdapter();\n public readonly pgp = new BrowserPGPAdapter();\n public readonly http = new BrowserHttpAdapter();\n public readonly cache = new BrowserCacheAdapter();\n public readonly platform = \"browser\" as const;\n}\n"],"mappings":"AAqCA,SAAS,0BAA0B;AACnC,SAAS,uBAAuB;AAChC,SAAS,kBAAkB;AAC3B,SAAS,kCAAkC;AAC3C,SAAS,gCAAgC;AACzC,SAAS,OAAO,SAAS,eAAe,eAAe,cAAc;AACrE,YAAY,eAAe;AAG3B,SAAS,iCAAiC;AAG1C,MAAM,aAAa,WAAW,MAAM,OAAO,SAAS,CAAC;AAYrD,MAAM,qBAAkD;AAAA,EAC9C,gBAAgB,IAAI,0BAA0B;AAAA,EAC9C,gBAAgB,IAAI,2BAA2B;AAAA,IACrD,eAAe,KAAK;AAAA,EACtB,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaD,MAAM,qBACJ,MACA,cACiB;AACjB,QAAI;AACF,YAAM,cAAc,aAAa,WAAW,IAAI,IAC5C,eACA,KAAK,YAAY;AACrB,YAAM,iBAAiB,QAAQ,aAA8B,OAAO;AAGpE,YAAM,YAAY,MAAM,KAAK,cAAc;AAAA,QACzC;AAAA,QACA,cAAc,IAAI;AAAA,MACpB;AAGA,YAAM,SAAS,OAAO;AAAA,QACpB,UAAU;AAAA,QACV,UAAU;AAAA,QACV,UAAU;AAAA,QACV,UAAU;AAAA,MACZ,CAAC;AAED,aAAO,MAAM,MAAM,EAAE,MAAM,CAAC;AAAA,IAC9B,SAAS,OAAO;AACd,YAAM,gBAAgB,wBAAwB,KAAK;AAAA,IACrD;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaA,MAAM,sBACJ,eACA,eACiB;AAC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