Skip to content

Commit

Permalink
Revert "chore: refactor message normalization (#59)"
Browse files Browse the repository at this point in the history
This reverts commit dbc3d8c.
  • Loading branch information
vasco-santos committed Jul 14, 2020
1 parent de50542 commit b7706ad
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
12 changes: 1 addition & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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<Boolean>}
*/
async validate (message) { // eslint-disable-line require-await
Expand Down
9 changes: 2 additions & 7 deletions src/message/sign.js
Original file line number Diff line number Diff line change
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 {InMessage} message
* @param {Message} message
* @returns {Promise<PublicKey>}
*/
async function messagePublicKey (message) {
Expand All @@ -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
Expand Down
25 changes: 20 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,39 @@ 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) {
m.from = multibase.decode('z' + message.from)
}
return m
}

exports.normalizeOutRpcMessages = (messages) => {
if (!messages) {
return messages
}
return messages.map(exports.normalizeOutRpcMessage)
}
8 changes: 2 additions & 6 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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)
})
})

0 comments on commit b7706ad

Please sign in to comment.