Skip to content

Commit

Permalink
importing a wallet already being watched, should remove the watched w…
Browse files Browse the repository at this point in the history
…allet, as it will cause confusion in the code
  • Loading branch information
simonmcl committed Jun 21, 2024
1 parent 1434c6d commit 922e83b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Sources/KukaiCoreSwift/Services/WalletCacheService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,17 @@ public class WalletCacheService {

if encryptAndWriteWalletsToDisk(wallets: newWallets) && encryptAndWriteMetadataToDisk(newMetadata) == false {
throw WalletCacheError.unableToEncryptAndWrite
} else {
removeNewAddressFromWatchListIfExists(wallet.address, list: newMetadata)
}
}

private func removeNewAddressFromWatchListIfExists(_ address: String, list: WalletMetadataList) {
if let _ = list.watchWallets.first(where: { $0.address == address }) {
let _ = deleteWatchWallet(address: address)
}
}

/**
Cahce a watch wallet metadata obj, only. Metadata cahcing handled via wallet cache method
*/
Expand Down
29 changes: 28 additions & 1 deletion Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class WalletCacheServiceTests: XCTestCase {

do {
try walletCacheService.cacheWatchWallet(metadata: watchWallet)
let list = walletCacheService.readMetadataFromDiskAndDecrypt()
let list = walletCacheService.readMetadataFromDiskAndDecrypt()
let watch = list.watchWallets
XCTAssert(watch.count == 1, watch.count.description)
} catch {
Expand All @@ -364,4 +364,31 @@ class WalletCacheServiceTests: XCTestCase {
XCTAssert(error.localizedDescription == "The operation couldn’t be completed. (KukaiCoreSwift.WalletCacheError error 8.)", error.localizedDescription)
}
}

func testWatchWalletRemovedAfterImported() {
XCTAssert(walletCacheService.deleteAllCacheAndKeys())

// Add watch and confirm
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)
let list = walletCacheService.readMetadataFromDiskAndDecrypt()
let watch = list.watchWallets
XCTAssert(watch.count == 1, watch.count.description)
} catch let error {
XCTFail("Should not error: \(error)")
}


// Add the wallet via an import, test watch is now been removed
do {
try walletCacheService.cache(wallet: MockConstants.defaultLinearWallet, childOfIndex: nil, backedUp: false)
let list = walletCacheService.readMetadataFromDiskAndDecrypt()
let watch = list.watchWallets
XCTAssert(watch.count == 0, watch.count.description)
} catch {
XCTFail("Should not error: \(error)")
}
}
}

0 comments on commit 922e83b

Please sign in to comment.