|
|
@ -44,7 +44,6 @@ private extension MutableCollection where Element == UInt8, Index == Int {
|
|
|
|
* Ref: libloki/proof-of-work.js
|
|
|
|
* Ref: libloki/proof-of-work.js
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@objc public class ProofOfWork : NSObject {
|
|
|
|
@objc public class ProofOfWork : NSObject {
|
|
|
|
private override init() {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If this changes then we also have to use something other than UInt64 to support the new length
|
|
|
|
// If this changes then we also have to use something other than UInt64 to support the new length
|
|
|
|
private static let nonceLength = 8
|
|
|
|
private static let nonceLength = 8
|
|
|
@ -56,6 +55,8 @@ private extension MutableCollection where Element == UInt8, Index == Int {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private override init() { }
|
|
|
|
|
|
|
|
|
|
|
|
/// Calculate a proof of work with the given configuration
|
|
|
|
/// Calculate a proof of work with the given configuration
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Ref: https://bitmessage.org/wiki/Proof_of_work
|
|
|
|
/// Ref: https://bitmessage.org/wiki/Proof_of_work
|
|
|
@ -66,7 +67,7 @@ private extension MutableCollection where Element == UInt8, Index == Int {
|
|
|
|
/// - timestamp: The timestamp
|
|
|
|
/// - timestamp: The timestamp
|
|
|
|
/// - ttl: The message time to live
|
|
|
|
/// - ttl: The message time to live
|
|
|
|
/// - Returns: A nonce string or nil if it failed
|
|
|
|
/// - Returns: A nonce string or nil if it failed
|
|
|
|
@objc public class func calculate(forData data: String, pubKey: String, timestamp: UInt64, ttl: Int) -> String? {
|
|
|
|
@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)
|
|
|
|
let payload = getPayload(pubKey: pubKey, data: data, timestamp: timestamp, ttl: ttl)
|
|
|
|
let target = calcTarget(ttl: ttl, payloadLength: payload.count, nonceTrials: nonceTrialCount)
|
|
|
|
let target = calcTarget(ttl: ttl, payloadLength: payload.count, nonceTrials: nonceTrialCount)
|
|
|
|
|
|
|
|
|
|
|
@ -90,7 +91,7 @@ private extension MutableCollection where Element == UInt8, Index == Int {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Get the proof of work payload
|
|
|
|
/// Get the proof of work payload
|
|
|
|
private class func getPayload(pubKey: String, data: String, timestamp: UInt64, ttl: Int) -> [UInt8] {
|
|
|
|
private static func getPayload(pubKey: String, data: String, timestamp: UInt64, ttl: Int) -> [UInt8] {
|
|
|
|
let timestampString = String(timestamp)
|
|
|
|
let timestampString = String(timestamp)
|
|
|
|
let ttlString = String(ttl)
|
|
|
|
let ttlString = String(ttl)
|
|
|
|
let payloadString = timestampString + ttlString + pubKey + data
|
|
|
|
let payloadString = timestampString + ttlString + pubKey + data
|
|
|
@ -98,7 +99,7 @@ private extension MutableCollection where Element == UInt8, Index == Int {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Calculate the target we need to reach
|
|
|
|
/// Calculate the target we need to reach
|
|
|
|
private class func calcTarget(ttl: Int, payloadLength: Int, nonceTrials: Int) -> UInt64 {
|
|
|
|
private static func calcTarget(ttl: Int, payloadLength: Int, nonceTrials: Int) -> UInt64 {
|
|
|
|
let two16 = UInt64(pow(2, 16) - 1)
|
|
|
|
let two16 = UInt64(pow(2, 16) - 1)
|
|
|
|
let two64 = UInt64(pow(2, 64) - 1)
|
|
|
|
let two64 = UInt64(pow(2, 64) - 1)
|
|
|
|
|
|
|
|
|
|
|
|