@opendatalabs/vana-sdk 0.1.0-alpha.5e925dc → 0.1.0-alpha.e64ec83
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chains.browser.cjs +93 -0
- package/dist/chains.browser.cjs.map +1 -0
- package/dist/chains.browser.d.cts +52 -0
- package/dist/chains.browser.d.ts +52 -0
- package/dist/chains.browser.js +63 -0
- package/dist/chains.browser.js.map +1 -0
- package/dist/chains.cjs +93 -0
- package/dist/chains.cjs.map +1 -0
- package/dist/chains.d.cts +2 -0
- package/dist/chains.d.ts +2 -0
- package/dist/chains.js +63 -0
- package/dist/chains.js.map +1 -0
- package/dist/chains.node.cjs +93 -0
- package/dist/chains.node.cjs.map +1 -0
- package/dist/chains.node.d.cts +2 -0
- package/dist/chains.node.d.ts +2 -0
- package/dist/chains.node.js +63 -0
- package/dist/chains.node.js.map +1 -0
- package/dist/index.browser.d.ts +50 -12
- package/dist/index.browser.js +458 -388
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.cts +236 -150
- package/dist/index.node.cjs +538 -51
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.d.cts +79 -1
- package/dist/index.node.d.ts +79 -1
- package/dist/index.node.js +529 -51
- package/dist/index.node.js.map +1 -1
- package/dist/platform.browser.d.ts +224 -0
- package/dist/platform.browser.js +467 -0
- package/dist/platform.browser.js.map +1 -0
- package/dist/platform.cjs +809 -0
- package/dist/platform.cjs.map +1 -0
- package/dist/platform.d.cts +1 -0
- package/dist/platform.d.ts +1 -0
- package/dist/platform.js +772 -0
- package/dist/platform.js.map +1 -0
- package/dist/platform.node.cjs +809 -0
- package/dist/platform.node.cjs.map +1 -0
- package/dist/platform.node.d.cts +264 -0
- package/dist/platform.node.d.ts +264 -0
- package/dist/platform.node.js +772 -0
- package/dist/platform.node.js.map +1 -0
- package/package.json +36 -2
package/dist/index.browser.js
CHANGED
|
@@ -10,6 +10,418 @@ var __export = (target, all) => {
|
|
|
10
10
|
};
|
|
11
11
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
12
12
|
|
|
13
|
+
// src/platform/shared/crypto-utils.ts
|
|
14
|
+
function processWalletPublicKey(publicKey) {
|
|
15
|
+
const publicKeyHex = publicKey.startsWith("0x") ? publicKey.slice(2) : publicKey;
|
|
16
|
+
const publicKeyBytes = Buffer.from(publicKeyHex, "hex");
|
|
17
|
+
return publicKeyBytes.length === 64 ? Buffer.concat([Buffer.from([4]), publicKeyBytes]) : publicKeyBytes;
|
|
18
|
+
}
|
|
19
|
+
function processWalletPrivateKey(privateKey) {
|
|
20
|
+
const privateKeyHex = privateKey.startsWith("0x") ? privateKey.slice(2) : privateKey;
|
|
21
|
+
return Buffer.from(privateKeyHex, "hex");
|
|
22
|
+
}
|
|
23
|
+
function parseEncryptedDataBuffer(encryptedBuffer) {
|
|
24
|
+
return {
|
|
25
|
+
iv: encryptedBuffer.slice(0, 16),
|
|
26
|
+
ephemPublicKey: encryptedBuffer.slice(16, 81),
|
|
27
|
+
// 65 bytes for uncompressed public key
|
|
28
|
+
ciphertext: encryptedBuffer.slice(81, -32),
|
|
29
|
+
mac: encryptedBuffer.slice(-32)
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function uint8ArrayToHex(array) {
|
|
33
|
+
return Array.from(array, (byte) => byte.toString(16).padStart(2, "0")).join(
|
|
34
|
+
""
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
var init_crypto_utils = __esm({
|
|
38
|
+
"src/platform/shared/crypto-utils.ts"() {
|
|
39
|
+
"use strict";
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// src/platform/shared/pgp-utils.ts
|
|
44
|
+
function processPGPKeyOptions(options) {
|
|
45
|
+
return {
|
|
46
|
+
name: options?.name || "Vana User",
|
|
47
|
+
email: options?.email || "user@vana.org",
|
|
48
|
+
passphrase: options?.passphrase
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function getPGPKeyGenParams(options) {
|
|
52
|
+
const { name, email, passphrase } = processPGPKeyOptions(options);
|
|
53
|
+
return {
|
|
54
|
+
type: "rsa",
|
|
55
|
+
rsaBits: 2048,
|
|
56
|
+
userIDs: [{ name, email }],
|
|
57
|
+
passphrase,
|
|
58
|
+
config: STANDARD_PGP_CONFIG
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
var STANDARD_PGP_CONFIG;
|
|
62
|
+
var init_pgp_utils = __esm({
|
|
63
|
+
"src/platform/shared/pgp-utils.ts"() {
|
|
64
|
+
"use strict";
|
|
65
|
+
STANDARD_PGP_CONFIG = {
|
|
66
|
+
preferredCompressionAlgorithm: 2,
|
|
67
|
+
// zlib (openpgp.enums.compression.zlib)
|
|
68
|
+
preferredSymmetricAlgorithm: 7
|
|
69
|
+
// aes256 (openpgp.enums.symmetric.aes256)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// src/platform/shared/error-utils.ts
|
|
75
|
+
function wrapCryptoError(operation, error) {
|
|
76
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
77
|
+
return new Error(`${operation} failed: ${message}`);
|
|
78
|
+
}
|
|
79
|
+
var init_error_utils = __esm({
|
|
80
|
+
"src/platform/shared/error-utils.ts"() {
|
|
81
|
+
"use strict";
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// src/platform/browser.ts
|
|
86
|
+
import * as openpgp from "openpgp";
|
|
87
|
+
function hexToUint8Array(hex) {
|
|
88
|
+
const result = new Uint8Array(hex.length / 2);
|
|
89
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
90
|
+
result[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
var BrowserECDH, BrowserCryptoAdapter, BrowserPGPAdapter, BrowserHttpAdapter, BrowserPlatformAdapter, browserPlatformAdapter;
|
|
95
|
+
var init_browser = __esm({
|
|
96
|
+
"src/platform/browser.ts"() {
|
|
97
|
+
"use strict";
|
|
98
|
+
init_crypto_utils();
|
|
99
|
+
init_pgp_utils();
|
|
100
|
+
init_error_utils();
|
|
101
|
+
BrowserECDH = class {
|
|
102
|
+
async generateKeyPair() {
|
|
103
|
+
const keyPair = await crypto.subtle.generateKey(
|
|
104
|
+
{
|
|
105
|
+
name: "ECDH",
|
|
106
|
+
namedCurve: "P-256"
|
|
107
|
+
},
|
|
108
|
+
true,
|
|
109
|
+
["deriveKey", "deriveBits"]
|
|
110
|
+
);
|
|
111
|
+
return keyPair;
|
|
112
|
+
}
|
|
113
|
+
async encrypt(publicKeyHex, message) {
|
|
114
|
+
const ephemeralKeyPair = await this.generateKeyPair();
|
|
115
|
+
const publicKeyData = hexToUint8Array(publicKeyHex);
|
|
116
|
+
const importedPublicKey = await crypto.subtle.importKey(
|
|
117
|
+
"raw",
|
|
118
|
+
publicKeyData,
|
|
119
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
120
|
+
false,
|
|
121
|
+
[]
|
|
122
|
+
);
|
|
123
|
+
const sharedKey = await crypto.subtle.deriveKey(
|
|
124
|
+
{ name: "ECDH", public: importedPublicKey },
|
|
125
|
+
ephemeralKeyPair.privateKey,
|
|
126
|
+
{ name: "AES-GCM", length: 256 },
|
|
127
|
+
false,
|
|
128
|
+
["encrypt"]
|
|
129
|
+
);
|
|
130
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
131
|
+
const encoder = new TextEncoder();
|
|
132
|
+
const data = encoder.encode(message);
|
|
133
|
+
const encrypted = await crypto.subtle.encrypt(
|
|
134
|
+
{ name: "AES-GCM", iv },
|
|
135
|
+
sharedKey,
|
|
136
|
+
data
|
|
137
|
+
);
|
|
138
|
+
const ephemeralPublicKeyData = await crypto.subtle.exportKey(
|
|
139
|
+
"raw",
|
|
140
|
+
ephemeralKeyPair.publicKey
|
|
141
|
+
);
|
|
142
|
+
return JSON.stringify({
|
|
143
|
+
encrypted: Array.from(new Uint8Array(encrypted)),
|
|
144
|
+
iv: Array.from(iv),
|
|
145
|
+
ephemeralPublicKey: Array.from(new Uint8Array(ephemeralPublicKeyData)),
|
|
146
|
+
publicKey: publicKeyHex
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
async decrypt(privateKeyHex, encryptedData) {
|
|
150
|
+
try {
|
|
151
|
+
const data = JSON.parse(encryptedData);
|
|
152
|
+
if (!data.encrypted || !data.iv || !data.ephemeralPublicKey) {
|
|
153
|
+
throw new Error("Invalid encrypted data format");
|
|
154
|
+
}
|
|
155
|
+
const privateKeyData = hexToUint8Array(privateKeyHex);
|
|
156
|
+
const importedPrivateKey = await crypto.subtle.importKey(
|
|
157
|
+
"pkcs8",
|
|
158
|
+
privateKeyData,
|
|
159
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
160
|
+
false,
|
|
161
|
+
["deriveKey"]
|
|
162
|
+
);
|
|
163
|
+
const ephemeralPublicKey = await crypto.subtle.importKey(
|
|
164
|
+
"raw",
|
|
165
|
+
new Uint8Array(data.ephemeralPublicKey),
|
|
166
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
167
|
+
false,
|
|
168
|
+
[]
|
|
169
|
+
);
|
|
170
|
+
const sharedKey = await crypto.subtle.deriveKey(
|
|
171
|
+
{ name: "ECDH", public: ephemeralPublicKey },
|
|
172
|
+
importedPrivateKey,
|
|
173
|
+
{ name: "AES-GCM", length: 256 },
|
|
174
|
+
false,
|
|
175
|
+
["decrypt"]
|
|
176
|
+
);
|
|
177
|
+
const decrypted = await crypto.subtle.decrypt(
|
|
178
|
+
{ name: "AES-GCM", iv: new Uint8Array(data.iv) },
|
|
179
|
+
sharedKey,
|
|
180
|
+
new Uint8Array(data.encrypted)
|
|
181
|
+
);
|
|
182
|
+
return new TextDecoder().decode(decrypted);
|
|
183
|
+
} catch (error) {
|
|
184
|
+
throw new Error(
|
|
185
|
+
`Decryption failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
BrowserCryptoAdapter = class {
|
|
191
|
+
async encryptWithPublicKey(data, publicKeyHex) {
|
|
192
|
+
try {
|
|
193
|
+
const ecdh = new BrowserECDH();
|
|
194
|
+
return await ecdh.encrypt(publicKeyHex, data);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
throw new Error(`Encryption failed: ${error}`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
async decryptWithPrivateKey(encryptedData, privateKeyHex) {
|
|
200
|
+
try {
|
|
201
|
+
const ecdh = new BrowserECDH();
|
|
202
|
+
return await ecdh.decrypt(privateKeyHex, encryptedData);
|
|
203
|
+
} catch (error) {
|
|
204
|
+
throw new Error(`Decryption failed: ${error}`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
async generateKeyPair() {
|
|
208
|
+
try {
|
|
209
|
+
const keyPair = await crypto.subtle.generateKey(
|
|
210
|
+
{
|
|
211
|
+
name: "ECDH",
|
|
212
|
+
namedCurve: "P-256"
|
|
213
|
+
},
|
|
214
|
+
true,
|
|
215
|
+
["deriveKey", "deriveBits"]
|
|
216
|
+
);
|
|
217
|
+
const publicKeyBuffer = await crypto.subtle.exportKey(
|
|
218
|
+
"raw",
|
|
219
|
+
keyPair.publicKey
|
|
220
|
+
);
|
|
221
|
+
const privateKeyBuffer = await crypto.subtle.exportKey(
|
|
222
|
+
"pkcs8",
|
|
223
|
+
keyPair.privateKey
|
|
224
|
+
);
|
|
225
|
+
return {
|
|
226
|
+
publicKey: uint8ArrayToHex(new Uint8Array(publicKeyBuffer)),
|
|
227
|
+
privateKey: uint8ArrayToHex(new Uint8Array(privateKeyBuffer))
|
|
228
|
+
};
|
|
229
|
+
} catch (error) {
|
|
230
|
+
throw wrapCryptoError("key generation", error);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
async encryptWithWalletPublicKey(data, publicKey) {
|
|
234
|
+
try {
|
|
235
|
+
const eccrypto = await import("eccrypto-js");
|
|
236
|
+
const uncompressedKey = processWalletPublicKey(publicKey);
|
|
237
|
+
const iv = Buffer.from([
|
|
238
|
+
1,
|
|
239
|
+
2,
|
|
240
|
+
3,
|
|
241
|
+
4,
|
|
242
|
+
5,
|
|
243
|
+
6,
|
|
244
|
+
7,
|
|
245
|
+
8,
|
|
246
|
+
9,
|
|
247
|
+
10,
|
|
248
|
+
11,
|
|
249
|
+
12,
|
|
250
|
+
13,
|
|
251
|
+
14,
|
|
252
|
+
15,
|
|
253
|
+
16
|
|
254
|
+
]);
|
|
255
|
+
const ephemeralKey = Buffer.from([
|
|
256
|
+
17,
|
|
257
|
+
34,
|
|
258
|
+
51,
|
|
259
|
+
68,
|
|
260
|
+
85,
|
|
261
|
+
102,
|
|
262
|
+
119,
|
|
263
|
+
136,
|
|
264
|
+
153,
|
|
265
|
+
170,
|
|
266
|
+
187,
|
|
267
|
+
204,
|
|
268
|
+
221,
|
|
269
|
+
238,
|
|
270
|
+
255,
|
|
271
|
+
0,
|
|
272
|
+
16,
|
|
273
|
+
32,
|
|
274
|
+
48,
|
|
275
|
+
64,
|
|
276
|
+
80,
|
|
277
|
+
96,
|
|
278
|
+
112,
|
|
279
|
+
128,
|
|
280
|
+
144,
|
|
281
|
+
160,
|
|
282
|
+
176,
|
|
283
|
+
192,
|
|
284
|
+
208,
|
|
285
|
+
224,
|
|
286
|
+
240,
|
|
287
|
+
0
|
|
288
|
+
]);
|
|
289
|
+
const encryptedBuffer = await eccrypto.encrypt(
|
|
290
|
+
uncompressedKey,
|
|
291
|
+
Buffer.from(data),
|
|
292
|
+
{
|
|
293
|
+
iv,
|
|
294
|
+
ephemPrivateKey: ephemeralKey
|
|
295
|
+
}
|
|
296
|
+
);
|
|
297
|
+
const result = Buffer.concat([
|
|
298
|
+
encryptedBuffer.iv,
|
|
299
|
+
encryptedBuffer.ephemPublicKey,
|
|
300
|
+
encryptedBuffer.ciphertext,
|
|
301
|
+
encryptedBuffer.mac
|
|
302
|
+
]);
|
|
303
|
+
return result.toString("hex");
|
|
304
|
+
} catch (error) {
|
|
305
|
+
throw wrapCryptoError("encrypt with wallet public key", error);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
async decryptWithWalletPrivateKey(encryptedData, privateKey) {
|
|
309
|
+
try {
|
|
310
|
+
const eccrypto = await import("eccrypto-js");
|
|
311
|
+
const privateKeyBuffer = processWalletPrivateKey(privateKey);
|
|
312
|
+
const encryptedBuffer = Buffer.from(encryptedData, "hex");
|
|
313
|
+
const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
|
|
314
|
+
const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
|
|
315
|
+
const decryptedBuffer = await eccrypto.decrypt(
|
|
316
|
+
privateKeyBuffer,
|
|
317
|
+
encryptedObj
|
|
318
|
+
);
|
|
319
|
+
return decryptedBuffer.toString("utf8");
|
|
320
|
+
} catch (error) {
|
|
321
|
+
throw wrapCryptoError("decrypt with wallet private key", error);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
async encryptWithPassword(data, password) {
|
|
325
|
+
try {
|
|
326
|
+
const openpgp2 = await import("openpgp");
|
|
327
|
+
const message = await openpgp2.createMessage({
|
|
328
|
+
binary: data
|
|
329
|
+
});
|
|
330
|
+
const encrypted = await openpgp2.encrypt({
|
|
331
|
+
message,
|
|
332
|
+
passwords: [password],
|
|
333
|
+
format: "binary"
|
|
334
|
+
});
|
|
335
|
+
const response = new Response(encrypted);
|
|
336
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
337
|
+
return new Uint8Array(arrayBuffer);
|
|
338
|
+
} catch (error) {
|
|
339
|
+
throw new Error(`Failed to encrypt with password: ${error}`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
async decryptWithPassword(encryptedData, password) {
|
|
343
|
+
try {
|
|
344
|
+
const openpgp2 = await import("openpgp");
|
|
345
|
+
const message = await openpgp2.readMessage({
|
|
346
|
+
binaryMessage: encryptedData
|
|
347
|
+
});
|
|
348
|
+
const { data: decrypted } = await openpgp2.decrypt({
|
|
349
|
+
message,
|
|
350
|
+
passwords: [password],
|
|
351
|
+
format: "binary"
|
|
352
|
+
});
|
|
353
|
+
return new Uint8Array(decrypted);
|
|
354
|
+
} catch (error) {
|
|
355
|
+
throw new Error(`Failed to decrypt with password: ${error}`);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
BrowserPGPAdapter = class {
|
|
360
|
+
async encrypt(data, publicKeyArmored) {
|
|
361
|
+
try {
|
|
362
|
+
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
|
|
363
|
+
const encrypted = await openpgp.encrypt({
|
|
364
|
+
message: await openpgp.createMessage({ text: data }),
|
|
365
|
+
encryptionKeys: publicKey,
|
|
366
|
+
config: {
|
|
367
|
+
preferredCompressionAlgorithm: openpgp.enums.compression.zlib
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
return encrypted;
|
|
371
|
+
} catch (error) {
|
|
372
|
+
throw new Error(`PGP encryption failed: ${error}`);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
async decrypt(encryptedData, privateKeyArmored) {
|
|
376
|
+
try {
|
|
377
|
+
const privateKey = await openpgp.readPrivateKey({
|
|
378
|
+
armoredKey: privateKeyArmored
|
|
379
|
+
});
|
|
380
|
+
const message = await openpgp.readMessage({
|
|
381
|
+
armoredMessage: encryptedData
|
|
382
|
+
});
|
|
383
|
+
const { data: decrypted } = await openpgp.decrypt({
|
|
384
|
+
message,
|
|
385
|
+
decryptionKeys: privateKey
|
|
386
|
+
});
|
|
387
|
+
return decrypted;
|
|
388
|
+
} catch (error) {
|
|
389
|
+
throw new Error(`PGP decryption failed: ${error}`);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
async generateKeyPair(options) {
|
|
393
|
+
try {
|
|
394
|
+
const keyGenParams = getPGPKeyGenParams(options);
|
|
395
|
+
const { privateKey, publicKey } = await openpgp.generateKey(keyGenParams);
|
|
396
|
+
return { publicKey, privateKey };
|
|
397
|
+
} catch (error) {
|
|
398
|
+
throw wrapCryptoError("PGP key generation", error);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
BrowserHttpAdapter = class {
|
|
403
|
+
async fetch(url, options) {
|
|
404
|
+
if (typeof fetch === "undefined") {
|
|
405
|
+
throw new Error("Fetch API not available in this browser environment");
|
|
406
|
+
}
|
|
407
|
+
return fetch(url, options);
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
BrowserPlatformAdapter = class {
|
|
411
|
+
constructor() {
|
|
412
|
+
__publicField(this, "crypto");
|
|
413
|
+
__publicField(this, "pgp");
|
|
414
|
+
__publicField(this, "http");
|
|
415
|
+
__publicField(this, "platform", "browser");
|
|
416
|
+
this.crypto = new BrowserCryptoAdapter();
|
|
417
|
+
this.pgp = new BrowserPGPAdapter();
|
|
418
|
+
this.http = new BrowserHttpAdapter();
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
browserPlatformAdapter = new BrowserPlatformAdapter();
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
|
|
13
425
|
// src/utils/ipfs.ts
|
|
14
426
|
var ipfs_exports = {};
|
|
15
427
|
__export(ipfs_exports, {
|
|
@@ -117,394 +529,8 @@ var init_ipfs = __esm({
|
|
|
117
529
|
}
|
|
118
530
|
});
|
|
119
531
|
|
|
120
|
-
// src/
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
// src/platform/shared/crypto-utils.ts
|
|
124
|
-
function processWalletPublicKey(publicKey) {
|
|
125
|
-
const publicKeyHex = publicKey.startsWith("0x") ? publicKey.slice(2) : publicKey;
|
|
126
|
-
const publicKeyBytes = Buffer.from(publicKeyHex, "hex");
|
|
127
|
-
return publicKeyBytes.length === 64 ? Buffer.concat([Buffer.from([4]), publicKeyBytes]) : publicKeyBytes;
|
|
128
|
-
}
|
|
129
|
-
function processWalletPrivateKey(privateKey) {
|
|
130
|
-
const privateKeyHex = privateKey.startsWith("0x") ? privateKey.slice(2) : privateKey;
|
|
131
|
-
return Buffer.from(privateKeyHex, "hex");
|
|
132
|
-
}
|
|
133
|
-
function parseEncryptedDataBuffer(encryptedBuffer) {
|
|
134
|
-
return {
|
|
135
|
-
iv: encryptedBuffer.slice(0, 16),
|
|
136
|
-
ephemPublicKey: encryptedBuffer.slice(16, 81),
|
|
137
|
-
// 65 bytes for uncompressed public key
|
|
138
|
-
ciphertext: encryptedBuffer.slice(81, -32),
|
|
139
|
-
mac: encryptedBuffer.slice(-32)
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
function uint8ArrayToHex(array) {
|
|
143
|
-
return Array.from(array, (byte) => byte.toString(16).padStart(2, "0")).join(
|
|
144
|
-
""
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// src/platform/shared/pgp-utils.ts
|
|
149
|
-
var STANDARD_PGP_CONFIG = {
|
|
150
|
-
preferredCompressionAlgorithm: 2,
|
|
151
|
-
// zlib (openpgp.enums.compression.zlib)
|
|
152
|
-
preferredSymmetricAlgorithm: 7
|
|
153
|
-
// aes256 (openpgp.enums.symmetric.aes256)
|
|
154
|
-
};
|
|
155
|
-
function processPGPKeyOptions(options) {
|
|
156
|
-
return {
|
|
157
|
-
name: options?.name || "Vana User",
|
|
158
|
-
email: options?.email || "user@vana.org",
|
|
159
|
-
passphrase: options?.passphrase
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
function getPGPKeyGenParams(options) {
|
|
163
|
-
const { name, email, passphrase } = processPGPKeyOptions(options);
|
|
164
|
-
return {
|
|
165
|
-
type: "rsa",
|
|
166
|
-
rsaBits: 2048,
|
|
167
|
-
userIDs: [{ name, email }],
|
|
168
|
-
passphrase,
|
|
169
|
-
config: STANDARD_PGP_CONFIG
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// src/platform/shared/error-utils.ts
|
|
174
|
-
function wrapCryptoError(operation, error) {
|
|
175
|
-
const message = error instanceof Error ? error.message : "Unknown error";
|
|
176
|
-
return new Error(`${operation} failed: ${message}`);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// src/platform/browser.ts
|
|
180
|
-
var BrowserECDH = class {
|
|
181
|
-
async generateKeyPair() {
|
|
182
|
-
const keyPair = await crypto.subtle.generateKey(
|
|
183
|
-
{
|
|
184
|
-
name: "ECDH",
|
|
185
|
-
namedCurve: "P-256"
|
|
186
|
-
},
|
|
187
|
-
true,
|
|
188
|
-
["deriveKey", "deriveBits"]
|
|
189
|
-
);
|
|
190
|
-
return keyPair;
|
|
191
|
-
}
|
|
192
|
-
async encrypt(publicKeyHex, message) {
|
|
193
|
-
const ephemeralKeyPair = await this.generateKeyPair();
|
|
194
|
-
const publicKeyData = hexToUint8Array(publicKeyHex);
|
|
195
|
-
const importedPublicKey = await crypto.subtle.importKey(
|
|
196
|
-
"raw",
|
|
197
|
-
publicKeyData,
|
|
198
|
-
{ name: "ECDH", namedCurve: "P-256" },
|
|
199
|
-
false,
|
|
200
|
-
[]
|
|
201
|
-
);
|
|
202
|
-
const sharedKey = await crypto.subtle.deriveKey(
|
|
203
|
-
{ name: "ECDH", public: importedPublicKey },
|
|
204
|
-
ephemeralKeyPair.privateKey,
|
|
205
|
-
{ name: "AES-GCM", length: 256 },
|
|
206
|
-
false,
|
|
207
|
-
["encrypt"]
|
|
208
|
-
);
|
|
209
|
-
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
210
|
-
const encoder = new TextEncoder();
|
|
211
|
-
const data = encoder.encode(message);
|
|
212
|
-
const encrypted = await crypto.subtle.encrypt(
|
|
213
|
-
{ name: "AES-GCM", iv },
|
|
214
|
-
sharedKey,
|
|
215
|
-
data
|
|
216
|
-
);
|
|
217
|
-
const ephemeralPublicKeyData = await crypto.subtle.exportKey(
|
|
218
|
-
"raw",
|
|
219
|
-
ephemeralKeyPair.publicKey
|
|
220
|
-
);
|
|
221
|
-
return JSON.stringify({
|
|
222
|
-
encrypted: Array.from(new Uint8Array(encrypted)),
|
|
223
|
-
iv: Array.from(iv),
|
|
224
|
-
ephemeralPublicKey: Array.from(new Uint8Array(ephemeralPublicKeyData)),
|
|
225
|
-
publicKey: publicKeyHex
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
async decrypt(privateKeyHex, encryptedData) {
|
|
229
|
-
try {
|
|
230
|
-
const data = JSON.parse(encryptedData);
|
|
231
|
-
if (!data.encrypted || !data.iv || !data.ephemeralPublicKey) {
|
|
232
|
-
throw new Error("Invalid encrypted data format");
|
|
233
|
-
}
|
|
234
|
-
const privateKeyData = hexToUint8Array(privateKeyHex);
|
|
235
|
-
const importedPrivateKey = await crypto.subtle.importKey(
|
|
236
|
-
"pkcs8",
|
|
237
|
-
privateKeyData,
|
|
238
|
-
{ name: "ECDH", namedCurve: "P-256" },
|
|
239
|
-
false,
|
|
240
|
-
["deriveKey"]
|
|
241
|
-
);
|
|
242
|
-
const ephemeralPublicKey = await crypto.subtle.importKey(
|
|
243
|
-
"raw",
|
|
244
|
-
new Uint8Array(data.ephemeralPublicKey),
|
|
245
|
-
{ name: "ECDH", namedCurve: "P-256" },
|
|
246
|
-
false,
|
|
247
|
-
[]
|
|
248
|
-
);
|
|
249
|
-
const sharedKey = await crypto.subtle.deriveKey(
|
|
250
|
-
{ name: "ECDH", public: ephemeralPublicKey },
|
|
251
|
-
importedPrivateKey,
|
|
252
|
-
{ name: "AES-GCM", length: 256 },
|
|
253
|
-
false,
|
|
254
|
-
["decrypt"]
|
|
255
|
-
);
|
|
256
|
-
const decrypted = await crypto.subtle.decrypt(
|
|
257
|
-
{ name: "AES-GCM", iv: new Uint8Array(data.iv) },
|
|
258
|
-
sharedKey,
|
|
259
|
-
new Uint8Array(data.encrypted)
|
|
260
|
-
);
|
|
261
|
-
return new TextDecoder().decode(decrypted);
|
|
262
|
-
} catch (error) {
|
|
263
|
-
throw new Error(
|
|
264
|
-
`Decryption failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
265
|
-
);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
function hexToUint8Array(hex) {
|
|
270
|
-
const result = new Uint8Array(hex.length / 2);
|
|
271
|
-
for (let i = 0; i < hex.length; i += 2) {
|
|
272
|
-
result[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
273
|
-
}
|
|
274
|
-
return result;
|
|
275
|
-
}
|
|
276
|
-
var BrowserCryptoAdapter = class {
|
|
277
|
-
async encryptWithPublicKey(data, publicKeyHex) {
|
|
278
|
-
try {
|
|
279
|
-
const ecdh = new BrowserECDH();
|
|
280
|
-
return await ecdh.encrypt(publicKeyHex, data);
|
|
281
|
-
} catch (error) {
|
|
282
|
-
throw new Error(`Encryption failed: ${error}`);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
async decryptWithPrivateKey(encryptedData, privateKeyHex) {
|
|
286
|
-
try {
|
|
287
|
-
const ecdh = new BrowserECDH();
|
|
288
|
-
return await ecdh.decrypt(privateKeyHex, encryptedData);
|
|
289
|
-
} catch (error) {
|
|
290
|
-
throw new Error(`Decryption failed: ${error}`);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
async generateKeyPair() {
|
|
294
|
-
try {
|
|
295
|
-
const keyPair = await crypto.subtle.generateKey(
|
|
296
|
-
{
|
|
297
|
-
name: "ECDH",
|
|
298
|
-
namedCurve: "P-256"
|
|
299
|
-
},
|
|
300
|
-
true,
|
|
301
|
-
["deriveKey", "deriveBits"]
|
|
302
|
-
);
|
|
303
|
-
const publicKeyBuffer = await crypto.subtle.exportKey(
|
|
304
|
-
"raw",
|
|
305
|
-
keyPair.publicKey
|
|
306
|
-
);
|
|
307
|
-
const privateKeyBuffer = await crypto.subtle.exportKey(
|
|
308
|
-
"pkcs8",
|
|
309
|
-
keyPair.privateKey
|
|
310
|
-
);
|
|
311
|
-
return {
|
|
312
|
-
publicKey: uint8ArrayToHex(new Uint8Array(publicKeyBuffer)),
|
|
313
|
-
privateKey: uint8ArrayToHex(new Uint8Array(privateKeyBuffer))
|
|
314
|
-
};
|
|
315
|
-
} catch (error) {
|
|
316
|
-
throw wrapCryptoError("key generation", error);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
async encryptWithWalletPublicKey(data, publicKey) {
|
|
320
|
-
try {
|
|
321
|
-
const eccrypto = await import("eccrypto-js");
|
|
322
|
-
const uncompressedKey = processWalletPublicKey(publicKey);
|
|
323
|
-
const iv = Buffer.from([
|
|
324
|
-
1,
|
|
325
|
-
2,
|
|
326
|
-
3,
|
|
327
|
-
4,
|
|
328
|
-
5,
|
|
329
|
-
6,
|
|
330
|
-
7,
|
|
331
|
-
8,
|
|
332
|
-
9,
|
|
333
|
-
10,
|
|
334
|
-
11,
|
|
335
|
-
12,
|
|
336
|
-
13,
|
|
337
|
-
14,
|
|
338
|
-
15,
|
|
339
|
-
16
|
|
340
|
-
]);
|
|
341
|
-
const ephemeralKey = Buffer.from([
|
|
342
|
-
17,
|
|
343
|
-
34,
|
|
344
|
-
51,
|
|
345
|
-
68,
|
|
346
|
-
85,
|
|
347
|
-
102,
|
|
348
|
-
119,
|
|
349
|
-
136,
|
|
350
|
-
153,
|
|
351
|
-
170,
|
|
352
|
-
187,
|
|
353
|
-
204,
|
|
354
|
-
221,
|
|
355
|
-
238,
|
|
356
|
-
255,
|
|
357
|
-
0,
|
|
358
|
-
16,
|
|
359
|
-
32,
|
|
360
|
-
48,
|
|
361
|
-
64,
|
|
362
|
-
80,
|
|
363
|
-
96,
|
|
364
|
-
112,
|
|
365
|
-
128,
|
|
366
|
-
144,
|
|
367
|
-
160,
|
|
368
|
-
176,
|
|
369
|
-
192,
|
|
370
|
-
208,
|
|
371
|
-
224,
|
|
372
|
-
240,
|
|
373
|
-
0
|
|
374
|
-
]);
|
|
375
|
-
const encryptedBuffer = await eccrypto.encrypt(
|
|
376
|
-
uncompressedKey,
|
|
377
|
-
Buffer.from(data),
|
|
378
|
-
{
|
|
379
|
-
iv,
|
|
380
|
-
ephemPrivateKey: ephemeralKey
|
|
381
|
-
}
|
|
382
|
-
);
|
|
383
|
-
const result = Buffer.concat([
|
|
384
|
-
encryptedBuffer.iv,
|
|
385
|
-
encryptedBuffer.ephemPublicKey,
|
|
386
|
-
encryptedBuffer.ciphertext,
|
|
387
|
-
encryptedBuffer.mac
|
|
388
|
-
]);
|
|
389
|
-
return result.toString("hex");
|
|
390
|
-
} catch (error) {
|
|
391
|
-
throw wrapCryptoError("encrypt with wallet public key", error);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
async decryptWithWalletPrivateKey(encryptedData, privateKey) {
|
|
395
|
-
try {
|
|
396
|
-
const eccrypto = await import("eccrypto-js");
|
|
397
|
-
const privateKeyBuffer = processWalletPrivateKey(privateKey);
|
|
398
|
-
const encryptedBuffer = Buffer.from(encryptedData, "hex");
|
|
399
|
-
const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
|
|
400
|
-
const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
|
|
401
|
-
const decryptedBuffer = await eccrypto.decrypt(
|
|
402
|
-
privateKeyBuffer,
|
|
403
|
-
encryptedObj
|
|
404
|
-
);
|
|
405
|
-
return decryptedBuffer.toString("utf8");
|
|
406
|
-
} catch (error) {
|
|
407
|
-
throw wrapCryptoError("decrypt with wallet private key", error);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
async encryptWithPassword(data, password) {
|
|
411
|
-
try {
|
|
412
|
-
const openpgp2 = await import("openpgp");
|
|
413
|
-
const message = await openpgp2.createMessage({
|
|
414
|
-
binary: data
|
|
415
|
-
});
|
|
416
|
-
const encrypted = await openpgp2.encrypt({
|
|
417
|
-
message,
|
|
418
|
-
passwords: [password],
|
|
419
|
-
format: "binary"
|
|
420
|
-
});
|
|
421
|
-
const response = new Response(encrypted);
|
|
422
|
-
const arrayBuffer = await response.arrayBuffer();
|
|
423
|
-
return new Uint8Array(arrayBuffer);
|
|
424
|
-
} catch (error) {
|
|
425
|
-
throw new Error(`Failed to encrypt with password: ${error}`);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
async decryptWithPassword(encryptedData, password) {
|
|
429
|
-
try {
|
|
430
|
-
const openpgp2 = await import("openpgp");
|
|
431
|
-
const message = await openpgp2.readMessage({
|
|
432
|
-
binaryMessage: encryptedData
|
|
433
|
-
});
|
|
434
|
-
const { data: decrypted } = await openpgp2.decrypt({
|
|
435
|
-
message,
|
|
436
|
-
passwords: [password],
|
|
437
|
-
format: "binary"
|
|
438
|
-
});
|
|
439
|
-
return new Uint8Array(decrypted);
|
|
440
|
-
} catch (error) {
|
|
441
|
-
throw new Error(`Failed to decrypt with password: ${error}`);
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
};
|
|
445
|
-
var BrowserPGPAdapter = class {
|
|
446
|
-
async encrypt(data, publicKeyArmored) {
|
|
447
|
-
try {
|
|
448
|
-
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
|
|
449
|
-
const encrypted = await openpgp.encrypt({
|
|
450
|
-
message: await openpgp.createMessage({ text: data }),
|
|
451
|
-
encryptionKeys: publicKey,
|
|
452
|
-
config: {
|
|
453
|
-
preferredCompressionAlgorithm: openpgp.enums.compression.zlib
|
|
454
|
-
}
|
|
455
|
-
});
|
|
456
|
-
return encrypted;
|
|
457
|
-
} catch (error) {
|
|
458
|
-
throw new Error(`PGP encryption failed: ${error}`);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
async decrypt(encryptedData, privateKeyArmored) {
|
|
462
|
-
try {
|
|
463
|
-
const privateKey = await openpgp.readPrivateKey({
|
|
464
|
-
armoredKey: privateKeyArmored
|
|
465
|
-
});
|
|
466
|
-
const message = await openpgp.readMessage({
|
|
467
|
-
armoredMessage: encryptedData
|
|
468
|
-
});
|
|
469
|
-
const { data: decrypted } = await openpgp.decrypt({
|
|
470
|
-
message,
|
|
471
|
-
decryptionKeys: privateKey
|
|
472
|
-
});
|
|
473
|
-
return decrypted;
|
|
474
|
-
} catch (error) {
|
|
475
|
-
throw new Error(`PGP decryption failed: ${error}`);
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
async generateKeyPair(options) {
|
|
479
|
-
try {
|
|
480
|
-
const keyGenParams = getPGPKeyGenParams(options);
|
|
481
|
-
const { privateKey, publicKey } = await openpgp.generateKey(keyGenParams);
|
|
482
|
-
return { publicKey, privateKey };
|
|
483
|
-
} catch (error) {
|
|
484
|
-
throw wrapCryptoError("PGP key generation", error);
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
};
|
|
488
|
-
var BrowserHttpAdapter = class {
|
|
489
|
-
async fetch(url, options) {
|
|
490
|
-
if (typeof fetch === "undefined") {
|
|
491
|
-
throw new Error("Fetch API not available in this browser environment");
|
|
492
|
-
}
|
|
493
|
-
return fetch(url, options);
|
|
494
|
-
}
|
|
495
|
-
};
|
|
496
|
-
var BrowserPlatformAdapter = class {
|
|
497
|
-
constructor() {
|
|
498
|
-
__publicField(this, "crypto");
|
|
499
|
-
__publicField(this, "pgp");
|
|
500
|
-
__publicField(this, "http");
|
|
501
|
-
__publicField(this, "platform", "browser");
|
|
502
|
-
this.crypto = new BrowserCryptoAdapter();
|
|
503
|
-
this.pgp = new BrowserPGPAdapter();
|
|
504
|
-
this.http = new BrowserHttpAdapter();
|
|
505
|
-
}
|
|
506
|
-
};
|
|
507
|
-
var browserPlatformAdapter = new BrowserPlatformAdapter();
|
|
532
|
+
// src/index.browser.ts
|
|
533
|
+
init_browser();
|
|
508
534
|
|
|
509
535
|
// src/types/config.ts
|
|
510
536
|
function isWalletConfig(config) {
|
|
@@ -40200,6 +40226,45 @@ var CircuitBreaker = class {
|
|
|
40200
40226
|
}
|
|
40201
40227
|
};
|
|
40202
40228
|
|
|
40229
|
+
// src/index.browser.ts
|
|
40230
|
+
init_browser();
|
|
40231
|
+
|
|
40232
|
+
// src/platform/browser-only.ts
|
|
40233
|
+
init_browser();
|
|
40234
|
+
function createBrowserPlatformAdapter() {
|
|
40235
|
+
return new BrowserPlatformAdapter();
|
|
40236
|
+
}
|
|
40237
|
+
function createPlatformAdapterSafe() {
|
|
40238
|
+
return createBrowserPlatformAdapter();
|
|
40239
|
+
}
|
|
40240
|
+
|
|
40241
|
+
// src/platform/utils.ts
|
|
40242
|
+
function detectPlatform() {
|
|
40243
|
+
if (typeof process !== "undefined" && process.versions && process.versions.node) {
|
|
40244
|
+
return "node";
|
|
40245
|
+
}
|
|
40246
|
+
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
40247
|
+
return "browser";
|
|
40248
|
+
}
|
|
40249
|
+
return "node";
|
|
40250
|
+
}
|
|
40251
|
+
function isPlatformSupported(platformType) {
|
|
40252
|
+
const currentPlatform = detectPlatform();
|
|
40253
|
+
return currentPlatform === platformType;
|
|
40254
|
+
}
|
|
40255
|
+
function getPlatformCapabilities() {
|
|
40256
|
+
const platform = detectPlatform();
|
|
40257
|
+
return {
|
|
40258
|
+
platform,
|
|
40259
|
+
crypto: {
|
|
40260
|
+
webCrypto: typeof crypto !== "undefined" && crypto.subtle,
|
|
40261
|
+
nodeCrypto: typeof process !== "undefined" && process.versions && process.versions.node
|
|
40262
|
+
},
|
|
40263
|
+
fetch: typeof fetch !== "undefined" || typeof globalThis.fetch !== "undefined",
|
|
40264
|
+
streams: typeof ReadableStream !== "undefined"
|
|
40265
|
+
};
|
|
40266
|
+
}
|
|
40267
|
+
|
|
40203
40268
|
// src/core/apiClient.ts
|
|
40204
40269
|
var ApiClient = class {
|
|
40205
40270
|
constructor(config = {}) {
|
|
@@ -40572,12 +40637,15 @@ export {
|
|
|
40572
40637
|
convertIpfsUrl,
|
|
40573
40638
|
convertIpfsUrlWithFallbacks,
|
|
40574
40639
|
createAndStoreGrant,
|
|
40640
|
+
createBrowserPlatformAdapter,
|
|
40575
40641
|
createGrantFile,
|
|
40642
|
+
createPlatformAdapterSafe,
|
|
40576
40643
|
createValidatedGrant,
|
|
40577
40644
|
decryptUserData,
|
|
40578
40645
|
decryptWithPrivateKey,
|
|
40579
40646
|
decryptWithWalletPrivateKey,
|
|
40580
40647
|
index_browser_default as default,
|
|
40648
|
+
detectPlatform,
|
|
40581
40649
|
encryptFileKey,
|
|
40582
40650
|
encryptUserData,
|
|
40583
40651
|
encryptWithWalletPublicKey,
|
|
@@ -40600,11 +40668,13 @@ export {
|
|
|
40600
40668
|
getGatewayUrls,
|
|
40601
40669
|
getGrantFileHash,
|
|
40602
40670
|
getGrantTimeRemaining,
|
|
40671
|
+
getPlatformCapabilities,
|
|
40603
40672
|
isAPIResponse,
|
|
40604
40673
|
isGrantExpired,
|
|
40605
40674
|
isIdentityServerOutput,
|
|
40606
40675
|
isIpfsUrl,
|
|
40607
40676
|
isPersonalServerOutput,
|
|
40677
|
+
isPlatformSupported,
|
|
40608
40678
|
isReplicateAPIResponse,
|
|
40609
40679
|
moksha,
|
|
40610
40680
|
mokshaTestnet,
|