|
|
|
@ -4,6 +4,7 @@ import Foundation |
|
|
|
|
import Alamofire |
|
|
|
|
import BigInt |
|
|
|
|
import Realm |
|
|
|
|
import TrustKeystore |
|
|
|
|
|
|
|
|
|
protocol UniversalLinkCoordinatorDelegate: class { |
|
|
|
|
func viewControllerForPresenting(in coordinator: UniversalLinkCoordinator) -> UIViewController? |
|
|
|
@ -18,6 +19,7 @@ class UniversalLinkCoordinator: Coordinator { |
|
|
|
|
var ethPrice: Subscribable<Double>? |
|
|
|
|
var ethBalance: Subscribable<BigInt>? |
|
|
|
|
var hasCompleted = false |
|
|
|
|
var addressOfNewWallet: String? |
|
|
|
|
|
|
|
|
|
func start() { |
|
|
|
|
preparingToImportUniversalLink() |
|
|
|
@ -39,7 +41,7 @@ class UniversalLinkCoordinator: Coordinator { |
|
|
|
|
"v": signature.substring(from: 128), |
|
|
|
|
"r": "0x" + signature.substring(with: Range(uncheckedBounds: (0, 64))), |
|
|
|
|
"s": "0x" + signature.substring(with: Range(uncheckedBounds: (64, 128))), |
|
|
|
|
"networkId": Config.init().chainID.description |
|
|
|
|
"networkId": Config().chainID.description |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
if isForTransfer { |
|
|
|
@ -51,7 +53,8 @@ class UniversalLinkCoordinator: Coordinator { |
|
|
|
|
|
|
|
|
|
func handlePaidUniversalLink(signedOrder: SignedOrder, ticketHolder: TicketHolder) -> Bool { |
|
|
|
|
//TODO localize |
|
|
|
|
if let viewController = delegate?.viewControllerForPresenting(in: self) { |
|
|
|
|
//TODO improve. Not an obvious link between the variables used in the if-statement and the body |
|
|
|
|
if delegate?.viewControllerForPresenting(in: self) != nil { |
|
|
|
|
if let vc = importTicketViewController { |
|
|
|
|
vc.signedOrder = signedOrder |
|
|
|
|
vc.tokenObject = TokenObject(contract: signedOrder.order.contractAddress, |
|
|
|
@ -94,7 +97,8 @@ class UniversalLinkCoordinator: Coordinator { |
|
|
|
|
func usePaymentServerForFreeTransferLinks(signedOrder: SignedOrder, ticketHolder: TicketHolder) -> Bool { |
|
|
|
|
let parameters = createHTTPParametersForPaymentServer(signedOrder: signedOrder, isForTransfer: true) |
|
|
|
|
let query = Constants.paymentServer |
|
|
|
|
if let viewController = delegate?.viewControllerForPresenting(in: self) { |
|
|
|
|
//TODO improve. Not an obvious link between the variables used in the if-statement and the body |
|
|
|
|
if delegate?.viewControllerForPresenting(in: self) != nil { |
|
|
|
|
if let vc = importTicketViewController { |
|
|
|
|
vc.query = query |
|
|
|
|
vc.parameters = parameters |
|
|
|
@ -110,12 +114,12 @@ class UniversalLinkCoordinator: Coordinator { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//Returns true if handled |
|
|
|
|
|
|
|
|
|
func handleUniversalLink(url: URL?) -> Bool { |
|
|
|
|
let matchedPrefix = (url?.description.contains(UniversalLinkHandler().urlPrefix))! |
|
|
|
|
guard matchedPrefix else { |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let signedOrder = UniversalLinkHandler().parseUniversalLink(url: (url?.absoluteString)!) |
|
|
|
|
getTicketDetailsAndEcRecover(signedOrder: signedOrder) { result in |
|
|
|
|
if let goodResult = result { |
|
|
|
@ -123,7 +127,7 @@ class UniversalLinkCoordinator: Coordinator { |
|
|
|
|
if let balance = self.ethBalance { |
|
|
|
|
balance.subscribeOnce { value in |
|
|
|
|
if value > signedOrder.order.price { |
|
|
|
|
let success = self.handlePaidUniversalLink(signedOrder: signedOrder, ticketHolder: goodResult) |
|
|
|
|
let _ = self.handlePaidUniversalLink(signedOrder: signedOrder, ticketHolder: goodResult) |
|
|
|
|
} else { |
|
|
|
|
if let price = self.ethPrice { |
|
|
|
|
if price.value == nil { |
|
|
|
@ -143,7 +147,7 @@ class UniversalLinkCoordinator: Coordinator { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
let success = self.usePaymentServerForFreeTransferLinks( |
|
|
|
|
let _ = self.usePaymentServerForFreeTransferLinks( |
|
|
|
|
signedOrder: signedOrder, |
|
|
|
|
ticketHolder: goodResult |
|
|
|
|
) |
|
|
|
@ -290,16 +294,15 @@ class UniversalLinkCoordinator: Coordinator { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if let vc = self.importTicketViewController { |
|
|
|
|
//TODO improve. Not an obvious link between the variables used in the if-statement and the body |
|
|
|
|
if let vc = self.importTicketViewController, vc.viewModel != nil { |
|
|
|
|
// TODO handle http response |
|
|
|
|
print(result) |
|
|
|
|
if let vc = self.importTicketViewController, var viewModel = vc.viewModel { |
|
|
|
|
if successful { |
|
|
|
|
self.showImportSuccessful() |
|
|
|
|
} else { |
|
|
|
|
//TODO Pass in error message |
|
|
|
|
self.showImportError(errorMessage: R.string.localizable.aClaimTicketFailedTitle()) |
|
|
|
|
} |
|
|
|
|
if successful { |
|
|
|
|
self.showImportSuccessful() |
|
|
|
|
} else { |
|
|
|
|
//TODO Pass in error message |
|
|
|
|
self.showImportError(errorMessage: R.string.localizable.aClaimTicketFailedTitle()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -312,6 +315,7 @@ class UniversalLinkCoordinator: Coordinator { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private func convert(ethCost: BigUInt) -> Decimal { |
|
|
|
|
//TODO extract constant. Used elsewhere too |
|
|
|
|
let divideAmount = Decimal(string: "1000000000000000000")! |
|
|
|
|
let etherCostDecimal = Decimal(string: ethCost.description)! / divideAmount |
|
|
|
|
return etherCostDecimal |
|
|
|
|