1
1
import { decryptFromKeystore , encryptToKeyStore , generatePhrase , validatePhrase } from '../src/crypto'
2
2
import { encodeAddress } from '../src/utils'
3
+ import crypto from 'crypto'
3
4
4
5
describe ( 'Generate Phrase' , ( ) => {
5
6
it ( 'Generates 12-word phrase' , ( ) => {
@@ -14,6 +15,50 @@ describe('Generate Phrase', () => {
14
15
} )
15
16
} )
16
17
18
+ describe ( 'Keystore regression test for encrypt/decrypt with internal migration' , ( ) => {
19
+ const phrase = 'patient use either flash couple jump castle true broccoli cancel brand mechanic'
20
+ const password = '1234'
21
+
22
+ const expectedKeystore = {
23
+ crypto : {
24
+ cipher : 'aes-128-ctr' ,
25
+ ciphertext :
26
+ 'aa6838a2aded226922107d5a2322513e5dd706149831580356dec17d8ab531f873d3f173c2775f125d2b3203c498214039cfa78ac1d0ecb29c3f9be35483c1e4d0e2267bba7b36cec14172f760a91a' ,
27
+ cipherparams : { iv : 'dffdb8bbe92e9a00e173eaa20f1a3784' } ,
28
+ kdf : 'pbkdf2' ,
29
+ kdfparams : {
30
+ prf : 'hmac-sha256' ,
31
+ dklen : 32 ,
32
+ salt : 'ead4ad6c09f5a5586235a642fa39c95741b35283304e3fd464d942e300fe0514' ,
33
+ c : 262144 ,
34
+ } ,
35
+ mac : '10597fd811d910c2a9ae3aa4538d03e7ddda672070c1e324603ecf6bcb0426dd' ,
36
+ } ,
37
+ id : '9ad9ea91-22ad-46a7-9613-4f9d190e32ab' ,
38
+ version : 1 ,
39
+ meta : 'xchain-keystore' ,
40
+ }
41
+
42
+ it ( 'encryptToKeyStore() should produce expected ciphertext and mac' , async ( ) => {
43
+ jest
44
+ . spyOn ( crypto , 'randomBytes' )
45
+ . mockImplementationOnce ( ( ) => Buffer . from ( expectedKeystore . crypto . kdfparams . salt , 'hex' ) ) // salt
46
+ . mockImplementationOnce ( ( ) => Buffer . from ( expectedKeystore . crypto . cipherparams . iv , 'hex' ) ) // iv
47
+
48
+ const keystore = await encryptToKeyStore ( phrase , password )
49
+
50
+ expect ( keystore . crypto . ciphertext ) . toBe ( expectedKeystore . crypto . ciphertext )
51
+ expect ( keystore . crypto . mac ) . toBe ( expectedKeystore . crypto . mac )
52
+ expect ( keystore . crypto . kdfparams ) . toEqual ( expectedKeystore . crypto . kdfparams )
53
+ expect ( keystore . crypto . cipherparams ) . toEqual ( expectedKeystore . crypto . cipherparams )
54
+ } )
55
+
56
+ it ( 'decryptFromKeystore() should return original phrase' , async ( ) => {
57
+ const result = await decryptFromKeystore ( expectedKeystore , password )
58
+ expect ( result ) . toBe ( phrase )
59
+ } )
60
+ } )
61
+
17
62
describe ( 'Validate Phrase' , ( ) => {
18
63
it ( 'Validates 12-word Phrase' , ( ) => {
19
64
const phrase = generatePhrase ( )
0 commit comments