File tree Expand file tree Collapse file tree 4 files changed +29
-25
lines changed
Expand file tree Collapse file tree 4 files changed +29
-25
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,16 @@ 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+
1828function validateRegistrar ( registrar ) {
1929 // registrar handling
2030 if ( typeof registrar !== 'object' ) {
@@ -257,7 +267,7 @@ class PubsubBaseProtocol extends EventEmitter {
257267
258268 /**
259269 * Validates the given message. The signature will be checked for authenticity.
260- * @param {rpc.RPC.Message } message
270+ * @param {InMessage } message
261271 * @returns {Promise<Boolean> }
262272 */
263273 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 {Message } message
56+ * @param {InMessage } message
5757 * @returns {Promise<PublicKey> }
5858 */
5959async function messagePublicKey ( message ) {
@@ -66,7 +66,12 @@ 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- const from = PeerId . createFromBytes ( message . from )
69+ let from
70+ if ( typeof message . from === 'string' ) {
71+ from = PeerId . createFromB58String ( message . from )
72+ } else {
73+ from = PeerId . createFromBytes ( message . from )
74+ }
7075
7176 if ( from . pubKey ) {
7277 return from . pubKey
Original file line number Diff line number Diff line change @@ -73,26 +73,18 @@ exports.ensureArray = (maybeArray) => {
7373 * Ensures `message.from` is base58 encoded
7474 * @param {Object } message
7575 * @param {Buffer|String } message.from
76+ * @param {PeerId } peerId
7677 * @return {Object }
7778 */
78- exports . normalizeInRpcMessage = ( message ) => {
79+ exports . normalizeInRpcMessage = ( message , peerId ) => {
7980 const m = Object . assign ( { } , message )
8081 if ( Buffer . isBuffer ( message . from ) ) {
8182 m . from = multibase . encode ( 'base58btc' , message . from ) . toString ( ) . slice ( 1 )
8283 }
83- return m
84- }
85-
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
84+ if ( peerId ) {
85+ m . receivedFrom = peerId . toB58String ( )
9486 }
95- return messages . map ( exports . normalizeInRpcMessage )
87+ return m
9688}
9789
9890exports . normalizeOutRpcMessage = ( message ) => {
@@ -102,10 +94,3 @@ exports.normalizeOutRpcMessage = (message) => {
10294 }
10395 return m
10496}
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,7 +59,9 @@ describe('utils', () => {
5959 { from : stringId } ,
6060 { from : stringId }
6161 ]
62- expect ( utils . normalizeInRpcMessages ( m ) ) . to . deep . eql ( expected )
62+ for ( let i = 0 ; i < m . length ; i ++ ) {
63+ expect ( utils . normalizeInRpcMessage ( m [ i ] ) ) . to . deep . eql ( expected [ i ] )
64+ }
6365 } )
6466
6567 it ( 'converts an OUT msg.from to binary' , ( ) => {
@@ -73,6 +75,8 @@ describe('utils', () => {
7375 { from : binaryId } ,
7476 { from : binaryId }
7577 ]
76- expect ( utils . normalizeOutRpcMessages ( m ) ) . to . deep . eql ( expected )
78+ for ( let i = 0 ; i < m . length ; i ++ ) {
79+ expect ( utils . normalizeOutRpcMessage ( m [ i ] ) ) . to . deep . eql ( expected [ i ] )
80+ }
7781 } )
7882} )
You can’t perform that action at this time.
0 commit comments