|
|
@ -3,6 +3,7 @@ import SessionSnodeKit
|
|
|
|
import SessionUtilities
|
|
|
|
import SessionUtilities
|
|
|
|
|
|
|
|
|
|
|
|
public enum SendingPipeline {
|
|
|
|
public enum SendingPipeline {
|
|
|
|
|
|
|
|
private static let ttl: UInt64 = 2 * 24 * 60 * 60 * 1000
|
|
|
|
|
|
|
|
|
|
|
|
public enum Destination {
|
|
|
|
public enum Destination {
|
|
|
|
case contact(publicKey: String)
|
|
|
|
case contact(publicKey: String)
|
|
|
@ -11,12 +12,14 @@ public enum SendingPipeline {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public enum Error : LocalizedError {
|
|
|
|
public enum Error : LocalizedError {
|
|
|
|
|
|
|
|
case invalidMessage
|
|
|
|
case protoConversionFailed
|
|
|
|
case protoConversionFailed
|
|
|
|
case protoSerializationFailed
|
|
|
|
case protoSerializationFailed
|
|
|
|
case proofOfWorkCalculationFailed
|
|
|
|
case proofOfWorkCalculationFailed
|
|
|
|
|
|
|
|
|
|
|
|
public var errorDescription: String? {
|
|
|
|
public var errorDescription: String? {
|
|
|
|
switch self {
|
|
|
|
switch self {
|
|
|
|
|
|
|
|
case .invalidMessage: return "Invalid message."
|
|
|
|
case .protoConversionFailed: return "Couldn't convert message to proto."
|
|
|
|
case .protoConversionFailed: return "Couldn't convert message to proto."
|
|
|
|
case .protoSerializationFailed: return "Couldn't serialize proto."
|
|
|
|
case .protoSerializationFailed: return "Couldn't serialize proto."
|
|
|
|
case .proofOfWorkCalculationFailed: return "Proof of work calculation failed."
|
|
|
|
case .proofOfWorkCalculationFailed: return "Proof of work calculation failed."
|
|
|
@ -25,19 +28,18 @@ public enum SendingPipeline {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static func send(_ message: Message, to destination: Destination) -> Promise<Void> {
|
|
|
|
public static func send(_ message: Message, to destination: Destination) -> Promise<Void> {
|
|
|
|
|
|
|
|
guard message.isValidForSending else { return Promise(error: Error.invalidMessage) }
|
|
|
|
guard let proto = message.toProto() else { return Promise(error: Error.protoConversionFailed) }
|
|
|
|
guard let proto = message.toProto() else { return Promise(error: Error.protoConversionFailed) }
|
|
|
|
let data: Data
|
|
|
|
let plaintext: Data
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
data = try proto.serializedData()
|
|
|
|
plaintext = try proto.serializedData()
|
|
|
|
} catch {
|
|
|
|
} catch {
|
|
|
|
SNLog("Couldn't serialize proto due to error: \(error).")
|
|
|
|
SNLog("Couldn't serialize proto due to error: \(error).")
|
|
|
|
return Promise(error: Error.protoSerializationFailed)
|
|
|
|
return Promise(error: Error.protoSerializationFailed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Encryption
|
|
|
|
let ciphertext = plaintext // TODO: Encryption
|
|
|
|
// TODO: Validation
|
|
|
|
let recipient = message.recipient!
|
|
|
|
let recipient = ""
|
|
|
|
let base64EncodedData = ciphertext.base64EncodedString()
|
|
|
|
let base64EncodedData = data.base64EncodedString()
|
|
|
|
|
|
|
|
let ttl: UInt64 = 2 * 24 * 60 * 60 * 1000
|
|
|
|
|
|
|
|
guard let (timestamp, nonce) = ProofOfWork.calculate(ttl: ttl, publicKey: recipient, data: base64EncodedData) else {
|
|
|
|
guard let (timestamp, nonce) = ProofOfWork.calculate(ttl: ttl, publicKey: recipient, data: base64EncodedData) else {
|
|
|
|
SNLog("Proof of work calculation failed.")
|
|
|
|
SNLog("Proof of work calculation failed.")
|
|
|
|
return Promise(error: Error.proofOfWorkCalculationFailed)
|
|
|
|
return Promise(error: Error.proofOfWorkCalculationFailed)
|
|
|
|