Skip to content

Commit

Permalink
watch wallet cache should fail if the wallet exists anywhere in the l…
Browse files Browse the repository at this point in the history
…ist as it will cause confusion to allow it to appear in both lists
  • Loading branch information
simonmcl committed Jun 21, 2024
1 parent 1e3c41c commit 1434c6d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/KukaiCoreSwift/Services/WalletCacheService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 1434c6d

Please sign in to comment.