Skip to content

Commit

Permalink
[ABW-2347 ] Post mainnet live, default to mainnet, stop checking if m…
Browse files Browse the repository at this point in the history
…ainnet is live (#796)
  • Loading branch information
CyonAlexRDX committed Sep 27, 2023
1 parent bb90bb0 commit 3981b6b
Show file tree
Hide file tree
Showing 41 changed files with 182 additions and 414 deletions.
1 change: 0 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,6 @@ package.addModules([
name: "ImportLegacyWalletClient",
dependencies: [
"AccountsClient",
"AppPreferencesClient", // FIXME: Post mainnet delete (read dev mode)
"LedgerHardwareWalletClient",
"Profile", // Olympia models
"EngineKit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ extension AuthorizedDappsClient {
if updatedAuthedDapp != authorizedDapp {
// Write back `updatedAuthedDapp` to Profile only if changes were needed
try await updateAuthorizedDapp(updatedAuthedDapp)
} else {
loggerGlobal.feature("nothing to do... skipped updating authorizedDapp")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public struct GatewayAPIClient: Sendable, DependencyKey {
public static var rdxClientVersion: String?

// MARK: Request
public var isMainnetLive: IsMainnetLive
public var getNetworkName: GetNetworkName
public var getEpoch: GetEpoch

Expand All @@ -34,8 +33,6 @@ public struct GatewayAPIClient: Sendable, DependencyKey {
}

extension GatewayAPIClient {
public typealias IsMainnetLive = @Sendable () async -> Bool

public typealias GetNetworkName = @Sendable (URL) async throws -> Radix.Network.Name
public typealias GetEpoch = @Sendable () async throws -> Epoch

Expand All @@ -59,14 +56,6 @@ extension GatewayAPIClient {
public typealias TransactionPreview = @Sendable (GatewayAPI.TransactionPreviewRequest) async throws -> GatewayAPI.TransactionPreviewResponse
}

// MARK: - IsMainnetLiveResponse
public struct IsMainnetLiveResponse: Decodable, Sendable, Hashable {
public let isMainnetLive: Bool
public init(isMainnetLive: Bool) {
self.isMainnetLive = isMainnetLive
}
}

extension GatewayAPIClient {
@Sendable
public func getSingleEntityDetails(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,6 @@ extension GatewayAPIClient {
}

return GatewayAPIClient(
isMainnetLive: {
do {
return try await makeRequest(
responseType: IsMainnetLiveResponse.self,
baseURL: URL(string: "https://mainnet-status.extratools.works")!,
timeoutInterval: 1
) {
$0
}.isMainnetLive
} catch {
loggerGlobal.notice("Failed to get mainnet is online status, error: \(error)")
return false
}
},
getNetworkName: { baseURL in
let response = try await makeRequest(
responseType: GatewayAPI.GatewayStatusResponse.self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ extension GatewayAPIClient: TestDependencyKey {
public static let previewValue = Self.mock()

public static let testValue = Self(
isMainnetLive: unimplemented("\(Self.self).isMainnetLive"),
getNetworkName: unimplemented("\(Self.self).getNetworkName"),
getEpoch: unimplemented("\(Self.self).getEpoch"),
getEntityDetails: unimplemented("\(Self.self).getEntityDetails"),
Expand All @@ -31,7 +30,6 @@ extension GatewayAPIClient: TestDependencyKey {
txStatus: GatewayAPI.TransactionStatus? = nil
) -> Self {
.init(
isMainnetLive: { true },
getNetworkName: { _ in .init("Nebunet") },
getEpoch: { .init(rawValue: 123) },
getEntityDetails: unimplemented("\(self).getEntityDetails"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,36 @@ extension ImportLegacyWalletClient {

import EngineKit

extension ImportLegacyWalletClient {
// FIXME: Post mainnet remove this function, only used to allow DEBUG builds import Olympia wallets
public static func canImportOlympiaWallet(
currentNetworkID: NetworkID,
isDeveloperModeEnabled: Bool
) -> Bool {
networkIDForOlympiaAccountsToImportInto(
currentNetworkID: currentNetworkID,
isDeveloperModeEnabled: isDeveloperModeEnabled
) != nil
}

// FIXME: Post mainnet remove this function, only used to allow DEBUG builds import Olympia wallets
/// Returns `nil` if it is not supported to import olympia accounts at all, given the input
public static func networkIDForOlympiaAccountsToImportInto(
currentNetworkID: NetworkID,
isDeveloperModeEnabled: Bool
) -> NetworkID? {
guard currentNetworkID == .mainnet else {
#if DEBUG
if isDeveloperModeEnabled {
// ONLY for DEBUG builds where `isDeveloperModeEnabled` is set, we allow
// importing into non mainnet
return currentNetworkID
}
#endif

// Current network is not mainnet, return nil marking it is not possible to import olympia accounts into non mainnet network
return nil
}
return .mainnet
}
}
// extension ImportLegacyWalletClient {
// // FIXME: Post mainnet remove this function, only used to allow DEBUG builds import Olympia wallets
// public static func canImportOlympiaWallet(
// currentNetworkID: NetworkID,
// isDeveloperModeEnabled: Bool
// ) -> Bool {
// networkIDForOlympiaAccountsToImportInto(
// currentNetworkID: currentNetworkID,
// isDeveloperModeEnabled: isDeveloperModeEnabled
// ) != nil
// }
//
// // FIXME: Post mainnet remove this function, only used to allow DEBUG builds import Olympia wallets
// /// Returns `nil` if it is not supported to import olympia accounts at all, given the input
// public static func networkIDForOlympiaAccountsToImportInto(
// currentNetworkID: NetworkID,
// isDeveloperModeEnabled: Bool
// ) -> NetworkID? {
// guard currentNetworkID == .mainnet else {
// #if DEBUG
// if isDeveloperModeEnabled {
// // ONLY for DEBUG builds where `isDeveloperModeEnabled` is set, we allow
// // importing into non mainnet
// return currentNetworkID
// }
// #endif
//
// // Current network is not mainnet, return nil marking it is not possible to import olympia accounts into non mainnet network
// return nil
// }
// return .mainnet
// }
// }
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AccountsClient
import AppPreferencesClient
import ClientPrelude
import Cryptography
import EngineKit
Expand All @@ -12,23 +11,14 @@ extension ImportLegacyWalletClient: DependencyKey {

public static let liveValue: Self = {
@Dependency(\.accountsClient) var accountsClient
@Dependency(\.appPreferencesClient) var appPreferencesClient
@Dependency(\.factorSourcesClient) var factorSourcesClient

@Sendable func migrate(
accounts: NonEmpty<Set<OlympiaAccountToMigrate>>,
factorSouceID: FactorSourceID.FromHash
) async throws -> (accounts: NonEmpty<OrderedSet<MigratedAccount>>, networkID: NetworkID) {
guard
let networkID = await networkIDForOlympiaAccountsToImportInto(
currentNetworkID: factorSourcesClient.getCurrentNetworkID(),
isDeveloperModeEnabled: appPreferencesClient.isDeveloperModeEnabled()
)
else {
struct NotPermittedToImportOlympiaAccountsToTestnet: Swift.Error {}
throw NotPermittedToImportOlympiaAccountsToTestnet()
}

// we only allow import of olympia accounts into mainnet
let networkID = NetworkID.mainnet
let sortedOlympia = accounts.sorted(by: \.addressIndex)

let accountIndexBase = await accountsClient.nextAccountIndex(networkID)
Expand Down Expand Up @@ -98,10 +88,7 @@ extension ImportLegacyWalletClient: DependencyKey {
loggerGlobal.warning("Failed to load accounts, error: \(error)")
}

return await canImportOlympiaWallet(
currentNetworkID: factorSourcesClient.getCurrentNetworkID(),
isDeveloperModeEnabled: appPreferencesClient.isDeveloperModeEnabled()
)
return await factorSourcesClient.getCurrentNetworkID() == .mainnet
},
parseHeaderFromQRCode: {
let header = try CAP33.deserializeHeader(payload: $0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import ClientPrelude

// MARK: - NetworkSwitchingClient
public struct NetworkSwitchingClient: Sendable, DependencyKey {
public var hasMainnetEverBeenLive: HasMainnetEverBeenLive
public var validateGatewayURL: ValidateGatewayURL
public var hasAccountOnNetwork: HasAccountOnNetwork
public var switchTo: SwitchTo
}

extension NetworkSwitchingClient {
public typealias HasMainnetEverBeenLive = @Sendable () async -> Bool
public typealias ValidateGatewayURL = @Sendable (URL) async throws -> Radix.Gateway?
public typealias HasAccountOnNetwork = @Sendable (Radix.Gateway) async throws -> Bool
public typealias SwitchTo = @Sendable (Radix.Gateway) async throws -> Radix.Gateway
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,6 @@ extension NetworkSwitchingClient {
}

return Self(
hasMainnetEverBeenLive: {
if userDefaultsClient.hasMainnetEverBeenLive {
loggerGlobal.debug("Mainnet has been live before..")
return true
}
loggerGlobal.debug("Mainnet has never been live before, checking if it is live now")
let isLive = await gatewayAPIClient.isMainnetLive()
if isLive {
loggerGlobal.notice("Mainnet is live, saving that is has been seen to be live...")
await userDefaultsClient.setMainnetIsLive()
} else {
loggerGlobal.notice("Mainnet is not live")
}
return isLive
},
validateGatewayURL: validateGatewayURL,
hasAccountOnNetwork: hasAccountOnNetwork,
switchTo: switchTo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import ClientPrelude
#if DEBUG
extension NetworkSwitchingClient: TestDependencyKey {
public static let testValue: Self = .init(
hasMainnetEverBeenLive: unimplemented("\(Self.self).hasMainnetEverBeenLive"),
validateGatewayURL: unimplemented("\(Self.self).validateGatewayURL"),
hasAccountOnNetwork: unimplemented("\(Self.self).hasAccountOnNetwork"),
switchTo: unimplemented("\(Self.self).switchTo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ extension OnboardingClient {
public typealias LoadProfile = @Sendable () async -> LoadProfileOutcome

/// Returns `true` iff Profile contains any mainnet accounts
public typealias CommitEphemeral = @Sendable () async throws -> Bool
public typealias CommitEphemeral = @Sendable () async throws -> Prelude.Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ extension OnboardingClient: TestDependencyKey {

public static let noop = Self(
loadProfile: { .newUser },
commitEphemeral: { false }
commitEphemeral: { Prelude.Unit.instance }
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extension OnboardingClient: DependencyKey {
},
commitEphemeral: {
try await getProfileStore().commitEphemeral()
return Prelude.Unit.instance
}
)
}
Expand Down
8 changes: 3 additions & 5 deletions Sources/Clients/ProfileStore/ProfileStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ extension ProfileStore {
public func getLoadProfileOutcome() async -> LoadProfileOutcome {
switch self.profileStateSubject.value {
case let .persisted(profile):
return .existingProfile(hasMainnetAccounts: profile.hasMainnetAccounts)
return .existingProfile
case let .ephemeral(ephemeral):
if let error = ephemeral.loadFailure {
return .usersExistingProfileCouldNotBeLoaded(failure: error)
Expand Down Expand Up @@ -184,11 +184,9 @@ extension ProfileStore {
}
}

/// Returns `true` iff Profile contains any mainnet accounts
public func commitEphemeral() async throws -> Bool {
public func commitEphemeral() async throws {
let ephemeral = try assertProfileStateIsEphemeral()
try await changeProfileSnapshot(to: ephemeral.profile.snapshot())
return ephemeral.profile.hasMainnetAccounts
}

/// If persisted: updates the in-memory across-the-app-used Profile and also
Expand Down Expand Up @@ -552,7 +550,7 @@ extension ProfileStore {
contentHint: .init() // Empty initially
)

loggerGlobal.debug("Created new ephemeral profile with ID: \(header.id), and device factorSourceID: \(factorSource.id)")
loggerGlobal.trace("Created new ephemeral profile with ID: \(header.id), and device factorSourceID: \(factorSource.id)")

return Profile(header: header, deviceFactorSource: factorSource)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ public struct AccountDetails: Sendable, FeatureReducer {
}

public enum InternalAction: Sendable, Equatable {
case transfer(hasMainnetEverBeenLive: Bool)

case markBackupNeeded
case accountUpdated(Profile.Network.Account)
case portfolioLoaded(AccountPortfolio)
Expand Down Expand Up @@ -195,10 +193,10 @@ public struct AccountDetails: Sendable, FeatureReducer {
return .none

case .transferButtonTapped:
return .run { send in
let hasMainnetEverBeenLive = await networkSwitchingClient.hasMainnetEverBeenLive()
await send(.internal(.transfer(hasMainnetEverBeenLive: hasMainnetEverBeenLive)))
}
state.destination = .transfer(.init(
from: state.account
))
return .none

case .exportMnemonicButtonTapped:
return loadMnemonic(state: state)
Expand Down Expand Up @@ -244,7 +242,6 @@ public struct AccountDetails: Sendable, FeatureReducer {
return checkAccountSecurityPromptStatus(state: &state)

case .destination(.dismiss):
loggerGlobal.feature("Dismissed child")
return checkAccountSecurityPromptStatus(state: &state)

default:
Expand All @@ -254,13 +251,6 @@ public struct AccountDetails: Sendable, FeatureReducer {

public func reduce(into state: inout State, internalAction: InternalAction) -> Effect<Action> {
switch internalAction {
case let .transfer(hasMainnetEverBeenLive):
state.destination = .transfer(.init(
from: state.account,
hasMainnetEverBeenLive: hasMainnetEverBeenLive
))
return .none

case .markBackupNeeded:
state.exportMnemonicPrompt = .init(needed: true)
return .none
Expand All @@ -272,7 +262,7 @@ public struct AccountDetails: Sendable, FeatureReducer {
return loadImport()

case let .loadMnemonicResult(.success(mnemonicWithPassphraseAndFactorSourceInfo)):
loggerGlobal.feature("Successfully loaded mnemonic to export")
loggerGlobal.trace("Successfully loaded mnemonic to export")
state.destination = .exportMnemonic(.init(
warning: L10n.RevealSeedPhrase.warning,
mnemonicWithPassphrase: mnemonicWithPassphraseAndFactorSourceInfo.mnemonicWithPassphrase,
Expand Down Expand Up @@ -306,7 +296,6 @@ public struct AccountDetails: Sendable, FeatureReducer {
}

private func loadMnemonic(state: State) -> Effect<Action> {
loggerGlobal.feature("implement export")
let factorInstance = state.deviceControlledFactorInstance
let factorSourceID = factorInstance.factorSourceID
return .run { send in
Expand Down
Loading

0 comments on commit 3981b6b

Please sign in to comment.