Add userDefaults depedency injection and cover watch address functionality

pull/2/head
Michael Scoff 7 years ago
parent e08e07582b
commit 5e2a776059
  1. 9
      Trust/EtherClient/EtherKeystore.swift
  2. 31
      TrustTests/EtherClient/EtherKeystoreTests.swift
  3. 3
      TrustTests/Factories/FakeEtherKeystore.swift

@ -22,10 +22,12 @@ open class EtherKeystore: Keystore {
let keyStore: KeyStore
private let defaultKeychainAccess: KeychainSwiftAccessOptions = .accessibleWhenUnlockedThisDeviceOnly
let keystoreDirectory: URL
let userDefaults: UserDefaults
public init(
keychain: KeychainSwift = KeychainSwift(keyPrefix: Constants.keychainKeyPrefix),
keyStoreSubfolder: String = "/keystore"
keyStoreSubfolder: String = "/keystore",
userDefaults: UserDefaults = UserDefaults.standard
) throws {
if !UIApplication.shared.isProtectedDataAvailable {
throw EtherKeystoreError.protectionDisabled
@ -34,6 +36,7 @@ open class EtherKeystore: Keystore {
self.keychain = keychain
self.keychain.synchronizable = false
self.keyStore = try KeyStore(keydir: keystoreDirectory)
self.userDefaults = userDefaults
}
var hasWallets: Bool {
@ -43,10 +46,10 @@ open class EtherKeystore: Keystore {
private var watchAddresses: [String] {
set {
let data = NSKeyedArchiver.archivedData(withRootObject: newValue)
return UserDefaults.standard.set(data, forKey: Keys.watchAddresses)
return userDefaults.set(data, forKey: Keys.watchAddresses)
}
get {
guard let data = UserDefaults.standard.data(forKey: Keys.watchAddresses) else { return [] }
guard let data = userDefaults.data(forKey: Keys.watchAddresses) else { return [] }
return NSKeyedUnarchiver.unarchiveObject(with: data) as? [String] ?? []
}
}

@ -242,4 +242,35 @@ class EtherKeystoreTests: XCTestCase {
// s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029',
// signature: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c'
}
func testAddWatchAddress() {
let keystore = FakeEtherKeystore()
let address: Address = .make()
keystore.importWallet(type: ImportType.watch(address: address)) {_ in }
XCTAssertEqual(1, keystore.wallets.count)
XCTAssertEqual(address, keystore.wallets[0].address)
}
func testDeleteWatchAddress() {
let keystore = FakeEtherKeystore()
let address: Address = .make()
// TODO. Move this into sync calls
keystore.importWallet(type: ImportType.watch(address: address)) { result in
switch result {
case .success(let wallet):
XCTAssertEqual(1, keystore.wallets.count)
XCTAssertEqual(address, keystore.wallets[0].address)
let _ = keystore.delete(wallet: wallet)
XCTAssertEqual(0, keystore.wallets.count)
case .failure:
XCTFail()
}
}
XCTAssertEqual(0, keystore.wallets.count)
}
}

@ -11,7 +11,8 @@ class FakeEtherKeystore: EtherKeystore {
let uniqueString = NSUUID().uuidString
try! self.init(
keychain: KeychainSwift(keyPrefix: "fake" + uniqueString),
keyStoreSubfolder: "/" + uniqueString
keyStoreSubfolder: "/" + uniqueString,
userDefaults: UserDefaults.test
)
}

Loading…
Cancel
Save