@opendatalabs/vana-sdk 0.1.0-alpha.d44f792 → 0.1.0-alpha.db07fe1
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/README.md +68 -13
- package/package.json +45 -24
- package/dist/chains.browser.cjs +0 -96
- package/dist/chains.browser.cjs.map +0 -1
- package/dist/chains.browser.d.cts +0 -53
- package/dist/chains.browser.d.ts +0 -53
- package/dist/chains.browser.js +0 -65
- package/dist/chains.browser.js.map +0 -1
- package/dist/chains.cjs +0 -96
- package/dist/chains.cjs.map +0 -1
- package/dist/chains.d.cts +0 -2
- package/dist/chains.d.ts +0 -2
- package/dist/chains.js +0 -65
- package/dist/chains.js.map +0 -1
- package/dist/chains.node.cjs +0 -96
- package/dist/chains.node.cjs.map +0 -1
- package/dist/chains.node.d.cts +0 -2
- package/dist/chains.node.d.ts +0 -2
- package/dist/chains.node.js +0 -65
- package/dist/chains.node.js.map +0 -1
- package/dist/index.browser.d.ts +0 -34334
- package/dist/index.browser.js +0 -41687
- package/dist/index.browser.js.map +0 -1
- package/dist/index.d.cts +0 -2
- package/dist/index.node.cjs +0 -42197
- package/dist/index.node.cjs.map +0 -1
- package/dist/index.node.d.cts +0 -34468
- package/dist/index.node.d.ts +0 -34468
- package/dist/index.node.js +0 -42057
- package/dist/index.node.js.map +0 -1
- package/dist/platform.browser.d.ts +0 -293
- package/dist/platform.browser.js +0 -365
- package/dist/platform.browser.js.map +0 -1
- package/dist/platform.cjs +0 -734
- package/dist/platform.cjs.map +0 -1
- package/dist/platform.d.cts +0 -1
- package/dist/platform.d.ts +0 -1
- package/dist/platform.js +0 -697
- package/dist/platform.js.map +0 -1
- package/dist/platform.node.cjs +0 -734
- package/dist/platform.node.cjs.map +0 -1
- package/dist/platform.node.d.cts +0 -334
- package/dist/platform.node.d.ts +0 -334
- package/dist/platform.node.js +0 -697
- package/dist/platform.node.js.map +0 -1
package/dist/platform.node.js
DELETED
|
@@ -1,697 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
-
var __esm = (fn, res) => function __init() {
|
|
4
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
5
|
-
};
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
// src/platform/shared/crypto-utils.ts
|
|
12
|
-
function processWalletPublicKey(publicKey) {
|
|
13
|
-
const publicKeyHex = publicKey.startsWith("0x") ? publicKey.slice(2) : publicKey;
|
|
14
|
-
const publicKeyBytes = Buffer.from(publicKeyHex, "hex");
|
|
15
|
-
return publicKeyBytes.length === 64 ? Buffer.concat([Buffer.from([4]), publicKeyBytes]) : publicKeyBytes;
|
|
16
|
-
}
|
|
17
|
-
function processWalletPrivateKey(privateKey) {
|
|
18
|
-
const privateKeyHex = privateKey.startsWith("0x") ? privateKey.slice(2) : privateKey;
|
|
19
|
-
return Buffer.from(privateKeyHex, "hex");
|
|
20
|
-
}
|
|
21
|
-
function parseEncryptedDataBuffer(encryptedBuffer) {
|
|
22
|
-
return {
|
|
23
|
-
iv: encryptedBuffer.slice(0, 16),
|
|
24
|
-
ephemPublicKey: encryptedBuffer.slice(16, 81),
|
|
25
|
-
// 65 bytes for uncompressed public key
|
|
26
|
-
ciphertext: encryptedBuffer.slice(81, -32),
|
|
27
|
-
mac: encryptedBuffer.slice(-32)
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
var init_crypto_utils = __esm({
|
|
31
|
-
"src/platform/shared/crypto-utils.ts"() {
|
|
32
|
-
"use strict";
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// src/platform/shared/pgp-utils.ts
|
|
37
|
-
function processPGPKeyOptions(options) {
|
|
38
|
-
return {
|
|
39
|
-
name: options?.name || "Vana User",
|
|
40
|
-
email: options?.email || "user@vana.org",
|
|
41
|
-
passphrase: options?.passphrase
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
function getPGPKeyGenParams(options) {
|
|
45
|
-
const { name, email, passphrase } = processPGPKeyOptions(options);
|
|
46
|
-
return {
|
|
47
|
-
type: "rsa",
|
|
48
|
-
rsaBits: 2048,
|
|
49
|
-
userIDs: [{ name, email }],
|
|
50
|
-
passphrase,
|
|
51
|
-
config: STANDARD_PGP_CONFIG
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
var STANDARD_PGP_CONFIG;
|
|
55
|
-
var init_pgp_utils = __esm({
|
|
56
|
-
"src/platform/shared/pgp-utils.ts"() {
|
|
57
|
-
"use strict";
|
|
58
|
-
STANDARD_PGP_CONFIG = {
|
|
59
|
-
preferredCompressionAlgorithm: 2,
|
|
60
|
-
// zlib (openpgp.enums.compression.zlib)
|
|
61
|
-
preferredSymmetricAlgorithm: 7
|
|
62
|
-
// aes256 (openpgp.enums.symmetric.aes256)
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
// src/platform/shared/error-utils.ts
|
|
68
|
-
function wrapCryptoError(operation, error) {
|
|
69
|
-
const message = error instanceof Error ? error.message : "Unknown error";
|
|
70
|
-
return new Error(`${operation} failed: ${message}`);
|
|
71
|
-
}
|
|
72
|
-
var init_error_utils = __esm({
|
|
73
|
-
"src/platform/shared/error-utils.ts"() {
|
|
74
|
-
"use strict";
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
// src/platform/browser.ts
|
|
79
|
-
var browser_exports = {};
|
|
80
|
-
__export(browser_exports, {
|
|
81
|
-
BrowserPlatformAdapter: () => BrowserPlatformAdapter,
|
|
82
|
-
browserPlatformAdapter: () => browserPlatformAdapter
|
|
83
|
-
});
|
|
84
|
-
import * as openpgp from "openpgp";
|
|
85
|
-
var BrowserCryptoAdapter, BrowserPGPAdapter, BrowserHttpAdapter, BrowserCacheAdapter, BrowserPlatformAdapter, browserPlatformAdapter;
|
|
86
|
-
var init_browser = __esm({
|
|
87
|
-
"src/platform/browser.ts"() {
|
|
88
|
-
"use strict";
|
|
89
|
-
init_crypto_utils();
|
|
90
|
-
init_pgp_utils();
|
|
91
|
-
init_error_utils();
|
|
92
|
-
BrowserCryptoAdapter = class {
|
|
93
|
-
async encryptWithPublicKey(data, publicKeyHex) {
|
|
94
|
-
try {
|
|
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");
|
|
108
|
-
} catch (error) {
|
|
109
|
-
throw new Error(`Encryption failed: ${error}`);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
async decryptWithPrivateKey(encryptedData, privateKeyHex) {
|
|
113
|
-
try {
|
|
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");
|
|
124
|
-
} catch (error) {
|
|
125
|
-
throw new Error(`Decryption failed: ${error}`);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
async generateKeyPair() {
|
|
129
|
-
try {
|
|
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);
|
|
135
|
-
return {
|
|
136
|
-
privateKey: privateKey.toString("hex"),
|
|
137
|
-
publicKey: publicKey.toString("hex")
|
|
138
|
-
};
|
|
139
|
-
} catch (error) {
|
|
140
|
-
throw wrapCryptoError("key generation", error);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
async encryptWithWalletPublicKey(data, publicKey) {
|
|
144
|
-
try {
|
|
145
|
-
const eccrypto2 = await import("eccrypto-js");
|
|
146
|
-
const uncompressedKey = processWalletPublicKey(publicKey);
|
|
147
|
-
const encryptedBuffer = await eccrypto2.encrypt(
|
|
148
|
-
uncompressedKey,
|
|
149
|
-
Buffer.from(data)
|
|
150
|
-
);
|
|
151
|
-
const result = Buffer.concat([
|
|
152
|
-
encryptedBuffer.iv,
|
|
153
|
-
encryptedBuffer.ephemPublicKey,
|
|
154
|
-
encryptedBuffer.ciphertext,
|
|
155
|
-
encryptedBuffer.mac
|
|
156
|
-
]);
|
|
157
|
-
return result.toString("hex");
|
|
158
|
-
} catch (error) {
|
|
159
|
-
throw wrapCryptoError("encrypt with wallet public key", error);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
async decryptWithWalletPrivateKey(encryptedData, privateKey) {
|
|
163
|
-
try {
|
|
164
|
-
const eccrypto2 = await import("eccrypto-js");
|
|
165
|
-
const privateKeyBuffer = processWalletPrivateKey(privateKey);
|
|
166
|
-
const encryptedBuffer = Buffer.from(encryptedData, "hex");
|
|
167
|
-
const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
|
|
168
|
-
const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
|
|
169
|
-
const decryptedBuffer = await eccrypto2.decrypt(
|
|
170
|
-
privateKeyBuffer,
|
|
171
|
-
encryptedObj
|
|
172
|
-
);
|
|
173
|
-
return decryptedBuffer.toString("utf8");
|
|
174
|
-
} catch (error) {
|
|
175
|
-
throw wrapCryptoError("decrypt with wallet private key", error);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
async encryptWithPassword(data, password) {
|
|
179
|
-
try {
|
|
180
|
-
const openpgp3 = await import("openpgp");
|
|
181
|
-
const message = await openpgp3.createMessage({
|
|
182
|
-
binary: data
|
|
183
|
-
});
|
|
184
|
-
const encrypted = await openpgp3.encrypt({
|
|
185
|
-
message,
|
|
186
|
-
passwords: [password],
|
|
187
|
-
format: "binary"
|
|
188
|
-
});
|
|
189
|
-
const response = new Response(encrypted);
|
|
190
|
-
const arrayBuffer = await response.arrayBuffer();
|
|
191
|
-
return new Uint8Array(arrayBuffer);
|
|
192
|
-
} catch (error) {
|
|
193
|
-
throw new Error(`Failed to encrypt with password: ${error}`);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
async decryptWithPassword(encryptedData, password) {
|
|
197
|
-
try {
|
|
198
|
-
const openpgp3 = await import("openpgp");
|
|
199
|
-
const message = await openpgp3.readMessage({
|
|
200
|
-
binaryMessage: encryptedData
|
|
201
|
-
});
|
|
202
|
-
const { data: decrypted } = await openpgp3.decrypt({
|
|
203
|
-
message,
|
|
204
|
-
passwords: [password],
|
|
205
|
-
format: "binary"
|
|
206
|
-
});
|
|
207
|
-
return new Uint8Array(decrypted);
|
|
208
|
-
} catch (error) {
|
|
209
|
-
throw new Error(`Failed to decrypt with password: ${error}`);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
BrowserPGPAdapter = class {
|
|
214
|
-
async encrypt(data, publicKeyArmored) {
|
|
215
|
-
try {
|
|
216
|
-
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
|
|
217
|
-
const encrypted = await openpgp.encrypt({
|
|
218
|
-
message: await openpgp.createMessage({ text: data }),
|
|
219
|
-
encryptionKeys: publicKey,
|
|
220
|
-
config: {
|
|
221
|
-
preferredCompressionAlgorithm: openpgp.enums.compression.zlib
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
return encrypted;
|
|
225
|
-
} catch (error) {
|
|
226
|
-
throw new Error(`PGP encryption failed: ${error}`);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
async decrypt(encryptedData, privateKeyArmored) {
|
|
230
|
-
try {
|
|
231
|
-
const privateKey = await openpgp.readPrivateKey({
|
|
232
|
-
armoredKey: privateKeyArmored
|
|
233
|
-
});
|
|
234
|
-
const message = await openpgp.readMessage({
|
|
235
|
-
armoredMessage: encryptedData
|
|
236
|
-
});
|
|
237
|
-
const { data: decrypted } = await openpgp.decrypt({
|
|
238
|
-
message,
|
|
239
|
-
decryptionKeys: privateKey
|
|
240
|
-
});
|
|
241
|
-
return decrypted;
|
|
242
|
-
} catch (error) {
|
|
243
|
-
throw new Error(`PGP decryption failed: ${error}`);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
async generateKeyPair(options) {
|
|
247
|
-
try {
|
|
248
|
-
const keyGenParams = getPGPKeyGenParams(options);
|
|
249
|
-
const { privateKey, publicKey } = await openpgp.generateKey(keyGenParams);
|
|
250
|
-
return { publicKey, privateKey };
|
|
251
|
-
} catch (error) {
|
|
252
|
-
throw wrapCryptoError("PGP key generation", error);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
BrowserHttpAdapter = class {
|
|
257
|
-
async fetch(url, options) {
|
|
258
|
-
if (typeof fetch === "undefined") {
|
|
259
|
-
throw new Error("Fetch API not available in this browser environment");
|
|
260
|
-
}
|
|
261
|
-
return fetch(url, options);
|
|
262
|
-
}
|
|
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
|
-
};
|
|
307
|
-
BrowserPlatformAdapter = class {
|
|
308
|
-
crypto;
|
|
309
|
-
pgp;
|
|
310
|
-
http;
|
|
311
|
-
cache;
|
|
312
|
-
platform = "browser";
|
|
313
|
-
constructor() {
|
|
314
|
-
this.crypto = new BrowserCryptoAdapter();
|
|
315
|
-
this.pgp = new BrowserPGPAdapter();
|
|
316
|
-
this.http = new BrowserHttpAdapter();
|
|
317
|
-
this.cache = new BrowserCacheAdapter();
|
|
318
|
-
}
|
|
319
|
-
};
|
|
320
|
-
browserPlatformAdapter = new BrowserPlatformAdapter();
|
|
321
|
-
}
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
// src/platform.node.ts
|
|
325
|
-
init_browser();
|
|
326
|
-
|
|
327
|
-
// src/platform/node.ts
|
|
328
|
-
init_crypto_utils();
|
|
329
|
-
init_pgp_utils();
|
|
330
|
-
init_error_utils();
|
|
331
|
-
import { randomBytes } from "crypto";
|
|
332
|
-
import * as openpgp2 from "openpgp";
|
|
333
|
-
|
|
334
|
-
// src/platform/shared/stream-utils.ts
|
|
335
|
-
async function streamToUint8Array(stream) {
|
|
336
|
-
const reader = stream.getReader();
|
|
337
|
-
const chunks = [];
|
|
338
|
-
try {
|
|
339
|
-
while (true) {
|
|
340
|
-
const { done, value } = await reader.read();
|
|
341
|
-
if (done) break;
|
|
342
|
-
chunks.push(value);
|
|
343
|
-
}
|
|
344
|
-
} finally {
|
|
345
|
-
reader.releaseLock();
|
|
346
|
-
}
|
|
347
|
-
const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
|
|
348
|
-
const result = new Uint8Array(totalLength);
|
|
349
|
-
let offset = 0;
|
|
350
|
-
for (const chunk of chunks) {
|
|
351
|
-
result.set(chunk, offset);
|
|
352
|
-
offset += chunk.length;
|
|
353
|
-
}
|
|
354
|
-
return result;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
// src/platform/node.ts
|
|
358
|
-
var eccrypto = null;
|
|
359
|
-
async function getEccrypto() {
|
|
360
|
-
if (!eccrypto) {
|
|
361
|
-
try {
|
|
362
|
-
const eccryptoLib = await import("eccrypto");
|
|
363
|
-
eccrypto = {
|
|
364
|
-
encrypt: eccryptoLib.encrypt,
|
|
365
|
-
decrypt: eccryptoLib.decrypt,
|
|
366
|
-
getPublicCompressed: eccryptoLib.getPublicCompressed
|
|
367
|
-
};
|
|
368
|
-
} catch (error) {
|
|
369
|
-
throw new Error(`Failed to load eccrypto library: ${error}`);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
return eccrypto;
|
|
373
|
-
}
|
|
374
|
-
var NodeCryptoAdapter = class {
|
|
375
|
-
async encryptWithPublicKey(data, publicKeyHex) {
|
|
376
|
-
try {
|
|
377
|
-
const eccryptoLib = await getEccrypto();
|
|
378
|
-
const publicKey = Buffer.from(publicKeyHex, "hex");
|
|
379
|
-
const message = Buffer.from(data, "utf8");
|
|
380
|
-
const encrypted = await eccryptoLib.encrypt(publicKey, message);
|
|
381
|
-
const result = Buffer.concat([
|
|
382
|
-
encrypted.iv,
|
|
383
|
-
encrypted.ephemPublicKey,
|
|
384
|
-
encrypted.ciphertext,
|
|
385
|
-
encrypted.mac
|
|
386
|
-
]);
|
|
387
|
-
return result.toString("hex");
|
|
388
|
-
} catch (error) {
|
|
389
|
-
throw new Error(`Encryption failed: ${error}`);
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
async decryptWithPrivateKey(encryptedData, privateKeyHex) {
|
|
393
|
-
try {
|
|
394
|
-
const eccryptoLib = await getEccrypto();
|
|
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
|
-
);
|
|
403
|
-
return decrypted.toString("utf8");
|
|
404
|
-
} catch (error) {
|
|
405
|
-
throw new Error(`Decryption failed: ${error}`);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
async generateKeyPair() {
|
|
409
|
-
try {
|
|
410
|
-
const eccryptoLib = await getEccrypto();
|
|
411
|
-
const privateKey = randomBytes(32);
|
|
412
|
-
const publicKey = eccryptoLib.getPublicCompressed(privateKey);
|
|
413
|
-
return {
|
|
414
|
-
privateKey: privateKey.toString("hex"),
|
|
415
|
-
publicKey: publicKey.toString("hex")
|
|
416
|
-
};
|
|
417
|
-
} catch (error) {
|
|
418
|
-
throw wrapCryptoError("key generation", error);
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
async encryptWithWalletPublicKey(data, publicKey) {
|
|
422
|
-
try {
|
|
423
|
-
const eccryptoLib = await getEccrypto();
|
|
424
|
-
const uncompressedKey = processWalletPublicKey(publicKey);
|
|
425
|
-
const encrypted = await eccryptoLib.encrypt(
|
|
426
|
-
uncompressedKey,
|
|
427
|
-
Buffer.from(data)
|
|
428
|
-
);
|
|
429
|
-
const result = Buffer.concat([
|
|
430
|
-
encrypted.iv,
|
|
431
|
-
encrypted.ephemPublicKey,
|
|
432
|
-
encrypted.ciphertext,
|
|
433
|
-
encrypted.mac
|
|
434
|
-
]);
|
|
435
|
-
return result.toString("hex");
|
|
436
|
-
} catch (error) {
|
|
437
|
-
throw wrapCryptoError("encrypt with wallet public key", error);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
async decryptWithWalletPrivateKey(encryptedData, privateKey) {
|
|
441
|
-
try {
|
|
442
|
-
const eccryptoLib = await getEccrypto();
|
|
443
|
-
const privateKeyBuffer = processWalletPrivateKey(privateKey);
|
|
444
|
-
const encryptedBuffer = Buffer.from(encryptedData, "hex");
|
|
445
|
-
const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
|
|
446
|
-
const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
|
|
447
|
-
const decryptedBuffer = await eccryptoLib.decrypt(
|
|
448
|
-
privateKeyBuffer,
|
|
449
|
-
encryptedObj
|
|
450
|
-
);
|
|
451
|
-
return decryptedBuffer.toString("utf8");
|
|
452
|
-
} catch (error) {
|
|
453
|
-
throw wrapCryptoError("decrypt with wallet private key", error);
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
async encryptWithPassword(data, password) {
|
|
457
|
-
try {
|
|
458
|
-
const message = await openpgp2.createMessage({
|
|
459
|
-
binary: data
|
|
460
|
-
});
|
|
461
|
-
const encrypted = await openpgp2.encrypt({
|
|
462
|
-
message,
|
|
463
|
-
passwords: [password],
|
|
464
|
-
format: "binary"
|
|
465
|
-
});
|
|
466
|
-
if (encrypted instanceof Uint8Array) {
|
|
467
|
-
return encrypted;
|
|
468
|
-
}
|
|
469
|
-
if (encrypted && typeof encrypted === "object" && "getReader" in encrypted) {
|
|
470
|
-
return await streamToUint8Array(
|
|
471
|
-
encrypted
|
|
472
|
-
);
|
|
473
|
-
}
|
|
474
|
-
throw new Error("Unexpected encrypted data format");
|
|
475
|
-
} catch (error) {
|
|
476
|
-
throw wrapCryptoError("encrypt with password", error);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
async decryptWithPassword(encryptedData, password) {
|
|
480
|
-
try {
|
|
481
|
-
const message = await openpgp2.readMessage({
|
|
482
|
-
binaryMessage: encryptedData
|
|
483
|
-
});
|
|
484
|
-
const { data: decrypted } = await openpgp2.decrypt({
|
|
485
|
-
message,
|
|
486
|
-
passwords: [password],
|
|
487
|
-
format: "binary"
|
|
488
|
-
});
|
|
489
|
-
return new Uint8Array(decrypted);
|
|
490
|
-
} catch (error) {
|
|
491
|
-
throw wrapCryptoError("decrypt with password", error);
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
};
|
|
495
|
-
var NodePGPAdapter = class {
|
|
496
|
-
async encrypt(data, publicKeyArmored) {
|
|
497
|
-
try {
|
|
498
|
-
const publicKey = await openpgp2.readKey({ armoredKey: publicKeyArmored });
|
|
499
|
-
const encrypted = await openpgp2.encrypt({
|
|
500
|
-
message: await openpgp2.createMessage({ text: data }),
|
|
501
|
-
encryptionKeys: publicKey,
|
|
502
|
-
config: {
|
|
503
|
-
preferredCompressionAlgorithm: openpgp2.enums.compression.zlib
|
|
504
|
-
}
|
|
505
|
-
});
|
|
506
|
-
return encrypted;
|
|
507
|
-
} catch (error) {
|
|
508
|
-
throw wrapCryptoError("PGP encryption", error);
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
async decrypt(encryptedData, privateKeyArmored) {
|
|
512
|
-
try {
|
|
513
|
-
const privateKey = await openpgp2.readPrivateKey({
|
|
514
|
-
armoredKey: privateKeyArmored
|
|
515
|
-
});
|
|
516
|
-
const message = await openpgp2.readMessage({
|
|
517
|
-
armoredMessage: encryptedData
|
|
518
|
-
});
|
|
519
|
-
const { data: decrypted } = await openpgp2.decrypt({
|
|
520
|
-
message,
|
|
521
|
-
decryptionKeys: privateKey
|
|
522
|
-
});
|
|
523
|
-
return decrypted;
|
|
524
|
-
} catch (error) {
|
|
525
|
-
throw wrapCryptoError("PGP decryption", error);
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
async generateKeyPair(options) {
|
|
529
|
-
try {
|
|
530
|
-
const keyGenParams = getPGPKeyGenParams(options);
|
|
531
|
-
const { privateKey, publicKey } = await openpgp2.generateKey(keyGenParams);
|
|
532
|
-
return { publicKey, privateKey };
|
|
533
|
-
} catch (error) {
|
|
534
|
-
throw wrapCryptoError("PGP key generation", error);
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
};
|
|
538
|
-
var NodeHttpAdapter = class {
|
|
539
|
-
async fetch(url, options) {
|
|
540
|
-
if (typeof globalThis.fetch !== "undefined") {
|
|
541
|
-
return globalThis.fetch(url, options);
|
|
542
|
-
}
|
|
543
|
-
throw new Error("No fetch implementation available in Node.js environment");
|
|
544
|
-
}
|
|
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
|
-
};
|
|
574
|
-
var NodePlatformAdapter = class {
|
|
575
|
-
crypto;
|
|
576
|
-
pgp;
|
|
577
|
-
http;
|
|
578
|
-
cache;
|
|
579
|
-
platform = "node";
|
|
580
|
-
constructor() {
|
|
581
|
-
this.crypto = new NodeCryptoAdapter();
|
|
582
|
-
this.pgp = new NodePGPAdapter();
|
|
583
|
-
this.http = new NodeHttpAdapter();
|
|
584
|
-
this.cache = new NodeCacheAdapter();
|
|
585
|
-
}
|
|
586
|
-
};
|
|
587
|
-
var nodePlatformAdapter = new NodePlatformAdapter();
|
|
588
|
-
|
|
589
|
-
// src/platform/utils.ts
|
|
590
|
-
function detectPlatform() {
|
|
591
|
-
if (typeof process !== "undefined" && process.versions && process.versions.node) {
|
|
592
|
-
return "node";
|
|
593
|
-
}
|
|
594
|
-
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
595
|
-
return "browser";
|
|
596
|
-
}
|
|
597
|
-
return "node";
|
|
598
|
-
}
|
|
599
|
-
async function createPlatformAdapter() {
|
|
600
|
-
const platform = detectPlatform();
|
|
601
|
-
try {
|
|
602
|
-
if (platform === "node") {
|
|
603
|
-
if (typeof window !== "undefined") {
|
|
604
|
-
throw new Error(
|
|
605
|
-
"NodePlatformAdapter is not available in browser environments. Use BrowserPlatformAdapter instead."
|
|
606
|
-
);
|
|
607
|
-
}
|
|
608
|
-
const moduleName = "./node";
|
|
609
|
-
const { NodePlatformAdapter: NodePlatformAdapter2 } = await import(moduleName);
|
|
610
|
-
return new NodePlatformAdapter2();
|
|
611
|
-
} else {
|
|
612
|
-
const { BrowserPlatformAdapter: BrowserPlatformAdapter2 } = await Promise.resolve().then(() => (init_browser(), browser_exports));
|
|
613
|
-
return new BrowserPlatformAdapter2();
|
|
614
|
-
}
|
|
615
|
-
} catch (error) {
|
|
616
|
-
throw new Error(
|
|
617
|
-
`Failed to create platform adapter for ${platform}: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
618
|
-
);
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
async function createPlatformAdapterFor(platformType) {
|
|
622
|
-
try {
|
|
623
|
-
if (platformType === "node") {
|
|
624
|
-
if (typeof window !== "undefined") {
|
|
625
|
-
throw new Error(
|
|
626
|
-
"NodePlatformAdapter is not available in browser environments. Use BrowserPlatformAdapter instead."
|
|
627
|
-
);
|
|
628
|
-
}
|
|
629
|
-
const moduleName = "./node";
|
|
630
|
-
const { NodePlatformAdapter: NodePlatformAdapter2 } = await import(moduleName);
|
|
631
|
-
return new NodePlatformAdapter2();
|
|
632
|
-
} else {
|
|
633
|
-
const { BrowserPlatformAdapter: BrowserPlatformAdapter2 } = await Promise.resolve().then(() => (init_browser(), browser_exports));
|
|
634
|
-
return new BrowserPlatformAdapter2();
|
|
635
|
-
}
|
|
636
|
-
} catch (error) {
|
|
637
|
-
throw new Error(
|
|
638
|
-
`Failed to create platform adapter for ${platformType}: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
639
|
-
);
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
function isPlatformSupported(platformType) {
|
|
643
|
-
const currentPlatform = detectPlatform();
|
|
644
|
-
return currentPlatform === platformType;
|
|
645
|
-
}
|
|
646
|
-
function getPlatformCapabilities() {
|
|
647
|
-
const platform = detectPlatform();
|
|
648
|
-
return {
|
|
649
|
-
platform,
|
|
650
|
-
crypto: {
|
|
651
|
-
webCrypto: typeof crypto !== "undefined" && crypto.subtle,
|
|
652
|
-
nodeCrypto: typeof process !== "undefined" && process.versions && process.versions.node
|
|
653
|
-
},
|
|
654
|
-
fetch: typeof fetch !== "undefined" || typeof globalThis.fetch !== "undefined",
|
|
655
|
-
streams: typeof ReadableStream !== "undefined"
|
|
656
|
-
};
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
// src/platform/browser-safe.ts
|
|
660
|
-
init_browser();
|
|
661
|
-
async function createNodePlatformAdapter() {
|
|
662
|
-
if (typeof window !== "undefined") {
|
|
663
|
-
throw new Error(
|
|
664
|
-
"NodePlatformAdapter is not available in browser environments. Use BrowserPlatformAdapter instead."
|
|
665
|
-
);
|
|
666
|
-
}
|
|
667
|
-
const moduleName = "./node";
|
|
668
|
-
const { NodePlatformAdapter: NodePlatformAdapter2 } = await import(moduleName);
|
|
669
|
-
return new NodePlatformAdapter2();
|
|
670
|
-
}
|
|
671
|
-
function createBrowserPlatformAdapter() {
|
|
672
|
-
return new BrowserPlatformAdapter();
|
|
673
|
-
}
|
|
674
|
-
async function createPlatformAdapterSafe() {
|
|
675
|
-
if (typeof window !== "undefined") {
|
|
676
|
-
return createBrowserPlatformAdapter();
|
|
677
|
-
}
|
|
678
|
-
if (typeof process !== "undefined" && process.versions && process.versions.node) {
|
|
679
|
-
if (typeof window === "undefined") {
|
|
680
|
-
return await createNodePlatformAdapter();
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
return createBrowserPlatformAdapter();
|
|
684
|
-
}
|
|
685
|
-
export {
|
|
686
|
-
BrowserPlatformAdapter,
|
|
687
|
-
NodePlatformAdapter,
|
|
688
|
-
createBrowserPlatformAdapter,
|
|
689
|
-
createNodePlatformAdapter,
|
|
690
|
-
createPlatformAdapter,
|
|
691
|
-
createPlatformAdapterFor,
|
|
692
|
-
createPlatformAdapterSafe,
|
|
693
|
-
detectPlatform,
|
|
694
|
-
getPlatformCapabilities,
|
|
695
|
-
isPlatformSupported
|
|
696
|
-
};
|
|
697
|
-
//# sourceMappingURL=platform.node.js.map
|