diff --git a/Sources/KukaiCoreSwift/Services/WalletCacheService.swift b/Sources/KukaiCoreSwift/Services/WalletCacheService.swift index 7ae4f0e2..07c6cfae 100644 --- a/Sources/KukaiCoreSwift/Services/WalletCacheService.swift +++ b/Sources/KukaiCoreSwift/Services/WalletCacheService.swift @@ -140,11 +140,19 @@ public class WalletCacheService { /** Cahce a watch wallet metadata obj, only. Metadata cahcing handled via wallet cache method */ - public func cacheWatchWallet(metadata: WalletMetadata) -> Bool { + 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") + throw WalletCacheError.walletAlreadyExists + } + list.watchWallets.append(metadata) - return encryptAndWriteMetadataToDisk(list) + if encryptAndWriteMetadataToDisk(list) == false { + throw WalletCacheError.unableToEncryptAndWrite + } } /** diff --git a/Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift b/Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift index 3fb821dc..be43bd6c 100644 --- a/Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift +++ b/Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift @@ -332,10 +332,14 @@ class WalletCacheServiceTests: XCTestCase { XCTAssert(walletCacheService.deleteAllCacheAndKeys()) let watchWallet = WalletMetadata(address: "tz1jkl", hdWalletGroupName: nil, mainnetDomains: [], ghostnetDomains: [], type: .hd, children: [], isChild: false, isWatchOnly: true, bas58EncodedPublicKey: "", backedUp: true) - XCTAssert(walletCacheService.cacheWatchWallet(metadata: watchWallet)) - let list = walletCacheService.readMetadataFromDiskAndDecrypt() - let watch = list.watchWallets - XCTAssert(watch.count == 1, watch.count.description) + do { + try walletCacheService.cacheWatchWallet(metadata: watchWallet) + let list = walletCacheService.readMetadataFromDiskAndDecrypt() + let watch = list.watchWallets + XCTAssert(watch.count == 1, watch.count.description) + } catch { + XCTFail("Should not error: \(error)") + } } }