-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AND-9038 Added Sign transactions with different sizes of hashes
- Loading branch information
Showing
7 changed files
with
293 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tangem-sdk-core/src/main/java/com/tangem/operations/sign/ChunkHashesUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.tangem.operations.sign | ||
|
||
object ChunkHashesUtils { | ||
// The max answer is 1152 bytes (unencrypted) and 1120 (encrypted). | ||
// The worst case is 8 hashes * 64 bytes for ed + 512 bytes of signatures + cardId, SignedHashes + TLV + SW is ok. | ||
private const val PACKAGE_SIZE = 512 | ||
// Card limitation | ||
private const val MAX_CHUNK_SIZE = 10 | ||
|
||
fun chunkHashes(hashesRaw: Array<ByteArray>): List<Chunk> { | ||
val hashes = hashesRaw.mapIndexed { index, hash -> Hash(index = index, data = hash) } | ||
val hashesBySize = hashes.groupBy { it.data.size } | ||
|
||
return hashesBySize.flatMap { hashesGroup -> | ||
val hashSize = hashesGroup.key | ||
val chunkSize = getChunkSize(hashSize) | ||
|
||
hashesGroup.value | ||
.chunked(chunkSize) | ||
.map { Chunk(hashSize, it) } | ||
} | ||
} | ||
|
||
private fun getChunkSize(hashSize: Int) = (PACKAGE_SIZE / hashSize).coerceIn(1, MAX_CHUNK_SIZE) | ||
} |
29 changes: 29 additions & 0 deletions
29
tangem-sdk-core/src/main/java/com/tangem/operations/sign/ChunkedHashesContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.tangem.operations.sign | ||
|
||
class ChunkedHashesContainer( | ||
hashes: Array<ByteArray>, | ||
) { | ||
val isEmpty: Boolean = hashes.isEmpty() | ||
var currentChunkIndex: Int = 0 | ||
private set | ||
|
||
val chunks = ChunkHashesUtils.chunkHashes(hashes) | ||
|
||
private var signedChunks: MutableList<SignedChunk> = mutableListOf() | ||
|
||
fun getCurrentChunk(): Chunk { | ||
return chunks[currentChunkIndex] | ||
} | ||
|
||
fun addSignedChunk(signedChunk: SignedChunk) { | ||
signedChunks.add(signedChunk) | ||
currentChunkIndex++ | ||
} | ||
|
||
fun getSignatures(): List<ByteArray> { | ||
return signedChunks | ||
.flatMap { it.signedHashes } | ||
.sortedBy { it.index } | ||
.map { it.signature } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
tangem-sdk-core/src/main/java/com/tangem/operations/sign/SignDTO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.tangem.operations.sign | ||
|
||
data class Hash( | ||
val index: Int, | ||
val data: ByteArray, | ||
) | ||
|
||
data class SignedHash( | ||
val index: Int, | ||
val data: ByteArray, | ||
val signature: ByteArray, | ||
) | ||
|
||
data class Chunk( | ||
val hashSize: Int, | ||
val hashes: List<Hash>, | ||
) | ||
|
||
data class SignedChunk( | ||
val signedHashes: List<SignedHash>, | ||
) |
Oops, something went wrong.