Skip to content

Commit

Permalink
fix: Crash on invalid words (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuliomir authored Feb 24, 2025
1 parent 1e79fb4 commit 0418802
Showing 1 changed file with 54 additions and 30 deletions.
84 changes: 54 additions & 30 deletions src/screens/InitWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,53 @@ class NewWordsScreen extends React.Component {
}
}

/**
* Handles the validation of the raw seed words input
* @param {string} rawWords String containing all the words written by the user
* @returns {{words: string[], errorMessage: string, isValid: boolean}}
*/
function handleValidWords(rawWords) {
let trimmedWordsStr = rawWords.trim(/\s+/);
let errorMessage = '';
let isValid = false;

try {
const { valid, words } = walletUtils.wordsValid(trimmedWordsStr);
trimmedWordsStr = words;
isValid = valid;
} catch (e) {
// Handling unknown errors
if (!(e instanceof hathorErrors.InvalidWords)) {
throw e;
}

// Handling Invalid Words errors
isValid = false;
errorMessage = e.message;
const listOfInvalidWords = e.invalidWords.filter((word) => word.length !== 0);
if (listOfInvalidWords.length > 0) {
errorMessage = `${errorMessage} List of invalid words: ${e.invalidWords.join(', ')}.`;
}
}
const wordsArr = trimmedWordsStr.split(/\s+/);
const nonEmptyWords = wordsArr.filter((word) => word.length !== 0);
const lowercaseWords = nonEmptyWords.map((word) => word.toLowerCase());

return {
words: lowercaseWords,
errorMessage,
isValid,
}
}

class LoadWordsScreen extends React.Component {
/**
* State for the import wallet words screen
* @type {{rawWords: '', words: string[], errorMessage: string, isValid: boolean}}
*/
state = {
words: '',
rawWords: '',
words: [],
errorMessage: '',
isValid: false,
};
Expand Down Expand Up @@ -265,44 +309,24 @@ class LoadWordsScreen extends React.Component {
}) });

onChangeText = (text) => {
const words = text.trim(/\s+/);
let errorMessage = '';
let isValid = false;
if (words) {
try {
walletUtils.wordsValid(words);
isValid = true;
} catch (e) {
if (e instanceof hathorErrors.InvalidWords) {
errorMessage = e.message;
if (e.invalidWords && e.invalidWords.length > 0) {
errorMessage = `${errorMessage} List of invalid words: ${e.invalidWords.join(', ')}.`;
}
} else {
throw e;
}
}
}

const wordsArr = words.split(/\s+/);
const nonEmptyWords = wordsArr.filter((value) => value.length !== 0);
const nonEmptyWordsLowerCase = nonEmptyWords.map((value) => value.toLowerCase());
const { words, isValid, errorMessage } = handleValidWords(text);
this.setState({
words: nonEmptyWordsLowerCase,
rawWords: text,
words,
errorMessage,
isValid,
});
}

loadClicked = () => {
Keyboard.dismiss();
const words = this.state.words.join(' ');
this.setState({ errorMessage: '' });
const result = walletUtils.wordsValid(words);
if (result.valid) {
this.props.navigation.navigate('ChoosePinScreen', { words });
const { rawWords } = this.state;
const { isValid, errorMessage, words } = handleValidWords(rawWords);

if (isValid) {
this.props.navigation.navigate('ChoosePinScreen', { words: words.join(' ') });
} else {
this.setState({ errorMessage: result.message });
this.setState({ errorMessage });
}
}

Expand Down

0 comments on commit 0418802

Please sign in to comment.