forked from ssbc/ssb-keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
112 lines (91 loc) · 2.87 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict'
var fs = require('fs')
var mkdirp = require('mkdirp')
var path = require('path')
var u = require('./util')
function isObject (o) {
return 'object' === typeof o
}
function isFunction (f) {
return 'function' === typeof f
}
function empty(v) { return !!v }
function toFile (filename) {
if(isObject(filename))
return path.join(filename.path, 'secret')
return filename
}
module.exports = function (generate) {
if(!fs || !fs.readFile)
return require('./local-storage')(generate)
var exports = {}
//(DE)SERIALIZE KEYS
function constructKeys(keys, legacy) {
if(!keys) throw new Error('*must* pass in keys')
return [
'# this is your SECRET name.',
'# this name gives you magical powers.',
'# with it you can mark your messages so that your friends can verify',
'# that they really did come from you.',
'#',
'# if any one learns this name, they can use it to destroy your identity',
'# NEVER show this to anyone!!!',
'',
legacy ? keys.private : JSON.stringify(keys, null, 2),
'',
'# WARNING! It\'s vital that you DO NOT edit OR share your secret name',
'# instead, share your public name',
'# your public name: ' + keys.id
].join('\n')
}
function reconstructKeys(keyfile) {
var privateKey = keyfile
.replace(/\s*\#[^\n]*/g, '')
.split('\n').filter(empty).join('')
//if the key is in JSON format, we are good.
try {
var keys = JSON.parse(privateKey)
if(!u.hasSigil(keys.id)) keys.id = '@' + keys.public
return keys
} catch (_) { console.error(_.stack) }
}
exports.load = function(filename, cb) {
filename = toFile(filename, 'secret')
fs.readFile(filename, 'ascii', function(err, privateKeyStr) {
if (err) return cb(err)
var keys
try { keys = reconstructKeys(privateKeyStr) }
catch (err) { return cb(err) }
cb(null, keys)
})
}
exports.loadSync = function(filename) {
filename = toFile(filename)
return reconstructKeys(fs.readFileSync(filename, 'ascii'))
}
exports.create = function(filename, curve, legacy, cb) {
if(isFunction(legacy))
cb = legacy, legacy = null
if(isFunction(curve))
cb = curve, curve = null
filename = toFile(filename)
var keys = generate(curve)
var keyfile = constructKeys(keys, legacy)
mkdirp(path.dirname(filename), function (err) {
if(err) return cb(err)
fs.writeFile(filename, keyfile, {mode: 0x100, flag: 'wx'}, function(err) {
if (err) return cb(err)
cb(null, keys)
})
})
}
exports.createSync = function(filename, curve, legacy) {
filename = toFile(filename)
var keys = generate(curve)
var keyfile = constructKeys(keys, legacy)
mkdirp.sync(path.dirname(filename))
fs.writeFileSync(filename, keyfile, {mode: 0x100, flag: 'wx'})
return keys
}
return exports
}