From 5a8c033561315246e2321eac13f90167c8227bb1 Mon Sep 17 00:00:00 2001 From: Ivan Lele <110336129+ivanlele@users.noreply.github.com> Date: Fri, 28 Jun 2024 21:53:32 +0700 Subject: [PATCH] fix import identity (#69) --- Rarime.xcodeproj/project.pbxproj | 8 +- .../xcshareddata/swiftpm/Package.resolved | 8 +- .../Managers/DecentralizeAuthManager.swift | 21 ++++- Rarime/Code/Modules/Home/Views/HomeView.swift | 12 ++- .../Modules/Home/Views/RewardsIntroView.swift | 8 +- .../Intro/Views/ImportIdentityView.swift | 77 ++++++++++++++----- .../Modules/Rewards/Views/RewardsView.swift | 16 +--- .../Views/ReserveTokensView.swift | 8 +- 8 files changed, 95 insertions(+), 63 deletions(-) diff --git a/Rarime.xcodeproj/project.pbxproj b/Rarime.xcodeproj/project.pbxproj index d9be7531..2a772702 100644 --- a/Rarime.xcodeproj/project.pbxproj +++ b/Rarime.xcodeproj/project.pbxproj @@ -1483,7 +1483,7 @@ "$(inherited)", "$(PROJECT_DIR)/Frameworks", ); - MARKETING_VERSION = 1.1; + MARKETING_VERSION = 1.3; OTHER_LDFLAGS = "-lc++"; PRODUCT_BUNDLE_IDENTIFIER = Rarilabs.Rarime; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -1533,7 +1533,7 @@ "$(inherited)", "$(PROJECT_DIR)/Frameworks", ); - MARKETING_VERSION = 1.1; + MARKETING_VERSION = 1.3; OTHER_LDFLAGS = "-lc++"; PRODUCT_BUNDLE_IDENTIFIER = Rarilabs.Rarime; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -1649,7 +1649,7 @@ "$(inherited)", "$(PROJECT_DIR)/Frameworks", ); - MARKETING_VERSION = 1.1; + MARKETING_VERSION = 1.3; OTHER_LDFLAGS = "-lc++"; PRODUCT_BUNDLE_IDENTIFIER = Rarilabs.Rarime; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -1758,7 +1758,7 @@ "$(inherited)", "$(PROJECT_DIR)/Frameworks", ); - MARKETING_VERSION = 1.1; + MARKETING_VERSION = 1.3; OTHER_LDFLAGS = "-lc++"; PRODUCT_BUNDLE_IDENTIFIER = Rarilabs.Rarime; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/Rarime.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Rarime.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 5792f3f1..6dfcada4 100644 --- a/Rarime.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Rarime.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -15,8 +15,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/attaswift/BigInt.git", "state" : { - "revision" : "0ed110f7555c34ff468e72e1686e59721f2b0da6", - "version" : "5.3.0" + "revision" : "88071df9f27361b52d92f9acea85e8b658af8183", + "version" : "5.4.0" } }, { @@ -248,8 +248,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-system.git", "state" : { - "revision" : "f9266c85189c2751589a50ea5aec72799797e471", - "version" : "1.3.0" + "revision" : "6a9e38e7bd22a3b8ba80bddf395623cf68f57807", + "version" : "1.3.1" } }, { diff --git a/Rarime/Code/Managers/DecentralizeAuthManager.swift b/Rarime/Code/Managers/DecentralizeAuthManager.swift index d86e3e05..931d5c35 100644 --- a/Rarime/Code/Managers/DecentralizeAuthManager.swift +++ b/Rarime/Code/Managers/DecentralizeAuthManager.swift @@ -77,8 +77,25 @@ class DecentralizedAuthManager: ObservableObject { let refreshJwtReponse = try await authorize.refreshJwt(refreshJwt.raw) - accessJwt = try JWT(refreshJwtReponse.data.attributes.accessToken.token) - self.refreshJwt = try JWT(refreshJwtReponse.data.attributes.refreshToken.token) + let newAccessJwt = try JWT(refreshJwtReponse.data.attributes.accessToken.token) + let newRefreshJwt = try JWT(refreshJwtReponse.data.attributes.refreshToken.token) + + DispatchQueue.main.async { + self.accessJwt = newAccessJwt + self.refreshJwt = newRefreshJwt + } + } + + func getAccessJwt(_ user: User) async throws -> JWT { + if self.accessJwt == nil { + try await self.initializeJWT(user.secretKey) + } + + try await self.refreshIfNeeded() + + guard let accessJwt = self.accessJwt else { throw "accessJwt is nil" } + + return accessJwt } func reset() { diff --git a/Rarime/Code/Modules/Home/Views/HomeView.swift b/Rarime/Code/Modules/Home/Views/HomeView.swift index cf29dcb0..7e80c619 100644 --- a/Rarime/Code/Modules/Home/Views/HomeView.swift +++ b/Rarime/Code/Modules/Home/Views/HomeView.swift @@ -268,6 +268,10 @@ struct HomeView: View { defer { self.isBalanceFetching = false } + + if userManager.user?.userReferalCode == nil { + return + } do { if isWalletBalanceDisplayed { @@ -278,13 +282,7 @@ struct HomeView: View { guard let user = userManager.user else { throw "failed to get user" } - if decentralizedAuthManager.accessJwt == nil { - try await decentralizedAuthManager.initializeJWT(user.secretKey) - } - - try await decentralizedAuthManager.refreshIfNeeded() - - guard let accessJwt = decentralizedAuthManager.accessJwt else { throw "accessJwt is nil" } + let accessJwt = try await decentralizedAuthManager.getAccessJwt(user) let pointsBalance = try await userManager.fetchPointsBalance(accessJwt) diff --git a/Rarime/Code/Modules/Home/Views/RewardsIntroView.swift b/Rarime/Code/Modules/Home/Views/RewardsIntroView.swift index 193c0b26..33e9474d 100644 --- a/Rarime/Code/Modules/Home/Views/RewardsIntroView.swift +++ b/Rarime/Code/Modules/Home/Views/RewardsIntroView.swift @@ -35,13 +35,7 @@ struct RewardsIntroView: View { do { guard let user = userManager.user else { throw "user is not initalized" } - if decentralizedAuthManager.accessJwt == nil { - try await decentralizedAuthManager.initializeJWT(user.secretKey) - } - - try await decentralizedAuthManager.refreshIfNeeded() - - guard let accessJwt = decentralizedAuthManager.accessJwt else { throw "accessJwt is nil" } + let accessJwt = try await decentralizedAuthManager.getAccessJwt(user) let pointsSvc = Points(ConfigManager.shared.api.pointsServiceURL) let result = try await pointsSvc.createPointsBalance( diff --git a/Rarime/Code/Modules/Intro/Views/ImportIdentityView.swift b/Rarime/Code/Modules/Intro/Views/ImportIdentityView.swift index 8d5d7f8d..9a0e6ee5 100644 --- a/Rarime/Code/Modules/Intro/Views/ImportIdentityView.swift +++ b/Rarime/Code/Modules/Intro/Views/ImportIdentityView.swift @@ -1,8 +1,8 @@ -// - import SwiftUI +import Alamofire struct ImportIdentityView: View { + @EnvironmentObject private var decentralizedAuthManager: DecentralizedAuthManager @EnvironmentObject private var userManager: UserManager var onNext: () -> Void @@ -10,6 +10,7 @@ struct ImportIdentityView: View { @State private var privateKeyHex = "" @State private var isInvalidPrivateKey = false + @State private var isImporting = false var body: some View { IdentityStepLayoutView( @@ -23,7 +24,9 @@ struct ImportIdentityView: View { text: "Continue", rightIcon: Icons.arrowRight, action: importIdentity - ).controlSize(.large) + ) + .controlSize(.large) + .disabled(isImporting) } ) { VStack { @@ -49,29 +52,66 @@ struct ImportIdentityView: View { } func importIdentity() { - do { - if !(try isValidPrivateKey(privateKeyHex)) { - privateKeyHex = "" - isInvalidPrivateKey = true - - return + self.isImporting = true + + Task { @MainActor in + defer { + self.isImporting = false } - guard let privateKey = Data(hex: privateKeyHex) else { - privateKeyHex = "" - isInvalidPrivateKey = true + do { + if !(try isValidPrivateKey(privateKeyHex)) { + privateKeyHex = "" + isInvalidPrivateKey = true + + return + } - return + guard let privateKey = Data(hex: privateKeyHex) else { + privateKeyHex = "" + isInvalidPrivateKey = true + + return + } + + try userManager.createFromSecretKey(privateKey) + try userManager.user?.save() + + try await setReferralCodeIfUserHasPointsBalance() + + LoggerUtil.common.info("Identity was imported") + + onNext() + } catch { + LoggerUtil.common.error("failed to import identity: \(error, privacy: .public)") } + } + } + + func setReferralCodeIfUserHasPointsBalance() async throws { + do { + guard let user = userManager.user else { throw "failed to get user" } + + let accessJwt = try await decentralizedAuthManager.getAccessJwt(user) - try userManager.createFromSecretKey(privateKey) - try userManager.user?.save() + let points = Points(ConfigManager.shared.api.pointsServiceURL) + let _ = try await points.getPointsBalance(accessJwt) - LoggerUtil.common.info("Identity was imported") + LoggerUtil.common.info("User has points balance, setting referral code") - onNext() + userManager.user?.userReferalCode = "placeholder" } catch { - LoggerUtil.common.error("failed to import identity") + guard let error = error as? AFError else { throw error } + + let openApiHttpCode = try error.retriveOpenApiHttpCode() + + if openApiHttpCode == HTTPStatusCode.notFound.rawValue { + LoggerUtil.common.info("User has no points balance") + + return + } + + throw error } } } @@ -91,5 +131,6 @@ fileprivate func isValidPrivateKey(_ privateKey: String) throws -> Bool { #Preview { ImportIdentityView(onNext: {}, onBack: {}) + .environmentObject(DecentralizedAuthManager.shared) .environmentObject(UserManager.shared) } diff --git a/Rarime/Code/Modules/Rewards/Views/RewardsView.swift b/Rarime/Code/Modules/Rewards/Views/RewardsView.swift index 17848d92..cf98c245 100644 --- a/Rarime/Code/Modules/Rewards/Views/RewardsView.swift +++ b/Rarime/Code/Modules/Rewards/Views/RewardsView.swift @@ -276,13 +276,7 @@ struct RewardsView: View { return } - if decentralizedAuthManager.accessJwt == nil { - try await decentralizedAuthManager.initializeJWT(user.secretKey) - } - - try await decentralizedAuthManager.refreshIfNeeded() - - guard let accessJwt = decentralizedAuthManager.accessJwt else { throw "accessJwt is nil" } + let accessJwt = try await decentralizedAuthManager.getAccessJwt(user) let points = Points(ConfigManager.shared.api.pointsServiceURL) @@ -303,13 +297,7 @@ struct RewardsView: View { do { guard let user = userManager.user else { throw "user is not initalized" } - if decentralizedAuthManager.accessJwt == nil { - try await decentralizedAuthManager.initializeJWT(user.secretKey) - } - - try await decentralizedAuthManager.refreshIfNeeded() - - guard let accessJwt = decentralizedAuthManager.accessJwt else { throw "accessJwt is nil" } + let accessJwt = try await decentralizedAuthManager.getAccessJwt(user) let points = Points(ConfigManager.shared.api.pointsServiceURL) diff --git a/Rarime/Code/Modules/ScanPassport/Views/ReserveTokensView.swift b/Rarime/Code/Modules/ScanPassport/Views/ReserveTokensView.swift index 0ca8d059..1bded616 100644 --- a/Rarime/Code/Modules/ScanPassport/Views/ReserveTokensView.swift +++ b/Rarime/Code/Modules/ScanPassport/Views/ReserveTokensView.swift @@ -49,13 +49,7 @@ struct ReserveTokensView: View { guard let user = userManager.user else { throw "failed to get user" } - if decentralizedAuthManager.accessJwt == nil { - try await decentralizedAuthManager.initializeJWT(user.secretKey) - } - - try await decentralizedAuthManager.refreshIfNeeded() - - guard let accessJwt = decentralizedAuthManager.accessJwt else { throw "accessJwt is nil" } + let accessJwt = try await decentralizedAuthManager.getAccessJwt(user) guard let passport else { throw "passport is nil" } guard let registerZkProof = userManager.registerZkProof else { throw "registerZkProof is nil" }