Updated on 2026-08-14

This commit is contained in:
Tangem 2024-11-29 10:59:52 +03:00
parent 334eb75395
commit c67fd1801c
11 changed files with 62 additions and 21 deletions

View file

@ -7,6 +7,7 @@ import com.tangem.common.authentication.storage.AuthenticatedStorage
import com.tangem.common.json.TangemSdkAdapter
import com.tangem.common.services.secure.SecureStorage
import com.tangem.datasource.local.preferences.AppPreferencesStore
import com.tangem.domain.models.scan.serialization.*
import com.tangem.domain.wallets.legacy.UserWalletsListManager
import com.tangem.sdk.storage.AndroidSecureStorage
import com.tangem.sdk.storage.createEncryptedSharedPreferences
@ -19,7 +20,6 @@ import com.tangem.tap.domain.userWalletList.repository.implementation.BiometricU
import com.tangem.tap.domain.userWalletList.repository.implementation.DefaultSelectedUserWalletRepository
import com.tangem.tap.domain.userWalletList.repository.implementation.DefaultUserWalletsPublicInformationRepository
import com.tangem.tap.domain.userWalletList.repository.implementation.DefaultUserWalletsSensitiveInformationRepository
import com.tangem.tap.domain.userWalletList.utils.json.*
import com.tangem.tap.tangemSdkManager
import com.tangem.utils.Provider
import com.tangem.utils.coroutines.CoroutineDispatcherProvider

View file

@ -1 +0,0 @@
package com.tangem.tap.domain.userWalletList.utils

View file

@ -1,22 +0,0 @@
package com.tangem.tap.domain.userWalletList.utils.json
import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.ToJson
import com.tangem.common.extensions.ByteArrayKey
internal class ByteArrayKeyAdapter {
@ToJson
fun toJson(writer: JsonWriter, src: ByteArrayKey, byteArrayAdapter: JsonAdapter<ByteArray>) {
byteArrayAdapter.toJson(writer, src.bytes)
}
@FromJson
fun fromJson(reader: JsonReader, byteArrayAdapter: JsonAdapter<ByteArray>): ByteArrayKey? {
return byteArrayAdapter.fromJson(reader)?.let {
ByteArrayKey(bytes = it)
}
}
}

View file

@ -1,50 +0,0 @@
package com.tangem.tap.domain.userWalletList.utils.json
import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.ToJson
import com.tangem.domain.models.scan.CardDTO
internal class CardBackupStatusAdapter {
@ToJson
fun toJson(writer: JsonWriter, src: CardDTO.BackupStatus?, mapAdapter: JsonAdapter<Map<String, String>>) {
val jsonMap = mutableMapOf<String, String>()
when (src) {
is CardDTO.BackupStatus.Active -> {
jsonMap["status"] = "active"
jsonMap["cardCount"] = src.cardCount.toString()
}
is CardDTO.BackupStatus.CardLinked -> {
jsonMap["status"] = "card_linked"
jsonMap["cardCount"] = src.cardCount.toString()
}
is CardDTO.BackupStatus.NoBackup -> {
jsonMap["status"] = "no_backup"
}
null -> {
jsonMap["status"] = "null"
}
}
mapAdapter.toJson(writer, jsonMap)
}
@FromJson
fun fromJson(reader: JsonReader, mapAdapter: JsonAdapter<Map<String, String>>): CardDTO.BackupStatus? {
val map = mapAdapter.fromJson(reader) ?: return null
return when (map["status"]) {
"active" -> CardDTO.BackupStatus.Active(
cardCount = map["cardCount"]?.toInt() ?: 0,
)
"card_linked" -> CardDTO.BackupStatus.CardLinked(
cardCount = map["cardCount"]?.toInt() ?: 0,
)
"no_backup" -> CardDTO.BackupStatus.NoBackup
else -> null
}
}
}

View file

@ -1,35 +0,0 @@
package com.tangem.tap.domain.userWalletList.utils.json
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import com.tangem.common.extensions.guard
import com.tangem.common.json.MoshiJsonConverter
import com.tangem.crypto.hdWallet.DerivationNode
import com.tangem.crypto.hdWallet.DerivationPath
import timber.log.Timber
class DerivationPathAdapterWithMigration {
@ToJson
fun toJson(src: DerivationPath): String = src.rawPath
@FromJson
fun fromJson(json: String): DerivationPath {
val jsonMap = MoshiJsonConverter.default().toMap(json)
return if (jsonMap.isEmpty()) {
DerivationPath(json)
} else {
fromLegacyScheme(jsonMap)
}
}
@Suppress("UNCHECKED_CAST")
private fun fromLegacyScheme(jsonMap: Map<String, Any>): DerivationPath {
val rawPath = jsonMap["rawPath"] as String
val nodeIndexes = (jsonMap["nodes"] as? List<Number>).guard {
Timber.e("Unable to convert derivation path nodes from JSON")
return DerivationPath(rawPath)
}
val nodes = nodeIndexes.map { DerivationNode.fromIndex(it.toLong()) }
return DerivationPath(rawPath, nodes)
}
}

View file

@ -1,57 +0,0 @@
package com.tangem.tap.domain.userWalletList.utils.json
import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.ToJson
import com.tangem.common.extensions.hexToBytes
import com.tangem.common.extensions.toHexString
import com.tangem.crypto.hdWallet.DerivationPath
import com.tangem.crypto.hdWallet.bip32.ExtendedPublicKey
import com.tangem.operations.derivation.ExtendedPublicKeysMap
internal class ExtendedPublicKeysMapAdapter {
@ToJson
fun toJson(
writer: JsonWriter,
src: ExtendedPublicKeysMap,
mapAdapter: JsonAdapter<Map<String, String>>,
derivationPathAdapter: JsonAdapter<DerivationPath>,
extendedPublicKeyAdapter: JsonAdapter<ExtendedPublicKey>,
) {
val jsonMap = mutableMapOf<String, String>()
src.forEach { (derivationPath, extendedPublicKey) ->
val derivationPathJson = derivationPathAdapter.toJson(derivationPath)
val derivationPathEncoded = derivationPathJson.encodeToByteArray().toHexString()
val extendedPublicKeyJson = extendedPublicKeyAdapter.toJson(extendedPublicKey)
jsonMap[derivationPathEncoded] = extendedPublicKeyJson
}
mapAdapter.toJson(writer, jsonMap)
}
@FromJson
fun fromJson(
reader: JsonReader,
mapAdapter: JsonAdapter<Map<String, String>>,
derivationPathAdapter: JsonAdapter<DerivationPath>,
extendedPublicKeyAdapter: JsonAdapter<ExtendedPublicKey>,
): ExtendedPublicKeysMap {
val map = mutableMapOf<DerivationPath, ExtendedPublicKey>()
mapAdapter.fromJson(reader)?.forEach { (derivationPathEncoded, extendedPublicKeyJson) ->
val derivationPathJson = derivationPathEncoded.hexToBytes().decodeToString()
val derivationPath = derivationPathAdapter.fromJson(derivationPathJson)
val extendedPublicKey = extendedPublicKeyAdapter.fromJson(extendedPublicKeyJson)
if (derivationPath != null && extendedPublicKey != null) {
map[derivationPath] = extendedPublicKey
}
}
return ExtendedPublicKeysMap(map)
}
}

View file

@ -1,52 +0,0 @@
package com.tangem.tap.domain.userWalletList.utils.json
import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.ToJson
import com.tangem.common.extensions.ByteArrayKey
import com.tangem.operations.derivation.ExtendedPublicKeysMap
internal class ScanResponseDerivedKeysMapAdapter {
@ToJson
fun toJson(
writer: JsonWriter,
src: Map<ByteArrayKey, ExtendedPublicKeysMap>,
mapAdapter: JsonAdapter<Map<String, String>>,
byteArrayKeyAdapter: JsonAdapter<ByteArrayKey>,
extendedPublicKeysMapAdapter: JsonAdapter<ExtendedPublicKeysMap>,
) {
val jsonMap = mutableMapOf<String, String>()
src.forEach { (key, extendedPublicKeysMap) ->
val keyJson = byteArrayKeyAdapter.toJson(key)
val extendedPublicKeysMapJson = extendedPublicKeysMapAdapter.toJson(extendedPublicKeysMap)
jsonMap[keyJson] = extendedPublicKeysMapJson
}
mapAdapter.toJson(writer, jsonMap)
}
@FromJson
fun fromJson(
reader: JsonReader,
mapAdapter: JsonAdapter<Map<String, String>>,
byteArrayKeyAdapter: JsonAdapter<ByteArrayKey>,
extendedPublicKeysMapAdapter: JsonAdapter<ExtendedPublicKeysMap>,
): Map<ByteArrayKey, ExtendedPublicKeysMap> {
val map = mutableMapOf<ByteArrayKey, ExtendedPublicKeysMap>()
mapAdapter.fromJson(reader)?.forEach { (keyJson, extendedPublicKeysMapJson) ->
val key = byteArrayKeyAdapter.fromJson(keyJson)
val extendedPublicKeysMap = extendedPublicKeysMapAdapter.fromJson(extendedPublicKeysMapJson)
if (key != null && extendedPublicKeysMap != null) {
map[key] = extendedPublicKeysMap
}
}
return map
}
}

View file

@ -1,56 +0,0 @@
package com.tangem.tap.domain.userWalletList.utils.json
import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.ToJson
import com.tangem.common.extensions.hexToBytes
import com.tangem.common.extensions.toHexString
import com.tangem.crypto.hdWallet.DerivationPath
import com.tangem.crypto.hdWallet.bip32.ExtendedPublicKey
internal class WalletDerivedKeysMapAdapter {
@ToJson
fun toJson(
writer: JsonWriter,
src: Map<DerivationPath, ExtendedPublicKey>,
mapAdapter: JsonAdapter<Map<String, String>>,
derivationPathAdapter: JsonAdapter<DerivationPath>,
extendedPublicKeyAdapter: JsonAdapter<ExtendedPublicKey>,
) {
val jsonMap = mutableMapOf<String, String>()
src.forEach { (derivationPath, extendedPublicKey) ->
val derivationPathJson = derivationPathAdapter.toJson(derivationPath)
val derivationPathEncoded = derivationPathJson.encodeToByteArray().toHexString()
val extendedPublicKeyJson = extendedPublicKeyAdapter.toJson(extendedPublicKey)
jsonMap[derivationPathEncoded] = extendedPublicKeyJson
}
mapAdapter.toJson(writer, jsonMap)
}
@FromJson
fun fromJson(
reader: JsonReader,
mapAdapter: JsonAdapter<Map<String, String>>,
derivationPathAdapter: JsonAdapter<DerivationPath>,
extendedPublicKeyAdapter: JsonAdapter<ExtendedPublicKey>,
): Map<DerivationPath, ExtendedPublicKey> {
val map = mutableMapOf<DerivationPath, ExtendedPublicKey>()
mapAdapter.fromJson(reader)?.forEach { (derivationPathEncoded, extendedPublicKeyJson) ->
val derivationPathJson = derivationPathEncoded.hexToBytes().decodeToString()
val derivationPath = derivationPathAdapter.fromJson(derivationPathJson)
val extendedPublicKey = extendedPublicKeyAdapter.fromJson(extendedPublicKeyJson)
if (derivationPath != null && extendedPublicKey != null) {
map[derivationPath] = extendedPublicKey
}
}
return map
}
}