Skip to content

Commit

Permalink
Use the optional flag when adding events for key fulfillments (#75)
Browse files Browse the repository at this point in the history
Currently when we repond to KeySolicitations we first post a
KeyFulfillment to the channel. Lots of users can post this fulfillment
at the same time, but the node will only accept one of them

After this change, instead of throwing tons of errors polluting all
logs, we now pass “optional: true” and check the error in the calling
code
  • Loading branch information
texuf authored May 27, 2024
1 parent 0ae84c7 commit e36bbfe
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
21 changes: 13 additions & 8 deletions core/encryption/src/decryptionExtensions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import TypedEmitter from 'typed-emitter'
import { Permission } from '@river-build/web3'
import {
AddEventResponse_Error,
EncryptedData,
SessionKeys,
UserInboxPayload_GroupEncryptionSessions,
Expand Down Expand Up @@ -196,7 +197,9 @@ export abstract class BaseDecryptionExtensions {
public abstract isUserInboxStreamUpToDate(upToDateStreams: Set<string>): boolean
public abstract onDecryptionError(item: EncryptedContentItem, err: DecryptionSessionError): void
public abstract sendKeySolicitation(args: KeySolicitationData): Promise<void>
public abstract sendKeyFulfillment(args: KeyFulfilmentData): Promise<void>
public abstract sendKeyFulfillment(
args: KeyFulfilmentData,
): Promise<{ error?: AddEventResponse_Error }>
public abstract encryptAndShareGroupSessions(args: GroupSessionsData): Promise<void>
public abstract shouldPauseTicking(): boolean
/**
Expand Down Expand Up @@ -661,7 +664,7 @@ export abstract class BaseDecryptionExtensions {

/**
* processKeySolicitation
* process incoming key solicitations and send keys and key fulfilments
* process incoming key solicitations and send keys and key fulfillments
*/
private async processKeySolicitation(item: KeySolicitationItem): Promise<void> {
this.log.debug('processing key solicitation', item.streamId, item)
Expand Down Expand Up @@ -710,18 +713,20 @@ export abstract class BaseDecryptionExtensions {
return
}

await this.sendKeyFulfillment({
const { error } = await this.sendKeyFulfillment({
streamId,
userAddress: item.fromUserAddress,
deviceKey: item.solicitation.deviceKey,
sessionIds: item.solicitation.isNewDevice ? [] : sessions.map((x) => x.sessionId),
})

await this.encryptAndShareGroupSessions({
streamId,
item,
sessions,
})
if (!error) {
await this.encryptAndShareGroupSessions({
streamId,
item,
sessions,
})
}
}

/**
Expand Down
18 changes: 13 additions & 5 deletions core/encryption/src/tests/decryptionExtensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
KeySolicitationData,
makeSessionKeys,
} from '../decryptionExtensions'
import { EncryptedData, UserInboxPayload_GroupEncryptionSessions } from '@river-build/proto'
import {
AddEventResponse_Error,
EncryptedData,
UserInboxPayload_GroupEncryptionSessions,
} from '@river-build/proto'
import { GroupEncryptionSession, UserDevice, UserDeviceCollection } from '../olmLib'
import { bin_fromHexString, bin_toHexString, dlog } from '@river-build/dlog'

Expand Down Expand Up @@ -280,9 +284,11 @@ class MockDecryptionExtensions extends BaseDecryptionExtensions {
return Promise.resolve()
}

public sendKeyFulfillment(args: KeyFulfilmentData): Promise<void> {
public sendKeyFulfillment(
args: KeyFulfilmentData,
): Promise<{ error?: AddEventResponse_Error }> {
log('sendKeyFulfillment', args)
return Promise.resolve()
return Promise.resolve({})
}

public encryptAndShareGroupSessions(args: GroupSessionsData): Promise<void> {
Expand Down Expand Up @@ -405,8 +411,10 @@ class MockGroupEncryptionClient
return Promise.resolve()
}

public sendKeyFulfillment(_args: KeyFulfilmentData): Promise<void> {
return Promise.resolve()
public sendKeyFulfillment(
_args: KeyFulfilmentData,
): Promise<{ error?: AddEventResponse_Error }> {
return Promise.resolve({})
}

public uploadDeviceKeys(): Promise<void> {
Expand Down
13 changes: 10 additions & 3 deletions core/sdk/src/clientDecryptionExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
KeySolicitationData,
UserDevice,
} from '@river-build/encryption'
import { EncryptedData, UserInboxPayload_GroupEncryptionSessions } from '@river-build/proto'
import {
AddEventResponse_Error,
EncryptedData,
UserInboxPayload_GroupEncryptionSessions,
} from '@river-build/proto'
import { make_MemberPayload_KeyFulfillment, make_MemberPayload_KeySolicitation } from './types'

import { Client } from './client'
Expand Down Expand Up @@ -231,14 +235,17 @@ export class ClientDecryptionExtensions extends BaseDecryptionExtensions {
userAddress,
deviceKey,
sessionIds,
}: KeyFulfilmentData): Promise<void> {
}: KeyFulfilmentData): Promise<{ error?: AddEventResponse_Error }> {
const fulfillment = make_MemberPayload_KeyFulfillment({
userAddress: userAddress,
deviceKey: deviceKey,
sessionIds: sessionIds,
})

await this.client.makeEventAndAddToStream(streamId, fulfillment)
const { error } = await this.client.makeEventAndAddToStream(streamId, fulfillment, {
optional: true,
})
return { error }
}

public async uploadDeviceKeys(): Promise<void> {
Expand Down

0 comments on commit e36bbfe

Please sign in to comment.