git ssb

1+

Dominic / ssb-keys



Tree: ed6d6091d9231d06e6e284991dfae1950559d734

Files: ed6d6091d9231d06e6e284991dfae1950559d734 / storage.js

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

Built with git-ssb-web