1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 11:01:41 +01:00
metamask-extension/ui/app/pages/first-time-flow/seed-phrase/confirm-seed-phrase/confirm-seed-phrase.component.js
Dan J Miller 7985f4f4f8
3box integration 2.0 (#6972)
* Adds threebox controller

* Adds threebox approval modal

* Fix unit tests and lint after addition of threebox

* Correct threebox behaviour after rejecting request for backup; fixes e2e tests.

* Update threebox controller for automatic syncing

* Ensure frontend locale updates when preferences are changed via direct update within controller

* Add toggle in settings for 3box syncing

* Update threebox controller for latest 3box version

* Delete unnecessary frontend changes for threebox integration

* Backing up address book contacts with threebox

* Update unit tests for 3box-integration additions

* Only enable threebox by default for new wallets

* Mock globals for correct unit tests

* 3box '1.10.2' -> '^1.10.2'

* Correct capilalization on 3Box

* Use log.debug instead of console.log in threebox controller

* Update yarn.lock

* Remove edge build

* Split 3box module into background deps js file

* extra bundle opts for bg-libs

* sync yarn.lock

* new3Box logic

* Show confirm threebox restore after import

* Remove bg-libs.js from manifest file for dev builds

* Switch 3Box controller to using the spaces api (instead of the profile api)

* Finalize switching to spaces api and only restoring from 3box after import

* Update metamask-controller-test.js for threebox controller changes

* Make threebox modal style consistent with others and update success button wording

* Use mock 3box when in test

* Correct 3box modal header

* Remove unnecessary property of threebox controller provider

* Remove unnecessary method calls after restoration from 3box in the threebox-restore-confirm modal.

* Replace setThreeBoxSyncingPermission calls in routes/index.js with turnThreeBoxSyncingOn

* Replace erroneous use of  with

* Replace erroneous use of threeboxSyncing with threeBoxSyncingAllowed in advancted-tab directory

* Lint fixes for 3box changes

* Log errors encountered when updating 3Box

* Remove unnecessary parameter from state update

* Add timeout to initial 3Box sync

The initial 3Box sync will now timeout after 1 minute. If the timeout
is triggered, 3Box is disabled and cannot be re-enabled unless the
initial sync does finally finish. If it never finishes, 3Box cannot
be enabled unless the extension is reinstalled.

The Advanced Settings page was updated to show this option as disabled
in that circumstance, with a new discription explaining why it's
disabled. The UI here could certainly be improved.

Additionally, "on" and "off" labels were added to the toggle to match
the other toggles on the Advanced Settings page.

* Use non-minified 3Box module

We had previously used the minified 3Box module to avoid a build error
encountered when `envify` was processing the `libp2p` module (which is
used by 3Box). The build would fail because `esprima` (used by `envify`)
is incompatible with the object spread/rest operator (which is used in
`libp2p`).

That issue has been solved by adding a global Babelify transformation
specifically for transpiling out the object rest/spread operator from
dependencies. It has been targetted to only affect `libp2p` to avoid
extending the build time too much. This workaround can be used until
a new version of `esprima` is released that includes this bug fix.

* Use app key addresses for threebox

* Replace use of modal for confirming 3box restoration with a home notification

* Adds e2e tests for restoring from threebox

* Update eth-keyring-controller to 5.1.0

* Correct parameters passed to getAppKeyAddress in threebox.js

* Add prefix to origin passed to getAppKeyAddress in threebox.js

* Remove unused locale message.

* Prevent CORS errors in firefox e2e tests

* Ensure extraneous scripts are excluded from the local test dev build

* Move threeBoxLastUpdate state from home.component to redux

* Threebox PR code cleanup

* Always use first address when initializing threebox

* Replace setRestoredFromThreeBox api with setRestoredFromThreeBoxToFalse and setRestoredFromThreeBoxToTrue

* Update development/metamaskbot-build-announce.js to include ui-libs and bg-libs in hard coded bundle list

* Update test/e2e/threebox.spec.js to use new helpers added with pull #7144

* Make setFeatureFlag available on the ui window during testing

* Hide threebox feature behind a feature flag that can only be activated via dev console

* Remove unnecessary migration of threebox feature flag

* Prevent this.init() call in threebox constructor if feature flag is not turned on

* Prevent threebox notification from showing if feature flag is falsy

* http://localhost/8889 -> http://localhost/* in gulp manifest:testing tasks
2019-09-16 14:41:01 -02:30

303 lines
8.5 KiB
JavaScript

import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import shuffle from 'lodash.shuffle'
import Button from '../../../../components/ui/button'
import {
INITIALIZE_END_OF_FLOW_ROUTE,
INITIALIZE_SEED_PHRASE_ROUTE,
DEFAULT_ROUTE,
} from '../../../../helpers/constants/routes'
import { exportAsFile } from '../../../../helpers/utils/util'
import DraggableSeed from './draggable-seed.component'
const EMPTY_SEEDS = Array(12).fill(null)
export default class ConfirmSeedPhrase extends PureComponent {
static contextTypes = {
metricsEvent: PropTypes.func,
t: PropTypes.func,
}
static defaultProps = {
seedPhrase: '',
}
static propTypes = {
history: PropTypes.object,
onSubmit: PropTypes.func,
seedPhrase: PropTypes.string,
selectedAddress: PropTypes.string,
initializeThreeBox: PropTypes.func,
}
state = {
selectedSeedIndices: [],
shuffledSeedWords: [],
pendingSeedIndices: [],
draggingSeedIndex: -1,
hoveringIndex: -1,
isDragging: false,
}
shouldComponentUpdate (nextProps, nextState) {
const { seedPhrase } = this.props
const {
selectedSeedIndices,
shuffledSeedWords,
pendingSeedIndices,
draggingSeedIndex,
hoveringIndex,
isDragging,
} = this.state
return seedPhrase !== nextProps.seedPhrase ||
draggingSeedIndex !== nextState.draggingSeedIndex ||
isDragging !== nextState.isDragging ||
hoveringIndex !== nextState.hoveringIndex ||
selectedSeedIndices.join(' ') !== nextState.selectedSeedIndices.join(' ') ||
shuffledSeedWords.join(' ') !== nextState.shuffledSeedWords.join(' ') ||
pendingSeedIndices.join(' ') !== nextState.pendingSeedIndices.join(' ')
}
componentDidMount () {
const { seedPhrase = '' } = this.props
const shuffledSeedWords = shuffle(seedPhrase.split(' ')) || []
this.setState({ shuffledSeedWords })
}
setDraggingSeedIndex = draggingSeedIndex => this.setState({ draggingSeedIndex })
setHoveringIndex = hoveringIndex => this.setState({ hoveringIndex })
onDrop = targetIndex => {
const {
selectedSeedIndices,
draggingSeedIndex,
} = this.state
const indices = insert(selectedSeedIndices, draggingSeedIndex, targetIndex, true)
this.setState({
selectedSeedIndices: indices,
pendingSeedIndices: indices,
draggingSeedIndex: -1,
hoveringIndex: -1,
})
}
handleExport = () => {
exportAsFile('MetaMask Secret Backup Phrase', this.props.seedPhrase, 'text/plain')
}
handleSubmit = async () => {
const {
history,
setSeedPhraseBackedUp,
showingSeedPhraseBackupAfterOnboarding,
hideSeedPhraseBackupAfterOnboarding,
initializeThreeBox,
} = this.props
if (!this.isValid()) {
return
}
try {
this.context.metricsEvent({
eventOpts: {
category: 'Onboarding',
action: 'Seed Phrase Setup',
name: 'Verify Complete',
},
})
setSeedPhraseBackedUp(true).then(() => {
if (showingSeedPhraseBackupAfterOnboarding) {
hideSeedPhraseBackupAfterOnboarding()
history.push(DEFAULT_ROUTE)
} else {
initializeThreeBox()
history.push(INITIALIZE_END_OF_FLOW_ROUTE)
}
})
} catch (error) {
console.error(error.message)
}
}
handleSelectSeedWord = (shuffledIndex) => {
this.setState({
selectedSeedIndices: [...this.state.selectedSeedIndices, shuffledIndex],
pendingSeedIndices: [...this.state.pendingSeedIndices, shuffledIndex],
})
}
handleDeselectSeedWord = shuffledIndex => {
this.setState({
selectedSeedIndices: this.state.selectedSeedIndices.filter(i => shuffledIndex !== i),
pendingSeedIndices: this.state.pendingSeedIndices.filter(i => shuffledIndex !== i),
})
}
isValid () {
const { seedPhrase } = this.props
const { selectedSeedIndices, shuffledSeedWords } = this.state
const selectedSeedWords = selectedSeedIndices.map(i => shuffledSeedWords[i])
return seedPhrase === selectedSeedWords.join(' ')
}
render () {
const { t } = this.context
const { history } = this.props
const {
selectedSeedIndices,
shuffledSeedWords,
draggingSeedIndex,
} = this.state
return (
<div className="confirm-seed-phrase">
<div className="confirm-seed-phrase__back-button">
<a
onClick={e => {
e.preventDefault()
history.push(INITIALIZE_SEED_PHRASE_ROUTE)
}}
href="#"
>
{`< Back`}
</a>
</div>
<div className="first-time-flow__header">
{ t('confirmSecretBackupPhrase') }
</div>
<div className="first-time-flow__text-block">
{ t('selectEachPhrase') }
</div>
<div
className={classnames('confirm-seed-phrase__selected-seed-words', {
'confirm-seed-phrase__selected-seed-words--dragging': draggingSeedIndex > -1,
})}
>
{ this.renderPendingSeeds() }
{ this.renderSelectedSeeds() }
</div>
<div className="confirm-seed-phrase__shuffled-seed-words">
{
shuffledSeedWords.map((word, index) => {
const isSelected = selectedSeedIndices.includes(index)
return (
<DraggableSeed
key={index}
seedIndex={index}
index={index}
draggingSeedIndex={this.state.draggingSeedIndex}
setDraggingSeedIndex={this.setDraggingSeedIndex}
setHoveringIndex={this.setHoveringIndex}
onDrop={this.onDrop}
className="confirm-seed-phrase__seed-word--shuffled"
selected={isSelected}
onClick={() => {
if (!isSelected) {
this.handleSelectSeedWord(index)
} else {
this.handleDeselectSeedWord(index)
}
}}
word={word}
/>
)
})
}
</div>
<Button
type="primary"
className="first-time-flow__button"
onClick={this.handleSubmit}
disabled={!this.isValid()}
>
{ t('confirm') }
</Button>
</div>
)
}
renderSelectedSeeds () {
const { shuffledSeedWords, selectedSeedIndices, draggingSeedIndex } = this.state
return EMPTY_SEEDS.map((_, index) => {
const seedIndex = selectedSeedIndices[index]
const word = shuffledSeedWords[seedIndex]
return (
<DraggableSeed
key={`selected-${seedIndex}-${index}`}
className="confirm-seed-phrase__selected-seed-words__selected-seed"
index={index}
seedIndex={seedIndex}
word={word}
draggingSeedIndex={draggingSeedIndex}
setDraggingSeedIndex={this.setDraggingSeedIndex}
setHoveringIndex={this.setHoveringIndex}
onDrop={this.onDrop}
draggable
/>
)
})
}
renderPendingSeeds () {
const {
pendingSeedIndices,
shuffledSeedWords,
draggingSeedIndex,
hoveringIndex,
} = this.state
const indices = insert(pendingSeedIndices, draggingSeedIndex, hoveringIndex)
return EMPTY_SEEDS.map((_, index) => {
const seedIndex = indices[index]
const word = shuffledSeedWords[seedIndex]
return (
<DraggableSeed
key={`pending-${seedIndex}-${index}`}
index={index}
className={classnames('confirm-seed-phrase__selected-seed-words__pending-seed', {
'confirm-seed-phrase__seed-word--hidden': draggingSeedIndex === seedIndex && index !== hoveringIndex,
})}
seedIndex={seedIndex}
word={word}
draggingSeedIndex={draggingSeedIndex}
setDraggingSeedIndex={this.setDraggingSeedIndex}
setHoveringIndex={this.setHoveringIndex}
onDrop={this.onDrop}
droppable={!!word}
/>
)
})
}
}
function insert (list, value, target, removeOld) {
let nextList = [...list]
if (typeof list[target] === 'number') {
nextList = [...list.slice(0, target), value, ...list.slice(target)]
}
if (removeOld) {
nextList = nextList.filter((seed, i) => {
return seed !== value || i === target
})
}
if (nextList.length > 12) {
nextList.pop()
}
return nextList
}