Skip to content

Commit

Permalink
Add support wallet connect for KyberSwap web
Browse files Browse the repository at this point in the history
  • Loading branch information
manhlx3006 committed Nov 26, 2019
1 parent 64afced commit 83d568d
Show file tree
Hide file tree
Showing 11 changed files with 589 additions and 13 deletions.
26 changes: 24 additions & 2 deletions KyberNetwork.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@
C680C1202382958700A621CB /* KNVersionControlManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C680C11F2382958700A621CB /* KNVersionControlManager.swift */; };
C68AA276237A6BE900B9EC35 /* KNTransactionStatusPopUp.swift in Sources */ = {isa = PBXBuildFile; fileRef = C68AA274237A6BE900B9EC35 /* KNTransactionStatusPopUp.swift */; };
C68AA277237A6BE900B9EC35 /* KNTransactionStatusPopUp.xib in Resources */ = {isa = PBXBuildFile; fileRef = C68AA275237A6BE900B9EC35 /* KNTransactionStatusPopUp.xib */; };
C6FE41812383A0C10096AAD8 /* KNWalletConnectViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6FE417F2383A0C10096AAD8 /* KNWalletConnectViewController.swift */; };
C6FE41822383A0C10096AAD8 /* KNWalletConnectViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C6FE41802383A0C10096AAD8 /* KNWalletConnectViewController.xib */; };
CCA4FE361FD4282400749AE4 /* DeviceChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = CCA4FE351FD4282400749AE4 /* DeviceChecker.swift */; };
CCA4FE381FD428B300749AE4 /* JailbreakChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = CCA4FE371FD428B300749AE4 /* JailbreakChecker.swift */; };
CCA4FE3A1FD42B4100749AE4 /* FakeJailbreakChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = CCA4FE391FD42B4100749AE4 /* FakeJailbreakChecker.swift */; };
Expand Down Expand Up @@ -1198,6 +1200,8 @@
C680C11F2382958700A621CB /* KNVersionControlManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KNVersionControlManager.swift; sourceTree = "<group>"; };
C68AA274237A6BE900B9EC35 /* KNTransactionStatusPopUp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KNTransactionStatusPopUp.swift; sourceTree = "<group>"; };
C68AA275237A6BE900B9EC35 /* KNTransactionStatusPopUp.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KNTransactionStatusPopUp.xib; sourceTree = "<group>"; };
C6FE417F2383A0C10096AAD8 /* KNWalletConnectViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KNWalletConnectViewController.swift; sourceTree = "<group>"; };
C6FE41802383A0C10096AAD8 /* KNWalletConnectViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KNWalletConnectViewController.xib; sourceTree = "<group>"; };
CCA4FE351FD4282400749AE4 /* DeviceChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceChecker.swift; sourceTree = "<group>"; };
CCA4FE371FD428B300749AE4 /* JailbreakChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JailbreakChecker.swift; sourceTree = "<group>"; };
CCA4FE391FD42B4100749AE4 /* FakeJailbreakChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FakeJailbreakChecker.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2137,6 +2141,7 @@
25682B6C20EE0315009738C4 /* Wallet */ = {
isa = PBXGroup;
children = (
C6FE417E2383A0AB0096AAD8 /* WalletConnect */,
253F50E321E5B4E70068813D /* PromoCode */,
25A4B4AF21242DC300924351 /* Create */,
25696F4620BD0ECD002CC53F /* HeaderView */,
Expand Down Expand Up @@ -4005,6 +4010,15 @@
path = TransactionStatus;
sourceTree = "<group>";
};
C6FE417E2383A0AB0096AAD8 /* WalletConnect */ = {
isa = PBXGroup;
children = (
C6FE417F2383A0C10096AAD8 /* KNWalletConnectViewController.swift */,
C6FE41802383A0C10096AAD8 /* KNWalletConnectViewController.xib */,
);
path = WalletConnect;
sourceTree = "<group>";
};
CCA4FE341FD427CA00749AE4 /* Helpers */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -4195,6 +4209,7 @@
25073D23211AF20D00833C92 /* Montserrat-Regular.ttf in Resources */,
25073D28211AF20D00833C92 /* Montserrat-ExtraLight.ttf in Resources */,
25FB472422CC939F00374CE8 /* config_env_staging2.json in Resources */,
C6FE41822383A0C10096AAD8 /* KNWalletConnectViewController.xib in Resources */,
25A91D6F226ECAE2001F2ABA /* KNEnterTwoFactorAuthenViewController.xib in Resources */,
254EBED2225F5BA500042FA2 /* KNEligibleTokensViewController.xib in Resources */,
25682ABF229FC3C7004AB452 /* KNApproveTokenViewController.xib in Resources */,
Expand Down Expand Up @@ -4504,6 +4519,7 @@
"${BUILT_PRODUCTS_DIR}/MBProgressHUD/MBProgressHUD.framework",
"${BUILT_PRODUCTS_DIR}/MSCircularSlider/MSCircularSlider.framework",
"${BUILT_PRODUCTS_DIR}/Moya/Moya.framework",
"${BUILT_PRODUCTS_DIR}/PromiseKit/PromiseKit.framework",
"${BUILT_PRODUCTS_DIR}/Protobuf/Protobuf.framework",
"${BUILT_PRODUCTS_DIR}/QRCodeReaderViewController/QRCodeReaderViewController.framework",
"${BUILT_PRODUCTS_DIR}/Realm/Realm.framework",
Expand All @@ -4512,6 +4528,7 @@
"${BUILT_PRODUCTS_DIR}/SAMKeychain/SAMKeychain.framework",
"${BUILT_PRODUCTS_DIR}/SeedStackViewController/StackViewController.framework",
"${BUILT_PRODUCTS_DIR}/SipHash/SipHash.framework",
"${BUILT_PRODUCTS_DIR}/Starscream/Starscream.framework",
"${BUILT_PRODUCTS_DIR}/StatefulViewController/StatefulViewController.framework",
"${BUILT_PRODUCTS_DIR}/SwiftChart/SwiftChart.framework",
"${BUILT_PRODUCTS_DIR}/SwiftMessages/SwiftMessages.framework",
Expand All @@ -4520,6 +4537,7 @@
"${BUILT_PRODUCTS_DIR}/TrustKeystore/TrustKeystore.framework",
"${PODS_ROOT}/TwitterCore/iOS/TwitterCore.framework",
"${PODS_ROOT}/TwitterKit/iOS/TwitterKit.framework",
"${BUILT_PRODUCTS_DIR}/WalletConnect/WalletConnect.framework",
"${BUILT_PRODUCTS_DIR}/nanopb/nanopb.framework",
);
name = "[CP] Embed Pods Frameworks";
Expand Down Expand Up @@ -4548,6 +4566,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MBProgressHUD.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MSCircularSlider.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Moya.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/PromiseKit.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Protobuf.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/QRCodeReaderViewController.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Realm.framework",
Expand All @@ -4556,6 +4575,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SAMKeychain.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/StackViewController.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SipHash.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Starscream.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/StatefulViewController.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftChart.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftMessages.framework",
Expand All @@ -4564,6 +4584,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/TrustKeystore.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/TwitterCore.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/TwitterKit.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/WalletConnect.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/nanopb.framework",
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -4797,6 +4818,7 @@
25CC0AA82058F1A00009D0D5 /* KNExchangeRequest.swift in Sources */,
25377B0D22842AC70012178F /* KNLimitOrderTabCoordinator.swift in Sources */,
291F52A51F6B762300B369AB /* EtherServiceRequest.swift in Sources */,
C6FE41812383A0C10096AAD8 /* KNWalletConnectViewController.swift in Sources */,
CCA4FE361FD4282400749AE4 /* DeviceChecker.swift in Sources */,
25931F1420AEEBF000BAC774 /* KNBackUpWalletViewModel.swift in Sources */,
25C45ADA20AD40F400CDA1FF /* KNImportPrivateKeyViewController.swift in Sources */,
Expand Down Expand Up @@ -5162,7 +5184,7 @@
ENABLE_BITCODE = YES;
ENABLE_ON_DEMAND_RESOURCES = NO;
INFOPLIST_FILE = KyberNetwork/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 2.0.19;
OTHER_SWIFT_FLAGS = "$(inherited) \"-D\" \"COCOAPODS\" -D DEBUG";
Expand Down Expand Up @@ -5191,7 +5213,7 @@
ENABLE_BITCODE = YES;
ENABLE_ON_DEMAND_RESOURCES = NO;
INFOPLIST_FILE = KyberNetwork/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 2.0.19;
PRODUCT_BUNDLE_IDENTIFIER = com.kyberswap.ios;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,83 @@ class KNExternalProvider {
}
}

func sendTxWalletConnect(txData: JSONDictionary, completion: @escaping (Result<String?, AnyError>) -> Void) {
guard let value = (txData["value"] as? String ?? "").fullBigInt(decimals: 0),
let from = txData["from"] as? String, let to = txData["to"] as? String,
let gasPrice = (txData["gasPrice"] as? String ?? "").fullBigInt(decimals: 0),
let gasLimit = (txData["gasLimit"] as? String ?? "").fullBigInt(decimals: 0),
from.lowercased() == self.account.address.description.lowercased(),
!gasPrice.isZero, !gasLimit.isZero else {
completion(.success(nil))
return
}

// Parse data from hex string
let dataParse: Data? = {
let string = (txData["data"] as? String ?? "").drop0x
if string.isEmpty { return Data() }
var data = Data(capacity: string.count / 2)

let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)
regex.enumerateMatches(in: string, range: NSRange(string.startIndex..., in: string)) { match, _, _ in
let byteString = (string as NSString).substring(with: match!.range)
let num = UInt8(byteString, radix: 16)!
data.append(num)
}

guard !data.isEmpty else { return nil }

return data
}()
guard let data = dataParse else {
completion(.success(nil))
return
}

guard let toAddr = Address(string: to) else {
completion(.success(nil))
return
}
self.getTransactionCount { [weak self] txCountResult in
guard let `self` = self else {
completion(.success(nil))
return
}
switch txCountResult {
case .success:
let signTx = SignTransaction(
value: value,
account: self.account,
to: toAddr,
nonce: self.minTxCount,
data: data,
gasPrice: gasPrice,
gasLimit: gasLimit,
chainID: KNEnvironment.default.chainID
)
self.signTransactionData(from: signTx) { [weak self] signResult in
switch signResult {
case .success(let signData):
KNGeneralProvider.shared.sendSignedTransactionData(signData, completion: { [weak self] result in
guard let `self` = self else { return }
switch result {
case .success(let txHash):
self.minTxCount += 1
completion(.success(txHash))
case .failure(let error):
completion(.failure(error))
}
})
case .failure(let error):
completion(.failure(error))
}
}
case .failure(let error):
completion(.failure(error))
}
}
}

func getReceipt(for transaction: KNTransaction, completion: @escaping (Result<KNTransaction, AnyError>) -> Void) {
let request = KNGetTransactionReceiptRequest(hash: transaction.id)
Session.send(EtherServiceRequest(batch: BatchFactory().create(request))) { [weak self] result in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum KNBalanceTabHamburgerMenuViewEvent {
case selectSendToken
case selectAllTransactions
case selectPromoCode
case selectWalletConnect
}

protocol KNBalanceTabHamburgerMenuViewControllerDelegate: class {
Expand Down Expand Up @@ -92,6 +93,9 @@ class KNBalanceTabHamburgerMenuViewController: KNBaseViewController {
@IBOutlet weak var walletListTableView: UITableView!
@IBOutlet weak var walletListTableViewHeightConstraint: NSLayoutConstraint!

@IBOutlet weak var walletConnectLabel: UILabel!
@IBOutlet weak var walletConnectView: UIView!

@IBOutlet weak var sendTokenButton: UIButton!
@IBOutlet weak var hamburgerMenuViewTrailingConstraint: NSLayoutConstraint!

Expand Down Expand Up @@ -153,6 +157,9 @@ class KNBalanceTabHamburgerMenuViewController: KNBaseViewController {
let promoTapGesture = UITapGestureRecognizer(target: self, action: #selector(self.promoCodeTapped(_:)))
self.promoCodeContainerView.addGestureRecognizer(promoTapGesture)

let walletConnectGesture = UITapGestureRecognizer(target: self, action: #selector(self.walletConnectTapped(_:)))
self.walletConnectView.addGestureRecognizer(walletConnectGesture)

self.mywalletsTextLabel.text = NSLocalizedString("my.wallets", value: "My wallet(s)", comment: "").uppercased()
self.update(transactions: self.viewModel.pendingTransactions)

Expand Down Expand Up @@ -217,6 +224,13 @@ class KNBalanceTabHamburgerMenuViewController: KNBaseViewController {
KNCrashlyticsUtil.logCustomEvent(withName: "hamburger_menu", customAttributes: ["type": "kybercode"])
}

@objc func walletConnectTapped(_ sender: Any?) {
self.hideMenu(animated: true) {
self.delegate?.balanceTabHamburgerMenuViewController(self, run: .selectWalletConnect)
}
KNCrashlyticsUtil.logCustomEvent(withName: "hamburger_menu", customAttributes: ["type": "wallet_connect"])
}

@objc func backgroundViewTap(_ recognizer: UITapGestureRecognizer) {
let point = recognizer.location(in: self.view)
if point.x <= self.view.frame.width - self.hamburgerView.frame.width {
Expand Down
Loading

0 comments on commit 83d568d

Please sign in to comment.