Updated on 2026-08-14
This commit is contained in:
parent
35fd75485c
commit
3e01845203
21 changed files with 1330 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue