Skip to content

Commit

Permalink
fix(identify): convert all addresses to multiaddr
Browse files Browse the repository at this point in the history
Fixes libp2p#37
  • Loading branch information
dignifiedquire committed Apr 24, 2016
1 parent a008ebd commit 6943e3e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
14 changes: 6 additions & 8 deletions src/identify.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {

pbs.on('identify', (msg) => {
if (msg.observedAddr.length > 0) {
peerInfo.multiaddr.addSafe(msg.observedAddr)
peerInfo.multiaddr.addSafe(multiaddr(msg.observedAddr))
}

const peerId = Id.createFromPubKey(msg.publicKey)
Expand All @@ -59,7 +59,7 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {
protocolVersion: 'na',
agentVersion: 'na',
publicKey: peerInfo.id.pubKey,
listenAddrs: peerInfo.multiaddrs.map((mh) => { return mh.buffer }),
listenAddrs: peerInfo.multiaddrs.map((mh) => mh.buffer),
observedAddr: obsMultiaddr ? obsMultiaddr.buffer : new Buffer('')
})

Expand All @@ -70,15 +70,15 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {
}

exports.handler = (peerInfo, swarm) => {
return function (conn) {
return (conn) => {
// 1. receive incoming observed info about me
// 2. update my own information (on peerInfo)
// 3. send back what I see from the other (get from swarm.muxedConns[incPeerID].conn.getObservedAddrs()
var pbs = pbStream()

pbs.on('identify', function (msg) {
pbs.on('identify', (msg) => {
if (msg.observedAddr.length > 0) {
peerInfo.multiaddr.addSafe(msg.observedAddr)
peerInfo.multiaddr.addSafe(multiaddr(msg.observedAddr))
}

const peerId = Id.createFromPubKey(msg.publicKey)
Expand All @@ -89,9 +89,7 @@ exports.handler = (peerInfo, swarm) => {
protocolVersion: 'na',
agentVersion: 'na',
publicKey: peerInfo.id.pubKey,
listenAddrs: peerInfo.multiaddrs.map(function (ma) {
return ma.buffer
}),
listenAddrs: peerInfo.multiaddrs.map((ma) => ma.buffer),
observedAddr: obsMultiaddr ? obsMultiaddr.buffer : new Buffer('')
})
pbs.finalize()
Expand Down
18 changes: 15 additions & 3 deletions test/01-transport-tcp.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,17 @@ describe('transport - tcp', function () {
function ready () {
if (++count === 2) {
expect(peerA.multiaddrs.length).to.equal(1)
expect(peerA.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/127.0.0.1/tcp/9888'))
expect(
peerA.multiaddrs[0].equals(multiaddr('/ip4/127.0.0.1/tcp/9888'))
).to.be.equal(
true
)
expect(peerB.multiaddrs.length).to.equal(1)
expect(peerB.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/127.0.0.1/tcp/9999'))
expect(
peerB.multiaddrs[0].equals(multiaddr('/ip4/127.0.0.1/tcp/9999'))
).to.be.equal(
true
)
done()
}
}
Expand Down Expand Up @@ -124,7 +132,11 @@ describe('transport - tcp', function () {

function ready () {
expect(peer.multiaddrs.length).to.equal(1)
expect(peer.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/0.0.0.0/tcp/9050'))
expect(
peer.multiaddrs[0].equals(multiaddr('/ip4/0.0.0.0/tcp/9050'))
).to.be.equal(
true
)
swarm.close(done)
}
})
Expand Down
12 changes: 10 additions & 2 deletions test/03-transport-websockets.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,17 @@ describe('transport - websockets', function () {
function ready () {
if (++count === 2) {
expect(peerA.multiaddrs.length).to.equal(1)
expect(peerA.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/127.0.0.1/tcp/9888/websockets'))
expect(
peerA.multiaddrs[0].equals(multiaddr('/ip4/127.0.0.1/tcp/9888/websockets'))
).to.be.equal(
true
)
expect(peerB.multiaddrs.length).to.equal(1)
expect(peerB.multiaddrs[0]).to.deep.equal(multiaddr('/ip4/127.0.0.1/tcp/9999/websockets'))
expect(
peerB.multiaddrs[0].equals(multiaddr('/ip4/127.0.0.1/tcp/9999/websockets'))
).to.equal(
true
)
done()
}
}
Expand Down

0 comments on commit 6943e3e

Please sign in to comment.