diff --git a/secant.xcodeproj/project.pbxproj b/secant.xcodeproj/project.pbxproj index 0b6951b..763ff33 100644 --- a/secant.xcodeproj/project.pbxproj +++ b/secant.xcodeproj/project.pbxproj @@ -145,6 +145,8 @@ 9E6713FA289BE0E100A6796F /* ClearBackgroundView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E6713F9289BE0E100A6796F /* ClearBackgroundView.swift */; }; 9E69A24D27FB002800A55317 /* WelcomeStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E69A24C27FB002800A55317 /* WelcomeStore.swift */; }; 9E6EF2CB291287BB00CA007B /* FeedbackGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E6EF2CA291287BB00CA007B /* FeedbackGenerator.swift */; }; + 9E6EF2D12913B75400CA007B /* MnemonicKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E6EF2D02913B75400CA007B /* MnemonicKey.swift */; }; + 9E6EF2D32913B79A00CA007B /* WalletStorageKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E6EF2D22913B79A00CA007B /* WalletStorageKey.swift */; }; 9E7225F12889539300DF7F17 /* SettingsSnapshotTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E7225F02889539300DF7F17 /* SettingsSnapshotTests.swift */; }; 9E7225F3288AB6DD00DF7F17 /* MultipleLineTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E7225F2288AB6DD00DF7F17 /* MultipleLineTextField.swift */; }; 9E7225F6288AC71A00DF7F17 /* MultiLineTextFieldStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E7225F5288AC71A00DF7F17 /* MultiLineTextFieldStore.swift */; }; @@ -391,6 +393,8 @@ 9E6713F9289BE0E100A6796F /* ClearBackgroundView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClearBackgroundView.swift; sourceTree = ""; }; 9E69A24C27FB002800A55317 /* WelcomeStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeStore.swift; sourceTree = ""; }; 9E6EF2CA291287BB00CA007B /* FeedbackGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbackGenerator.swift; sourceTree = ""; }; + 9E6EF2D02913B75400CA007B /* MnemonicKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MnemonicKey.swift; sourceTree = ""; }; + 9E6EF2D22913B79A00CA007B /* WalletStorageKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletStorageKey.swift; sourceTree = ""; }; 9E7225F02889539300DF7F17 /* SettingsSnapshotTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsSnapshotTests.swift; sourceTree = ""; }; 9E7225F2288AB6DD00DF7F17 /* MultipleLineTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultipleLineTextField.swift; sourceTree = ""; }; 9E7225F5288AC71A00DF7F17 /* MultiLineTextFieldStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultiLineTextFieldStore.swift; sourceTree = ""; }; @@ -1131,6 +1135,8 @@ 9EF1082A29114B93003D8097 /* Pasteboard.swift */, 9EF1082C29114BCD003D8097 /* NewRecoveryPhrase.swift */, 9E6EF2CA291287BB00CA007B /* FeedbackGenerator.swift */, + 9E6EF2D02913B75400CA007B /* MnemonicKey.swift */, + 9E6EF2D22913B79A00CA007B /* WalletStorageKey.swift */, ); path = Dependencies; sourceTree = ""; @@ -1664,6 +1670,7 @@ 9E02B56A27FED43E005B809B /* WrappedFileManager.swift in Sources */, 663FABA2271D876C00E495F8 /* SecondaryButton.swift in Sources */, 9E7CB6202874143800A02233 /* AddressDetailsStore.swift in Sources */, + 9E6EF2D12913B75400CA007B /* MnemonicKey.swift in Sources */, 0DC487C32772574C00BE6A63 /* RecoveryPhraseBackupSucceededView.swift in Sources */, 2EB1C5E827D77F6100BC43D7 /* TCATextFieldStore.swift in Sources */, 9E5BF648282277BE00BA3F17 /* WrappedNotificationCenter.swift in Sources */, @@ -1791,6 +1798,7 @@ 9EF8139C27F47AED0075AF48 /* InitializationState.swift in Sources */, 0D0781C9278776D20083ACD7 /* ZcashSymbol.swift in Sources */, 2E8719CB27FB09990082C926 /* TransactionAmountTextField.swift in Sources */, + 9E6EF2D32913B79A00CA007B /* WalletStorageKey.swift in Sources */, 9E7CB6212874143800A02233 /* AddressDetailsView.swift in Sources */, 9E6713FA289BE0E100A6796F /* ClearBackgroundView.swift in Sources */, 34E5F2F328E46DB700C17E5F /* DiskSpaceChecker.swift in Sources */, diff --git a/secant/Dependencies/MnemonicKey.swift b/secant/Dependencies/MnemonicKey.swift new file mode 100644 index 0000000..1479282 --- /dev/null +++ b/secant/Dependencies/MnemonicKey.swift @@ -0,0 +1,20 @@ +// +// MnemonicKey.swift +// secant-testnet +// +// Created by Lukáš Korba on 03.11.2022. +// + +import ComposableArchitecture + +private enum MnemonicKey: DependencyKey { + static let liveValue = WrappedMnemonic.live + static let testValue = WrappedMnemonic.mock +} + +extension DependencyValues { + var mnemonic: WrappedMnemonic { + get { self[MnemonicKey.self] } + set { self[MnemonicKey.self] = newValue } + } +} diff --git a/secant/Dependencies/RecoveryPhraseRandomizer.swift b/secant/Dependencies/RecoveryPhraseRandomizer.swift index 741664c..bef778d 100644 --- a/secant/Dependencies/RecoveryPhraseRandomizer.swift +++ b/secant/Dependencies/RecoveryPhraseRandomizer.swift @@ -6,6 +6,7 @@ // import Foundation +import ComposableArchitecture struct RecoveryPhraseRandomizer { func random(phrase: RecoveryPhrase) -> RecoveryPhraseValidationFlow.State { @@ -27,8 +28,6 @@ struct RecoveryPhraseRandomizer { } } -import ComposableArchitecture - private enum RecoveryPhraseRandomKey: DependencyKey { static let liveValue = WrappedRecoveryPhraseRandomizer.live } diff --git a/secant/Dependencies/WalletStorageKey.swift b/secant/Dependencies/WalletStorageKey.swift new file mode 100644 index 0000000..2e372b3 --- /dev/null +++ b/secant/Dependencies/WalletStorageKey.swift @@ -0,0 +1,20 @@ +// +// WalletStorageKey.swift +// secant-testnet +// +// Created by Lukáš Korba on 03.11.2022. +// + +import ComposableArchitecture + +private enum WalletStorageKey: DependencyKey { + static let liveValue = WrappedWalletStorage.live() + static let testValue = WrappedWalletStorage.throwing +} + +extension DependencyValues { + var walletStorage: WrappedWalletStorage { + get { self[WalletStorageKey.self] } + set { self[WalletStorageKey.self] = newValue } + } +} diff --git a/secant/Dependencies/ZCashSDKEnvironment.swift b/secant/Dependencies/ZCashSDKEnvironment.swift index 21a45ac..c093177 100644 --- a/secant/Dependencies/ZCashSDKEnvironment.swift +++ b/secant/Dependencies/ZCashSDKEnvironment.swift @@ -7,6 +7,7 @@ import Foundation import ZcashLightClientKit +import ComposableArchitecture // swiftlint:disable:next private_over_fileprivate strict_fileprivate fileprivate enum ZcashSDKConstants { @@ -80,3 +81,15 @@ extension ZCashSDKEnvironment { sdkVersion: "0.16.5-beta" ) } + +private enum ZCashSDKEnvironmentKey: DependencyKey { + static let liveValue = ZCashSDKEnvironment.mainnet + static let testValue = ZCashSDKEnvironment.testnet +} + +extension DependencyValues { + var zcashSDKEnvironment: ZCashSDKEnvironment { + get { self[ZCashSDKEnvironmentKey.self] } + set { self[ZCashSDKEnvironmentKey.self] = newValue } + } +} diff --git a/secant/Features/ImportWallet/ImportWalletStore.swift b/secant/Features/ImportWallet/ImportWalletStore.swift index c9af8e5..b88aba0 100644 --- a/secant/Features/ImportWallet/ImportWalletStore.swift +++ b/secant/Features/ImportWallet/ImportWalletStore.swift @@ -8,171 +8,148 @@ import ComposableArchitecture import ZcashLightClientKit -typealias ImportWalletReducer = Reducer -typealias ImportWalletStore = Store -typealias ImportWalletViewStore = ViewStore +typealias ImportWalletStore = Store +typealias ImportWalletViewStore = ViewStore -// MARK: - State - -struct ImportWalletState: Equatable { - @BindableState var alert: AlertState? - @BindableState var importedSeedPhrase: String = "" - @BindableState var birthdayHeight: String = "" - var wordsCount = 0 - var maxWordsCount = 0 - var isValidMnemonic = false - var isValidNumberOfWords = false - var birthdayHeightValue: BlockHeight? - - var mnemonicStatus: String { - if isValidMnemonic { - return "VALID SEED PHRASE" - } else { - return "\(wordsCount)/\(maxWordsCount)" +struct ImportWallet: ReducerProtocol { + struct State: Equatable { + @BindableState var alert: AlertState? + @BindableState var importedSeedPhrase: String = "" + @BindableState var birthdayHeight: String = "" + var wordsCount = 0 + var maxWordsCount = 0 + var isValidMnemonic = false + var isValidNumberOfWords = false + var birthdayHeightValue: BlockHeight? + + var mnemonicStatus: String { + if isValidMnemonic { + return "VALID SEED PHRASE" + } else { + return "\(wordsCount)/\(maxWordsCount)" + } + } + + var isValidForm: Bool { + isValidMnemonic && + (birthdayHeight.isEmpty || + (!birthdayHeight.isEmpty && birthdayHeightValue != nil)) } } - - var isValidForm: Bool { - isValidMnemonic && - (birthdayHeight.isEmpty || - (!birthdayHeight.isEmpty && birthdayHeightValue != nil)) + + @Dependency(\.zcashSDKEnvironment) var zcashSDKEnvironment + @Dependency(\.mnemonic) var mnemonic + @Dependency(\.walletStorage) var walletStorage + + enum Action: Equatable, BindableAction { + case binding(BindingAction) + case dismissAlert + case restoreWallet + case importPrivateOrViewingKey + case initializeSDK + case onAppear + case successfullyRecovered } -} - -// MARK: - Action - -enum ImportWalletAction: Equatable, BindableAction { - case binding(BindingAction) - case dismissAlert - case restoreWallet - case importPrivateOrViewingKey - case initializeSDK - case onAppear - case successfullyRecovered -} - -// MARK: - Environment - -struct ImportWalletEnvironment { - let mnemonic: WrappedMnemonic - let walletStorage: WrappedWalletStorage - let zcashSDKEnvironment: ZCashSDKEnvironment -} - -extension ImportWalletEnvironment { - static let live = ImportWalletEnvironment( - mnemonic: .live, - walletStorage: .live(), - zcashSDKEnvironment: .mainnet - ) - - static let demo = ImportWalletEnvironment( - mnemonic: .mock, - walletStorage: .live(), - zcashSDKEnvironment: .testnet - ) -} - -// MARK: - Reducer - -extension ImportWalletReducer { - static let `default` = ImportWalletReducer { state, action, environment in - switch action { - case .onAppear: - state.maxWordsCount = environment.zcashSDKEnvironment.mnemonicWordsMaxCount - return .none - - case .binding(\.$importedSeedPhrase): - state.wordsCount = state.importedSeedPhrase.split(separator: " ").count - state.isValidNumberOfWords = state.wordsCount == state.maxWordsCount - // is the mnemonic valid one? - do { - try environment.mnemonic.isValid(state.importedSeedPhrase) - } catch { - state.isValidMnemonic = false + + var body: some ReducerProtocol { + BindingReducer() + + Reduce { state, action in + switch action { + case .onAppear: + state.maxWordsCount = zcashSDKEnvironment.mnemonicWordsMaxCount + return .none + + case .binding(\.$importedSeedPhrase): + state.wordsCount = state.importedSeedPhrase.split(separator: " ").count + state.isValidNumberOfWords = state.wordsCount == state.maxWordsCount + // is the mnemonic valid one? + do { + try mnemonic.isValid(state.importedSeedPhrase) + } catch { + state.isValidMnemonic = false + return .none + } + state.isValidMnemonic = true + return .none + + case .binding(\.$birthdayHeight): + if let birthdayHeight = BlockHeight(state.birthdayHeight), birthdayHeight >= zcashSDKEnvironment.defaultBirthday { + state.birthdayHeightValue = birthdayHeight + } else { + state.birthdayHeightValue = nil + } + return .none + + case .binding: + return .none + + case .dismissAlert: + state.alert = nil + return .none + + case .restoreWallet: + do { + // validate the seed + try mnemonic.isValid(state.importedSeedPhrase) + + // store it to the keychain + let birthday = state.birthdayHeightValue ?? zcashSDKEnvironment.defaultBirthday + try walletStorage.importWallet(state.importedSeedPhrase, birthday, .english, false) + + // update the backup phrase validation flag + try walletStorage.markUserPassedPhraseBackupTest() + + // notify user + // TODO [#221]: Proper Error/Success handling (https://github.com/zcash/secant-ios-wallet/issues/221) + state.alert = AlertState( + title: TextState("Success"), + message: TextState("The wallet has been successfully recovered."), + dismissButton: .default( + TextState("Ok"), + action: .send(.successfullyRecovered) + ) + ) + + return Effect(value: .initializeSDK) + } catch { + // TODO [#221]: Proper Error/Success handling (https://github.com/zcash/secant-ios-wallet/issues/221) + state.alert = AlertState( + title: TextState("Wrong Seed Phrase"), + message: TextState("The seed phrase must be 24 words separated by space."), + dismissButton: .default( + TextState("Ok"), + action: .send(.dismissAlert) + ) + ) + } + + return .none + + case .importPrivateOrViewingKey: + return .none + + case .successfullyRecovered: + return Effect(value: .dismissAlert) + + case .initializeSDK: return .none } - state.isValidMnemonic = true - return .none - - case .binding(\.$birthdayHeight): - if let birthdayHeight = BlockHeight(state.birthdayHeight), birthdayHeight >= environment.zcashSDKEnvironment.defaultBirthday { - state.birthdayHeightValue = birthdayHeight - } else { - state.birthdayHeightValue = nil - } - return .none - - case .binding: - return .none - - case .dismissAlert: - state.alert = nil - return .none - - case .restoreWallet: - do { - // validate the seed - try environment.mnemonic.isValid(state.importedSeedPhrase) - - // store it to the keychain - var birthday = state.birthdayHeightValue ?? environment.zcashSDKEnvironment.defaultBirthday - try environment.walletStorage.importWallet(state.importedSeedPhrase, birthday, .english, false) - - // update the backup phrase validation flag - try environment.walletStorage.markUserPassedPhraseBackupTest() - - // notify user - // TODO [#221]: Proper Error/Success handling (https://github.com/zcash/secant-ios-wallet/issues/221) - state.alert = AlertState( - title: TextState("Success"), - message: TextState("The wallet has been successfully recovered."), - dismissButton: .default( - TextState("Ok"), - action: .send(.successfullyRecovered) - ) - ) - - return Effect(value: .initializeSDK) - } catch { - // TODO [#221]: Proper Error/Success handling (https://github.com/zcash/secant-ios-wallet/issues/221) - state.alert = AlertState( - title: TextState("Wrong Seed Phrase"), - message: TextState("The seed phrase must be 24 words separated by space."), - dismissButton: .default( - TextState("Ok"), - action: .send(.dismissAlert) - ) - ) - } - - return .none - - case .importPrivateOrViewingKey: - return .none - - case .successfullyRecovered: - return Effect(value: .dismissAlert) - - case .initializeSDK: - return .none } } - .binding() } // MARK: - Placeholders -extension ImportWalletState { - static let placeholder = ImportWalletState() +extension ImportWallet.State { + static let placeholder = ImportWallet.State() - static let live = ImportWalletState() + static let live = ImportWallet.State() } extension ImportWalletStore { static let demo = Store( initialState: .placeholder, - reducer: .default, - environment: .demo + reducer: ImportWallet() ) } diff --git a/secant/Features/OnboardingFlow/OnboardingFlowStore.swift b/secant/Features/OnboardingFlow/OnboardingFlowStore.swift index 8ca5927..3b64502 100644 --- a/secant/Features/OnboardingFlow/OnboardingFlowStore.swift +++ b/secant/Features/OnboardingFlow/OnboardingFlowStore.swift @@ -13,6 +13,8 @@ typealias OnboardingFlowReducer = Reducer typealias OnboardingFlowViewStore = ViewStore +typealias AnyImportWalletReducer = AnyReducer + // MARK: - State struct OnboardingFlowState: Equatable { @@ -46,7 +48,7 @@ struct OnboardingFlowState: Equatable { } /// Import Wallet - var importWalletState: ImportWalletState + var importWalletState: ImportWallet.State } extension OnboardingFlowState { @@ -93,7 +95,7 @@ enum OnboardingFlowAction: Equatable { case updateRoute(OnboardingFlowState.Route?) case createNewWallet case importExistingWallet - case importWallet(ImportWalletAction) + case importWallet(ImportWallet.Action) } // MARK: - Environment @@ -168,16 +170,13 @@ extension OnboardingFlowReducer { } } - private static let importWalletReducer: OnboardingFlowReducer = ImportWalletReducer.default.pullback( + private static let importWalletReducer: OnboardingFlowReducer = AnyImportWalletReducer { _ in + ImportWallet() + } + .pullback( state: \OnboardingFlowState.importWalletState, action: /OnboardingFlowAction.importWallet, - environment: { environment in - ImportWalletEnvironment( - mnemonic: environment.mnemonic, - walletStorage: environment.walletStorage, - zcashSDKEnvironment: environment.zcashSDKEnvironment - ) - } + environment: { $0 } ) } diff --git a/secantTests/ImportWalletTests/ImportWalletTests.swift b/secantTests/ImportWalletTests/ImportWalletTests.swift index adc242b..a6fb742 100644 --- a/secantTests/ImportWalletTests/ImportWalletTests.swift +++ b/secantTests/ImportWalletTests/ImportWalletTests.swift @@ -12,16 +12,9 @@ import ComposableArchitecture // swiftlint:disable type_body_length class ImportWalletTests: XCTestCase { func testOnAppear() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .mock, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet - ) - let store = TestStore( initialState: .placeholder, - reducer: ImportWalletReducer.default, - environment: testEnvironment + reducer: ImportWallet() ) store.send(.onAppear) { state in @@ -30,16 +23,10 @@ class ImportWalletTests: XCTestCase { } func testWordsCount() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .live, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet - ) - let store = TestStore( initialState: .placeholder, - reducer: ImportWalletReducer.default, - environment: testEnvironment + reducer: ImportWallet() + .dependency(\.mnemonic, .live) ) store.send(.binding(.set(\.$importedSeedPhrase, "one two three"))) { state in @@ -50,18 +37,12 @@ class ImportWalletTests: XCTestCase { } func testMaxWordsInvalidMnemonic() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .live, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet + let store = TestStore( + initialState: ImportWallet.State(maxWordsCount: 24), + reducer: ImportWallet() + .dependency(\.mnemonic, .live) ) - let store = TestStore( - initialState: ImportWalletState(maxWordsCount: 24), - reducer: ImportWalletReducer.default, - environment: testEnvironment - ) - store.send(.binding(.set(\.$importedSeedPhrase, "a a a a a a a a a a a a a a a a a a a a a a a a"))) { state in state.importedSeedPhrase = "a a a a a a a a a a a a a a a a a a a a a a a a" state.wordsCount = 24 @@ -71,18 +52,12 @@ class ImportWalletTests: XCTestCase { } func testValidMnemonic() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .live, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet + let store = TestStore( + initialState: ImportWallet.State(maxWordsCount: 24), + reducer: ImportWallet() + .dependency(\.mnemonic, .live) ) - let store = TestStore( - initialState: ImportWalletState(maxWordsCount: 24), - reducer: ImportWalletReducer.default, - environment: testEnvironment - ) - store.send( .binding( .set( @@ -109,16 +84,9 @@ class ImportWalletTests: XCTestCase { } func testInvalidBirthdayHeight_lessThanDefault() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .mock, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet - ) - let store = TestStore( initialState: .placeholder, - reducer: ImportWalletReducer.default, - environment: testEnvironment + reducer: ImportWallet() ) store.send(.binding(.set(\.$birthdayHeight, "1600000"))) { state in @@ -127,16 +95,9 @@ class ImportWalletTests: XCTestCase { } func testInvalidBirthdayHeight_invalidInput() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .mock, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet - ) - let store = TestStore( initialState: .placeholder, - reducer: ImportWalletReducer.default, - environment: testEnvironment + reducer: ImportWallet() ) store.send(.binding(.set(\.$birthdayHeight, "abc"))) { state in @@ -145,16 +106,9 @@ class ImportWalletTests: XCTestCase { } func testInvalidBirthdayHeight_validInput() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .mock, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet - ) - let store = TestStore( initialState: .placeholder, - reducer: ImportWalletReducer.default, - environment: testEnvironment + reducer: ImportWallet() ) store.send(.binding(.set(\.$birthdayHeight, "1700000"))) { state in @@ -164,15 +118,9 @@ class ImportWalletTests: XCTestCase { } func testDismissAlert() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .mock, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet - ) - let store = TestStore( initialState: - ImportWalletState( + ImportWallet.State( alert: AlertState( title: TextState("Success"), message: TextState("The wallet has been successfully recovered."), @@ -182,8 +130,7 @@ class ImportWalletTests: XCTestCase { ) ) ), - reducer: ImportWalletReducer.default, - environment: testEnvironment + reducer: ImportWallet() ) store.send(.dismissAlert) { state in @@ -192,16 +139,10 @@ class ImportWalletTests: XCTestCase { } func testFormValidity_validBirthday_invalidMnemonic() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .live, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet - ) - let store = TestStore( - initialState: ImportWalletState(maxWordsCount: 24), - reducer: ImportWalletReducer.default, - environment: testEnvironment + initialState: ImportWallet.State(maxWordsCount: 24), + reducer: ImportWallet() + .dependency(\.mnemonic, .live) ) store.send(.binding(.set(\.$birthdayHeight, "1700000"))) { state in @@ -222,18 +163,12 @@ class ImportWalletTests: XCTestCase { } func testFormValidity_invalidBirthday_invalidMnemonic() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .live, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet + let store = TestStore( + initialState: ImportWallet.State(maxWordsCount: 24), + reducer: ImportWallet() + .dependency(\.mnemonic, .live) ) - let store = TestStore( - initialState: ImportWalletState(maxWordsCount: 24), - reducer: ImportWalletReducer.default, - environment: testEnvironment - ) - store.send(.binding(.set(\.$birthdayHeight, "1600000"))) { state in state.birthdayHeight = "1600000" } @@ -251,18 +186,12 @@ class ImportWalletTests: XCTestCase { } func testFormValidity_invalidBirthday_validMnemonic() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .live, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet + let store = TestStore( + initialState: ImportWallet.State(maxWordsCount: 24), + reducer: ImportWallet() + .dependency(\.mnemonic, .live) ) - let store = TestStore( - initialState: ImportWalletState(maxWordsCount: 24), - reducer: ImportWalletReducer.default, - environment: testEnvironment - ) - store.send(.binding(.set(\.$birthdayHeight, "1600000"))) { state in state.birthdayHeight = "1600000" } @@ -297,18 +226,12 @@ class ImportWalletTests: XCTestCase { } func testFormValidity_validBirthday_validMnemonic() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .live, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet + let store = TestStore( + initialState: ImportWallet.State(maxWordsCount: 24), + reducer: ImportWallet() + .dependency(\.mnemonic, .live) ) - let store = TestStore( - initialState: ImportWalletState(maxWordsCount: 24), - reducer: ImportWalletReducer.default, - environment: testEnvironment - ) - store.send(.binding(.set(\.$birthdayHeight, "1700000"))) { state in state.birthdayHeight = "1700000" state.birthdayHeightValue = 1_700_000 @@ -344,18 +267,12 @@ class ImportWalletTests: XCTestCase { } func testFormValidity_noBirthday_validMnemonic() throws { - let testEnvironment = ImportWalletEnvironment( - mnemonic: .live, - walletStorage: .throwing, - zcashSDKEnvironment: .testnet + let store = TestStore( + initialState: ImportWallet.State(maxWordsCount: 24), + reducer: ImportWallet() + .dependency(\.mnemonic, .live) ) - let store = TestStore( - initialState: ImportWalletState(maxWordsCount: 24), - reducer: ImportWalletReducer.default, - environment: testEnvironment - ) - store.send( .binding( .set( @@ -384,20 +301,14 @@ class ImportWalletTests: XCTestCase { ) } } - + func testRestoreWallet() throws { var storage = WalletStorage(secItem: .live) storage.zcashStoredWalletPrefix = "test_importWallet_" storage.deleteData(forKey: WalletStorage.Constants.zcashStoredWallet) - let testEnvironment = ImportWalletEnvironment( - mnemonic: .live, - walletStorage: .live(walletStorage: storage), - zcashSDKEnvironment: .testnet - ) - let store = TestStore( - initialState: ImportWalletState( + initialState: ImportWallet.State( alert: nil, importedSeedPhrase: """ still champion voice habit trend flight \ @@ -412,8 +323,9 @@ class ImportWalletTests: XCTestCase { isValidNumberOfWords: true, birthdayHeightValue: 1_700_000 ), - reducer: ImportWalletReducer.default, - environment: testEnvironment + reducer: ImportWallet() + .dependency(\.mnemonic, .live) + .dependency(\.walletStorage, .live(walletStorage: storage)) ) store.send(.restoreWallet) { state in diff --git a/secantTests/SnapshotTests/ImportWalletSnapshotTests/ImportWalletSnapshotTests.swift b/secantTests/SnapshotTests/ImportWalletSnapshotTests/ImportWalletSnapshotTests.swift index be91f72..83d87cd 100644 --- a/secantTests/SnapshotTests/ImportWalletSnapshotTests/ImportWalletSnapshotTests.swift +++ b/secantTests/SnapshotTests/ImportWalletSnapshotTests/ImportWalletSnapshotTests.swift @@ -13,8 +13,7 @@ class ImportWalletSnapshotTests: XCTestCase { func testImportWalletSnapshot() throws { let store = ImportWalletStore( initialState: .placeholder, - reducer: .default, - environment: .demo + reducer: ImportWallet() ) addAttachments(ImportWalletView(store: store))