secant-ios-wallet/secant/Stubs/MockServices.swift

175 lines
4.6 KiB
Swift
Raw Normal View History

2021-08-06 16:38:47 -07:00
//
// MockServices.swift
// secant
//
// Created by Francisco Gindre on 8/6/21.
//
2022-02-18 13:39:04 -08:00
// TODO: Move this to different Target when real functionality is developed.
2021-08-06 16:38:47 -07:00
import Foundation
2021-09-14 04:15:29 -07:00
// swiftlint:disable line_length
class MockServices: Services {
var networkProvider: ZcashNetworkProvider = MockNetworkProvider()
var seedHandler: MnemonicSeedPhraseHandling = MockMnemonicPhraseHandling()
var keyStorage: KeyStoring = MockKeyStoring()
2021-08-06 16:38:47 -07:00
}
class MockNetworkProvider: ZcashNetworkProvider {
func currentNetwork() -> ZcashNetwork {
ZcashMainnet()
}
}
class MockMnemonicPhraseHandling: MnemonicSeedPhraseHandling {
class TestSeed {
/**
2021-09-14 04:15:29 -07:00
Test account: "still champion voice habit trend flight survey between bitter process artefact blind carbon truly provide dizzy crush flush breeze blouse charge solid fish spread"
*/
let seedString = Data(
base64Encoded: "9VDVOZZZOWWHpZtq1Ebridp3Qeux5C+HwiRR0g7Oi7HgnMs8Gfln83+/Q1NnvClcaSwM4ADFL1uZHxypEWlWXg=="
)!// swiftlint:disable:this force_unwrapping
2021-08-06 16:38:47 -07:00
func seed() -> [UInt8] {
[UInt8](seedString)
}
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
func randomMnemonic() throws -> String {
"still champion voice habit trend flight survey between bitter process artefact blind carbon truly provide dizzy crush flush breeze blouse charge solid fish spread"
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
func randomMnemonicWords() throws -> [String] {
2021-09-14 04:15:29 -07:00
"still champion voice habit trend flight survey between bitter process artefact blind carbon truly provide dizzy crush flush breeze blouse charge solid fish spread"
.components(separatedBy: " ")
2021-08-06 16:38:47 -07:00
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
func toSeed(mnemonic: String) throws -> [UInt8] {
TestSeed().seed()
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
func asWords(mnemonic: String) throws -> [String] {
2021-09-14 04:15:29 -07:00
"still champion voice habit trend flight survey between bitter process artefact blind carbon truly provide dizzy crush flush breeze blouse charge solid fish spread"
.components(separatedBy: " ")
2021-08-06 16:38:47 -07:00
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
func isValid(mnemonic: String) throws {}
}
class KeysPresentStub: KeyStoring {
init(returnBlock: @escaping () throws -> Bool) {
self.returnBlock = returnBlock
}
var returnBlock: () throws -> Bool
var called = false
func areKeysPresent() throws -> Bool {
called = true
return try returnBlock()
}
var birthday: BlockHeight?
var phrase: String?
func importBirthday(_ height: BlockHeight) throws {
guard birthday == nil else {
throw KeyStoringError.alreadyImported
}
birthday = height
}
func exportBirthday() throws -> BlockHeight {
guard let birthday = birthday else {
throw KeyStoringError.uninitializedWallet
}
return birthday
}
func importPhrase(bip39 phrase: String) throws {
guard self.phrase == nil else {
throw KeyStoringError.alreadyImported
}
self.phrase = phrase
}
func exportPhrase() throws -> String {
guard let phrase = self.phrase else {
throw KeyStoringError.uninitializedWallet
}
return phrase
}
var keysPresent: Bool {
return self.phrase != nil && self.birthday != nil
}
func nukePhrase() {
self.phrase = nil
}
func nukeBirthday() {
self.birthday = nil
}
func nukeWallet() {
nukePhrase()
nukeBirthday()
}
}
2021-08-06 16:38:47 -07:00
class MockKeyStoring: KeyStoring {
2021-09-21 13:38:36 -07:00
var birthday: BlockHeight?
var phrase: String?
var keysPresent: Bool {
return self.phrase != nil && self.birthday != nil
}
func areKeysPresent() throws -> Bool {
false
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
func importBirthday(_ height: BlockHeight) throws {
guard birthday == nil else {
throw KeyStoringError.alreadyImported
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
birthday = height
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
func exportBirthday() throws -> BlockHeight {
2021-09-14 04:15:29 -07:00
guard let birthday = birthday else {
2021-08-06 16:38:47 -07:00
throw KeyStoringError.uninitializedWallet
}
2021-09-14 04:15:29 -07:00
return birthday
2021-08-06 16:38:47 -07:00
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
func importPhrase(bip39 phrase: String) throws {
guard self.phrase == nil else {
throw KeyStoringError.alreadyImported
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
self.phrase = phrase
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
func exportPhrase() throws -> String {
2021-09-14 04:15:29 -07:00
guard let phrase = self.phrase else {
2021-08-06 16:38:47 -07:00
throw KeyStoringError.uninitializedWallet
}
2021-09-14 04:15:29 -07:00
return phrase
2021-08-06 16:38:47 -07:00
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
func nukePhrase() {
self.phrase = nil
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
func nukeBirthday() {
self.birthday = nil
}
2021-09-14 04:15:29 -07:00
2021-08-06 16:38:47 -07:00
func nukeWallet() {
nukePhrase()
nukeBirthday()
}
}