@opendatalabs/vana-sdk 0.1.0-alpha.e64ec83 → 0.1.0-alpha.ee24d86

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 (43) hide show
  1. package/README.md +211 -436
  2. package/dist/chains.browser.cjs +5 -2
  3. package/dist/chains.browser.cjs.map +1 -1
  4. package/dist/chains.browser.d.cts +2 -1
  5. package/dist/chains.browser.d.ts +2 -1
  6. package/dist/chains.browser.js +4 -2
  7. package/dist/chains.browser.js.map +1 -1
  8. package/dist/chains.cjs +5 -2
  9. package/dist/chains.cjs.map +1 -1
  10. package/dist/chains.d.cts +1 -1
  11. package/dist/chains.d.ts +1 -1
  12. package/dist/chains.js +4 -2
  13. package/dist/chains.js.map +1 -1
  14. package/dist/chains.node.cjs +5 -2
  15. package/dist/chains.node.cjs.map +1 -1
  16. package/dist/chains.node.d.cts +1 -1
  17. package/dist/chains.node.d.ts +1 -1
  18. package/dist/chains.node.js +4 -2
  19. package/dist/chains.node.js.map +1 -1
  20. package/dist/index.browser.d.ts +10186 -4394
  21. package/dist/index.browser.js +5534 -2049
  22. package/dist/index.browser.js.map +1 -1
  23. package/dist/index.d.cts +1 -31277
  24. package/dist/index.node.cjs +5618 -2099
  25. package/dist/index.node.cjs.map +1 -1
  26. package/dist/index.node.d.cts +10211 -4391
  27. package/dist/index.node.d.ts +10211 -4391
  28. package/dist/index.node.js +5549 -2032
  29. package/dist/index.node.js.map +1 -1
  30. package/dist/platform.browser.d.ts +69 -0
  31. package/dist/platform.browser.js +79 -181
  32. package/dist/platform.browser.js.map +1 -1
  33. package/dist/platform.cjs +122 -197
  34. package/dist/platform.cjs.map +1 -1
  35. package/dist/platform.js +122 -197
  36. package/dist/platform.js.map +1 -1
  37. package/dist/platform.node.cjs +122 -197
  38. package/dist/platform.node.cjs.map +1 -1
  39. package/dist/platform.node.d.cts +70 -0
  40. package/dist/platform.node.d.ts +70 -0
  41. package/dist/platform.node.js +122 -197
  42. package/dist/platform.node.js.map +1 -1
  43. package/package.json +24 -18
package/dist/platform.js CHANGED
@@ -27,11 +27,6 @@ function parseEncryptedDataBuffer(encryptedBuffer) {
27
27
  mac: encryptedBuffer.slice(-32)
28
28
  };
29
29
  }
30
- function uint8ArrayToHex(array) {
31
- return Array.from(array, (byte) => byte.toString(16).padStart(2, "0")).join(
32
- ""
33
- );
34
- }
35
30
  var init_crypto_utils = __esm({
36
31
  "src/platform/shared/crypto-utils.ts"() {
37
32
  "use strict";
@@ -87,147 +82,59 @@ __export(browser_exports, {
87
82
  browserPlatformAdapter: () => browserPlatformAdapter
88
83
  });
89
84
  import * as openpgp from "openpgp";
90
- function hexToUint8Array(hex) {
91
- const result = new Uint8Array(hex.length / 2);
92
- for (let i = 0; i < hex.length; i += 2) {
93
- result[i / 2] = parseInt(hex.substr(i, 2), 16);
94
- }
95
- return result;
96
- }
97
- var BrowserECDH, BrowserCryptoAdapter, BrowserPGPAdapter, BrowserHttpAdapter, BrowserPlatformAdapter, browserPlatformAdapter;
85
+ var BrowserCryptoAdapter, BrowserPGPAdapter, BrowserHttpAdapter, BrowserCacheAdapter, BrowserPlatformAdapter, browserPlatformAdapter;
98
86
  var init_browser = __esm({
99
87
  "src/platform/browser.ts"() {
100
88
  "use strict";
101
89
  init_crypto_utils();
102
90
  init_pgp_utils();
103
91
  init_error_utils();
104
- BrowserECDH = class {
105
- async generateKeyPair() {
106
- const keyPair = await crypto.subtle.generateKey(
107
- {
108
- name: "ECDH",
109
- namedCurve: "P-256"
110
- },
111
- true,
112
- ["deriveKey", "deriveBits"]
113
- );
114
- return keyPair;
115
- }
116
- async encrypt(publicKeyHex, message) {
117
- const ephemeralKeyPair = await this.generateKeyPair();
118
- const publicKeyData = hexToUint8Array(publicKeyHex);
119
- const importedPublicKey = await crypto.subtle.importKey(
120
- "raw",
121
- publicKeyData,
122
- { name: "ECDH", namedCurve: "P-256" },
123
- false,
124
- []
125
- );
126
- const sharedKey = await crypto.subtle.deriveKey(
127
- { name: "ECDH", public: importedPublicKey },
128
- ephemeralKeyPair.privateKey,
129
- { name: "AES-GCM", length: 256 },
130
- false,
131
- ["encrypt"]
132
- );
133
- const iv = crypto.getRandomValues(new Uint8Array(12));
134
- const encoder = new TextEncoder();
135
- const data = encoder.encode(message);
136
- const encrypted = await crypto.subtle.encrypt(
137
- { name: "AES-GCM", iv },
138
- sharedKey,
139
- data
140
- );
141
- const ephemeralPublicKeyData = await crypto.subtle.exportKey(
142
- "raw",
143
- ephemeralKeyPair.publicKey
144
- );
145
- return JSON.stringify({
146
- encrypted: Array.from(new Uint8Array(encrypted)),
147
- iv: Array.from(iv),
148
- ephemeralPublicKey: Array.from(new Uint8Array(ephemeralPublicKeyData)),
149
- publicKey: publicKeyHex
150
- });
151
- }
152
- async decrypt(privateKeyHex, encryptedData) {
153
- try {
154
- const data = JSON.parse(encryptedData);
155
- if (!data.encrypted || !data.iv || !data.ephemeralPublicKey) {
156
- throw new Error("Invalid encrypted data format");
157
- }
158
- const privateKeyData = hexToUint8Array(privateKeyHex);
159
- const importedPrivateKey = await crypto.subtle.importKey(
160
- "pkcs8",
161
- privateKeyData,
162
- { name: "ECDH", namedCurve: "P-256" },
163
- false,
164
- ["deriveKey"]
165
- );
166
- const ephemeralPublicKey = await crypto.subtle.importKey(
167
- "raw",
168
- new Uint8Array(data.ephemeralPublicKey),
169
- { name: "ECDH", namedCurve: "P-256" },
170
- false,
171
- []
172
- );
173
- const sharedKey = await crypto.subtle.deriveKey(
174
- { name: "ECDH", public: ephemeralPublicKey },
175
- importedPrivateKey,
176
- { name: "AES-GCM", length: 256 },
177
- false,
178
- ["decrypt"]
179
- );
180
- const decrypted = await crypto.subtle.decrypt(
181
- { name: "AES-GCM", iv: new Uint8Array(data.iv) },
182
- sharedKey,
183
- new Uint8Array(data.encrypted)
184
- );
185
- return new TextDecoder().decode(decrypted);
186
- } catch (error) {
187
- throw new Error(
188
- `Decryption failed: ${error instanceof Error ? error.message : "Unknown error"}`
189
- );
190
- }
191
- }
192
- };
193
92
  BrowserCryptoAdapter = class {
194
93
  async encryptWithPublicKey(data, publicKeyHex) {
195
94
  try {
196
- const ecdh = new BrowserECDH();
197
- return await ecdh.encrypt(publicKeyHex, data);
95
+ const eccrypto2 = await import("eccrypto-js");
96
+ const publicKeyBuffer = Buffer.from(publicKeyHex, "hex");
97
+ const encrypted = await eccrypto2.encrypt(
98
+ publicKeyBuffer,
99
+ Buffer.from(data, "utf8")
100
+ );
101
+ const result = Buffer.concat([
102
+ encrypted.iv,
103
+ encrypted.ephemPublicKey,
104
+ encrypted.ciphertext,
105
+ encrypted.mac
106
+ ]);
107
+ return result.toString("hex");
198
108
  } catch (error) {
199
109
  throw new Error(`Encryption failed: ${error}`);
200
110
  }
201
111
  }
202
112
  async decryptWithPrivateKey(encryptedData, privateKeyHex) {
203
113
  try {
204
- const ecdh = new BrowserECDH();
205
- return await ecdh.decrypt(privateKeyHex, encryptedData);
114
+ const eccrypto2 = await import("eccrypto-js");
115
+ const privateKeyBuffer = processWalletPrivateKey(privateKeyHex);
116
+ const encryptedBuffer = Buffer.from(encryptedData, "hex");
117
+ const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
118
+ const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
119
+ const decryptedBuffer = await eccrypto2.decrypt(
120
+ privateKeyBuffer,
121
+ encryptedObj
122
+ );
123
+ return decryptedBuffer.toString("utf8");
206
124
  } catch (error) {
207
125
  throw new Error(`Decryption failed: ${error}`);
208
126
  }
209
127
  }
210
128
  async generateKeyPair() {
211
129
  try {
212
- const keyPair = await crypto.subtle.generateKey(
213
- {
214
- name: "ECDH",
215
- namedCurve: "P-256"
216
- },
217
- true,
218
- ["deriveKey", "deriveBits"]
219
- );
220
- const publicKeyBuffer = await crypto.subtle.exportKey(
221
- "raw",
222
- keyPair.publicKey
223
- );
224
- const privateKeyBuffer = await crypto.subtle.exportKey(
225
- "pkcs8",
226
- keyPair.privateKey
227
- );
130
+ const eccrypto2 = await import("eccrypto-js");
131
+ const privateKeyBytes = new Uint8Array(32);
132
+ crypto.getRandomValues(privateKeyBytes);
133
+ const privateKey = Buffer.from(privateKeyBytes);
134
+ const publicKey = eccrypto2.getPublicCompressed(privateKey);
228
135
  return {
229
- publicKey: uint8ArrayToHex(new Uint8Array(publicKeyBuffer)),
230
- privateKey: uint8ArrayToHex(new Uint8Array(privateKeyBuffer))
136
+ privateKey: privateKey.toString("hex"),
137
+ publicKey: publicKey.toString("hex")
231
138
  };
232
139
  } catch (error) {
233
140
  throw wrapCryptoError("key generation", error);
@@ -237,65 +144,9 @@ var init_browser = __esm({
237
144
  try {
238
145
  const eccrypto2 = await import("eccrypto-js");
239
146
  const uncompressedKey = processWalletPublicKey(publicKey);
240
- const iv = Buffer.from([
241
- 1,
242
- 2,
243
- 3,
244
- 4,
245
- 5,
246
- 6,
247
- 7,
248
- 8,
249
- 9,
250
- 10,
251
- 11,
252
- 12,
253
- 13,
254
- 14,
255
- 15,
256
- 16
257
- ]);
258
- const ephemeralKey = Buffer.from([
259
- 17,
260
- 34,
261
- 51,
262
- 68,
263
- 85,
264
- 102,
265
- 119,
266
- 136,
267
- 153,
268
- 170,
269
- 187,
270
- 204,
271
- 221,
272
- 238,
273
- 255,
274
- 0,
275
- 16,
276
- 32,
277
- 48,
278
- 64,
279
- 80,
280
- 96,
281
- 112,
282
- 128,
283
- 144,
284
- 160,
285
- 176,
286
- 192,
287
- 208,
288
- 224,
289
- 240,
290
- 0
291
- ]);
292
147
  const encryptedBuffer = await eccrypto2.encrypt(
293
148
  uncompressedKey,
294
- Buffer.from(data),
295
- {
296
- iv,
297
- ephemPrivateKey: ephemeralKey
298
- }
149
+ Buffer.from(data)
299
150
  );
300
151
  const result = Buffer.concat([
301
152
  encryptedBuffer.iv,
@@ -410,15 +261,60 @@ var init_browser = __esm({
410
261
  return fetch(url, options);
411
262
  }
412
263
  };
264
+ BrowserCacheAdapter = class {
265
+ prefix = "vana_cache_";
266
+ get(key) {
267
+ try {
268
+ if (typeof sessionStorage === "undefined") {
269
+ return null;
270
+ }
271
+ return sessionStorage.getItem(this.prefix + key);
272
+ } catch {
273
+ return null;
274
+ }
275
+ }
276
+ set(key, value) {
277
+ try {
278
+ if (typeof sessionStorage !== "undefined") {
279
+ sessionStorage.setItem(this.prefix + key, value);
280
+ }
281
+ } catch {
282
+ }
283
+ }
284
+ delete(key) {
285
+ try {
286
+ if (typeof sessionStorage !== "undefined") {
287
+ sessionStorage.removeItem(this.prefix + key);
288
+ }
289
+ } catch {
290
+ }
291
+ }
292
+ clear() {
293
+ try {
294
+ if (typeof sessionStorage === "undefined") {
295
+ return;
296
+ }
297
+ const keys = Object.keys(sessionStorage);
298
+ for (const key of keys) {
299
+ if (key.startsWith(this.prefix)) {
300
+ sessionStorage.removeItem(key);
301
+ }
302
+ }
303
+ } catch {
304
+ }
305
+ }
306
+ };
413
307
  BrowserPlatformAdapter = class {
414
308
  crypto;
415
309
  pgp;
416
310
  http;
311
+ cache;
417
312
  platform = "browser";
418
313
  constructor() {
419
314
  this.crypto = new BrowserCryptoAdapter();
420
315
  this.pgp = new BrowserPGPAdapter();
421
316
  this.http = new BrowserHttpAdapter();
317
+ this.cache = new BrowserCacheAdapter();
422
318
  }
423
319
  };
424
320
  browserPlatformAdapter = new BrowserPlatformAdapter();
@@ -482,13 +378,13 @@ var NodeCryptoAdapter = class {
482
378
  const publicKey = Buffer.from(publicKeyHex, "hex");
483
379
  const message = Buffer.from(data, "utf8");
484
380
  const encrypted = await eccryptoLib.encrypt(publicKey, message);
485
- const serialized = {
486
- iv: encrypted.iv.toString("hex"),
487
- ephemPublicKey: encrypted.ephemPublicKey.toString("hex"),
488
- ciphertext: encrypted.ciphertext.toString("hex"),
489
- mac: encrypted.mac.toString("hex")
490
- };
491
- return JSON.stringify(serialized);
381
+ const result = Buffer.concat([
382
+ encrypted.iv,
383
+ encrypted.ephemPublicKey,
384
+ encrypted.ciphertext,
385
+ encrypted.mac
386
+ ]);
387
+ return result.toString("hex");
492
388
  } catch (error) {
493
389
  throw new Error(`Encryption failed: ${error}`);
494
390
  }
@@ -496,15 +392,14 @@ var NodeCryptoAdapter = class {
496
392
  async decryptWithPrivateKey(encryptedData, privateKeyHex) {
497
393
  try {
498
394
  const eccryptoLib = await getEccrypto();
499
- const privateKey = Buffer.from(privateKeyHex, "hex");
500
- const serialized = JSON.parse(encryptedData);
501
- const encrypted = {
502
- iv: Buffer.from(serialized.iv, "hex"),
503
- ephemPublicKey: Buffer.from(serialized.ephemPublicKey, "hex"),
504
- ciphertext: Buffer.from(serialized.ciphertext, "hex"),
505
- mac: Buffer.from(serialized.mac, "hex")
506
- };
507
- const decrypted = await eccryptoLib.decrypt(privateKey, encrypted);
395
+ const privateKeyBuffer = processWalletPrivateKey(privateKeyHex);
396
+ const encryptedBuffer = Buffer.from(encryptedData, "hex");
397
+ const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
398
+ const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
399
+ const decrypted = await eccryptoLib.decrypt(
400
+ privateKeyBuffer,
401
+ encryptedObj
402
+ );
508
403
  return decrypted.toString("utf8");
509
404
  } catch (error) {
510
405
  throw new Error(`Decryption failed: ${error}`);
@@ -648,15 +543,45 @@ var NodeHttpAdapter = class {
648
543
  throw new Error("No fetch implementation available in Node.js environment");
649
544
  }
650
545
  };
546
+ var NodeCacheAdapter = class {
547
+ cache = /* @__PURE__ */ new Map();
548
+ defaultTtl = 2 * 60 * 60 * 1e3;
549
+ // 2 hours in milliseconds
550
+ get(key) {
551
+ const entry = this.cache.get(key);
552
+ if (!entry) {
553
+ return null;
554
+ }
555
+ if (Date.now() > entry.expires) {
556
+ this.cache.delete(key);
557
+ return null;
558
+ }
559
+ return entry.value;
560
+ }
561
+ set(key, value) {
562
+ this.cache.set(key, {
563
+ value,
564
+ expires: Date.now() + this.defaultTtl
565
+ });
566
+ }
567
+ delete(key) {
568
+ this.cache.delete(key);
569
+ }
570
+ clear() {
571
+ this.cache.clear();
572
+ }
573
+ };
651
574
  var NodePlatformAdapter = class {
652
575
  crypto;
653
576
  pgp;
654
577
  http;
578
+ cache;
655
579
  platform = "node";
656
580
  constructor() {
657
581
  this.crypto = new NodeCryptoAdapter();
658
582
  this.pgp = new NodePGPAdapter();
659
583
  this.http = new NodeHttpAdapter();
584
+ this.cache = new NodeCacheAdapter();
660
585
  }
661
586
  };
662
587
  var nodePlatformAdapter = new NodePlatformAdapter();