@otoplo/wallet-common 0.1.8 → 0.1.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@otoplo/wallet-common",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Shared common library for internal use in Otoplo wallet",
5
5
  "license": "MIT",
6
6
  "author": "vgrunner",
@@ -31,8 +31,6 @@
31
31
  "url": "git+https://gitlab.com/nexa/otoplo/otoplo-wallet-common.git"
32
32
  },
33
33
  "dependencies": {
34
- "@noble/ciphers": "^2.1.1",
35
- "@noble/hashes": "^2.0.1",
36
34
  "@scure/bip39": "^2.0.1",
37
35
  "jszip": "^3.10.1"
38
36
  },
package/src/utils/seed.ts CHANGED
@@ -1,8 +1,3 @@
1
- import { gcm } from "@noble/ciphers/aes.js";
2
- import { argon2idAsync } from "@noble/hashes/argon2.js";
3
- import { pbkdf2Async } from "@noble/hashes/pbkdf2.js";
4
- import { scryptAsync } from "@noble/hashes/scrypt.js";
5
- import { sha256 } from "@noble/hashes/sha2.js";
6
1
  import { generateMnemonic, validateMnemonic } from "@scure/bip39";
7
2
  import { wordlist } from "@scure/bip39/wordlists/english.js";
8
3
  import { BufferUtils } from "libnexa-ts";
@@ -15,49 +10,76 @@ export function isMnemonicValid(mnemonic: string): boolean {
15
10
  return validateMnemonic(mnemonic, wordlist);
16
11
  }
17
12
 
18
- async function deriveKey(password: string, salt: Uint8Array): Promise<Uint8Array> {
19
- let start = performance.now();
20
- const key = await argon2idAsync(password, salt, { t: 2, m: 19456, p: 1, dkLen: 32 });
21
- console.log(performance.now() - start);
13
+ async function deriveKey(secret: Uint8Array, salt: Uint8Array): Promise<CryptoKey> {
14
+ const perf = performance.now();
15
+ const baseKey = await crypto.subtle.importKey(
16
+ 'raw',
17
+ new Uint8Array(secret),
18
+ 'PBKDF2',
19
+ false,
20
+ ['deriveKey']
21
+ );
22
22
 
23
- let start2 = performance.now();
24
- const key2 = await pbkdf2Async(sha256, password, salt, { c: 210_000, dkLen: 32 });
25
- console.log(performance.now() - start2);
26
-
27
- let start3 = performance.now();
28
- const key3 = await pbkdf2Async(sha256, password, salt, { c: 100_000, dkLen: 32 });
29
- console.log(performance.now() - start3);
30
-
31
- let start4 = performance.now();
32
- const key4 = await argon2idAsync(password, salt, { t: 4, m: 9216, p: 1, dkLen: 32 });
33
- console.log(performance.now() - start4);
23
+ const key = await crypto.subtle.deriveKey(
24
+ {
25
+ name: 'PBKDF2',
26
+ salt: new Uint8Array(salt),
27
+ iterations: 210_000,
28
+ hash: 'SHA-256',
29
+ },
30
+ baseKey,
31
+ {
32
+ name: 'AES-GCM',
33
+ length: 256,
34
+ },
35
+ false,
36
+ ['encrypt', 'decrypt']
37
+ );
34
38
 
39
+ console.log(performance.now() - perf);
35
40
  return key;
36
41
  }
37
42
 
38
43
  export async function encryptMnemonic(phrase: string, password: string): Promise<string> {
44
+ const secret = BufferUtils.utf8ToBuffer(password);
39
45
  const salt = BufferUtils.getRandomBuffer(16);
40
46
  const iv = BufferUtils.getRandomBuffer(12);
41
47
  const data = BufferUtils.utf8ToBuffer(phrase);
42
48
 
43
- const key = await deriveKey(password, salt);
49
+ const key = await deriveKey(secret, salt);
50
+
51
+ const cipher = await crypto.subtle.encrypt(
52
+ {
53
+ name: 'AES-GCM',
54
+ iv: new Uint8Array(iv),
55
+ },
56
+ key,
57
+ new Uint8Array(data)
58
+ );
44
59
 
45
- const cipher = gcm(key, iv).encrypt(data);
46
- const encBuf = BufferUtils.concat([salt, iv, cipher]);
60
+ const encBuf = BufferUtils.concat([salt, iv, new Uint8Array(cipher)]);
47
61
  return BufferUtils.bufferToBase64(encBuf);
48
62
  }
49
63
 
50
64
  async function decryptMnemonic(encSeed: string, password: string): Promise<string> {
65
+ const secret = BufferUtils.utf8ToBuffer(password);
51
66
  const encBuf = BufferUtils.base64ToBuffer(encSeed);
52
67
 
53
- const salt = encBuf.subarray(0, 16);
54
- const iv = encBuf.subarray(16, 28);
55
- const cipher = encBuf.subarray(28);
68
+ const salt = encBuf.slice(0, 16);
69
+ const iv = encBuf.slice(16, 28);
70
+ const cipher = encBuf.slice(28);
56
71
 
57
- const key = await deriveKey(password, salt);
72
+ const key = await deriveKey(secret, salt);
58
73
 
59
- const data = gcm(key, iv).decrypt(cipher);
60
- return BufferUtils.bufferToUtf8(data);
74
+ const data = await crypto.subtle.decrypt(
75
+ {
76
+ name: 'AES-GCM',
77
+ iv,
78
+ },
79
+ key,
80
+ cipher
81
+ );
82
+ return BufferUtils.bufferToUtf8(new Uint8Array(data));
61
83
  }
62
84
 
63
85
  export async function validateAndDecryptMnemonic(encSeed: string, password: string): Promise<string | false> {