Skip to content

Commit e21f55f

Browse files
author
sevenbitbyte
committed
tweak secure config events
1 parent a3b2e56 commit e21f55f

2 files changed

Lines changed: 26 additions & 26 deletions

File tree

examples/secure-config.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Dataparty = require('../src/index')
33

44
const prompt = require('prompt')
55

6+
let secureConfig = null
67

78
async function main(){
89
const memoryConfig = new Dataparty.Config.MemoryConfig({foo: 'bar'})
@@ -13,8 +14,8 @@ async function main(){
1314
})
1415

1516

16-
const secureConfig = new Dataparty.Config.SecureConfig({
17-
config: memoryConfig
17+
secureConfig = new Dataparty.Config.SecureConfig({
18+
config: jsonConfig
1819
})
1920

2021

@@ -41,6 +42,7 @@ async function main(){
4142
if(await secureConfig.isInitialized() && secureConfig.isLocked()){
4243

4344
if(blocked){
45+
console.log('blocked true')
4446
await secureConfig.waitForUnlocked()
4547
return
4648
}
@@ -94,38 +96,40 @@ async function main(){
9496
console.log("passwords don't match")
9597
}
9698

97-
secureConfig.setPassword(password, {
99+
await secureConfig.setPassword(password, {
98100
foo: 'bar'
99101
})
100102

103+
console.log('password set')
104+
105+
106+
await secureConfig.unlock(password)
107+
101108
})
102109

110+
console.log('starting')
111+
103112
await secureConfig.start()
104113

114+
console.log('wait for startup')
115+
105116
await secureConfig.waitForUnlocked('startup')
106117

107118
console.log('wait over')
108119

109120
console.log('main config', await secureConfig.readAll())
110121

111-
setTimeout(async ()=>{
122+
let timer = setTimeout(async ()=>{
112123

113124

114125
console.log('timer config', await secureConfig.readAll())
115126

116127
}, 30000)
117128

118-
//process.exit()
119129
}
120130

121131
main().catch((err)=>{
122132
console.error('crashed', err)
123133
}).then(()=>{
124134
console.log('done')
125135
})
126-
127-
128-
/*
129-
setTimeout(function () {
130-
process.exit();
131-
}, 5000);*/

src/config/secure-config.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class SecureConfig extends IConfig {
2424
this.lastActivity = null
2525
this.timeoutMs = timeoutMs || 15*1000
2626
this.includeActivity = includeActivity || true
27+
28+
this.blocked = false
2729
}
2830

2931
async start(){
@@ -78,30 +80,32 @@ class SecureConfig extends IConfig {
7880

7981
await this.config.save()
8082

81-
this.emit('ready')
82-
83+
8384
const contentMsg = new dataparty_crypto.Message( initialContent )
84-
85-
console.log('msg', contentMsg)
86-
8785
//! Verify message
8886
await contentMsg.decrypt(pwIdentity)
87+
this.emit('ready')
8988
}
9089

9190
async waitForUnlocked(reason){
92-
9391

9492
if(!this.isLocked()){
9593
return
9694
}
9795

9896
debug('waitForUnlocked', reason)
9997

100-
this.emit('blocked', reason)
98+
if(!this.blocked){
99+
this.emit('blocked', reason)
100+
}
101+
102+
this.blocked = true
103+
101104

102105
let waiting = new Promise((resolve,reject)=>{
103106

104107
this.once('unlocked', ()=>{
108+
this.blocked = false
105109
resolve()
106110
debug('waitForUnlocked - done')
107111
})
@@ -121,26 +125,18 @@ class SecureConfig extends IConfig {
121125
let salt = Buffer.from(await this.config.read('salt'),'hex')
122126
let rounds = await this.config.read('rounds')
123127

124-
console.log('salt', salt)
125-
console.log('rounds', rounds)
126-
127128
let key = await dataparty_crypto.Routines.createKeyFromPassword(password, salt, rounds)
128129

129130
const pwIdentity = new dataparty_crypto.Identity({
130131
key,
131132
id: this.id
132133
})
133134

134-
console.log(pwIdentity)
135135

136136
this.content = await this.config.read('content')
137137

138-
console.log('content', this.content, typeof this.content)
139-
140138
const contentMsg = new dataparty_crypto.Message( this.content )
141139

142-
console.log('msg', contentMsg)
143-
144140
//! Verify message
145141
await contentMsg.decrypt(pwIdentity)
146142

0 commit comments

Comments
 (0)