From b7706ad08d1ea4bceb2d5a065185bb82b84f0aef Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 14 Jul 2020 12:25:05 +0200 Subject: [PATCH] Revert "chore: refactor message normalization (#59)" This reverts commit dbc3d8cf12dc42d6a91b14270731f1954fa54f2c. --- src/index.js | 12 +----------- src/message/sign.js | 9 ++------- src/utils.js | 25 ++++++++++++++++++++----- test/utils.spec.js | 8 ++------ 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/index.js b/src/index.js index 5289413615..96741cb2b7 100644 --- a/src/index.js +++ b/src/index.js @@ -15,16 +15,6 @@ 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') { @@ -267,7 +257,7 @@ class PubsubBaseProtocol extends EventEmitter { /** * Validates the given message. The signature will be checked for authenticity. - * @param {InMessage} message + * @param {rpc.RPC.Message} message * @returns {Promise} */ async validate (message) { // eslint-disable-line require-await diff --git a/src/message/sign.js b/src/message/sign.js index 89e124f7d5..bfa546db24 100644 --- a/src/message/sign.js +++ b/src/message/sign.js @@ -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 {InMessage} message + * @param {Message} message * @returns {Promise} */ async function messagePublicKey (message) { @@ -66,12 +66,7 @@ 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) - let from - if (typeof message.from === 'string') { - from = PeerId.createFromB58String(message.from) - } else { - from = PeerId.createFromBytes(message.from) - } + const from = PeerId.createFromBytes(message.from) if (from.pubKey) { return from.pubKey diff --git a/src/utils.js b/src/utils.js index 513aec0d39..f20efb9102 100644 --- a/src/utils.js +++ b/src/utils.js @@ -73,20 +73,28 @@ 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, peerId) => { +exports.normalizeInRpcMessage = (message) => { const m = Object.assign({}, message) if (Buffer.isBuffer(message.from)) { m.from = multibase.encode('base58btc', message.from).toString().slice(1) } - if (peerId) { - m.receivedFrom = peerId.toB58String() - } 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 + } + return messages.map(exports.normalizeInRpcMessage) +} + exports.normalizeOutRpcMessage = (message) => { const m = Object.assign({}, message) if (typeof message.from === 'string' || message.from instanceof String) { @@ -94,3 +102,10 @@ exports.normalizeOutRpcMessage = (message) => { } return m } + +exports.normalizeOutRpcMessages = (messages) => { + if (!messages) { + return messages + } + return messages.map(exports.normalizeOutRpcMessage) +} diff --git a/test/utils.spec.js b/test/utils.spec.js index fafb907dc1..17ee6116db 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -59,9 +59,7 @@ describe('utils', () => { { from: stringId }, { from: stringId } ] - for (let i = 0; i < m.length; i++) { - expect(utils.normalizeInRpcMessage(m[i])).to.deep.eql(expected[i]) - } + expect(utils.normalizeInRpcMessages(m)).to.deep.eql(expected) }) it('converts an OUT msg.from to binary', () => { @@ -75,8 +73,6 @@ describe('utils', () => { { from: binaryId }, { from: binaryId } ] - for (let i = 0; i < m.length; i++) { - expect(utils.normalizeOutRpcMessage(m[i])).to.deep.eql(expected[i]) - } + expect(utils.normalizeOutRpcMessages(m)).to.deep.eql(expected) }) })