diff --git a/Sources/KukaiCoreSwift/Services/WalletCacheService.swift b/Sources/KukaiCoreSwift/Services/WalletCacheService.swift index 65e7f9f..466407e 100644 --- a/Sources/KukaiCoreSwift/Services/WalletCacheService.swift +++ b/Sources/KukaiCoreSwift/Services/WalletCacheService.swift @@ -143,8 +143,8 @@ public class WalletCacheService { public func cacheWatchWallet(metadata: WalletMetadata) throws { var list = readMetadataFromDiskAndDecrypt() - if let _ = list.watchWallets.first(where: { $0.address == metadata.address }) { - Logger.walletCache.error("cacheWatchWallet - Unable to cache wallet, walelt already exists") + if let _ = list.addresses().first(where: { $0 == metadata.address }) { + Logger.walletCache.error("cacheWatchWallet - Unable to cache wallet, wallet already exists") throw WalletCacheError.walletAlreadyExists } diff --git a/Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift b/Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift index 7e19da8..c295fa4 100644 --- a/Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift +++ b/Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift @@ -345,4 +345,23 @@ class WalletCacheServiceTests: XCTestCase { XCTFail("Should not error: \(error)") } } + + func testWatchWalletThatAlreadyExists() { + XCTAssert(walletCacheService.deleteAllCacheAndKeys()) + + do { + try walletCacheService.cache(wallet: MockConstants.defaultLinearWallet, childOfIndex: nil, backedUp: false) + } catch { + XCTFail("Should not error: \(error)") + } + + let watchWallet = WalletMetadata(address: MockConstants.defaultLinearWallet.address, hdWalletGroupName: nil, mainnetDomains: [], ghostnetDomains: [], type: .hd, children: [], isChild: false, isWatchOnly: true, bas58EncodedPublicKey: "", backedUp: true) + + do { + try walletCacheService.cacheWatchWallet(metadata: watchWallet) + XCTFail("Already exists, should be rejected") + } catch let error { + XCTAssert(error.localizedDescription == "The operation couldn’t be completed. (KukaiCoreSwift.WalletCacheError error 8.)", error.localizedDescription) + } + } }