|
| 1 | +import { LitProtocolFacade } from './LitProtocolFacade' |
| 2 | +import { inject, Lifecycle, scoped } from 'tsyringe' |
| 3 | +import { GroupKeyStore } from './GroupKeyStore' |
| 4 | +import { GroupKey } from './GroupKey' |
| 5 | +import { StreamID, StreamPartID, StreamPartIDUtils } from '@streamr/protocol' |
| 6 | +import { EthereumAddress, waitForEvent } from '@streamr/utils' |
| 7 | +import { SubscriberKeyExchange } from './SubscriberKeyExchange' |
| 8 | +import { ConfigInjectionToken, StrictStreamrClientConfig } from '../Config' |
| 9 | +import { StreamrClientEventEmitter } from '../events' |
| 10 | +import { DestroySignal } from '../DestroySignal' |
| 11 | +import crypto from 'crypto' |
| 12 | +import { uuid } from '../utils/uuid' |
| 13 | + |
| 14 | +@scoped(Lifecycle.ContainerScoped) |
| 15 | +export class GroupKeyManager { |
| 16 | + private readonly groupKeyStore: GroupKeyStore |
| 17 | + private readonly litProtocolFacade: LitProtocolFacade |
| 18 | + private readonly subscriberKeyExchange: SubscriberKeyExchange |
| 19 | + private readonly eventEmitter: StreamrClientEventEmitter |
| 20 | + private readonly destroySignal: DestroySignal |
| 21 | + private readonly config: Pick<StrictStreamrClientConfig, 'decryption' | 'encryption'> |
| 22 | + |
| 23 | + constructor( |
| 24 | + groupKeyStore: GroupKeyStore, |
| 25 | + litProtocolFacade: LitProtocolFacade, |
| 26 | + subscriberKeyExchange: SubscriberKeyExchange, |
| 27 | + eventEmitter: StreamrClientEventEmitter, |
| 28 | + destroySignal: DestroySignal, |
| 29 | + @inject(ConfigInjectionToken) config: Pick<StrictStreamrClientConfig, 'decryption' | 'encryption'> |
| 30 | + ) { |
| 31 | + this.groupKeyStore = groupKeyStore |
| 32 | + this.litProtocolFacade = litProtocolFacade |
| 33 | + this.subscriberKeyExchange = subscriberKeyExchange |
| 34 | + this.eventEmitter = eventEmitter |
| 35 | + this.destroySignal = destroySignal |
| 36 | + this.config = config |
| 37 | + } |
| 38 | + |
| 39 | + async fetchKey(streamPartId: StreamPartID, groupKeyId: string, publisherId: EthereumAddress): Promise<GroupKey> { |
| 40 | + const streamId = StreamPartIDUtils.getStreamID(streamPartId) |
| 41 | + |
| 42 | + // 1st try: local storage |
| 43 | + let groupKey = await this.groupKeyStore.get(groupKeyId, streamId) |
| 44 | + if (groupKey !== undefined) { |
| 45 | + return groupKey |
| 46 | + } |
| 47 | + |
| 48 | + // 2nd try: lit-protocol |
| 49 | + if (this.config.encryption.litProtocolEnabled) { |
| 50 | + groupKey = await this.litProtocolFacade.get(streamId, groupKeyId) |
| 51 | + if (groupKey !== undefined) { |
| 52 | + await this.groupKeyStore.add(groupKey, streamId) |
| 53 | + return groupKey |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // 3rd try: Streamr key-exchange |
| 58 | + await this.subscriberKeyExchange.requestGroupKey(groupKeyId, publisherId, streamPartId) |
| 59 | + const groupKeys = await waitForEvent( |
| 60 | + // TODO remove "as any" type casing in NET-889 |
| 61 | + this.eventEmitter as any, |
| 62 | + 'addGroupKey', |
| 63 | + this.config.decryption.keyRequestTimeout, |
| 64 | + (storedGroupKey: GroupKey) => storedGroupKey.id === groupKeyId, |
| 65 | + this.destroySignal.abortSignal |
| 66 | + ) |
| 67 | + return groupKeys[0] as GroupKey |
| 68 | + } |
| 69 | + |
| 70 | + async storeKey(groupKey: GroupKey | undefined, streamId: StreamID): Promise<GroupKey> { // TODO: name |
| 71 | + if (groupKey === undefined) { |
| 72 | + const keyData = crypto.randomBytes(32) |
| 73 | + // 1st try lit-protocol, if a key cannot be generated and stored, then generate group key locally |
| 74 | + if (this.config.encryption.litProtocolEnabled) { |
| 75 | + groupKey = await this.litProtocolFacade.store(streamId, keyData) |
| 76 | + } |
| 77 | + if (groupKey === undefined) { |
| 78 | + groupKey = new GroupKey(uuid('GroupKey'), keyData) |
| 79 | + } |
| 80 | + } |
| 81 | + await this.groupKeyStore.add(groupKey, streamId) |
| 82 | + return groupKey |
| 83 | + } |
| 84 | + |
| 85 | + addKeyToLocalStore(groupKey: GroupKey, streamId: StreamID): Promise<void> { |
| 86 | + return this.groupKeyStore.add(groupKey, streamId) |
| 87 | + } |
| 88 | +} |
0 commit comments