Skip to content

Commit 1e209ba

Browse files
author
sevenbitbyte
committed
update crypto version
1 parent 882bcfd commit 1e209ba

5 files changed

Lines changed: 162 additions & 27 deletions

File tree

examples/secure-config.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
const Dataparty = require('../src/index')
3+
4+
const prompt = require('prompt')
5+
6+
7+
async function main(){
8+
//const memoryConfig = new Dataparty.Config.MemoryConfig({foo: 'bar'})
9+
10+
11+
const jsonConfig = new Dataparty.Config.JsonFileConfig({
12+
foo: 'bar',
13+
basePath: '/tmp'
14+
})
15+
16+
17+
const secureConfig = new Dataparty.Config.SecureConfig({
18+
config: jsonConfig
19+
})
20+
21+
22+
secureConfig.on('locked', ()=>{ console.log('locked') })
23+
24+
secureConfig.on('unlocked', ()=>{ console.log('unlocked') })
25+
26+
secureConfig.on('timeout', ()=>{ console.log('timeout') })
27+
28+
secureConfig.on('ready', ()=>{ console.log('ready') })
29+
30+
secureConfig.on('blocked', async (reason)=>{
31+
32+
if(await secureConfig.isInitialized() && secureConfig.isLocked()){
33+
34+
35+
36+
console.log('blocked -',reason)
37+
38+
const {password} = await prompt.get({
39+
properties: {
40+
password: {
41+
message: 'Enter password',
42+
hidden: true
43+
}
44+
}})
45+
46+
secureConfig.unlock(password)
47+
}
48+
49+
50+
})
51+
52+
secureConfig.on('setup-required', async ()=>{
53+
54+
console.log('setup-required')
55+
56+
let password = ''
57+
58+
while(1){
59+
let passes = await prompt.get({
60+
properties: {
61+
password1: {
62+
message: 'Set password',
63+
hidden: true
64+
},
65+
password2: {
66+
message: 'Confim password',
67+
hidden: true
68+
}
69+
}
70+
})
71+
72+
if(passes.password1 == passes.password2){
73+
74+
password = passes.password1
75+
break
76+
}
77+
78+
console.log("passwords don't match")
79+
}
80+
81+
secureConfig.setPassword(password)
82+
83+
})
84+
85+
await secureConfig.start()
86+
87+
await secureConfig.waitForUnlocked('startup')
88+
89+
console.log('wait over')
90+
91+
//process.exit()
92+
}
93+
94+
main().catch((err)=>{
95+
console.error('crashed', err)
96+
}).then(()=>{
97+
console.log('done')
98+
})
99+
100+
101+
/*
102+
setTimeout(function () {
103+
process.exit();
104+
}, 5000);*/

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
},
6060
"dependencies": {
6161
"@dataparty/bouncer-db": "1.0.1",
62-
"@dataparty/crypto": "^1.1.0",
62+
"@dataparty/crypto": "^1.1.1",
6363
"@dataparty/tasker": "^0.0.2",
6464
"@diva.exchange/i2p-sam": "^4.1.8",
6565
"@markwylde/liferaft": "^1.3.4",
@@ -96,7 +96,6 @@
9696
"origin-router": "^1.6.4",
9797
"parse-url": "^5.0.1",
9898
"promisfy": "^1.2.0",
99-
"prompt": "^1.0.0",
10099
"roslib": "^1.3.0",
101100
"sanitize-filename": "^1.6.3",
102101
"simple-peer": "9.7.2",
@@ -129,6 +128,7 @@
129128
"parcel": "^2.3.1",
130129
"path-browserify": "^1.0.1",
131130
"process": "^0.11.10",
131+
"prompt": "^1.3.0",
132132
"punycode": "^1.4.1",
133133
"querystring-es3": "^0.2.1",
134134
"stream-browserify": "^3.0.0",

src/config/iconfig.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const EventEmitter = require('eventemitter3')
55
* @link module.Config
66
*/
77
class IConfig extends EventEmitter {
8-
constructor(){}
8+
constructor(){super()}
99
async start(){ throw 'not implemented' }
1010
async clear(){ throw 'not implemented' }
11-
readAll(){ throw 'not implemented' }
12-
read(key){ throw 'not implemented' }
11+
async readAll(){ throw 'not implemented' }
12+
async read(key){ throw 'not implemented' }
1313
async write(key, data){ throw 'not implemented' }
14-
exists(key){ throw 'not implemented' }
14+
async exists(key){ throw 'not implemented' }
1515
async save(){ throw 'not implemented' }
1616
}
1717

src/config/secure-config.js

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/index-browser.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const Topics = require('./topics')
44

55
const MemoryConfig = require('./config/memory')
66
const LocalStorageConfig = require('./config/local-storage')
7+
const SecureConfig = require('./config/secure-config')
78

89
const Config = {
910
MemoryConfig,
10-
LocalStorageConfig
11+
LocalStorageConfig,
12+
SecureConfig
1113
}
1214

1315

0 commit comments

Comments
 (0)