-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IOS-8008 Sign hashes with different sizes #382
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// ChunkHashesUtil.swift | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Перенес старую логику чанкования из команды в отдельную утилиту ради тестируемости и добавил сюда новый чанкинг |
||
// TangemSdk | ||
// | ||
// Created by Alexander Osokin on 31.10.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
struct ChunkHashesUtil { | ||
func chunkHashes(_ hashes: [Data]) -> [Chunk] { | ||
let hashes = hashes.enumerated().map { Hash(index: $0.offset, data: $0.element) } | ||
let hashesBySize = Dictionary(grouping: hashes, by: { $0.data.count }) | ||
|
||
let chunks = hashesBySize.flatMap { hashesGroup in | ||
let hashSize = hashesGroup.key | ||
let chunkSize = getChunkSize(for: hashSize) | ||
|
||
let chunkedHashes = hashesGroup.value.chunked(into: chunkSize) | ||
let chunks = chunkedHashes.map { Chunk(hashSize: hashSize, hashes: $0) } | ||
|
||
return chunks | ||
} | ||
|
||
return chunks | ||
} | ||
|
||
func getChunkSize(for hashSize: Int) -> Int { | ||
/// These devices are not able to sign long hashes. | ||
if NFCUtils.isPoorNfcQualityDevice { | ||
return Constants.maxChunkSizePoorNfcQualityDevice | ||
} | ||
|
||
guard hashSize > 0 else { | ||
return Constants.maxChunkSize | ||
} | ||
|
||
let estimatedChunkSize = Constants.packageSize / hashSize | ||
let chunkSize = max(1, min(estimatedChunkSize, Constants.maxChunkSize)) | ||
return chunkSize | ||
} | ||
} | ||
|
||
// MARK: - Constants | ||
|
||
private extension ChunkHashesUtil { | ||
enum Constants { | ||
/// 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. | ||
static let packageSize = 512 | ||
|
||
/// Card limitation | ||
static let maxChunkSize = 10 | ||
|
||
/// Empirical value | ||
static let maxChunkSizePoorNfcQualityDevice = 2 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// ChunkedHashesContainer.swift | ||
// TangemSdk | ||
// | ||
// Created by Alexander Osokin on 31.10.2024. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Сюда перенес стейт менеджмент чанков из команды опять же ради тестируемости. Логика чуть поменялась. Раньше я оперировал ренджами, вычисляя чанки на лету, а теперь сразу чанками |
||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
struct ChunkedHashesContainer { | ||
var isEmpty: Bool { chunks.isEmpty } | ||
let chunksCount: Int | ||
|
||
private(set) var currentChunkIndex: Int = 0 | ||
|
||
private let chunks: [Chunk] | ||
private var signedChunks: [SignedChunk] = [] | ||
|
||
init(hashes: [Data]) { | ||
self.chunks = ChunkHashesUtil().chunkHashes(hashes) | ||
self.chunksCount = chunks.count | ||
} | ||
|
||
func getCurrentChunk() throws -> Chunk { | ||
guard currentChunkIndex < chunks.count else { | ||
throw ChunkedHashesContainerError.processingError | ||
} | ||
|
||
return chunks[currentChunkIndex] | ||
} | ||
|
||
mutating func addSignedChunk(_ signedChunk: SignedChunk) { | ||
signedChunks.append(signedChunk) | ||
currentChunkIndex += 1 | ||
} | ||
|
||
func getSignatures() -> [Data] { | ||
let signedHashes = signedChunks.flatMap { $0.signedHashes }.sorted() | ||
let signatures = signedHashes.map { $0.signature } | ||
return signatures | ||
} | ||
} | ||
|
||
// MARK: - ChunkedHashesContainerError | ||
|
||
enum ChunkedHashesContainerError: Error { | ||
case processingError | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
больше не актуально, карта конечно все равно вернет свою ошибку, но посредством сдк эту ошибку получить более невозможно