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.
session-ios/SessionSnodeKit/Models/SendMessageResponse.swift

131 lines
4.3 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import SessionUtilitiesKit
public final class SendMessagesResponse: SnodeRecursiveResponse<SendMessagesResponse.SwarmItem> {
private enum CodingKeys: String, CodingKey {
case hash
case swarm
}
public let hash: String
// MARK: - Initialization
internal init(
hash: String,
swarm: [String: SwarmItem],
hardFork: [Int],
timeOffset: Int64
) {
self.hash = hash
super.init(
swarm: swarm,
hardFork: hardFork,
timeOffset: timeOffset
)
}
required init(from decoder: Decoder) throws {
let container: KeyedDecodingContainer<CodingKeys> = try decoder.container(keyedBy: CodingKeys.self)
hash = try container.decode(String.self, forKey: .hash)
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(hash, forKey: .hash)
try super.encode(to: encoder)
}
}
// MARK: - SwarmItem
public extension SendMessagesResponse {
class SwarmItem: SnodeSwarmItem {
private enum CodingKeys: String, CodingKey {
case hash
case already
}
public let hash: String?
/// `true` if a message with this hash was already stored
///
/// **Note:** The `hash` is still included and signed even if this occurs
public let already: Bool
// MARK: - Initialization
required init(from decoder: Decoder) throws {
let container: KeyedDecodingContainer<CodingKeys> = try decoder.container(keyedBy: CodingKeys.self)
hash = try container.decodeIfPresent(String.self, forKey: .hash)
already = ((try? container.decode(Bool.self, forKey: .already)) ?? false)
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.encodeIfPresent(hash, forKey: .hash)
try container.encode(already, forKey: .already)
try super.encode(to: encoder)
}
}
}
// MARK: - ValidatableResponse
extension SendMessagesResponse: ValidatableResponse {
typealias ValidationData = Void
typealias ValidationResponse = Bool
/// Half of the responses in the swarm must be valid
internal static var requiredSuccessfulResponses: Int { -2 }
internal func validResultMap(
Merge remote-tracking branch 'upstream/dev' into feature/groups-rebuild # Conflicts: # LibSession-Util # Podfile # Podfile.lock # Session.xcodeproj/project.pbxproj # Session.xcodeproj/xcshareddata/xcschemes/SessionSnodeKit.xcscheme # Session/Calls/Call Management/SessionCallManager.swift # Session/Closed Groups/EditClosedGroupVC.swift # Session/Closed Groups/NewClosedGroupVC.swift # Session/Conversations/ConversationVC+Interaction.swift # Session/Conversations/ConversationVC.swift # Session/Conversations/ConversationViewModel.swift # Session/Conversations/Settings/ThreadDisappearingMessagesSettingsViewModel.swift # Session/Conversations/Settings/ThreadSettingsViewModel.swift # Session/Home/New Conversation/NewDMVC.swift # Session/Media Viewing & Editing/GIFs/GiphyDownloader.swift # Session/Meta/AppDelegate.swift # Session/Meta/SessionApp.swift # Session/Notifications/SyncPushTokensJob.swift # Session/Notifications/UserNotificationsAdaptee.swift # Session/Onboarding/Onboarding.swift # Session/Path/PathStatusView.swift # Session/Path/PathVC.swift # Session/Settings/NukeDataModal.swift # Session/Utilities/BackgroundPoller.swift # Session/Utilities/IP2Country.swift # SessionMessagingKit/Database/Migrations/_014_GenerateInitialUserConfigDumps.swift # SessionMessagingKit/Database/Migrations/_015_BlockCommunityMessageRequests.swift # SessionMessagingKit/Database/Migrations/_018_DisappearingMessagesConfiguration.swift # SessionMessagingKit/Database/Models/Attachment.swift # SessionMessagingKit/Database/Models/ClosedGroup.swift # SessionMessagingKit/Database/Models/ConfigDump.swift # SessionMessagingKit/Database/Models/DisappearingMessageConfiguration.swift # SessionMessagingKit/Database/Models/Interaction.swift # SessionMessagingKit/Database/Models/SessionThread.swift # SessionMessagingKit/File Server/FileServerAPI.swift # SessionMessagingKit/Jobs/AttachmentDownloadJob.swift # SessionMessagingKit/Jobs/ConfigMessageReceiveJob.swift # SessionMessagingKit/Jobs/ConfigurationSyncJob.swift # SessionMessagingKit/Jobs/ExpirationUpdateJob.swift # SessionMessagingKit/Jobs/GetExpirationJob.swift # SessionMessagingKit/Jobs/MessageSendJob.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+Contacts.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+ConvoInfoVolatile.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+Shared.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+UserGroups.swift # SessionMessagingKit/LibSession/Config Handling/LibSession+UserProfile.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupInfo.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupKeys.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+GroupMembers.swift # SessionMessagingKit/LibSession/Config Handling/SessionUtil+SharedGroup.swift # SessionMessagingKit/LibSession/Database/QueryInterfaceRequest+Utilities.swift # SessionMessagingKit/LibSession/Database/Setting+Utilities.swift # SessionMessagingKit/LibSession/LibSession+SessionMessagingKit.swift # SessionMessagingKit/Messages/Message+Origin.swift # SessionMessagingKit/Messages/Message.swift # SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift # SessionMessagingKit/Open Groups/Models/SOGSMessage.swift # SessionMessagingKit/Open Groups/OpenGroupAPI.swift # SessionMessagingKit/Open Groups/OpenGroupManager.swift # SessionMessagingKit/Open Groups/OpenGroupServerIdLookup.swift # SessionMessagingKit/Open Groups/Types/Request+OpenGroupAPI.swift # SessionMessagingKit/Open Groups/Types/SOGSEndpoint.swift # SessionMessagingKit/Protos/Generated/SNProto.swift # SessionMessagingKit/Protos/Generated/SessionProtos.pb.swift # SessionMessagingKit/Protos/SessionProtos.proto # SessionMessagingKit/Sending & Receiving/Errors/MessageSenderError.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ExpirationTimers.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+UnsendRequests.swift # SessionMessagingKit/Sending & Receiving/MessageReceiver.swift # SessionMessagingKit/Sending & Receiving/MessageSender.swift # SessionMessagingKit/Sending & Receiving/Notifications/Models/PushNotificationAPIRequest.swift # SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift # SessionMessagingKit/Sending & Receiving/Notifications/Types/Request+PushNotificationAPI.swift # SessionMessagingKit/Sending & Receiving/Pollers/CurrentUserPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/GroupPoller.swift # SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupAPI+Poller.swift # SessionMessagingKit/Sending & Receiving/Pollers/Poller.swift # SessionMessagingKit/SessionUtil/SessionUtilError.swift # SessionMessagingKit/SessionUtil/Utilities/TypeConversion+Utilities.swift # SessionMessagingKit/Shared Models/MessageViewModel.swift # SessionMessagingKit/Shared Models/SessionThreadViewModel.swift # SessionMessagingKit/Utilities/ProfileManager.swift # SessionMessagingKitTests/LibSession/LibSessionSpec.swift # SessionMessagingKitTests/LibSession/LibSessionUtilSpec.swift # SessionMessagingKitTests/Open Groups/Models/SOGSMessageSpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupAPISpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupManagerSpec.swift # SessionNotificationServiceExtension/NotificationServiceExtension.swift # SessionShareExtension/ShareNavController.swift # SessionShareExtension/ThreadPickerVC.swift # SessionSnodeKit/Configuration.swift # SessionSnodeKit/Database/Migrations/_001_InitialSetupMigration.swift # SessionSnodeKit/Database/Models/Snode.swift # SessionSnodeKit/Database/Models/SnodeReceivedMessageInfo.swift # SessionSnodeKit/Database/Models/SnodeSet.swift # SessionSnodeKit/Jobs/GetSnodePoolJob.swift # SessionSnodeKit/Models/DeleteAllBeforeRequest.swift # SessionSnodeKit/Models/DeleteAllMessagesRequest.swift # SessionSnodeKit/Models/DeleteMessagesRequest.swift # SessionSnodeKit/Models/GetExpiriesRequest.swift # SessionSnodeKit/Models/GetMessagesRequest.swift # SessionSnodeKit/Models/ONSResolveResponse.swift # SessionSnodeKit/Models/RevokeSubkeyRequest.swift # SessionSnodeKit/Models/SendMessageRequest.swift # SessionSnodeKit/Models/SnodeAuthenticatedRequestBody.swift # SessionSnodeKit/Models/SnodeRequest.swift # SessionSnodeKit/Models/SwarmSnode.swift # SessionSnodeKit/Models/UpdateExpiryAllRequest.swift # SessionSnodeKit/Models/UpdateExpiryRequest.swift # SessionSnodeKit/Networking/OnionRequestAPI.swift # SessionSnodeKit/Networking/PreparedRequest+OnionRequest.swift # SessionSnodeKit/Networking/Request+SnodeAPI.swift # SessionSnodeKit/Networking/SnodeAPI.swift # SessionSnodeKit/Types/OnionRequestAPIError.swift # SessionSnodeKit/Types/SnodeAPIEndpoint.swift # SessionSnodeKit/Types/SnodeAPIError.swift # SessionSnodeKit/Types/SnodeAPINamespace.swift # SessionSnodeKit/Types/SwarmDrainBehaviour.swift # SessionSnodeKitTests/Models/SnodeRequestSpec.swift # SessionTests/Database/DatabaseSpec.swift # SessionUIKit/Style Guide/Values.swift # SessionUtilitiesKit/Database/Migrations/_005_AddJobUniqueHash.swift # SessionUtilitiesKit/Database/Models/Job.swift # SessionUtilitiesKit/Database/Types/Migration.swift # SessionUtilitiesKit/General/Data+Utilities.swift # SessionUtilitiesKit/General/Dependencies.swift # SessionUtilitiesKit/General/Features.swift # SessionUtilitiesKit/General/Logging.swift # SessionUtilitiesKit/JobRunner/JobRunner.swift # SessionUtilitiesKit/LibSession/Utilities/Crypto+SessionUtil.swift # SessionUtilitiesKit/LibSession/Utilities/TypeConversion+Utilities.swift # SessionUtilitiesKit/Networking/BatchRequest.swift # SessionUtilitiesKit/Networking/BatchResponse.swift # SessionUtilitiesKit/Networking/HTTP.swift # SessionUtilitiesKit/Networking/HTTPError.swift # SessionUtilitiesKit/Networking/PreparedRequest.swift # SessionUtilitiesKit/Networking/Request.swift # SessionUtilitiesKit/Networking/RequestTarget.swift # SessionUtilitiesKit/SessionUtil/Utilities/TypeConversion+Utilities.swift # SessionUtilitiesKit/Utilities/Bencode.swift # SessionUtilitiesKit/Utilities/JSONEncoder+Utilities.swift # SessionUtilitiesKitTests/JobRunner/JobRunnerSpec.swift # SessionUtilitiesKitTests/Networking/BatchRequestSpec.swift # SessionUtilitiesKitTests/Networking/BatchResponseSpec.swift # SessionUtilitiesKitTests/Networking/PreparedRequestSpec.swift # SessionUtilitiesKitTests/Networking/RequestSpec.swift # SessionUtilitiesKitTests/Utilities/BencodeResponseSpec.swift # SignalUtilitiesKit/Configuration.swift # SignalUtilitiesKit/Utilities/AppSetup.swift # SignalUtilitiesKit/Utilities/Bench.swift # SignalUtilitiesKit/Utilities/UIGestureRecognizer+OWS.swift # _SharedTestUtilities/CommonMockedExtensions.swift # _SharedTestUtilities/MockJobRunner.swift # _SharedTestUtilities/Mocked.swift
11 months ago
swarmPublicKey: String,
validationData: Void,
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),
let hash: String = next.value.hash
else {
result[next.key] = false
if let reason: String = next.value.reason, let statusCode: Int = next.value.code {
Log.warn(.validator(self), "Couldn't store message on: \(next.key) due to error: \(reason) (\(statusCode)).")
}
else {
Log.warn(.validator(self), "Couldn't store message on: \(next.key).")
}
return
}
/// Signature of `hash` signed by the node's ed25519 pubkey
let verificationBytes: [UInt8] = hash.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)
}
}