Skip to content

Commit

Permalink
fix import identity (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanlele authored Jun 28, 2024
1 parent cc4f013 commit 5a8c033
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 63 deletions.
8 changes: 4 additions & 4 deletions Rarime.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
Expand Down Expand Up @@ -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)";
Expand Down Expand Up @@ -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)";
Expand Down Expand Up @@ -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)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down Expand Up @@ -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"
}
},
{
Expand Down
21 changes: 19 additions & 2 deletions Rarime/Code/Managers/DecentralizeAuthManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 5 additions & 7 deletions Rarime/Code/Modules/Home/Views/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ struct HomeView: View {
defer {
self.isBalanceFetching = false
}

if userManager.user?.userReferalCode == nil {
return
}

do {
if isWalletBalanceDisplayed {
Expand All @@ -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)

Expand Down
8 changes: 1 addition & 7 deletions Rarime/Code/Modules/Home/Views/RewardsIntroView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
77 changes: 59 additions & 18 deletions Rarime/Code/Modules/Intro/Views/ImportIdentityView.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
//

import SwiftUI
import Alamofire

struct ImportIdentityView: View {
@EnvironmentObject private var decentralizedAuthManager: DecentralizedAuthManager
@EnvironmentObject private var userManager: UserManager

var onNext: () -> Void
var onBack: () -> Void

@State private var privateKeyHex = ""
@State private var isInvalidPrivateKey = false
@State private var isImporting = false

var body: some View {
IdentityStepLayoutView(
Expand All @@ -23,7 +24,9 @@ struct ImportIdentityView: View {
text: "Continue",
rightIcon: Icons.arrowRight,
action: importIdentity
).controlSize(.large)
)
.controlSize(.large)
.disabled(isImporting)
}
) {
VStack {
Expand All @@ -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
}
}
}
Expand All @@ -91,5 +131,6 @@ fileprivate func isValidPrivateKey(_ privateKey: String) throws -> Bool {

#Preview {
ImportIdentityView(onNext: {}, onBack: {})
.environmentObject(DecentralizedAuthManager.shared)
.environmentObject(UserManager.shared)
}
16 changes: 2 additions & 14 deletions Rarime/Code/Modules/Rewards/Views/RewardsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit 5a8c033

Please sign in to comment.