Skip to content

Commit

Permalink
feat: support Peer ID represented as CID
Browse files Browse the repository at this point in the history
This change adds support for Peer ID represented as CID.
Implements `Stage 1` from libp2p/specs#216

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
lidel committed Oct 25, 2019
1 parent 43cb934 commit dd09e00
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -31,11 +31,12 @@
"homepage": "https://github.com/multiformats/js-multiaddr",
"dependencies": {
"bs58": "^4.0.1",
"cids": "~0.7.1",
"class-is": "^1.1.0",
"hi-base32": "~0.5.0",
"ip": "^1.1.5",
"is-ip": "^3.1.0",
"varint": "^5.0.0",
"hi-base32": "~0.5.0"
"varint": "^5.0.0"
},
"devDependencies": {
"aegir": "^20.0.0",
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Expand Up @@ -4,6 +4,7 @@ const codec = require('./codec')
const protocols = require('./protocols-table')
const varint = require('varint')
const bs58 = require('bs58')
const CID = require('cids')
const withIs = require('class-is')

/**
Expand Down Expand Up @@ -287,8 +288,8 @@ Multiaddr.prototype.getPeerId = function getPeerId () {

// Get the last id
b58str = tuples.pop()[1]

bs58.decode(b58str)
// Get multihash, unwrap from CID if needed
b58str = bs58.encode(new CID(b58str).multihash)
} catch (e) {
b58str = null
}
Expand Down
5 changes: 5 additions & 0 deletions test/index.spec.js
Expand Up @@ -776,6 +776,11 @@ describe('helpers', () => {
multiaddr('/p2p-circuit/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC').getPeerId()
).to.equal('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
})
it('parses extracts the peer Id from a multiaddr, p2p and CIDv1 Base32', () => {
expect(
multiaddr('/p2p-circuit/p2p/bafzbeigweq4zr4x4ky2dvv7nanbkw6egutvrrvzw6g3h2rftp7gidyhtt4').getPeerId()
).to.equal('QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi')
})
})

describe('.getPeerId should return null on missing peer id in multiaddr', () => {
Expand Down

0 comments on commit dd09e00

Please sign in to comment.