session-ios/SessionUtilitiesKit/General/SessionId.swift
Morgan Pretty 5b6be3912d Fixed an edge-case crash, a couple of minor bugs and made future-proofing tweaks
Fixed a bit of the OnionRequest error handling to better send through server error messages for debugging
Fixed a bug where the initial offset could be negative if the number of messages was less than the page size resulting in a crash
Fixed a crash due to a code path which was thought to be impossible exiting but is actually possible (so just erroring)
Added the 'expire' SnodeAPI endpoint
Removed the 'openGroupServerTimestamp' property (was unused and just added confusion)
Updated the logic to always handle the 'fileId' for uploads/downloads as a string instead of casting it to an Int64
Updated the OpenGroup room parsing to support either Int or String values for image ids
2022-07-12 17:43:52 +10:00

51 lines
1.8 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import Sodium
import Curve25519Kit
public struct SessionId {
public enum Prefix: String, CaseIterable {
case standard = "05" // Used for identified users, open groups, etc.
case blinded = "15" // Used for authentication and participants in open groups with blinding enabled
case unblinded = "00" // Used for authentication in open groups with blinding disabled
public init?(from stringValue: String?) {
guard let stringValue: String = stringValue else { return nil }
guard stringValue.count > 2 else {
guard let targetPrefix: Prefix = Prefix(rawValue: stringValue) else { return nil }
self = targetPrefix
return
}
guard ECKeyPair.isValidHexEncodedPublicKey(candidate: stringValue) else { return nil }
guard let targetPrefix: Prefix = Prefix(rawValue: String(stringValue.prefix(2))) else { return nil }
self = targetPrefix
}
}
public let prefix: Prefix
public let publicKey: String
public var hexString: String {
return prefix.rawValue + publicKey
}
// MARK: - Initialization
public init?(from idString: String?) {
guard let idString: String = idString, idString.count > 2 else { return nil }
guard let targetPrefix: Prefix = Prefix(from: idString) else { return nil }
self.prefix = targetPrefix
self.publicKey = idString.substring(from: 2)
}
public init(_ type: Prefix, publicKey: Bytes) {
self.prefix = type
self.publicKey = publicKey.map { String(format: "%02hhx", $0) }.joined()
}
}