@@ -5,13 +5,15 @@ import { SignatureValidator } from '../signature/SignatureValidator'
55import { getPartitionCount } from '../StreamMetadata'
66import { StreamrClientError } from '../StreamrClientError'
77import { GroupKeyRequest , GroupKeyResponse } from '@streamr/trackerless-network'
8+ import { StrictStreamrClientConfig } from '../Config'
89
910export const validateStreamMessage = async (
1011 msg : StreamMessage ,
1112 streamRegistry : StreamRegistry ,
12- signatureValidator : SignatureValidator
13+ signatureValidator : SignatureValidator ,
14+ config : Pick < StrictStreamrClientConfig , 'validation' >
1315) : Promise < void > => {
14- await doValidate ( msg , streamRegistry , signatureValidator ) . catch ( ( err : any ) => {
16+ await doValidate ( msg , streamRegistry , signatureValidator , config ) . catch ( ( err : any ) => {
1517 // all StreamMessageError already have this streamMessage, maybe this is
1618 // here if e.g. contract call fails? TODO is this really needed as
1719 // the onError callback in messagePipeline knows which message
@@ -33,25 +35,28 @@ export const validateStreamMessage = async (
3335const doValidate = async (
3436 streamMessage : StreamMessage ,
3537 streamRegistry : StreamRegistry ,
36- signatureValidator : SignatureValidator
38+ signatureValidator : SignatureValidator ,
39+ config : Pick < StrictStreamrClientConfig , 'validation' >
3740) : Promise < void > => {
3841 await signatureValidator . assertSignatureIsValid ( streamMessage )
3942 switch ( streamMessage . messageType ) {
4043 case StreamMessageType . MESSAGE :
41- return validateMessage ( streamMessage , streamRegistry )
44+ return validateMessage ( streamMessage , streamRegistry , config )
4245 case StreamMessageType . GROUP_KEY_REQUEST :
4346 return validateGroupKeyMessage (
4447 streamMessage ,
4548 toUserId ( GroupKeyRequest . fromBinary ( streamMessage . content ) . recipientId ) ,
4649 streamMessage . getPublisherId ( ) ,
47- streamRegistry
50+ streamRegistry ,
51+ config
4852 )
4953 case StreamMessageType . GROUP_KEY_RESPONSE :
5054 return validateGroupKeyMessage (
5155 streamMessage ,
5256 streamMessage . getPublisherId ( ) ,
5357 toUserId ( GroupKeyResponse . fromBinary ( streamMessage . content ) . recipientId ) ,
54- streamRegistry
58+ streamRegistry ,
59+ config
5560 )
5661 default :
5762 throw new StreamrClientError ( `Unknown message type: ${ streamMessage . messageType } !` , 'ASSERTION_FAILED' , streamMessage )
@@ -60,38 +65,46 @@ const doValidate = async (
6065
6166const validateMessage = async (
6267 streamMessage : StreamMessage ,
63- streamRegistry : StreamRegistry
68+ streamRegistry : StreamRegistry ,
69+ config : Pick < StrictStreamrClientConfig , 'validation' >
6470) : Promise < void > => {
6571 const streamId = streamMessage . getStreamId ( )
66- const streamMetadata = await streamRegistry . getStreamMetadata ( streamId )
67- const partitionCount = getPartitionCount ( streamMetadata )
68- if ( streamMessage . getStreamPartition ( ) < 0 || streamMessage . getStreamPartition ( ) >= partitionCount ) {
69- throw new StreamrClientError (
70- `Partition ${ streamMessage . getStreamPartition ( ) } is out of range (0..${ partitionCount - 1 } )` ,
71- 'INVALID_PARTITION' ,
72- streamMessage
73- )
72+ if ( config . validation . partitions ) {
73+ const streamMetadata = await streamRegistry . getStreamMetadata ( streamId )
74+ const partitionCount = getPartitionCount ( streamMetadata )
75+ if ( streamMessage . getStreamPartition ( ) < 0 || streamMessage . getStreamPartition ( ) >= partitionCount ) {
76+ throw new StreamrClientError (
77+ `Partition ${ streamMessage . getStreamPartition ( ) } is out of range (0..${ partitionCount - 1 } )` ,
78+ 'INVALID_PARTITION' ,
79+ streamMessage
80+ )
81+ }
7482 }
75- const sender = streamMessage . getPublisherId ( )
76- const isPublisher = await streamRegistry . isStreamPublisher ( streamId , sender )
77- if ( ! isPublisher ) {
78- throw new StreamrClientError ( `${ sender } is not a publisher on stream ${ streamId } ` , 'MISSING_PERMISSION' , streamMessage )
83+ if ( config . validation . permissions ) {
84+ const sender = streamMessage . getPublisherId ( )
85+ const isPublisher = await streamRegistry . isStreamPublisher ( streamId , sender )
86+ if ( ! isPublisher ) {
87+ throw new StreamrClientError ( `${ sender } is not a publisher on stream ${ streamId } ` , 'MISSING_PERMISSION' , streamMessage )
88+ }
7989 }
8090}
8191
8292const validateGroupKeyMessage = async (
8393 streamMessage : StreamMessage ,
8494 expectedPublisherId : UserID ,
8595 expectedSubscriberId : UserID ,
86- streamRegistry : StreamRegistry
96+ streamRegistry : StreamRegistry ,
97+ config : Pick < StrictStreamrClientConfig , 'validation' >
8798) : Promise < void > => {
88- const streamId = streamMessage . getStreamId ( )
89- const isPublisher = await streamRegistry . isStreamPublisher ( streamId , expectedPublisherId )
90- if ( ! isPublisher ) {
91- throw new StreamrClientError ( `${ expectedPublisherId } is not a publisher on stream ${ streamId } ` , 'MISSING_PERMISSION' , streamMessage )
92- }
93- const isSubscriber = await streamRegistry . isStreamSubscriber ( streamId , expectedSubscriberId )
94- if ( ! isSubscriber ) {
95- throw new StreamrClientError ( `${ expectedSubscriberId } is not a subscriber on stream ${ streamId } ` , 'MISSING_PERMISSION' , streamMessage )
99+ if ( config . validation . permissions ) {
100+ const streamId = streamMessage . getStreamId ( )
101+ const isPublisher = await streamRegistry . isStreamPublisher ( streamId , expectedPublisherId )
102+ if ( ! isPublisher ) {
103+ throw new StreamrClientError ( `${ expectedPublisherId } is not a publisher on stream ${ streamId } ` , 'MISSING_PERMISSION' , streamMessage )
104+ }
105+ const isSubscriber = await streamRegistry . isStreamSubscriber ( streamId , expectedSubscriberId )
106+ if ( ! isSubscriber ) {
107+ throw new StreamrClientError ( `${ expectedSubscriberId } is not a subscriber on stream ${ streamId } ` , 'MISSING_PERMISSION' , streamMessage )
108+ }
96109 }
97110}
0 commit comments