Compare commits

...

13 Commits

4 changed files with 64 additions and 50 deletions

View File

@ -58,7 +58,7 @@ var PeerId = require('peer-id')
var bs58 = require('bs58') var bs58 = require('bs58')
PeerId.create({ bits: 1024 }, (err, id) => { PeerId.create({ bits: 1024 }, (err, id) => {
console.log(JSON.stringify(id.toJSON(), null, 2) console.log(JSON.stringify(id.toJSON(), null, 2))
}) })
``` ```

View File

@ -1,6 +1,6 @@
{ {
"name": "peer-id", "name": "peer-id",
"version": "0.8.0", "version": "0.8.3",
"description": "IPFS Peer Id implementation in Node.js", "description": "IPFS Peer Id implementation in Node.js",
"main": "src/index.js", "main": "src/index.js",
"bin": "src/bin.js", "bin": "src/bin.js",
@ -26,21 +26,23 @@
"test" "test"
], ],
"engines": { "engines": {
"node": ">=4.0.0" "node": ">=4.0.0",
"npm": ">=3.0.0"
}, },
"bugs": { "bugs": {
"url": "https://github.com/libp2p/js-peer-id/issues" "url": "https://github.com/libp2p/js-peer-id/issues"
}, },
"homepage": "https://github.com/libp2p/js-peer-id", "homepage": "https://github.com/libp2p/js-peer-id",
"devDependencies": { "devDependencies": {
"aegir": "^9.1.2", "aegir": "^11.0.0",
"chai": "^3.5.0", "chai": "^3.5.0",
"pre-commit": "^1.1.3" "dirty-chai": "^1.2.2",
"pre-commit": "^1.2.2"
}, },
"dependencies": { "dependencies": {
"async": "^2.0.1", "libp2p-crypto": "~0.8.6",
"libp2p-crypto": "^0.7.0", "multihashes": "~0.4.4",
"multihashes": "^0.3.0" "async": "^2.1.5"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -50,9 +52,10 @@
"David Dias <daviddias.p@gmail.com>", "David Dias <daviddias.p@gmail.com>",
"David Dias <mail@daviddias.me>", "David Dias <mail@daviddias.me>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>", "Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Greenkeeper <support@greenkeeper.io>",
"Prashanth Chandra <coolshanth94@gmail.com>",
"Richard Littauer <richard.littauer@gmail.com>", "Richard Littauer <richard.littauer@gmail.com>",
"Stephen Whitmore <stephen.whitmore@gmail.com>", "Stephen Whitmore <stephen.whitmore@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>",
"nginnever <ginneversource@gmail.com>", "nginnever <ginneversource@gmail.com>",
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>" "npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>"
] ]

View File

@ -17,11 +17,20 @@ class PeerId {
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments') assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments')
} }
this.id = id this._id = id
this._idB58String = mh.toB58String(this.id)
this._privKey = privKey this._privKey = privKey
this._pubKey = pubKey this._pubKey = pubKey
} }
get id () {
return this._id
}
set id (val) {
throw new Error('Id is immutable')
}
get privKey () { get privKey () {
return this._privKey return this._privKey
} }
@ -61,7 +70,7 @@ class PeerId {
// of go-ipfs for its config file // of go-ipfs for its config file
toJSON () { toJSON () {
return { return {
id: mh.toB58String(this.id), id: this.toB58String(),
privKey: toB64Opt(this.marshalPrivKey()), privKey: toB64Opt(this.marshalPrivKey()),
pubKey: toB64Opt(this.marshalPubKey()) pubKey: toB64Opt(this.marshalPubKey())
} }
@ -77,7 +86,7 @@ class PeerId {
} }
toB58String () { toB58String () {
return mh.toB58String(this.id) return this._idB58String
} }
} }

View File

@ -2,7 +2,10 @@
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'
const expect = require('chai').expect const chai = require('chai')
const dirtyChai = require('dirty-chai')
chai.use(dirtyChai)
const expect = chai.expect
const crypto = require('libp2p-crypto') const crypto = require('libp2p-crypto')
const mh = require('multihashes') const mh = require('multihashes')
const parallel = require('async/parallel') const parallel = require('async/parallel')
@ -23,12 +26,23 @@ describe('PeerId', () => {
it('create a new id', (done) => { it('create a new id', (done) => {
PeerId.create((err, id) => { PeerId.create((err, id) => {
expect(err).to.not.exist expect(err).to.not.exist()
expect(id.toB58String().length).to.equal(46) expect(id.toB58String().length).to.equal(46)
done() done()
}) })
}) })
it('throws on changing the id', (done) => {
PeerId.create((err, id) => {
expect(err).to.not.exist()
expect(id.toB58String().length).to.equal(46)
expect(() => {
id.id = new Buffer('hello')
}).to.throw(/immutable/)
done()
})
})
it('recreate an Id from Hex string', () => { it('recreate an Id from Hex string', () => {
const id = PeerId.createFromHexString(testIdHex) const id = PeerId.createFromHexString(testIdHex)
expect(testIdBytes).to.deep.equal(id.id) expect(testIdBytes).to.deep.equal(id.id)
@ -46,7 +60,7 @@ describe('PeerId', () => {
it('Recreate from a Public Key', (done) => { it('Recreate from a Public Key', (done) => {
PeerId.createFromPubKey(testId.pubKey, (err, id) => { PeerId.createFromPubKey(testId.pubKey, (err, id) => {
expect(err).to.not.exist expect(err).to.not.exist()
expect(testIdB58String).to.equal(id.toB58String()) expect(testIdB58String).to.equal(id.toB58String())
done() done()
}) })
@ -54,12 +68,12 @@ describe('PeerId', () => {
it('Recreate from a Private Key', (done) => { it('Recreate from a Private Key', (done) => {
PeerId.createFromPrivKey(testId.privKey, (err, id) => { PeerId.createFromPrivKey(testId.privKey, (err, id) => {
expect(err).to.not.exist expect(err).to.not.exist()
expect(testIdB58String).to.equal(id.toB58String()) expect(testIdB58String).to.equal(id.toB58String())
const encoded = new Buffer(testId.privKey, 'base64') const encoded = new Buffer(testId.privKey, 'base64')
PeerId.createFromPrivKey(encoded, (err, id2) => { PeerId.createFromPrivKey(encoded, (err, id2) => {
expect(err).to.not.exist expect(err).to.not.exist()
expect(testIdB58String).to.equal(id2.toB58String()) expect(testIdB58String).to.equal(id2.toB58String())
done() done()
}) })
@ -68,10 +82,10 @@ describe('PeerId', () => {
it('Compare generated ID with one created from PubKey', (done) => { it('Compare generated ID with one created from PubKey', (done) => {
PeerId.create((err, id1) => { PeerId.create((err, id1) => {
expect(err).to.not.exist expect(err).to.not.exist()
PeerId.createFromPubKey(id1.marshalPubKey(), (err, id2) => { PeerId.createFromPubKey(id1.marshalPubKey(), (err, id2) => {
expect(err).to.not.exist expect(err).to.not.exist()
expect(id1.id).to.be.eql(id2.id) expect(id1.id).to.be.eql(id2.id)
done() done()
}) })
@ -80,9 +94,9 @@ describe('PeerId', () => {
it('Non-default # of bits', (done) => { it('Non-default # of bits', (done) => {
PeerId.create({ bits: 1024 }, (err, shortId) => { PeerId.create({ bits: 1024 }, (err, shortId) => {
expect(err).to.not.exist expect(err).to.not.exist()
PeerId.create({ bits: 4096 }, (err, longId) => { PeerId.create({ bits: 4096 }, (err, longId) => {
expect(err).to.not.exist expect(err).to.not.exist()
expect(shortId.privKey.bytes.length).is.below(longId.privKey.bytes.length) expect(shortId.privKey.bytes.length).is.below(longId.privKey.bytes.length)
done() done()
}) })
@ -91,9 +105,9 @@ describe('PeerId', () => {
it('Pretty printing', (done) => { it('Pretty printing', (done) => {
PeerId.create((err, id1) => { PeerId.create((err, id1) => {
expect(err).to.not.exist expect(err).to.not.exist()
PeerId.createFromPrivKey(id1.toPrint().privKey, (err, id2) => { PeerId.createFromPrivKey(id1.toPrint().privKey, (err, id2) => {
expect(err).to.not.exist expect(err).to.not.exist()
expect(id1.toPrint()).to.be.eql(id2.toPrint()) expect(id1.toPrint()).to.be.eql(id2.toPrint())
done() done()
}) })
@ -108,25 +122,13 @@ describe('PeerId', () => {
describe('fromJSON', () => { describe('fromJSON', () => {
it('full node', (done) => { it('full node', (done) => {
PeerId.create({ bits: 1024 }, (err, id) => { PeerId.create({ bits: 1024 }, (err, id) => {
expect(err).to.not.exist expect(err).to.not.exist()
PeerId.createFromJSON(id.toJSON(), (err, other) => { PeerId.createFromJSON(id.toJSON(), (err, other) => {
expect(err).to.not.exist expect(err).to.not.exist()
expect( expect(id.toB58String()).to.equal(other.toB58String())
id.toB58String() expect(id.privKey.bytes).to.eql(other.privKey.bytes)
).to.equal( expect(id.pubKey.bytes).to.eql(other.pubKey.bytes)
other.toB58String()
)
expect(
id.privKey.bytes
).to.deep.equal(
other.privKey.bytes
)
expect(
id.pubKey.bytes
).to.deep.equal(
other.pubKey.bytes
)
done() done()
}) })
}) })
@ -134,16 +136,16 @@ describe('PeerId', () => {
it('only id', (done) => { it('only id', (done) => {
crypto.generateKeyPair('RSA', 1024, (err, key) => { crypto.generateKeyPair('RSA', 1024, (err, key) => {
expect(err).to.not.exist expect(err).to.not.exist()
key.public.hash((err, digest) => { key.public.hash((err, digest) => {
expect(err).to.not.exist expect(err).to.not.exist()
const id = PeerId.createFromBytes(digest) const id = PeerId.createFromBytes(digest)
expect(id.privKey).to.not.exist expect(id.privKey).to.not.exist()
expect(id.pubKey).to.not.exist expect(id.pubKey).to.not.exist()
PeerId.createFromJSON(id.toJSON(), (err, other) => { PeerId.createFromJSON(id.toJSON(), (err, other) => {
expect(err).to.not.exist expect(err).to.not.exist()
expect( expect(
id.toB58String() id.toB58String()
).to.equal( ).to.equal(
@ -157,9 +159,9 @@ describe('PeerId', () => {
it('go interop', (done) => { it('go interop', (done) => {
PeerId.createFromJSON(goId, (err, id) => { PeerId.createFromJSON(goId, (err, id) => {
expect(err).to.not.exist expect(err).to.not.exist()
id.privKey.public.hash((err, digest) => { id.privKey.public.hash((err, digest) => {
expect(err).to.not.exist expect(err).to.not.exist()
expect( expect(
mh.toB58String(digest) mh.toB58String(digest)
).to.be.eql( ).to.be.eql(
@ -192,7 +194,7 @@ describe('PeerId', () => {
it('missmatch private - public key', (done) => { it('missmatch private - public key', (done) => {
k1.public.hash((err, digest) => { k1.public.hash((err, digest) => {
expect(err).to.not.exist expect(err).to.not.exist()
expect( expect(
() => new PeerId(digest, k1, k2.public) () => new PeerId(digest, k1, k2.public)
).to.throw( ).to.throw(
@ -204,7 +206,7 @@ describe('PeerId', () => {
it('missmatch id - private - public key', (done) => { it('missmatch id - private - public key', (done) => {
k1.public.hash((err, digest) => { k1.public.hash((err, digest) => {
expect(err).to.not.exist expect(err).to.not.exist()
expect( expect(
() => new PeerId(digest, k1, k3.public) () => new PeerId(digest, k1, k3.public)
).to.throw( ).to.throw(