From 1fa4b6061412674002275f2237021a7ae117cc0e Mon Sep 17 00:00:00 2001 From: Tangem Date: Sun, 23 Feb 2020 08:57:45 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../src/main/java/com/tangem/CardManager.kt | 76 ++++++++-------- .../tangem/commands/ReadUserDataCommand.kt | 88 +++++++++---------- .../tangem/commands/WriteUserDataCommand.kt | 59 +++++++------ .../main/java/com/tangem/common/tlv/TlvTag.kt | 8 +- 4 files changed, 116 insertions(+), 115 deletions(-) diff --git a/tangem-core/src/main/java/com/tangem/CardManager.kt b/tangem-core/src/main/java/com/tangem/CardManager.kt index 3e1d706b5c..b679c726ce 100644 --- a/tangem-core/src/main/java/com/tangem/CardManager.kt +++ b/tangem-core/src/main/java/com/tangem/CardManager.kt @@ -177,45 +177,45 @@ class CardManager( runTask(task, cardId, callback) } - /** - * This command write some of User_Data, User_ProtectedData, User_Counter and User_ProtectedCounter fields. - * User_Data and User_ProtectedData are never changed or parsed by the executable code the Tangem COS. - * The App defines purpose of use, format and it's payload. For example, this field may contain cashed information - * from blockchain to accelerate preparing new transaction. - * User_Counter and User_ProtectedCounter are counters, that initial values can be set by App and increased on every signing - * of new transaction (on SIGN command that calculate new signatures). The App defines purpose of use. - * For example, this fields may contain blockchain nonce value. - * - * Writing of User_Counter and User_Data protected only by PIN1. - * User_ProtectedCounter and User_ProtectedData additionaly need PIN2 to confirmation. - */ - fun writeUserData( - cardId: String, - userData: ByteArray, - userProtectedData: ByteArray? = null, - userCounter: Int? = null, - userProtectedCounter: Int? = null, - callback: (result: TaskEvent) -> Unit - ) { - val writeIssuerDataCommand = WriteUserDataCommand(userData, userProtectedData, userCounter, userProtectedCounter) - val task = SingleCommandTask(writeIssuerDataCommand) - runTask(task, cardId, callback) - } + /** + * This command write some of User_Data, User_ProtectedData, User_Counter and User_ProtectedCounter fields. + * User_Data and User_ProtectedData are never changed or parsed by the executable code the Tangem COS. + * The App defines purpose of use, format and it's payload. For example, this field may contain cashed information + * from blockchain to accelerate preparing new transaction. + * User_Counter and User_ProtectedCounter are counters, that initial values can be set by App and increased on every signing + * of new transaction (on SIGN command that calculate new signatures). The App defines purpose of use. + * For example, this fields may contain blockchain nonce value. + * + * Writing of User_Counter and User_Data protected only by PIN1. + * User_ProtectedCounter and User_ProtectedData additionaly need PIN2 to confirmation. + */ + fun writeUserData( + cardId: String, + userData: ByteArray, + userProtectedData: ByteArray? = null, + userCounter: Int? = null, + userProtectedCounter: Int? = null, + callback: (result: TaskEvent) -> Unit + ) { + val writeIssuerDataCommand = WriteUserDataCommand(userData, userProtectedData, userCounter, userProtectedCounter) + val task = SingleCommandTask(writeIssuerDataCommand) + runTask(task, cardId, callback) + } - /** - * This command returns two up to 512-byte User_Data, User_Protected_Data and two counters User_Counter and - * User_Protected_Counter fields. - * User_Data and User_ProtectedData are never changed or parsed by the executable code the Tangem COS. - * The App defines purpose of use, format and it's payload. For example, this field may contain cashed information - * from blockchain to accelerate preparing new transaction. - * User_Counter and User_ProtectedCounter are counters, that initial values can be set by App and increased on every signing - * of new transaction (on SIGN command that calculate new signatures). The App defines purpose of use. - * For example, this fields may contain blockchain nonce value. - */ - fun readUserData(cardId: String, callback: (result: TaskEvent) -> Unit) { - val task = SingleCommandTask(ReadUserDataCommand()) - runTask(task, cardId, callback) - } + /** + * This command returns two up to 512-byte User_Data, User_Protected_Data and two counters User_Counter and + * User_Protected_Counter fields. + * User_Data and User_ProtectedData are never changed or parsed by the executable code the Tangem COS. + * The App defines purpose of use, format and it's payload. For example, this field may contain cashed information + * from blockchain to accelerate preparing new transaction. + * User_Counter and User_ProtectedCounter are counters, that initial values can be set by App and increased on every signing + * of new transaction (on SIGN command that calculate new signatures). The App defines purpose of use. + * For example, this fields may contain blockchain nonce value. + */ + fun readUserData(cardId: String, callback: (result: TaskEvent) -> Unit) { + val task = SingleCommandTask(ReadUserDataCommand()) + runTask(task, cardId, callback) + } /** * This command will create a new wallet on the card having ‘Empty’ state. diff --git a/tangem-core/src/main/java/com/tangem/commands/ReadUserDataCommand.kt b/tangem-core/src/main/java/com/tangem/commands/ReadUserDataCommand.kt index e1fbaa972a..a321876aee 100644 --- a/tangem-core/src/main/java/com/tangem/commands/ReadUserDataCommand.kt +++ b/tangem-core/src/main/java/com/tangem/commands/ReadUserDataCommand.kt @@ -13,41 +13,41 @@ import com.tangem.tasks.TaskError [REDACTED_AUTHOR] */ class ReadUserDataResponse( - /** - * CID, Unique Tangem card ID number. - */ - val cardId: String, + /** + * CID, Unique Tangem card ID number. + */ + val cardId: String, - /** - * Data defined by user's App. - */ - val userData: ByteArray, + /** + * Data defined by user's App. + */ + val userData: ByteArray, - /** - * Data defined by user's App (confirmed by PIN2). - */ - val userProtectedData: ByteArray, + /** + * Data defined by user's App (confirmed by PIN2). + */ + val userProtectedData: ByteArray, - /** - * Counter initialized by user's App and increased on every signing of new transaction - */ - val userCounter: Int, + /** + * Counter initialized by user's App and increased on every signing of new transaction + */ + val userCounter: Int, - /** - * Counter initialized by user's App (confirmed by PIN2) and increased on every signing of new transaction - */ - val userProtectedCounter: Int + /** + * Counter initialized by user's App (confirmed by PIN2) and increased on every signing of new transaction + */ + val userProtectedCounter: Int ): CommandResponse { - companion object { - internal fun fromMapper(mapper: TlvMapper): ReadUserDataResponse = ReadUserDataResponse( - cardId = mapper.map(TlvTag.CardId), - userData = mapper.map(TlvTag.IssuerData), - userProtectedData = mapper.map(TlvTag.IssuerDataSignature), - userCounter = mapper.map(TlvTag.IssuerDataCounter), - userProtectedCounter = mapper.map(TlvTag.IssuerDataCounter) - ) - } + companion object { + internal fun fromMapper(mapper: TlvMapper): ReadUserDataResponse = ReadUserDataResponse( + cardId = mapper.map(TlvTag.CardId), + userData = mapper.map(TlvTag.IssuerData), + userProtectedData = mapper.map(TlvTag.IssuerDataSignature), + userCounter = mapper.map(TlvTag.IssuerDataCounter), + userProtectedCounter = mapper.map(TlvTag.IssuerDataCounter) + ) + } } /** @@ -62,22 +62,22 @@ class ReadUserDataResponse( */ class ReadUserDataCommand: CommandSerializer() { - override fun serialize(cardEnvironment: CardEnvironment): CommandApdu { - val serializedTlv = TlvBuilder().apply { - append(TlvTag.CardId, cardEnvironment.cardId) - append(TlvTag.Pin, cardEnvironment.pin1) - }.serialize() + override fun serialize(cardEnvironment: CardEnvironment): CommandApdu { + val serializedTlv = TlvBuilder().apply { + append(TlvTag.CardId, cardEnvironment.cardId) + append(TlvTag.Pin, cardEnvironment.pin1) + }.serialize() - return CommandApdu(Instruction.ReadUserData, serializedTlv) - } + return CommandApdu(Instruction.ReadUserData, serializedTlv) + } - override fun deserialize(cardEnvironment: CardEnvironment, responseApdu: ResponseApdu): ReadUserDataResponse? { - val tlvData = responseApdu.getTlvData() ?: return null + override fun deserialize(cardEnvironment: CardEnvironment, responseApdu: ResponseApdu): ReadUserDataResponse? { + val tlvData = responseApdu.getTlvData() ?: return null - return try { - ReadUserDataResponse.fromMapper(TlvMapper(tlvData)) - } catch (exception: Exception) { - throw TaskError.SerializeCommandError() - } - } + return try { + ReadUserDataResponse.fromMapper(TlvMapper(tlvData)) + } catch (exception: Exception) { + throw TaskError.SerializeCommandError() + } + } } \ No newline at end of file diff --git a/tangem-core/src/main/java/com/tangem/commands/WriteUserDataCommand.kt b/tangem-core/src/main/java/com/tangem/commands/WriteUserDataCommand.kt index 64d5359e2d..689e274dea 100644 --- a/tangem-core/src/main/java/com/tangem/commands/WriteUserDataCommand.kt +++ b/tangem-core/src/main/java/com/tangem/commands/WriteUserDataCommand.kt @@ -14,10 +14,10 @@ import com.tangem.tasks.TaskError */ class WriteUserDataResponse( - /** - * CID, Unique Tangem card ID number. - */ - val cardId: String + /** + * CID, Unique Tangem card ID number. + */ + val cardId: String ): CommandResponse /** @@ -33,32 +33,33 @@ class WriteUserDataResponse( * User_ProtectedCounter and User_ProtectedData additionaly need PIN2 to confirmation. */ class WriteUserDataCommand( - private val userData: ByteArray? = null, - private val userProtectedData: ByteArray? = null, - private val userCounter: Int? = null, - private val userProtectedCounter: Int? = null + private val userData: ByteArray? = null, + private val userProtectedData: ByteArray? = null, + private val userCounter: Int? = null, + private val userProtectedCounter: Int? = null ): CommandSerializer() { - override fun serialize(cardEnvironment: CardEnvironment): CommandApdu { - val serializedTlv = TlvBuilder().apply { - append(TlvTag.CardId, cardEnvironment.cardId) - append(TlvTag.Pin, cardEnvironment.pin1) - append(TlvTag.UserData, userData) - append(TlvTag.UserCounter, userCounter) - append(TlvTag.UserProtectedData, userProtectedData) - append(TlvTag.UserProtectedCounter, userProtectedCounter) - if (userProtectedCounter != null || userProtectedData != null) - append(TlvTag.Pin2, cardEnvironment.pin2) - }.serialize() - return CommandApdu(Instruction.WriteUserData, serializedTlv) - } - override fun deserialize(cardEnvironment: CardEnvironment, responseApdu: ResponseApdu): WriteUserDataResponse? { - val tlvData = responseApdu.getTlvData() ?: return null + override fun serialize(cardEnvironment: CardEnvironment): CommandApdu { + val serializedTlv = TlvBuilder().apply { + append(TlvTag.CardId, cardEnvironment.cardId) + append(TlvTag.Pin, cardEnvironment.pin1) + append(TlvTag.UserData, userData) + append(TlvTag.UserCounter, userCounter) + append(TlvTag.UserProtectedData, userProtectedData) + append(TlvTag.UserProtectedCounter, userProtectedCounter) + if (userProtectedCounter != null || userProtectedData != null) + append(TlvTag.Pin2, cardEnvironment.pin2) + }.serialize() + return CommandApdu(Instruction.WriteUserData, serializedTlv) + } - return try { - WriteUserDataResponse(TlvMapper(tlvData).map(TlvTag.CardId)) - } catch (exception: Exception) { - throw TaskError.SerializeCommandError() - } - } + override fun deserialize(cardEnvironment: CardEnvironment, responseApdu: ResponseApdu): WriteUserDataResponse? { + val tlvData = responseApdu.getTlvData() ?: return null + + return try { + WriteUserDataResponse(TlvMapper(tlvData).map(TlvTag.CardId)) + } catch (exception: Exception) { + throw TaskError.SerializeCommandError() + } + } } \ No newline at end of file diff --git a/tangem-core/src/main/java/com/tangem/common/tlv/TlvTag.kt b/tangem-core/src/main/java/com/tangem/common/tlv/TlvTag.kt index 19d0713c7d..43732307e6 100644 --- a/tangem-core/src/main/java/com/tangem/common/tlv/TlvTag.kt +++ b/tangem-core/src/main/java/com/tangem/common/tlv/TlvTag.kt @@ -108,10 +108,10 @@ enum class TlvTag(val code: Int) { TerminalPublicKey(0x5C), TerminalTransactionSignature(0x57), - UserData(0x2A), - UserProtectedData(0x2B), - UserCounter(0x2C), - UserProtectedCounter(0x2D); + UserData(0x2A), + UserProtectedData(0x2B), + UserCounter(0x2C), + UserProtectedCounter(0x2D); /** * @return [TlvValueType] associated with a [TlvTag]