Skip to content

Commit 7eea9fa

Browse files
committed
Merge pull-request #23
2 parents 6555f78 + 7ed7cef commit 7eea9fa

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed
Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
import Foundation
22

3-
/// Tracks generated but unused public keys along with their creation timestamps.
3+
/// Tracks generated but unused public keys along with their expiration timestamps.
44
/// This is used to clean up stale key material that was never used to establish a session.
55
enum PendingKeysStore {
6-
private static let storeKey = Constants.Storage.pendingKeysStoreKey
7-
private static let secureAccount = Constants.Storage.secureAccount
8-
private static let q = DispatchQueue(label: "pendingKeys", attributes: .concurrent)
9-
10-
static func add(_ pub: String) throws {
11-
try q.sync(flags: .barrier) {
12-
var dict = (try? LocalStore.get(storeKey) as [String: Date]?) ?? [:]
13-
dict[pub] = Date()
14-
try LocalStore.set(dict, for: storeKey)
6+
private static let storeKey = Constants.Storage.pendingKeysStoreKey
7+
private static let secureAccount = Constants.Storage.secureAccount
8+
private static let q = DispatchQueue(label: "pendingKeys", attributes: .concurrent)
9+
10+
static func add(_ pub: String, ttlHours: Double = 1) throws {
11+
try q.sync(flags: .barrier) {
12+
var dict = (try? LocalStore.get(storeKey) as [String: TimeInterval]?) ?? [:]
13+
let expiry = Date().addingTimeInterval(ttlHours * 3600).timeIntervalSince1970
14+
dict[pub] = expiry
15+
try LocalStore.set(dict, for: storeKey)
16+
}
1517
}
16-
}
17-
18-
static func remove(_ pub: String) throws {
19-
try q.sync(flags: .barrier) {
20-
var dict = (try? LocalStore.get(storeKey) as [String: Date]?) ?? [:]
21-
dict.removeValue(forKey: pub)
22-
try LocalStore.set(dict, for: storeKey)
18+
19+
static func remove(_ pub: String) throws {
20+
try q.sync(flags: .barrier) {
21+
var dict = (try? LocalStore.get(storeKey) as [String: TimeInterval]?) ?? [:]
22+
dict.removeValue(forKey: pub)
23+
try LocalStore.set(dict, for: storeKey)
24+
}
2325
}
24-
}
25-
26-
static func all() -> [String: Date] {
27-
q.sync { (try? LocalStore.get(storeKey) as [String: Date]?) ?? [:] }
28-
}
29-
30-
static func purge(ttlHours: Double = 24) {
31-
let cutoff = Date().addingTimeInterval(-ttlHours * 3600)
32-
for (pub, createdAt) in all() where createdAt < cutoff {
33-
do {
34-
try SecureStore.delete(service: pub, account: secureAccount)
35-
try remove(pub)
36-
} catch {
37-
print("PendingKeysStore purge error for \(pub): \(error)")
38-
}
26+
27+
static func all() -> [String: TimeInterval] {
28+
q.sync { (try? LocalStore.get(storeKey) as [String: TimeInterval]?) ?? [:] }
29+
}
30+
31+
static func purge() {
32+
let now = Date().timeIntervalSince1970
33+
for (pub, expiry) in all() where expiry < now {
34+
do {
35+
try SecureStore.delete(service: pub, account: secureAccount)
36+
try remove(pub)
37+
} catch {
38+
print("PendingKeysStore purge error for \(pub): \(error)")
39+
}
40+
}
3941
}
40-
}
4142
}

Sources/TurnkeySwift/Public/TurnkeyContext+Session.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ extension TurnkeyContext {
4040
throw TurnkeySwiftError.keyNotFound
4141
}
4242

43+
try PendingKeysStore.remove(dto.publicKey)
44+
4345
if selectedSessionKey == nil {
4446
_ = try await setSelectedSession(sessionKey: sessionKey)
4547
}

Sources/TurnkeySwift/Public/TurnkeyContext.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final class TurnkeyContext: NSObject, ObservableObject {
4040

4141
private func postInitSetup() {
4242
// clean up expired sessions and pending keys
43-
PendingKeysStore.purge(ttlHours: 2)
43+
PendingKeysStore.purge()
4444
SessionRegistryStore.purgeExpiredSessions()
4545

4646
// restore session and timers after launch
@@ -53,7 +53,7 @@ public final class TurnkeyContext: NSObject, ObservableObject {
5353
if let note = Self.foregroundNotification {
5454
Task.detached {
5555
for await _ in NotificationCenter.default.notifications(named: note) {
56-
PendingKeysStore.purge(ttlHours: 1)
56+
PendingKeysStore.purge()
5757
SessionRegistryStore.purgeExpiredSessions()
5858
}
5959
}

0 commit comments

Comments
 (0)