diff --git a/packages/yoroi-extension/app/components/wallet/create-wallet/VerifyRecoveryPhraseStep.js b/packages/yoroi-extension/app/components/wallet/create-wallet/VerifyRecoveryPhraseStep.js index d746c88bca..1303062c4b 100644 --- a/packages/yoroi-extension/app/components/wallet/create-wallet/VerifyRecoveryPhraseStep.js +++ b/packages/yoroi-extension/app/components/wallet/create-wallet/VerifyRecoveryPhraseStep.js @@ -10,6 +10,7 @@ import styles from './VerifyRecoveryPhraseStep.scss'; import classnames from 'classnames'; import { ReactComponent as VerifiedIcon } from '../../../assets/images/verify-icon-green.inline.svg' import environment from '../../../environment'; +import { makeSortedPhrase } from '../../../utils/recoveryPhrase'; const messages: * = defineMessages({ description: { @@ -74,30 +75,9 @@ function VerifyRecoveryPhraseStep(props: Props & Intl): Node { return () => setCurrentStep(CREATE_WALLET_SETPS.ADD_WALLET_DETAILS) }; - const shortedRecoveryPhrase = useMemo(() => { - const sorted = recoveryPhrase.slice().sort(); + const sortedRecoveryPhrase = useMemo(() => makeSortedPhrase(recoveryPhrase), [recoveryPhrase]); - 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 { - word: sortedWord, - originalIdx, - id: sortedWordIdx, - }; - }); - }, [recoveryPhrase]); - - console.log(shortedRecoveryPhrase); + console.log(sortedRecoveryPhrase, recoveryPhrase); return ( diff --git a/packages/yoroi-extension/app/utils/recoveryPhrase.test.js b/packages/yoroi-extension/app/utils/recoveryPhrase.test.js new file mode 100644 index 0000000000..7029710f11 --- /dev/null +++ b/packages/yoroi-extension/app/utils/recoveryPhrase.test.js @@ -0,0 +1,41 @@ +// @flow + +import { makeSortedPhrase } from './recoveryPhrase'; + +describe('makeSortedPhrase()', () => { + test('sort recovery phrase with unique words', () => { + const phraseWithUniqueWords = ['w_3', 'w_2', 'w_1']; + const sorted = makeSortedPhrase(phraseWithUniqueWords); + + const [first, second, third] = sorted; + expect(first.id).toBe(0); + expect(first.word).toBe('w_1'); + expect(first.originalIdx).toBe(2); + + expect(second.id).toBe(1); + expect(second.word).toBe('w_2'); + expect(second.originalIdx).toBe(1); + + expect(third.id).toBe(2); + expect(third.word).toBe('w_3'); + expect(third.originalIdx).toBe(0); + }); + + test('sort recovery phrase with duplicates', () => { + const recoveryPhraseWithDuplicates = ['abandon', 'address', 'abandon']; + const sorted = makeSortedPhrase(recoveryPhraseWithDuplicates); + + const [first, second, third] = sorted; + expect(first.id).toBe(0); + expect(first.word).toBe('abandon'); + expect(first.originalIdx).toBe(0); + + expect(second.id).toBe(1); + expect(second.word).toBe('abandon'); + expect(second.originalIdx).toBe(2); + + expect(third.id).toBe(2); + expect(third.word).toBe('address'); + expect(third.originalIdx).toBe(1); + }) +}); \ No newline at end of file