Skip to content

Commit

Permalink
add encryption algorithm slot (#1862)
Browse files Browse the repository at this point in the history
same issue here — initially created the PR with unsigned commits... 
should be 1:1 with #1808
  • Loading branch information
erikolsson authored Dec 19, 2024
1 parent f3015d5 commit 3b26b56
Show file tree
Hide file tree
Showing 11 changed files with 1,685 additions and 1,452 deletions.
3 changes: 3 additions & 0 deletions core/node/events/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ func update_Snapshot_Member(
}
snapshot.Pins = snapPins
return nil
case *MemberPayload_EncryptionAlgorithm_:
snapshot.EncryptionAlgorithm.Algorithm = content.EncryptionAlgorithm.Algorithm
return nil
default:
return RiverError(Err_INVALID_ARGUMENT, "unknown membership payload type %T", memberPayload.Content)
}
Expand Down
3,002 changes: 1,552 additions & 1,450 deletions core/node/protocol/protocol.pb.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions core/node/rules/can_add_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,9 @@ func (params *aeParams) canAddMemberPayload(payload *StreamEvent_MemberPayload)
case *MemberPayload_Mls_:
return aeBuilder().
check(params.creatorIsMember)
case *MemberPayload_EncryptionAlgorithm_:
return aeBuilder().
check(params.creatorIsMember)
default:
return aeBuilder().
fail(unknownContentType(content))
Expand Down
24 changes: 24 additions & 0 deletions packages/sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ import {
make_UserMetadataPayload_Bio,
make_UserPayload_BlockchainTransaction,
ContractReceipt,
make_MemberPayload_EncryptionAlgorithm,
} from './types'

import debug from 'debug'
Expand Down Expand Up @@ -876,6 +877,29 @@ export class Client
})
}

async setStreamEncryptionAlgorithm(streamId: string, encryptionAlgorithm?: string) {
assert(
isChannelStreamId(streamId) ||
isSpaceStreamId(streamId) ||
isDMChannelStreamId(streamId) ||
isGDMChannelStreamId(streamId),
'channelId must be a valid streamId',
)
const stream = this.stream(streamId)
check(isDefined(stream), 'stream not found')
check(
stream.view.membershipContent.encryptionAlgorithm != encryptionAlgorithm,
`mlsEnabled is already set to ${encryptionAlgorithm}`,
)
return this.makeEventAndAddToStream(
streamId,
make_MemberPayload_EncryptionAlgorithm(encryptionAlgorithm),
{
method: 'setMlsEnabled',
},
)
}

async sendFullyReadMarkers(
channelId: string | Uint8Array,
fullyReadMarkers: Record<string, FullyReadMarker>,
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/streamEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export type StreamStateEvents = {
streamEnsAddressUpdated: (streamId: string, userId: string) => void
streamNftUpdated: (streamId: string, userId: string) => void
streamChannelPropertiesUpdated: (streamId: string) => void
streamEncryptionAlgorithmUpdated: (streamId: string, encryptionAlgorithm?: string) => void
}

export type StreamEvents = StreamEncryptionEvents & StreamStateEvents & SyncedStreamEvents
13 changes: 13 additions & 0 deletions packages/sdk/src/streamStateView_Members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class StreamStateView_Members extends StreamStateView_AbstractContent {
readonly solicitHelper: StreamStateView_Members_Solicitations
readonly memberMetadata: StreamStateView_MemberMetadata
readonly pins: Pin[] = []
encryptionAlgorithm?: string = undefined

constructor(streamId: string) {
super()
Expand Down Expand Up @@ -150,6 +151,8 @@ export class StreamStateView_Members extends StreamStateView_AbstractContent {
)
}
})

this.encryptionAlgorithm = snapshot.members.encryptionAlgorithm?.algorithm
}

prependEvent(
Expand Down Expand Up @@ -309,6 +312,14 @@ export class StreamStateView_Members extends StreamStateView_AbstractContent {
break
case 'mls':
break
case 'encryptionAlgorithm':
this.encryptionAlgorithm = payload.content.value.algorithm
stateEmitter?.emit(
'streamEncryptionAlgorithmUpdated',
this.streamId,
this.encryptionAlgorithm,
)
break
case undefined:
break
default:
Expand Down Expand Up @@ -362,6 +373,8 @@ export class StreamStateView_Members extends StreamStateView_AbstractContent {
break
case 'mls':
break
case 'encryptionAlgorithm':
break
case undefined:
break
default:
Expand Down
7 changes: 7 additions & 0 deletions packages/sdk/src/sync-agent/timeline/models/timeline-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export type TimelineEvent_OneOf =
| UserReceivedBlockchainTransactionEvent
| UnpinEvent
| MlsEvent
| StreamEncryptionAlgorithmEvent

export enum RiverTimelineEvent {
MiniblockHeader = 'm.miniblockheader',
Expand Down Expand Up @@ -120,6 +121,7 @@ export enum RiverTimelineEvent {
UserBlockchainTransaction = 'm.user_blockchain_transaction',
UserReceivedBlockchainTransaction = 'm.user_received_blockchain_transaction',
Mls = 'm.mls',
StreamEncryptionAlgorithm = 'm.stream_encryption_algorithm',
}

export interface MiniblockHeaderEvent {
Expand Down Expand Up @@ -221,6 +223,11 @@ export interface MlsEvent {
kind: RiverTimelineEvent.Mls
}

export interface StreamEncryptionAlgorithmEvent {
kind: RiverTimelineEvent.StreamEncryptionAlgorithm
algorithm?: string
}

export interface RoomMessageEncryptedEvent {
kind: RiverTimelineEvent.RoomMessageEncrypted
error?: DecryptionSessionError
Expand Down
11 changes: 10 additions & 1 deletion packages/sdk/src/sync-agent/timeline/models/timelineEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
UserBlockchainTransactionEvent,
MemberBlockchainTransactionEvent,
UserReceivedBlockchainTransactionEvent,
StreamEncryptionAlgorithmEvent,
} from './timeline-types'
import type { PlainMessage } from '@bufbuild/protobuf'
import { userIdFromAddress, streamIdFromBytes, streamIdAsString } from '../../../id'
Expand Down Expand Up @@ -364,7 +365,13 @@ function toTownsContent_MemberPayload(
kind: RiverTimelineEvent.Mls,
},
}
break
case 'encryptionAlgorithm':
return {
content: {
kind: RiverTimelineEvent.StreamEncryptionAlgorithm,
algorithm: value.content.value.algorithm,
} satisfies StreamEncryptionAlgorithmEvent,
}
case 'memberBlockchainTransaction':
return {
content: {
Expand Down Expand Up @@ -987,6 +994,8 @@ export function getFallbackContent(
? bin_toHexString(content.receivedTransaction.fromUserAddress)
: ''
}`
case RiverTimelineEvent.StreamEncryptionAlgorithm:
return `algorithm: ${content.algorithm}`
}
}

Expand Down
51 changes: 50 additions & 1 deletion packages/sdk/src/tests/multi_ne/memberMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { MemberPayload_Nft } from '@river-build/proto'
import { Client } from '../../client'
import { userIdFromAddress } from '../../id'
import { makeUniqueChannelStreamId, userIdFromAddress } from '../../id'
import {
makeDonePromise,
makeRandomUserAddress,
Expand Down Expand Up @@ -659,4 +659,53 @@ describe('memberMetadataTests', () => {

await expect(bobsClient.setNft(streamId, '', 0, '')).resolves.not.toThrow()
})

test('clientCanSetStreamEncryptionAlgorithm', async () => {
await expect(bobsClient.initializeUser()).resolves.not.toThrow()
bobsClient.startSync()
const spaceId = makeUniqueSpaceStreamId()
const channelId = makeUniqueChannelStreamId(spaceId)
await bobsClient.createSpace(spaceId)
await bobsClient.waitForStream(spaceId)

await bobsClient.createChannel(spaceId, 'secret channel', 'messaging like spies', channelId)
await bobsClient.waitForStream(channelId)

// initial value is "undefined"
expect(bobsClient.stream(channelId)?.view.membershipContent.encryptionAlgorithm).toBe(
undefined,
)

// set mls enabled to mls_0.0.1
const truePromise = makeDonePromise()
bobsClient.once('streamEncryptionAlgorithmUpdated', (updatedStreamId, value) => {
expect(updatedStreamId).toBe(channelId)
expect(value).toBe('mls_0.0.1')
truePromise.done()
})

await expect(
bobsClient.setStreamEncryptionAlgorithm(channelId, 'mls_0.0.1'),
).resolves.not.toThrow()
await truePromise.expectToSucceed()
expect(bobsClient.stream(channelId)?.view.membershipContent.encryptionAlgorithm).toBe(
'mls_0.0.1',
)

// toggle back to to undefined
const falsePromise = makeDonePromise()
bobsClient.once('streamEncryptionAlgorithmUpdated', (updatedStreamId, value) => {
expect(updatedStreamId).toBe(channelId)
expect(value).toBe(undefined)
falsePromise.done()
})

await expect(
bobsClient.setStreamEncryptionAlgorithm(channelId, undefined),
).resolves.not.toThrow()
await falsePromise.expectToSucceed()
expect(bobsClient.stream(channelId)?.view.membershipContent.encryptionAlgorithm).toBe(
undefined,
)
})
})
16 changes: 16 additions & 0 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,22 @@ export const make_ChannelPayload_Redaction = (
}
}

export const make_MemberPayload_EncryptionAlgorithm = (
content?: string,
): PlainMessage<StreamEvent>['payload'] => {
return {
case: 'memberPayload',
value: {
content: {
case: 'encryptionAlgorithm',
value: {
algorithm: content,
},
},
},
}
}

export const make_MemberPayload_KeyFulfillment = (
value: PlainMessage<MemberPayload_KeyFulfillment>,
): PlainMessage<StreamEvent>['payload'] => {
Expand Down
6 changes: 6 additions & 0 deletions protocol/protocol.proto
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ message MemberPayload {
repeated Member joined = 1;
repeated SnappedPin pins = 2;
Mls mls = 3;
EncryptionAlgorithm encryption_algorithm = 4;
}

message Membership {
Expand Down Expand Up @@ -222,6 +223,10 @@ message MemberPayload {

}

message EncryptionAlgorithm {
optional string algorithm = 1;
}

message MemberBlockchainTransaction {
BlockchainTransaction transaction = 1;
bytes from_user_address = 2;
Expand All @@ -239,6 +244,7 @@ message MemberPayload {
Unpin unpin = 9;
Mls mls = 10;
MemberBlockchainTransaction member_blockchain_transaction = 11;
EncryptionAlgorithm encryption_algorithm = 12;
}
}

Expand Down

0 comments on commit 3b26b56

Please sign in to comment.