Merge pull request #84 from libp2p/feat/add-class-is-module

feat: add class-is module
This commit is contained in:
Pedro Teixeira 2018-10-18 10:50:45 +01:00 committed by GitHub
commit d7e633a90b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 9 deletions

View File

@ -39,6 +39,7 @@
},
"dependencies": {
"async": "^2.6.1",
"class-is": "^1.1.0",
"libp2p-crypto": "~0.13.0",
"lodash": "^4.17.10",
"multihashes": "~0.4.13"

View File

@ -8,6 +8,7 @@ const mh = require('multihashes')
const crypto = require('libp2p-crypto')
const assert = require('assert')
const waterfall = require('async/waterfall')
const withIs = require('class-is')
class PeerId {
constructor (id, privKey, pubKey) {
@ -132,7 +133,9 @@ class PeerId {
}
}
exports = module.exports = PeerId
const PeerIdWithIs = withIs(PeerId, { className: 'PeerId', symbolName: '@libp2p/js-peer-id/PeerId' })
exports = module.exports = PeerIdWithIs
// generation
exports.create = function (opts, callback) {
@ -153,20 +156,20 @@ exports.create = function (opts, callback) {
return callback(err)
}
callback(null, new PeerId(digest, privKey))
callback(null, new PeerIdWithIs(digest, privKey))
})
}
exports.createFromHexString = function (str) {
return new PeerId(mh.fromHexString(str))
return new PeerIdWithIs(mh.fromHexString(str))
}
exports.createFromBytes = function (buf) {
return new PeerId(buf)
return new PeerIdWithIs(buf)
}
exports.createFromB58String = function (str) {
return new PeerId(mh.fromB58String(str))
return new PeerIdWithIs(mh.fromB58String(str))
}
// Public Key input will be a buffer
@ -195,7 +198,7 @@ exports.createFromPubKey = function (key, callback) {
return callback(err)
}
callback(null, new PeerId(digest, null, pubKey))
callback(null, new PeerIdWithIs(digest, null, pubKey))
})
}
@ -227,7 +230,7 @@ exports.createFromPrivKey = function (key, callback) {
return callback(err)
}
callback(null, new PeerId(digest, privKey, privKey.public))
callback(null, new PeerIdWithIs(digest, privKey, privKey.public))
})
}
@ -278,10 +281,10 @@ exports.createFromJSON = function (obj, callback) {
return callback(new Error('Id and private key do not match'))
}
callback(null, new PeerId(id, priv, pub))
callback(null, new PeerIdWithIs(id, priv, pub))
})
} else {
callback(null, new PeerId(id, null, pub))
callback(null, new PeerIdWithIs(id, null, pub))
}
}