-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathWCPairing.swift
66 lines (55 loc) · 1.95 KB
/
WCPairing.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import Foundation
import WalletConnectUtils
public struct WCPairing: SequenceObject {
enum Errors: Error {
case invalidUpdateExpiryValue
}
public let topic: String
public let relay: RelayProtocolOptions
public var peerMetadata: AppMetadata?
public private (set) var expiryDate: Date
public private (set) var active: Bool
#if DEBUG
public static var dateInitializer: () -> Date = Date.init
#else
private static var dateInitializer: () -> Date = Date.init
#endif
public static var timeToLiveInactive: TimeInterval {
5 * .minute
}
public static var timeToLiveActive: TimeInterval {
30 * .day
}
public init(topic: String, relay: RelayProtocolOptions, peerMetadata: AppMetadata, isActive: Bool = false, expiryDate: Date) {
self.topic = topic
self.relay = relay
self.peerMetadata = peerMetadata
self.active = isActive
self.expiryDate = expiryDate
}
public init(topic: String) {
self.topic = topic
self.relay = RelayProtocolOptions(protocol: "irn", data: nil)
self.active = false
self.expiryDate = Self.dateInitializer().advanced(by: Self.timeToLiveInactive)
}
public init(uri: WalletConnectURI) {
self.topic = uri.topic
self.relay = uri.relay
self.active = false
self.expiryDate = Self.dateInitializer().advanced(by: Self.timeToLiveInactive)
}
public mutating func activate() {
active = true
try? updateExpiry()
}
public mutating func updateExpiry(_ ttl: TimeInterval = WCPairing.timeToLiveActive) throws {
let now = Self.dateInitializer()
let newExpiryDate = now.advanced(by: ttl)
let maxExpiryDate = now.advanced(by: Self.timeToLiveActive)
guard newExpiryDate > expiryDate && newExpiryDate <= maxExpiryDate else {
throw Errors.invalidUpdateExpiryValue
}
expiryDate = newExpiryDate
}
}