Skip to content

Commit

Permalink
Hybrid group encryption fixes (#2075)
Browse files Browse the repository at this point in the history
1. Use miniblockInfo.max instead of .min. this was a typo
2. Only send one key fulfillment. For the case of isNewDevice, any
subsequent key fulfillment will get rejected, and this is more effecient
all around.
  • Loading branch information
texuf authored Jan 17, 2025
1 parent a7e1f7c commit 20aec6e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
42 changes: 24 additions & 18 deletions packages/encryption/src/decryptionExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,26 @@ export abstract class BaseDecryptionExtensions {
if (allSessions.length === 0) {
return
}
// send a single key fulfillment for all algorithms
const { error } = await this.sendKeyFulfillment({
streamId,
userAddress: item.fromUserAddress,
deviceKey: item.solicitation.deviceKey,
sessionIds: item.solicitation.isNewDevice
? []
: allSessions.map((x) => x.sessionId).sort(),
})

// if the key fulfillment failed, someone else already sent a key fulfillment
if (error) {
if (!error.msg.includes('DUPLICATE_EVENT')) {
// duplicate events are expected, we can ignore them, others are not
this.log.error('failed to send key fulfillment', error)
}
return
}

// if the key fulfillment succeeded, send one group session payload for each algorithm
const sessions = allSessions.reduce((acc, session) => {
if (!acc[session.algorithm]) {
acc[session.algorithm] = []
Expand All @@ -766,26 +785,13 @@ export abstract class BaseDecryptionExtensions {
for (const kv of Object.entries(sessions)) {
const algorithm = kv[0] as GroupEncryptionAlgorithmId
const sessions = kv[1]
const { error } = await this.sendKeyFulfillment({

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

if (!error) {
await this.encryptAndShareGroupSessions({
streamId,
item,
sessions,
algorithm,
})
} else if (!error.msg.includes('DUPLICATE_EVENT')) {
// duplicate events are expected, we can ignore them, others are not
this.log.error('failed to send key fulfillment', error)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2181,7 +2181,7 @@ export class Client
check(isDefined(streamView.miniblockInfo), `stream not initialized: ${streamId}`)
check(isDefined(streamView.prevMiniblockHash), `prevMiniblockHash not found: ${streamId}`)
return {
miniblockNum: streamView.miniblockInfo.min,
miniblockNum: streamView.miniblockInfo.max,
miniblockHash: streamView.prevMiniblockHash,
}
}
Expand Down

0 comments on commit 20aec6e

Please sign in to comment.