diff --git a/SignalServiceKit/src/Loki/LokiMessage.swift b/SignalServiceKit/src/Loki/LokiMessage.swift index 2468dbab9..63285f7a0 100644 --- a/SignalServiceKit/src/Loki/LokiMessage.swift +++ b/SignalServiceKit/src/Loki/LokiMessage.swift @@ -28,7 +28,7 @@ public struct LokiMessage { let ttl = LokiMessagingAPI.defaultTTL if isPoWRequired { let timestamp = UInt64(Date().timeIntervalSince1970) - if let nonce = ProofOfWork.calculate(data: data, pubKey: destination, timestamp: timestamp, ttl: Int(ttl)) { + if let nonce = ProofOfWork.calculate(data: data, pubKey: destination, timestamp: timestamp, ttl: ttl) { let result = LokiMessage(destination: destination, data: data, ttl: ttl, timestamp: timestamp, nonce: nonce) seal.fulfill(result) } else { diff --git a/SignalServiceKit/src/Loki/ProofOfWork.swift b/SignalServiceKit/src/Loki/ProofOfWork.swift index 4a8da86b5..25e3014d8 100644 --- a/SignalServiceKit/src/Loki/ProofOfWork.swift +++ b/SignalServiceKit/src/Loki/ProofOfWork.swift @@ -66,9 +66,9 @@ private extension MutableCollection where Element == UInt8, Index == Int { /// - pubKey: The message recipient /// - timestamp: The timestamp /// - ttl: The message time to live - /// - Returns: A nonce string or nil if it failed - @objc public static func calculate(data: String, pubKey: String, timestamp: UInt64, ttl: Int) -> String? { - let payload = getPayload(pubKey: pubKey, data: data, timestamp: timestamp, ttl: ttl) + /// - Returns: A nonce string or `nil` if it failed + @objc public static func calculate(data: String, pubKey: String, timestamp: UInt64, ttl: UInt64) -> String? { + let payload = createPayload(pubKey: pubKey, data: data, timestamp: timestamp, ttl: ttl) let target = calcTarget(ttl: ttl, payloadLength: payload.count, nonceTrials: nonceTrialCount) // Start with the max value @@ -91,7 +91,7 @@ private extension MutableCollection where Element == UInt8, Index == Int { } /// Get the proof of work payload - private static func getPayload(pubKey: String, data: String, timestamp: UInt64, ttl: Int) -> [UInt8] { + private static func createPayload(pubKey: String, data: String, timestamp: UInt64, ttl: UInt64) -> [UInt8] { let timestampString = String(timestamp) let ttlString = String(ttl) let payloadString = timestampString + ttlString + pubKey + data @@ -99,16 +99,13 @@ private extension MutableCollection where Element == UInt8, Index == Int { } /// Calculate the target we need to reach - private static func calcTarget(ttl: Int, payloadLength: Int, nonceTrials: Int) -> UInt64 { + private static func calcTarget(ttl: UInt64, payloadLength: Int, nonceTrials: Int) -> UInt64 { let two16 = UInt64(pow(2, 16) - 1) let two64 = UInt64(pow(2, 64) - 1) - - // ttl converted to seconds - let ttlSeconds = ttl / 1000 // Do all the calculations let totalLength = UInt64(payloadLength + nonceLength) - let ttlMult = UInt64(ttlSeconds) * totalLength + let ttlMult = ttl * totalLength // UInt64 values let innerFrac = ttlMult / two16