@@ -108,49 +108,48 @@ function GroupKeyStore({ clientId, streamId, groupKeys }: GroupKeyStoreOptions)
108108 async has ( id : GroupKeyId ) {
109109 if ( currentGroupKeyId === id ) { return true }
110110
111- if ( nextGroupKeys . some ( ( nextKey ) => nextKey . id === id ) ) { return true }
111+ if ( nextGroupKeys . some ( ( nextKey ) => nextKey . id === id ) ) { return true }
112112
113113 return store . has ( id )
114114 } ,
115115 async isEmpty ( ) {
116116 return ! nextGroupKeys . length && await store . size ( ) === 0
117117 } ,
118- async useGroupKey ( ) : Promise < [ GroupKey , GroupKey | undefined ] > {
118+ async useGroupKey ( ) : Promise < [ GroupKey | undefined , GroupKey | undefined ] > {
119119 const nextGroupKey = nextGroupKeys . pop ( )
120- switch ( true ) {
121- // First use of group key on this stream, no current key. Make next key current.
122- case ! ! ( ! currentGroupKeyId && nextGroupKey ) : {
123- await storeKey ( nextGroupKey )
124- currentGroupKeyId = nextGroupKey . id
125- return [
126- await this . get ( currentGroupKeyId ) ,
127- undefined ,
128- ]
129- }
130- // Keep using current key (empty next)
131- case ! ! ( currentGroupKeyId && ! nextGroupKey ) : {
132- return [
133- await this . get ( currentGroupKeyId ) ,
134- undefined
135- ]
136- }
137- // Key changed (non-empty next). return current + next. Make next key current.
138- case ! ! ( currentGroupKeyId && nextGroupKey ) : {
139- await storeKey ( nextGroupKey )
140- const prevGroupKey = await this . get ( currentGroupKeyId )
141- currentGroupKeyId = nextGroupKey . id
142- // use current key one more time
143- return [
144- prevGroupKey ,
145- nextGroupKey ,
146- ]
147- }
148- // Generate & use new key if none already set.
149- default : {
150- await this . rotateGroupKey ( )
151- return this . useGroupKey ( )
120+ // First use of group key on this stream, no current key. Make next key current.
121+ if ( ! currentGroupKeyId && nextGroupKey ) {
122+ await storeKey ( nextGroupKey )
123+ currentGroupKeyId = nextGroupKey . id
124+ return [
125+ await this . get ( currentGroupKeyId ) ,
126+ undefined ,
127+ ]
152128 }
153- }
129+
130+ // Keep using current key (empty next)
131+ if ( currentGroupKeyId != null && ! nextGroupKey ) {
132+ return [
133+ await this . get ( currentGroupKeyId ) ,
134+ undefined
135+ ]
136+ }
137+
138+ // Key changed (non-empty next). return current + next. Make next key current.
139+ if ( currentGroupKeyId != null && nextGroupKey != null ) {
140+ await storeKey ( nextGroupKey )
141+ const prevGroupKey = await this . get ( currentGroupKeyId )
142+ currentGroupKeyId = nextGroupKey . id
143+ // use current key one more time
144+ return [
145+ prevGroupKey ,
146+ nextGroupKey ,
147+ ]
148+ }
149+
150+ // Generate & use new key if none already set.
151+ await this . rotateGroupKey ( )
152+ return this . useGroupKey ( )
154153 } ,
155154 async get ( id : GroupKeyId ) {
156155 return store . get ( id )
@@ -257,7 +256,7 @@ async function PublisherKeyExhangeSubscription(client: StreamrClient, getGroupKe
257256 const subscriberId = streamMessage . getPublisherId ( )
258257
259258 const groupKeyStore = await getGroupKeyStore ( streamId )
260- const isSubscriber = await client . isStreamSubscriber ( streamId , subscriberId )
259+ const isSubscriber = await client . isStreamSubscriber ( streamId , subscriberId )
261260 const encryptedGroupKeys = ( ! isSubscriber ? [ ] : await Promise . all ( groupKeyIds . map ( async ( id ) => {
262261 const groupKey = await groupKeyStore . get ( id )
263262 if ( ! groupKey ) {
@@ -357,7 +356,7 @@ export function PublisherKeyExhange(client: StreamrClient, { groupKeys = {} }: K
357356
358357 async function useGroupKey ( streamId : string ) {
359358 await next ( )
360- if ( ! enabled ) { return undefined }
359+ if ( ! enabled ) { return [ ] }
361360 const groupKeyStore = await getGroupKeyStore ( streamId )
362361 return groupKeyStore . useGroupKey ( )
363362 }
@@ -367,10 +366,10 @@ export function PublisherKeyExhange(client: StreamrClient, { groupKeys = {} }: K
367366 return ! groupKeyStore . isEmpty ( )
368367 }
369368
370- async function rekey ( streamId ) {
369+ async function rekey ( streamId : string ) {
371370 if ( ! enabled ) { return }
372- const groupKeyStore = getGroupKeyStore ( streamId )
373- groupKeyStore . rekey ( )
371+ const groupKeyStore = await getGroupKeyStore ( streamId )
372+ await groupKeyStore . rekey ( )
374373 await next ( )
375374 }
376375
@@ -652,11 +651,12 @@ export function SubscriberKeyExchange(client: StreamrClient, { groupKeys = {} }:
652651 enabled = true
653652 return next ( )
654653 } ,
655- async addNewKey ( streamMessage ) {
654+ async addNewKey ( streamMessage : StreamMessage ) {
656655 if ( ! streamMessage . newGroupKey ) { return }
657656 const streamId = streamMessage . getStreamId ( )
658657 const groupKeyStore = await getGroupKeyStore ( streamId )
659- return groupKeyStore . add ( streamMessage . newGroupKey )
658+ const newGroupKey : unknown = streamMessage . newGroupKey
659+ await groupKeyStore . add ( newGroupKey as GroupKey )
660660 } ,
661661 async stop ( ) {
662662 enabled = false
0 commit comments