From f770faee2bd4115bb7871bd87641587ac3434f70 Mon Sep 17 00:00:00 2001 From: Simon Mcloughlin Date: Mon, 21 Oct 2024 15:27:18 +0100 Subject: [PATCH] - add new walletCache function to migrate ledgers to new UUID - add new test --- .../Services/WalletCacheService.swift | 23 ++++++++++++ .../Services/WalletCacheServiceTests.swift | 35 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/Sources/KukaiCoreSwift/Services/WalletCacheService.swift b/Sources/KukaiCoreSwift/Services/WalletCacheService.swift index 6492b60..77f871c 100644 --- a/Sources/KukaiCoreSwift/Services/WalletCacheService.swift +++ b/Sources/KukaiCoreSwift/Services/WalletCacheService.swift @@ -292,6 +292,29 @@ public class WalletCacheService { return cacheItems[address] } + /** + Migrate a LedgerWallet and its children to a new physical device, denoted by a new UUID + */ + public func migrateLedger(metadata: WalletMetadata, toNewUUID: String) -> Bool { + guard let cacheItems = readWalletsFromDiskAndDecrypt() else { + Logger.walletCache.error("Unable to read wallet items") + return false + } + + var addressesToMigrate = [metadata.address] + addressesToMigrate.append(contentsOf: metadata.children.map({ $0.address })) + + for address in addressesToMigrate { + guard let ledgerWallet = cacheItems[address] as? LedgerWallet else { + return false + } + + ledgerWallet.ledgerUUID = toNewUUID + } + + return encryptAndWriteWalletsToDisk(wallets: cacheItems) + } + /** Delete the cached files and the assoicate keys used to encrypt it - Returns: Bool, indicating if the process was successful or not diff --git a/Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift b/Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift index 712d8d4..3dcb55a 100644 --- a/Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift +++ b/Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift @@ -427,4 +427,39 @@ class WalletCacheServiceTests: XCTestCase { XCTFail("Should not error: \(error)") } } + + func testLedgerWalletMigrate() { + XCTAssert(walletCacheService.deleteAllCacheAndKeys()) + + let ledgerWallet = LedgerWallet(address: "tz1abc", publicKey: "edpks1234", derivationPath: HD.defaultDerivationPath, curve: .ed25519, ledgerUUID: "blah1")! + let ledgerWalletChild1 = LedgerWallet(address: "tz1def", publicKey: "edpks1234", derivationPath: HD.defaultDerivationPath, curve: .ed25519, ledgerUUID: "blah1")! + let ledgerWalletChild2 = LedgerWallet(address: "tz1ghi", publicKey: "edpks1234", derivationPath: HD.defaultDerivationPath, curve: .ed25519, ledgerUUID: "blah1")! + + do { + try walletCacheService.cache(wallet: ledgerWallet, childOfIndex: nil, backedUp: true) + try walletCacheService.cache(wallet: ledgerWalletChild1, childOfIndex: 0, backedUp: true) + try walletCacheService.cache(wallet: ledgerWalletChild2, childOfIndex: 0, backedUp: true) + + let list = walletCacheService.readMetadataFromDiskAndDecrypt() + guard let ledger = list.ledgerWallets.first else { + XCTFail("Ledger list empty") + return + } + + XCTAssert(walletCacheService.migrateLedger(metadata: ledger, toNewUUID: "migrated")) + + let ledgerParent = walletCacheService.fetchWallet(forAddress: ledgerWallet.address) as? LedgerWallet + XCTAssert(ledgerParent?.ledgerUUID == "migrated") + + let ledgerChild1 = walletCacheService.fetchWallet(forAddress: ledgerWalletChild1.address) as? LedgerWallet + XCTAssert(ledgerChild1?.ledgerUUID == "migrated") + + let ledgerChild2 = walletCacheService.fetchWallet(forAddress: ledgerWalletChild2.address) as? LedgerWallet + XCTAssert(ledgerChild2?.ledgerUUID == "migrated") + + + } catch let error { + XCTFail("Should not error: \(error)") + } + } }