Move words(fromMissingIndices:size) to RecoveryPhrase

This commit is contained in:
Francisco Gindre 2022-01-06 11:40:59 -03:00
parent 2a91f3a541
commit 91ead4675b
3 changed files with 11 additions and 11 deletions

View File

@ -83,6 +83,14 @@ struct RecoveryPhrase: Equatable {
func toString() -> String {
words.joined(separator: " ")
}
func words(fromMissingIndices indices: [Int]) -> [PhraseChip.Kind] {
assert((indices.count - 1) * chunkSize <= self.words.count)
return indices.enumerated().map({ index, position in
.unassigned(word: self.words[(index * chunkSize) + position])
})
}
}
struct RecoveryPhraseDisplayState: Equatable {

View File

@ -71,7 +71,7 @@ extension RecoveryPhraseValidationState {
/// - Note: Use this function to create a random validation puzzle for a given phrase.
static func random(phrase: RecoveryPhrase) -> Self {
let missingIndices = Self.randomIndices()
let missingWordChipKind = Self.pickWords(fromMissingIndices: missingIndices, phrase: phrase).shuffled()
let missingWordChipKind = phrase.words(fromMissingIndices: missingIndices)
return RecoveryPhraseValidationState(
phrase: phrase,
missingIndices: missingIndices,
@ -159,14 +159,6 @@ extension RecoveryPhraseValidationState {
}
}
static func pickWords(fromMissingIndices indices: [Int], phrase: RecoveryPhrase) -> [PhraseChip.Kind] {
assert((indices.count - 1) * Self.wordGroupSize <= phrase.words.count)
return indices.enumerated().map({ index, position in
.unassigned(word: phrase.words[(index * Self.wordGroupSize) + position])
})
}
static func randomIndices() -> [Int] {
return (0..<phraseChunks).map { _ in
Int.random(in: 0 ..< wordGroupSize)

View File

@ -29,7 +29,7 @@ class RecoveryPhraseValidationTests: XCTestCase {
let expected = ["salute", "boil", "cancel", "pizza"].map({ PhraseChip.Kind.unassigned(word: $0) })
let result = RecoveryPhraseValidationState.pickWords(fromMissingIndices: indices, phrase: phrase)
let result = phrase.words(fromMissingIndices: indices)
XCTAssertEqual(expected, result)
}
@ -319,7 +319,7 @@ class RecoveryPhraseValidationTests: XCTestCase {
let result = RecoveryPhraseValidationState(
phrase: phrase,
missingIndices: missingIndices,
missingWordChips: RecoveryPhraseValidationState.pickWords(fromMissingIndices: missingIndices, phrase: phrase),
missingWordChips: phrase.words(fromMissingIndices: missingIndices),
validationWords: completion,
route: nil
)