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

[WIP] feat: adding p2p-circuit proto #41

Merged
merged 4 commits into from
Mar 27, 2017
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@
"greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>",
"npm-to-cdn-bot (by Forbes Lindesay) <npmcdn-to-unpkg-bot@users.noreply.github.com>"
]
}
}
35 changes: 31 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const extend = require('xtend')
const codec = require('./codec')
const protocols = require('./protocols-table')
const varint = require('varint')
const bs58 = require('bs58')

const NotImplemented = new Error('Sorry, Not Implemented Yet.')

Expand Down Expand Up @@ -83,8 +84,8 @@ Multiaddr.prototype.toOptions = function toOptions () {
*/
Multiaddr.prototype.inspect = function inspect () {
return '<Multiaddr ' +
this.buffer.toString('hex') + ' - ' +
codec.bufferToString(this.buffer) + '>'
this.buffer.toString('hex') + ' - ' +
codec.bufferToString(this.buffer) + '>'
}

/**
Expand All @@ -105,7 +106,7 @@ Multiaddr.prototype.inspect = function inspect () {
Multiaddr.prototype.protos = function protos () {
return map(this.protoCodes(), function (code) {
return extend(protocols(code))
// copy to prevent users from modifying the internal objs.
// copy to prevent users from modifying the internal objs.
})
}

Expand Down Expand Up @@ -231,6 +232,33 @@ Multiaddr.prototype.decapsulate = function decapsulate (addr) {
return Multiaddr(s.slice(0, i))
}

/**
* Extract the peerId if the multiaddr contains one
*
* @return {String} peerId - The id of the peer
* @example
* const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string')
* // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string>
*
* const peerId
*
* try {
* peerId = mh1.getPeerId()
* } catch (err) {
* // not a valid base58 address
* }
*/
Multiaddr.prototype.getPeerId = function getPeerId () {
let b58str = this.stringTuples().filter((tuple) => {
if (tuple[0] === protocols.names['ipfs'].code) {
return true
}
})[0][1]

bs58.decode(b58str)
return b58str
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/**
* Checks if two Multiaddrs are the same
*
Expand Down Expand Up @@ -428,4 +456,3 @@ Multiaddr.resolve = function resolve (addr, callback) {
*/
return callback(new Error('not implemented yet'))
}

3 changes: 2 additions & 1 deletion src/protocols-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ Protocols.table = [
[477, 0, 'ws'],
[478, 0, 'wss'],
[275, 0, 'libp2p-webrtc-star'],
[276, 0, 'libp2p-webrtc-direct']
[276, 0, 'libp2p-webrtc-direct'],
[290, 0, 'p2p-circuit']
]

Protocols.names = {}
Expand Down
14 changes: 14 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,20 @@ describe('variants', () => {
expect(addr.toString()).to.equal(str)
})

it('p2p-circuit', () => {
const str = '/p2p-circuit/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC'
const addr = multiaddr(str)
expect(addr).to.have.property('buffer')
expect(addr.toString()).to.equal(str)
})

it('p2p-circuit ipfs', () => {
const str = '/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/p2p-circuit'
const addr = multiaddr(str)
expect(addr).to.have.property('buffer')
expect(addr.toString()).to.equal(str)
})

it('webrtc-star', () => {
const str = '/libp2p-webrtc-star/ip4/127.0.0.1/tcp/9090/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC'
const addr = multiaddr(str)
Expand Down