diff --git a/README.md b/README.md index 9313222..9f847eb 100644 --- a/README.md +++ b/README.md @@ -201,7 +201,9 @@ Returns an `obj` of the form ### `toPrint()` -Alias for `.toJSON()`. +Returns the Peer ID as a printable string. + +Example: `` ### `isEqual(id)` diff --git a/src/index.js b/src/index.js index 87145c6..a9e7010 100644 --- a/src/index.js +++ b/src/index.js @@ -67,9 +67,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 '' } // return the jsonified version of the key, matching the formatting diff --git a/test/peer-id.spec.js b/test/peer-id.spec.js index 57fdcd7..5d04c92 100644 --- a/test/peer-id.spec.js +++ b/test/peer-id.spec.js @@ -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('') done() }) })