Skip to content

Commit

Permalink
Add a migrating service to a new VWS endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
SwenVanZanten committed Oct 26, 2019
1 parent 41dccaa commit 59c0e4a
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 16 deletions.
8 changes: 8 additions & 0 deletions VergeiOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@
A3408E4C217333B5009F0C9D /* NotificationViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A3408E4B217333B5009F0C9D /* NotificationViewController.xib */; };
A3408E56217687F9009F0C9D /* Charts.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A3408E55217687F9009F0C9D /* Charts.framework */; };
A3408E5821768AF4009F0C9D /* Date+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3408E5721768AF4009F0C9D /* Date+Extensions.swift */; };
A3478B4923647FF20018149C /* WalletManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3478B4823647FF20018149C /* WalletManager.swift */; };
A3478B4B236480230018149C /* WalletManagerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3478B4A236480230018149C /* WalletManagerProtocol.swift */; };
A3549DD521B9DB5A0091EAC3 /* PassphraseConfirmationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3549DD421B9DB5A0091EAC3 /* PassphraseConfirmationViewController.swift */; };
A35EFA9B22EA48F30064D3E0 /* TableHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A35EFA9A22EA48F30064D3E0 /* TableHeaderView.swift */; };
A35F3D82211CE738006068A2 /* ApplicationRepositoryTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = A35F3D81211CE738006068A2 /* ApplicationRepositoryTest.swift */; };
Expand Down Expand Up @@ -517,6 +519,8 @@
A3408E4B217333B5009F0C9D /* NotificationViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = NotificationViewController.xib; sourceTree = "<group>"; };
A3408E55217687F9009F0C9D /* Charts.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Charts.framework; path = Carthage/Build/iOS/Charts.framework; sourceTree = "<group>"; };
A3408E5721768AF4009F0C9D /* Date+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+Extensions.swift"; sourceTree = "<group>"; };
A3478B4823647FF20018149C /* WalletManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletManager.swift; sourceTree = "<group>"; };
A3478B4A236480230018149C /* WalletManagerProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletManagerProtocol.swift; sourceTree = "<group>"; };
A3549DD421B9DB5A0091EAC3 /* PassphraseConfirmationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PassphraseConfirmationViewController.swift; sourceTree = "<group>"; };
A35EFA9A22EA48F30064D3E0 /* TableHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableHeaderView.swift; sourceTree = "<group>"; };
A35F3D81211CE738006068A2 /* ApplicationRepositoryTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApplicationRepositoryTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1016,6 +1020,7 @@
A3EA38E922FB619F00E89CF0 /* SweeperHelperProtocol.swift */,
A3A71C1722DA66B80098D861 /* TransactionFactoryProtocol.swift */,
A30D279A2260D2AD000377F8 /* WalletClientProtocol.swift */,
A3478B4A236480230018149C /* WalletManagerProtocol.swift */,
A3EA38E722FB58CE00E89CF0 /* WalletSweepingScannerViewDelegate.swift */,
);
path = Protocols;
Expand Down Expand Up @@ -1395,6 +1400,7 @@
B2BD7365DCBB13FEA0B32516 /* TxAction.swift */,
B2BD7A3D8D516C3586332E99 /* TxTransponder.swift */,
B2BD78A6683D5C34785959E3 /* WalletClient.swift */,
A3478B4823647FF20018149C /* WalletManager.swift */,
B2BD7C51C174D8B86AFF7D60 /* WalletTicker.swift */,
A3239D6322E60C2B00430F8B /* SweeperHelper.swift */,
);
Expand Down Expand Up @@ -2262,6 +2268,7 @@
B2BD7A6D183C729DC88C4E3B /* ReceiveViewServiceProvider.swift in Sources */,
B2BD72FBDC0135287D5E1587 /* HttpServiceProvider.swift in Sources */,
A3AA2B7F2263D207000E4571 /* TextLabel.swift in Sources */,
A3478B4923647FF20018149C /* WalletManager.swift in Sources */,
B2BD7C656B2431B5A693354F /* WalletViewServiceProvider.swift in Sources */,
B2BD7E83471050D83BF5709F /* Application.swift in Sources */,
B2BD7E88E838F993C74C3EA7 /* EventServiceProvider.swift in Sources */,
Expand All @@ -2271,6 +2278,7 @@
B2BD7EF437ABA5F3917715CF /* ThemedImageView.swift in Sources */,
A3EA38DE22FA0C5900E89CF0 /* TableSection.swift in Sources */,
CAA6031322BC3E3B00EFCB24 /* UIView+Themeable.swift in Sources */,
A3478B4B236480230018149C /* WalletManagerProtocol.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ServiceUrlTableViewController: LocalizableTableViewController {
walletClient.resetServiceUrl(baseUrl: serviceUrl)
walletTicker.start()
}

private func joinWallet(alert: UIAlertController, create: Bool = true) {
self.walletClient.joinWallet(walletIdentifier: self.applicationRepository.walletId!) { error in
guard let error = error else {
Expand All @@ -76,19 +76,19 @@ class ServiceUrlTableViewController: LocalizableTableViewController {
self.urlChanged(alert: alert)
}
}

print(error)

if !create {
return
}

print("Joining wallet failed... tring to create a new wallet")
print("Joining wallet failed... trying to create a new wallet")

return self.createWallet(alert: alert)
}
}

private func createWallet(alert: UIAlertController) {
self.walletClient.createWallet(
walletName: "ioswallet",
Expand All @@ -105,7 +105,7 @@ class ServiceUrlTableViewController: LocalizableTableViewController {
print(error ?? "")
return
}

self.joinWallet(alert: alert, create: false)
}
}
Expand Down
15 changes: 15 additions & 0 deletions VergeiOS/Protocols/WalletManagerProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// WalletManagerProtocol.swift
// VergeiOS
//
// Created by Swen van Zanten on 26/10/2019.
// Copyright © 2019 Verge Currency. All rights reserved.
//

import Foundation

protocol WalletManagerProtocol {
func joinWallet(createWallet: Bool, completion: @escaping (_ error: Error?) -> Void)
func createWallet(completion: @escaping (_ error: Error?) -> Void)
func synchronizeWallet(completion: @escaping (_ error: Error?) -> Void)
}
68 changes: 58 additions & 10 deletions VergeiOS/Providers/WalletServiceProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,26 @@ import Swinject
class WalletServiceProvider: ServiceProvider {

override func register() {
registerWalletCredentials()
registerWalletClient()
registerTxTransponder()
registerTransactionRepository()
registerTransactionFactory()
registerTransactionManager()
registerWalletTicker()
registerFiatRateTicker()
registerAddressBookRepository()
registerSweeperHelper()
self.registerWalletCredentials()
self.registerWalletClient()
self.registerTxTransponder()
self.registerTransactionRepository()
self.registerTransactionFactory()
self.registerTransactionManager()
self.registerWalletTicker()
self.registerFiatRateTicker()
self.registerAddressBookRepository()
self.registerSweeperHelper()
self.registerWalletManager()
}

override func boot() {
NotificationCenter.default.addObserver(
self,
selector: #selector(bootServerMigration(notification:)),
name: .didEstablishTorConnection,
object: nil
)
}

func registerWalletCredentials() {
Expand Down Expand Up @@ -117,4 +127,42 @@ class WalletServiceProvider: ServiceProvider {
}
}

func registerWalletManager() {
container.register(WalletManagerProtocol.self) { r in
return WalletManager(
walletClient: r.resolve(WalletClientProtocol.self)!,
walletTicker: r.resolve(WalletTicker.self)!,
applicationRepository: r.resolve(ApplicationRepository.self)!
)
}.inObjectScope(.container)
}

@objc func bootServerMigration(notification: Notification) {
let applicationRepository = self.container.resolve(ApplicationRepository.self)!

// Check if the deprecated VWS endpoints are in the users memory.
if !Constants.deprecatedBwsEndpoints.contains(applicationRepository.walletServiceUrl) {
return print("No deprecated VWS endpoints found.")
}

let walletClient = self.container.resolve(WalletClientProtocol.self)!
let walletManager = self.container.resolve(WalletManagerProtocol.self)!

// If so replace them by the replacement VWS endpoint.
applicationRepository.walletServiceUrl = Constants.bwsEndpoint
walletClient.resetServiceUrl(baseUrl: applicationRepository.walletServiceUrl)

// If the wallet is setup we check on the server if there is a wallet present.
if applicationRepository.setup {
walletManager.joinWallet(createWallet: true) { error in
if error != nil {
return print(error ?? "Unresolved error")
}

walletManager.synchronizeWallet { error in
print(error ?? "Unresolved error")
}
}
}
}
}
7 changes: 7 additions & 0 deletions VergeiOS/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public struct Constants {
*/
public static let bwsEndpoint: String = "https://api.vergecurrency.network/vws/api/"

/**
* A list of deprecated Verge Wallet Service endpoints.
*/
public static let deprecatedBwsEndpoints: [String] = [
"https://vws.vergecurrency.network/vws/api/"
]

/**
* The default Verge Bitcore Node.
*/
Expand Down
73 changes: 73 additions & 0 deletions VergeiOS/Wallet/WalletManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// WalletManager.swift
// VergeiOS
//
// Created by Swen van Zanten on 26/10/2019.
// Copyright © 2019 Verge Currency. All rights reserved.
//

import Foundation

class WalletManager: WalletManagerProtocol {
let walletClient: WalletClientProtocol
let walletTicker: WalletTicker
let applicationRepository: ApplicationRepository

let walletName = "ioswallet"
let copayerName = "iosuser"
let walletM = 1
let walletN = 1

init(walletClient: WalletClientProtocol, walletTicker: WalletTicker, applicationRepository: ApplicationRepository) {
self.walletClient = walletClient
self.walletTicker = walletTicker
self.applicationRepository = applicationRepository
}

func joinWallet(createWallet: Bool = true, completion: @escaping (_ error: Error?) -> Void = { _ in }) {
self.walletTicker.stop()

self.walletClient.joinWallet(walletIdentifier: self.applicationRepository.walletId!) { error in
if error == nil {
return DispatchQueue.main.async {
self.walletTicker.start()
completion(nil)
}
}

if !createWallet {
return
}

print("Joining wallet failed... trying to create a new wallet")

return self.createWallet(completion: completion)
}
}

func createWallet(completion: @escaping (_ error: Error?) -> Void = { _ in }) {
self.walletTicker.stop()

self.walletClient.createWallet(
walletName: self.walletName,
copayerName: self.copayerName,
m: self.walletM,
n: self.walletN,
options: nil
) { error, secret in
if (error != nil || secret == nil) {
DispatchQueue.main.async {
completion(error)
}

return print(error ?? "")
}

self.joinWallet(createWallet: false, completion: completion)
}
}

func synchronizeWallet(completion: @escaping (_ error: Error?) -> Void = { _ in }) {
self.walletClient.scanAddresses(completion: completion)
}
}

0 comments on commit 59c0e4a

Please sign in to comment.