Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
fix: RPC msg.from is now binary
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Nov 19, 2017
1 parent 3edacfe commit ff77021
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
},
"dependencies": {
"async": "^2.6.0",
"bs58": "^4.0.1",
"debug": "^3.1.0",
"length-prefixed-stream": "^1.5.1",
"libp2p-crypto": "~0.10.3",
Expand Down
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class FloodSub extends EventEmitter {
peer.attachConnection(conn)

// Immediately send my own subscriptions to the newly established conn
console.log('subs', this.subscriptions)
peer.sendSubscriptions(this.subscriptions)
setImmediate(() => callback())
}
Expand Down Expand Up @@ -138,7 +137,7 @@ class FloodSub extends EventEmitter {
const msgs = rpc.msgs

if (msgs && msgs.length) {
this._processRpcMessages(rpc.msgs)
this._processRpcMessages(utils.normalizeInRpcMessages(rpc.msgs))
}

if (subs && subs.length) {
Expand Down Expand Up @@ -206,7 +205,7 @@ class FloodSub extends EventEmitter {
return
}

peer.sendMessages(messages)
peer.sendMessages(utils.normalizeOutRpcMessages(messages))

log('publish msgs on topics', topics, peer.info.id.toB58String())
})
Expand Down
2 changes: 1 addition & 1 deletion src/message/rpc.proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ message RPC {
}
message Message {
optional string from = 1;

This comment has been minimized.

Copy link
@daviddias

daviddias Nov 19, 2017

Member

@richardschneider you might have caught the same issue that we discovered with https://github.com/ipfs/js-ipfs-bitswap/blob/master/src/types/message/message.proto.js#L9 (will check this more tomorrow asap)

@whyrusleeping ^^

optional bytes from = 1;
optional bytes data = 2;
optional bytes seqno = 3;
repeated string topicCIDs = 4; // CID of topic descriptor object
Expand Down
27 changes: 27 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const crypto = require('libp2p-crypto')
const bs58 = require('bs58')

exports = module.exports

Expand Down Expand Up @@ -66,3 +67,29 @@ exports.ensureArray = (maybeArray) => {

return maybeArray
}

exports.normalizeInRpcMessages = (messages) => {
if (!messages) {
return messages;
}
return messages.map((msg) => {
const m = Object.assign({}, msg)
if (Buffer.isBuffer(msg.from)) {
m.from = bs58.encode(msg.from)
}
return m;
})
}

exports.normalizeOutRpcMessages = (messages) => {
if (!messages) {
return messages;
}
return messages.map((msg) => {
const m = Object.assign({}, msg)
if (typeof msg.from === 'string' || msg.from instanceof String) {
m.from = bs58.decode(msg.from)
}
return m;
})
}
28 changes: 28 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,32 @@ describe('utils', () => {
expect(utils.ensureArray('hello')).to.be.eql(['hello'])
expect(utils.ensureArray([1, 2])).to.be.eql([1, 2])
})

it('converts an IN msg.from to b58', () => {
let binaryId = Buffer.from('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'hex')
let stringId = 'QmdZEWgtaWAxBh93fELFT298La1rsZfhiC2pqwMVwy3jZM'
const m = [
{ from: binaryId },
{ from: stringId }
]
const expected = [
{ from: stringId },
{ from: stringId }
];
expect(utils.normalizeInRpcMessages(m)).to.deep.eql(expected)
})

it('converts an OUT msg.from to binary', () => {
let binaryId = Buffer.from('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'hex')
let stringId = 'QmdZEWgtaWAxBh93fELFT298La1rsZfhiC2pqwMVwy3jZM'
const m = [
{ from: binaryId },
{ from: stringId }
]
const expected = [
{ from: binaryId },
{ from: binaryId }
];
expect(utils.normalizeOutRpcMessages(m)).to.deep.eql(expected)
})
})

0 comments on commit ff77021

Please sign in to comment.