mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
3.0 KiB
Swift
85 lines
3.0 KiB
Swift
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import Foundation
|
|
import SessionUtilitiesKit
|
|
|
|
public final class DeleteMessagesResponse: SnodeRecursiveResponse<DeleteMessagesResponse.SwarmItem> {}
|
|
|
|
// MARK: - SwarmItem
|
|
|
|
public extension DeleteMessagesResponse {
|
|
class SwarmItem: SnodeSwarmItem {
|
|
private enum CodingKeys: String, CodingKey {
|
|
case deleted
|
|
}
|
|
|
|
public let deleted: [String]
|
|
|
|
// MARK: - Initialization
|
|
|
|
required init(from decoder: Decoder) throws {
|
|
let container: KeyedDecodingContainer<CodingKeys> = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
deleted = ((try? container.decode([String].self, forKey: .deleted)) ?? [])
|
|
|
|
try super.init(from: decoder)
|
|
}
|
|
|
|
public override func encode(to encoder: any Encoder) throws {
|
|
var container: KeyedEncodingContainer<CodingKeys> = encoder.container(keyedBy: CodingKeys.self)
|
|
try container.encode(deleted, forKey: .deleted)
|
|
|
|
try super.encode(to: encoder)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - ValidatableResponse
|
|
|
|
extension DeleteMessagesResponse: ValidatableResponse {
|
|
typealias ValidationData = [String]
|
|
typealias ValidationResponse = Bool
|
|
|
|
/// Just one response in the swarm must be valid
|
|
internal static var requiredSuccessfulResponses: Int { 1 }
|
|
|
|
internal func validResultMap(
|
|
swarmPublicKey: String,
|
|
validationData: [String],
|
|
using dependencies: Dependencies
|
|
) throws -> [String: Bool] {
|
|
let validationMap: [String: Bool] = swarm.reduce(into: [:]) { result, next in
|
|
guard
|
|
!next.value.failed,
|
|
let signatureBase64: String = next.value.signatureBase64,
|
|
let encodedSignature: Data = Data(base64Encoded: signatureBase64)
|
|
else {
|
|
result[next.key] = false
|
|
|
|
if let reason: String = next.value.reason, let statusCode: Int = next.value.code {
|
|
Log.warn("Couldn't delete data from: \(next.key) due to error: \(reason) (\(statusCode)).")
|
|
}
|
|
else {
|
|
Log.warn("Couldn't delete data from: \(next.key).")
|
|
}
|
|
return
|
|
}
|
|
|
|
/// The signature format is `( PUBKEY_HEX || RMSG[0] || ... || RMSG[N] || DMSG[0] || ... || DMSG[M] )`
|
|
let verificationBytes: [UInt8] = swarmPublicKey.bytes
|
|
.appending(contentsOf: validationData.joined().bytes)
|
|
.appending(contentsOf: next.value.deleted.joined().bytes)
|
|
|
|
result[next.key] = dependencies[singleton: .crypto].verify(
|
|
.signature(
|
|
message: verificationBytes,
|
|
publicKey: Data(hex: next.key).bytes,
|
|
signature: encodedSignature.bytes
|
|
)
|
|
)
|
|
}
|
|
|
|
return try Self.validated(map: validationMap)
|
|
}
|
|
}
|