@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
@@ -49,11 +49,6 @@ function parseEncryptedDataBuffer(encryptedBuffer) {
49
49
  mac: encryptedBuffer.slice(-32)
50
50
  };
51
51
  }
52
- function uint8ArrayToHex(array) {
53
- return Array.from(array, (byte) => byte.toString(16).padStart(2, "0")).join(
54
- ""
55
- );
56
- }
57
52
  var init_crypto_utils = __esm({
58
53
  "src/platform/shared/crypto-utils.ts"() {
59
54
  "use strict";
@@ -108,14 +103,7 @@ __export(browser_exports, {
108
103
  BrowserPlatformAdapter: () => BrowserPlatformAdapter,
109
104
  browserPlatformAdapter: () => browserPlatformAdapter
110
105
  });
111
- function hexToUint8Array(hex) {
112
- const result = new Uint8Array(hex.length / 2);
113
- for (let i = 0; i < hex.length; i += 2) {
114
- result[i / 2] = parseInt(hex.substr(i, 2), 16);
115
- }
116
- return result;
117
- }
118
- var openpgp, BrowserECDH, BrowserCryptoAdapter, BrowserPGPAdapter, BrowserHttpAdapter, BrowserPlatformAdapter, browserPlatformAdapter;
106
+ var openpgp, BrowserCryptoAdapter, BrowserPGPAdapter, BrowserHttpAdapter, BrowserCacheAdapter, BrowserPlatformAdapter, browserPlatformAdapter;
119
107
  var init_browser = __esm({
120
108
  "src/platform/browser.ts"() {
121
109
  "use strict";
@@ -123,133 +111,52 @@ var init_browser = __esm({
123
111
  init_crypto_utils();
124
112
  init_pgp_utils();
125
113
  init_error_utils();
126
- BrowserECDH = class {
127
- async generateKeyPair() {
128
- const keyPair = await crypto.subtle.generateKey(
129
- {
130
- name: "ECDH",
131
- namedCurve: "P-256"
132
- },
133
- true,
134
- ["deriveKey", "deriveBits"]
135
- );
136
- return keyPair;
137
- }
138
- async encrypt(publicKeyHex, message) {
139
- const ephemeralKeyPair = await this.generateKeyPair();
140
- const publicKeyData = hexToUint8Array(publicKeyHex);
141
- const importedPublicKey = await crypto.subtle.importKey(
142
- "raw",
143
- publicKeyData,
144
- { name: "ECDH", namedCurve: "P-256" },
145
- false,
146
- []
147
- );
148
- const sharedKey = await crypto.subtle.deriveKey(
149
- { name: "ECDH", public: importedPublicKey },
150
- ephemeralKeyPair.privateKey,
151
- { name: "AES-GCM", length: 256 },
152
- false,
153
- ["encrypt"]
154
- );
155
- const iv = crypto.getRandomValues(new Uint8Array(12));
156
- const encoder = new TextEncoder();
157
- const data = encoder.encode(message);
158
- const encrypted = await crypto.subtle.encrypt(
159
- { name: "AES-GCM", iv },
160
- sharedKey,
161
- data
162
- );
163
- const ephemeralPublicKeyData = await crypto.subtle.exportKey(
164
- "raw",
165
- ephemeralKeyPair.publicKey
166
- );
167
- return JSON.stringify({
168
- encrypted: Array.from(new Uint8Array(encrypted)),
169
- iv: Array.from(iv),
170
- ephemeralPublicKey: Array.from(new Uint8Array(ephemeralPublicKeyData)),
171
- publicKey: publicKeyHex
172
- });
173
- }
174
- async decrypt(privateKeyHex, encryptedData) {
175
- try {
176
- const data = JSON.parse(encryptedData);
177
- if (!data.encrypted || !data.iv || !data.ephemeralPublicKey) {
178
- throw new Error("Invalid encrypted data format");
179
- }
180
- const privateKeyData = hexToUint8Array(privateKeyHex);
181
- const importedPrivateKey = await crypto.subtle.importKey(
182
- "pkcs8",
183
- privateKeyData,
184
- { name: "ECDH", namedCurve: "P-256" },
185
- false,
186
- ["deriveKey"]
187
- );
188
- const ephemeralPublicKey = await crypto.subtle.importKey(
189
- "raw",
190
- new Uint8Array(data.ephemeralPublicKey),
191
- { name: "ECDH", namedCurve: "P-256" },
192
- false,
193
- []
194
- );
195
- const sharedKey = await crypto.subtle.deriveKey(
196
- { name: "ECDH", public: ephemeralPublicKey },
197
- importedPrivateKey,
198
- { name: "AES-GCM", length: 256 },
199
- false,
200
- ["decrypt"]
201
- );
202
- const decrypted = await crypto.subtle.decrypt(
203
- { name: "AES-GCM", iv: new Uint8Array(data.iv) },
204
- sharedKey,
205
- new Uint8Array(data.encrypted)
206
- );
207
- return new TextDecoder().decode(decrypted);
208
- } catch (error) {
209
- throw new Error(
210
- `Decryption failed: ${error instanceof Error ? error.message : "Unknown error"}`
211
- );
212
- }
213
- }
214
- };
215
114
  BrowserCryptoAdapter = class {
216
115
  async encryptWithPublicKey(data, publicKeyHex) {
217
116
  try {
218
- const ecdh = new BrowserECDH();
219
- return await ecdh.encrypt(publicKeyHex, data);
117
+ const eccrypto2 = await import("eccrypto-js");
118
+ const publicKeyBuffer = Buffer.from(publicKeyHex, "hex");
119
+ const encrypted = await eccrypto2.encrypt(
120
+ publicKeyBuffer,
121
+ Buffer.from(data, "utf8")
122
+ );
123
+ const result = Buffer.concat([
124
+ encrypted.iv,
125
+ encrypted.ephemPublicKey,
126
+ encrypted.ciphertext,
127
+ encrypted.mac
128
+ ]);
129
+ return result.toString("hex");
220
130
  } catch (error) {
221
131
  throw new Error(`Encryption failed: ${error}`);
222
132
  }
223
133
  }
224
134
  async decryptWithPrivateKey(encryptedData, privateKeyHex) {
225
135
  try {
226
- const ecdh = new BrowserECDH();
227
- return await ecdh.decrypt(privateKeyHex, encryptedData);
136
+ const eccrypto2 = await import("eccrypto-js");
137
+ const privateKeyBuffer = processWalletPrivateKey(privateKeyHex);
138
+ const encryptedBuffer = Buffer.from(encryptedData, "hex");
139
+ const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
140
+ const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
141
+ const decryptedBuffer = await eccrypto2.decrypt(
142
+ privateKeyBuffer,
143
+ encryptedObj
144
+ );
145
+ return decryptedBuffer.toString("utf8");
228
146
  } catch (error) {
229
147
  throw new Error(`Decryption failed: ${error}`);
230
148
  }
231
149
  }
232
150
  async generateKeyPair() {
233
151
  try {
234
- const keyPair = await crypto.subtle.generateKey(
235
- {
236
- name: "ECDH",
237
- namedCurve: "P-256"
238
- },
239
- true,
240
- ["deriveKey", "deriveBits"]
241
- );
242
- const publicKeyBuffer = await crypto.subtle.exportKey(
243
- "raw",
244
- keyPair.publicKey
245
- );
246
- const privateKeyBuffer = await crypto.subtle.exportKey(
247
- "pkcs8",
248
- keyPair.privateKey
249
- );
152
+ const eccrypto2 = await import("eccrypto-js");
153
+ const privateKeyBytes = new Uint8Array(32);
154
+ crypto.getRandomValues(privateKeyBytes);
155
+ const privateKey = Buffer.from(privateKeyBytes);
156
+ const publicKey = eccrypto2.getPublicCompressed(privateKey);
250
157
  return {
251
- publicKey: uint8ArrayToHex(new Uint8Array(publicKeyBuffer)),
252
- privateKey: uint8ArrayToHex(new Uint8Array(privateKeyBuffer))
158
+ privateKey: privateKey.toString("hex"),
159
+ publicKey: publicKey.toString("hex")
253
160
  };
254
161
  } catch (error) {
255
162
  throw wrapCryptoError("key generation", error);
@@ -259,65 +166,9 @@ var init_browser = __esm({
259
166
  try {
260
167
  const eccrypto2 = await import("eccrypto-js");
261
168
  const uncompressedKey = processWalletPublicKey(publicKey);
262
- const iv = Buffer.from([
263
- 1,
264
- 2,
265
- 3,
266
- 4,
267
- 5,
268
- 6,
269
- 7,
270
- 8,
271
- 9,
272
- 10,
273
- 11,
274
- 12,
275
- 13,
276
- 14,
277
- 15,
278
- 16
279
- ]);
280
- const ephemeralKey = Buffer.from([
281
- 17,
282
- 34,
283
- 51,
284
- 68,
285
- 85,
286
- 102,
287
- 119,
288
- 136,
289
- 153,
290
- 170,
291
- 187,
292
- 204,
293
- 221,
294
- 238,
295
- 255,
296
- 0,
297
- 16,
298
- 32,
299
- 48,
300
- 64,
301
- 80,
302
- 96,
303
- 112,
304
- 128,
305
- 144,
306
- 160,
307
- 176,
308
- 192,
309
- 208,
310
- 224,
311
- 240,
312
- 0
313
- ]);
314
169
  const encryptedBuffer = await eccrypto2.encrypt(
315
170
  uncompressedKey,
316
- Buffer.from(data),
317
- {
318
- iv,
319
- ephemPrivateKey: ephemeralKey
320
- }
171
+ Buffer.from(data)
321
172
  );
322
173
  const result = Buffer.concat([
323
174
  encryptedBuffer.iv,
@@ -432,15 +283,60 @@ var init_browser = __esm({
432
283
  return fetch(url, options);
433
284
  }
434
285
  };
286
+ BrowserCacheAdapter = class {
287
+ prefix = "vana_cache_";
288
+ get(key) {
289
+ try {
290
+ if (typeof sessionStorage === "undefined") {
291
+ return null;
292
+ }
293
+ return sessionStorage.getItem(this.prefix + key);
294
+ } catch {
295
+ return null;
296
+ }
297
+ }
298
+ set(key, value) {
299
+ try {
300
+ if (typeof sessionStorage !== "undefined") {
301
+ sessionStorage.setItem(this.prefix + key, value);
302
+ }
303
+ } catch {
304
+ }
305
+ }
306
+ delete(key) {
307
+ try {
308
+ if (typeof sessionStorage !== "undefined") {
309
+ sessionStorage.removeItem(this.prefix + key);
310
+ }
311
+ } catch {
312
+ }
313
+ }
314
+ clear() {
315
+ try {
316
+ if (typeof sessionStorage === "undefined") {
317
+ return;
318
+ }
319
+ const keys = Object.keys(sessionStorage);
320
+ for (const key of keys) {
321
+ if (key.startsWith(this.prefix)) {
322
+ sessionStorage.removeItem(key);
323
+ }
324
+ }
325
+ } catch {
326
+ }
327
+ }
328
+ };
435
329
  BrowserPlatformAdapter = class {
436
330
  crypto;
437
331
  pgp;
438
332
  http;
333
+ cache;
439
334
  platform = "browser";
440
335
  constructor() {
441
336
  this.crypto = new BrowserCryptoAdapter();
442
337
  this.pgp = new BrowserPGPAdapter();
443
338
  this.http = new BrowserHttpAdapter();
339
+ this.cache = new BrowserCacheAdapter();
444
340
  }
445
341
  };
446
342
  browserPlatformAdapter = new BrowserPlatformAdapter();
@@ -518,13 +414,13 @@ var NodeCryptoAdapter = class {
518
414
  const publicKey = Buffer.from(publicKeyHex, "hex");
519
415
  const message = Buffer.from(data, "utf8");
520
416
  const encrypted = await eccryptoLib.encrypt(publicKey, message);
521
- const serialized = {
522
- iv: encrypted.iv.toString("hex"),
523
- ephemPublicKey: encrypted.ephemPublicKey.toString("hex"),
524
- ciphertext: encrypted.ciphertext.toString("hex"),
525
- mac: encrypted.mac.toString("hex")
526
- };
527
- return JSON.stringify(serialized);
417
+ const result = Buffer.concat([
418
+ encrypted.iv,
419
+ encrypted.ephemPublicKey,
420
+ encrypted.ciphertext,
421
+ encrypted.mac
422
+ ]);
423
+ return result.toString("hex");
528
424
  } catch (error) {
529
425
  throw new Error(`Encryption failed: ${error}`);
530
426
  }
@@ -532,15 +428,14 @@ var NodeCryptoAdapter = class {
532
428
  async decryptWithPrivateKey(encryptedData, privateKeyHex) {
533
429
  try {
534
430
  const eccryptoLib = await getEccrypto();
535
- const privateKey = Buffer.from(privateKeyHex, "hex");
536
- const serialized = JSON.parse(encryptedData);
537
- const encrypted = {
538
- iv: Buffer.from(serialized.iv, "hex"),
539
- ephemPublicKey: Buffer.from(serialized.ephemPublicKey, "hex"),
540
- ciphertext: Buffer.from(serialized.ciphertext, "hex"),
541
- mac: Buffer.from(serialized.mac, "hex")
542
- };
543
- const decrypted = await eccryptoLib.decrypt(privateKey, encrypted);
431
+ const privateKeyBuffer = processWalletPrivateKey(privateKeyHex);
432
+ const encryptedBuffer = Buffer.from(encryptedData, "hex");
433
+ const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
434
+ const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
435
+ const decrypted = await eccryptoLib.decrypt(
436
+ privateKeyBuffer,
437
+ encryptedObj
438
+ );
544
439
  return decrypted.toString("utf8");
545
440
  } catch (error) {
546
441
  throw new Error(`Decryption failed: ${error}`);
@@ -684,15 +579,45 @@ var NodeHttpAdapter = class {
684
579
  throw new Error("No fetch implementation available in Node.js environment");
685
580
  }
686
581
  };
582
+ var NodeCacheAdapter = class {
583
+ cache = /* @__PURE__ */ new Map();
584
+ defaultTtl = 2 * 60 * 60 * 1e3;
585
+ // 2 hours in milliseconds
586
+ get(key) {
587
+ const entry = this.cache.get(key);
588
+ if (!entry) {
589
+ return null;
590
+ }
591
+ if (Date.now() > entry.expires) {
592
+ this.cache.delete(key);
593
+ return null;
594
+ }
595
+ return entry.value;
596
+ }
597
+ set(key, value) {
598
+ this.cache.set(key, {
599
+ value,
600
+ expires: Date.now() + this.defaultTtl
601
+ });
602
+ }
603
+ delete(key) {
604
+ this.cache.delete(key);
605
+ }
606
+ clear() {
607
+ this.cache.clear();
608
+ }
609
+ };
687
610
  var NodePlatformAdapter = class {
688
611
  crypto;
689
612
  pgp;
690
613
  http;
614
+ cache;
691
615
  platform = "node";
692
616
  constructor() {
693
617
  this.crypto = new NodeCryptoAdapter();
694
618
  this.pgp = new NodePGPAdapter();
695
619
  this.http = new NodeHttpAdapter();
620
+ this.cache = new NodeCacheAdapter();
696
621
  }
697
622
  };
698
623
  var nodePlatformAdapter = new NodePlatformAdapter();