Skip to content

Commit

Permalink
- add new walletCache function to migrate ledgers to new UUID
Browse files Browse the repository at this point in the history
- add new test
  • Loading branch information
simonmcl committed Oct 21, 2024
1 parent 5c30006 commit f770fae
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Sources/KukaiCoreSwift/Services/WalletCacheService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions Tests/KukaiCoreSwiftTests/Services/WalletCacheServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}
}

0 comments on commit f770fae

Please sign in to comment.