diff --git a/app/scripts/controllers/threebox.js b/app/scripts/controllers/threebox.js index 1677231492ca..e1729f1a5b2f 100644 --- a/app/scripts/controllers/threebox.js +++ b/app/scripts/controllers/threebox.js @@ -48,7 +48,7 @@ class ThreeBoxController { }) const initState = { - threeBoxSyncingAllowed: true, + threeBoxSyncingAllowed: false, restoredFromThreeBox: null, threeBoxLastUpdated: 0, ...opts.initState, @@ -60,7 +60,9 @@ class ThreeBoxController { this.registeringUpdates = false this.lastMigration = migrations.sort((a, b) => a.version - b.version).slice(-1)[0] - this.init() + if (initState.threeBoxSyncingAllowed) { + this.init() + } } async init () { @@ -83,6 +85,7 @@ class ThreeBoxController { } await this.space.private.set('metamaskBackup', JSON.stringify(newState)) + await this.setRestoredFromThreeBoxToFalse() } } catch (error) { console.error(error) @@ -107,13 +110,23 @@ class ThreeBoxController { }) } + async _checkThreeBoxBackupExistance () { + + } + async new3Box () { const accounts = await this.keyringController.getAccounts() - const address = accounts[0] - - if (this.getThreeBoxSyncingState()) { + this.address = await this.keyringController.getAppKeyAddress(accounts[0], 'wallet://3box.metamask.io') + let backupExists + try { + const threeBoxConfig = await Box.getConfig(this.address) + backupExists = threeBoxConfig.spaces && threeBoxConfig.spaces.metamask + } catch (e) { + log.error(e) + backupExists = false + } + if (this.getThreeBoxSyncingState() || backupExists) { this.store.updateState({ threeBoxSynced: false }) - this.address = await this.keyringController.getAppKeyAddress(address, 'wallet://3box.metamask.io') let timedOut = false const syncTimeout = setTimeout(() => { @@ -125,13 +138,13 @@ class ThreeBoxController { }) }, SYNC_TIMEOUT) try { - this.box = await Box.openBox(address, this.provider) + this.box = await Box.openBox(this.address, this.provider) await this._waitForOnSyncDone() this.space = await this.box.openSpace('metamask', { onSyncDone: async () => { const stateUpdate = { threeBoxSynced: true, - threeBoxAddress: address, + threeBoxAddress: this.address, } if (timedOut) { log.info(`3Box sync completed after timeout; no longer disabled`) diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 9a5b7f6bcbf9..f645a69a76ec 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1699,7 +1699,7 @@ module.exports = class MetamaskController extends EventEmitter { } async initializeThreeBox () { - await this.threeBoxController.new3Box() + await this.threeBoxController.init() } /** diff --git a/development/mock-3box.js b/development/mock-3box.js index 781b32f3b49c..53f228f2168b 100644 --- a/development/mock-3box.js +++ b/development/mock-3box.js @@ -55,6 +55,13 @@ class Mock3Box { logout: () => {}, }) } + + static async getConfig (address) { + const backup = await loadFromMock3Box(`${address}-metamask-metamaskBackup`) + return backup + ? { spaces: { metamask: {} } } + : {} + } } module.exports = Mock3Box diff --git a/test/e2e/threebox.spec.js b/test/e2e/threebox.spec.js index 44c4e4d051e8..eb0eebcb2fe3 100644 --- a/test/e2e/threebox.spec.js +++ b/test/e2e/threebox.spec.js @@ -102,7 +102,7 @@ describe('MetaMask', function () { }) }) - describe('updates settings and address book', () => { + describe('turns on threebox syncing', () => { it('goes to the settings screen', async () => { await driver.findElement(By.css('.account-menu__icon')).click() await delay(regularDelayMs) @@ -111,6 +111,23 @@ describe('MetaMask', function () { settingsButton.click() }) + it('turns on threebox syncing', async () => { + const advancedButton = await findElement(driver, By.xpath(`//div[contains(text(), 'Advanced')]`)) + await advancedButton.click() + + const threeBoxToggle = await findElements(driver, By.css('.toggle-button')) + const threeBoxToggleButton = await threeBoxToggle[3].findElement(By.css('div')) + await threeBoxToggleButton.click() + }) + + }) + + describe('updates settings and address book', () => { + it('adds an address to the contact list', async () => { + const generalButton = await findElement(driver, By.xpath(`//div[contains(text(), 'General')]`)) + await generalButton.click() + }) + it('turns on use of blockies', async () => { const toggleButton = await findElement(driver, By.css('.toggle-button > div')) await toggleButton.click() diff --git a/ui/app/pages/settings/advanced-tab/advanced-tab.container.js b/ui/app/pages/settings/advanced-tab/advanced-tab.container.js index ebbee3469d6f..f4f3eaee9825 100644 --- a/ui/app/pages/settings/advanced-tab/advanced-tab.container.js +++ b/ui/app/pages/settings/advanced-tab/advanced-tab.container.js @@ -10,6 +10,7 @@ import { setShowFiatConversionOnTestnetsPreference, setAutoLogoutTimeLimit, setThreeBoxSyncingPermission, + turnThreeBoxSyncingOnAndInitialize, } from '../../../store/actions' import {preferencesSelector} from '../../../selectors/selectors' @@ -49,7 +50,13 @@ export const mapDispatchToProps = dispatch => { setAutoLogoutTimeLimit: value => { return dispatch(setAutoLogoutTimeLimit(value)) }, - setThreeBoxSyncingPermission: newThreeBoxSyncingState => dispatch(setThreeBoxSyncingPermission(newThreeBoxSyncingState)), + setThreeBoxSyncingPermission: newThreeBoxSyncingState => { + if (newThreeBoxSyncingState) { + dispatch(turnThreeBoxSyncingOnAndInitialize()) + } else { + dispatch(setThreeBoxSyncingPermission(newThreeBoxSyncingState)) + } + }, } } diff --git a/ui/app/store/actions.js b/ui/app/store/actions.js index 7983a7d45e36..d14d2f316fad 100644 --- a/ui/app/store/actions.js +++ b/ui/app/store/actions.js @@ -383,6 +383,7 @@ var actions = { setThreeBoxSyncingPermission, setRestoredFromThreeBoxToFalse, turnThreeBoxSyncingOn, + turnThreeBoxSyncingOnAndInitialize, } module.exports = actions @@ -2879,3 +2880,11 @@ function setThreeBoxSyncingPermission (threeBoxSyncingAllowed) { }) } } + +function turnThreeBoxSyncingOnAndInitialize () { + return async (dispatch) => { + await dispatch(setThreeBoxSyncingPermission(true)) + await dispatch(turnThreeBoxSyncingOn()) + await dispatch(initializeThreeBox(true)) + } +}