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.
60 lines
2.2 KiB
Swift
60 lines
2.2 KiB
Swift
2 years ago
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||
|
|
||
|
import Foundation
|
||
|
import Sodium
|
||
|
import SessionUtilitiesKit
|
||
|
|
||
2 years ago
|
public class RevokeSubaccountResponse: SnodeRecursiveResponse<SnodeSwarmItem> {}
|
||
2 years ago
|
|
||
|
// MARK: - ValidatableResponse
|
||
|
|
||
2 years ago
|
extension RevokeSubaccountResponse: ValidatableResponse {
|
||
2 years ago
|
typealias ValidationData = String
|
||
|
typealias ValidationResponse = Bool
|
||
2 years ago
|
|
||
2 years ago
|
/// All responses in the swarm must be valid
|
||
|
internal static var requiredSuccessfulResponses: Int { -1 }
|
||
|
|
||
|
internal func validResultMap(
|
||
2 years ago
|
publicKey: String,
|
||
|
validationData: String,
|
||
|
using dependencies: Dependencies
|
||
2 years ago
|
) throws -> [String: Bool] {
|
||
|
let validationMap: [String: Bool] = try swarm.reduce(into: [:]) { result, next in
|
||
2 years ago
|
guard
|
||
2 years ago
|
!next.value.failed,
|
||
|
let signatureBase64: String = next.value.signatureBase64,
|
||
|
let encodedSignature: Data = Data(base64Encoded: signatureBase64)
|
||
2 years ago
|
else {
|
||
2 years ago
|
if let reason: String = next.value.reason, let statusCode: Int = next.value.code {
|
||
|
SNLog("Couldn't revoke subkey from: \(next.key) due to error: \(reason) (\(statusCode)).")
|
||
2 years ago
|
}
|
||
|
else {
|
||
2 years ago
|
SNLog("Couldn't revoke subkey from: \(next.key).")
|
||
2 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
2 years ago
|
/// Signature of `( PUBKEY_HEX || SUBACCOUNT_TAG_BYTES )` where `SUBACCOUNT_TAG_BYTES` is the
|
||
2 years ago
|
/// requested subkey tag for revocation
|
||
2 years ago
|
let verificationBytes: [UInt8] = publicKey.bytes
|
||
2 years ago
|
.appending(contentsOf: validationData.bytes)
|
||
2 years ago
|
|
||
|
let isValid: Bool = dependencies[singleton: .crypto].verify(
|
||
|
.signature(
|
||
|
message: verificationBytes,
|
||
|
publicKey: Data(hex: next.key).bytes,
|
||
|
signature: encodedSignature.bytes
|
||
|
)
|
||
2 years ago
|
)
|
||
|
|
||
|
// If the update signature is invalid then we want to fail here
|
||
|
guard isValid else { throw SnodeAPIError.signatureVerificationFailed }
|
||
2 years ago
|
|
||
|
result[next.key] = isValid
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
return try Self.validated(map: validationMap)
|
||
2 years ago
|
}
|
||
|
}
|