Compare commits

...

15 Commits

5 changed files with 75 additions and 25 deletions

View File

@ -1,3 +1,28 @@
<a name="0.12.0"></a>
# [0.12.0](https://github.com/libp2p/js-peer-id/compare/v0.11.0...v0.12.0) (2018-10-18)
### Bug Fixes
* add peerIdWithIs to the API functions using the instance ([2e5e666](https://github.com/libp2p/js-peer-id/commit/2e5e666))
### Features
* add class-is module ([6513a02](https://github.com/libp2p/js-peer-id/commit/6513a02))
<a name="0.11.0"></a>
# [0.11.0](https://github.com/libp2p/js-peer-id/compare/v0.10.7...v0.11.0) (2018-07-02)
### Features
* change toPrint output to match go implementation ([e8ab1b9](https://github.com/libp2p/js-peer-id/commit/e8ab1b9))
<a name="0.10.7"></a>
## [0.10.7](https://github.com/libp2p/js-peer-id/compare/v0.10.6...v0.10.7) (2018-04-05)

View File

@ -11,6 +11,12 @@
> [IPFS](https://github.com/ipfs/ipfs) Peer ID implementation in JavaScript.
## Lead Maintainer
[Pedro Teixeira](https://github.com/pgte)
## Table of Contents
- [Description](#description)
- [Example](#example)
- [Installation](#installation)
@ -138,6 +144,7 @@ Creates a Peer ID from hex string representing the key's multihash.
Creates a Peer ID from a buffer representing the key's multihash.
### `createFromB58String(str)`
Creates a Peer ID from a Base58 string representing the key's multihash.
### `createFromPubKey(pubKey)`
@ -155,8 +162,8 @@ Creates a Peer ID from a buffer containing a private key.
### `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 'base 64'
- `obj.pubKey: String` - The public key in protobuf format, encoded in `base64`
- `obj.privKey: String` - The private key in protobuf format, encoded in `base64`
## Export
@ -192,10 +199,11 @@ Returns an `obj` of the form
- `obj.pubKey: String` - The public key in protobuf format, encoded in 'base64'
- `obj.privKey: String` - The private key in protobuf format, encoded in 'base 64'
### `toPrint()`
Alias for `.toJSON()`.
Returns the Peer ID as a printable string without the `Qm` prefix.
Example: `<peer.ID xxxxxx>`
### `isEqual(id)`

View File

@ -1,7 +1,8 @@
{
"name": "peer-id",
"version": "0.10.7",
"version": "0.12.0",
"description": "IPFS Peer Id implementation in Node.js",
"leadMaintainer": "Pedro Teixeira <i@pgte.me>",
"main": "src/index.js",
"bin": "src/bin.js",
"scripts": {
@ -18,9 +19,8 @@
"keywords": [
"IPFS"
],
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"pre-commit": [
"pre-push": [
"lint",
"test"
],
@ -33,15 +33,15 @@
},
"homepage": "https://github.com/libp2p/js-peer-id",
"devDependencies": {
"aegir": "^13.0.6",
"aegir": "^14.0.0",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"pre-commit": "^1.2.2"
"dirty-chai": "^2.0.1"
},
"dependencies": {
"async": "^2.6.0",
"libp2p-crypto": "~0.12.1",
"lodash": "^4.17.5",
"async": "^2.6.1",
"class-is": "^1.1.0",
"libp2p-crypto": "~0.13.0",
"lodash": "^4.17.10",
"multihashes": "~0.4.13"
},
"repository": {
@ -53,10 +53,13 @@
"David Dias <mail@daviddias.me>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Maciej Krüger <mkg20001@gmail.com>",
"Michael Garvin <gar+gh@danger.computer>",
"Pedro Teixeira <i@pgte.me>",
"Prashanth Chandra <coolshanth94@gmail.com>",
"Richard Littauer <richard.littauer@gmail.com>",
"Richard Schneider <makaretu@gmail.com>",
"Stephen Whitmore <stephen.whitmore@gmail.com>",
"Vasco Santos <vasco.santos@moxy.studio>",
"Yahya <ya7yaz@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>",
"nginnever <ginneversource@gmail.com>",

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) {
@ -67,9 +68,19 @@ class PeerId {
}
}
// pretty print
toPrint () {
return this.toJSON()
let pid = this.toB58String()
// All sha256 nodes start with Qm
// We can skip the Qm to make the peer.ID more useful
if (pid.startsWith('Qm')) {
pid = pid.slice(2)
}
let maxRunes = 6
if (pid.length < maxRunes) {
maxRunes = pid.length
}
return '<peer.ID ' + pid.substr(0, maxRunes) + '>'
}
// return the jsonified version of the key, matching the formatting
@ -122,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) {
@ -143,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
@ -185,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))
})
}
@ -217,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))
})
}
@ -268,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))
}
}

View File

@ -136,9 +136,10 @@ describe('PeerId', () => {
it('Pretty printing', (done) => {
PeerId.create(testOpts, (err, id1) => {
expect(err).to.not.exist()
PeerId.createFromPrivKey(id1.toPrint().privKey, (err, id2) => {
PeerId.createFromPrivKey(id1.toJSON().privKey, (err, id2) => {
expect(err).to.not.exist()
expect(id1.toPrint()).to.be.eql(id2.toPrint())
expect(id1.toPrint()).to.equal('<peer.ID ' + id1.toB58String().substr(2, 6) + '>')
done()
})
})