feat: isPeerId

This commit is contained in:
David Dias
2017-03-27 13:23:18 +01:00
parent 8c49610dff
commit 0acc572fd3
2 changed files with 16 additions and 0 deletions

View File

@ -219,6 +219,12 @@ exports.createFromJSON = function (obj, callback) {
} }
} }
exports.isPeerId = function (peerId) {
if (peerId.constructor && peerId.constructor.name) {
return peerId.constructor.name === 'PeerId'
}
}
function toB64Opt (val) { function toB64Opt (val) {
if (val) { if (val) {
return val.toString('base64') return val.toString('base64')

View File

@ -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) => { it('throws on changing the id', (done) => {
PeerId.create((err, id) => { PeerId.create((err, id) => {
expect(err).to.not.exist() expect(err).to.not.exist()