Skip to content

Commit

Permalink
Merge branch '4-tunnel' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lika-vorobeva committed Oct 5, 2022
2 parents 7dfd5d7 + 0437a3a commit ece615e
Show file tree
Hide file tree
Showing 35 changed files with 2,068 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension NodesAPITarget: APITarget {
case .getNodes:
return "dvpn/getNodes"
case .postNodesByAddress:
return "dvpn/postNodesByAddress"
return "dvpn/getNodesByAddress"
case .getCountries:
return "dvpn/getCountries"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// StartSessionRequest.swift
// SOLARAPI
//
// Created by Lika Vorobeva on 26.09.2022.
//

import Foundation

public struct StartSessionRequest: Codable {
public let key: String
public let signature: String

public init(key: String, signature: String) {
self.key = key
self.signature = signature
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// StartSessionResponse.swift
// SOLARAPI
//
// Created by Lika Vorobeva on 26.09.2022.
//

import Foundation

public struct StartSessionResponse: Codable {
public let success: Bool
public let result: String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// NodeSessionAPITarget.swift
// SOLARAPI
//
// Created by Lika Vorobeva on 26.09.2022.
//

import Foundation
import Alamofire

enum NodeSessionAPITarget {
case createClient(address: String, id: String, request: StartSessionRequest)
}

extension NodeSessionAPITarget: APITarget {
var method: HTTPMethod {
switch self {
case .createClient:
return .post
}
}

var path: String {
switch self {
case let .createClient(address, id, _):
return "accounts/\(address)/sessions/\(id)"
}
}

var parameters: Parameters {
switch self {
case let .createClient(_, _, data):
return .requestJSONEncodable(data)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// NodeSessionProvider.swift
// SOLARAPI
//
// Created by Lika Vorobeva on 26.09.2022.
//

import Foundation
import Alamofire

public final class NodeSessionProvider: SOLARAPIProvider {
public init() {}
}

// MARK: - StealthProviderType implementation

extension NodeSessionProvider: NodeSessionProviderType {
public func createClient(
remoteURL: URL,
address: String,
id: String,
request: StartSessionRequest,
completion: @escaping (Result<StartSessionResponse, NetworkError>) -> Void
) {
let apiRequest = APIRequest(
baseURL: remoteURL,
target: NodeSessionAPITarget.createClient(address: address, id: id, request: request)
)

AF.request(apiRequest)
.validate()
.responseDecodable(completionHandler: getResponseHandler(mapsInnerErrors: true, completion: completion))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// NodeSessionProviderType.swift
// SOLARAPI
//
// Created by Lika Vorobeva on 26.09.2022.
//

import Foundation
import Alamofire

public protocol NodeSessionProviderType {
func createClient(
remoteURL: URL,
address: String,
id: String,
request: StartSessionRequest,
completion: @escaping (Result<StartSessionResponse, NetworkError>) -> Void
)
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,110 @@
//

import Foundation
import SentinelWallet
import SOLARAPI

protocol NoContext {}

final class CommonContext {
// Providers
let nodesProvider: NodesProviderType
typealias Storage = StoresConnectInfo & StoresWallet & StoresDNSServers
let storage: Storage

// Providers
let nodesProvider: SOLARAPI.NodesProviderType

// Services
let securityService: SecurityService
let nodesService: NodesServiceType

let walletService: WalletService
let subscriptionsService: SubscriptionsServiceType
let sessionsService: SessionsServiceType

let tunnelManager: TunnelManagerType

init(
nodesProvider: NodesProviderType,
nodesService: NodesServiceType
storage: Storage,
nodesProvider: SOLARAPI.NodesProviderType,
securityService: SecurityService,
walletService: WalletService,
nodesService: NodesServiceType,
subscriptionsService: SubscriptionsServiceType,
sessionsService: SessionsServiceType,
tunnelManager: TunnelManagerType
) {
self.storage = storage
self.nodesProvider = nodesProvider
self.securityService = securityService
self.walletService = walletService
self.nodesService = nodesService
self.subscriptionsService = subscriptionsService
self.sessionsService = sessionsService
self.tunnelManager = tunnelManager
}
}

protocol HasNodesProvider { var nodesProvider: NodesProviderType { get } }
protocol HasWalletService {
var walletService: WalletService { get }

func updateWalletContext()
func resetWalletContext()
}

extension CommonContext: HasWalletService {
func updateWalletContext() {
let walletAddress = storage.walletAddress
guard !walletAddress.isEmpty else { return }
walletService.manage(address: walletAddress)
}

func resetWalletContext() {
let mnemonics = securityService.generateMnemonics().components(separatedBy: " ")
switch securityService.restore(from: mnemonics) {
case .failure(let error):
fatalError("failed to generate wallet due to \(error), terminate")
case .success(let address):
saveMnemonicsIfNeeded(for: address, mnemonics: mnemonics)
storage.set(wallet: address)
updateWalletContext()
}
}

private func saveMnemonicsIfNeeded(for account: String, mnemonics: [String]) {
guard !securityService.mnemonicsExists(for: account) else { return }
if !self.securityService.save(mnemonics: mnemonics, for: account) {
log.error("Failed to save mnemonics info")
}
}
}

protocol HasSubscriptionsService { var subscriptionsService: SubscriptionsServiceType { get } }
extension CommonContext: HasSubscriptionsService {}

protocol HasNodesProvider { var nodesProvider: SOLARAPI.NodesProviderType { get } }
extension CommonContext: HasNodesProvider {}

protocol HasNodesService { var nodesService: NodesServiceType { get } }
extension CommonContext: HasNodesService {}

protocol HasTunnelManager { var tunnelManager: TunnelManagerType { get } }
extension CommonContext: HasTunnelManager {}

protocol HasSessionsService { var sessionsService: SessionsServiceType { get } }
extension CommonContext: HasSessionsService {}

// MARK: - Storages

protocol HasConnectionInfoStorage { var connectionInfoStorage: StoresConnectInfo { get } }
extension CommonContext: HasConnectionInfoStorage {
var connectionInfoStorage: StoresConnectInfo {
storage as StoresConnectInfo
}
}

protocol HasWalletStorage { var walletStorage: StoresWallet { get } }
extension CommonContext: HasWalletStorage {
var walletStorage: StoresWallet {
storage as StoresWallet
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,82 @@
//

import Foundation
import SentinelWallet
import SOLARAPI

/// This class should configure all required services and inject them into a Context
final class ContextBuilder {
func buildContext() -> CommonContext {
let nodesProvider = NodesProvider(configuration: .init(baseURL: ClientConstants.backendURL))
let generalSettingsStorage = GeneralSettingsStorage()

let nodesProvider = NodesProvider(configuration: .init(baseURL: ClientConstants.backendURL))
let nodesService = NodesService(nodesProvider: nodesProvider)

let securityService = SecurityService()
let subscriptionsProvider = SubscriptionsProvider(configuration: ApplicationConfiguration.shared)
let sessionProvider = NodeSessionProvider()
let walletService = buildWalletService(storage: generalSettingsStorage, securityService: securityService)

let sessionsService = SessionsService(
sessionProvider: sessionProvider,
subscriptionsProvider: subscriptionsProvider,
walletService: walletService
)
let subscriptionsService = SubscriptionsService(
subscriptionsProvider: subscriptionsProvider,
walletService: walletService
)

let tunnelManager = TunnelManager(storage: generalSettingsStorage)

return CommonContext(
storage: generalSettingsStorage,
nodesProvider: nodesProvider,
nodesService: nodesService
securityService: securityService,
walletService: walletService,
nodesService: nodesService,
subscriptionsService: subscriptionsService,
sessionsService: sessionsService,
tunnelManager: tunnelManager
)
}

func buildWalletService(
storage: StoresWallet,
securityService: SecurityService
) -> WalletService {
let walletAddress = storage.walletAddress
let grpc = ApplicationConfiguration.shared
guard !walletAddress.isEmpty else {
let mnemonic = securityService.generateMnemonics().components(separatedBy: " ")
switch securityService.restore(from: mnemonic) {
case .failure(let error):
fatalError("failed to generate wallet due to \(error), terminate")
case .success(let address):
saveMnemonicsIfNeeded(for: address, mnemonics: mnemonic, securityService: securityService)
storage.set(wallet: address)
return .init(
for: address,
configuration: grpc,
securityService: securityService
)
}
}
return WalletService(
for: walletAddress,
configuration: grpc,
securityService: securityService
)
}

private func saveMnemonicsIfNeeded(
for account: String,
mnemonics: [String],
securityService: SecurityService
) {
guard !securityService.mnemonicsExists(for: account) else { return }
if !securityService.save(mnemonics: mnemonics, for: account) {
log.error("Failed to save mnemonics info")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SentinelWallet

extension SentinelNode {
func set(node: Node) -> SentinelNode {
func set(node: SentinelWallet.Node) -> SentinelNode {
return .init(
address: self.address,
provider: self.provider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@
//

import Foundation
import SentinelWallet

enum ClientConstants {
static let host = "localhost"
static let port = 3876

static let defaultLCDHostString = "lcd-sentinel.dvpn.solar"
static let defaultLCDPort = 993

static let apiPath = "api"

static let backendURL = URL(string: "https://BACKEND")!
}

final class ApplicationConfiguration: ClientConnectionConfigurationType {
private(set) static var shared = ApplicationConfiguration()

var grpcMirror: ClientConnectionConfiguration = .init(
host: ClientConstants.defaultLCDHostString,
port: ClientConstants.defaultLCDPort
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>App GroupID</key>
<string>group.ee.solarlabs.community-core.ios</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>App GroupID</key>
<string>group.ee.solarlabs.community-core.ios</string>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
Expand Down
Loading

0 comments on commit ece615e

Please sign in to comment.