aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWhymarrh Whitby <whymarrh.whitby@gmail.com>2019-06-28 01:14:41 +0800
committerGitHub <noreply@github.com>2019-06-28 01:14:41 +0800
commitd566543bf518827e29ae4b61d673e54f94efe8b7 (patch)
tree029ec9e0ffd458353bdf533429113a889df9ded4
parenta6bfc6f441e280effa046700adb52414fdb2de6d (diff)
downloadtangerine-wallet-browser-d566543bf518827e29ae4b61d673e54f94efe8b7.tar.gz
tangerine-wallet-browser-d566543bf518827e29ae4b61d673e54f94efe8b7.tar.zst
tangerine-wallet-browser-d566543bf518827e29ae4b61d673e54f94efe8b7.zip
Handle invalid strings during seed phrase import (#6743)
* Add tests for ImportWithSeedPhrase#parseSeedPhrase * Handle importing whitespace-only seed phrases Fixes #6694 This changeset fixes our parsing of seed phrases during import to handle the case where a user tries to import a seed phrase that consists solely of whitespace. We no longer produce an error and instead treat it as an incorrect seed phrase. * Handle importing more invalid seed phrases
-rw-r--r--ui/app/pages/first-time-flow/create-password/import-with-seed-phrase/import-with-seed-phrase.component.js19
-rw-r--r--ui/app/pages/first-time-flow/create-password/import-with-seed-phrase/tests/import-with-seed-phrase.component.test.js78
2 files changed, 93 insertions, 4 deletions
diff --git a/ui/app/pages/first-time-flow/create-password/import-with-seed-phrase/import-with-seed-phrase.component.js b/ui/app/pages/first-time-flow/create-password/import-with-seed-phrase/import-with-seed-phrase.component.js
index 5092d277e..a2fb5a3bf 100644
--- a/ui/app/pages/first-time-flow/create-password/import-with-seed-phrase/import-with-seed-phrase.component.js
+++ b/ui/app/pages/first-time-flow/create-password/import-with-seed-phrase/import-with-seed-phrase.component.js
@@ -30,10 +30,21 @@ export default class ImportWithSeedPhrase extends PureComponent {
}
parseSeedPhrase = (seedPhrase) => {
- return seedPhrase
- .trim()
- .match(/\w+/g)
- .join(' ')
+ if (!seedPhrase) {
+ return ''
+ }
+
+ const trimmed = seedPhrase.trim()
+ if (!trimmed) {
+ return ''
+ }
+
+ const words = trimmed.match(/\w+/g)
+ if (!words) {
+ return ''
+ }
+
+ return words.join(' ')
}
componentWillMount () {
diff --git a/ui/app/pages/first-time-flow/create-password/import-with-seed-phrase/tests/import-with-seed-phrase.component.test.js b/ui/app/pages/first-time-flow/create-password/import-with-seed-phrase/tests/import-with-seed-phrase.component.test.js
new file mode 100644
index 000000000..7960d17b2
--- /dev/null
+++ b/ui/app/pages/first-time-flow/create-password/import-with-seed-phrase/tests/import-with-seed-phrase.component.test.js
@@ -0,0 +1,78 @@
+import React from 'react'
+import assert from 'assert'
+import { shallow } from 'enzyme'
+import sinon from 'sinon'
+import ImportWithSeedPhrase from '../import-with-seed-phrase.component'
+
+function shallowRender (props = {}, context = {}) {
+ return shallow(<ImportWithSeedPhrase {...props} />, {
+ context: {
+ t: str => str + '_t',
+ metricsEvent: sinon.spy(),
+ ...context,
+ },
+ })
+}
+
+describe('ImportWithSeedPhrase Component', () => {
+ it('should render without error', () => {
+ const root = shallowRender({
+ onSubmit: sinon.spy(),
+ })
+ const textareaCount = root.find('.first-time-flow__textarea').length
+ assert.equal(textareaCount, 1, 'should render 12 seed phrases')
+ })
+
+ describe('parseSeedPhrase', () => {
+ it('should handle a regular seed phrase', () => {
+ const root = shallowRender({
+ onSubmit: sinon.spy(),
+ })
+
+ const {parseSeedPhrase} = root.instance()
+
+ assert.deepEqual(parseSeedPhrase('foo bar baz'), 'foo bar baz')
+ })
+
+ it('should trim extraneous whitespace from the given seed phrase', () => {
+ const root = shallowRender({
+ onSubmit: sinon.spy(),
+ })
+
+ const {parseSeedPhrase} = root.instance()
+
+ assert.deepEqual(parseSeedPhrase(' foo bar baz '), 'foo bar baz')
+ })
+
+ it('should return an empty string when given a whitespace-only string', () => {
+ const root = shallowRender({
+ onSubmit: sinon.spy(),
+ })
+
+ const {parseSeedPhrase} = root.instance()
+
+ assert.deepEqual(parseSeedPhrase(' '), '')
+ })
+
+ it('should return an empty string when given a string with only symbols', () => {
+ const root = shallowRender({
+ onSubmit: sinon.spy(),
+ })
+
+ const {parseSeedPhrase} = root.instance()
+
+ assert.deepEqual(parseSeedPhrase('$'), '')
+ })
+
+ it('should return an empty string for both null and undefined', () => {
+ const root = shallowRender({
+ onSubmit: sinon.spy(),
+ })
+
+ const {parseSeedPhrase} = root.instance()
+
+ assert.deepEqual(parseSeedPhrase(undefined), '')
+ assert.deepEqual(parseSeedPhrase(null), '')
+ })
+ })
+})