navio-sdk 0.1.2 → 0.1.3
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/index.d.mts +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +44 -24
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +44 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { argon2id } from 'hash-wasm';
|
|
2
2
|
import { sha256 } from '@noble/hashes/sha256';
|
|
3
3
|
import { ripemd160 } from '@noble/hashes/ripemd160';
|
|
4
4
|
import * as bip39 from '@scure/bip39';
|
|
@@ -43,19 +43,20 @@ function randomBytes(length) {
|
|
|
43
43
|
return bytes;
|
|
44
44
|
}
|
|
45
45
|
async function deriveKey(password, salt) {
|
|
46
|
-
const
|
|
47
|
-
|
|
46
|
+
const hashHex = await argon2id({
|
|
47
|
+
password,
|
|
48
48
|
salt,
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
time: ARGON2_PARAMS.iterations,
|
|
49
|
+
memorySize: ARGON2_PARAMS.memorySize,
|
|
50
|
+
iterations: ARGON2_PARAMS.iterations,
|
|
52
51
|
parallelism: ARGON2_PARAMS.parallelism,
|
|
53
|
-
|
|
52
|
+
hashLength: ARGON2_PARAMS.hashLength,
|
|
53
|
+
outputType: "hex"
|
|
54
54
|
});
|
|
55
|
+
const hashBytes = hexToBytes(hashHex);
|
|
55
56
|
const crypto = getCrypto();
|
|
56
57
|
const key = await crypto.subtle.importKey(
|
|
57
58
|
"raw",
|
|
58
|
-
toBufferSource(
|
|
59
|
+
toBufferSource(hashBytes),
|
|
59
60
|
{ name: "AES-GCM", length: 256 },
|
|
60
61
|
false,
|
|
61
62
|
// not extractable
|
|
@@ -64,16 +65,16 @@ async function deriveKey(password, salt) {
|
|
|
64
65
|
return key;
|
|
65
66
|
}
|
|
66
67
|
async function deriveKeyBytes(password, salt) {
|
|
67
|
-
const
|
|
68
|
-
|
|
68
|
+
const hashHex = await argon2id({
|
|
69
|
+
password,
|
|
69
70
|
salt,
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
time: ARGON2_PARAMS.iterations,
|
|
71
|
+
memorySize: ARGON2_PARAMS.memorySize,
|
|
72
|
+
iterations: ARGON2_PARAMS.iterations,
|
|
73
73
|
parallelism: ARGON2_PARAMS.parallelism,
|
|
74
|
-
|
|
74
|
+
hashLength: ARGON2_PARAMS.hashLength,
|
|
75
|
+
outputType: "hex"
|
|
75
76
|
});
|
|
76
|
-
return
|
|
77
|
+
return hexToBytes(hashHex);
|
|
77
78
|
}
|
|
78
79
|
async function encrypt(data, password) {
|
|
79
80
|
const crypto = getCrypto();
|
|
@@ -197,6 +198,13 @@ async function verifyPassword(password, salt, storedHash) {
|
|
|
197
198
|
}
|
|
198
199
|
return result === 0;
|
|
199
200
|
}
|
|
201
|
+
function hexToBytes(hex) {
|
|
202
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
203
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
204
|
+
bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
|
|
205
|
+
}
|
|
206
|
+
return bytes;
|
|
207
|
+
}
|
|
200
208
|
function bytesToBase64(bytes) {
|
|
201
209
|
if (typeof Buffer !== "undefined") {
|
|
202
210
|
return Buffer.from(bytes).toString("base64");
|
|
@@ -224,13 +232,12 @@ var ARGON2_PARAMS, ENCRYPTION_VERSION, IV_LENGTH, SALT_LENGTH;
|
|
|
224
232
|
var init_encryption = __esm({
|
|
225
233
|
"src/crypto/encryption.ts"() {
|
|
226
234
|
ARGON2_PARAMS = {
|
|
227
|
-
|
|
228
|
-
memory
|
|
229
|
-
// 64 MB
|
|
235
|
+
memorySize: 65536,
|
|
236
|
+
// 64 MB (hash-wasm uses memorySize instead of memory)
|
|
230
237
|
iterations: 3,
|
|
231
238
|
parallelism: 4,
|
|
232
|
-
|
|
233
|
-
// 256 bits for AES-256
|
|
239
|
+
hashLength: 32
|
|
240
|
+
// 256 bits for AES-256 (hash-wasm uses hashLength instead of hashLen)
|
|
234
241
|
};
|
|
235
242
|
ENCRYPTION_VERSION = 1;
|
|
236
243
|
IV_LENGTH = 12;
|
|
@@ -400,7 +407,7 @@ var KeyManager = class _KeyManager {
|
|
|
400
407
|
return true;
|
|
401
408
|
}
|
|
402
409
|
/**
|
|
403
|
-
* Lock the wallet, clearing the cached encryption key
|
|
410
|
+
* Lock the wallet, clearing the cached encryption key and unencrypted keys
|
|
404
411
|
* After locking, private keys cannot be accessed without unlocking again
|
|
405
412
|
*/
|
|
406
413
|
lock() {
|
|
@@ -408,6 +415,8 @@ var KeyManager = class _KeyManager {
|
|
|
408
415
|
return;
|
|
409
416
|
}
|
|
410
417
|
this.encryptionKey = null;
|
|
418
|
+
this.keys.clear();
|
|
419
|
+
this.outKeys.clear();
|
|
411
420
|
}
|
|
412
421
|
/**
|
|
413
422
|
* Change the wallet password
|
|
@@ -456,6 +465,18 @@ var KeyManager = class _KeyManager {
|
|
|
456
465
|
this.passwordVerificationHash = this.hexToBytes(verificationHash);
|
|
457
466
|
this.encrypted = true;
|
|
458
467
|
}
|
|
468
|
+
/**
|
|
469
|
+
* Get key storage statistics (for testing/debugging)
|
|
470
|
+
* @returns Object with counts of plain and encrypted keys
|
|
471
|
+
*/
|
|
472
|
+
getKeyStats() {
|
|
473
|
+
return {
|
|
474
|
+
plainKeys: this.keys.size,
|
|
475
|
+
plainOutKeys: this.outKeys.size,
|
|
476
|
+
encryptedKeys: this.cryptedKeys.size,
|
|
477
|
+
encryptedOutKeys: this.cryptedOutKeys.size
|
|
478
|
+
};
|
|
479
|
+
}
|
|
459
480
|
/**
|
|
460
481
|
* Encrypt all private keys in the wallet
|
|
461
482
|
* Internal method called when setting password
|
|
@@ -482,6 +503,8 @@ var KeyManager = class _KeyManager {
|
|
|
482
503
|
encryptedSecret: this.serializeEncryptedToBytes(encrypted)
|
|
483
504
|
});
|
|
484
505
|
}
|
|
506
|
+
this.keys.clear();
|
|
507
|
+
this.outKeys.clear();
|
|
485
508
|
}
|
|
486
509
|
/**
|
|
487
510
|
* Decrypt essential keys needed for wallet operations
|