Skip to content

Commit 83478f4

Browse files
authored
More aggressive whitespace removal + lowercase A-Z (#294)
1 parent dae8fc9 commit 83478f4

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

renderer/components/ImportAccountModal/MnemonicImport.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "@/ui/Forms/MnemonicPhrase/MnemonicPhrase";
99
import { TextInput } from "@/ui/Forms/TextInput/TextInput";
1010
import { PillButton } from "@/ui/PillButton/PillButton";
11-
import { validateMnemonic } from "@/utils/mnemonic";
11+
import { formatMnemonic, validateMnemonic } from "@/utils/mnemonic";
1212

1313
const messages = defineMessages({
1414
accountNameLabel: {
@@ -64,7 +64,7 @@ export function MnemonicImport({
6464

6565
onImport({
6666
name: accountName,
67-
account: phraseValues.join(" "),
67+
account: formatMnemonic(phraseValues),
6868
});
6969
}, [accountName, onImport, hasValidName, phraseValues]);
7070

renderer/components/OnboardingFlow/ImportAccount/ImportAccount.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { defineMessages, useIntl } from "react-intl";
1313

1414
import { BackButton } from "@/components/BackButton/BackButton";
1515
import { trpcReact } from "@/providers/TRPCProvider";
16+
import { formatMnemonic } from "@/utils/mnemonic";
1617

1718
import { EncodedKeyImport } from "./EncodedKeyImport";
1819
import { FileImport } from "./FileImport";
@@ -73,7 +74,7 @@ export function ImportAccount() {
7374
importAccount(
7475
{
7576
name,
76-
account,
77+
account: formatMnemonic(account),
7778
},
7879
{
7980
onSuccess: () => {

renderer/ui/Forms/MnemonicPhrase/MnemonicPhrase.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { useCopyToClipboard, useToggle } from "usehooks-ts";
1818
import { COLORS } from "@/ui/colors";
1919
import { useIFToast } from "@/ui/Toast/Toast";
2020
import { useHasGroupBlur } from "@/utils/formUtils";
21+
import { formatMnemonicWord } from "@/utils/mnemonic";
2122
import { MergeProps } from "@/utils/react";
2223

2324
import { FormField, FormFieldProps } from "../FormField/FormField";
@@ -79,8 +80,9 @@ export function MnemonicPhrase({
7980
}
8081
const index = parseInt(number, 10) - 1;
8182
const nextValues = phrase
82-
.toSpliced(index, 1, e.target.value)
83+
.toSpliced(index, 1, formatMnemonicWord(e.target.value))
8384
.slice(0, PHRASE_ITEM_COUNT);
85+
8486
onChange(nextValues);
8587
},
8688
[onChange, phrase],
@@ -98,7 +100,10 @@ export function MnemonicPhrase({
98100
throw new Error("data-number not found in mnemonic phrase input");
99101
}
100102

101-
const words = e.clipboardData.getData("text").trim().split(/\s+/g);
103+
const clipboardText = e.clipboardData.getData("text");
104+
105+
const words = clipboardText.split(/\s+/g).map(formatMnemonicWord);
106+
102107
const index = parseInt(number, 10) - 1;
103108

104109
if (words.length === PHRASE_ITEM_COUNT) {

renderer/utils/mnemonic.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ export const EMPTY_MNEMONIC_ARRAY = Array.from(
44
() => "",
55
);
66

7+
export function formatMnemonicWord(word: string) {
8+
return word
9+
.replace(/([A-Z])/g, (char) => char.toLowerCase())
10+
.replace(/\s+/g, "");
11+
}
12+
713
export function formatMnemonic(phrase: string | Array<string>) {
8-
const asString = Array.isArray(phrase) ? phrase.join(" ") : phrase;
9-
return asString.trim().replace(/\s+/g, " ");
14+
const asArray = Array.isArray(phrase) ? phrase : phrase.split(/\s+/);
15+
return asArray.map(formatMnemonicWord).join(" ");
1016
}
1117

1218
export function validateMnemonic(

0 commit comments

Comments
 (0)