@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.
Files changed (37) hide show
  1. package/dist/config/features.cjs +1 -24
  2. package/dist/config/features.cjs.map +1 -1
  3. package/dist/config/features.d.ts +13 -44
  4. package/dist/config/features.js +1 -24
  5. package/dist/config/features.js.map +1 -1
  6. package/dist/controllers/permissions.cjs +19 -14
  7. package/dist/controllers/permissions.cjs.map +1 -1
  8. package/dist/controllers/permissions.js +19 -14
  9. package/dist/controllers/permissions.js.map +1 -1
  10. package/dist/crypto/ecies/index.cjs.map +1 -1
  11. package/dist/crypto/ecies/index.d.ts +10 -2
  12. package/dist/crypto/ecies/index.js.map +1 -1
  13. package/dist/index.browser.d.ts +2 -0
  14. package/dist/index.browser.js +10 -0
  15. package/dist/index.browser.js.map +1 -1
  16. package/dist/index.node.cjs +12 -0
  17. package/dist/index.node.cjs.map +1 -1
  18. package/dist/index.node.d.ts +3 -0
  19. package/dist/index.node.js +12 -0
  20. package/dist/index.node.js.map +1 -1
  21. package/dist/platform/browser.cjs +40 -119
  22. package/dist/platform/browser.cjs.map +1 -1
  23. package/dist/platform/browser.d.ts +7 -7
  24. package/dist/platform/browser.js +40 -119
  25. package/dist/platform/browser.js.map +1 -1
  26. package/dist/platform/node.cjs +51 -129
  27. package/dist/platform/node.cjs.map +1 -1
  28. package/dist/platform/node.d.ts +5 -5
  29. package/dist/platform/node.js +51 -129
  30. package/dist/platform/node.js.map +1 -1
  31. package/dist/types/permissions.cjs.map +1 -1
  32. package/dist/types/permissions.d.ts +2 -0
  33. package/package.json +1 -2
  34. package/dist/types/eccrypto-js.d.cjs +0 -2
  35. package/dist/types/eccrypto-js.d.cjs.map +0 -1
  36. package/dist/types/eccrypto-js.d.js +0 -1
  37. package/dist/types/eccrypto-js.d.js.map +0 -1
@@ -36,7 +36,6 @@ var import_pgp_utils = require("./shared/pgp-utils");
36
36
  var import_error_utils = require("./shared/error-utils");
37
37
  var import_stream_utils = require("./shared/stream-utils");
38
38
  var import_lazy_import = require("../utils/lazy-import");
39
- var import_features = require("../config/features");
40
39
  var import_WalletKeyEncryptionService = require("../crypto/services/WalletKeyEncryptionService");
41
40
  var import_crypto_utils = require("../utils/crypto-utils");
42
41
  var import_node = require("../crypto/ecies/node");
@@ -44,12 +43,10 @@ var import_interface = require("../crypto/ecies/interface");
44
43
  var import_crypto = require("crypto");
45
44
  var import_secp256k1 = __toESM(require("secp256k1"), 1);
46
45
  const getOpenPGP = (0, import_lazy_import.lazyImport)(() => import("openpgp"));
47
- const getEccryptoJS = (0, import_lazy_import.lazyImport)(() => import("eccrypto-js"));
48
46
  class NodeCryptoAdapter {
49
- // Initialize both providers - only one will be used based on feature flag
50
- customEciesProvider = new import_node.NodeECIESUint8Provider();
51
- customWalletService = new import_WalletKeyEncryptionService.WalletKeyEncryptionService({
52
- eciesProvider: this.customEciesProvider
47
+ eciesProvider = new import_node.NodeECIESUint8Provider();
48
+ walletService = new import_WalletKeyEncryptionService.WalletKeyEncryptionService({
49
+ eciesProvider: this.eciesProvider
53
50
  });
54
51
  /**
55
52
  * Encrypts data using ECIES with a public key.
@@ -64,39 +61,19 @@ class NodeCryptoAdapter {
64
61
  */
65
62
  async encryptWithPublicKey(data, publicKeyHex) {
66
63
  try {
67
- if (import_features.features.useCustomECIES) {
68
- const publicKey = Buffer.from(publicKeyHex, "hex");
69
- const message = Buffer.from(data, "utf8");
70
- const encrypted = await this.customEciesProvider.encrypt(
71
- publicKey,
72
- message
73
- );
74
- const result = Buffer.concat([
75
- encrypted.iv,
76
- encrypted.ephemPublicKey,
77
- encrypted.ciphertext,
78
- encrypted.mac
79
- ]);
80
- return result.toString("hex");
81
- } else {
82
- const eccryptojs = await getEccryptoJS();
83
- const publicKeyBytes = Buffer.from(publicKeyHex, "hex");
84
- const uncompressed = this.customEciesProvider.normalizeToUncompressed(
85
- new Uint8Array(publicKeyBytes)
86
- );
87
- const publicKey = Buffer.from(uncompressed);
88
- const message = Buffer.from(data, "utf8");
89
- const encrypted = await eccryptojs.encrypt(publicKey, message);
90
- const result = Buffer.concat([
91
- encrypted.iv,
92
- encrypted.ephemPublicKey,
93
- encrypted.ciphertext,
94
- encrypted.mac
95
- ]);
96
- return result.toString("hex");
97
- }
64
+ const publicKeyBytes = (0, import_crypto_utils.processWalletPublicKey)(publicKeyHex);
65
+ const publicKey = Buffer.from(publicKeyBytes);
66
+ const message = Buffer.from(data, "utf8");
67
+ const encrypted = await this.eciesProvider.encrypt(publicKey, message);
68
+ const result = Buffer.concat([
69
+ encrypted.iv,
70
+ encrypted.ephemPublicKey,
71
+ encrypted.ciphertext,
72
+ encrypted.mac
73
+ ]);
74
+ return result.toString("hex");
98
75
  } catch (error) {
99
- if (import_features.features.useCustomECIES && error instanceof import_interface.ECIESError) {
76
+ if (error instanceof import_interface.ECIESError) {
100
77
  throw error;
101
78
  }
102
79
  throw new Error(
@@ -118,36 +95,23 @@ class NodeCryptoAdapter {
118
95
  */
119
96
  async decryptWithPrivateKey(encryptedData, privateKeyHex) {
120
97
  try {
121
- if (import_features.features.useCustomECIES) {
122
- const privateKeyBuffer = (0, import_crypto_utils.processWalletPrivateKey)(privateKeyHex);
123
- const encryptedBuffer = Buffer.from(encryptedData, "hex");
124
- const { iv, ephemPublicKey, ciphertext, mac } = (0, import_crypto_utils.parseEncryptedDataBuffer)(encryptedBuffer);
125
- const encryptedObj = {
126
- iv,
127
- ephemPublicKey,
128
- ciphertext,
129
- mac
130
- };
131
- const decrypted = await this.customEciesProvider.decrypt(
132
- privateKeyBuffer,
133
- encryptedObj
134
- );
135
- return new TextDecoder().decode(decrypted);
136
- } else {
137
- const eccryptojs = await getEccryptoJS();
138
- const privateKey = Buffer.from(privateKeyHex, "hex");
139
- const encryptedBuffer = Buffer.from(encryptedData, "hex");
140
- const { iv, ephemPublicKey, ciphertext, mac } = (0, import_crypto_utils.parseEncryptedDataBuffer)(encryptedBuffer);
141
- const decrypted = await eccryptojs.decrypt(privateKey, {
142
- iv: Buffer.from(iv),
143
- ephemPublicKey: Buffer.from(ephemPublicKey),
144
- ciphertext: Buffer.from(ciphertext),
145
- mac: Buffer.from(mac)
146
- });
147
- return decrypted.toString("utf8");
148
- }
98
+ const privateKeyBuffer = (0, import_crypto_utils.processWalletPrivateKey)(privateKeyHex);
99
+ const encryptedHex = encryptedData.startsWith("0x") ? encryptedData.slice(2) : encryptedData;
100
+ const encryptedBuffer = Buffer.from(encryptedHex, "hex");
101
+ const { iv, ephemPublicKey, ciphertext, mac } = (0, import_crypto_utils.parseEncryptedDataBuffer)(encryptedBuffer);
102
+ const encryptedObj = {
103
+ iv,
104
+ ephemPublicKey,
105
+ ciphertext,
106
+ mac
107
+ };
108
+ const decrypted = await this.eciesProvider.decrypt(
109
+ privateKeyBuffer,
110
+ encryptedObj
111
+ );
112
+ return new TextDecoder().decode(decrypted);
149
113
  } catch (error) {
150
- if (import_features.features.useCustomECIES && error instanceof import_interface.ECIESError) {
114
+ if (error instanceof import_interface.ECIESError) {
151
115
  throw error;
152
116
  }
153
117
  throw new Error(
@@ -166,28 +130,18 @@ class NodeCryptoAdapter {
166
130
  */
167
131
  async generateKeyPair() {
168
132
  try {
169
- if (import_features.features.useCustomECIES) {
170
- const secp256k1 = import_secp256k1.default;
171
- let privateKey;
172
- do {
173
- privateKey = (0, import_crypto.randomBytes)(32);
174
- } while (!secp256k1.privateKeyVerify(privateKey));
175
- const publicKey = Buffer.from(
176
- secp256k1.publicKeyCreate(privateKey, true)
177
- );
178
- return {
179
- privateKey: privateKey.toString("hex"),
180
- publicKey: publicKey.toString("hex")
181
- };
182
- } else {
183
- const eccryptojs = await getEccryptoJS();
184
- const privateKey = eccryptojs.generatePrivate();
185
- const publicKey = eccryptojs.getPublic(privateKey);
186
- return {
187
- privateKey: privateKey.toString("hex"),
188
- publicKey: publicKey.toString("hex")
189
- };
190
- }
133
+ const secp256k1 = import_secp256k1.default;
134
+ let privateKey;
135
+ do {
136
+ privateKey = (0, import_crypto.randomBytes)(32);
137
+ } while (!secp256k1.privateKeyVerify(privateKey));
138
+ const publicKey = Buffer.from(
139
+ secp256k1.publicKeyCreate(privateKey, true)
140
+ );
141
+ return {
142
+ privateKey: privateKey.toString("hex"),
143
+ publicKey: publicKey.toString("hex")
144
+ };
191
145
  } catch (error) {
192
146
  throw (0, import_error_utils.wrapCryptoError)("key generation", error);
193
147
  }
@@ -205,26 +159,10 @@ class NodeCryptoAdapter {
205
159
  */
206
160
  async encryptWithWalletPublicKey(data, publicKey) {
207
161
  try {
208
- if (import_features.features.useCustomECIES) {
209
- return await this.customWalletService.encryptWithWalletPublicKey(
210
- data,
211
- publicKey
212
- );
213
- } else {
214
- const eccryptojs = await getEccryptoJS();
215
- const publicKeyBytes = (0, import_crypto_utils.processWalletPublicKey)(publicKey);
216
- const uncompressed = this.customEciesProvider.normalizeToUncompressed(publicKeyBytes);
217
- const publicKeyBuffer = Buffer.from(uncompressed);
218
- const message = Buffer.from(data, "utf8");
219
- const encrypted = await eccryptojs.encrypt(publicKeyBuffer, message);
220
- const result = Buffer.concat([
221
- encrypted.iv,
222
- encrypted.ephemPublicKey,
223
- encrypted.ciphertext,
224
- encrypted.mac
225
- ]);
226
- return result.toString("hex");
227
- }
162
+ return await this.walletService.encryptWithWalletPublicKey(
163
+ data,
164
+ publicKey
165
+ );
228
166
  } catch (error) {
229
167
  throw (0, import_error_utils.wrapCryptoError)("encrypt with wallet public key", error);
230
168
  }
@@ -242,26 +180,10 @@ class NodeCryptoAdapter {
242
180
  */
243
181
  async decryptWithWalletPrivateKey(encryptedData, privateKey) {
244
182
  try {
245
- if (import_features.features.useCustomECIES) {
246
- return await this.customWalletService.decryptWithWalletPrivateKey(
247
- encryptedData,
248
- privateKey
249
- );
250
- } else {
251
- const eccryptojs = await getEccryptoJS();
252
- const privateKeyBuffer = Buffer.from(
253
- (0, import_crypto_utils.processWalletPrivateKey)(privateKey)
254
- );
255
- const encryptedBuffer = Buffer.from(encryptedData, "hex");
256
- const { iv, ephemPublicKey, ciphertext, mac } = (0, import_crypto_utils.parseEncryptedDataBuffer)(encryptedBuffer);
257
- const decrypted = await eccryptojs.decrypt(privateKeyBuffer, {
258
- iv: Buffer.from(iv),
259
- ephemPublicKey: Buffer.from(ephemPublicKey),
260
- ciphertext: Buffer.from(ciphertext),
261
- mac: Buffer.from(mac)
262
- });
263
- return decrypted.toString("utf8");
264
- }
183
+ return await this.walletService.decryptWithWalletPrivateKey(
184
+ encryptedData,
185
+ privateKey
186
+ );
265
187
  } catch (error) {
266
188
  throw (0, import_error_utils.wrapCryptoError)("decrypt with wallet private key", error);
267
189
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/platform/node.ts"],"sourcesContent":["/**\n * Provides Node.js-specific implementations of platform abstraction interfaces.\n *\n * @remarks\n * This module implements all platform-specific operations for Node.js environments,\n * including cryptography, PGP operations, HTTP requests, and caching. It dynamically\n * imports dependencies to avoid Turbopack TDZ issues and supports both standard\n * eccrypto and custom ECIES implementations based on feature flags.\n *\n * WARNING: Dependencies that access globals during init MUST be dynamically imported\n * to support Turbopack. See: https://github.com/vercel/next.js/issues/82632\n *\n * @example\n * ```typescript\n * // Use the Node.js platform adapter\n * import { nodePlatformAdapter } from '@vana-sdk/platform/node';\n *\n * // Encrypt data with public key\n * const encrypted = await nodePlatformAdapter.crypto.encryptWithPublicKey(\n * 'sensitive data',\n * '0x04...' // Public key hex\n * );\n *\n * // Generate PGP key pair\n * const { publicKey, privateKey } = await nodePlatformAdapter.pgp.generateKeyPair({\n * name: 'Data Owner',\n * email: 'owner@example.com'\n * });\n * ```\n *\n * @category Platform\n * @module platform/node\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 { streamToUint8Array } from \"./shared/stream-utils\";\nimport { lazyImport } from \"../utils/lazy-import\";\nimport { features } from \"../config/features\";\nimport { WalletKeyEncryptionService } from \"../crypto/services/WalletKeyEncryptionService\";\nimport {\n processWalletPrivateKey,\n parseEncryptedDataBuffer,\n processWalletPublicKey,\n} from \"../utils/crypto-utils\";\n\n// Lazy-loaded dependencies to avoid Turbopack TDZ issues\nconst getOpenPGP = lazyImport(() => import(\"openpgp\"));\nconst getEccryptoJS = lazyImport(() => import(\"eccrypto-js\"));\n\n// Import both ECIES implementations statically\nimport { NodeECIESUint8Provider } from \"../crypto/ecies/node\";\nimport { ECIESError } from \"../crypto/ecies/interface\";\nimport type { ECIESEncrypted } from \"../crypto/ecies\";\nimport { randomBytes } from \"crypto\";\nimport secp256k1Import from \"secp256k1\";\n\n// Type definition for secp256k1 module\ninterface Secp256k1Module {\n privateKeyVerify(privateKey: Buffer): boolean;\n publicKeyCreate(privateKey: Buffer, compressed: boolean): Buffer;\n publicKeyVerify(publicKey: Buffer): boolean;\n publicKeyConvert(publicKey: Buffer, compressed: boolean): Buffer;\n ecdh(\n publicKey: Buffer,\n privateKey: Buffer,\n options: {\n hashfn: (x: Uint8Array, y: Uint8Array, output?: Uint8Array) => Uint8Array;\n },\n output: Buffer,\n ): Buffer;\n}\n\n/**\n * Implements cryptographic operations for Node.js 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 * The implementation choice is controlled by the `useCustomECIES` feature flag.\n *\n * @internal\n */\nclass NodeCryptoAdapter implements VanaCryptoAdapter {\n // Initialize both providers - only one will be used based on feature flag\n private customEciesProvider = new NodeECIESUint8Provider();\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 * Obtain from key generation or user profile.\n * @returns Encrypted data as a hex string containing IV, ephemeral key, ciphertext, and MAC\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 publicKey = Buffer.from(publicKeyHex, \"hex\");\n const message = Buffer.from(data, \"utf8\");\n\n const encrypted = await this.customEciesProvider.encrypt(\n publicKey,\n message,\n );\n\n // Concatenate all components and return as hex string for API consistency\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 } else {\n // Use eccrypto-js (default)\n const eccryptojs = await getEccryptoJS();\n const publicKeyBytes = Buffer.from(publicKeyHex, \"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 publicKey = Buffer.from(uncompressed);\n\n const message = Buffer.from(data, \"utf8\");\n\n const encrypted = await eccryptojs.encrypt(publicKey, message);\n\n // Concatenate all components and return as hex string for API consistency\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 if (features.useCustomECIES && error instanceof ECIESError) {\n throw error;\n }\n throw new Error(\n `Encryption failed: ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n }\n\n /**\n * Decrypts ECIES-encrypted data using a private key.\n *\n * @param encryptedData - Hex string containing encrypted data.\n * Must include IV, ephemeral public key, ciphertext, and MAC.\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 * @throws {ECIESError} If using custom ECIES and specific error occurs\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 privateKeyBuffer = processWalletPrivateKey(privateKeyHex);\n const encryptedBuffer = Buffer.from(encryptedData, \"hex\");\n const { iv, ephemPublicKey, ciphertext, mac } =\n parseEncryptedDataBuffer(encryptedBuffer);\n\n // Reconstruct the encrypted data structure\n const encryptedObj: ECIESEncrypted = {\n iv,\n ephemPublicKey,\n ciphertext,\n mac,\n };\n\n const decrypted = await this.customEciesProvider.decrypt(\n privateKeyBuffer,\n encryptedObj,\n );\n return new TextDecoder().decode(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 if (features.useCustomECIES && error instanceof ECIESError) {\n throw error;\n }\n throw new Error(\n `Decryption failed: ${error instanceof Error ? error.message : String(error)}`,\n );\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.publicKey - Compressed public key in hex format\n * @returns returns.privateKey - Private key in hex format\n *\n * @throws {Error} If key generation fails\n */\n async generateKeyPair(): Promise<{ publicKey: string; privateKey: string }> {\n try {\n if (features.useCustomECIES) {\n // Use custom implementation with secp256k1\n const secp256k1 = secp256k1Import as unknown as Secp256k1Module;\n\n // Generate private key\n let privateKey: Buffer;\n do {\n privateKey = randomBytes(32);\n } while (!secp256k1.privateKeyVerify(privateKey));\n\n // Get compressed public key\n const publicKey = Buffer.from(\n secp256k1.publicKeyCreate(privateKey, true),\n );\n\n return {\n privateKey: privateKey.toString(\"hex\"),\n publicKey: publicKey.toString(\"hex\"),\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(\"key generation\", 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 (with or without 0x prefix).\n * Obtain from wallet connection or user profile.\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 const publicKeyBytes = processWalletPublicKey(publicKey);\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 =\n this.customEciesProvider.normalizeToUncompressed(publicKeyBytes);\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(\"encrypt with wallet public key\", 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(\n processWalletPrivateKey(privateKey),\n );\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(\"decrypt with wallet private key\", 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. Note that this is not\n * deterministic due to OpenPGP's random salt generation.\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 const message = await openpgp.createMessage({\n binary: data,\n });\n\n // Use password-based encryption with wallet signature as password\n // Note: For deterministic encryption, we would need to control the salt\n // This implementation is secure but not deterministic due to OpenPGP's design\n const encrypted = await openpgp.encrypt({\n message,\n passwords: [password],\n format: \"binary\",\n });\n\n // In Node.js, the encrypted result is already a Uint8Array\n if (encrypted instanceof Uint8Array) {\n return encrypted;\n }\n\n // If it's a stream (should not happen with format: \"binary\"), read it\n if (\n encrypted &&\n typeof encrypted === \"object\" &&\n \"getReader\" in encrypted\n ) {\n return await streamToUint8Array(\n encrypted as ReadableStream<Uint8Array>,\n );\n }\n\n throw new Error(\"Unexpected encrypted data format\");\n } catch (error) {\n throw wrapCryptoError(\"encrypt with password\", 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 const message = await openpgp.readMessage({\n binaryMessage: encryptedData,\n });\n\n // Use password-based decryption with wallet signature as password\n const { data: decrypted } = await openpgp.decrypt({\n message,\n passwords: [password],\n format: \"binary\",\n });\n\n // Convert decrypted data back to Uint8Array\n return new Uint8Array(decrypted as ArrayBuffer);\n } catch (error) {\n throw wrapCryptoError(\"decrypt with password\", error);\n }\n }\n}\n\n/**\n * Implements PGP operations for Node.js environments.\n *\n * @remarks\n * Provides PGP encryption, decryption, and key generation using the OpenPGP.js\n * library with Node.js-specific optimizations like zlib compression.\n *\n * @internal\n */\nclass NodePGPAdapter 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 wrapCryptoError(\"PGP encryption\", 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 wrapCryptoError(\"PGP decryption\", 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 Node.js environments.\n *\n * @remarks\n * Provides fetch functionality using the global fetch if available,\n * suitable for Node.js 18+ or environments with fetch polyfills.\n *\n * @internal\n */\nclass NodeHttpAdapter implements VanaHttpAdapter {\n /**\n * Performs an HTTP request using fetch.\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 * @throws {Error} If fetch is not available in the environment\n */\n async fetch(url: string, options?: RequestInit): Promise<Response> {\n if (typeof globalThis.fetch !== \"undefined\") {\n return globalThis.fetch(url, options);\n }\n\n throw new Error(\"No fetch implementation available in Node.js environment\");\n }\n}\n\n/**\n * Implements in-memory caching for Node.js environments.\n *\n * @remarks\n * Provides a simple TTL-based cache using a Map. Cached values expire\n * after 2 hours by default. This cache is not persistent and will be\n * cleared when the process exits.\n *\n * @internal\n */\nclass NodeCacheAdapter implements VanaCacheAdapter {\n private cache = new Map<string, { value: string; expires: number }>();\n private readonly defaultTtl = 2 * 60 * 60 * 1000; // 2 hours in milliseconds\n\n /**\n * Retrieves a cached value by key.\n *\n * @param key - The cache key to look up.\n * Typically derived from operation parameters.\n * @returns The cached value or null if not found/expired\n */\n get(key: string): string | null {\n const entry = this.cache.get(key);\n if (!entry) {\n return null;\n }\n\n // Check if expired\n if (Date.now() > entry.expires) {\n this.cache.delete(key);\n return null;\n }\n\n return entry.value;\n }\n\n /**\n * Stores a value in the cache with TTL.\n *\n * @param key - The cache key.\n * Should be unique per operation.\n * @param value - The value to cache.\n * Typically serialized data or signatures.\n */\n set(key: string, value: string): void {\n this.cache.set(key, {\n value,\n expires: Date.now() + this.defaultTtl,\n });\n }\n\n /**\n * Removes a specific key from the cache.\n *\n * @param key - The cache key to remove.\n * Use when cached data becomes invalid.\n */\n delete(key: string): void {\n this.cache.delete(key);\n }\n\n /**\n * Clears all cached values.\n *\n * @remarks\n * Use with caution as this removes all cached signatures\n * and other performance optimizations.\n */\n clear(): void {\n this.cache.clear();\n }\n}\n\n/**\n * Provides complete platform abstraction for Node.js environments.\n *\n * @remarks\n * This adapter aggregates all Node.js-specific implementations of platform\n * operations. It automatically selects appropriate cryptographic implementations\n * based on feature flags and provides consistent APIs across all operations.\n *\n * @example\n * ```typescript\n * // Create a custom Node.js adapter instance\n * const adapter = new NodePlatformAdapter();\n *\n * // Use for encryption\n * const encrypted = await adapter.crypto.encryptWithPublicKey(\n * 'secret data',\n * publicKeyHex\n * );\n *\n * // Use for caching\n * adapter.cache.set('signature_key', signatureValue);\n * ```\n *\n * @category Platform\n */\nexport class NodePlatformAdapter implements VanaPlatformAdapter {\n crypto: VanaCryptoAdapter;\n pgp: VanaPGPAdapter;\n http: VanaHttpAdapter;\n cache: VanaCacheAdapter;\n platform: \"node\" = \"node\" as const;\n\n constructor() {\n this.crypto = new NodeCryptoAdapter();\n this.pgp = new NodePGPAdapter();\n this.http = new NodeHttpAdapter();\n this.cache = new NodeCacheAdapter();\n }\n}\n\n/**\n * Pre-configured Node.js platform adapter instance.\n *\n * @remarks\n * This singleton instance is the default adapter used by the SDK when\n * running in Node.js environments. It's automatically selected based on\n * platform detection.\n *\n * @example\n * ```typescript\n * import { nodePlatformAdapter } from '@vana-sdk/platform/node';\n *\n * // Use directly for platform operations\n * const keys = await nodePlatformAdapter.crypto.generateKeyPair();\n * ```\n *\n * @category Platform\n */\nexport const nodePlatformAdapter: VanaPlatformAdapter =\n new NodePlatformAdapter();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAyCA,uBAAmC;AACnC,yBAAgC;AAChC,0BAAmC;AACnC,yBAA2B;AAC3B,sBAAyB;AACzB,wCAA2C;AAC3C,0BAIO;AAOP,kBAAuC;AACvC,uBAA2B;AAE3B,oBAA4B;AAC5B,uBAA4B;AAR5B,MAAM,iBAAa,+BAAW,MAAM,OAAO,SAAS,CAAC;AACrD,MAAM,oBAAgB,+BAAW,MAAM,OAAO,aAAa,CAAC;AAmC5D,MAAM,kBAA+C;AAAA;AAAA,EAE3C,sBAAsB,IAAI,mCAAuB;AAAA,EACjD,sBAAsB,IAAI,6DAA2B;AAAA,IAC3D,eAAe,KAAK;AAAA,EACtB,CAAC;AAAA;AAAA;AAAA;AAAA;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ChC,QAAI,CAAC,OAAO;AACV,aAAO;AAAA,IACT;AAGA,QAAI,KAAK,IAAI,IAAI,MAAM,SAAS;AAC9B,WAAK,MAAM,OAAO,GAAG;AACrB,aAAO;AAAA,IACT;AAEA,WAAO,MAAM;AAAA,EACf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,IAAI,KAAa,OAAqB;AACpC,SAAK,MAAM,IAAI,KAAK;AAAA,MAClB;AAAA,MACA,SAAS,KAAK,IAAI,IAAI,KAAK;AAAA,IAC7B,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,KAAmB;AACxB,SAAK,MAAM,OAAO,GAAG;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,QAAc;AACZ,SAAK,MAAM,MAAM;AAAA,EACnB;AACF;AA2BO,MAAM,oBAAmD;AAAA,EAC9D;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAmB;AAAA,EAEnB,cAAc;AACZ,SAAK,SAAS,IAAI,kBAAkB;AACpC,SAAK,MAAM,IAAI,eAAe;AAC9B,SAAK,OAAO,IAAI,gBAAgB;AAChC,SAAK,QAAQ,IAAI,iBAAiB;AAAA,EACpC;AACF;AAoBO,MAAM,sBACX,IAAI,oBAAoB;","names":["secp256k1Import"]}
1
+ {"version":3,"sources":["../../src/platform/node.ts"],"sourcesContent":["/**\n * Provides Node.js-specific implementations of platform abstraction interfaces.\n *\n * @remarks\n * This module implements all platform-specific operations for Node.js environments,\n * including cryptography, PGP operations, HTTP requests, and caching. It dynamically\n * imports dependencies to avoid Turbopack TDZ issues and uses a custom ECIES\n * implementation with native secp256k1 for optimal performance.\n *\n * WARNING: Dependencies that access globals during init MUST be dynamically imported\n * to support Turbopack. See: https://github.com/vercel/next.js/issues/82632\n *\n * @example\n * ```typescript\n * // Use the Node.js platform adapter\n * import { nodePlatformAdapter} from '@vana-sdk/platform/node';\n *\n * // Encrypt data with public key\n * const encrypted = await nodePlatformAdapter.crypto.encryptWithPublicKey(\n * 'sensitive data',\n * '0x04...' // Public key hex\n * );\n *\n * // Generate PGP key pair\n * const { publicKey, privateKey } = await nodePlatformAdapter.pgp.generateKeyPair({\n * name: 'Data Owner',\n * email: 'owner@example.com'\n * });\n * ```\n *\n * @category Platform\n * @module platform/node\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 { streamToUint8Array } from \"./shared/stream-utils\";\nimport { lazyImport } from \"../utils/lazy-import\";\nimport { WalletKeyEncryptionService } from \"../crypto/services/WalletKeyEncryptionService\";\nimport {\n processWalletPrivateKey,\n parseEncryptedDataBuffer,\n processWalletPublicKey,\n} from \"../utils/crypto-utils\";\n\n// Lazy-loaded dependencies to avoid Turbopack TDZ issues\nconst getOpenPGP = lazyImport(() => import(\"openpgp\"));\n\n// Import ECIES implementation\nimport { NodeECIESUint8Provider } from \"../crypto/ecies/node\";\nimport { ECIESError } from \"../crypto/ecies/interface\";\nimport type { ECIESEncrypted } from \"../crypto/ecies\";\nimport { randomBytes } from \"crypto\";\nimport secp256k1Import from \"secp256k1\";\n\n// Type definition for secp256k1 module\ninterface Secp256k1Module {\n privateKeyVerify(privateKey: Buffer): boolean;\n publicKeyCreate(privateKey: Buffer, compressed: boolean): Buffer;\n publicKeyVerify(publicKey: Buffer): boolean;\n publicKeyConvert(publicKey: Buffer, compressed: boolean): Buffer;\n ecdh(\n publicKey: Buffer,\n privateKey: Buffer,\n options: {\n hashfn: (x: Uint8Array, y: Uint8Array, output?: Uint8Array) => Uint8Array;\n },\n output: Buffer,\n ): Buffer;\n}\n\n/**\n * Implements cryptographic operations for Node.js environments.\n *\n * @remarks\n * Provides ECIES encryption/decryption, key generation, and password-based\n * encryption using a custom ECIES implementation with native secp256k1.\n *\n * @internal\n */\nclass NodeCryptoAdapter implements VanaCryptoAdapter {\n private eciesProvider = new NodeECIESUint8Provider();\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 * Obtain from key generation or user profile.\n * @returns Encrypted data as a hex string containing IV, ephemeral key, ciphertext, and MAC\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 // Process public key to handle 0x prefix and convert to Buffer\n const publicKeyBytes = processWalletPublicKey(publicKeyHex);\n const publicKey = Buffer.from(publicKeyBytes);\n const message = Buffer.from(data, \"utf8\");\n\n const encrypted = await this.eciesProvider.encrypt(publicKey, message);\n\n // Concatenate all components and return as hex string for API consistency\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 } catch (error) {\n if (error instanceof ECIESError) {\n throw error;\n }\n throw new Error(\n `Encryption failed: ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n }\n\n /**\n * Decrypts ECIES-encrypted data using a private key.\n *\n * @param encryptedData - Hex string containing encrypted data.\n * Must include IV, ephemeral public key, ciphertext, and MAC.\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 * @throws {ECIESError} If using custom ECIES and specific error occurs\n */\n async decryptWithPrivateKey(\n encryptedData: string,\n privateKeyHex: string,\n ): Promise<string> {\n try {\n const privateKeyBuffer = processWalletPrivateKey(privateKeyHex);\n // Handle 0x prefix in encrypted data (e.g., from viem's toHex)\n const encryptedHex = encryptedData.startsWith(\"0x\")\n ? encryptedData.slice(2)\n : encryptedData;\n const encryptedBuffer = Buffer.from(encryptedHex, \"hex\");\n const { iv, ephemPublicKey, ciphertext, mac } =\n parseEncryptedDataBuffer(encryptedBuffer);\n\n // Reconstruct the encrypted data structure\n const encryptedObj: ECIESEncrypted = {\n iv,\n ephemPublicKey,\n ciphertext,\n mac,\n };\n\n const decrypted = await this.eciesProvider.decrypt(\n privateKeyBuffer,\n encryptedObj,\n );\n return new TextDecoder().decode(decrypted);\n } catch (error) {\n if (error instanceof ECIESError) {\n throw error;\n }\n throw new Error(\n `Decryption failed: ${error instanceof Error ? error.message : String(error)}`,\n );\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.publicKey - Compressed public key in hex format\n * @returns returns.privateKey - Private key in hex format\n *\n * @throws {Error} If key generation fails\n */\n async generateKeyPair(): Promise<{ publicKey: string; privateKey: string }> {\n try {\n const secp256k1 = secp256k1Import as unknown as Secp256k1Module;\n\n // Generate private key\n let privateKey: Buffer;\n do {\n privateKey = randomBytes(32);\n } while (!secp256k1.privateKeyVerify(privateKey));\n\n // Get compressed public key\n const publicKey = Buffer.from(\n secp256k1.publicKeyCreate(privateKey, true),\n );\n\n return {\n privateKey: privateKey.toString(\"hex\"),\n publicKey: publicKey.toString(\"hex\"),\n };\n } catch (error) {\n throw wrapCryptoError(\"key generation\", 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 (with or without 0x prefix).\n * Obtain from wallet connection or user profile.\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(\"encrypt with wallet public key\", 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(\"decrypt with wallet private key\", 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. Note that this is not\n * deterministic due to OpenPGP's random salt generation.\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 const message = await openpgp.createMessage({\n binary: data,\n });\n\n // Use password-based encryption with wallet signature as password\n // Note: For deterministic encryption, we would need to control the salt\n // This implementation is secure but not deterministic due to OpenPGP's design\n const encrypted = await openpgp.encrypt({\n message,\n passwords: [password],\n format: \"binary\",\n });\n\n // In Node.js, the encrypted result is already a Uint8Array\n if (encrypted instanceof Uint8Array) {\n return encrypted;\n }\n\n // If it's a stream (should not happen with format: \"binary\"), read it\n if (\n encrypted &&\n typeof encrypted === \"object\" &&\n \"getReader\" in encrypted\n ) {\n return await streamToUint8Array(\n encrypted as ReadableStream<Uint8Array>,\n );\n }\n\n throw new Error(\"Unexpected encrypted data format\");\n } catch (error) {\n throw wrapCryptoError(\"encrypt with password\", 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 const message = await openpgp.readMessage({\n binaryMessage: encryptedData,\n });\n\n // Use password-based decryption with wallet signature as password\n const { data: decrypted } = await openpgp.decrypt({\n message,\n passwords: [password],\n format: \"binary\",\n });\n\n // Convert decrypted data back to Uint8Array\n return new Uint8Array(decrypted as ArrayBuffer);\n } catch (error) {\n throw wrapCryptoError(\"decrypt with password\", error);\n }\n }\n}\n\n/**\n * Implements PGP operations for Node.js environments.\n *\n * @remarks\n * Provides PGP encryption, decryption, and key generation using the OpenPGP.js\n * library with Node.js-specific optimizations like zlib compression.\n *\n * @internal\n */\nclass NodePGPAdapter 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 wrapCryptoError(\"PGP encryption\", 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 wrapCryptoError(\"PGP decryption\", 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 Node.js environments.\n *\n * @remarks\n * Provides fetch functionality using the global fetch if available,\n * suitable for Node.js 18+ or environments with fetch polyfills.\n *\n * @internal\n */\nclass NodeHttpAdapter implements VanaHttpAdapter {\n /**\n * Performs an HTTP request using fetch.\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 * @throws {Error} If fetch is not available in the environment\n */\n async fetch(url: string, options?: RequestInit): Promise<Response> {\n if (typeof globalThis.fetch !== \"undefined\") {\n return globalThis.fetch(url, options);\n }\n\n throw new Error(\"No fetch implementation available in Node.js environment\");\n }\n}\n\n/**\n * Implements in-memory caching for Node.js environments.\n *\n * @remarks\n * Provides a simple TTL-based cache using a Map. Cached values expire\n * after 2 hours by default. This cache is not persistent and will be\n * cleared when the process exits.\n *\n * @internal\n */\nclass NodeCacheAdapter implements VanaCacheAdapter {\n private cache = new Map<string, { value: string; expires: number }>();\n private readonly defaultTtl = 2 * 60 * 60 * 1000; // 2 hours in milliseconds\n\n /**\n * Retrieves a cached value by key.\n *\n * @param key - The cache key to look up.\n * Typically derived from operation parameters.\n * @returns The cached value or null if not found/expired\n */\n get(key: string): string | null {\n const entry = this.cache.get(key);\n if (!entry) {\n return null;\n }\n\n // Check if expired\n if (Date.now() > entry.expires) {\n this.cache.delete(key);\n return null;\n }\n\n return entry.value;\n }\n\n /**\n * Stores a value in the cache with TTL.\n *\n * @param key - The cache key.\n * Should be unique per operation.\n * @param value - The value to cache.\n * Typically serialized data or signatures.\n */\n set(key: string, value: string): void {\n this.cache.set(key, {\n value,\n expires: Date.now() + this.defaultTtl,\n });\n }\n\n /**\n * Removes a specific key from the cache.\n *\n * @param key - The cache key to remove.\n * Use when cached data becomes invalid.\n */\n delete(key: string): void {\n this.cache.delete(key);\n }\n\n /**\n * Clears all cached values.\n *\n * @remarks\n * Use with caution as this removes all cached signatures\n * and other performance optimizations.\n */\n clear(): void {\n this.cache.clear();\n }\n}\n\n/**\n * Provides complete platform abstraction for Node.js environments.\n *\n * @remarks\n * This adapter aggregates all Node.js-specific implementations of platform\n * operations using a custom ECIES implementation with native secp256k1 for\n * optimal performance and provides consistent APIs across all operations.\n *\n * @example\n * ```typescript\n * // Create a custom Node.js adapter instance\n * const adapter = new NodePlatformAdapter();\n *\n * // Use for encryption\n * const encrypted = await adapter.crypto.encryptWithPublicKey(\n * 'secret data',\n * publicKeyHex\n * );\n *\n * // Use for caching\n * adapter.cache.set('signature_key', signatureValue);\n * ```\n *\n * @category Platform\n */\nexport class NodePlatformAdapter implements VanaPlatformAdapter {\n crypto: VanaCryptoAdapter;\n pgp: VanaPGPAdapter;\n http: VanaHttpAdapter;\n cache: VanaCacheAdapter;\n platform: \"node\" = \"node\" as const;\n\n constructor() {\n this.crypto = new NodeCryptoAdapter();\n this.pgp = new NodePGPAdapter();\n this.http = new NodeHttpAdapter();\n this.cache = new NodeCacheAdapter();\n }\n}\n\n/**\n * Pre-configured Node.js platform adapter instance.\n *\n * @remarks\n * This singleton instance is the default adapter used by the SDK when\n * running in Node.js environments. It's automatically selected based on\n * platform detection.\n *\n * @example\n * ```typescript\n * import { nodePlatformAdapter } from '@vana-sdk/platform/node';\n *\n * // Use directly for platform operations\n * const keys = await nodePlatformAdapter.crypto.generateKeyPair();\n * ```\n *\n * @category Platform\n */\nexport const nodePlatformAdapter: VanaPlatformAdapter =\n new NodePlatformAdapter();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAyCA,uBAAmC;AACnC,yBAAgC;AAChC,0BAAmC;AACnC,yBAA2B;AAC3B,wCAA2C;AAC3C,0BAIO;AAMP,kBAAuC;AACvC,uBAA2B;AAE3B,oBAA4B;AAC5B,uBAA4B;AAP5B,MAAM,iBAAa,+BAAW,MAAM,OAAO,SAAS,CAAC;AAkCrD,MAAM,kBAA+C;AAAA,EAC3C,gBAAgB,IAAI,mCAAuB;AAAA,EAC3C,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;AAEF,YAAM,qBAAiB,4CAAuB,YAAY;AAC1D,YAAM,YAAY,OAAO,KAAK,cAAc;AAC5C,YAAM,UAAU,OAAO,KAAK,MAAM,MAAM;AAExC,YAAM,YAAY,MAAM,KAAK,cAAc,QAAQ,WAAW,OAAO;AAGrE,YAAM,SAAS,OAAO,OAAO;AAAA,QAC3B,UAAU;AAAA,QACV,UAAU;AAAA,QACV,UAAU;AAAA,QACV,UAAU;AAAA,MACZ,CAAC;AAED,aAAO,OAAO,SAAS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MAAM,SAAS;AAC9B,WAAK,MAAM,OAAO,GAAG;AACrB,aAAO;AAAA,IACT;AAEA,WAAO,MAAM;AAAA,EACf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,IAAI,KAAa,OAAqB;AACpC,SAAK,MAAM,IAAI,KAAK;AAAA,MAClB;AAAA,MACA,SAAS,KAAK,IAAI,IAAI,KAAK;AAAA,IAC7B,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,KAAmB;AACxB,SAAK,MAAM,OAAO,GAAG;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,QAAc;AACZ,SAAK,MAAM,MAAM;AAAA,EACnB;AACF;AA2BO,MAAM,oBAAmD;AAAA,EAC9D;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAmB;AAAA,EAEnB,cAAc;AACZ,SAAK,SAAS,IAAI,kBAAkB;AACpC,SAAK,MAAM,IAAI,eAAe;AAC9B,SAAK,OAAO,IAAI,gBAAgB;AAChC,SAAK,QAAQ,IAAI,iBAAiB;AAAA,EACpC;AACF;AAoBO,MAAM,sBACX,IAAI,oBAAoB;","names":["secp256k1Import"]}
@@ -4,8 +4,8 @@
4
4
  * @remarks
5
5
  * This module implements all platform-specific operations for Node.js environments,
6
6
  * including cryptography, PGP operations, HTTP requests, and caching. It dynamically
7
- * imports dependencies to avoid Turbopack TDZ issues and supports both standard
8
- * eccrypto and custom ECIES implementations based on feature flags.
7
+ * imports dependencies to avoid Turbopack TDZ issues and uses a custom ECIES
8
+ * implementation with native secp256k1 for optimal performance.
9
9
  *
10
10
  * WARNING: Dependencies that access globals during init MUST be dynamically imported
11
11
  * to support Turbopack. See: https://github.com/vercel/next.js/issues/82632
@@ -13,7 +13,7 @@
13
13
  * @example
14
14
  * ```typescript
15
15
  * // Use the Node.js platform adapter
16
- * import { nodePlatformAdapter } from '@vana-sdk/platform/node';
16
+ * import { nodePlatformAdapter} from '@vana-sdk/platform/node';
17
17
  *
18
18
  * // Encrypt data with public key
19
19
  * const encrypted = await nodePlatformAdapter.crypto.encryptWithPublicKey(
@@ -37,8 +37,8 @@ import type { VanaPlatformAdapter, VanaCryptoAdapter, VanaPGPAdapter, VanaHttpAd
37
37
  *
38
38
  * @remarks
39
39
  * This adapter aggregates all Node.js-specific implementations of platform
40
- * operations. It automatically selects appropriate cryptographic implementations
41
- * based on feature flags and provides consistent APIs across all operations.
40
+ * operations using a custom ECIES implementation with native secp256k1 for
41
+ * optimal performance and provides consistent APIs across all operations.
42
42
  *
43
43
  * @example
44
44
  * ```typescript
@@ -2,7 +2,6 @@ import { getPGPKeyGenParams } from "./shared/pgp-utils";
2
2
  import { wrapCryptoError } from "./shared/error-utils";
3
3
  import { streamToUint8Array } from "./shared/stream-utils";
4
4
  import { lazyImport } from "../utils/lazy-import";
5
- import { features } from "../config/features";
6
5
  import { WalletKeyEncryptionService } from "../crypto/services/WalletKeyEncryptionService";
7
6
  import {
8
7
  processWalletPrivateKey,
@@ -10,16 +9,14 @@ import {
10
9
  processWalletPublicKey
11
10
  } from "../utils/crypto-utils";
12
11
  const getOpenPGP = lazyImport(() => import("openpgp"));
13
- const getEccryptoJS = lazyImport(() => import("eccrypto-js"));
14
12
  import { NodeECIESUint8Provider } from "../crypto/ecies/node";
15
13
  import { ECIESError } from "../crypto/ecies/interface";
16
14
  import { randomBytes } from "crypto";
17
15
  import secp256k1Import from "secp256k1";
18
16
  class NodeCryptoAdapter {
19
- // Initialize both providers - only one will be used based on feature flag
20
- customEciesProvider = new NodeECIESUint8Provider();
21
- customWalletService = new WalletKeyEncryptionService({
22
- eciesProvider: this.customEciesProvider
17
+ eciesProvider = new NodeECIESUint8Provider();
18
+ walletService = new WalletKeyEncryptionService({
19
+ eciesProvider: this.eciesProvider
23
20
  });
24
21
  /**
25
22
  * Encrypts data using ECIES with a public key.
@@ -34,39 +31,19 @@ class NodeCryptoAdapter {
34
31
  */
35
32
  async encryptWithPublicKey(data, publicKeyHex) {
36
33
  try {
37
- if (features.useCustomECIES) {
38
- const publicKey = Buffer.from(publicKeyHex, "hex");
39
- const message = Buffer.from(data, "utf8");
40
- const encrypted = await this.customEciesProvider.encrypt(
41
- publicKey,
42
- message
43
- );
44
- const result = Buffer.concat([
45
- encrypted.iv,
46
- encrypted.ephemPublicKey,
47
- encrypted.ciphertext,
48
- encrypted.mac
49
- ]);
50
- return result.toString("hex");
51
- } else {
52
- const eccryptojs = await getEccryptoJS();
53
- const publicKeyBytes = Buffer.from(publicKeyHex, "hex");
54
- const uncompressed = this.customEciesProvider.normalizeToUncompressed(
55
- new Uint8Array(publicKeyBytes)
56
- );
57
- const publicKey = Buffer.from(uncompressed);
58
- const message = Buffer.from(data, "utf8");
59
- const encrypted = await eccryptojs.encrypt(publicKey, message);
60
- const result = Buffer.concat([
61
- encrypted.iv,
62
- encrypted.ephemPublicKey,
63
- encrypted.ciphertext,
64
- encrypted.mac
65
- ]);
66
- return result.toString("hex");
67
- }
34
+ const publicKeyBytes = processWalletPublicKey(publicKeyHex);
35
+ const publicKey = Buffer.from(publicKeyBytes);
36
+ const message = Buffer.from(data, "utf8");
37
+ const encrypted = await this.eciesProvider.encrypt(publicKey, message);
38
+ const result = Buffer.concat([
39
+ encrypted.iv,
40
+ encrypted.ephemPublicKey,
41
+ encrypted.ciphertext,
42
+ encrypted.mac
43
+ ]);
44
+ return result.toString("hex");
68
45
  } catch (error) {
69
- if (features.useCustomECIES && error instanceof ECIESError) {
46
+ if (error instanceof ECIESError) {
70
47
  throw error;
71
48
  }
72
49
  throw new Error(
@@ -88,36 +65,23 @@ class NodeCryptoAdapter {
88
65
  */
89
66
  async decryptWithPrivateKey(encryptedData, privateKeyHex) {
90
67
  try {
91
- if (features.useCustomECIES) {
92
- const privateKeyBuffer = processWalletPrivateKey(privateKeyHex);
93
- const encryptedBuffer = Buffer.from(encryptedData, "hex");
94
- const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
95
- const encryptedObj = {
96
- iv,
97
- ephemPublicKey,
98
- ciphertext,
99
- mac
100
- };
101
- const decrypted = await this.customEciesProvider.decrypt(
102
- privateKeyBuffer,
103
- encryptedObj
104
- );
105
- return new TextDecoder().decode(decrypted);
106
- } else {
107
- const eccryptojs = await getEccryptoJS();
108
- const privateKey = Buffer.from(privateKeyHex, "hex");
109
- const encryptedBuffer = Buffer.from(encryptedData, "hex");
110
- const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
111
- const decrypted = await eccryptojs.decrypt(privateKey, {
112
- iv: Buffer.from(iv),
113
- ephemPublicKey: Buffer.from(ephemPublicKey),
114
- ciphertext: Buffer.from(ciphertext),
115
- mac: Buffer.from(mac)
116
- });
117
- return decrypted.toString("utf8");
118
- }
68
+ const privateKeyBuffer = processWalletPrivateKey(privateKeyHex);
69
+ const encryptedHex = encryptedData.startsWith("0x") ? encryptedData.slice(2) : encryptedData;
70
+ const encryptedBuffer = Buffer.from(encryptedHex, "hex");
71
+ const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
72
+ const encryptedObj = {
73
+ iv,
74
+ ephemPublicKey,
75
+ ciphertext,
76
+ mac
77
+ };
78
+ const decrypted = await this.eciesProvider.decrypt(
79
+ privateKeyBuffer,
80
+ encryptedObj
81
+ );
82
+ return new TextDecoder().decode(decrypted);
119
83
  } catch (error) {
120
- if (features.useCustomECIES && error instanceof ECIESError) {
84
+ if (error instanceof ECIESError) {
121
85
  throw error;
122
86
  }
123
87
  throw new Error(
@@ -136,28 +100,18 @@ class NodeCryptoAdapter {
136
100
  */
137
101
  async generateKeyPair() {
138
102
  try {
139
- if (features.useCustomECIES) {
140
- const secp256k1 = secp256k1Import;
141
- let privateKey;
142
- do {
143
- privateKey = randomBytes(32);
144
- } while (!secp256k1.privateKeyVerify(privateKey));
145
- const publicKey = Buffer.from(
146
- secp256k1.publicKeyCreate(privateKey, true)
147
- );
148
- return {
149
- privateKey: privateKey.toString("hex"),
150
- publicKey: publicKey.toString("hex")
151
- };
152
- } else {
153
- const eccryptojs = await getEccryptoJS();
154
- const privateKey = eccryptojs.generatePrivate();
155
- const publicKey = eccryptojs.getPublic(privateKey);
156
- return {
157
- privateKey: privateKey.toString("hex"),
158
- publicKey: publicKey.toString("hex")
159
- };
160
- }
103
+ const secp256k1 = secp256k1Import;
104
+ let privateKey;
105
+ do {
106
+ privateKey = randomBytes(32);
107
+ } while (!secp256k1.privateKeyVerify(privateKey));
108
+ const publicKey = Buffer.from(
109
+ secp256k1.publicKeyCreate(privateKey, true)
110
+ );
111
+ return {
112
+ privateKey: privateKey.toString("hex"),
113
+ publicKey: publicKey.toString("hex")
114
+ };
161
115
  } catch (error) {
162
116
  throw wrapCryptoError("key generation", error);
163
117
  }
@@ -175,26 +129,10 @@ class NodeCryptoAdapter {
175
129
  */
176
130
  async encryptWithWalletPublicKey(data, publicKey) {
177
131
  try {
178
- if (features.useCustomECIES) {
179
- return await this.customWalletService.encryptWithWalletPublicKey(
180
- data,
181
- publicKey
182
- );
183
- } else {
184
- const eccryptojs = await getEccryptoJS();
185
- const publicKeyBytes = processWalletPublicKey(publicKey);
186
- const uncompressed = this.customEciesProvider.normalizeToUncompressed(publicKeyBytes);
187
- const publicKeyBuffer = Buffer.from(uncompressed);
188
- const message = Buffer.from(data, "utf8");
189
- const encrypted = await eccryptojs.encrypt(publicKeyBuffer, message);
190
- const result = Buffer.concat([
191
- encrypted.iv,
192
- encrypted.ephemPublicKey,
193
- encrypted.ciphertext,
194
- encrypted.mac
195
- ]);
196
- return result.toString("hex");
197
- }
132
+ return await this.walletService.encryptWithWalletPublicKey(
133
+ data,
134
+ publicKey
135
+ );
198
136
  } catch (error) {
199
137
  throw wrapCryptoError("encrypt with wallet public key", error);
200
138
  }
@@ -212,26 +150,10 @@ class NodeCryptoAdapter {
212
150
  */
213
151
  async decryptWithWalletPrivateKey(encryptedData, privateKey) {
214
152
  try {
215
- if (features.useCustomECIES) {
216
- return await this.customWalletService.decryptWithWalletPrivateKey(
217
- encryptedData,
218
- privateKey
219
- );
220
- } else {
221
- const eccryptojs = await getEccryptoJS();
222
- const privateKeyBuffer = Buffer.from(
223
- processWalletPrivateKey(privateKey)
224
- );
225
- const encryptedBuffer = Buffer.from(encryptedData, "hex");
226
- const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
227
- const decrypted = await eccryptojs.decrypt(privateKeyBuffer, {
228
- iv: Buffer.from(iv),
229
- ephemPublicKey: Buffer.from(ephemPublicKey),
230
- ciphertext: Buffer.from(ciphertext),
231
- mac: Buffer.from(mac)
232
- });
233
- return decrypted.toString("utf8");
234
- }
153
+ return await this.walletService.decryptWithWalletPrivateKey(
154
+ encryptedData,
155
+ privateKey
156
+ );
235
157
  } catch (error) {
236
158
  throw wrapCryptoError("decrypt with wallet private key", error);
237
159
  }