Implement basic snode interaction

This commit is contained in:
Niels Andriesse 2019-04-30 14:27:39 +10:00
parent 9d64b76af6
commit 011d4e1465
6 changed files with 71 additions and 13 deletions

View File

@ -562,6 +562,7 @@
B821F2F62272CCD9002C88C0 /* english.txt in Resources */ = {isa = PBXBuildFile; fileRef = B821F2F22272CCD9002C88C0 /* english.txt */; };
B821F2F82272CED3002C88C0 /* OnboardingAccountDetailsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B821F2F72272CED3002C88C0 /* OnboardingAccountDetailsViewController.swift */; };
B821F2FA2272CEEE002C88C0 /* OnboardingPublicKeyViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B821F2F92272CEEE002C88C0 /* OnboardingPublicKeyViewController.swift */; };
B8C915232277FDC30028E4A9 /* ECKeyPair.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8C915222277FDC30028E4A9 /* ECKeyPair.swift */; };
B90418E6183E9DD40038554A /* DateUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = B90418E5183E9DD40038554A /* DateUtil.m */; };
B9EB5ABD1884C002007CBB57 /* MessageUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B9EB5ABC1884C002007CBB57 /* MessageUI.framework */; };
BFF3FB9730634F37D25903F4 /* Pods_Signal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D17BB5C25D615AB49813100C /* Pods_Signal.framework */; };
@ -1351,6 +1352,7 @@
B821F2F22272CCD9002C88C0 /* english.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = english.txt; path = mnemonic/english.txt; sourceTree = "<group>"; };
B821F2F72272CED3002C88C0 /* OnboardingAccountDetailsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingAccountDetailsViewController.swift; sourceTree = "<group>"; };
B821F2F92272CEEE002C88C0 /* OnboardingPublicKeyViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingPublicKeyViewController.swift; sourceTree = "<group>"; };
B8C915222277FDC30028E4A9 /* ECKeyPair.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ECKeyPair.swift; path = ../../../SignalServiceKit/src/Util/ECKeyPair.swift; sourceTree = "<group>"; };
B90418E4183E9DD40038554A /* DateUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DateUtil.h; sourceTree = "<group>"; };
B90418E5183E9DD40038554A /* DateUtil.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DateUtil.m; sourceTree = "<group>"; };
B97940251832BD2400BD66CB /* UIUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIUtil.h; sourceTree = "<group>"; };
@ -2429,6 +2431,7 @@
B90418E4183E9DD40038554A /* DateUtil.h */,
3496956121A301A100DCFE74 /* Backup */,
B90418E5183E9DD40038554A /* DateUtil.m */,
B8C915222277FDC30028E4A9 /* ECKeyPair.swift */,
34B0796C1FCF46B000E248C2 /* MainAppContext.h */,
34B0796B1FCF46B000E248C2 /* MainAppContext.m */,
B821F2EC2272CBC0002C88C0 /* Mnemonic.swift */,
@ -3693,6 +3696,7 @@
3448E16022134C89004B052E /* OnboardingSplashViewController.swift in Sources */,
34B6A903218B3F63007C4606 /* TypingIndicatorView.swift in Sources */,
458E38371D668EBF0094BD24 /* OWSDeviceProvisioningURLParser.m in Sources */,
B8C915232277FDC30028E4A9 /* ECKeyPair.swift in Sources */,
34B6A905218B4C91007C4606 /* TypingIndicatorInteraction.swift in Sources */,
4517642B1DE939FD00EDB8B9 /* ContactCell.swift in Sources */,
34EA69402194933900702471 /* MediaDownloadView.swift in Sources */,

View File

@ -1,5 +1,3 @@
import UIKit
import PromiseKit
final class OnboardingAccountDetailsViewController : OnboardingBaseViewController {
@ -41,7 +39,7 @@ final class OnboardingAccountDetailsViewController : OnboardingBaseViewControlle
let passwordLabel = createExplanationLabel(text: NSLocalizedString("Type an optional password for added security", comment: ""))
passwordLabel.accessibilityIdentifier = "onboarding.accountDetailsStep.passwordLabel"
let bottomSpacer = UIView.vStretchingSpacer()
let nextButton = createButton(title: NSLocalizedString("Next", comment: ""), selector: #selector(updateProfile))
let nextButton = createButton(title: NSLocalizedString("Next", comment: ""), selector: #selector(handleNextButtonPressed))
nextButton.accessibilityIdentifier = "onboarding.accountDetailsStep.nextButton"
let stackView = UIStackView(arrangedSubviews: [
titleLabel,
@ -72,16 +70,12 @@ final class OnboardingAccountDetailsViewController : OnboardingBaseViewControlle
displayNameTextField.becomeFirstResponder()
}
@objc private func updateProfile() {
@objc private func handleNextButtonPressed() {
if let normalizedName = normalizedName {
guard !OWSProfileManager.shared().isProfileNameTooLong(normalizedName) else {
return OWSAlerts.showErrorAlert(message: NSLocalizedString("PROFILE_VIEW_ERROR_PROFILE_NAME_TOO_LONG", comment: "Error message shown when user tries to update profile with a profile name that is too long"))
}
}
goToSeedStep()
}
@objc private func goToSeedStep() {
onboardingController.pushPublicKeyViewController(from: self, userName: normalizedName)
}
}

View File

@ -1,5 +1,3 @@
import UIKit
import PromiseKit
final class OnboardingPublicKeyViewController : OnboardingBaseViewController {
private var keyPair: ECKeyPair! { didSet { updateMnemonic() } }
@ -32,6 +30,17 @@ final class OnboardingPublicKeyViewController : OnboardingBaseViewController {
super.loadView()
setUpViewHierarchy()
updateKeyPair()
// Test
// ================
let _ = ServiceNode.retrieveAllMessages().done { result in
print(result.task.originalRequest!)
print(result.task.response!)
}
let _ = ServiceNode.sendTestMessage().done { result in
print(result.task.originalRequest!)
print(result.task.response!)
}
// ================
}
private func setUpViewHierarchy() {
@ -69,12 +78,12 @@ final class OnboardingPublicKeyViewController : OnboardingBaseViewController {
private func updateKeyPair() {
let identityManager = OWSIdentityManager.shared()
identityManager.generateNewIdentityKey() // Generates and stores a new key pair
identityManager.generateNewIdentityKey() // Generate and store a new identity key pair
keyPair = identityManager.identityKeyPair()!
}
private func updateMnemonic() {
hexEncodedPublicKey = keyPair.publicKey.map { String(format: "%02hhx", $0) }.joined()
hexEncodedPublicKey = keyPair.hexEncodedPublicKey
mnemonic = Mnemonic.encode(hexEncodedString: hexEncodedPublicKey)
}

View File

@ -0,0 +1,44 @@
import PromiseKit
public struct ServiceNode {
private static var snodeURL: String { return textSecureServerURL }
private static var port: String { return "8080" }
private static var apiVersion: String { return "v1" }
// MARK: Types
private enum Method : String {
case retrieveAllMessages = "retrieve"
case send = "store"
}
public typealias Response = TSNetworkManager.NetworkManagerResult
// MARK: Lifecycle
private init() { }
// MARK: API
private static func invoke(_ method: Method, parameters: [String:String] = [:]) -> Promise<Response> {
let url = URL(string: "\(snodeURL):\(port)/\(apiVersion)/storage_rpc")!
var parameters = parameters
parameters["pubKey"] = "050371e72be8dd42ff77105e474a3ac26a503d017fb4562409c639eaf5965f5b31" // OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey
let request = TSRequest(url: url, method: "POST", parameters: [ "method" : method.rawValue, "params" : parameters ])
return TSNetworkManager.shared().makePromise(request: request)
}
public static func sendTestMessage() -> Promise<Response> {
let ttl = String(4 * 24 * 60 * 60 * 1000)
let parameters = [
"ttl" : ttl,
"nonce" : "AAAAAAAA5rs=", // TODO: Proof of work
"timestamp" : "1556259498201", // TODO: Message send time
"data" : "CAESvgMKA1BVVBIPL2FwaS92MS9tZXNzYWdlGqIDCGUSQjA1MDM3MWU3MmJlOGRkNDJmZjc3MTA1ZTQ3NGEzYWMyNmE1MDNkMDE3ZmI0NTYyNDA5YzYzOWVhZjU5NjVmNWIzYzgBKK3QrcKlLULQAlxclJTbzKeQjJPfPlvo0VdoNw+O6kmpAUAKz2Mmz0YDHnhIsFgdWlBIoudqxVDu7swq5Z4cUqMfcQ5Z0b03/dVjkmFYo79Hzv7wkmRlPsfqAOVLBgV06sLVl+C5d8EmDtfH+k2iT62HnD8fub8tIxHn2l0MCefB4kO8tbA4dl/n/IXlvRAFS7OPJiq3jLyykyZkauAW7SVdDBAO6exJlNyOHTgSaHF924V3a/s3BK0useVMbzJSun9cx68Jm3WGERMFqrd75X70PN933zUSHedBAmMFW1Mvecko1G854tfNPZllP7OO/o+6XrQm8hMoe0Zo3POelrXwRdX88jp9VSEio/Yugq9MMcBuMsU5G0ePK5ZJMNfGLwExGSLY4br3sYpJz5yO7slpq2GgPuO6t9hWwIfzWynvNIfVtDxBkLVSV5XZU7720p/KP6kqZWCGHyCsAQ==" // TODO: Encrypted content
]
return invoke(.send, parameters: parameters)
}
public static func retrieveAllMessages() -> Promise<Response> {
let parameters = [ "lastHash" : "" ]
return invoke(.retrieveAllMessages, parameters: parameters)
}
}

View File

@ -26,7 +26,7 @@ typedef NS_ENUM(NSInteger, TSWhisperMessageType) {
// Production
#define textSecureWebSocketAPI @"wss://textsecure-service.whispersystems.org/v1/websocket/"
#define textSecureServerURL @"https://textsecure-service.whispersystems.org/"
#define textSecureServerURL @"http://13.238.53.205" // TODO: Temporary
#define textSecureCDNServerURL @"https://cdn.signal.org"
// Use same reflector for service and CDN
#define textSecureServiceReflectorHost @"europe-west1-signal-cdn-reflector.cloudfunctions.net"

View File

@ -0,0 +1,7 @@
extension ECKeyPair {
var hexEncodedPublicKey: String {
return publicKey.map { String(format: "%02hhx", $0) }.joined()
}
}