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 {
15
15
verifySignature
16
16
} = require ( './message/sign' )
17
17
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
+
18
28
function validateRegistrar ( registrar ) {
19
29
// registrar handling
20
30
if ( typeof registrar !== 'object' ) {
@@ -257,7 +267,7 @@ class PubsubBaseProtocol extends EventEmitter {
257
267
258
268
/**
259
269
* Validates the given message. The signature will be checked for authenticity.
260
- * @param {rpc.RPC.Message } message
270
+ * @param {InMessage } message
261
271
* @returns {Promise<Boolean> }
262
272
*/
263
273
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) {
53
53
* Returns the PublicKey associated with the given message.
54
54
* If no, valid PublicKey can be retrieved an error will be returned.
55
55
*
56
- * @param {Message } message
56
+ * @param {InMessage } message
57
57
* @returns {Promise<PublicKey> }
58
58
*/
59
59
async function messagePublicKey ( message ) {
@@ -66,7 +66,12 @@ async function messagePublicKey (message) {
66
66
throw new Error ( 'Public Key does not match the originator' )
67
67
} else {
68
68
// 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
+ }
70
75
71
76
if ( from . pubKey ) {
72
77
return from . pubKey
Original file line number Diff line number Diff line change @@ -73,26 +73,18 @@ exports.ensureArray = (maybeArray) => {
73
73
* Ensures `message.from` is base58 encoded
74
74
* @param {Object } message
75
75
* @param {Buffer|String } message.from
76
+ * @param {PeerId } peerId
76
77
* @return {Object }
77
78
*/
78
- exports . normalizeInRpcMessage = ( message ) => {
79
+ exports . normalizeInRpcMessage = ( message , peerId ) => {
79
80
const m = Object . assign ( { } , message )
80
81
if ( Buffer . isBuffer ( message . from ) ) {
81
82
m . from = multibase . encode ( 'base58btc' , message . from ) . toString ( ) . slice ( 1 )
82
83
}
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 ( )
94
86
}
95
- return messages . map ( exports . normalizeInRpcMessage )
87
+ return m
96
88
}
97
89
98
90
exports . normalizeOutRpcMessage = ( message ) => {
@@ -102,10 +94,3 @@ exports.normalizeOutRpcMessage = (message) => {
102
94
}
103
95
return m
104
96
}
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', () => {
59
59
{ from : stringId } ,
60
60
{ from : stringId }
61
61
]
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
+ }
63
65
} )
64
66
65
67
it ( 'converts an OUT msg.from to binary' , ( ) => {
@@ -73,6 +75,8 @@ describe('utils', () => {
73
75
{ from : binaryId } ,
74
76
{ from : binaryId }
75
77
]
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
+ }
77
81
} )
78
82
} )
You can’t perform that action at this time.
0 commit comments