Skip to content

Commit

Permalink
- prevent caching duplicate watch wallets
Browse files Browse the repository at this point in the history
- change function to throw error so we can be more clear to user on what the issue was
  • Loading branch information
simonmcl committed Feb 22, 2024
1 parent d4115b2 commit 4d52997
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 10 additions & 2 deletions Sources/KukaiCoreSwift/Services/WalletCacheService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

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

0 comments on commit 4d52997

Please sign in to comment.