@opendatalabs/vana-sdk 0.1.0-alpha.606fa2d → 0.1.0-alpha.61efc06

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 (44) hide show
  1. package/README.md +4 -13
  2. package/dist/{browser-Bb8gLWHp.d.ts → browser-DY8XDblx.d.ts} +14 -61
  3. package/dist/browser.d.ts +1 -1
  4. package/dist/browser.js +106 -723
  5. package/dist/browser.js.map +1 -1
  6. package/dist/chains.browser.cjs +2 -2
  7. package/dist/chains.browser.cjs.map +1 -1
  8. package/dist/chains.browser.js +2 -2
  9. package/dist/chains.browser.js.map +1 -1
  10. package/dist/chains.cjs +2 -2
  11. package/dist/chains.cjs.map +1 -1
  12. package/dist/chains.js +2 -2
  13. package/dist/chains.js.map +1 -1
  14. package/dist/chains.node.cjs +2 -2
  15. package/dist/chains.node.cjs.map +1 -1
  16. package/dist/chains.node.js +2 -2
  17. package/dist/chains.node.js.map +1 -1
  18. package/dist/index.browser.d.ts +201 -342
  19. package/dist/index.browser.js +628 -861
  20. package/dist/index.browser.js.map +1 -1
  21. package/dist/index.node.cjs +666 -1010
  22. package/dist/index.node.cjs.map +1 -1
  23. package/dist/index.node.d.cts +205 -101
  24. package/dist/index.node.d.ts +205 -101
  25. package/dist/index.node.js +668 -1024
  26. package/dist/index.node.js.map +1 -1
  27. package/dist/node.cjs +53 -692
  28. package/dist/node.cjs.map +1 -1
  29. package/dist/node.js +52 -704
  30. package/dist/node.js.map +1 -1
  31. package/dist/platform.browser.d.ts +2 -2
  32. package/dist/platform.browser.js +116 -780
  33. package/dist/platform.browser.js.map +1 -1
  34. package/dist/platform.cjs +156 -933
  35. package/dist/platform.cjs.map +1 -1
  36. package/dist/platform.js +156 -945
  37. package/dist/platform.js.map +1 -1
  38. package/dist/platform.node.cjs +156 -933
  39. package/dist/platform.node.cjs.map +1 -1
  40. package/dist/platform.node.d.cts +14 -61
  41. package/dist/platform.node.d.ts +14 -61
  42. package/dist/platform.node.js +156 -945
  43. package/dist/platform.node.js.map +1 -1
  44. package/package.json +15 -31
@@ -6,6 +6,31 @@ var __esm = (fn, res) => function __init() {
6
6
  };
7
7
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
8
 
9
+ // src/platform/shared/crypto-utils.ts
10
+ function processWalletPublicKey(publicKey) {
11
+ const publicKeyHex = publicKey.startsWith("0x") ? publicKey.slice(2) : publicKey;
12
+ const publicKeyBytes = Buffer.from(publicKeyHex, "hex");
13
+ return publicKeyBytes.length === 64 ? Buffer.concat([Buffer.from([4]), publicKeyBytes]) : publicKeyBytes;
14
+ }
15
+ function processWalletPrivateKey(privateKey) {
16
+ const privateKeyHex = privateKey.startsWith("0x") ? privateKey.slice(2) : privateKey;
17
+ return Buffer.from(privateKeyHex, "hex");
18
+ }
19
+ function parseEncryptedDataBuffer(encryptedBuffer) {
20
+ return {
21
+ iv: encryptedBuffer.slice(0, 16),
22
+ ephemPublicKey: encryptedBuffer.slice(16, 81),
23
+ // 65 bytes for uncompressed public key
24
+ ciphertext: encryptedBuffer.slice(81, -32),
25
+ mac: encryptedBuffer.slice(-32)
26
+ };
27
+ }
28
+ var init_crypto_utils = __esm({
29
+ "src/platform/shared/crypto-utils.ts"() {
30
+ "use strict";
31
+ }
32
+ });
33
+
9
34
  // src/platform/shared/pgp-utils.ts
10
35
  function processPGPKeyOptions(options) {
11
36
  return {
@@ -67,811 +92,118 @@ var init_lazy_import = __esm({
67
92
  }
68
93
  });
69
94
 
70
- // src/utils/encoding.ts
71
- function toHex(data) {
72
- if (typeof Buffer !== "undefined" && Buffer.from) {
73
- return Buffer.from(data).toString("hex");
74
- }
75
- return Array.from(data, (byte) => byte.toString(16).padStart(2, "0")).join(
76
- ""
77
- );
78
- }
79
- function fromHex(hex) {
80
- const cleanHex = hex.startsWith("0x") ? hex.slice(2) : hex;
81
- if (cleanHex.length % 2 !== 0) {
82
- throw new Error("Invalid hex string: odd length");
83
- }
84
- if (!/^[0-9a-fA-F]*$/.test(cleanHex)) {
85
- throw new Error("Invalid hex string: contains non-hex characters");
86
- }
87
- if (typeof Buffer !== "undefined" && Buffer.from) {
88
- return new Uint8Array(Buffer.from(cleanHex, "hex"));
89
- }
90
- const bytes = new Uint8Array(cleanHex.length / 2);
91
- for (let i = 0; i < cleanHex.length; i += 2) {
92
- bytes[i / 2] = parseInt(cleanHex.substr(i, 2), 16);
93
- }
94
- return bytes;
95
- }
96
- function stringToBytes(str) {
97
- if (typeof Buffer !== "undefined" && Buffer.from) {
98
- return new Uint8Array(Buffer.from(str, "utf8"));
99
- }
100
- if (typeof TextEncoder !== "undefined") {
101
- return new TextEncoder().encode(str);
102
- }
103
- const bytes = [];
104
- for (let i = 0; i < str.length; i++) {
105
- const char = str.charCodeAt(i);
106
- if (char < 128) {
107
- bytes.push(char);
108
- } else if (char < 2048) {
109
- bytes.push(192 | char >> 6, 128 | char & 63);
110
- } else if (char < 55296 || char >= 57344) {
111
- bytes.push(
112
- 224 | char >> 12,
113
- 128 | char >> 6 & 63,
114
- 128 | char & 63
115
- );
116
- } else {
117
- i++;
118
- const char2 = str.charCodeAt(i);
119
- const codePoint = 65536 + ((char & 1023) << 10 | char2 & 1023);
120
- bytes.push(
121
- 240 | codePoint >> 18,
122
- 128 | codePoint >> 12 & 63,
123
- 128 | codePoint >> 6 & 63,
124
- 128 | codePoint & 63
125
- );
126
- }
127
- }
128
- return new Uint8Array(bytes);
129
- }
130
- function bytesToString(bytes) {
131
- if (typeof Buffer !== "undefined" && Buffer.from) {
132
- return Buffer.from(bytes).toString("utf8");
133
- }
134
- if (typeof TextDecoder !== "undefined") {
135
- return new TextDecoder().decode(bytes);
136
- }
137
- let str = "";
138
- let i = 0;
139
- while (i < bytes.length) {
140
- const byte = bytes[i];
141
- if (byte < 128) {
142
- str += String.fromCharCode(byte);
143
- i++;
144
- } else if ((byte & 224) === 192) {
145
- str += String.fromCharCode((byte & 31) << 6 | bytes[i + 1] & 63);
146
- i += 2;
147
- } else if ((byte & 240) === 224) {
148
- str += String.fromCharCode(
149
- (byte & 15) << 12 | (bytes[i + 1] & 63) << 6 | bytes[i + 2] & 63
150
- );
151
- i += 3;
152
- } else {
153
- const codePoint = ((byte & 7) << 18 | (bytes[i + 1] & 63) << 12 | (bytes[i + 2] & 63) << 6 | bytes[i + 3] & 63) - 65536;
154
- str += String.fromCharCode(
155
- 55296 + (codePoint >> 10),
156
- 56320 + (codePoint & 1023)
157
- );
158
- i += 4;
159
- }
160
- }
161
- return str;
162
- }
163
- var init_encoding = __esm({
164
- "src/utils/encoding.ts"() {
165
- "use strict";
166
- }
167
- });
168
-
169
- // src/utils/crypto-utils.ts
170
- function concatBytes(...arrays) {
171
- const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
172
- const result = new Uint8Array(totalLength);
173
- let offset = 0;
174
- for (const arr of arrays) {
175
- result.set(arr, offset);
176
- offset += arr.length;
177
- }
178
- return result;
179
- }
180
- function hexToBytes(hex) {
181
- return fromHex(hex);
182
- }
183
- function bytesToHex(bytes) {
184
- return toHex(bytes);
185
- }
186
- function processWalletPublicKey(publicKey) {
187
- const publicKeyHex = typeof publicKey === "string" ? publicKey.startsWith("0x") ? publicKey.slice(2) : publicKey : bytesToHex(publicKey);
188
- const publicKeyBytes = hexToBytes(publicKeyHex);
189
- return publicKeyBytes.length === 64 ? concatBytes(new Uint8Array([4]), publicKeyBytes) : publicKeyBytes;
190
- }
191
- function processWalletPrivateKey(privateKey) {
192
- const privateKeyHex = typeof privateKey === "string" ? privateKey.startsWith("0x") ? privateKey.slice(2) : privateKey : bytesToHex(privateKey);
193
- return hexToBytes(privateKeyHex);
194
- }
195
- function parseEncryptedDataBuffer(encryptedBuffer) {
196
- return {
197
- iv: encryptedBuffer.slice(0, 16),
198
- ephemPublicKey: encryptedBuffer.slice(16, 81),
199
- // 65 bytes for uncompressed public key
200
- ciphertext: encryptedBuffer.slice(81, -32),
201
- mac: encryptedBuffer.slice(-32)
202
- };
203
- }
204
- var init_crypto_utils = __esm({
205
- "src/utils/crypto-utils.ts"() {
206
- "use strict";
207
- init_encoding();
208
- }
209
- });
210
-
211
- // src/crypto/services/WalletKeyEncryptionService.ts
212
- var WalletKeyEncryptionService;
213
- var init_WalletKeyEncryptionService = __esm({
214
- "src/crypto/services/WalletKeyEncryptionService.ts"() {
215
- "use strict";
216
- init_crypto_utils();
217
- init_encoding();
218
- WalletKeyEncryptionService = class {
219
- constructor(config) {
220
- __publicField(this, "eciesProvider");
221
- this.eciesProvider = config.eciesProvider;
222
- }
223
- /**
224
- * Encrypts data using a wallet's public key.
225
- *
226
- * @param data - The plaintext message to encrypt for the wallet owner.
227
- * @param publicKey - The recipient wallet's public key for encryption.
228
- * @returns A promise that resolves to the encrypted data as a hex string.
229
- * @throws {Error} When encryption fails due to invalid key format.
230
- *
231
- * @example
232
- * ```typescript
233
- * const encrypted = await processor.encryptWithWalletPublicKey(
234
- * "Secret message",
235
- * "0x04..." // 65-byte uncompressed public key
236
- * );
237
- * console.log(`Encrypted: ${encrypted}`);
238
- * ```
239
- */
240
- async encryptWithWalletPublicKey(data, publicKey) {
241
- const publicKeyBytes = processWalletPublicKey(publicKey);
242
- const dataBytes = stringToBytes(data);
243
- const encrypted = await this.eciesProvider.encrypt(
244
- publicKeyBytes,
245
- dataBytes
246
- );
247
- const result = concatBytes(
248
- encrypted.iv,
249
- encrypted.ephemPublicKey,
250
- encrypted.ciphertext,
251
- encrypted.mac
252
- );
253
- return bytesToHex(result);
254
- }
255
- /**
256
- * Decrypts data using a wallet's private key.
257
- *
258
- * @param encryptedData - The hex-encoded encrypted data to decrypt.
259
- * @param privateKey - The wallet's private key for decryption.
260
- * @returns A promise that resolves to the decrypted plaintext message.
261
- * @throws {Error} When decryption fails due to invalid data or key format.
262
- *
263
- * @example
264
- * ```typescript
265
- * const decrypted = await processor.decryptWithWalletPrivateKey(
266
- * encryptedHexString,
267
- * "0x..." // 32-byte private key
268
- * );
269
- * console.log(`Decrypted: ${decrypted}`);
270
- * ```
271
- */
272
- async decryptWithWalletPrivateKey(encryptedData, privateKey) {
273
- const privateKeyBytes = processWalletPrivateKey(privateKey);
274
- const encryptedBytes = hexToBytes(encryptedData);
275
- const encrypted = parseEncryptedDataBuffer(encryptedBytes);
276
- const decrypted = await this.eciesProvider.decrypt(
277
- privateKeyBytes,
278
- encrypted
279
- );
280
- return bytesToString(decrypted);
281
- }
282
- /**
283
- * Encrypts a Uint8Array with a wallet public key
284
- *
285
- * @param data - Binary data to encrypt
286
- * @param publicKey - Public key as hex string or Uint8Array
287
- * @returns Encrypted data structure
288
- */
289
- async encryptBinary(data, publicKey) {
290
- const publicKeyBytes = processWalletPublicKey(publicKey);
291
- return this.eciesProvider.encrypt(publicKeyBytes, data);
292
- }
293
- /**
294
- * Decrypts to a Uint8Array with a wallet private key
295
- *
296
- * @param encrypted - Encrypted data structure
297
- * @param privateKey - Private key as hex string or Uint8Array
298
- * @returns Decrypted binary data
299
- */
300
- async decryptBinary(encrypted, privateKey) {
301
- const privateKeyBytes = processWalletPrivateKey(privateKey);
302
- return this.eciesProvider.decrypt(privateKeyBytes, encrypted);
303
- }
304
- /**
305
- * Gets the underlying ECIES provider
306
- *
307
- * @returns The ECIES provider instance
308
- */
309
- getECIESProvider() {
310
- return this.eciesProvider;
311
- }
312
- };
313
- }
314
- });
315
-
316
- // src/crypto/ecies/utils.ts
317
- function hexToBytes2(hex) {
318
- if (hex.length % 2 !== 0) {
319
- throw new Error("Hex string must have even length");
320
- }
321
- const bytes = new Uint8Array(hex.length / 2);
322
- for (let i = 0; i < hex.length; i += 2) {
323
- bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
324
- }
325
- return bytes;
326
- }
327
- function bytesToHex2(bytes) {
328
- return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
329
- }
330
- function stringToBytes2(str) {
331
- return new TextEncoder().encode(str);
332
- }
333
- function bytesToString2(bytes) {
334
- return new TextDecoder().decode(bytes);
335
- }
336
- function concatBytes2(...arrays) {
337
- const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
338
- const result = new Uint8Array(totalLength);
339
- let offset = 0;
340
- for (const arr of arrays) {
341
- result.set(arr, offset);
342
- offset += arr.length;
343
- }
344
- return result;
345
- }
346
- function constantTimeEqual(a, b) {
347
- if (a.length !== b.length) return false;
348
- let result = 0;
349
- for (let i = 0; i < a.length; i++) {
350
- result |= a[i] ^ b[i];
351
- }
352
- return result === 0;
353
- }
354
- var init_utils = __esm({
355
- "src/crypto/ecies/utils.ts"() {
356
- "use strict";
357
- }
358
- });
359
-
360
- // src/crypto/ecies/constants.ts
361
- var CURVE, CIPHER, KDF, MAC, FORMAT;
362
- var init_constants = __esm({
363
- "src/crypto/ecies/constants.ts"() {
364
- "use strict";
365
- CURVE = {
366
- /** The elliptic curve used (secp256k1 - same as Bitcoin/Ethereum) */
367
- name: "secp256k1",
368
- /** Private key length in bytes */
369
- PRIVATE_KEY_LENGTH: 32,
370
- /** Compressed public key length in bytes (0x02 or 0x03 prefix + 32 bytes) */
371
- COMPRESSED_PUBLIC_KEY_LENGTH: 33,
372
- /** Uncompressed public key length in bytes (0x04 prefix + 64 bytes) */
373
- UNCOMPRESSED_PUBLIC_KEY_LENGTH: 65,
374
- /** ECDH shared secret X coordinate length */
375
- SHARED_SECRET_LENGTH: 32,
376
- /** Public key prefixes */
377
- PREFIX: {
378
- /** Uncompressed public key prefix */
379
- UNCOMPRESSED: 4,
380
- /** Compressed public key prefix for even Y */
381
- COMPRESSED_EVEN: 2,
382
- /** Compressed public key prefix for odd Y */
383
- COMPRESSED_ODD: 3
384
- },
385
- /** X coordinate starts at byte 1 (after prefix) */
386
- X_COORDINATE_OFFSET: 1,
387
- /** X coordinate ends at byte 33 (1 + 32) */
388
- X_COORDINATE_END: 33
389
- };
390
- CIPHER = {
391
- /** Cipher algorithm - must match eccrypto */
392
- algorithm: "aes-256-cbc",
393
- /** AES key length in bytes */
394
- KEY_LENGTH: 32,
395
- /** Initialization vector length in bytes */
396
- IV_LENGTH: 16,
397
- /** Block size for AES */
398
- BLOCK_SIZE: 16
399
- };
400
- KDF = {
401
- /** Hash algorithm for key derivation - must match eccrypto */
402
- algorithm: "sha512",
403
- /** Output length of SHA-512 in bytes */
404
- OUTPUT_LENGTH: 64,
405
- /** Encryption key slice (first 32 bytes of KDF output) */
406
- ENCRYPTION_KEY_OFFSET: 0,
407
- ENCRYPTION_KEY_LENGTH: 32,
408
- /** MAC key slice (last 32 bytes of KDF output) */
409
- MAC_KEY_OFFSET: 32,
410
- MAC_KEY_LENGTH: 32
411
- };
412
- MAC = {
413
- /** MAC algorithm - must match eccrypto */
414
- algorithm: "sha256",
415
- /** HMAC-SHA256 output length in bytes */
416
- LENGTH: 32
417
- };
418
- FORMAT = {
419
- /** Offsets for each component in serialized format */
420
- IV_OFFSET: 0,
421
- IV_LENGTH: CIPHER.IV_LENGTH,
422
- /** Ephemeral public key (always uncompressed in eccrypto format) */
423
- EPHEMERAL_KEY_OFFSET: CIPHER.IV_LENGTH,
424
- EPHEMERAL_KEY_LENGTH: CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH,
425
- /** Ciphertext starts after IV and ephemeral key */
426
- CIPHERTEXT_OFFSET: CIPHER.IV_LENGTH + CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH,
427
- /** MAC is always the last 32 bytes */
428
- MAC_LENGTH: MAC.LENGTH,
429
- /** Minimum size of encrypted data (IV + ephemKey + MAC, no ciphertext) */
430
- MIN_ENCRYPTED_LENGTH: CIPHER.IV_LENGTH + CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH + MAC.LENGTH,
431
- /**
432
- * Helper to calculate total length of encrypted data
433
- *
434
- * @param ciphertextLength - Length of the ciphertext portion
435
- * @returns Total length including all components
436
- */
437
- getTotalLength: (ciphertextLength) => CIPHER.IV_LENGTH + CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH + ciphertextLength + MAC.LENGTH
438
- };
439
- }
440
- });
441
-
442
- // src/crypto/ecies/interface.ts
443
- function isECIESEncrypted(obj) {
444
- if (!obj || typeof obj !== "object") return false;
445
- const enc = obj;
446
- const isUint8Array = (value) => {
447
- return value instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(value);
448
- };
449
- return isUint8Array(enc.iv) && enc.iv.length === CIPHER.IV_LENGTH && isUint8Array(enc.ephemPublicKey) && (enc.ephemPublicKey.length === CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH || enc.ephemPublicKey.length === CURVE.COMPRESSED_PUBLIC_KEY_LENGTH) && isUint8Array(enc.ciphertext) && enc.ciphertext.length > 0 && isUint8Array(enc.mac) && enc.mac.length === MAC.LENGTH;
450
- }
451
- var ECIESError;
452
- var init_interface = __esm({
453
- "src/crypto/ecies/interface.ts"() {
454
- "use strict";
455
- init_constants();
456
- ECIESError = class extends Error {
457
- constructor(message, code, cause) {
458
- super(message);
459
- this.code = code;
460
- this.cause = cause;
461
- this.name = "ECIESError";
462
- }
463
- };
464
- }
465
- });
466
-
467
- // src/crypto/ecies/base.ts
468
- var _BaseECIESUint8, BaseECIESUint8;
469
- var init_base = __esm({
470
- "src/crypto/ecies/base.ts"() {
471
- "use strict";
472
- init_interface();
473
- init_constants();
474
- init_utils();
475
- _BaseECIESUint8 = class _BaseECIESUint8 {
476
- /**
477
- * Normalizes a public key to uncompressed format.
478
- *
479
- * @param publicKey - Public key in any format.
480
- * @returns Uncompressed public key (65 bytes).
481
- * @throws {ECIESError} If key format is invalid.
482
- */
483
- normalizePublicKey(publicKey) {
484
- if (_BaseECIESUint8.validatedKeys.has(publicKey)) {
485
- return publicKey;
486
- }
487
- if (publicKey.length === CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH) {
488
- if (publicKey[0] !== CURVE.PREFIX.UNCOMPRESSED) {
489
- throw new ECIESError(
490
- "Invalid uncompressed public key prefix",
491
- "INVALID_KEY"
492
- );
493
- }
494
- if (!this.validatePublicKey(publicKey)) {
495
- throw new ECIESError("Invalid public key", "INVALID_KEY");
496
- }
497
- _BaseECIESUint8.validatedKeys.set(publicKey, true);
498
- return publicKey;
499
- }
500
- if (publicKey.length === CURVE.COMPRESSED_PUBLIC_KEY_LENGTH) {
501
- const decompressed = this.decompressPublicKey(publicKey);
502
- if (!decompressed) {
503
- throw new ECIESError("Failed to decompress public key", "INVALID_KEY");
504
- }
505
- _BaseECIESUint8.validatedKeys.set(decompressed, true);
506
- return decompressed;
507
- }
508
- throw new ECIESError(
509
- `Invalid public key length: ${publicKey.length}`,
510
- "INVALID_KEY"
511
- );
512
- }
513
- /**
514
- * Encrypts data using ECIES.
515
- *
516
- * @param publicKey - The recipient's public key (compressed or uncompressed)
517
- * @param message - The data to encrypt
518
- * @returns Promise resolving to encrypted data structure
519
- */
520
- async encrypt(publicKey, message) {
521
- try {
522
- if (!(publicKey instanceof Uint8Array)) {
523
- throw new ECIESError("Public key must be a Uint8Array", "INVALID_KEY");
524
- }
525
- if (!(message instanceof Uint8Array)) {
526
- throw new ECIESError(
527
- "Message must be a Uint8Array",
528
- "ENCRYPTION_FAILED"
529
- );
530
- }
531
- if (publicKey.length === 0) {
532
- throw new ECIESError("Public key cannot be empty", "INVALID_KEY");
533
- }
534
- const pubKey = this.normalizePublicKey(publicKey);
535
- let ephemeralPrivateKey;
536
- do {
537
- ephemeralPrivateKey = this.generateRandomBytes(
538
- CURVE.PRIVATE_KEY_LENGTH
539
- );
540
- } while (!this.verifyPrivateKey(ephemeralPrivateKey));
541
- const ephemeralPublicKey = this.createPublicKey(
542
- ephemeralPrivateKey,
543
- false
544
- );
545
- if (!ephemeralPublicKey) {
546
- throw new ECIESError(
547
- "Failed to generate ephemeral public key",
548
- "ENCRYPTION_FAILED"
549
- );
550
- }
551
- const sharedSecret = this.performECDH(pubKey, ephemeralPrivateKey);
552
- const kdf = this.sha512(sharedSecret);
553
- const encryptionKey = kdf.slice(
554
- KDF.ENCRYPTION_KEY_OFFSET,
555
- KDF.ENCRYPTION_KEY_OFFSET + KDF.ENCRYPTION_KEY_LENGTH
556
- );
557
- const macKey = kdf.slice(
558
- KDF.MAC_KEY_OFFSET,
559
- KDF.MAC_KEY_OFFSET + KDF.MAC_KEY_LENGTH
560
- );
561
- const iv = this.generateRandomBytes(CIPHER.IV_LENGTH);
562
- const ciphertext = await this.aesEncrypt(encryptionKey, iv, message);
563
- const macData = concatBytes2(iv, ephemeralPublicKey, ciphertext);
564
- const mac = this.hmacSha256(macKey, macData);
565
- this.clearBuffer(ephemeralPrivateKey);
566
- this.clearBuffer(sharedSecret);
567
- this.clearBuffer(kdf);
568
- return {
569
- iv,
570
- ephemPublicKey: ephemeralPublicKey,
571
- ciphertext,
572
- mac
573
- };
574
- } catch (error) {
575
- if (error instanceof ECIESError) throw error;
576
- throw new ECIESError(
577
- `Encryption failed: ${error instanceof Error ? error.message : "Unknown error"}`,
578
- "ENCRYPTION_FAILED",
579
- error instanceof Error ? error : void 0
580
- );
581
- }
582
- }
583
- /**
584
- * Decrypts ECIES encrypted data.
585
- *
586
- * @param privateKey - The recipient's private key (32 bytes)
587
- * @param encrypted - The encrypted data structure from encrypt()
588
- * @returns Promise resolving to the original plaintext
589
- */
590
- async decrypt(privateKey, encrypted) {
591
- try {
592
- if (!(privateKey instanceof Uint8Array)) {
593
- throw new ECIESError("Private key must be a Uint8Array", "INVALID_KEY");
594
- }
595
- if (!isECIESEncrypted(encrypted)) {
596
- throw new ECIESError(
597
- "Invalid encrypted data structure",
598
- "DECRYPTION_FAILED"
599
- );
600
- }
601
- if (privateKey.length !== CURVE.PRIVATE_KEY_LENGTH) {
602
- throw new ECIESError(
603
- `Invalid private key length: ${privateKey.length}`,
604
- "INVALID_KEY"
605
- );
606
- }
607
- if (!this.verifyPrivateKey(privateKey)) {
608
- throw new ECIESError("Invalid private key", "INVALID_KEY");
609
- }
610
- const ephemeralPublicKey = this.normalizePublicKey(
611
- encrypted.ephemPublicKey
612
- );
613
- const sharedSecret = this.performECDH(ephemeralPublicKey, privateKey);
614
- const kdf = this.sha512(sharedSecret);
615
- const encryptionKey = kdf.slice(
616
- KDF.ENCRYPTION_KEY_OFFSET,
617
- KDF.ENCRYPTION_KEY_OFFSET + KDF.ENCRYPTION_KEY_LENGTH
618
- );
619
- const macKey = kdf.slice(
620
- KDF.MAC_KEY_OFFSET,
621
- KDF.MAC_KEY_OFFSET + KDF.MAC_KEY_LENGTH
622
- );
623
- const macData = concatBytes2(
624
- encrypted.iv,
625
- encrypted.ephemPublicKey,
626
- encrypted.ciphertext
627
- );
628
- const expectedMac = this.hmacSha256(macKey, macData);
629
- if (!constantTimeEqual(encrypted.mac, expectedMac)) {
630
- throw new ECIESError("MAC verification failed", "MAC_MISMATCH");
631
- }
632
- const decrypted = await this.aesDecrypt(
633
- encryptionKey,
634
- encrypted.iv,
635
- encrypted.ciphertext
636
- );
637
- this.clearBuffer(sharedSecret);
638
- this.clearBuffer(kdf);
639
- return decrypted;
640
- } catch (error) {
641
- if (error instanceof ECIESError) throw error;
642
- throw new ECIESError(
643
- `Decryption failed: ${error instanceof Error ? error.message : "Unknown error"}`,
644
- "DECRYPTION_FAILED",
645
- error instanceof Error ? error : void 0
646
- );
647
- }
648
- }
649
- /**
650
- * Clears sensitive data from memory using multi-pass overwrite.
651
- *
652
- * @remarks
653
- * Uses multiple passes with different patterns to make it harder
654
- * for JIT compilers to optimize away the operation. While not
655
- * guaranteed in JavaScript, this is a best-effort approach to
656
- * clear sensitive data from memory.
657
- *
658
- * @param buffer - The buffer to clear
659
- */
660
- clearBuffer(buffer) {
661
- if (buffer && buffer.length > 0) {
662
- buffer.fill(0);
663
- buffer.fill(255);
664
- buffer.fill(170);
665
- buffer.fill(0);
666
- for (let i = 0; i < buffer.length; i++) {
667
- buffer[i] = i & 255 ^ 90;
668
- }
669
- buffer.fill(0);
670
- }
671
- }
672
- };
673
- // Cache for validated public keys to avoid repeated validation
674
- __publicField(_BaseECIESUint8, "validatedKeys", /* @__PURE__ */ new WeakMap());
675
- BaseECIESUint8 = _BaseECIESUint8;
676
- }
677
- });
678
-
679
- // src/crypto/ecies/browser.ts
680
- import * as secp256k1 from "@noble/secp256k1";
681
- import { hmac } from "@noble/hashes/hmac";
682
- import { sha256, sha512 as nobleSha512 } from "@noble/hashes/sha2";
683
- var BrowserECIESUint8Provider;
684
- var init_browser = __esm({
685
- "src/crypto/ecies/browser.ts"() {
686
- "use strict";
687
- init_base();
688
- BrowserECIESUint8Provider = class extends BaseECIESUint8 {
689
- generateRandomBytes(length) {
690
- const bytes = new Uint8Array(length);
691
- crypto.getRandomValues(bytes);
692
- return bytes;
693
- }
694
- verifyPrivateKey(privateKey) {
695
- try {
696
- return secp256k1.utils.isValidPrivateKey(privateKey);
697
- } catch {
698
- return false;
699
- }
700
- }
701
- createPublicKey(privateKey, compressed) {
702
- try {
703
- return secp256k1.getPublicKey(privateKey, compressed);
704
- } catch {
705
- return null;
706
- }
707
- }
708
- validatePublicKey(publicKey) {
709
- try {
710
- secp256k1.Point.fromHex(publicKey);
711
- return true;
712
- } catch {
713
- return false;
714
- }
715
- }
716
- decompressPublicKey(publicKey) {
717
- try {
718
- const point = secp256k1.Point.fromHex(publicKey);
719
- return point.toRawBytes(false);
720
- } catch {
721
- return null;
722
- }
723
- }
724
- performECDH(publicKey, privateKey) {
725
- try {
726
- const sharedPoint = secp256k1.getSharedSecret(
727
- privateKey,
728
- publicKey,
729
- true
730
- );
731
- return sharedPoint.slice(1);
732
- } catch (error) {
733
- throw new Error(
734
- `ECDH failed: ${error instanceof Error ? error.message : "Unknown error"}`
735
- );
736
- }
737
- }
738
- sha512(data) {
739
- return nobleSha512(data);
740
- }
741
- hmacSha256(key, data) {
742
- return hmac(sha256, key, data);
743
- }
744
- async aesEncrypt(key, iv, plaintext) {
745
- const cryptoKey = await crypto.subtle.importKey(
746
- "raw",
747
- key,
748
- { name: "AES-CBC" },
749
- false,
750
- ["encrypt"]
751
- );
752
- const encrypted = await crypto.subtle.encrypt(
753
- { name: "AES-CBC", iv },
754
- cryptoKey,
755
- plaintext
756
- );
757
- return new Uint8Array(encrypted);
758
- }
759
- async aesDecrypt(key, iv, ciphertext) {
760
- const cryptoKey = await crypto.subtle.importKey(
761
- "raw",
762
- key,
763
- { name: "AES-CBC" },
764
- false,
765
- ["decrypt"]
766
- );
767
- const decrypted = await crypto.subtle.decrypt(
768
- { name: "AES-CBC", iv },
769
- cryptoKey,
770
- ciphertext
771
- );
772
- return new Uint8Array(decrypted);
773
- }
774
- };
775
- }
776
- });
777
-
778
95
  // src/platform/browser.ts
779
- import * as secp256k12 from "@noble/secp256k1";
780
- var getOpenPGP, BrowserCryptoAdapter, BrowserPGPAdapter, BrowserHttpAdapter, BrowserCacheAdapter, BrowserPlatformAdapter;
781
- var init_browser2 = __esm({
96
+ var getOpenPGP, BrowserCryptoAdapter, BrowserPGPAdapter, BrowserHttpAdapter, BrowserCacheAdapter, BrowserPlatformAdapter, browserPlatformAdapter;
97
+ var init_browser = __esm({
782
98
  "src/platform/browser.ts"() {
783
99
  "use strict";
100
+ init_crypto_utils();
784
101
  init_pgp_utils();
785
102
  init_error_utils();
786
103
  init_lazy_import();
787
- init_WalletKeyEncryptionService();
788
- init_crypto_utils();
789
- init_utils();
790
- init_browser();
791
104
  getOpenPGP = lazyImport(() => import("openpgp"));
792
105
  BrowserCryptoAdapter = class {
793
- constructor() {
794
- __publicField(this, "eciesProvider", new BrowserECIESUint8Provider());
795
- __publicField(this, "walletKeyEncryptionService", new WalletKeyEncryptionService({
796
- eciesProvider: this.eciesProvider
797
- }));
798
- }
799
106
  async encryptWithPublicKey(data, publicKeyHex) {
800
107
  try {
801
- const publicKeyBytes = hexToBytes2(publicKeyHex);
802
- const encrypted = await this.eciesProvider.encrypt(
803
- publicKeyBytes,
804
- stringToBytes2(data)
108
+ const eccrypto = await import("eccrypto-js");
109
+ const publicKeyBuffer = Buffer.from(publicKeyHex, "hex");
110
+ const encrypted = await eccrypto.encrypt(
111
+ publicKeyBuffer,
112
+ Buffer.from(data, "utf8")
805
113
  );
806
- const result = concatBytes2(
114
+ const result = Buffer.concat([
807
115
  encrypted.iv,
808
116
  encrypted.ephemPublicKey,
809
117
  encrypted.ciphertext,
810
118
  encrypted.mac
811
- );
812
- return bytesToHex2(result);
119
+ ]);
120
+ return result.toString("hex");
813
121
  } catch (error) {
814
- throw wrapCryptoError("encryptWithPublicKey", error);
122
+ throw new Error(`Encryption failed: ${error}`);
815
123
  }
816
124
  }
817
125
  async decryptWithPrivateKey(encryptedData, privateKeyHex) {
818
126
  try {
819
- const encryptedBytes = hexToBytes2(encryptedData);
820
- const privateKeyBytes = hexToBytes2(privateKeyHex);
821
- const encrypted = parseEncryptedDataBuffer(encryptedBytes);
822
- const decrypted = await this.eciesProvider.decrypt(
823
- privateKeyBytes,
824
- encrypted
127
+ const eccrypto = await import("eccrypto-js");
128
+ const privateKeyBuffer = processWalletPrivateKey(privateKeyHex);
129
+ const encryptedBuffer = Buffer.from(encryptedData, "hex");
130
+ const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
131
+ const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
132
+ const decryptedBuffer = await eccrypto.decrypt(
133
+ privateKeyBuffer,
134
+ encryptedObj
825
135
  );
826
- return bytesToString2(decrypted);
136
+ return decryptedBuffer.toString("utf8");
827
137
  } catch (error) {
828
- throw wrapCryptoError("decryptWithPrivateKey", error);
138
+ throw new Error(`Decryption failed: ${error}`);
829
139
  }
830
140
  }
831
- async encryptWithWalletPublicKey(data, publicKey) {
141
+ async generateKeyPair() {
832
142
  try {
833
- return await this.walletKeyEncryptionService.encryptWithWalletPublicKey(
834
- data,
835
- publicKey
836
- );
143
+ const eccrypto = await import("eccrypto-js");
144
+ const privateKeyBytes = new Uint8Array(32);
145
+ crypto.getRandomValues(privateKeyBytes);
146
+ const privateKey = Buffer.from(privateKeyBytes);
147
+ const publicKey = eccrypto.getPublicCompressed(privateKey);
148
+ return {
149
+ privateKey: privateKey.toString("hex"),
150
+ publicKey: publicKey.toString("hex")
151
+ };
837
152
  } catch (error) {
838
- throw wrapCryptoError("encryptWithWalletPublicKey", error);
153
+ throw wrapCryptoError("key generation", error);
839
154
  }
840
155
  }
841
- async decryptWithWalletPrivateKey(encryptedData, privateKey) {
156
+ async encryptWithWalletPublicKey(data, publicKey) {
842
157
  try {
843
- return await this.walletKeyEncryptionService.decryptWithWalletPrivateKey(
844
- encryptedData,
845
- privateKey
158
+ const eccrypto = await import("eccrypto-js");
159
+ const uncompressedKey = processWalletPublicKey(publicKey);
160
+ const encryptedBuffer = await eccrypto.encrypt(
161
+ uncompressedKey,
162
+ Buffer.from(data)
846
163
  );
164
+ const result = Buffer.concat([
165
+ encryptedBuffer.iv,
166
+ encryptedBuffer.ephemPublicKey,
167
+ encryptedBuffer.ciphertext,
168
+ encryptedBuffer.mac
169
+ ]);
170
+ return result.toString("hex");
847
171
  } catch (error) {
848
- throw wrapCryptoError("decryptWithWalletPrivateKey", error);
172
+ throw wrapCryptoError("encrypt with wallet public key", error);
849
173
  }
850
174
  }
851
- async generateKeyPair() {
175
+ async decryptWithWalletPrivateKey(encryptedData, privateKey) {
852
176
  try {
853
- const privateKeyBytes = secp256k12.utils.randomPrivateKey();
854
- const publicKeyBytes = secp256k12.getPublicKey(privateKeyBytes, true);
855
- return {
856
- privateKey: bytesToHex2(privateKeyBytes),
857
- publicKey: bytesToHex2(publicKeyBytes)
858
- };
177
+ const eccrypto = await import("eccrypto-js");
178
+ const privateKeyBuffer = processWalletPrivateKey(privateKey);
179
+ const encryptedBuffer = Buffer.from(encryptedData, "hex");
180
+ const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
181
+ const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
182
+ const decryptedBuffer = await eccrypto.decrypt(
183
+ privateKeyBuffer,
184
+ encryptedObj
185
+ );
186
+ return decryptedBuffer.toString("utf8");
859
187
  } catch (error) {
860
- throw wrapCryptoError("generateKeyPair", error);
188
+ throw wrapCryptoError("decrypt with wallet private key", error);
861
189
  }
862
190
  }
863
191
  async encryptWithPassword(data, password) {
864
192
  try {
865
193
  const openpgp = await getOpenPGP();
866
- const message = await openpgp.createMessage({ binary: data });
194
+ const message = await openpgp.createMessage({
195
+ binary: data
196
+ });
867
197
  const encrypted = await openpgp.encrypt({
868
198
  message,
869
199
  passwords: [password],
870
200
  format: "binary"
871
201
  });
872
- return new Uint8Array(encrypted);
202
+ const response = new Response(encrypted);
203
+ const arrayBuffer = await response.arrayBuffer();
204
+ return new Uint8Array(arrayBuffer);
873
205
  } catch (error) {
874
- throw wrapCryptoError("encryptWithPassword", error);
206
+ throw new Error(`Failed to encrypt with password: ${error}`);
875
207
  }
876
208
  }
877
209
  async decryptWithPassword(encryptedData, password) {
@@ -880,14 +212,14 @@ var init_browser2 = __esm({
880
212
  const message = await openpgp.readMessage({
881
213
  binaryMessage: encryptedData
882
214
  });
883
- const { data } = await openpgp.decrypt({
215
+ const { data: decrypted } = await openpgp.decrypt({
884
216
  message,
885
217
  passwords: [password],
886
218
  format: "binary"
887
219
  });
888
- return new Uint8Array(data);
220
+ return new Uint8Array(decrypted);
889
221
  } catch (error) {
890
- throw wrapCryptoError("decryptWithPassword", error);
222
+ throw new Error(`Failed to decrypt with password: ${error}`);
891
223
  }
892
224
  }
893
225
  };
@@ -939,6 +271,9 @@ var init_browser2 = __esm({
939
271
  };
940
272
  BrowserHttpAdapter = class {
941
273
  async fetch(url, options) {
274
+ if (typeof fetch === "undefined") {
275
+ throw new Error("Fetch API not available in this browser environment");
276
+ }
942
277
  return fetch(url, options);
943
278
  }
944
279
  };
@@ -958,19 +293,17 @@ var init_browser2 = __esm({
958
293
  }
959
294
  set(key, value) {
960
295
  try {
961
- if (typeof sessionStorage === "undefined") {
962
- return;
296
+ if (typeof sessionStorage !== "undefined") {
297
+ sessionStorage.setItem(this.prefix + key, value);
963
298
  }
964
- sessionStorage.setItem(this.prefix + key, value);
965
299
  } catch {
966
300
  }
967
301
  }
968
302
  delete(key) {
969
303
  try {
970
- if (typeof sessionStorage === "undefined") {
971
- return;
304
+ if (typeof sessionStorage !== "undefined") {
305
+ sessionStorage.removeItem(this.prefix + key);
972
306
  }
973
- sessionStorage.removeItem(this.prefix + key);
974
307
  } catch {
975
308
  }
976
309
  }
@@ -979,35 +312,38 @@ var init_browser2 = __esm({
979
312
  if (typeof sessionStorage === "undefined") {
980
313
  return;
981
314
  }
982
- const keysToRemove = [];
983
- for (let i = 0; i < sessionStorage.length; i++) {
984
- const key = sessionStorage.key(i);
985
- if (key?.startsWith(this.prefix)) {
986
- keysToRemove.push(key);
315
+ const keys = Object.keys(sessionStorage);
316
+ for (const key of keys) {
317
+ if (key.startsWith(this.prefix)) {
318
+ sessionStorage.removeItem(key);
987
319
  }
988
320
  }
989
- keysToRemove.forEach((key) => sessionStorage.removeItem(key));
990
321
  } catch {
991
322
  }
992
323
  }
993
324
  };
994
325
  BrowserPlatformAdapter = class {
995
326
  constructor() {
996
- __publicField(this, "crypto", new BrowserCryptoAdapter());
997
- __publicField(this, "pgp", new BrowserPGPAdapter());
998
- __publicField(this, "http", new BrowserHttpAdapter());
999
- __publicField(this, "cache", new BrowserCacheAdapter());
327
+ __publicField(this, "crypto");
328
+ __publicField(this, "pgp");
329
+ __publicField(this, "http");
330
+ __publicField(this, "cache");
1000
331
  __publicField(this, "platform", "browser");
332
+ this.crypto = new BrowserCryptoAdapter();
333
+ this.pgp = new BrowserPGPAdapter();
334
+ this.http = new BrowserHttpAdapter();
335
+ this.cache = new BrowserCacheAdapter();
1001
336
  }
1002
337
  };
338
+ browserPlatformAdapter = new BrowserPlatformAdapter();
1003
339
  }
1004
340
  });
1005
341
 
1006
342
  // src/platform.browser.ts
1007
- init_browser2();
343
+ init_browser();
1008
344
 
1009
345
  // src/platform/browser-only.ts
1010
- init_browser2();
346
+ init_browser();
1011
347
  function createBrowserPlatformAdapter() {
1012
348
  return new BrowserPlatformAdapter();
1013
349
  }