Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit 703ef73

Browse files
committed
test(keyexchange): Fix conflicts from NET-208-revocation branch.
1 parent 6ef29fb commit 703ef73

File tree

3 files changed

+59
-56
lines changed

3 files changed

+59
-56
lines changed

src/publish/Encrypt.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ export default function Encrypt(client: StreamrClient) {
4949
if (streamMessage.messageType !== StreamMessage.MESSAGE_TYPES.MESSAGE) {
5050
return
5151
}
52+
5253
const [groupKey, nextGroupKey] = await getPublisherKeyExchange().useGroupKey(stream.id)
54+
if (!groupKey) {
55+
throw new Error(`Tried to use group key but no group key found for stream: ${stream.id}`)
56+
}
57+
5358
await EncryptionUtil.encryptStreamMessage(streamMessage, groupKey, nextGroupKey)
5459
}
5560

src/stream/KeyExchange.ts

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/integration/EncryptionKeyPersistence.test.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
import { wait } from 'streamr-test-utils'
22

33
import { describeRepeats, fakePrivateKey, uid, getPublishTestMessages, addAfterFn } from '../utils'
4-
// import { Defer } from '../../src/utils'
54
import { StreamrClient } from '../../src/StreamrClient'
5+
import { Stream, StreamOperation } from '../../src/stream'
66
import { GroupKey } from '../../src/stream/Encryption'
77
import Connection from '../../src/Connection'
88

99
import config from './config'
1010

11-
const TIMEOUT = 30 * 1000
11+
const TIMEOUT = 10 * 1000
1212

1313
describeRepeats('decryption', () => {
14-
let publishTestMessages
1514
let expectErrors = 0 // check no errors by default
16-
let errors = []
17-
let subscriber
18-
const addAfter = addAfterFn()
19-
20-
const getOnError = (errs) => jest.fn((err) => {
15+
let errors: Error[] = []
16+
let onError = jest.fn()
17+
const getOnError = (errs: Error[]) => jest.fn((err) => {
2118
errs.push(err)
2219
})
2320

24-
let onError = jest.fn()
25-
let publisher
26-
let stream
21+
let publisher: StreamrClient
22+
let subscriber: StreamrClient
23+
let stream: Stream
24+
let publishTestMessages: ReturnType<typeof getPublishTestMessages>
25+
const addAfter = addAfterFn()
2726

2827
const createClient = (opts = {}) => {
2928
const c = new StreamrClient({
@@ -122,8 +121,8 @@ describeRepeats('decryption', () => {
122121
}
123122
})
124123
const otherUser = await subscriber.getUserInfo()
125-
await stream.grantPermission('stream_get', otherUser.username)
126-
await stream.grantPermission('stream_subscribe', otherUser.username)
124+
await stream.grantPermission(StreamOperation.STREAM_GET, otherUser.username)
125+
await stream.grantPermission(StreamOperation.STREAM_SUBSCRIBE, otherUser.username)
127126
const groupKey = GroupKey.generate()
128127
await publisher.setNextGroupKey(stream.id, groupKey)
129128
})
@@ -277,4 +276,3 @@ describeRepeats('decryption', () => {
277276
expect(received2).toEqual(published2)
278277
}, 2 * TIMEOUT)
279278
})
280-

0 commit comments

Comments
 (0)