From dbc3d8cf12dc42d6a91b14270731f1954fa54f2c Mon Sep 17 00:00:00 2001 From: Cayman Date: Tue, 14 Jul 2020 03:31:40 -0500 Subject: [PATCH] chore: refactor message normalization (#59) --- src/index.js | 12 +++++++++++- src/message/sign.js | 9 +++++++-- src/utils.js | 25 +++++-------------------- test/utils.spec.js | 8 ++++++-- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/index.js b/src/index.js index 96741cb2b7..5289413615 100644 --- a/src/index.js +++ b/src/index.js @@ -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') { @@ -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} */ async validate (message) { // eslint-disable-line require-await diff --git a/src/message/sign.js b/src/message/sign.js index bfa546db24..89e124f7d5 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 {Message} message + * @param {InMessage} message * @returns {Promise} */ async function messagePublicKey (message) { @@ -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 diff --git a/src/utils.js b/src/utils.js index f20efb9102..513aec0d39 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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) => { @@ -102,10 +94,3 @@ 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 17ee6116db..fafb907dc1 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -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', () => { @@ -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]) + } }) })