git ssb

1+

Dominic / ssb-keys



Commit b3f31bca140418c4a8945f19d1e6108c2e18b61f

Merge pull request #1 from dominictarr/master

add loadOrCreate
Paul Frazee committed on 11/2/2014, 3:17:50 PM
Parent: 3c86f2b6d1444f5a148454cbe412609ffc372808
Parent: 0644061d62c451cf63f1489c1b7e8c5bab16b76b

Files changed

index.jschanged
index.jsView
@@ -2,8 +2,10 @@
22 var crypto = require('crypto')
33 var ecc = require('eccjs')
44 var k256 = ecc.curves.k256
55 var Blake2s = require('blake2s')
6+var mkdirp = require('mkdirp')
7+var path = require('path')
68
79 function bsum (value) {
810 return new Blake2s().update(value).digest()
911 }
@@ -52,17 +54,35 @@
5254 }
5355
5456 exports.create = function(namefile, cb) {
5557 var k = constructKeys()
56- fs.writeFile(namefile, k.keyfile, function(err) {
57- if (err) return cb(err)
58- delete k.keyfile
59- cb(null, k)
58+ mkdirp(path.dirname(namefile), function (err) {
59+ if(err) return cb(err)
60+ fs.writeFile(namefile, k.keyfile, function(err) {
61+ if (err) return cb(err)
62+ delete k.keyfile
63+ cb(null, k)
64+ })
6065 })
6166 }
6267
6368 exports.createSync = function(namefile) {
6469 var k = constructKeys()
70+ mkdirp.sync(path.dirname(namefile))
6571 fs.writeFileSync(namefile, k.keyfile)
6672 delete k.keyfile
6773 return k
68-}
74+}
75+
76+exports.loadOrCreate = function (namefile, cb) {
77+ exports.load(namefile, function (err, keys) {
78+ if(!err) return cb(null, keys)
79+ exports.create(namefile, cb)
80+ })
81+}
82+exports.loadOrCreateSync = function (namefile) {
83+ try {
84+ return exports.loadSync(namefile)
85+ } catch (err) {
86+ return exports.createSync(namefile)
87+ }
88+}

Built with git-ssb-web