Updated on 2026-08-14

This commit is contained in:
Tangem 2025-09-01 13:58:38 +03:00
commit 9ba66ba084
179 changed files with 5757 additions and 1156 deletions

View file

@ -1,5 +1,8 @@
package com.tangem.domain.models.account
import arrow.core.Either
import arrow.core.raise.either
import arrow.core.raise.ensure
import com.tangem.common.extensions.toByteArray
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.utils.extensions.toHexString
@ -18,9 +21,39 @@ data class AccountId private constructor(
val userWalletId: UserWalletId,
) {
sealed interface Error {
val tag: String
get() = this::class.simpleName ?: "AccountId.Error"
data object Empty : Error {
override fun toString(): String = "$tag: Account ID cannot be blank"
}
data object InvalidFormat : Error {
override fun toString(): String = "$tag: Account ID must be a 64-character hexadecimal string"
}
}
companion object {
private val sha256Digest: MessageDigest by lazy { MessageDigest.getInstance("SHA-256") }
private val hexRegex = Regex("^[a-fA-F0-9]{64}$")
/**
* Creates a unique account identifier for a crypto portfolio
*
* @param userWalletId the identifier of the user wallet
* @param value the unique string value representing the account
*
* @return an [Either] containing the [AccountId] on success, or an [Error] on failure
*/
fun forCryptoPortfolio(userWalletId: UserWalletId, value: String): Either<Error, AccountId> = either {
ensure(value.isNotBlank()) { Error.Empty }
ensure(value.matches(hexRegex)) { Error.InvalidFormat }
AccountId(value = value, userWalletId = userWalletId)
}
/**
* Creates a unique account identifier for a crypto portfolio