Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create sorted copy of the recovery phrase with original index
- Loading branch information
Showing
2 changed files
with
55 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// @flow | ||
|
||
export type SortedRecoveryPhraseEntry = {| | ||
id: number, | ||
word: string, | ||
originalIdx: number, | ||
|}; | ||
|
||
export function makeSortedPhrase(recoveryPhrase: Array<string>): SortedRecoveryPhraseEntry[] { | ||
const sorted = recoveryPhrase.slice().sort(); | ||
const wordIndexes = new Set(); | ||
|
||
return sorted.map((sortedWord, sortedWordIdx) => { | ||
const originalIdx = recoveryPhrase.findIndex((originalWord, idx) => { | ||
return sortedWord === originalWord && !wordIndexes.has(idx) | ||
}); | ||
|
||
if (originalIdx === -1) throw new Error('Word not found in the original recovery phrase. Should never happen'); | ||
|
||
// Mark word index as watched to handle recovery phrase with duplicates | ||
wordIndexes.add(originalIdx); | ||
|
||
return { | ||
id: sortedWordIdx, | ||
word: sortedWord, | ||
originalIdx, | ||
}; | ||
}); | ||
} |