1 |
|
2 |
|
3 |
|
4 |
|
5 | const { crypto } = require('../../src');
|
6 |
|
7 | describe('encryption/decryption of private keys with wallet passwords', () => {
|
8 | let privateKey;
|
9 | let salt;
|
10 | let walletPassword;
|
11 | let encrypted;
|
12 |
|
13 | beforeAll(() => {
|
14 | privateKey = ORE_TESTA_ACCOUNT_KEY;
|
15 | salt = USER_ACCOUNT_ENCRYPTION_SALT;
|
16 | walletPassword = WALLET_PASSWORD;
|
17 | encrypted = crypto.encrypt(privateKey, walletPassword, salt);
|
18 | });
|
19 |
|
20 | describe('deriveKey', () => {
|
21 | it('returns a deterministic salt', () => {
|
22 | expect(crypto.deriveKey(walletPassword, salt)).toEqual(crypto.deriveKey(walletPassword, salt));
|
23 | expect(crypto.deriveKey(walletPassword, salt)).not.toEqual(crypto.deriveKey(walletPassword, ''));
|
24 | });
|
25 | });
|
26 |
|
27 | describe('decryptWithKey', () => {
|
28 | it('returns the original private key', () => {
|
29 | const key = crypto.deriveKey(walletPassword, salt);
|
30 | const decrypted = crypto.decryptWithKey(encrypted, key);
|
31 | expect(decrypted.toString()).toMatch(privateKey);
|
32 | });
|
33 |
|
34 | it('does not return privateKey with a bad key', () => {
|
35 | const key = 'badkey';
|
36 | const decrypted = crypto.decryptWithKey(encrypted, key);
|
37 | expect(decrypted.toString()).not.toMatch(privateKey);
|
38 | });
|
39 | });
|
40 |
|
41 | describe('encrypt', () => {
|
42 | it('returns an encrypted string', () => {
|
43 | expect(encrypted.toString()).toEqual(expect.not.stringContaining(privateKey));
|
44 | });
|
45 | });
|
46 |
|
47 | describe('decrypt', () => {
|
48 | it('returns the original privateKey', () => {
|
49 | const decrypted = crypto.decrypt(encrypted, walletPassword, salt);
|
50 | expect(decrypted.toString()).toMatch(privateKey);
|
51 | });
|
52 |
|
53 | it('does not return privateKey with a bad password', () => {
|
54 | const badPassword = 'BadPassword';
|
55 | const decrypted = crypto.decrypt(encrypted, badPassword, salt);
|
56 | expect(decrypted.toString()).not.toMatch(privateKey);
|
57 | });
|
58 |
|
59 | it('does not return privateKey with a bad salt', () => {
|
60 | const badPassword = 'BadPassword';
|
61 | const decrypted = crypto.decrypt(encrypted, walletPassword, '');
|
62 | expect(decrypted.toString()).not.toMatch(privateKey);
|
63 | });
|
64 | });
|
65 | });
|