@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.
- package/README.md +4 -13
- package/dist/{browser-Bb8gLWHp.d.ts → browser-DY8XDblx.d.ts} +14 -61
- package/dist/browser.d.ts +1 -1
- package/dist/browser.js +106 -723
- package/dist/browser.js.map +1 -1
- package/dist/chains.browser.cjs +2 -2
- package/dist/chains.browser.cjs.map +1 -1
- package/dist/chains.browser.js +2 -2
- package/dist/chains.browser.js.map +1 -1
- package/dist/chains.cjs +2 -2
- package/dist/chains.cjs.map +1 -1
- package/dist/chains.js +2 -2
- package/dist/chains.js.map +1 -1
- package/dist/chains.node.cjs +2 -2
- package/dist/chains.node.cjs.map +1 -1
- package/dist/chains.node.js +2 -2
- package/dist/chains.node.js.map +1 -1
- package/dist/index.browser.d.ts +201 -342
- package/dist/index.browser.js +628 -861
- package/dist/index.browser.js.map +1 -1
- package/dist/index.node.cjs +666 -1010
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.d.cts +205 -101
- package/dist/index.node.d.ts +205 -101
- package/dist/index.node.js +668 -1024
- package/dist/index.node.js.map +1 -1
- package/dist/node.cjs +53 -692
- package/dist/node.cjs.map +1 -1
- package/dist/node.js +52 -704
- package/dist/node.js.map +1 -1
- package/dist/platform.browser.d.ts +2 -2
- package/dist/platform.browser.js +116 -780
- package/dist/platform.browser.js.map +1 -1
- package/dist/platform.cjs +156 -933
- package/dist/platform.cjs.map +1 -1
- package/dist/platform.js +156 -945
- package/dist/platform.js.map +1 -1
- package/dist/platform.node.cjs +156 -933
- package/dist/platform.node.cjs.map +1 -1
- package/dist/platform.node.d.cts +14 -61
- package/dist/platform.node.d.ts +14 -61
- package/dist/platform.node.js +156 -945
- package/dist/platform.node.js.map +1 -1
- package/package.json +15 -31
package/dist/platform.node.cjs
CHANGED
|
@@ -30,6 +30,31 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
30
30
|
));
|
|
31
31
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
32
|
|
|
33
|
+
// src/platform/shared/crypto-utils.ts
|
|
34
|
+
function processWalletPublicKey(publicKey) {
|
|
35
|
+
const publicKeyHex = publicKey.startsWith("0x") ? publicKey.slice(2) : publicKey;
|
|
36
|
+
const publicKeyBytes = Buffer.from(publicKeyHex, "hex");
|
|
37
|
+
return publicKeyBytes.length === 64 ? Buffer.concat([Buffer.from([4]), publicKeyBytes]) : publicKeyBytes;
|
|
38
|
+
}
|
|
39
|
+
function processWalletPrivateKey(privateKey) {
|
|
40
|
+
const privateKeyHex = privateKey.startsWith("0x") ? privateKey.slice(2) : privateKey;
|
|
41
|
+
return Buffer.from(privateKeyHex, "hex");
|
|
42
|
+
}
|
|
43
|
+
function parseEncryptedDataBuffer(encryptedBuffer) {
|
|
44
|
+
return {
|
|
45
|
+
iv: encryptedBuffer.slice(0, 16),
|
|
46
|
+
ephemPublicKey: encryptedBuffer.slice(16, 81),
|
|
47
|
+
// 65 bytes for uncompressed public key
|
|
48
|
+
ciphertext: encryptedBuffer.slice(81, -32),
|
|
49
|
+
mac: encryptedBuffer.slice(-32)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
var init_crypto_utils = __esm({
|
|
53
|
+
"src/platform/shared/crypto-utils.ts"() {
|
|
54
|
+
"use strict";
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
33
58
|
// src/platform/shared/pgp-utils.ts
|
|
34
59
|
function processPGPKeyOptions(options) {
|
|
35
60
|
return {
|
|
@@ -91,812 +116,123 @@ var init_lazy_import = __esm({
|
|
|
91
116
|
}
|
|
92
117
|
});
|
|
93
118
|
|
|
94
|
-
// src/utils/encoding.ts
|
|
95
|
-
function toHex(data) {
|
|
96
|
-
if (typeof Buffer !== "undefined" && Buffer.from) {
|
|
97
|
-
return Buffer.from(data).toString("hex");
|
|
98
|
-
}
|
|
99
|
-
return Array.from(data, (byte) => byte.toString(16).padStart(2, "0")).join(
|
|
100
|
-
""
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
function fromHex(hex) {
|
|
104
|
-
const cleanHex = hex.startsWith("0x") ? hex.slice(2) : hex;
|
|
105
|
-
if (cleanHex.length % 2 !== 0) {
|
|
106
|
-
throw new Error("Invalid hex string: odd length");
|
|
107
|
-
}
|
|
108
|
-
if (!/^[0-9a-fA-F]*$/.test(cleanHex)) {
|
|
109
|
-
throw new Error("Invalid hex string: contains non-hex characters");
|
|
110
|
-
}
|
|
111
|
-
if (typeof Buffer !== "undefined" && Buffer.from) {
|
|
112
|
-
return new Uint8Array(Buffer.from(cleanHex, "hex"));
|
|
113
|
-
}
|
|
114
|
-
const bytes = new Uint8Array(cleanHex.length / 2);
|
|
115
|
-
for (let i = 0; i < cleanHex.length; i += 2) {
|
|
116
|
-
bytes[i / 2] = parseInt(cleanHex.substr(i, 2), 16);
|
|
117
|
-
}
|
|
118
|
-
return bytes;
|
|
119
|
-
}
|
|
120
|
-
function stringToBytes(str) {
|
|
121
|
-
if (typeof Buffer !== "undefined" && Buffer.from) {
|
|
122
|
-
return new Uint8Array(Buffer.from(str, "utf8"));
|
|
123
|
-
}
|
|
124
|
-
if (typeof TextEncoder !== "undefined") {
|
|
125
|
-
return new TextEncoder().encode(str);
|
|
126
|
-
}
|
|
127
|
-
const bytes = [];
|
|
128
|
-
for (let i = 0; i < str.length; i++) {
|
|
129
|
-
const char = str.charCodeAt(i);
|
|
130
|
-
if (char < 128) {
|
|
131
|
-
bytes.push(char);
|
|
132
|
-
} else if (char < 2048) {
|
|
133
|
-
bytes.push(192 | char >> 6, 128 | char & 63);
|
|
134
|
-
} else if (char < 55296 || char >= 57344) {
|
|
135
|
-
bytes.push(
|
|
136
|
-
224 | char >> 12,
|
|
137
|
-
128 | char >> 6 & 63,
|
|
138
|
-
128 | char & 63
|
|
139
|
-
);
|
|
140
|
-
} else {
|
|
141
|
-
i++;
|
|
142
|
-
const char2 = str.charCodeAt(i);
|
|
143
|
-
const codePoint = 65536 + ((char & 1023) << 10 | char2 & 1023);
|
|
144
|
-
bytes.push(
|
|
145
|
-
240 | codePoint >> 18,
|
|
146
|
-
128 | codePoint >> 12 & 63,
|
|
147
|
-
128 | codePoint >> 6 & 63,
|
|
148
|
-
128 | codePoint & 63
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return new Uint8Array(bytes);
|
|
153
|
-
}
|
|
154
|
-
function bytesToString(bytes) {
|
|
155
|
-
if (typeof Buffer !== "undefined" && Buffer.from) {
|
|
156
|
-
return Buffer.from(bytes).toString("utf8");
|
|
157
|
-
}
|
|
158
|
-
if (typeof TextDecoder !== "undefined") {
|
|
159
|
-
return new TextDecoder().decode(bytes);
|
|
160
|
-
}
|
|
161
|
-
let str = "";
|
|
162
|
-
let i = 0;
|
|
163
|
-
while (i < bytes.length) {
|
|
164
|
-
const byte = bytes[i];
|
|
165
|
-
if (byte < 128) {
|
|
166
|
-
str += String.fromCharCode(byte);
|
|
167
|
-
i++;
|
|
168
|
-
} else if ((byte & 224) === 192) {
|
|
169
|
-
str += String.fromCharCode((byte & 31) << 6 | bytes[i + 1] & 63);
|
|
170
|
-
i += 2;
|
|
171
|
-
} else if ((byte & 240) === 224) {
|
|
172
|
-
str += String.fromCharCode(
|
|
173
|
-
(byte & 15) << 12 | (bytes[i + 1] & 63) << 6 | bytes[i + 2] & 63
|
|
174
|
-
);
|
|
175
|
-
i += 3;
|
|
176
|
-
} else {
|
|
177
|
-
const codePoint = ((byte & 7) << 18 | (bytes[i + 1] & 63) << 12 | (bytes[i + 2] & 63) << 6 | bytes[i + 3] & 63) - 65536;
|
|
178
|
-
str += String.fromCharCode(
|
|
179
|
-
55296 + (codePoint >> 10),
|
|
180
|
-
56320 + (codePoint & 1023)
|
|
181
|
-
);
|
|
182
|
-
i += 4;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return str;
|
|
186
|
-
}
|
|
187
|
-
var init_encoding = __esm({
|
|
188
|
-
"src/utils/encoding.ts"() {
|
|
189
|
-
"use strict";
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
// src/utils/crypto-utils.ts
|
|
194
|
-
function concatBytes(...arrays) {
|
|
195
|
-
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
196
|
-
const result = new Uint8Array(totalLength);
|
|
197
|
-
let offset = 0;
|
|
198
|
-
for (const arr of arrays) {
|
|
199
|
-
result.set(arr, offset);
|
|
200
|
-
offset += arr.length;
|
|
201
|
-
}
|
|
202
|
-
return result;
|
|
203
|
-
}
|
|
204
|
-
function hexToBytes(hex) {
|
|
205
|
-
return fromHex(hex);
|
|
206
|
-
}
|
|
207
|
-
function bytesToHex(bytes) {
|
|
208
|
-
return toHex(bytes);
|
|
209
|
-
}
|
|
210
|
-
function processWalletPublicKey(publicKey) {
|
|
211
|
-
const publicKeyHex = typeof publicKey === "string" ? publicKey.startsWith("0x") ? publicKey.slice(2) : publicKey : bytesToHex(publicKey);
|
|
212
|
-
const publicKeyBytes = hexToBytes(publicKeyHex);
|
|
213
|
-
return publicKeyBytes.length === 64 ? concatBytes(new Uint8Array([4]), publicKeyBytes) : publicKeyBytes;
|
|
214
|
-
}
|
|
215
|
-
function processWalletPrivateKey(privateKey) {
|
|
216
|
-
const privateKeyHex = typeof privateKey === "string" ? privateKey.startsWith("0x") ? privateKey.slice(2) : privateKey : bytesToHex(privateKey);
|
|
217
|
-
return hexToBytes(privateKeyHex);
|
|
218
|
-
}
|
|
219
|
-
function parseEncryptedDataBuffer(encryptedBuffer) {
|
|
220
|
-
return {
|
|
221
|
-
iv: encryptedBuffer.slice(0, 16),
|
|
222
|
-
ephemPublicKey: encryptedBuffer.slice(16, 81),
|
|
223
|
-
// 65 bytes for uncompressed public key
|
|
224
|
-
ciphertext: encryptedBuffer.slice(81, -32),
|
|
225
|
-
mac: encryptedBuffer.slice(-32)
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
var init_crypto_utils = __esm({
|
|
229
|
-
"src/utils/crypto-utils.ts"() {
|
|
230
|
-
"use strict";
|
|
231
|
-
init_encoding();
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
// src/crypto/services/WalletKeyEncryptionService.ts
|
|
236
|
-
var WalletKeyEncryptionService;
|
|
237
|
-
var init_WalletKeyEncryptionService = __esm({
|
|
238
|
-
"src/crypto/services/WalletKeyEncryptionService.ts"() {
|
|
239
|
-
"use strict";
|
|
240
|
-
init_crypto_utils();
|
|
241
|
-
init_encoding();
|
|
242
|
-
WalletKeyEncryptionService = class {
|
|
243
|
-
eciesProvider;
|
|
244
|
-
constructor(config) {
|
|
245
|
-
this.eciesProvider = config.eciesProvider;
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* Encrypts data using a wallet's public key.
|
|
249
|
-
*
|
|
250
|
-
* @param data - The plaintext message to encrypt for the wallet owner.
|
|
251
|
-
* @param publicKey - The recipient wallet's public key for encryption.
|
|
252
|
-
* @returns A promise that resolves to the encrypted data as a hex string.
|
|
253
|
-
* @throws {Error} When encryption fails due to invalid key format.
|
|
254
|
-
*
|
|
255
|
-
* @example
|
|
256
|
-
* ```typescript
|
|
257
|
-
* const encrypted = await processor.encryptWithWalletPublicKey(
|
|
258
|
-
* "Secret message",
|
|
259
|
-
* "0x04..." // 65-byte uncompressed public key
|
|
260
|
-
* );
|
|
261
|
-
* console.log(`Encrypted: ${encrypted}`);
|
|
262
|
-
* ```
|
|
263
|
-
*/
|
|
264
|
-
async encryptWithWalletPublicKey(data, publicKey) {
|
|
265
|
-
const publicKeyBytes = processWalletPublicKey(publicKey);
|
|
266
|
-
const dataBytes = stringToBytes(data);
|
|
267
|
-
const encrypted = await this.eciesProvider.encrypt(
|
|
268
|
-
publicKeyBytes,
|
|
269
|
-
dataBytes
|
|
270
|
-
);
|
|
271
|
-
const result = concatBytes(
|
|
272
|
-
encrypted.iv,
|
|
273
|
-
encrypted.ephemPublicKey,
|
|
274
|
-
encrypted.ciphertext,
|
|
275
|
-
encrypted.mac
|
|
276
|
-
);
|
|
277
|
-
return bytesToHex(result);
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Decrypts data using a wallet's private key.
|
|
281
|
-
*
|
|
282
|
-
* @param encryptedData - The hex-encoded encrypted data to decrypt.
|
|
283
|
-
* @param privateKey - The wallet's private key for decryption.
|
|
284
|
-
* @returns A promise that resolves to the decrypted plaintext message.
|
|
285
|
-
* @throws {Error} When decryption fails due to invalid data or key format.
|
|
286
|
-
*
|
|
287
|
-
* @example
|
|
288
|
-
* ```typescript
|
|
289
|
-
* const decrypted = await processor.decryptWithWalletPrivateKey(
|
|
290
|
-
* encryptedHexString,
|
|
291
|
-
* "0x..." // 32-byte private key
|
|
292
|
-
* );
|
|
293
|
-
* console.log(`Decrypted: ${decrypted}`);
|
|
294
|
-
* ```
|
|
295
|
-
*/
|
|
296
|
-
async decryptWithWalletPrivateKey(encryptedData, privateKey) {
|
|
297
|
-
const privateKeyBytes = processWalletPrivateKey(privateKey);
|
|
298
|
-
const encryptedBytes = hexToBytes(encryptedData);
|
|
299
|
-
const encrypted = parseEncryptedDataBuffer(encryptedBytes);
|
|
300
|
-
const decrypted = await this.eciesProvider.decrypt(
|
|
301
|
-
privateKeyBytes,
|
|
302
|
-
encrypted
|
|
303
|
-
);
|
|
304
|
-
return bytesToString(decrypted);
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Encrypts a Uint8Array with a wallet public key
|
|
308
|
-
*
|
|
309
|
-
* @param data - Binary data to encrypt
|
|
310
|
-
* @param publicKey - Public key as hex string or Uint8Array
|
|
311
|
-
* @returns Encrypted data structure
|
|
312
|
-
*/
|
|
313
|
-
async encryptBinary(data, publicKey) {
|
|
314
|
-
const publicKeyBytes = processWalletPublicKey(publicKey);
|
|
315
|
-
return this.eciesProvider.encrypt(publicKeyBytes, data);
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Decrypts to a Uint8Array with a wallet private key
|
|
319
|
-
*
|
|
320
|
-
* @param encrypted - Encrypted data structure
|
|
321
|
-
* @param privateKey - Private key as hex string or Uint8Array
|
|
322
|
-
* @returns Decrypted binary data
|
|
323
|
-
*/
|
|
324
|
-
async decryptBinary(encrypted, privateKey) {
|
|
325
|
-
const privateKeyBytes = processWalletPrivateKey(privateKey);
|
|
326
|
-
return this.eciesProvider.decrypt(privateKeyBytes, encrypted);
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Gets the underlying ECIES provider
|
|
330
|
-
*
|
|
331
|
-
* @returns The ECIES provider instance
|
|
332
|
-
*/
|
|
333
|
-
getECIESProvider() {
|
|
334
|
-
return this.eciesProvider;
|
|
335
|
-
}
|
|
336
|
-
};
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
// src/crypto/ecies/utils.ts
|
|
341
|
-
function hexToBytes2(hex) {
|
|
342
|
-
if (hex.length % 2 !== 0) {
|
|
343
|
-
throw new Error("Hex string must have even length");
|
|
344
|
-
}
|
|
345
|
-
const bytes = new Uint8Array(hex.length / 2);
|
|
346
|
-
for (let i = 0; i < hex.length; i += 2) {
|
|
347
|
-
bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
348
|
-
}
|
|
349
|
-
return bytes;
|
|
350
|
-
}
|
|
351
|
-
function bytesToHex2(bytes) {
|
|
352
|
-
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
353
|
-
}
|
|
354
|
-
function stringToBytes2(str) {
|
|
355
|
-
return new TextEncoder().encode(str);
|
|
356
|
-
}
|
|
357
|
-
function bytesToString2(bytes) {
|
|
358
|
-
return new TextDecoder().decode(bytes);
|
|
359
|
-
}
|
|
360
|
-
function concatBytes2(...arrays) {
|
|
361
|
-
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
362
|
-
const result = new Uint8Array(totalLength);
|
|
363
|
-
let offset = 0;
|
|
364
|
-
for (const arr of arrays) {
|
|
365
|
-
result.set(arr, offset);
|
|
366
|
-
offset += arr.length;
|
|
367
|
-
}
|
|
368
|
-
return result;
|
|
369
|
-
}
|
|
370
|
-
function constantTimeEqual(a, b) {
|
|
371
|
-
if (a.length !== b.length) return false;
|
|
372
|
-
let result = 0;
|
|
373
|
-
for (let i = 0; i < a.length; i++) {
|
|
374
|
-
result |= a[i] ^ b[i];
|
|
375
|
-
}
|
|
376
|
-
return result === 0;
|
|
377
|
-
}
|
|
378
|
-
var init_utils = __esm({
|
|
379
|
-
"src/crypto/ecies/utils.ts"() {
|
|
380
|
-
"use strict";
|
|
381
|
-
}
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
// src/crypto/ecies/constants.ts
|
|
385
|
-
var CURVE, CIPHER, KDF, MAC, FORMAT;
|
|
386
|
-
var init_constants = __esm({
|
|
387
|
-
"src/crypto/ecies/constants.ts"() {
|
|
388
|
-
"use strict";
|
|
389
|
-
CURVE = {
|
|
390
|
-
/** The elliptic curve used (secp256k1 - same as Bitcoin/Ethereum) */
|
|
391
|
-
name: "secp256k1",
|
|
392
|
-
/** Private key length in bytes */
|
|
393
|
-
PRIVATE_KEY_LENGTH: 32,
|
|
394
|
-
/** Compressed public key length in bytes (0x02 or 0x03 prefix + 32 bytes) */
|
|
395
|
-
COMPRESSED_PUBLIC_KEY_LENGTH: 33,
|
|
396
|
-
/** Uncompressed public key length in bytes (0x04 prefix + 64 bytes) */
|
|
397
|
-
UNCOMPRESSED_PUBLIC_KEY_LENGTH: 65,
|
|
398
|
-
/** ECDH shared secret X coordinate length */
|
|
399
|
-
SHARED_SECRET_LENGTH: 32,
|
|
400
|
-
/** Public key prefixes */
|
|
401
|
-
PREFIX: {
|
|
402
|
-
/** Uncompressed public key prefix */
|
|
403
|
-
UNCOMPRESSED: 4,
|
|
404
|
-
/** Compressed public key prefix for even Y */
|
|
405
|
-
COMPRESSED_EVEN: 2,
|
|
406
|
-
/** Compressed public key prefix for odd Y */
|
|
407
|
-
COMPRESSED_ODD: 3
|
|
408
|
-
},
|
|
409
|
-
/** X coordinate starts at byte 1 (after prefix) */
|
|
410
|
-
X_COORDINATE_OFFSET: 1,
|
|
411
|
-
/** X coordinate ends at byte 33 (1 + 32) */
|
|
412
|
-
X_COORDINATE_END: 33
|
|
413
|
-
};
|
|
414
|
-
CIPHER = {
|
|
415
|
-
/** Cipher algorithm - must match eccrypto */
|
|
416
|
-
algorithm: "aes-256-cbc",
|
|
417
|
-
/** AES key length in bytes */
|
|
418
|
-
KEY_LENGTH: 32,
|
|
419
|
-
/** Initialization vector length in bytes */
|
|
420
|
-
IV_LENGTH: 16,
|
|
421
|
-
/** Block size for AES */
|
|
422
|
-
BLOCK_SIZE: 16
|
|
423
|
-
};
|
|
424
|
-
KDF = {
|
|
425
|
-
/** Hash algorithm for key derivation - must match eccrypto */
|
|
426
|
-
algorithm: "sha512",
|
|
427
|
-
/** Output length of SHA-512 in bytes */
|
|
428
|
-
OUTPUT_LENGTH: 64,
|
|
429
|
-
/** Encryption key slice (first 32 bytes of KDF output) */
|
|
430
|
-
ENCRYPTION_KEY_OFFSET: 0,
|
|
431
|
-
ENCRYPTION_KEY_LENGTH: 32,
|
|
432
|
-
/** MAC key slice (last 32 bytes of KDF output) */
|
|
433
|
-
MAC_KEY_OFFSET: 32,
|
|
434
|
-
MAC_KEY_LENGTH: 32
|
|
435
|
-
};
|
|
436
|
-
MAC = {
|
|
437
|
-
/** MAC algorithm - must match eccrypto */
|
|
438
|
-
algorithm: "sha256",
|
|
439
|
-
/** HMAC-SHA256 output length in bytes */
|
|
440
|
-
LENGTH: 32
|
|
441
|
-
};
|
|
442
|
-
FORMAT = {
|
|
443
|
-
/** Offsets for each component in serialized format */
|
|
444
|
-
IV_OFFSET: 0,
|
|
445
|
-
IV_LENGTH: CIPHER.IV_LENGTH,
|
|
446
|
-
/** Ephemeral public key (always uncompressed in eccrypto format) */
|
|
447
|
-
EPHEMERAL_KEY_OFFSET: CIPHER.IV_LENGTH,
|
|
448
|
-
EPHEMERAL_KEY_LENGTH: CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH,
|
|
449
|
-
/** Ciphertext starts after IV and ephemeral key */
|
|
450
|
-
CIPHERTEXT_OFFSET: CIPHER.IV_LENGTH + CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH,
|
|
451
|
-
/** MAC is always the last 32 bytes */
|
|
452
|
-
MAC_LENGTH: MAC.LENGTH,
|
|
453
|
-
/** Minimum size of encrypted data (IV + ephemKey + MAC, no ciphertext) */
|
|
454
|
-
MIN_ENCRYPTED_LENGTH: CIPHER.IV_LENGTH + CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH + MAC.LENGTH,
|
|
455
|
-
/**
|
|
456
|
-
* Helper to calculate total length of encrypted data
|
|
457
|
-
*
|
|
458
|
-
* @param ciphertextLength - Length of the ciphertext portion
|
|
459
|
-
* @returns Total length including all components
|
|
460
|
-
*/
|
|
461
|
-
getTotalLength: (ciphertextLength) => CIPHER.IV_LENGTH + CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH + ciphertextLength + MAC.LENGTH
|
|
462
|
-
};
|
|
463
|
-
}
|
|
464
|
-
});
|
|
465
|
-
|
|
466
|
-
// src/crypto/ecies/interface.ts
|
|
467
|
-
function isECIESEncrypted(obj) {
|
|
468
|
-
if (!obj || typeof obj !== "object") return false;
|
|
469
|
-
const enc = obj;
|
|
470
|
-
const isUint8Array = (value) => {
|
|
471
|
-
return value instanceof Uint8Array || typeof Buffer !== "undefined" && Buffer.isBuffer(value);
|
|
472
|
-
};
|
|
473
|
-
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;
|
|
474
|
-
}
|
|
475
|
-
var ECIESError;
|
|
476
|
-
var init_interface = __esm({
|
|
477
|
-
"src/crypto/ecies/interface.ts"() {
|
|
478
|
-
"use strict";
|
|
479
|
-
init_constants();
|
|
480
|
-
ECIESError = class extends Error {
|
|
481
|
-
constructor(message, code, cause) {
|
|
482
|
-
super(message);
|
|
483
|
-
this.code = code;
|
|
484
|
-
this.cause = cause;
|
|
485
|
-
this.name = "ECIESError";
|
|
486
|
-
}
|
|
487
|
-
};
|
|
488
|
-
}
|
|
489
|
-
});
|
|
490
|
-
|
|
491
|
-
// src/crypto/ecies/base.ts
|
|
492
|
-
var BaseECIESUint8;
|
|
493
|
-
var init_base = __esm({
|
|
494
|
-
"src/crypto/ecies/base.ts"() {
|
|
495
|
-
"use strict";
|
|
496
|
-
init_interface();
|
|
497
|
-
init_constants();
|
|
498
|
-
init_utils();
|
|
499
|
-
BaseECIESUint8 = class _BaseECIESUint8 {
|
|
500
|
-
// Cache for validated public keys to avoid repeated validation
|
|
501
|
-
static validatedKeys = /* @__PURE__ */ new WeakMap();
|
|
502
|
-
/**
|
|
503
|
-
* Normalizes a public key to uncompressed format.
|
|
504
|
-
*
|
|
505
|
-
* @param publicKey - Public key in any format.
|
|
506
|
-
* @returns Uncompressed public key (65 bytes).
|
|
507
|
-
* @throws {ECIESError} If key format is invalid.
|
|
508
|
-
*/
|
|
509
|
-
normalizePublicKey(publicKey) {
|
|
510
|
-
if (_BaseECIESUint8.validatedKeys.has(publicKey)) {
|
|
511
|
-
return publicKey;
|
|
512
|
-
}
|
|
513
|
-
if (publicKey.length === CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH) {
|
|
514
|
-
if (publicKey[0] !== CURVE.PREFIX.UNCOMPRESSED) {
|
|
515
|
-
throw new ECIESError(
|
|
516
|
-
"Invalid uncompressed public key prefix",
|
|
517
|
-
"INVALID_KEY"
|
|
518
|
-
);
|
|
519
|
-
}
|
|
520
|
-
if (!this.validatePublicKey(publicKey)) {
|
|
521
|
-
throw new ECIESError("Invalid public key", "INVALID_KEY");
|
|
522
|
-
}
|
|
523
|
-
_BaseECIESUint8.validatedKeys.set(publicKey, true);
|
|
524
|
-
return publicKey;
|
|
525
|
-
}
|
|
526
|
-
if (publicKey.length === CURVE.COMPRESSED_PUBLIC_KEY_LENGTH) {
|
|
527
|
-
const decompressed = this.decompressPublicKey(publicKey);
|
|
528
|
-
if (!decompressed) {
|
|
529
|
-
throw new ECIESError("Failed to decompress public key", "INVALID_KEY");
|
|
530
|
-
}
|
|
531
|
-
_BaseECIESUint8.validatedKeys.set(decompressed, true);
|
|
532
|
-
return decompressed;
|
|
533
|
-
}
|
|
534
|
-
throw new ECIESError(
|
|
535
|
-
`Invalid public key length: ${publicKey.length}`,
|
|
536
|
-
"INVALID_KEY"
|
|
537
|
-
);
|
|
538
|
-
}
|
|
539
|
-
/**
|
|
540
|
-
* Encrypts data using ECIES.
|
|
541
|
-
*
|
|
542
|
-
* @param publicKey - The recipient's public key (compressed or uncompressed)
|
|
543
|
-
* @param message - The data to encrypt
|
|
544
|
-
* @returns Promise resolving to encrypted data structure
|
|
545
|
-
*/
|
|
546
|
-
async encrypt(publicKey, message) {
|
|
547
|
-
try {
|
|
548
|
-
if (!(publicKey instanceof Uint8Array)) {
|
|
549
|
-
throw new ECIESError("Public key must be a Uint8Array", "INVALID_KEY");
|
|
550
|
-
}
|
|
551
|
-
if (!(message instanceof Uint8Array)) {
|
|
552
|
-
throw new ECIESError(
|
|
553
|
-
"Message must be a Uint8Array",
|
|
554
|
-
"ENCRYPTION_FAILED"
|
|
555
|
-
);
|
|
556
|
-
}
|
|
557
|
-
if (publicKey.length === 0) {
|
|
558
|
-
throw new ECIESError("Public key cannot be empty", "INVALID_KEY");
|
|
559
|
-
}
|
|
560
|
-
const pubKey = this.normalizePublicKey(publicKey);
|
|
561
|
-
let ephemeralPrivateKey;
|
|
562
|
-
do {
|
|
563
|
-
ephemeralPrivateKey = this.generateRandomBytes(
|
|
564
|
-
CURVE.PRIVATE_KEY_LENGTH
|
|
565
|
-
);
|
|
566
|
-
} while (!this.verifyPrivateKey(ephemeralPrivateKey));
|
|
567
|
-
const ephemeralPublicKey = this.createPublicKey(
|
|
568
|
-
ephemeralPrivateKey,
|
|
569
|
-
false
|
|
570
|
-
);
|
|
571
|
-
if (!ephemeralPublicKey) {
|
|
572
|
-
throw new ECIESError(
|
|
573
|
-
"Failed to generate ephemeral public key",
|
|
574
|
-
"ENCRYPTION_FAILED"
|
|
575
|
-
);
|
|
576
|
-
}
|
|
577
|
-
const sharedSecret = this.performECDH(pubKey, ephemeralPrivateKey);
|
|
578
|
-
const kdf = this.sha512(sharedSecret);
|
|
579
|
-
const encryptionKey = kdf.slice(
|
|
580
|
-
KDF.ENCRYPTION_KEY_OFFSET,
|
|
581
|
-
KDF.ENCRYPTION_KEY_OFFSET + KDF.ENCRYPTION_KEY_LENGTH
|
|
582
|
-
);
|
|
583
|
-
const macKey = kdf.slice(
|
|
584
|
-
KDF.MAC_KEY_OFFSET,
|
|
585
|
-
KDF.MAC_KEY_OFFSET + KDF.MAC_KEY_LENGTH
|
|
586
|
-
);
|
|
587
|
-
const iv = this.generateRandomBytes(CIPHER.IV_LENGTH);
|
|
588
|
-
const ciphertext = await this.aesEncrypt(encryptionKey, iv, message);
|
|
589
|
-
const macData = concatBytes2(iv, ephemeralPublicKey, ciphertext);
|
|
590
|
-
const mac = this.hmacSha256(macKey, macData);
|
|
591
|
-
this.clearBuffer(ephemeralPrivateKey);
|
|
592
|
-
this.clearBuffer(sharedSecret);
|
|
593
|
-
this.clearBuffer(kdf);
|
|
594
|
-
return {
|
|
595
|
-
iv,
|
|
596
|
-
ephemPublicKey: ephemeralPublicKey,
|
|
597
|
-
ciphertext,
|
|
598
|
-
mac
|
|
599
|
-
};
|
|
600
|
-
} catch (error) {
|
|
601
|
-
if (error instanceof ECIESError) throw error;
|
|
602
|
-
throw new ECIESError(
|
|
603
|
-
`Encryption failed: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
604
|
-
"ENCRYPTION_FAILED",
|
|
605
|
-
error instanceof Error ? error : void 0
|
|
606
|
-
);
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
/**
|
|
610
|
-
* Decrypts ECIES encrypted data.
|
|
611
|
-
*
|
|
612
|
-
* @param privateKey - The recipient's private key (32 bytes)
|
|
613
|
-
* @param encrypted - The encrypted data structure from encrypt()
|
|
614
|
-
* @returns Promise resolving to the original plaintext
|
|
615
|
-
*/
|
|
616
|
-
async decrypt(privateKey, encrypted) {
|
|
617
|
-
try {
|
|
618
|
-
if (!(privateKey instanceof Uint8Array)) {
|
|
619
|
-
throw new ECIESError("Private key must be a Uint8Array", "INVALID_KEY");
|
|
620
|
-
}
|
|
621
|
-
if (!isECIESEncrypted(encrypted)) {
|
|
622
|
-
throw new ECIESError(
|
|
623
|
-
"Invalid encrypted data structure",
|
|
624
|
-
"DECRYPTION_FAILED"
|
|
625
|
-
);
|
|
626
|
-
}
|
|
627
|
-
if (privateKey.length !== CURVE.PRIVATE_KEY_LENGTH) {
|
|
628
|
-
throw new ECIESError(
|
|
629
|
-
`Invalid private key length: ${privateKey.length}`,
|
|
630
|
-
"INVALID_KEY"
|
|
631
|
-
);
|
|
632
|
-
}
|
|
633
|
-
if (!this.verifyPrivateKey(privateKey)) {
|
|
634
|
-
throw new ECIESError("Invalid private key", "INVALID_KEY");
|
|
635
|
-
}
|
|
636
|
-
const ephemeralPublicKey = this.normalizePublicKey(
|
|
637
|
-
encrypted.ephemPublicKey
|
|
638
|
-
);
|
|
639
|
-
const sharedSecret = this.performECDH(ephemeralPublicKey, privateKey);
|
|
640
|
-
const kdf = this.sha512(sharedSecret);
|
|
641
|
-
const encryptionKey = kdf.slice(
|
|
642
|
-
KDF.ENCRYPTION_KEY_OFFSET,
|
|
643
|
-
KDF.ENCRYPTION_KEY_OFFSET + KDF.ENCRYPTION_KEY_LENGTH
|
|
644
|
-
);
|
|
645
|
-
const macKey = kdf.slice(
|
|
646
|
-
KDF.MAC_KEY_OFFSET,
|
|
647
|
-
KDF.MAC_KEY_OFFSET + KDF.MAC_KEY_LENGTH
|
|
648
|
-
);
|
|
649
|
-
const macData = concatBytes2(
|
|
650
|
-
encrypted.iv,
|
|
651
|
-
encrypted.ephemPublicKey,
|
|
652
|
-
encrypted.ciphertext
|
|
653
|
-
);
|
|
654
|
-
const expectedMac = this.hmacSha256(macKey, macData);
|
|
655
|
-
if (!constantTimeEqual(encrypted.mac, expectedMac)) {
|
|
656
|
-
throw new ECIESError("MAC verification failed", "MAC_MISMATCH");
|
|
657
|
-
}
|
|
658
|
-
const decrypted = await this.aesDecrypt(
|
|
659
|
-
encryptionKey,
|
|
660
|
-
encrypted.iv,
|
|
661
|
-
encrypted.ciphertext
|
|
662
|
-
);
|
|
663
|
-
this.clearBuffer(sharedSecret);
|
|
664
|
-
this.clearBuffer(kdf);
|
|
665
|
-
return decrypted;
|
|
666
|
-
} catch (error) {
|
|
667
|
-
if (error instanceof ECIESError) throw error;
|
|
668
|
-
throw new ECIESError(
|
|
669
|
-
`Decryption failed: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
670
|
-
"DECRYPTION_FAILED",
|
|
671
|
-
error instanceof Error ? error : void 0
|
|
672
|
-
);
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
/**
|
|
676
|
-
* Clears sensitive data from memory using multi-pass overwrite.
|
|
677
|
-
*
|
|
678
|
-
* @remarks
|
|
679
|
-
* Uses multiple passes with different patterns to make it harder
|
|
680
|
-
* for JIT compilers to optimize away the operation. While not
|
|
681
|
-
* guaranteed in JavaScript, this is a best-effort approach to
|
|
682
|
-
* clear sensitive data from memory.
|
|
683
|
-
*
|
|
684
|
-
* @param buffer - The buffer to clear
|
|
685
|
-
*/
|
|
686
|
-
clearBuffer(buffer) {
|
|
687
|
-
if (buffer && buffer.length > 0) {
|
|
688
|
-
buffer.fill(0);
|
|
689
|
-
buffer.fill(255);
|
|
690
|
-
buffer.fill(170);
|
|
691
|
-
buffer.fill(0);
|
|
692
|
-
for (let i = 0; i < buffer.length; i++) {
|
|
693
|
-
buffer[i] = i & 255 ^ 90;
|
|
694
|
-
}
|
|
695
|
-
buffer.fill(0);
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
};
|
|
699
|
-
}
|
|
700
|
-
});
|
|
701
|
-
|
|
702
|
-
// src/crypto/ecies/browser.ts
|
|
703
|
-
var secp256k1, import_hmac, import_sha2, BrowserECIESUint8Provider;
|
|
704
|
-
var init_browser = __esm({
|
|
705
|
-
"src/crypto/ecies/browser.ts"() {
|
|
706
|
-
"use strict";
|
|
707
|
-
secp256k1 = __toESM(require("@noble/secp256k1"), 1);
|
|
708
|
-
init_base();
|
|
709
|
-
import_hmac = require("@noble/hashes/hmac");
|
|
710
|
-
import_sha2 = require("@noble/hashes/sha2");
|
|
711
|
-
BrowserECIESUint8Provider = class extends BaseECIESUint8 {
|
|
712
|
-
generateRandomBytes(length) {
|
|
713
|
-
const bytes = new Uint8Array(length);
|
|
714
|
-
crypto.getRandomValues(bytes);
|
|
715
|
-
return bytes;
|
|
716
|
-
}
|
|
717
|
-
verifyPrivateKey(privateKey) {
|
|
718
|
-
try {
|
|
719
|
-
return secp256k1.utils.isValidPrivateKey(privateKey);
|
|
720
|
-
} catch {
|
|
721
|
-
return false;
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
createPublicKey(privateKey, compressed) {
|
|
725
|
-
try {
|
|
726
|
-
return secp256k1.getPublicKey(privateKey, compressed);
|
|
727
|
-
} catch {
|
|
728
|
-
return null;
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
validatePublicKey(publicKey) {
|
|
732
|
-
try {
|
|
733
|
-
secp256k1.Point.fromHex(publicKey);
|
|
734
|
-
return true;
|
|
735
|
-
} catch {
|
|
736
|
-
return false;
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
decompressPublicKey(publicKey) {
|
|
740
|
-
try {
|
|
741
|
-
const point = secp256k1.Point.fromHex(publicKey);
|
|
742
|
-
return point.toRawBytes(false);
|
|
743
|
-
} catch {
|
|
744
|
-
return null;
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
performECDH(publicKey, privateKey) {
|
|
748
|
-
try {
|
|
749
|
-
const sharedPoint = secp256k1.getSharedSecret(
|
|
750
|
-
privateKey,
|
|
751
|
-
publicKey,
|
|
752
|
-
true
|
|
753
|
-
);
|
|
754
|
-
return sharedPoint.slice(1);
|
|
755
|
-
} catch (error) {
|
|
756
|
-
throw new Error(
|
|
757
|
-
`ECDH failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
758
|
-
);
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
sha512(data) {
|
|
762
|
-
return (0, import_sha2.sha512)(data);
|
|
763
|
-
}
|
|
764
|
-
hmacSha256(key, data) {
|
|
765
|
-
return (0, import_hmac.hmac)(import_sha2.sha256, key, data);
|
|
766
|
-
}
|
|
767
|
-
async aesEncrypt(key, iv, plaintext) {
|
|
768
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
769
|
-
"raw",
|
|
770
|
-
key,
|
|
771
|
-
{ name: "AES-CBC" },
|
|
772
|
-
false,
|
|
773
|
-
["encrypt"]
|
|
774
|
-
);
|
|
775
|
-
const encrypted = await crypto.subtle.encrypt(
|
|
776
|
-
{ name: "AES-CBC", iv },
|
|
777
|
-
cryptoKey,
|
|
778
|
-
plaintext
|
|
779
|
-
);
|
|
780
|
-
return new Uint8Array(encrypted);
|
|
781
|
-
}
|
|
782
|
-
async aesDecrypt(key, iv, ciphertext) {
|
|
783
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
784
|
-
"raw",
|
|
785
|
-
key,
|
|
786
|
-
{ name: "AES-CBC" },
|
|
787
|
-
false,
|
|
788
|
-
["decrypt"]
|
|
789
|
-
);
|
|
790
|
-
const decrypted = await crypto.subtle.decrypt(
|
|
791
|
-
{ name: "AES-CBC", iv },
|
|
792
|
-
cryptoKey,
|
|
793
|
-
ciphertext
|
|
794
|
-
);
|
|
795
|
-
return new Uint8Array(decrypted);
|
|
796
|
-
}
|
|
797
|
-
};
|
|
798
|
-
}
|
|
799
|
-
});
|
|
800
|
-
|
|
801
119
|
// src/platform/browser.ts
|
|
802
120
|
var browser_exports = {};
|
|
803
121
|
__export(browser_exports, {
|
|
804
|
-
BrowserPlatformAdapter: () => BrowserPlatformAdapter
|
|
122
|
+
BrowserPlatformAdapter: () => BrowserPlatformAdapter,
|
|
123
|
+
browserPlatformAdapter: () => browserPlatformAdapter
|
|
805
124
|
});
|
|
806
|
-
var
|
|
807
|
-
var
|
|
125
|
+
var getOpenPGP, BrowserCryptoAdapter, BrowserPGPAdapter, BrowserHttpAdapter, BrowserCacheAdapter, BrowserPlatformAdapter, browserPlatformAdapter;
|
|
126
|
+
var init_browser = __esm({
|
|
808
127
|
"src/platform/browser.ts"() {
|
|
809
128
|
"use strict";
|
|
129
|
+
init_crypto_utils();
|
|
810
130
|
init_pgp_utils();
|
|
811
131
|
init_error_utils();
|
|
812
132
|
init_lazy_import();
|
|
813
|
-
init_WalletKeyEncryptionService();
|
|
814
|
-
init_crypto_utils();
|
|
815
|
-
init_utils();
|
|
816
|
-
secp256k12 = __toESM(require("@noble/secp256k1"), 1);
|
|
817
|
-
init_browser();
|
|
818
133
|
getOpenPGP = lazyImport(() => import("openpgp"));
|
|
819
134
|
BrowserCryptoAdapter = class {
|
|
820
|
-
eciesProvider = new BrowserECIESUint8Provider();
|
|
821
|
-
walletKeyEncryptionService = new WalletKeyEncryptionService({
|
|
822
|
-
eciesProvider: this.eciesProvider
|
|
823
|
-
});
|
|
824
135
|
async encryptWithPublicKey(data, publicKeyHex) {
|
|
825
136
|
try {
|
|
826
|
-
const
|
|
827
|
-
const
|
|
828
|
-
|
|
829
|
-
|
|
137
|
+
const eccrypto = await import("eccrypto-js");
|
|
138
|
+
const publicKeyBuffer = Buffer.from(publicKeyHex, "hex");
|
|
139
|
+
const encrypted = await eccrypto.encrypt(
|
|
140
|
+
publicKeyBuffer,
|
|
141
|
+
Buffer.from(data, "utf8")
|
|
830
142
|
);
|
|
831
|
-
const result =
|
|
143
|
+
const result = Buffer.concat([
|
|
832
144
|
encrypted.iv,
|
|
833
145
|
encrypted.ephemPublicKey,
|
|
834
146
|
encrypted.ciphertext,
|
|
835
147
|
encrypted.mac
|
|
836
|
-
);
|
|
837
|
-
return
|
|
148
|
+
]);
|
|
149
|
+
return result.toString("hex");
|
|
838
150
|
} catch (error) {
|
|
839
|
-
throw
|
|
151
|
+
throw new Error(`Encryption failed: ${error}`);
|
|
840
152
|
}
|
|
841
153
|
}
|
|
842
154
|
async decryptWithPrivateKey(encryptedData, privateKeyHex) {
|
|
843
155
|
try {
|
|
844
|
-
const
|
|
845
|
-
const
|
|
846
|
-
const
|
|
847
|
-
const
|
|
848
|
-
|
|
849
|
-
|
|
156
|
+
const eccrypto = await import("eccrypto-js");
|
|
157
|
+
const privateKeyBuffer = processWalletPrivateKey(privateKeyHex);
|
|
158
|
+
const encryptedBuffer = Buffer.from(encryptedData, "hex");
|
|
159
|
+
const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
|
|
160
|
+
const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
|
|
161
|
+
const decryptedBuffer = await eccrypto.decrypt(
|
|
162
|
+
privateKeyBuffer,
|
|
163
|
+
encryptedObj
|
|
850
164
|
);
|
|
851
|
-
return
|
|
165
|
+
return decryptedBuffer.toString("utf8");
|
|
852
166
|
} catch (error) {
|
|
853
|
-
throw
|
|
167
|
+
throw new Error(`Decryption failed: ${error}`);
|
|
854
168
|
}
|
|
855
169
|
}
|
|
856
|
-
async
|
|
170
|
+
async generateKeyPair() {
|
|
857
171
|
try {
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
);
|
|
172
|
+
const eccrypto = await import("eccrypto-js");
|
|
173
|
+
const privateKeyBytes = new Uint8Array(32);
|
|
174
|
+
crypto.getRandomValues(privateKeyBytes);
|
|
175
|
+
const privateKey = Buffer.from(privateKeyBytes);
|
|
176
|
+
const publicKey = eccrypto.getPublicCompressed(privateKey);
|
|
177
|
+
return {
|
|
178
|
+
privateKey: privateKey.toString("hex"),
|
|
179
|
+
publicKey: publicKey.toString("hex")
|
|
180
|
+
};
|
|
862
181
|
} catch (error) {
|
|
863
|
-
throw wrapCryptoError("
|
|
182
|
+
throw wrapCryptoError("key generation", error);
|
|
864
183
|
}
|
|
865
184
|
}
|
|
866
|
-
async
|
|
185
|
+
async encryptWithWalletPublicKey(data, publicKey) {
|
|
867
186
|
try {
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
187
|
+
const eccrypto = await import("eccrypto-js");
|
|
188
|
+
const uncompressedKey = processWalletPublicKey(publicKey);
|
|
189
|
+
const encryptedBuffer = await eccrypto.encrypt(
|
|
190
|
+
uncompressedKey,
|
|
191
|
+
Buffer.from(data)
|
|
871
192
|
);
|
|
193
|
+
const result = Buffer.concat([
|
|
194
|
+
encryptedBuffer.iv,
|
|
195
|
+
encryptedBuffer.ephemPublicKey,
|
|
196
|
+
encryptedBuffer.ciphertext,
|
|
197
|
+
encryptedBuffer.mac
|
|
198
|
+
]);
|
|
199
|
+
return result.toString("hex");
|
|
872
200
|
} catch (error) {
|
|
873
|
-
throw wrapCryptoError("
|
|
201
|
+
throw wrapCryptoError("encrypt with wallet public key", error);
|
|
874
202
|
}
|
|
875
203
|
}
|
|
876
|
-
async
|
|
204
|
+
async decryptWithWalletPrivateKey(encryptedData, privateKey) {
|
|
877
205
|
try {
|
|
878
|
-
const
|
|
879
|
-
const
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
206
|
+
const eccrypto = await import("eccrypto-js");
|
|
207
|
+
const privateKeyBuffer = processWalletPrivateKey(privateKey);
|
|
208
|
+
const encryptedBuffer = Buffer.from(encryptedData, "hex");
|
|
209
|
+
const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
|
|
210
|
+
const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
|
|
211
|
+
const decryptedBuffer = await eccrypto.decrypt(
|
|
212
|
+
privateKeyBuffer,
|
|
213
|
+
encryptedObj
|
|
214
|
+
);
|
|
215
|
+
return decryptedBuffer.toString("utf8");
|
|
884
216
|
} catch (error) {
|
|
885
|
-
throw wrapCryptoError("
|
|
217
|
+
throw wrapCryptoError("decrypt with wallet private key", error);
|
|
886
218
|
}
|
|
887
219
|
}
|
|
888
220
|
async encryptWithPassword(data, password) {
|
|
889
221
|
try {
|
|
890
222
|
const openpgp = await getOpenPGP();
|
|
891
|
-
const message = await openpgp.createMessage({
|
|
223
|
+
const message = await openpgp.createMessage({
|
|
224
|
+
binary: data
|
|
225
|
+
});
|
|
892
226
|
const encrypted = await openpgp.encrypt({
|
|
893
227
|
message,
|
|
894
228
|
passwords: [password],
|
|
895
229
|
format: "binary"
|
|
896
230
|
});
|
|
897
|
-
|
|
231
|
+
const response = new Response(encrypted);
|
|
232
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
233
|
+
return new Uint8Array(arrayBuffer);
|
|
898
234
|
} catch (error) {
|
|
899
|
-
throw
|
|
235
|
+
throw new Error(`Failed to encrypt with password: ${error}`);
|
|
900
236
|
}
|
|
901
237
|
}
|
|
902
238
|
async decryptWithPassword(encryptedData, password) {
|
|
@@ -905,14 +241,14 @@ var init_browser2 = __esm({
|
|
|
905
241
|
const message = await openpgp.readMessage({
|
|
906
242
|
binaryMessage: encryptedData
|
|
907
243
|
});
|
|
908
|
-
const { data } = await openpgp.decrypt({
|
|
244
|
+
const { data: decrypted } = await openpgp.decrypt({
|
|
909
245
|
message,
|
|
910
246
|
passwords: [password],
|
|
911
247
|
format: "binary"
|
|
912
248
|
});
|
|
913
|
-
return new Uint8Array(
|
|
249
|
+
return new Uint8Array(decrypted);
|
|
914
250
|
} catch (error) {
|
|
915
|
-
throw
|
|
251
|
+
throw new Error(`Failed to decrypt with password: ${error}`);
|
|
916
252
|
}
|
|
917
253
|
}
|
|
918
254
|
};
|
|
@@ -964,6 +300,9 @@ var init_browser2 = __esm({
|
|
|
964
300
|
};
|
|
965
301
|
BrowserHttpAdapter = class {
|
|
966
302
|
async fetch(url, options) {
|
|
303
|
+
if (typeof fetch === "undefined") {
|
|
304
|
+
throw new Error("Fetch API not available in this browser environment");
|
|
305
|
+
}
|
|
967
306
|
return fetch(url, options);
|
|
968
307
|
}
|
|
969
308
|
};
|
|
@@ -981,19 +320,17 @@ var init_browser2 = __esm({
|
|
|
981
320
|
}
|
|
982
321
|
set(key, value) {
|
|
983
322
|
try {
|
|
984
|
-
if (typeof sessionStorage
|
|
985
|
-
|
|
323
|
+
if (typeof sessionStorage !== "undefined") {
|
|
324
|
+
sessionStorage.setItem(this.prefix + key, value);
|
|
986
325
|
}
|
|
987
|
-
sessionStorage.setItem(this.prefix + key, value);
|
|
988
326
|
} catch {
|
|
989
327
|
}
|
|
990
328
|
}
|
|
991
329
|
delete(key) {
|
|
992
330
|
try {
|
|
993
|
-
if (typeof sessionStorage
|
|
994
|
-
|
|
331
|
+
if (typeof sessionStorage !== "undefined") {
|
|
332
|
+
sessionStorage.removeItem(this.prefix + key);
|
|
995
333
|
}
|
|
996
|
-
sessionStorage.removeItem(this.prefix + key);
|
|
997
334
|
} catch {
|
|
998
335
|
}
|
|
999
336
|
}
|
|
@@ -1002,25 +339,30 @@ var init_browser2 = __esm({
|
|
|
1002
339
|
if (typeof sessionStorage === "undefined") {
|
|
1003
340
|
return;
|
|
1004
341
|
}
|
|
1005
|
-
const
|
|
1006
|
-
for (
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
keysToRemove.push(key);
|
|
342
|
+
const keys = Object.keys(sessionStorage);
|
|
343
|
+
for (const key of keys) {
|
|
344
|
+
if (key.startsWith(this.prefix)) {
|
|
345
|
+
sessionStorage.removeItem(key);
|
|
1010
346
|
}
|
|
1011
347
|
}
|
|
1012
|
-
keysToRemove.forEach((key) => sessionStorage.removeItem(key));
|
|
1013
348
|
} catch {
|
|
1014
349
|
}
|
|
1015
350
|
}
|
|
1016
351
|
};
|
|
1017
352
|
BrowserPlatformAdapter = class {
|
|
1018
|
-
crypto
|
|
1019
|
-
pgp
|
|
1020
|
-
http
|
|
1021
|
-
cache
|
|
353
|
+
crypto;
|
|
354
|
+
pgp;
|
|
355
|
+
http;
|
|
356
|
+
cache;
|
|
1022
357
|
platform = "browser";
|
|
358
|
+
constructor() {
|
|
359
|
+
this.crypto = new BrowserCryptoAdapter();
|
|
360
|
+
this.pgp = new BrowserPGPAdapter();
|
|
361
|
+
this.http = new BrowserHttpAdapter();
|
|
362
|
+
this.cache = new BrowserCacheAdapter();
|
|
363
|
+
}
|
|
1023
364
|
};
|
|
365
|
+
browserPlatformAdapter = new BrowserPlatformAdapter();
|
|
1024
366
|
}
|
|
1025
367
|
});
|
|
1026
368
|
|
|
@@ -1039,9 +381,10 @@ __export(platform_node_exports, {
|
|
|
1039
381
|
isPlatformSupported: () => isPlatformSupported
|
|
1040
382
|
});
|
|
1041
383
|
module.exports = __toCommonJS(platform_node_exports);
|
|
1042
|
-
|
|
384
|
+
init_browser();
|
|
1043
385
|
|
|
1044
386
|
// src/platform/node.ts
|
|
387
|
+
init_crypto_utils();
|
|
1045
388
|
init_pgp_utils();
|
|
1046
389
|
init_error_utils();
|
|
1047
390
|
|
|
@@ -1070,123 +413,15 @@ async function streamToUint8Array(stream) {
|
|
|
1070
413
|
|
|
1071
414
|
// src/platform/node.ts
|
|
1072
415
|
init_lazy_import();
|
|
1073
|
-
init_WalletKeyEncryptionService();
|
|
1074
|
-
init_crypto_utils();
|
|
1075
|
-
|
|
1076
|
-
// src/crypto/ecies/node.ts
|
|
1077
|
-
var import_crypto = require("crypto");
|
|
1078
|
-
init_base();
|
|
1079
|
-
var secp256k13;
|
|
1080
|
-
try {
|
|
1081
|
-
secp256k13 = require("secp256k1");
|
|
1082
|
-
} catch {
|
|
1083
|
-
throw new Error(
|
|
1084
|
-
"Native secp256k1 module not found. Please install with: npm install secp256k1\nThis is required for optimal performance in Node.js environments."
|
|
1085
|
-
);
|
|
1086
|
-
}
|
|
1087
|
-
var NodeECIESUint8Provider = class extends BaseECIESUint8 {
|
|
1088
|
-
// Identity hash function for ECDH - returns raw X coordinate
|
|
1089
|
-
// CRITICAL: Must handle (x, y, output) signature correctly
|
|
1090
|
-
identityHashFn = (x, y, output) => {
|
|
1091
|
-
if (output && output.length >= 32) {
|
|
1092
|
-
output.set(x);
|
|
1093
|
-
return output;
|
|
1094
|
-
}
|
|
1095
|
-
return x;
|
|
1096
|
-
};
|
|
1097
|
-
generateRandomBytes(length) {
|
|
1098
|
-
return new Uint8Array((0, import_crypto.randomBytes)(length));
|
|
1099
|
-
}
|
|
1100
|
-
verifyPrivateKey(privateKey) {
|
|
1101
|
-
return secp256k13.privateKeyVerify(Buffer.from(privateKey)) === true;
|
|
1102
|
-
}
|
|
1103
|
-
createPublicKey(privateKey, compressed) {
|
|
1104
|
-
try {
|
|
1105
|
-
return new Uint8Array(
|
|
1106
|
-
secp256k13.publicKeyCreate(Buffer.from(privateKey), compressed)
|
|
1107
|
-
);
|
|
1108
|
-
} catch {
|
|
1109
|
-
return null;
|
|
1110
|
-
}
|
|
1111
|
-
}
|
|
1112
|
-
validatePublicKey(publicKey) {
|
|
1113
|
-
return secp256k13.publicKeyVerify(Buffer.from(publicKey)) === true;
|
|
1114
|
-
}
|
|
1115
|
-
decompressPublicKey(publicKey) {
|
|
1116
|
-
try {
|
|
1117
|
-
return new Uint8Array(
|
|
1118
|
-
secp256k13.publicKeyConvert(Buffer.from(publicKey), false)
|
|
1119
|
-
);
|
|
1120
|
-
} catch {
|
|
1121
|
-
return null;
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
performECDH(publicKey, privateKey) {
|
|
1125
|
-
try {
|
|
1126
|
-
const output = Buffer.alloc(32);
|
|
1127
|
-
secp256k13.ecdh(
|
|
1128
|
-
Buffer.from(publicKey),
|
|
1129
|
-
Buffer.from(privateKey),
|
|
1130
|
-
{ hashfn: this.identityHashFn },
|
|
1131
|
-
output
|
|
1132
|
-
);
|
|
1133
|
-
return new Uint8Array(output);
|
|
1134
|
-
} catch (error) {
|
|
1135
|
-
throw new Error(
|
|
1136
|
-
`ECDH failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1137
|
-
);
|
|
1138
|
-
}
|
|
1139
|
-
}
|
|
1140
|
-
sha512(data) {
|
|
1141
|
-
return new Uint8Array(
|
|
1142
|
-
(0, import_crypto.createHash)("sha512").update(Buffer.from(data)).digest()
|
|
1143
|
-
);
|
|
1144
|
-
}
|
|
1145
|
-
hmacSha256(key, data) {
|
|
1146
|
-
return new Uint8Array(
|
|
1147
|
-
(0, import_crypto.createHmac)("sha256", Buffer.from(key)).update(Buffer.from(data)).digest()
|
|
1148
|
-
);
|
|
1149
|
-
}
|
|
1150
|
-
async aesEncrypt(key, iv, plaintext) {
|
|
1151
|
-
const cipher = (0, import_crypto.createCipheriv)(
|
|
1152
|
-
"aes-256-cbc",
|
|
1153
|
-
Buffer.from(key),
|
|
1154
|
-
Buffer.from(iv)
|
|
1155
|
-
);
|
|
1156
|
-
const encrypted = Buffer.concat([
|
|
1157
|
-
cipher.update(Buffer.from(plaintext)),
|
|
1158
|
-
cipher.final()
|
|
1159
|
-
]);
|
|
1160
|
-
return new Uint8Array(encrypted);
|
|
1161
|
-
}
|
|
1162
|
-
async aesDecrypt(key, iv, ciphertext) {
|
|
1163
|
-
const decipher = (0, import_crypto.createDecipheriv)(
|
|
1164
|
-
"aes-256-cbc",
|
|
1165
|
-
Buffer.from(key),
|
|
1166
|
-
Buffer.from(iv)
|
|
1167
|
-
);
|
|
1168
|
-
const decrypted = Buffer.concat([
|
|
1169
|
-
decipher.update(Buffer.from(ciphertext)),
|
|
1170
|
-
decipher.final()
|
|
1171
|
-
]);
|
|
1172
|
-
return new Uint8Array(decrypted);
|
|
1173
|
-
}
|
|
1174
|
-
// No Buffer compatibility methods - Uint8Array only public API
|
|
1175
|
-
};
|
|
1176
|
-
|
|
1177
|
-
// src/platform/node.ts
|
|
1178
|
-
init_interface();
|
|
1179
416
|
var getOpenPGP2 = lazyImport(() => import("openpgp"));
|
|
417
|
+
var getEccrypto = lazyImport(() => import("eccrypto"));
|
|
1180
418
|
var NodeCryptoAdapter = class {
|
|
1181
|
-
eciesProvider = new NodeECIESUint8Provider();
|
|
1182
|
-
walletKeyEncryptionService = new WalletKeyEncryptionService({
|
|
1183
|
-
eciesProvider: this.eciesProvider
|
|
1184
|
-
});
|
|
1185
419
|
async encryptWithPublicKey(data, publicKeyHex) {
|
|
1186
420
|
try {
|
|
421
|
+
const eccrypto = await getEccrypto();
|
|
1187
422
|
const publicKey = Buffer.from(publicKeyHex, "hex");
|
|
1188
423
|
const message = Buffer.from(data, "utf8");
|
|
1189
|
-
const encrypted = await
|
|
424
|
+
const encrypted = await eccrypto.encrypt(publicKey, message);
|
|
1190
425
|
const result = Buffer.concat([
|
|
1191
426
|
encrypted.iv,
|
|
1192
427
|
encrypted.ephemPublicKey,
|
|
@@ -1195,54 +430,27 @@ var NodeCryptoAdapter = class {
|
|
|
1195
430
|
]);
|
|
1196
431
|
return result.toString("hex");
|
|
1197
432
|
} catch (error) {
|
|
1198
|
-
|
|
1199
|
-
throw error;
|
|
1200
|
-
}
|
|
1201
|
-
throw new ECIESError(
|
|
1202
|
-
`Encryption failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1203
|
-
"ENCRYPTION_FAILED",
|
|
1204
|
-
error instanceof Error ? error : void 0
|
|
1205
|
-
);
|
|
433
|
+
throw new Error(`Encryption failed: ${error}`);
|
|
1206
434
|
}
|
|
1207
435
|
}
|
|
1208
436
|
async decryptWithPrivateKey(encryptedData, privateKeyHex) {
|
|
1209
437
|
try {
|
|
438
|
+
const eccrypto = await getEccrypto();
|
|
1210
439
|
const privateKeyBuffer = processWalletPrivateKey(privateKeyHex);
|
|
1211
440
|
const encryptedBuffer = Buffer.from(encryptedData, "hex");
|
|
1212
441
|
const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
|
|
1213
|
-
const encryptedObj = {
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
ciphertext,
|
|
1217
|
-
mac
|
|
1218
|
-
};
|
|
1219
|
-
const decrypted = await this.eciesProvider.decrypt(
|
|
1220
|
-
privateKeyBuffer,
|
|
1221
|
-
encryptedObj
|
|
1222
|
-
);
|
|
1223
|
-
return new TextDecoder().decode(decrypted);
|
|
442
|
+
const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
|
|
443
|
+
const decrypted = await eccrypto.decrypt(privateKeyBuffer, encryptedObj);
|
|
444
|
+
return decrypted.toString("utf8");
|
|
1224
445
|
} catch (error) {
|
|
1225
|
-
|
|
1226
|
-
throw error;
|
|
1227
|
-
}
|
|
1228
|
-
throw new ECIESError(
|
|
1229
|
-
`Decryption failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1230
|
-
"DECRYPTION_FAILED",
|
|
1231
|
-
error instanceof Error ? error : void 0
|
|
1232
|
-
);
|
|
446
|
+
throw new Error(`Decryption failed: ${error}`);
|
|
1233
447
|
}
|
|
1234
448
|
}
|
|
1235
449
|
async generateKeyPair() {
|
|
1236
450
|
try {
|
|
1237
|
-
const
|
|
1238
|
-
const
|
|
1239
|
-
|
|
1240
|
-
do {
|
|
1241
|
-
privateKey = randomBytes2(32);
|
|
1242
|
-
} while (!secp256k14.privateKeyVerify(privateKey));
|
|
1243
|
-
const publicKey = Buffer.from(
|
|
1244
|
-
secp256k14.publicKeyCreate(privateKey, true)
|
|
1245
|
-
);
|
|
451
|
+
const eccrypto = await getEccrypto();
|
|
452
|
+
const privateKey = eccrypto.generatePrivate();
|
|
453
|
+
const publicKey = eccrypto.getPublicCompressed(privateKey);
|
|
1246
454
|
return {
|
|
1247
455
|
privateKey: privateKey.toString("hex"),
|
|
1248
456
|
publicKey: publicKey.toString("hex")
|
|
@@ -1253,20 +461,35 @@ var NodeCryptoAdapter = class {
|
|
|
1253
461
|
}
|
|
1254
462
|
async encryptWithWalletPublicKey(data, publicKey) {
|
|
1255
463
|
try {
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
464
|
+
const eccrypto = await getEccrypto();
|
|
465
|
+
const uncompressedKey = processWalletPublicKey(publicKey);
|
|
466
|
+
const encrypted = await eccrypto.encrypt(
|
|
467
|
+
uncompressedKey,
|
|
468
|
+
Buffer.from(data)
|
|
1259
469
|
);
|
|
470
|
+
const result = Buffer.concat([
|
|
471
|
+
encrypted.iv,
|
|
472
|
+
encrypted.ephemPublicKey,
|
|
473
|
+
encrypted.ciphertext,
|
|
474
|
+
encrypted.mac
|
|
475
|
+
]);
|
|
476
|
+
return result.toString("hex");
|
|
1260
477
|
} catch (error) {
|
|
1261
478
|
throw wrapCryptoError("encrypt with wallet public key", error);
|
|
1262
479
|
}
|
|
1263
480
|
}
|
|
1264
481
|
async decryptWithWalletPrivateKey(encryptedData, privateKey) {
|
|
1265
482
|
try {
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
483
|
+
const eccrypto = await getEccrypto();
|
|
484
|
+
const privateKeyBuffer = processWalletPrivateKey(privateKey);
|
|
485
|
+
const encryptedBuffer = Buffer.from(encryptedData, "hex");
|
|
486
|
+
const { iv, ephemPublicKey, ciphertext, mac } = parseEncryptedDataBuffer(encryptedBuffer);
|
|
487
|
+
const encryptedObj = { iv, ephemPublicKey, ciphertext, mac };
|
|
488
|
+
const decryptedBuffer = await eccrypto.decrypt(
|
|
489
|
+
privateKeyBuffer,
|
|
490
|
+
encryptedObj
|
|
1269
491
|
);
|
|
492
|
+
return decryptedBuffer.toString("utf8");
|
|
1270
493
|
} catch (error) {
|
|
1271
494
|
throw wrapCryptoError("decrypt with wallet private key", error);
|
|
1272
495
|
}
|
|
@@ -1432,7 +655,7 @@ async function createPlatformAdapter() {
|
|
|
1432
655
|
const { NodePlatformAdapter: NodePlatformAdapter2 } = await import(moduleName);
|
|
1433
656
|
return new NodePlatformAdapter2();
|
|
1434
657
|
} else {
|
|
1435
|
-
const { BrowserPlatformAdapter: BrowserPlatformAdapter2 } = await Promise.resolve().then(() => (
|
|
658
|
+
const { BrowserPlatformAdapter: BrowserPlatformAdapter2 } = await Promise.resolve().then(() => (init_browser(), browser_exports));
|
|
1436
659
|
return new BrowserPlatformAdapter2();
|
|
1437
660
|
}
|
|
1438
661
|
} catch (error) {
|
|
@@ -1453,7 +676,7 @@ async function createPlatformAdapterFor(platformType) {
|
|
|
1453
676
|
const { NodePlatformAdapter: NodePlatformAdapter2 } = await import(moduleName);
|
|
1454
677
|
return new NodePlatformAdapter2();
|
|
1455
678
|
} else {
|
|
1456
|
-
const { BrowserPlatformAdapter: BrowserPlatformAdapter2 } = await Promise.resolve().then(() => (
|
|
679
|
+
const { BrowserPlatformAdapter: BrowserPlatformAdapter2 } = await Promise.resolve().then(() => (init_browser(), browser_exports));
|
|
1457
680
|
return new BrowserPlatformAdapter2();
|
|
1458
681
|
}
|
|
1459
682
|
} catch (error) {
|
|
@@ -1480,7 +703,7 @@ function getPlatformCapabilities() {
|
|
|
1480
703
|
}
|
|
1481
704
|
|
|
1482
705
|
// src/platform/browser-safe.ts
|
|
1483
|
-
|
|
706
|
+
init_browser();
|
|
1484
707
|
async function createNodePlatformAdapter() {
|
|
1485
708
|
if (typeof window !== "undefined") {
|
|
1486
709
|
throw new Error(
|