feat: async await (#87)

BREAKING CHANGE: API refactored to use async/await
This commit is contained in:
Henrique Dias 2019-07-11 18:09:21 +01:00 committed by Jacob Heun
parent bbabd7451e
commit c3463c7421
5 changed files with 200 additions and 336 deletions

View File

@ -28,7 +28,7 @@
- [API](#api)
- [Create](#create)
- [`new PeerId(id[, privKey, pubKey])`](#new-peeridid-privkey-pubkey)
- [`create([opts], callback)`](#createopts-callback)
- [`create([opts])`](#createopts)
- [Import](#import)
- [`createFromHexString(str)`](#createfromhexstringstr)
- [`createFromBytes(buf)`](#createfrombytesbuf)
@ -57,11 +57,10 @@ The public key is a base64 encoded string of a protobuf containing an RSA DER bu
```JavaScript
const PeerId = require('peer-id')
PeerId.create({ bits: 1024 }, (err, id) => {
if (err) { throw err }
console.log(JSON.stringify(id.toJSON(), null, 2))
})
const id = await PeerId.create({ bits: 1024 })
console.log(JSON.stringify(id.toJSON(), null, 2))
```
```bash
{
"id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
@ -124,14 +123,13 @@ const PeerId = require('peer-id')
The key format is detailed in [libp2p-crypto](https://github.com/libp2p/js-libp2p-crypto).
### `create([opts], callback)`
### `create([opts])`
Generates a new Peer ID, complete with public/private keypair.
- `opts: Object`: Default: `{bits: 2048}`
- `callback: Function`
Calls back `callback` with `err, id`.
Returns `Promise<PeerId>`.
## Import
@ -139,32 +137,44 @@ Calls back `callback` with `err, id`.
Creates a Peer ID from hex string representing the key's multihash.
Returns `PeerId.
### `createFromBytes(buf)`
Creates a Peer ID from a buffer representing the key's multihash.
Returns `PeerId`.
### `createFromB58String(str)`
Creates a Peer ID from a Base58 string representing the key's multihash.
Returns `PeerId`.
### `createFromPubKey(pubKey)`
- `publicKey: Buffer`
Creates a Peer ID from a buffer containing a public key.
Returns `Promise<PeerId>`.
### `createFromPrivKey(privKey)`
- `privKey: Buffer`
Creates a Peer ID from a buffer containing a private key.
Returns `Promise<PeerId>`.
### `createFromJSON(obj)`
- `obj.id: String` - The multihash encoded in `base58`
- `obj.pubKey: String` - The public key in protobuf format, encoded in `base64`
- `obj.privKey: String` - The private key in protobuf format, encoded in `base64`
Returns `Promise<PeerId>`.
## Export
### `toHexString()`

View File

@ -34,15 +34,14 @@
},
"homepage": "https://github.com/libp2p/js-peer-id",
"devDependencies": {
"aegir": "^18.2.2",
"bundlesize": "~0.17.1",
"aegir": "^19.0.5",
"bundlesize": "~0.18.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1"
},
"dependencies": {
"async": "^2.6.2",
"class-is": "^1.1.0",
"libp2p-crypto": "~0.16.1",
"libp2p-crypto": "~0.17.0",
"multihashes": "~0.4.14"
},
"repository": {

View File

@ -4,10 +4,9 @@
const PeerId = require('./index.js')
PeerId.create((err, id) => {
if (err) {
throw err
}
async function main () {
const id = await PeerId.create()
console.log(JSON.stringify(id.toJSON(), null, 2)) // eslint-disable-line no-console
}
console.log(JSON.stringify(id.toJSON(), null, 2))
})
main()

View File

@ -7,7 +7,6 @@
const mh = require('multihashes')
const cryptoKeys = require('libp2p-crypto/src/keys')
const assert = require('assert')
const waterfall = require('async/waterfall')
const withIs = require('class-is')
class PeerId {
@ -119,176 +118,111 @@ class PeerId {
/*
* Check if this PeerId instance is valid (privKey -> pubKey -> Id)
*/
isValid (callback) {
// TODO Needs better checking
if (this.privKey &&
isValid () {
// TODO: needs better checking
return Boolean(this.privKey &&
this.privKey.public &&
this.privKey.public.bytes &&
Buffer.isBuffer(this.pubKey.bytes) &&
this.privKey.public.bytes.equals(this.pubKey.bytes)) {
callback()
} else {
callback(new Error('Keys not match'))
}
this.privKey.public.bytes.equals(this.pubKey.bytes))
}
}
const PeerIdWithIs = withIs(PeerId, { className: 'PeerId', symbolName: '@libp2p/js-peer-id/PeerId' })
const PeerIdWithIs = withIs(PeerId, {
className: 'PeerId',
symbolName: '@libp2p/js-peer-id/PeerId'
})
exports = module.exports = PeerIdWithIs
// generation
exports.create = function (opts, callback) {
if (typeof opts === 'function') {
callback = opts
opts = {}
}
exports.create = async (opts) => {
opts = opts || {}
opts.bits = opts.bits || 2048
waterfall([
(cb) => cryptoKeys.generateKeyPair('RSA', opts.bits, cb),
(privKey, cb) => privKey.public.hash((err, digest) => {
cb(err, digest, privKey)
})
], (err, digest, privKey) => {
if (err) {
return callback(err)
}
const key = await cryptoKeys.generateKeyPair('RSA', opts.bits)
const digest = await key.public.hash()
callback(null, new PeerIdWithIs(digest, privKey))
})
return new PeerIdWithIs(digest, key)
}
exports.createFromHexString = function (str) {
exports.createFromHexString = (str) => {
return new PeerIdWithIs(mh.fromHexString(str))
}
exports.createFromBytes = function (buf) {
exports.createFromBytes = (buf) => {
return new PeerIdWithIs(buf)
}
exports.createFromB58String = function (str) {
exports.createFromB58String = (str) => {
return new PeerIdWithIs(mh.fromB58String(str))
}
// Public Key input will be a buffer
exports.createFromPubKey = function (key, callback) {
if (typeof callback !== 'function') {
throw new Error('callback is required')
}
let pubKey
try {
exports.createFromPubKey = async (key) => {
let buf = key
if (typeof buf === 'string') {
buf = Buffer.from(key, 'base64')
}
if (!Buffer.isBuffer(buf)) throw new Error('Supplied key is neither a base64 string nor a buffer')
pubKey = cryptoKeys.unmarshalPublicKey(buf)
} catch (err) {
return callback(err)
if (!Buffer.isBuffer(buf)) {
throw new Error('Supplied key is neither a base64 string nor a buffer')
}
pubKey.hash((err, digest) => {
if (err) {
return callback(err)
}
callback(null, new PeerIdWithIs(digest, null, pubKey))
})
const pubKey = await cryptoKeys.unmarshalPublicKey(buf)
const digest = await pubKey.hash()
return new PeerIdWithIs(digest, null, pubKey)
}
// Private key input will be a string
exports.createFromPrivKey = function (key, callback) {
if (typeof callback !== 'function') {
throw new Error('callback is required')
}
exports.createFromPrivKey = async (key) => {
let buf = key
try {
if (typeof buf === 'string') {
buf = Buffer.from(key, 'base64')
}
if (!Buffer.isBuffer(buf)) throw new Error('Supplied key is neither a base64 string nor a buffer')
} catch (err) {
return callback(err)
if (!Buffer.isBuffer(buf)) {
throw new Error('Supplied key is neither a base64 string nor a buffer')
}
waterfall([
(cb) => cryptoKeys.unmarshalPrivateKey(buf, cb),
(privKey, cb) => privKey.public.hash((err, digest) => {
cb(err, digest, privKey)
})
], (err, digest, privKey) => {
if (err) {
return callback(err)
}
const privKey = await cryptoKeys.unmarshalPrivateKey(buf)
const digest = await privKey.public.hash()
callback(null, new PeerIdWithIs(digest, privKey, privKey.public))
})
return new PeerIdWithIs(digest, privKey, privKey.public)
}
exports.createFromJSON = function (obj, callback) {
if (typeof callback !== 'function') {
throw new Error('callback is required')
exports.createFromJSON = async (obj) => {
let id = mh.fromB58String(obj.id)
let rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64')
let rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64')
let pub = rawPubKey && await cryptoKeys.unmarshalPublicKey(rawPubKey)
if (!rawPrivKey) {
return new PeerIdWithIs(id, null, pub)
}
let id
let rawPrivKey
let rawPubKey
let pub
const privKey = await cryptoKeys.unmarshalPrivateKey(rawPrivKey)
const privDigest = await privKey.public.hash()
let pubDigest
try {
id = mh.fromB58String(obj.id)
rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64')
rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64')
pub = rawPubKey && cryptoKeys.unmarshalPublicKey(rawPubKey)
} catch (err) {
return callback(err)
}
if (rawPrivKey) {
waterfall([
(cb) => cryptoKeys.unmarshalPrivateKey(rawPrivKey, cb),
(priv, cb) => priv.public.hash((err, digest) => {
cb(err, digest, priv)
}),
(privDigest, priv, cb) => {
if (pub) {
pub.hash((err, pubDigest) => {
cb(err, privDigest, priv, pubDigest)
})
} else {
cb(null, privDigest, priv)
}
}
], (err, privDigest, priv, pubDigest) => {
if (err) {
return callback(err)
pubDigest = await pub.hash()
}
if (pub && !privDigest.equals(pubDigest)) {
return callback(new Error('Public and private key do not match'))
throw new Error('Public and private key do not match')
}
if (id && !privDigest.equals(id)) {
return callback(new Error('Id and private key do not match'))
throw new Error('Id and private key do not match')
}
callback(null, new PeerIdWithIs(id, priv, pub))
})
} else {
callback(null, new PeerIdWithIs(id, null, pub))
}
return new PeerIdWithIs(id, privKey, pub)
}
exports.isPeerId = function (peerId) {
exports.isPeerId = (peerId) => {
return Boolean(typeof peerId === 'object' &&
peerId._id &&
peerId._idB58String)

View File

@ -8,7 +8,6 @@ chai.use(dirtyChai)
const expect = chai.expect
const crypto = require('libp2p-crypto')
const mh = require('multihashes')
const parallel = require('async/parallel')
const PeerId = require('../src')
@ -32,34 +31,24 @@ describe('PeerId', () => {
expect(PeerId).to.throw(Error)
})
it('create a new id', (done) => {
PeerId.create(testOpts, (err, id) => {
expect(err).to.not.exist()
it('create a new id', async () => {
const id = await PeerId.create(testOpts)
expect(id.toB58String().length).to.equal(46)
done()
})
})
it('isPeerId', (done) => {
PeerId.create(testOpts, (err, id) => {
expect(err).to.not.exist()
it('isPeerId', async () => {
const id = await PeerId.create(testOpts)
expect(PeerId.isPeerId(id)).to.equal(true)
expect(PeerId.isPeerId('aaa')).to.equal(false)
expect(PeerId.isPeerId(Buffer.from('batatas'))).to.equal(false)
done()
})
})
it('throws on changing the id', function (done) {
this.timeout(10000)
PeerId.create(testOpts, (err, id) => {
expect(err).to.not.exist()
it('throws on changing the id', async () => {
const id = await PeerId.create(testOpts)
expect(id.toB58String().length).to.equal(46)
expect(() => {
id.id = Buffer.from('hello')
}).to.throw(/immutable/)
done()
})
})
it('recreate an Id from Hex string', () => {
@ -77,72 +66,44 @@ describe('PeerId', () => {
expect(testIdB58String).to.equal(id.toB58String())
})
it('Recreate from a Public Key', (done) => {
PeerId.createFromPubKey(testId.pubKey, (err, id) => {
expect(err).to.not.exist()
it('Recreate from a Public Key', async () => {
const id = await PeerId.createFromPubKey(testId.pubKey)
expect(testIdB58String).to.equal(id.toB58String())
done()
})
})
it('Recreate from a Private Key', (done) => {
PeerId.createFromPrivKey(testId.privKey, (err, id) => {
expect(err).to.not.exist()
it('Recreate from a Private Key', async () => {
const id = await PeerId.createFromPrivKey(testId.privKey)
expect(testIdB58String).to.equal(id.toB58String())
const encoded = Buffer.from(testId.privKey, 'base64')
PeerId.createFromPrivKey(encoded, (err, id2) => {
expect(err).to.not.exist()
const id2 = await PeerId.createFromPrivKey(encoded)
expect(testIdB58String).to.equal(id2.toB58String())
expect(id.marshalPubKey()).to.deep.equal(id2.marshalPubKey())
done()
})
})
})
it('Compare generated ID with one created from PubKey', (done) => {
PeerId.create(testOpts, (err, id1) => {
expect(err).to.not.exist()
PeerId.createFromPubKey(id1.marshalPubKey(), (err, id2) => {
expect(err).to.not.exist()
it('Compare generated ID with one created from PubKey', async () => {
const id1 = await PeerId.create(testOpts)
const id2 = await PeerId.createFromPubKey(id1.marshalPubKey())
expect(id1.id).to.be.eql(id2.id)
done()
})
})
})
it('Works with default options', function (done) {
it('Works with default options', async function () {
this.timeout(10000)
PeerId.create((err, id) => {
expect(err).to.not.exist()
const id = await PeerId.create()
expect(id.toB58String().length).to.equal(46)
done()
})
})
it('Non-default # of bits', function (done) {
it('Non-default # of bits', async function () {
this.timeout(1000 * 60)
PeerId.create(testOpts, (err, shortId) => {
expect(err).to.not.exist()
PeerId.create({ bits: 1024 }, (err, longId) => {
expect(err).to.not.exist()
const shortId = await PeerId.create(testOpts)
const longId = await PeerId.create({ bits: 1024 })
expect(shortId.privKey.bytes.length).is.below(longId.privKey.bytes.length)
done()
})
})
})
it('Pretty printing', (done) => {
PeerId.create(testOpts, (err, id1) => {
expect(err).to.not.exist()
PeerId.createFromPrivKey(id1.toJSON().privKey, (err, id2) => {
expect(err).to.not.exist()
it('Pretty printing', async () => {
const id1 = await PeerId.create(testOpts)
const id2 = await PeerId.createFromPrivKey((id1.toJSON()).privKey)
expect(id1.toPrint()).to.be.eql(id2.toPrint())
expect(id1.toPrint()).to.equal('<peer.ID ' + id1.toB58String().substr(2, 6) + '>')
done()
})
})
})
it('toBytes', () => {
@ -150,120 +111,88 @@ describe('PeerId', () => {
expect(id.toBytes().toString('hex')).to.equal(testIdBytes.toString('hex'))
})
it('isEqual', (done) => {
parallel([
(cb) => PeerId.create(testOpts, cb),
(cb) => PeerId.create(testOpts, cb)
], (err, ids) => {
expect(err).to.not.exist()
it('isEqual', async () => {
const ids = await Promise.all([
PeerId.create(testOpts),
PeerId.create(testOpts)
])
expect(ids[0].isEqual(ids[0])).to.equal(true)
expect(ids[0].isEqual(ids[1])).to.equal(false)
expect(ids[0].isEqual(ids[0].id)).to.equal(true)
expect(ids[0].isEqual(ids[1].id)).to.equal(false)
done()
})
})
describe('fromJSON', () => {
it('full node', (done) => {
PeerId.create(testOpts, (err, id) => {
expect(err).to.not.exist()
PeerId.createFromJSON(id.toJSON(), (err, other) => {
expect(err).to.not.exist()
it('full node', async () => {
const id = await PeerId.create(testOpts)
const other = await PeerId.createFromJSON(id.toJSON())
expect(id.toB58String()).to.equal(other.toB58String())
expect(id.privKey.bytes).to.eql(other.privKey.bytes)
expect(id.pubKey.bytes).to.eql(other.pubKey.bytes)
done()
})
})
})
it('only id', (done) => {
crypto.keys.generateKeyPair('RSA', 1024, (err, key) => {
expect(err).to.not.exist()
key.public.hash((err, digest) => {
expect(err).to.not.exist()
it('only id', async () => {
const key = await crypto.keys.generateKeyPair('RSA', 1024)
const digest = await key.public.hash()
const id = PeerId.createFromBytes(digest)
expect(id.privKey).to.not.exist()
expect(id.pubKey).to.not.exist()
PeerId.createFromJSON(id.toJSON(), (err, other) => {
expect(err).to.not.exist()
const other = await PeerId.createFromJSON(id.toJSON())
expect(id.toB58String()).to.equal(other.toB58String())
done()
})
})
})
})
it('go interop', (done) => {
PeerId.createFromJSON(goId, (err, id) => {
expect(err).to.not.exist()
id.privKey.public.hash((err, digest) => {
expect(err).to.not.exist()
it('go interop', async () => {
const id = await PeerId.createFromJSON(goId)
const digest = await id.privKey.public.hash()
expect(mh.toB58String(digest)).to.eql(goId.id)
done()
})
})
})
})
it('set privKey (valid)', (done) => {
PeerId.create(testOpts, (err, peerId) => {
expect(err).to.not.exist()
it('set privKey (valid)', async () => {
const peerId = await PeerId.create(testOpts)
peerId.privKey = peerId._privKey
peerId.isValid(done)
})
expect(peerId.isValid()).to.equal(true)
})
it('set pubKey (valid)', (done) => {
PeerId.create(testOpts, (err, peerId) => {
expect(err).to.not.exist()
it('set pubKey (valid)', async () => {
const peerId = await PeerId.create(testOpts)
peerId.pubKey = peerId._pubKey
peerId.isValid(done)
})
expect(peerId.isValid()).to.equal(true)
})
it('set privKey (invalid)', (done) => {
PeerId.create(testOpts, (err, peerId) => {
expect(err).to.not.exist()
it('set privKey (invalid)', async () => {
const peerId = await PeerId.create(testOpts)
peerId.privKey = Buffer.from('bufff')
peerId.isValid((err) => {
expect(err).to.exist()
done()
})
})
expect(peerId.isValid()).to.equal(false)
})
it('set pubKey (invalid)', (done) => {
PeerId.create(testOpts, (err, peerId) => {
expect(err).to.not.exist()
peerId.pubKey = Buffer.from('buffff')
peerId.isValid((err) => {
expect(err).to.exist()
done()
})
})
it('set pubKey (invalid)', async () => {
const peerId = await PeerId.create(testOpts)
peerId.pubKey = Buffer.from('bufff')
expect(peerId.isValid()).to.equal(false)
})
describe('returns error via cb instead of crashing', () => {
const garbage = [Buffer.from('00010203040506070809', 'hex'), {}, null, false, undefined, true, 1, 0, Buffer.from(''), 'aGVsbG93b3JsZA==', 'helloworld', '']
const garbage = [
Buffer.from('00010203040506070809', 'hex'),
{}, null, false, undefined, true, 1, 0,
Buffer.from(''), 'aGVsbG93b3JsZA==', 'helloworld', ''
]
const fncs = ['createFromPubKey', 'createFromPrivKey', 'createFromJSON']
garbage.forEach(garbage => {
fncs.forEach(fnc => {
it(fnc + '(' + util.inspect(garbage) + ')', cb => {
PeerId[fnc](garbage, (err, res) => {
for (const gb of garbage) {
for (const fn of fncs) {
it(`${fn} (${util.inspect(gb)})`, async () => {
try {
await PeerId[fn](gb)
} catch (err) {
expect(err).to.exist()
expect(res).to.not.exist()
cb()
})
})
})
}
})
}
}
})
describe('throws on inconsistent data', () => {
@ -271,37 +200,30 @@ describe('PeerId', () => {
let k2
let k3
before((done) => {
parallel([
(cb) => crypto.keys.generateKeyPair('RSA', 512, cb),
(cb) => crypto.keys.generateKeyPair('RSA', 512, cb),
(cb) => crypto.keys.generateKeyPair('RSA', 512, cb)
], (err, keys) => {
expect(err).to.not.exist()
before(async () => {
const keys = await Promise.all([
crypto.keys.generateKeyPair('RSA', 512),
crypto.keys.generateKeyPair('RSA', 512),
crypto.keys.generateKeyPair('RSA', 512)
])
k1 = keys[0]
k2 = keys[1]
k3 = keys[2]
done()
})
})
it('missmatch private - public key', (done) => {
k1.public.hash((err, digest) => {
expect(err).to.not.exist()
expect(() => new PeerId(digest, k1, k2.public))
.to.throw(/inconsistent arguments/)
done()
})
it('missmatch private - public key', async () => {
const digest = await k1.public.hash()
expect(() => {
new PeerId(digest, k1, k2.public) // eslint-disable-line no-new
}).to.throw(/inconsistent arguments/)
})
it('missmatch id - private - public key', (done) => {
k1.public.hash((err, digest) => {
expect(err).to.not.exist()
expect(() => new PeerId(digest, k1, k3.public))
.to.throw(/inconsistent arguments/)
done()
})
it('missmatch id - private - public key', async () => {
const digest = await k1.public.hash()
expect(() => {
new PeerId(digest, k1, k3.public) // eslint-disable-line no-new
}).to.throw(/inconsistent arguments/)
})
it('invalid id', () => {