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

Commit 4a4c0e3

Browse files
committed
chore: convert from callbacks to async
BREAKING CHANGE: All places in the API that used callbacks are now replaced with async/await
1 parent 5a44cee commit 4a4c0e3

File tree

4 files changed

+48
-74
lines changed

4 files changed

+48
-74
lines changed

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,16 @@
4141
"aegir": "^18.1.1",
4242
"chai": "^4.2.0",
4343
"dirty-chai": "^2.0.1",
44-
"libp2p-crypto": "~0.16.0",
45-
"peer-id": "~0.12.2",
44+
"libp2p-crypto": "libp2p/js-libp2p-crypto#feat/async-await",
45+
"peer-id": "libp2p/js-peer-id#feat/async-await",
4646
"pre-commit": "^1.2.2"
4747
},
4848
"dependencies": {
49-
"async": "^2.6.2",
5049
"buffer-split": "^1.0.0",
5150
"err-code": "^1.1.2",
5251
"left-pad": "^1.3.0",
5352
"multihashes": "~0.4.14",
54-
"multihashing-async": "~0.5.2",
53+
"multihashing-async": "multiformats/js-multihashing-async#feat/async-iterators",
5554
"protons": "^1.0.1"
5655
},
5756
"contributors": [

src/validator.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@ const errcode = require('err-code')
66
/**
77
* Checks a record and ensures it is still valid.
88
* It runs the needed validators.
9+
* If verification fails the returned Promise will reject with the error.
910
*
1011
* @param {Object} validators
1112
* @param {Record} record
12-
* @param {function(Error)} callback
13-
* @returns {undefined}
13+
* @returns {Promise}
1414
*/
15-
const verifyRecord = (validators, record, callback) => {
15+
const verifyRecord = (validators, record) => {
1616
const key = record.key
1717
const parts = bsplit(key, Buffer.from('/'))
1818

1919
if (parts.length < 3) {
2020
// No validator available
21-
return callback()
21+
return
2222
}
2323

2424
const validator = validators[parts[1].toString()]
2525

2626
if (!validator) {
2727
const errMsg = `Invalid record keytype`
2828

29-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_RECORD_KEY_TYPE'))
29+
throw errcode(new Error(errMsg), 'ERR_INVALID_RECORD_KEY_TYPE')
3030
}
3131

32-
validator.func(key, record.value, callback)
32+
return validator.func(key, record.value)
3333
}
3434

3535
module.exports = {

src/validators/public-key.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,39 @@
11
'use strict'
22

3-
const setImmediate = require('async/setImmediate')
43
const multihashing = require('multihashing-async')
54

65
/**
76
* Validator for publick key records.
87
* Verifies that the passed in record value is the PublicKey
98
* that matches the passed in key.
9+
* If validation fails the returned Promise will reject with the error.
1010
*
1111
* @param {Buffer} key - A valid key is of the form `'/pk/<keymultihash>'`
1212
* @param {Buffer} publicKey - The public key to validate against (protobuf encoded).
13-
* @param {function(Error)} callback
14-
* @returns {undefined}
13+
* @returns {Promise}
1514
*/
16-
const validatePublicKeyRecord = (key, publicKey, callback) => {
17-
const done = (err) => setImmediate(() => callback(err))
18-
15+
const validatePublicKeyRecord = async (key, publicKey) => {
1916
if (!Buffer.isBuffer(key)) {
20-
return done(new Error('"key" must be a Buffer'))
17+
throw new Error('"key" must be a Buffer')
2118
}
2219

2320
if (key.length < 3) {
24-
return done(new Error('invalid public key record'))
21+
throw new Error('invalid public key record')
2522
}
2623

2724
const prefix = key.slice(0, 4).toString()
2825

2926
if (prefix !== '/pk/') {
30-
return done(new Error('key was not prefixed with /pk/'))
27+
throw new Error('key was not prefixed with /pk/')
3128
}
3229

3330
const keyhash = key.slice(4)
3431

35-
multihashing(publicKey, 'sha2-256', (err, publicKeyHash) => {
36-
if (err) {
37-
return done(err)
38-
}
39-
40-
if (!keyhash.equals(publicKeyHash)) {
41-
return done(new Error('public key does not match passed in key'))
42-
}
32+
const publicKeyHash = await multihashing(publicKey, 'sha2-256')
4333

44-
done()
45-
})
34+
if (!keyhash.equals(publicKeyHash)) {
35+
throw new Error('public key does not match passed in key')
36+
}
4637
}
4738

4839
module.exports = {

test/validator.spec.js

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
const chai = require('chai')
66
chai.use(require('dirty-chai'))
77
const expect = chai.expect
8-
const waterfall = require('async/waterfall')
9-
const each = require('async/each')
108
const crypto = require('libp2p-crypto')
119
const PeerId = require('peer-id')
1210

@@ -47,57 +45,47 @@ describe('validator', () => {
4745
let hash
4846
let cases
4947

50-
before((done) => {
51-
waterfall([
52-
(cb) => crypto.keys.generateKeyPair('rsa', 1024, cb),
53-
(pair, cb) => {
54-
key = pair
55-
pair.public.hash(cb)
56-
},
57-
(_hash, cb) => {
58-
hash = _hash
59-
cases = generateCases(hash)
60-
cb()
61-
}
62-
], done)
48+
before(async () => {
49+
key = await crypto.keys.generateKeyPair('rsa', 1024)
50+
hash = await key.public.hash()
51+
cases = generateCases(hash)
6352
})
6453

6554
describe('verifyRecord', () => {
66-
it('calls matching validator', (done) => {
55+
it('calls matching validator', () => {
6756
const k = Buffer.from('/hello/you')
6857
const rec = new Record(k, Buffer.from('world'), new PeerId(hash))
6958

7059
const validators = {
7160
hello: {
72-
func (key, value, cb) {
61+
func (key, value) {
7362
expect(key).to.eql(k)
7463
expect(value).to.eql(Buffer.from('world'))
75-
cb()
7664
},
7765
sign: false
7866
}
7967
}
80-
validator.verifyRecord(validators, rec, done)
68+
return validator.verifyRecord(validators, rec)
8169
})
8270

83-
it('calls not matching any validator', (done) => {
71+
it('calls not matching any validator', () => {
8472
const k = Buffer.from('/hallo/you')
8573
const rec = new Record(k, Buffer.from('world'), new PeerId(hash))
8674

8775
const validators = {
8876
hello: {
89-
func (key, value, cb) {
77+
func (key, value) {
9078
expect(key).to.eql(k)
9179
expect(value).to.eql(Buffer.from('world'))
92-
cb()
9380
},
9481
sign: false
9582
}
9683
}
97-
validator.verifyRecord(validators, rec, (err) => {
98-
expect(err).to.exist()
99-
done()
100-
})
84+
return expect(
85+
() => validator.verifyRecord(validators, rec)
86+
).to.throw(
87+
/Invalid record keytype/
88+
)
10189
})
10290
})
10391

@@ -107,40 +95,36 @@ describe('validator', () => {
10795
})
10896

10997
describe('public key', () => {
110-
it('exports func and sing', () => {
98+
it('exports func and sign', () => {
11199
const pk = validator.validators.pk
112100

113101
expect(pk).to.have.property('func')
114102
expect(pk).to.have.property('sign', false)
115103
})
116104

117-
it('does not error on valid record', (done) => {
118-
each(cases.valid.publicKey, (k, cb) => {
119-
validator.validators.pk.func(k, key.public.bytes, cb)
120-
}, done)
105+
it('does not error on valid record', () => {
106+
return Promise.all(cases.valid.publicKey, (k) => {
107+
return validator.validators.pk.func(k, key.public.bytes)
108+
})
121109
})
122110

123-
it('throws on invalid records', (done) => {
124-
each(cases.invalid.publicKey, (k, cb) => {
125-
validator.validators.pk.func(k, key.public.bytes, (err) => {
126-
expect(err).to.exist()
127-
cb()
128-
})
129-
}, done)
111+
it('throws on invalid records', () => {
112+
return Promise.all(cases.invalid.publicKey, (k) => {
113+
return expect(
114+
() => validator.validators.pk.func(k, key.public.bytes)
115+
).to.throw()
116+
})
130117
})
131118
})
132119
})
133120

134121
describe('go interop', () => {
135-
it('record with key from from go', (done) => {
122+
it('record with key from from go', async () => {
136123
const pubKey = crypto.keys.unmarshalPublicKey(fixture.publicKey)
137124

138-
pubKey.hash((err, hash) => {
139-
expect(err).to.not.exist()
140-
const k = Buffer.concat([Buffer.from('/pk/'), hash])
141-
142-
validator.validators.pk.func(k, pubKey.bytes, done)
143-
})
125+
const hash = await pubKey.hash()
126+
const k = Buffer.concat([Buffer.from('/pk/'), hash])
127+
return validator.validators.pk.func(k, pubKey.bytes)
144128
})
145129
})
146130
})

0 commit comments

Comments
 (0)