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

Commit

Permalink
chore: convert from callbacks to async
Browse files Browse the repository at this point in the history
BREAKING CHANGE: All places in the API that used callbacks are now replaced with async/await
  • Loading branch information
dirkmc committed Feb 19, 2019
1 parent 0b2b8e8 commit d971398
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 74 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
12 changes: 6 additions & 6 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ 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()]

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 = {
Expand Down
29 changes: 10 additions & 19 deletions src/validators/public-key.js
Original file line number Diff line number Diff line change
@@ -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/<keymultihash>'`
* @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 = {
Expand Down
74 changes: 29 additions & 45 deletions test/validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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/
)
})
})

Expand All @@ -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)
})
})
})

0 comments on commit d971398

Please sign in to comment.