File tree Expand file tree Collapse file tree 4 files changed +25
-29
lines changed
Expand file tree Collapse file tree 4 files changed +25
-29
lines changed Original file line number Diff line number Diff line change @@ -15,16 +15,6 @@ const {
1515 verifySignature
1616} = require ( './message/sign' )
1717
18- /**
19- * @typedef {Object } InMessage
20- * @property {string } from
21- * @property {string } receivedFrom
22- * @property {string[] } topicIDs
23- * @property {Buffer } data
24- * @property {Buffer } [signature]
25- * @property {Buffer } [key]
26- */
27-
2818function validateRegistrar ( registrar ) {
2919 // registrar handling
3020 if ( typeof registrar !== 'object' ) {
@@ -267,7 +257,7 @@ class PubsubBaseProtocol extends EventEmitter {
267257
268258 /**
269259 * Validates the given message. The signature will be checked for authenticity.
270- * @param {InMessage } message
260+ * @param {rpc.RPC.Message } message
271261 * @returns {Promise<Boolean> }
272262 */
273263 async validate ( message ) { // eslint-disable-line require-await
Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ async function verifySignature (message) {
5353 * Returns the PublicKey associated with the given message.
5454 * If no, valid PublicKey can be retrieved an error will be returned.
5555 *
56- * @param {InMessage } message
56+ * @param {Message } message
5757 * @returns {Promise<PublicKey> }
5858 */
5959async function messagePublicKey ( message ) {
@@ -66,12 +66,7 @@ async function messagePublicKey (message) {
6666 throw new Error ( 'Public Key does not match the originator' )
6767 } else {
6868 // should be available in the from property of the message (peer id)
69- let from
70- if ( typeof message . from === 'string' ) {
71- from = PeerId . createFromB58String ( message . from )
72- } else {
73- from = PeerId . createFromBytes ( message . from )
74- }
69+ const from = PeerId . createFromBytes ( message . from )
7570
7671 if ( from . pubKey ) {
7772 return from . pubKey
Original file line number Diff line number Diff line change @@ -73,24 +73,39 @@ exports.ensureArray = (maybeArray) => {
7373 * Ensures `message.from` is base58 encoded
7474 * @param {Object } message
7575 * @param {Buffer|String } message.from
76- * @param {PeerId } peerId
7776 * @return {Object }
7877 */
79- exports . normalizeInRpcMessage = ( message , peerId ) => {
78+ exports . normalizeInRpcMessage = ( message ) => {
8079 const m = Object . assign ( { } , message )
8180 if ( Buffer . isBuffer ( message . from ) ) {
8281 m . from = multibase . encode ( 'base58btc' , message . from ) . toString ( ) . slice ( 1 )
8382 }
84- if ( peerId ) {
85- m . receivedFrom = peerId . toB58String ( )
86- }
8783 return m
8884}
8985
86+ /**
87+ * The same as `normalizeInRpcMessage`, but performed on an array of messages
88+ * @param {Object[] } messages
89+ * @return {Object[] }
90+ */
91+ exports . normalizeInRpcMessages = ( messages ) => {
92+ if ( ! messages ) {
93+ return messages
94+ }
95+ return messages . map ( exports . normalizeInRpcMessage )
96+ }
97+
9098exports . normalizeOutRpcMessage = ( message ) => {
9199 const m = Object . assign ( { } , message )
92100 if ( typeof message . from === 'string' || message . from instanceof String ) {
93101 m . from = multibase . decode ( 'z' + message . from )
94102 }
95103 return m
96104}
105+
106+ exports . normalizeOutRpcMessages = ( messages ) => {
107+ if ( ! messages ) {
108+ return messages
109+ }
110+ return messages . map ( exports . normalizeOutRpcMessage )
111+ }
Original file line number Diff line number Diff line change @@ -59,9 +59,7 @@ describe('utils', () => {
5959 { from : stringId } ,
6060 { from : stringId }
6161 ]
62- for ( let i = 0 ; i < m . length ; i ++ ) {
63- expect ( utils . normalizeInRpcMessage ( m [ i ] ) ) . to . deep . eql ( expected [ i ] )
64- }
62+ expect ( utils . normalizeInRpcMessages ( m ) ) . to . deep . eql ( expected )
6563 } )
6664
6765 it ( 'converts an OUT msg.from to binary' , ( ) => {
@@ -75,8 +73,6 @@ describe('utils', () => {
7573 { from : binaryId } ,
7674 { from : binaryId }
7775 ]
78- for ( let i = 0 ; i < m . length ; i ++ ) {
79- expect ( utils . normalizeOutRpcMessage ( m [ i ] ) ) . to . deep . eql ( expected [ i ] )
80- }
76+ expect ( utils . normalizeOutRpcMessages ( m ) ) . to . deep . eql ( expected )
8177 } )
8278} )
You can’t perform that action at this time.
0 commit comments