-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathencryptAccount.js
79 lines (73 loc) · 1.81 KB
/
encryptAccount.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* istanbul ignore file */
import { cryptography } from '@liskhq/lisk-client';
import { extractKeyPair, extractAddressFromPublicKey } from 'src/modules/wallet/utils/account';
const ARGON2 = {
ITERATIONS: 3,
MEMORY: 65536,
};
// eslint-disable-next-line max-statements
export const encryptAccount = async ({
recoveryPhrase,
password,
name,
derivationPath,
enableAccessToLegacyAccounts = false,
}) => {
const options = {
passphrase: recoveryPhrase,
enableAccessToLegacyAccounts,
derivationPath,
};
try {
const { privateKey, publicKey, isValid } = await extractKeyPair(options);
if (!isValid) {
return { error: true };
}
const address = extractAddressFromPublicKey(publicKey);
const plainText = JSON.stringify({ privateKey, recoveryPhrase });
// Recommended options https://github.com/LiskHQ/lips/pull/465/files
const encryptOptions = {
kdf: cryptography.encrypt.KDF.ARGON2,
kdfparams: {
iterations: ARGON2.ITERATIONS,
memorySize: ARGON2.MEMORY,
},
};
const crypto = await cryptography.encrypt.encryptMessageWithPassword(
plainText,
password,
encryptOptions
);
return {
error: false,
result: {
crypto,
metadata: {
name,
pubkey: publicKey,
path: options.derivationPath,
address,
creationTime: new Date().toISOString(),
},
version: 1,
},
};
} catch {
return { error: true };
}
};
export const decryptAccount = async (crypto, password) => {
try {
const plainText = await cryptography.encrypt.decryptMessageWithPassword(
crypto,
password,
'utf-8'
);
return {
error: null,
result: JSON.parse(plainText),
};
} catch (error) {
return { error: true };
}
};