|
|
@ -12,10 +12,12 @@ public extension UpdateExpiryResponse {
|
|
|
|
class SwarmItem: SnodeSwarmItem {
|
|
|
|
class SwarmItem: SnodeSwarmItem {
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
|
|
case updated
|
|
|
|
case updated
|
|
|
|
|
|
|
|
case unchanged
|
|
|
|
case expiry
|
|
|
|
case expiry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public let updated: [String]
|
|
|
|
public let updated: [String]
|
|
|
|
|
|
|
|
public let unchanged: [String: UInt64]
|
|
|
|
public let expiry: UInt64?
|
|
|
|
public let expiry: UInt64?
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Initialization
|
|
|
|
// MARK: - Initialization
|
|
|
@ -24,6 +26,7 @@ public extension UpdateExpiryResponse {
|
|
|
|
let container: KeyedDecodingContainer<CodingKeys> = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
let container: KeyedDecodingContainer<CodingKeys> = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
|
|
|
|
|
|
|
updated = ((try? container.decode([String].self, forKey: .updated)) ?? [])
|
|
|
|
updated = ((try? container.decode([String].self, forKey: .updated)) ?? [])
|
|
|
|
|
|
|
|
unchanged = ((try? container.decode([String: UInt64].self, forKey: .unchanged)) ?? [:])
|
|
|
|
expiry = try? container.decode(UInt64.self, forKey: .expiry)
|
|
|
|
expiry = try? container.decode(UInt64.self, forKey: .expiry)
|
|
|
|
|
|
|
|
|
|
|
|
try super.init(from: decoder)
|
|
|
|
try super.init(from: decoder)
|
|
|
@ -35,7 +38,7 @@ public extension UpdateExpiryResponse {
|
|
|
|
|
|
|
|
|
|
|
|
extension UpdateExpiryResponse: ValidatableResponse {
|
|
|
|
extension UpdateExpiryResponse: ValidatableResponse {
|
|
|
|
typealias ValidationData = [String]
|
|
|
|
typealias ValidationData = [String]
|
|
|
|
typealias ValidationResponse = (hashes: [String], expiry: UInt64)
|
|
|
|
typealias ValidationResponse = [(hash: String, expiry: UInt64)]
|
|
|
|
|
|
|
|
|
|
|
|
/// All responses in the swarm must be valid
|
|
|
|
/// All responses in the swarm must be valid
|
|
|
|
internal static var requiredSuccessfulResponses: Int { -1 }
|
|
|
|
internal static var requiredSuccessfulResponses: Int { -1 }
|
|
|
@ -44,14 +47,15 @@ extension UpdateExpiryResponse: ValidatableResponse {
|
|
|
|
sodium: Sodium,
|
|
|
|
sodium: Sodium,
|
|
|
|
userX25519PublicKey: String,
|
|
|
|
userX25519PublicKey: String,
|
|
|
|
validationData: [String]
|
|
|
|
validationData: [String]
|
|
|
|
) throws -> [String: (hashes: [String], expiry: UInt64)] {
|
|
|
|
) throws -> [String: [(hash: String, expiry: UInt64)]] {
|
|
|
|
let validationMap: [String: (hashes: [String], expiry: UInt64)] = try swarm.reduce(into: [:]) { result, next in
|
|
|
|
let validationMap: [String: [(hash: String, expiry: UInt64)]] = try swarm.reduce(into: [:]) { result, next in
|
|
|
|
guard
|
|
|
|
guard
|
|
|
|
!next.value.failed,
|
|
|
|
!next.value.failed,
|
|
|
|
|
|
|
|
let appliedExpiry: UInt64 = next.value.expiry,
|
|
|
|
let signatureBase64: String = next.value.signatureBase64,
|
|
|
|
let signatureBase64: String = next.value.signatureBase64,
|
|
|
|
let encodedSignature: Data = Data(base64Encoded: signatureBase64)
|
|
|
|
let encodedSignature: Data = Data(base64Encoded: signatureBase64)
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
result[next.key] = ([], 0)
|
|
|
|
result[next.key] = []
|
|
|
|
|
|
|
|
|
|
|
|
if let reason: String = next.value.reason, let statusCode: Int = next.value.code {
|
|
|
|
if let reason: String = next.value.reason, let statusCode: Int = next.value.code {
|
|
|
|
SNLog("Couldn't update expiry from: \(next.key) due to error: \(reason) (\(statusCode)).")
|
|
|
|
SNLog("Couldn't update expiry from: \(next.key) due to error: \(reason) (\(statusCode)).")
|
|
|
@ -63,13 +67,24 @@ extension UpdateExpiryResponse: ValidatableResponse {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Signature of
|
|
|
|
/// Signature of
|
|
|
|
/// `( PUBKEY_HEX || EXPIRY || RMSG[0] || ... || RMSG[N] || UMSG[0] || ... || UMSG[M] )`
|
|
|
|
/// `( PUBKEY_HEX || EXPIRY || RMSGs... || UMSGs... || CMSG_EXPs... )`
|
|
|
|
/// where RMSG are the requested expiry hashes and UMSG are the actual updated hashes. The signature uses
|
|
|
|
/// where RMSGs are the requested expiry hashes, UMSGs are the actual updated hashes, and
|
|
|
|
/// the node's ed25519 pubkey.
|
|
|
|
/// CMSG_EXPs are (HASH || EXPIRY) values, ascii-sorted by hash, for the unchanged message
|
|
|
|
|
|
|
|
/// hashes included in the "unchanged" field. The signature uses the node's ed25519 pubkey.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// **Note:** If `updated` is empty then the `expiry` value will match the value that was
|
|
|
|
|
|
|
|
/// included in the original request
|
|
|
|
let verificationBytes: [UInt8] = userX25519PublicKey.bytes
|
|
|
|
let verificationBytes: [UInt8] = userX25519PublicKey.bytes
|
|
|
|
.appending(contentsOf: "\(String(describing: next.value.expiry))".data(using: .ascii)?.bytes)
|
|
|
|
.appending(contentsOf: "\(appliedExpiry)".data(using: .ascii)?.bytes)
|
|
|
|
.appending(contentsOf: validationData.joined().bytes)
|
|
|
|
.appending(contentsOf: validationData.joined().bytes)
|
|
|
|
.appending(contentsOf: next.value.updated.joined().bytes)
|
|
|
|
.appending(contentsOf: next.value.updated.sorted().joined().bytes)
|
|
|
|
|
|
|
|
.appending(contentsOf: next.value.unchanged
|
|
|
|
|
|
|
|
.sorted(by: { lhs, rhs in lhs.key < rhs.key })
|
|
|
|
|
|
|
|
.reduce(into: [UInt8]()) { result, nextUnchanged in
|
|
|
|
|
|
|
|
result.append(contentsOf: nextUnchanged.key.bytes)
|
|
|
|
|
|
|
|
result.append(contentsOf: "\(nextUnchanged.value)".data(using: .ascii)?.bytes ?? [])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
let isValid: Bool = sodium.sign.verify(
|
|
|
|
let isValid: Bool = sodium.sign.verify(
|
|
|
|
message: verificationBytes,
|
|
|
|
message: verificationBytes,
|
|
|
|
publicKey: Data(hex: next.key).bytes,
|
|
|
|
publicKey: Data(hex: next.key).bytes,
|
|
|
@ -79,11 +94,9 @@ extension UpdateExpiryResponse: ValidatableResponse {
|
|
|
|
// If the update signature is invalid then we want to fail here
|
|
|
|
// If the update signature is invalid then we want to fail here
|
|
|
|
guard isValid else { throw SnodeAPIError.signatureVerificationFailed }
|
|
|
|
guard isValid else { throw SnodeAPIError.signatureVerificationFailed }
|
|
|
|
|
|
|
|
|
|
|
|
// If we didn't get an `expiry` value from the snode then don't bother adding it to the result
|
|
|
|
result[next.key] = next.value.updated
|
|
|
|
// as it's not valid data
|
|
|
|
.map { ($0, appliedExpiry) }
|
|
|
|
guard let expiry: UInt64 = next.value.expiry else { return }
|
|
|
|
.appending(contentsOf: next.value.unchanged.map { ($0.key, $0.value) })
|
|
|
|
|
|
|
|
|
|
|
|
result[next.key] = (hashes: next.value.updated, expiry: expiry)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return try Self.validated(map: validationMap, totalResponseCount: swarm.count)
|
|
|
|
return try Self.validated(map: validationMap, totalResponseCount: swarm.count)
|
|
|
|