Skip to content

Commit

Permalink
chore: refactor message normalization (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Jul 14, 2020
1 parent e2ac419 commit dbc3d8c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
12 changes: 11 additions & 1 deletion src/index.js
Expand Up @@ -15,6 +15,16 @@ const {
verifySignature
} = require('./message/sign')

/**
* @typedef {Object} InMessage
* @property {string} from
* @property {string} receivedFrom
* @property {string[]} topicIDs
* @property {Buffer} data
* @property {Buffer} [signature]
* @property {Buffer} [key]
*/

function validateRegistrar (registrar) {
// registrar handling
if (typeof registrar !== 'object') {
Expand Down Expand Up @@ -257,7 +267,7 @@ class PubsubBaseProtocol extends EventEmitter {

/**
* Validates the given message. The signature will be checked for authenticity.
* @param {rpc.RPC.Message} message
* @param {InMessage} message
* @returns {Promise<Boolean>}
*/
async validate (message) { // eslint-disable-line require-await
Expand Down
9 changes: 7 additions & 2 deletions src/message/sign.js
Expand Up @@ -53,7 +53,7 @@ async function verifySignature (message) {
* Returns the PublicKey associated with the given message.
* If no, valid PublicKey can be retrieved an error will be returned.
*
* @param {Message} message
* @param {InMessage} message
* @returns {Promise<PublicKey>}
*/
async function messagePublicKey (message) {
Expand All @@ -66,7 +66,12 @@ async function messagePublicKey (message) {
throw new Error('Public Key does not match the originator')
} else {
// should be available in the from property of the message (peer id)
const from = PeerId.createFromBytes(message.from)
let from
if (typeof message.from === 'string') {
from = PeerId.createFromB58String(message.from)
} else {
from = PeerId.createFromBytes(message.from)
}

if (from.pubKey) {
return from.pubKey
Expand Down
25 changes: 5 additions & 20 deletions src/utils.js
Expand Up @@ -73,26 +73,18 @@ exports.ensureArray = (maybeArray) => {
* Ensures `message.from` is base58 encoded
* @param {Object} message
* @param {Buffer|String} message.from
* @param {PeerId} peerId
* @return {Object}
*/
exports.normalizeInRpcMessage = (message) => {
exports.normalizeInRpcMessage = (message, peerId) => {
const m = Object.assign({}, message)
if (Buffer.isBuffer(message.from)) {
m.from = multibase.encode('base58btc', message.from).toString().slice(1)
}
return m
}

/**
* The same as `normalizeInRpcMessage`, but performed on an array of messages
* @param {Object[]} messages
* @return {Object[]}
*/
exports.normalizeInRpcMessages = (messages) => {
if (!messages) {
return messages
if (peerId) {
m.receivedFrom = peerId.toB58String()
}
return messages.map(exports.normalizeInRpcMessage)
return m
}

exports.normalizeOutRpcMessage = (message) => {
Expand All @@ -102,10 +94,3 @@ exports.normalizeOutRpcMessage = (message) => {
}
return m
}

exports.normalizeOutRpcMessages = (messages) => {
if (!messages) {
return messages
}
return messages.map(exports.normalizeOutRpcMessage)
}
8 changes: 6 additions & 2 deletions test/utils.spec.js
Expand Up @@ -59,7 +59,9 @@ describe('utils', () => {
{ from: stringId },
{ from: stringId }
]
expect(utils.normalizeInRpcMessages(m)).to.deep.eql(expected)
for (let i = 0; i < m.length; i++) {
expect(utils.normalizeInRpcMessage(m[i])).to.deep.eql(expected[i])
}
})

it('converts an OUT msg.from to binary', () => {
Expand All @@ -73,6 +75,8 @@ describe('utils', () => {
{ from: binaryId },
{ from: binaryId }
]
expect(utils.normalizeOutRpcMessages(m)).to.deep.eql(expected)
for (let i = 0; i < m.length; i++) {
expect(utils.normalizeOutRpcMessage(m[i])).to.deep.eql(expected[i])
}
})
})

0 comments on commit dbc3d8c

Please sign in to comment.