@@ -14,15 +14,16 @@ class SecureConfig extends IConfig {
1414 id = 'secure-config' ,
1515 config, timeoutMs= 60 * 10 * 1000 , includeActivity= true
1616 } ) {
17- this . id = id
17+ super ( )
18+ this . id = id || 'secure-config'
1819 this . config = config
1920
2021 this . content = null
2122 this . identity = null
2223 this . timer = null
2324 this . lastActivity = null
24- this . timeoutMs = timeoutMs
25- this . includeActivity = includeActivity
25+ this . timeoutMs = timeoutMs || 60 * 10 * 1000
26+ this . includeActivity = includeActivity || true
2627 }
2728
2829 async start ( ) {
@@ -37,10 +38,11 @@ class SecureConfig extends IConfig {
3738 }
3839
3940 async isInitialized ( ) {
40- let salt = this . config . read ( 'salt' )
41- let rounds = this . config . read ( 'rounds' )
41+ let salt = await this . config . read ( 'salt' )
42+ let rounds = await this . config . read ( 'rounds' )
4243
43- return ( salt . length == 32 && rounds > 100000 )
44+
45+ return ( salt != undefined && salt . length > 16 && rounds > 100000 )
4446 }
4547
4648 isLocked ( ) {
@@ -65,34 +67,48 @@ class SecureConfig extends IConfig {
6567
6668 await initialContent . encrypt ( pwIdentity , pwIdentity . toMini ( ) )
6769
68- await this . config . write ( 'salt' , salt )
70+ await this . config . write ( 'salt' , salt . toString ( 'hex' ) )
6971 await this . config . write ( 'rounds' , rounds )
7072 //await this.config.write('identity', pwIdentity.toJSON())
71- await this . config . write ( 'content' , {
72- enc : initialContent . enc ,
73- sig : initialContent . sig
74- } )
73+ await this . config . write ( 'content' , initialContent . toJSON ( ) )
7574
76- debug ( '\t' , 'identity' , pwIdentity . toJSON )
75+ debug ( '\t' , 'identity' , pwIdentity )
76+
77+ debug ( '\t' , 'content' , initialContent . toJSON ( ) )
7778
7879 await this . config . save ( )
7980
8081 this . emit ( 'ready' )
82+
83+ const contentMsg = new dataparty_crypto . Message ( initialContent )
84+
85+ console . log ( 'msg' , contentMsg )
86+
87+ //! Verify message
88+ await contentMsg . decrypt ( pwIdentity )
8189 }
8290
8391 async waitForUnlocked ( reason ) {
8492
93+
8594 if ( ! this . isLocked ( ) ) {
8695 return
8796 }
97+
98+ debug ( 'waitForUnlocked' , reason )
8899
89100 this . emit ( 'blocked' , reason )
90101
91- return new Promise ( ( resolve , reject ) => {
102+ let waiting = new Promise ( ( resolve , reject ) => {
92103
93- this . once ( 'unlocked' , resolve )
104+ this . once ( 'unlocked' , ( ) => {
105+ resolve ( )
106+ debug ( 'waitForUnlocked - done' )
107+ } )
94108
95109 } )
110+
111+ await waiting
96112 }
97113
98114 async unlock ( password ) {
@@ -102,21 +118,32 @@ class SecureConfig extends IConfig {
102118 this . timer = null
103119 }
104120
105- let salt = this . config . read ( 'salt' )
106- let rounds = this . config . read ( 'rounds' )
121+ let salt = Buffer . from ( await this . config . read ( 'salt' ) , 'hex' )
122+ let rounds = await this . config . read ( 'rounds' )
123+
124+ console . log ( 'salt' , salt )
125+ console . log ( 'rounds' , rounds )
107126
108127 let key = await dataparty_crypto . Routines . createKeyFromPassword ( password , salt , rounds )
109128
110129 const pwIdentity = new dataparty_crypto . Identity ( {
111130 key,
112- id : this . id ,
131+ id : this . id
113132 } )
114133
115- const contentMsg = new dataparty_crypto . Message ( this . config . read ( 'content' ) )
134+ console . log ( pwIdentity )
135+
136+ this . content = await this . config . read ( 'content' )
116137
138+ console . log ( 'content' , this . content , typeof this . content )
139+
140+ const contentMsg = new dataparty_crypto . Message ( this . content )
141+
142+ console . log ( 'msg' , contentMsg )
143+
144+ //! Verify message
117145 await contentMsg . decrypt ( pwIdentity )
118146
119- this . content = this . config . read ( 'content' )
120147 this . identity = pwIdentity
121148
122149 this . timer = setTimeout ( this . onTimeout . bind ( this ) , this . timeoutMs )
@@ -236,4 +263,6 @@ class SecureConfig extends IConfig {
236263 sig : this . content . sig
237264 } )
238265 }
239- }
266+ }
267+
268+ module . exports = SecureConfig
0 commit comments