diff --git a/package.json b/package.json index 9722641e..e8597432 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "license": "MIT", "dependencies": { "async": "^2.5.0", - "libp2p-crypto": "~0.8.8", + "libp2p-crypto": "~0.9.0", "multihashing-async": "~0.4.6", "nodeify": "^1.0.1", "safe-buffer": "^5.1.1", @@ -38,6 +38,7 @@ "aegir": "^11.0.2", "benchmark": "^2.1.4", "chai": "^4.1.0", + "dirty-chai": "^2.0.1", "pre-commit": "^1.2.2" }, "pre-commit": [ @@ -61,4 +62,4 @@ "Friedel Ziegelmayer ", "Yusef Napora " ] -} \ No newline at end of file +} diff --git a/src/crypto/index.js b/src/crypto/index.js index ca09fbe6..7e2b4949 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -28,7 +28,7 @@ exports.hashAndSign = function (key, msg, callback) { }) multihashing.digest(msg, HASH_ALGORITHM, (err, digest) => { - if (err) return done(err) + if (err) { return done(err) } try { const sig = secp256k1.sign(digest, key) const sigDER = secp256k1.signatureExport(sig.signature) @@ -45,7 +45,7 @@ exports.hashAndVerify = function (key, sig, msg, callback) { }) multihashing.digest(msg, HASH_ALGORITHM, (err, digest) => { - if (err) return done(err) + if (err) { return done(err) } try { sig = secp256k1.signatureImport(sig) const valid = secp256k1.verify(digest, sig, key) diff --git a/src/index.js b/src/index.js index e5dafd03..3d3c030d 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ const multihashing = require('multihashing-async') const crypto = require('./crypto') -const pbm = require('libp2p-crypto').protobuf +const pbm = require('libp2p-crypto').keys.pbm class Secp256k1PublicKey { constructor (key) { diff --git a/test/secp256k1.spec.js b/test/secp256k1.spec.js index 65a95e82..d68f6530 100644 --- a/test/secp256k1.spec.js +++ b/test/secp256k1.spec.js @@ -1,45 +1,39 @@ /* eslint-env mocha */ 'use strict' -const expect = require('chai').expect +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) + const Buffer = require('safe-buffer').Buffer const secp256k1 = require('../src') const crypto = require('../src/crypto') const libp2pCrypto = require('libp2p-crypto') -const pbm = libp2pCrypto.protobuf +const pbm = libp2pCrypto.keys.pbm const randomBytes = libp2pCrypto.randomBytes describe('secp256k1 keys', () => { let key before((done) => { secp256k1.generateKeyPair((err, _key) => { - if (err) return done(err) + expect(err).to.not.exist() key = _key done() }) }) it('generates a valid key', (done) => { - expect( - key - ).to.be.an.instanceof( - secp256k1.Secp256k1PrivateKey - ) - expect( - key.public - ).to.be.an.instanceof( - secp256k1.Secp256k1PublicKey - ) + expect(key).to.be.an.instanceof(secp256k1.Secp256k1PrivateKey) + expect(key.public).to.be.an.instanceof(secp256k1.Secp256k1PublicKey) key.hash((err, digest) => { - if (err) return done(err) - + expect(err).to.not.exist() expect(digest).to.have.length(34) key.public.hash((err, digest) => { - if (err) return done(err) - + expect(err).to.not.exist() expect(digest).to.have.length(34) done() }) @@ -48,33 +42,25 @@ describe('secp256k1 keys', () => { it('optionally accepts a `bits` argument when generating a key', (done) => { secp256k1.generateKeyPair(256, (err, _key) => { - expect(err).to.not.exist + expect(err).to.not.exist() expect(_key).to.be.an.instanceof(secp256k1.Secp256k1PrivateKey) done() }) }) - it('requires a callback to generate a key', (done) => { - expect(() => - secp256k1.generateKeyPair() - ).to.throw() - done() + it('requires a callback to generate a key', () => { + expect(() => secp256k1.generateKeyPair()).to.throw() }) it('signs', (done) => { const text = randomBytes(512) key.sign(text, (err, sig) => { - if (err) { - return done(err) - } + expect(err).to.not.exist() key.public.verify(text, sig, (err, res) => { - if (err) { - return done(err) - } - - expect(res).to.be.eql(true) + expect(err).to.not.exist() + expect(res).to.equal(true) done() }) }) @@ -83,73 +69,36 @@ describe('secp256k1 keys', () => { it('encoding', (done) => { const keyMarshal = key.marshal() secp256k1.unmarshalSecp256k1PrivateKey(keyMarshal, (err, key2) => { - if (err) { - return done(err) - } + expect(err).to.not.exist() const keyMarshal2 = key2.marshal() - expect( - keyMarshal - ).to.be.eql( - keyMarshal2 - ) + expect(keyMarshal).to.eql(keyMarshal2) const pk = key.public const pkMarshal = pk.marshal() const pk2 = secp256k1.unmarshalSecp256k1PublicKey(pkMarshal) const pkMarshal2 = pk2.marshal() - expect( - pkMarshal - ).to.be.eql( - pkMarshal2 - ) + expect(pkMarshal).to.eql(pkMarshal2) done() }) }) describe('key equals', () => { it('equals itself', () => { - expect( - key.equals(key) - ).to.be.eql( - true - ) - - expect( - key.public.equals(key.public) - ).to.be.eql( - true - ) + expect(key.equals(key)).to.eql(true) + + expect(key.public.equals(key.public)).to.eql(true) }) it('not equals other key', (done) => { secp256k1.generateKeyPair(256, (err, key2) => { - if (err) return done(err) - - expect( - key.equals(key2) - ).to.be.eql( - false - ) - - expect( - key2.equals(key) - ).to.be.eql( - false - ) - - expect( - key.public.equals(key2.public) - ).to.be.eql( - false - ) - - expect( - key2.public.equals(key.public) - ).to.be.eql( - false - ) + expect(err).to.not.exist() + + expect(key.equals(key2)).to.eql(false) + expect(key2.equals(key)).to.eql(false) + expect(key.public.equals(key2.public)).to.eql(false) + expect(key2.public.equals(key.public)).to.eql(false) done() }) }) @@ -158,15 +107,11 @@ describe('secp256k1 keys', () => { it('sign and verify', (done) => { const data = Buffer.from('hello world') key.sign(data, (err, sig) => { - if (err) { - return done(err) - } + expect(err).to.not.exist() key.public.verify(data, sig, (err, valid) => { - if (err) { - return done(err) - } - expect(valid).to.be.eql(true) + expect(err).to.not.exist() + expect(valid).to.eql(true) done() }) }) @@ -175,15 +120,11 @@ describe('secp256k1 keys', () => { it('fails to verify for different data', (done) => { const data = Buffer.from('hello world') key.sign(data, (err, sig) => { - if (err) { - return done(err) - } + expect(err).to.not.exist() key.public.verify(Buffer.from('hello'), sig, (err, valid) => { - if (err) { - return done(err) - } - expect(valid).to.be.eql(false) + expect(err).to.not.exist() + expect(valid).to.eql(false) done() }) }) @@ -206,8 +147,8 @@ describe('key generation error', () => { it('returns an error if key generation fails', (done) => { secp256k1.generateKeyPair((err, key) => { - expect(err).to.exist - expect(key).to.not.exist + expect(err).to.exist() + expect(key).to.not.exist() done() }) }) @@ -229,19 +170,20 @@ describe('handles generation of invalid key', () => { it('returns an error if key generator returns an invalid key', (done) => { secp256k1.generateKeyPair((err, key) => { - expect(err).to.exist - expect(key).to.not.exist + expect(err).to.exist() + expect(key).to.not.exist() done() }) }) }) describe('crypto functions', () => { - let privKey, pubKey + let privKey + let pubKey before((done) => { crypto.generateKey((err, _key) => { - if (err) return done(err) + expect(err).to.not.exist() privKey = _key pubKey = crypto.computePublicKey(privKey) done() @@ -257,22 +199,17 @@ describe('crypto functions', () => { }) it('does not validate an invalid key', (done) => { - expect(() => { - crypto.validatePublicKey(Buffer.from('42')) - }).to.throw() - - expect(() => { - crypto.validatePrivateKey(Buffer.from('42')) - }).to.throw() + expect(() => crypto.validatePublicKey(Buffer.from('42'))).to.throw() + expect(() => crypto.validatePrivateKey(Buffer.from('42'))).to.throw() done() }) it('validates a correct signature', (done) => { crypto.hashAndSign(privKey, Buffer.from('hello'), (err, sig) => { - if (err) return done(err) + expect(err).to.not.exist() crypto.hashAndVerify(pubKey, sig, Buffer.from('hello'), (err, valid) => { - if (err) return done(err) - expect(valid).to.be.eql(true) + expect(err).to.not.exist() + expect(valid).to.equal(true) done() }) }) @@ -280,27 +217,27 @@ describe('crypto functions', () => { it('errors if given a null buffer to sign', (done) => { crypto.hashAndSign(privKey, null, (err, sig) => { - expect(err).to.exist - expect(sig).to.not.exist + expect(err).to.exist() + expect(sig).to.not.exist() done() }) }) it('errors when signing with an invalid key', (done) => { crypto.hashAndSign(Buffer.from('42'), Buffer.from('Hello'), (err, sig) => { - expect(err).to.exist - expect(sig).to.not.exist + expect(err).to.exist() + expect(sig).to.not.exist() done() }) }) it('errors if given a null buffer to validate', (done) => { crypto.hashAndSign(privKey, Buffer.from('hello'), (err, sig) => { - if (err) return done(err) + expect(err).to.not.exist() crypto.hashAndVerify(privKey, sig, null, (err, valid) => { - expect(err).to.exist - expect(valid).to.not.exist + expect(err).to.exist() + expect(valid).to.not.exist() done() }) }) @@ -308,40 +245,36 @@ describe('crypto functions', () => { it('errors when validating a message with an invalid signature', (done) => { crypto.hashAndVerify(pubKey, Buffer.from('invalid-sig'), Buffer.from('hello'), (err, valid) => { - expect(err).to.exist - expect(valid).to.not.exist + expect(err).to.exist() + expect(valid).to.not.exist() done() }) }) it('errors when signing with an invalid key', (done) => { crypto.hashAndSign(Buffer.from('42'), Buffer.from('Hello'), (err, sig) => { - expect(err).to.exist - expect(sig).to.not.exist + expect(err).to.exist() + expect(sig).to.not.exist() done() }) }) it('throws when compressing an invalid public key', (done) => { - expect(() => { - crypto.compressPublicKey(Buffer.from('42')) - }).to.throw() + expect(() => crypto.compressPublicKey(Buffer.from('42'))).to.throw() done() }) it('throws when decompressing an invalid public key', (done) => { - expect(() => { - crypto.decompressPublicKey(Buffer.from('42')) - }).to.throw() + expect(() => crypto.decompressPublicKey(Buffer.from('42'))).to.throw() done() }) it('compresses/decompresses a valid public key', (done) => { const decompressed = crypto.decompressPublicKey(pubKey) - expect(decompressed).to.exist + expect(decompressed).to.exist() expect(decompressed.length).to.be.eql(65) const recompressed = crypto.compressPublicKey(decompressed) - expect(recompressed).to.be.eql(pubKey) + expect(recompressed).to.eql(pubKey) done() }) }) @@ -353,13 +286,13 @@ describe('go interop', () => { // we need to first extract the key data from the protobuf, which is // normally handled by js-libp2p-crypto const decoded = pbm.PrivateKey.decode(fixtures.privateKey) - expect(decoded.Type).to.be.eql(pbm.KeyType.Secp256k1) + expect(decoded.Type).to.eql(pbm.KeyType.Secp256k1) secp256k1.unmarshalSecp256k1PrivateKey(decoded.Data, (err, key) => { - if (err) return done(err) + expect(err).to.not.exist() expect(key).to.be.an.instanceof(secp256k1.Secp256k1PrivateKey) - expect(key.bytes).to.be.eql(fixtures.privateKey) + expect(key.bytes).to.eql(fixtures.privateKey) done() }) }) @@ -370,20 +303,20 @@ describe('go interop', () => { const key = secp256k1.unmarshalSecp256k1PublicKey(decoded.Data) expect(key).to.be.an.instanceof(secp256k1.Secp256k1PublicKey) - expect(key.bytes).to.be.eql(fixtures.publicKey) + expect(key.bytes).to.eql(fixtures.publicKey) done() }) it('generates the same signature as go-libp2p-crypto', (done) => { const decoded = pbm.PrivateKey.decode(fixtures.privateKey) - expect(decoded.Type).to.be.eql(pbm.KeyType.Secp256k1) + expect(decoded.Type).to.eql(pbm.KeyType.Secp256k1) secp256k1.unmarshalSecp256k1PrivateKey(decoded.Data, (err, key) => { - if (err) return done(err) + expect(err).to.not.exist() key.sign(fixtures.message, (err, sig) => { - if (err) return done(err) - expect(sig).to.be.eql(fixtures.signature) + expect(err).to.not.exist() + expect(sig).to.eql(fixtures.signature) done() }) })