diff --git a/package.json b/package.json index bfdc489..f7c3ea4 100644 --- a/package.json +++ b/package.json @@ -34,15 +34,16 @@ }, "homepage": "https://github.com/libp2p/js-peer-id", "devDependencies": { - "aegir": "^11.0.0", + "aegir": "^11.0.1", "chai": "^3.5.0", "dirty-chai": "^1.2.2", "pre-commit": "^1.2.2" }, "dependencies": { - "libp2p-crypto": "~0.8.6", - "multihashes": "~0.4.4", - "async": "^2.1.5" + "async": "^2.2.0", + "libp2p-crypto": "~0.8.7", + "lodash": "^4.17.4", + "multihashes": "~0.4.5" }, "repository": { "type": "git", @@ -59,4 +60,4 @@ "nginnever ", "npmcdn-to-unpkg-bot " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 9cf6fb0..c1baba8 100644 --- a/src/index.js +++ b/src/index.js @@ -91,7 +91,6 @@ class PeerId { } exports = module.exports = PeerId -exports.Buffer = Buffer // generation exports.create = function (opts, callback) { @@ -219,6 +218,12 @@ exports.createFromJSON = function (obj, callback) { } } +exports.isPeerId = function (peerId) { + return Boolean(typeof peerId === 'object' && + peerId._id && + peerId._idB58String) +} + function toB64Opt (val) { if (val) { return val.toString('base64') diff --git a/test/index.spec.js b/test/index.spec.js index 782747c..5cbc952 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -32,6 +32,16 @@ describe('PeerId', () => { }) }) + it('isPeerId', (done) => { + PeerId.create((err, id) => { + expect(err).to.not.exist() + expect(PeerId.isPeerId(id)).to.equal(true) + expect(PeerId.isPeerId('aaa')).to.equal(false) + expect(PeerId.isPeerId(new Buffer('batatas'))).to.equal(false) + done() + }) + }) + it('throws on changing the id', (done) => { PeerId.create((err, id) => { expect(err).to.not.exist()