Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
fix: more linting
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Dec 6, 2017
1 parent 409a999 commit 7c44c91
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 46 deletions.
49 changes: 29 additions & 20 deletions src/keychain.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'

const async = require('async')
const sanitize = require("sanitize-filename")
const sanitize = require('sanitize-filename')
const forge = require('node-forge')
const deepmerge = require('deepmerge')
const crypto = require('crypto')
const libp2pCrypto = require('libp2p-crypto')
const crypto = require('libp2p-crypto')
const util = require('./util')
const CMS = require('./cms')
const DS = require('interface-datastore')
Expand All @@ -32,7 +31,6 @@ const defaultOptions = {

function validateKeyName (name) {
if (!name) return false

return name === sanitize(name.trim())
}

Expand All @@ -44,8 +42,9 @@ function validateKeyName (name) {
*
* @param {function(Error)} callback - The caller
* @param {string | Error} err - The error
* @returns {undefined}
*/
function _error(callback, err) {
function _error (callback, err) {
const min = 200
const max = 1000
const delay = Math.random() * (max - min) + min
Expand All @@ -55,15 +54,21 @@ function _error(callback, err) {

/**
* Converts a key name into a datastore name.
*
* @param {string} name
* @returns {DS.Key}
*/
function DsName (name) {
return new DS.Key('/' + name)
}

/**
* Converts a datastore name into a key name.
*
* @param {DS.Key} name - A datastore name
* @returns {string}
*/
function KsName(name) {
function KsName (name) {
return name.toString().slice(1)
}

Expand Down Expand Up @@ -111,7 +116,7 @@ class Keychain {
this.cms = new CMS(this)
}

static get options() {
static get options () {
return defaultOptions
}

Expand All @@ -123,6 +128,7 @@ class Keychain {
}
const dsname = DsName(name)
self.store.has(dsname, (err, exists) => {
if (err) return _error(callback, err)
if (exists) return _error(callback, `Key '${name}' already exists'`)

switch (type.toLowerCase()) {
Expand All @@ -133,14 +139,14 @@ class Keychain {
forge.pki.rsa.generateKeyPair({bits: size, workers: -1}, (err, keypair) => {
if (err) return _error(callback, err)

const pem = forge.pki.encryptRsaPrivateKey(keypair.privateKey, this._());
const pem = forge.pki.encryptRsaPrivateKey(keypair.privateKey, this._())
return self.store.put(dsname, pem, (err) => {
if (err) return _error(callback, err)

self._getKeyInfo(name, callback)
})
})
break;
break

default:
return _error(callback, `Invalid key type '${type}'`)
Expand Down Expand Up @@ -181,13 +187,14 @@ class Keychain {
}
const dsname = DsName(name)
self.store.has(dsname, (err, exists) => {
if (err) return _error(callback, err)
if (!exists) return _error(callback, `Key '${name}' does not exist'`)

self.store.delete(dsname, callback)
})
}

renameKey(oldName, newName, callback) {
renameKey (oldName, newName, callback) {
const self = this
if (!validateKeyName(oldName) || oldName === 'self') {
return _error(callback, `Invalid old key name '${oldName}'`)
Expand All @@ -203,6 +210,7 @@ class Keychain {
}
const pem = res.toString()
self.store.has(newDsname, (err, exists) => {
if (err) return _error(callback, err)
if (exists) return _error(callback, `Key '${newName}' already exists'`)

const batch = self.store.batch()
Expand Down Expand Up @@ -246,7 +254,7 @@ class Keychain {
})
}

importKey(name, pem, password, callback) {
importKey (name, pem, password, callback) {
const self = this
if (!validateKeyName(name) || name === 'self') {
return _error(callback, `Invalid key name '${name}'`)
Expand All @@ -256,15 +264,16 @@ class Keychain {
}
const dsname = DsName(name)
self.store.has(dsname, (err, exists) => {
if (err) return _error(callback, err)
if (exists) return _error(callback, `Key '${name}' already exists'`)
try {
const privateKey = forge.pki.decryptRsaPrivateKey(pem, password)
if (privateKey === null) {
return _error(callback, 'Cannot read the key, most likely the password is wrong')
}
const newpem = forge.pki.encryptRsaPrivateKey(privateKey, this._());
const newpem = forge.pki.encryptRsaPrivateKey(privateKey, this._())
return self.store.put(dsname, newpem, (err) => {
if (err) return _error(callback, err)
if (err) return _error(callback, err)

this._getKeyInfo(name, callback)
})
Expand All @@ -280,23 +289,25 @@ class Keychain {
return _error(callback, `Invalid key name '${name}'`)
}
if (!peer || !peer.privKey) {
return _error(callback, 'Peer.privKey \is required')
return _error(callback, 'Peer.privKey is required')
}
const dsname = DsName(name)
self.store.has(dsname, (err, exists) => {
if (err) return _error(callback, err)
if (exists) return _error(callback, `Key '${name}' already exists'`)

const privateKeyProtobuf = peer.marshalPrivKey()
libp2pCrypto.keys.unmarshalPrivateKey(privateKeyProtobuf, (err, key) => {
crypto.keys.unmarshalPrivateKey(privateKeyProtobuf, (err, key) => {
if (err) return _error(callback, err)
try {
const der = key.marshal()
const buf = forge.util.createBuffer(der.toString('binary'));
const buf = forge.util.createBuffer(der.toString('binary'))
const obj = forge.asn1.fromDer(buf)
const privateKey = forge.pki.privateKeyFromAsn1(obj)
if (privateKey === null) {
return _error(callback, 'Cannot read the peer private key')
}
const pem = forge.pki.encryptRsaPrivateKey(privateKey, this._());
const pem = forge.pki.encryptRsaPrivateKey(privateKey, this._())
return self.store.put(dsname, pem, (err) => {
if (err) return _error(callback, err)

Expand All @@ -314,9 +325,9 @@ class Keychain {
*
* @param {string} name
* @param {function(Error, string)} callback
* @returns {undefined}
*/
_getPrivateKey (name, callback) {
const self = this
if (!validateKeyName(name)) {
return _error(callback, `Invalid key name '${name}'`)
}
Expand All @@ -329,7 +340,6 @@ class Keychain {
}

_getKeyInfo (name, callback) {
const self = this
if (!validateKeyName(name)) {
return _error(callback, `Invalid key name '${name}'`)
}
Expand All @@ -356,7 +366,6 @@ class Keychain {
}
})
}

}

module.exports = Keychain
22 changes: 11 additions & 11 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports.keyId = (privateKey, callback) => {
try {
const publicKey = pki.setRsaPublicKey(privateKey.n, privateKey.e)
const spki = pki.publicKeyToSubjectPublicKeyInfo(publicKey)
const der = new Buffer(forge.asn1.toDer(spki).getBytes(), 'binary')
const der = Buffer.from(forge.asn1.toDer(spki).getBytes(), 'binary')
const jwk = rsaUtils.pkixToJwk(der)
const rsa = new rsaClass.RsaPublicKey(jwk)
rsa.hash((err, kid) => {
Expand All @@ -33,12 +33,12 @@ exports.certificateForKey = (privateKey, callback) => {
if (err) return callback(err)

const publicKey = pki.setRsaPublicKey(privateKey.n, privateKey.e)
const cert = pki.createCertificate();
cert.publicKey = publicKey;
cert.serialNumber = '01';
cert.validity.notBefore = new Date();
cert.validity.notAfter = new Date();
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 10);
const cert = pki.createCertificate()
cert.publicKey = publicKey
cert.serialNumber = '01'
cert.validity.notBefore = new Date()
cert.validity.notAfter = new Date()
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 10)
var attrs = [{
name: 'organizationName',
value: 'ipfs'
Expand All @@ -48,9 +48,9 @@ exports.certificateForKey = (privateKey, callback) => {
}, {
name: 'commonName',
value: kid
}];
cert.setSubject(attrs);
cert.setIssuer(attrs);
}]
cert.setSubject(attrs)
cert.setIssuer(attrs)
cert.setExtensions([{
name: 'basicConstraints',
cA: true
Expand All @@ -77,7 +77,7 @@ exports.certificateForKey = (privateKey, callback) => {
sslCA: true,
emailCA: true,
objCA: true
}]);
}])
// self-sign certificate
cert.sign(privateKey)

Expand Down
7 changes: 2 additions & 5 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
const async = require('async')
const LevelStore = require('datastore-level')

// use in the browser with level.js
const browserStore = new LevelStore('my/db/name', {db: require('level-js')})

describe('browser', () => {
const datastore1 = new LevelStore('test-keystore-1', {db: require('level-js')})
const datastore2 = new LevelStore('test-keystore-2', {db: require('level-js')})
const datastore1 = new LevelStore('test-keystore-1', {db: require('level-js')})
const datastore2 = new LevelStore('test-keystore-2', {db: require('level-js')})

before((done) => {
async.series([
Expand Down
15 changes: 7 additions & 8 deletions test/keychain.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'

Expand Down Expand Up @@ -28,12 +29,12 @@ module.exports = (datastore1, datastore2) => {
expect(() => new Keychain(datastore2)).to.throw()
})

it ('needs a NIST SP 800-132 non-weak pass phrase', () => {
expect(() => new Keychain(datastore2, { passPhrase: '< 20 character'})).to.throw()
it('needs a NIST SP 800-132 non-weak pass phrase', () => {
expect(() => new Keychain(datastore2, { passPhrase: '< 20 character' })).to.throw()
})

it('needs a store to persist a key', () => {
expect(() => new Keychain(null, { passPhrase: passPhrase})).to.throw()
expect(() => new Keychain(null, { passPhrase: passPhrase })).to.throw()
})

it('has default options', () => {
Expand Down Expand Up @@ -112,7 +113,6 @@ module.exports = (datastore1, datastore2) => {
})
})
})

})

describe('query', () => {
Expand Down Expand Up @@ -177,7 +177,7 @@ module.exports = (datastore1, datastore2) => {
})

it('is a PKCS #7 message', (done) => {
ks.cms.readData("not CMS", (err) => {
ks.cms.readData('not CMS', (err) => {
expect(err).to.exist()
done()
})
Expand Down Expand Up @@ -205,7 +205,6 @@ module.exports = (datastore1, datastore2) => {
done()
})
})

})

describe('exported key', () => {
Expand Down Expand Up @@ -243,7 +242,7 @@ module.exports = (datastore1, datastore2) => {
done()
})
})
})
})

describe('peer id', () => {
const alicePrivKey = 'CAASpgkwggSiAgEAAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAECggEAZtju/bcKvKFPz0mkHiaJcpycy9STKphorpCT83srBVQi59CdFU6Mj+aL/xt0kCPMVigJw8P3/YCEJ9J+rS8BsoWE+xWUEsJvtXoT7vzPHaAtM3ci1HZd302Mz1+GgS8Epdx+7F5p80XAFLDUnELzOzKftvWGZmWfSeDnslwVONkL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datastore1, datastore2) => {
before(function (done) {
const encoded = Buffer.from(alicePrivKey, 'base64')
PeerId.createFromPrivKey(encoded, (err, id) => {
expect(err).to.not.exist()
alice = id
done()
})
Expand Down Expand Up @@ -351,6 +351,5 @@ module.exports = (datastore1, datastore2) => {
})
})
})

})
}
8 changes: 6 additions & 2 deletions test/peerid.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('peer ID', () => {
before(function (done) {
const encoded = Buffer.from(sample.privKey, 'base64')
PeerId.createFromPrivKey(encoded, (err, id) => {
expect(err).to.not.exist()
peer = id
done()
})
Expand All @@ -44,6 +45,7 @@ describe('peer ID', () => {
// get protobuf version of the private key
const privateKeyProtobuf = peer.marshalPrivKey()
crypto.keys.unmarshalPrivateKey(privateKeyProtobuf, (err, key) => {
expect(err).to.not.exist()
// console.log('private key', key)
// console.log('\nprivate key der', key.marshal().toString('base64'))
done()
Expand All @@ -56,6 +58,7 @@ describe('peer ID', () => {
const rsa = new rsaClass.RsaPublicKey(jwk)
// console.log('rsa', rsa)
rsa.hash((err, keyId) => {
expect(err).to.not.exist()
// console.log('err', err)
// console.log('keyId', keyId)
// console.log('id decoded', multihash.decode(keyId))
Expand All @@ -78,6 +81,7 @@ describe('peer ID', () => {
const rsa = new rsaClass.RsaPublicKey(jwk)
// console.log('rsa', rsa)
rsa.hash((err, keyId) => {
expect(err).to.not.exist()
// console.log('err', err)
// console.log('keyId', keyId)
// console.log('id decoded', multihash.decode(keyId))
Expand All @@ -96,10 +100,10 @@ describe('peer ID', () => {
// get protobuf version of the private key
const privateKeyProtobuf = peer.marshalPrivKey()
crypto.keys.unmarshalPrivateKey(privateKeyProtobuf, (err, key) => {
expect(err).to.not.exist()
// console.log('private key', key)
//console.log('\nprivate key der', key.marshal().toString('base64'))
// console.log('\nprivate key der', key.marshal().toString('base64'))
done()
})
})

})

0 comments on commit 7c44c91

Please sign in to comment.