From 1434c6da0436ddeea18c9537b70b84a968a66e65 Mon Sep 17 00:00:00 2001 From: Simon McLoughlin Date: Fri, 21 Jun 2024 10:40:24 +0100 Subject: [PATCH] watch wallet cache should fail if the wallet exists anywhere in the list as it will cause confusion to allow it to appear in both lists --- .../Services/WalletCacheService.swift | 4 ++-- .../Services/WalletCacheServiceTests.swift | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Sources/KukaiCoreSwift/Services/WalletCacheService.swift b/Sources/KukaiCoreSwift/Services/WalletCacheService.swift index 65e7f9fd..466407e8 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 7e19da89..c295fa47 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) + } + } }