Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multiaddr support Peer ID represented as CID #102

Merged
merged 1 commit into from
Oct 28, 2019
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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