git ssb

1+

Dominic / ssb-keys



Commit d5a075ec0b62f472d55f353de0a53d05c5a99845

Merge pull request #10 from dominictarr/master

allow key load/create calls to take config object
Paul Frazee committed on 3/6/2015, 5:18:11 PM
Parent: a9f960514483cbb32536d7c65a6056fd9c1933bd
Parent: dbfcdc3e700706a37cf8e462ae1d1560d58ac44f

Files changed

index.jschanged
index.jsView
@@ -26,8 +26,12 @@
2626 function isHash (data) {
2727 return isString(data) && /^[A-Za-z0-9\/+]{43}=\.blake2s$/.test(data)
2828 }
2929
30+function isObject (o) {
31+ return 'object' === typeof o
32+}
33+
3034 exports.isHash = isHash
3135 exports.hash = hash
3236
3337 function isString(s) {
@@ -101,21 +105,30 @@
101105 function tag (key, tag) {
102106 return key.toString('base64')+'.' + tag.replace(/^\./, '')
103107 }
104108
109+var toNameFile = exports.toNameFile = function (namefile) {
110+ if(isObject(namefile))
111+ return path.join(namefile.path, 'secret')
112+ return namefile
113+}
114+
105115 exports.load = function(namefile, cb) {
116+ namefile = toNameFile(namefile)
106117 fs.readFile(namefile, 'ascii', function(err, privateKeyStr) {
107118 if (err) return cb(err)
108119 try { cb(null, reconstructKeys(privateKeyStr)) }
109120 catch (e) { cb(err) }
110121 })
111122 }
112123
113124 exports.loadSync = function(namefile) {
125+ namefile = toNameFile(namefile)
114126 return reconstructKeys(fs.readFileSync(namefile, 'ascii'))
115127 }
116128
117129 exports.create = function(namefile, cb) {
130+ namefile = toNameFile(namefile)
118131 var k = constructKeys()
119132 mkdirp(path.dirname(namefile), function (err) {
120133 if(err) return cb(err)
121134 fs.writeFile(namefile, k.keyfile, function(err) {
@@ -126,22 +139,25 @@
126139 })
127140 }
128141
129142 exports.createSync = function(namefile) {
143+ namefile = toNameFile(namefile)
130144 var k = constructKeys()
131145 mkdirp.sync(path.dirname(namefile))
132146 fs.writeFileSync(namefile, k.keyfile)
133147 delete k.keyfile
134148 return k
135149 }
136150
137151 exports.loadOrCreate = function (namefile, cb) {
152+ namefile = toNameFile(namefile)
138153 exports.load(namefile, function (err, keys) {
139154 if(!err) return cb(null, keys)
140155 exports.create(namefile, cb)
141156 })
142157 }
143158 exports.loadOrCreateSync = function (namefile) {
159+ namefile = toNameFile(namefile)
144160 try {
145161 return exports.loadSync(namefile)
146162 } catch (err) {
147163 return exports.createSync(namefile)
@@ -210,5 +226,5 @@
210226 delete obj.hmac
211227 var str = JSON.stringify(obj, null, 2)
212228 var _hmac = exports.hmac(str, secret)
213229 return deepEqual(hmac, _hmac)
214-}
230+}

Built with git-ssb-web