Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 12 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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))
})
}

Expand Down Expand Up @@ -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))
})
}

Expand Down Expand Up @@ -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))
}
}

Expand Down