diff --git a/package.json b/package.json index 8ba7c5f..4ebe7ad 100644 --- a/package.json +++ b/package.json @@ -41,17 +41,16 @@ "aegir": "^12.0.6", "chai": "^4.1.2", "dirty-chai": "^2.0.1", - "libp2p-crypto": "~0.10.3", - "peer-id": "~0.11.0", + "libp2p-crypto": "libp2p/js-libp2p-crypto#feat/async-await", + "peer-id": "libp2p/js-peer-id#feat/async-await", "pre-commit": "^1.2.2" }, "dependencies": { - "async": "^2.5.0", "buffer-split": "^1.0.0", "err-code": "^1.1.2", "left-pad": "^1.1.3", "multihashes": "~0.4.14", - "multihashing-async": "~0.4.6", + "multihashing-async": "multiformats/js-multihashing-async#feat/async-iterators", "protons": "^1.0.0" }, "contributors": [ diff --git a/src/validator.js b/src/validator.js index 2529418..bff4109 100644 --- a/src/validator.js +++ b/src/validator.js @@ -6,19 +6,19 @@ const errcode = require('err-code') /** * Checks a record and ensures it is still valid. * It runs the needed validators. + * If verification fails the returned Promise will reject with the error. * * @param {Object} validators * @param {Record} record - * @param {function(Error)} callback - * @returns {undefined} + * @returns {Promise} */ -const verifyRecord = (validators, record, callback) => { +const verifyRecord = (validators, record) => { const key = record.key const parts = bsplit(key, Buffer.from('/')) if (parts.length < 3) { // No validator available - return callback() + return } const validator = validators[parts[1].toString()] @@ -26,10 +26,10 @@ const verifyRecord = (validators, record, callback) => { if (!validator) { const errMsg = `Invalid record keytype` - return callback(errcode(new Error(errMsg), 'ERR_INVALID_RECORD_KEY_TYPE')) + throw errcode(new Error(errMsg), 'ERR_INVALID_RECORD_KEY_TYPE') } - validator.func(key, record.value, callback) + return validator.func(key, record.value) } module.exports = { diff --git a/src/validators/public-key.js b/src/validators/public-key.js index 5de337d..0673992 100644 --- a/src/validators/public-key.js +++ b/src/validators/public-key.js @@ -1,48 +1,39 @@ 'use strict' -const setImmediate = require('async/setImmediate') const multihashing = require('multihashing-async') /** * Validator for publick key records. * Verifies that the passed in record value is the PublicKey * that matches the passed in key. + * If validation fails the returned Promise will reject with the error. * * @param {Buffer} key - A valid key is of the form `'/pk/'` * @param {Buffer} publicKey - The public key to validate against (protobuf encoded). - * @param {function(Error)} callback - * @returns {undefined} + * @returns {Promise} */ -const validatePublicKeyRecord = (key, publicKey, callback) => { - const done = (err) => setImmediate(() => callback(err)) - +const validatePublicKeyRecord = async (key, publicKey) => { if (!Buffer.isBuffer(key)) { - return done(new Error('"key" must be a Buffer')) + throw new Error('"key" must be a Buffer') } if (key.length < 3) { - return done(new Error('invalid public key record')) + throw new Error('invalid public key record') } const prefix = key.slice(0, 4).toString() if (prefix !== '/pk/') { - return done(new Error('key was not prefixed with /pk/')) + throw new Error('key was not prefixed with /pk/') } const keyhash = key.slice(4) - multihashing(publicKey, 'sha2-256', (err, publicKeyHash) => { - if (err) { - return done(err) - } - - if (!keyhash.equals(publicKeyHash)) { - return done(new Error('public key does not match passed in key')) - } + const publicKeyHash = await multihashing(publicKey, 'sha2-256') - done() - }) + if (!keyhash.equals(publicKeyHash)) { + throw new Error('public key does not match passed in key') + } } module.exports = { diff --git a/test/validator.spec.js b/test/validator.spec.js index ea16ee3..d911f01 100644 --- a/test/validator.spec.js +++ b/test/validator.spec.js @@ -5,8 +5,6 @@ const chai = require('chai') chai.use(require('dirty-chai')) const expect = chai.expect -const waterfall = require('async/waterfall') -const each = require('async/each') const crypto = require('libp2p-crypto') const PeerId = require('peer-id') @@ -47,57 +45,47 @@ describe('validator', () => { let hash let cases - before((done) => { - waterfall([ - (cb) => crypto.keys.generateKeyPair('rsa', 1024, cb), - (pair, cb) => { - key = pair - pair.public.hash(cb) - }, - (_hash, cb) => { - hash = _hash - cases = generateCases(hash) - cb() - } - ], done) + before(async () => { + key = await crypto.keys.generateKeyPair('rsa', 1024) + hash = await key.public.hash() + cases = generateCases(hash) }) describe('verifyRecord', () => { - it('calls matching validator', (done) => { + it('calls matching validator', () => { const k = Buffer.from('/hello/you') const rec = new Record(k, Buffer.from('world'), new PeerId(hash)) const validators = { hello: { - func (key, value, cb) { + func (key, value) { expect(key).to.eql(k) expect(value).to.eql(Buffer.from('world')) - cb() }, sign: false } } - validator.verifyRecord(validators, rec, done) + return validator.verifyRecord(validators, rec) }) - it('calls not matching any validator', (done) => { + it('calls not matching any validator', () => { const k = Buffer.from('/hallo/you') const rec = new Record(k, Buffer.from('world'), new PeerId(hash)) const validators = { hello: { - func (key, value, cb) { + func (key, value) { expect(key).to.eql(k) expect(value).to.eql(Buffer.from('world')) - cb() }, sign: false } } - validator.verifyRecord(validators, rec, (err) => { - expect(err).to.exist() - done() - }) + return expect( + () => validator.verifyRecord(validators, rec) + ).to.throw( + /Invalid record keytype/ + ) }) }) @@ -107,40 +95,36 @@ describe('validator', () => { }) describe('public key', () => { - it('exports func and sing', () => { + it('exports func and sign', () => { const pk = validator.validators.pk expect(pk).to.have.property('func') expect(pk).to.have.property('sign', false) }) - it('does not error on valid record', (done) => { - each(cases.valid.publicKey, (k, cb) => { - validator.validators.pk.func(k, key.public.bytes, cb) - }, done) + it('does not error on valid record', () => { + return Promise.all(cases.valid.publicKey, (k) => { + return validator.validators.pk.func(k, key.public.bytes) + }) }) - it('throws on invalid records', (done) => { - each(cases.invalid.publicKey, (k, cb) => { - validator.validators.pk.func(k, key.public.bytes, (err) => { - expect(err).to.exist() - cb() - }) - }, done) + it('throws on invalid records', () => { + return Promise.all(cases.invalid.publicKey, (k) => { + return expect( + () => validator.validators.pk.func(k, key.public.bytes) + ).to.throw() + }) }) }) }) describe('go interop', () => { - it('record with key from from go', (done) => { + it('record with key from from go', async () => { const pubKey = crypto.keys.unmarshalPublicKey(fixture.publicKey) - pubKey.hash((err, hash) => { - expect(err).to.not.exist() - const k = Buffer.concat([Buffer.from('/pk/'), hash]) - - validator.validators.pk.func(k, pubKey.bytes, done) - }) + const hash = await pubKey.hash() + const k = Buffer.concat([Buffer.from('/pk/'), hash]) + return validator.validators.pk.func(k, pubKey.bytes) }) }) })