Skip to content

Commit

Permalink
fix: add loading session for each encryption - improve encrypt func (#6)
Browse files Browse the repository at this point in the history
* fix: add loading session for each encryption - improve encrypt func

fix issues caused by caching outdated session

* test: add tests for situation when session does not exist
  • Loading branch information
AndriyVasyk authored Feb 21, 2024
1 parent 17c6921 commit b7d4bbb
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 257 deletions.
25 changes: 14 additions & 11 deletions Sources/DXProtocol/Session/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,24 @@ public struct Session: Codable {
/// - identityStore: The identity store.
///
/// - Returns: The encrypted message container.
public mutating func encrypt(data: Data,
for address: ProtocolAddress,
sessionStore: SessionStorable,
identityStore: IdentityKeyStorable) throws -> MessageContainer {
public static func encrypt(data: Data,
for address: ProtocolAddress,
sessionStore: SessionStorable,
identityStore: IdentityKeyStorable) throws -> MessageContainer {
let lock = SessionLock(address: address)
lock.lock()
defer { lock.unlock() }

let result = try self.state.encrypt(
data: data,
sessionStore: sessionStore,
identityStore: identityStore)
try sessionStore.storeSession(self, for: address)

guard var session = try sessionStore.loadSession(for: address) else {
throw DXError.sessionNotFound("Failed to find session while encrypting message")
}

let result = try session.state.encrypt(
data: data,
sessionStore: sessionStore,
identityStore: identityStore)
try sessionStore.storeSession(session, for: address)

return result
}

Expand Down Expand Up @@ -367,7 +371,6 @@ extension Session {
lock.lock()
defer { lock.unlock() }

// This code is not covered by tests
guard var session = try sessionStore.loadSession(for: address) else {
throw DXError.sessionNotFound("Failed to find session while decrypting message")
}
Expand Down
Loading

0 comments on commit b7d4bbb

Please sign in to comment.