@@ -764,6 +764,12 @@ class TrezorRepo @Inject constructor(
764764 }
765765 }
766766
767+ fun deriveWalletId (xpubs : Collection <String >): String {
768+ val xpubList = xpubs.toList()
769+ if (xpubList.isEmpty() || xpubList.any { it.isBlank() }) return " "
770+ return trezorService.deriveWalletId(ActivityWalletType .TREZOR .id, xpubList)
771+ }
772+
767773 suspend fun stopWatcher (watcherId : String ): Result <Unit > = withContext(ioDispatcher) {
768774 runCatching {
769775 awaitSetup()
@@ -888,7 +894,7 @@ class TrezorRepo @Inject constructor(
888894 lastConnectedAt = clock.nowMs(),
889895 xpubs = xpubs,
890896 customLabel = previous?.customLabel,
891- walletId = knownDevices.findHardwareWalletId(deviceInfo.id, xpubs),
897+ walletId = knownDevices.findHardwareWalletId(deviceInfo.id, xpubs, ::deriveWalletId ),
892898 )
893899 val updated = knownDevices.filter { it.id != known.id } + known
894900 saveKnownDevices(updated)
@@ -918,7 +924,7 @@ class TrezorRepo @Inject constructor(
918924
919925 private suspend fun loadKnownDevices (): List <KnownDevice > = runCatching {
920926 val devices = hwWalletStore.loadKnownDevices()
921- val migrated = devices.withHardwareWalletIds()
927+ val migrated = devices.withHardwareWalletIds(::deriveWalletId )
922928 if (migrated != devices) {
923929 hwWalletStore.saveKnownDevices(migrated)
924930 }
@@ -1088,30 +1094,32 @@ private val KnownDevice.walletKey: String
10881094private fun walletKey (xpubs : Map <String , String >, fallback : String ): String =
10891095 xpubs.values.sorted().joinToString().ifEmpty { fallback }
10901096
1091- private fun List<KnownDevice>.findHardwareWalletId (deviceId : String , xpubs : Map <String , String >): String {
1097+ private fun List<KnownDevice>.findHardwareWalletId (
1098+ deviceId : String ,
1099+ xpubs : Map <String , String >,
1100+ deriveWalletId : (Collection <String >) -> String ,
1101+ ): String {
10921102 val walletKey = walletKey(xpubs, deviceId)
10931103 firstOrNull { it.walletKey == walletKey }?.walletId?.takeIf { it.isNotBlank() }?.let { return it }
1094- if (xpubs.values.any { it.isNotBlank() }) return deriveHardwareWalletId (xpubs)
1104+ if (xpubs.values.any { it.isNotBlank() }) return deriveWalletId (xpubs.values )
10951105
10961106 return firstOrNull { it.id == deviceId }?.walletId?.takeIf { it.isNotBlank() }.orEmpty()
10971107}
10981108
1099- private fun List<KnownDevice>.withHardwareWalletIds (): List <KnownDevice > {
1109+ private fun List<KnownDevice>.withHardwareWalletIds (
1110+ deriveWalletId : (Collection <String >) -> String ,
1111+ ): List <KnownDevice > {
11001112 val existingByWallet = filter { it.walletId.isNotBlank() }
11011113 .associate { it.walletKey to it.walletId }
11021114 val generatedByWallet = mutableMapOf<String , String >()
11031115
11041116 return map {
11051117 val walletId = existingByWallet[it.walletKey]
1106- ? : generatedByWallet.getOrPut(it.walletKey) { deriveHardwareWalletId (it.xpubs) }
1118+ ? : generatedByWallet.getOrPut(it.walletKey) { deriveWalletId (it.xpubs.values ) }
11071119 if (it.walletId == walletId) it else it.copy(walletId = walletId)
11081120 }
11091121}
11101122
1111- private fun deriveHardwareWalletId (xpubs : Map <String , String >): String {
1112- return ActivityWalletType .TREZOR .deriveId(xpubs.values)
1113- }
1114-
11151123private const val DEFAULT_GAP_LIMIT = 20u
11161124
11171125private fun KnownDevice.toDeviceInfo () = TrezorDeviceInfo (
0 commit comments