Skip to content

Commit

Permalink
3box integration 2.0 (#6972)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
danjm authored Sep 16, 2019
1 parent bf72f39 commit 7985f4f
Show file tree
Hide file tree
Showing 38 changed files with 5,168 additions and 130 deletions.
19 changes: 19 additions & 0 deletions app/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@
"customRPC": {
"message": "Custom RPC"
},
"dataBackupFoundInfo": {
"message": "Some of your account data was backed up during a previous installation of MetaMask. This could include your settings, contacts and tokens. Would you like to restore this data now?"
},
"decimalsMustZerotoTen": {
"message": "Decimals must be at least 0, and not over 36."
},
Expand Down Expand Up @@ -1148,6 +1151,9 @@
"noTransactions": {
"message": "You have no transactions"
},
"noThanks": {
"message": "No Thanks"
},
"notEnoughGas": {
"message": "Not Enough Gas"
},
Expand Down Expand Up @@ -1337,6 +1343,10 @@
"restoreAccountWithSeed": {
"message": "Restore your Account with Seed Phrase"
},
"restoreWalletPreferences": {
"message": "A backup of your data from $1 has been found. Would you like to restore your wallet preferences?",
"description": "$1 is the date at which the data was backed up"
},
"requestsAwaitingAcknowledgement": {
"message": "requests waiting to be acknowledged"
},
Expand Down Expand Up @@ -1642,6 +1652,15 @@
"symbolBetweenZeroTwelve": {
"message": "Symbol must be between 0 and 12 characters."
},
"syncWithThreeBox": {
"message": "Sync data with 3Box"
},
"syncWithThreeBoxDescription": {
"message": "Turn on to have your settings backed up with 3Box"
},
"syncWithThreeBoxDisabled": {
"message": "3Box has been disabled due to an error during the initial sync"
},
"syncWithMobile": {
"message": "Sync with mobile"
},
Expand Down
2 changes: 1 addition & 1 deletion app/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</head>
<body>
<div id="app-content"></div>
<script src="./libs.js" type="text/javascript" charset="utf-8"></script>
<script src="./ui-libs.js" type="text/javascript" charset="utf-8"></script>
<script src="./ui.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
1 change: 1 addition & 0 deletions app/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"background": {
"scripts": [
"chromereload.js",
"bg-libs.js",
"background.js"
],
"persistent": true
Expand Down
2 changes: 1 addition & 1 deletion app/notification.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<img id="loading__logo" src="./images/logo/metamask-fox.svg" />
<img id="loading__spinner" src="./images/spinner.gif" />
</div>
<script src="./libs.js" type="text/javascript" charset="utf-8"></script>
<script src="./ui-libs.js" type="text/javascript" charset="utf-8"></script>
<script src="./ui.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion app/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</head>
<body style="width:357px; height:600px;">
<div id="app-content"></div>
<script src="./libs.js" type="text/javascript" charset="utf-8"></script>
<script src="./ui-libs.js" type="text/javascript" charset="utf-8"></script>
<script src="./ui.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
1 change: 1 addition & 0 deletions app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class PreferencesController {
// perform sensitive operations.
featureFlags: {
showIncomingTransactions: true,
threeBox: false,
},
knownMethodData: {},
participateInMetaMetrics: null,
Expand Down
213 changes: 213 additions & 0 deletions app/scripts/controllers/threebox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
const ObservableStore = require('obs-store')
const Box = process.env.IN_TEST
? require('../../../development/mock-3box')
: require('3box')
// const Box = require(process.env.IN_TEST ? '../lib/mock-3box' : '3box/dist/3box.min')
const log = require('loglevel')

const JsonRpcEngine = require('json-rpc-engine')
const providerFromEngine = require('eth-json-rpc-middleware/providerFromEngine')
const createMetamaskMiddleware = require('./network/createMetamaskMiddleware')
const createOriginMiddleware = require('../lib/createOriginMiddleware')

const SYNC_TIMEOUT = 60 * 1000 // one minute

class ThreeBoxController {
constructor (opts = {}) {
const {
preferencesController,
keyringController,
addressBookController,
version,
getKeyringControllerState,
getSelectedAddress,
} = opts

this.preferencesController = preferencesController
this.addressBookController = addressBookController
this.keyringController = keyringController
this.provider = this._createProvider({
version,
getAccounts: async ({ origin }) => {
if (origin !== '3Box') { return [] }
const isUnlocked = getKeyringControllerState().isUnlocked

const selectedAddress = getSelectedAddress()

if (isUnlocked && selectedAddress) {
return [selectedAddress]
} else {
return []
}
},
processPersonalMessage: (msgParams) => {
return Promise.resolve(keyringController.signPersonalMessage(msgParams, {
withAppKeyOrigin: 'wallet://3box.metamask.io',
}))
},
})

const initState = {
threeBoxSyncingAllowed: true,
restoredFromThreeBox: null,
...opts.initState,
threeBoxAddress: null,
threeBoxSynced: false,
threeBoxDisabled: false,
}
this.store = new ObservableStore(initState)
this.registeringUpdates = false

const threeBoxFeatureFlagTurnedOn = this.preferencesController.getFeatureFlags().threeBox

if (threeBoxFeatureFlagTurnedOn) {
this.init()
}
}

async init () {
const accounts = await this.keyringController.getAccounts()
this.address = accounts[0]
if (this.address && !(this.box && this.store.getState().threeBoxSynced)) {
await this.new3Box()
}
}

async _update3Box ({ type }, newState) {
try {
const { threeBoxSyncingAllowed, threeBoxSynced } = this.store.getState()
if (threeBoxSyncingAllowed && threeBoxSynced) {
await this.space.private.set('lastUpdated', Date.now())
await this.space.private.set(type, JSON.stringify(newState))
}
} catch (error) {
console.error(error)
}
}

_createProvider (providerOpts) {
const metamaskMiddleware = createMetamaskMiddleware(providerOpts)
const engine = new JsonRpcEngine()
engine.push(createOriginMiddleware({ origin: '3Box' }))
engine.push(metamaskMiddleware)
const provider = providerFromEngine(engine)
return provider
}

_waitForOnSyncDone () {
return new Promise((resolve) => {
this.box.onSyncDone(() => {
log.debug('3Box box sync done')
return resolve()
})
})
}

async new3Box () {
const accounts = await this.keyringController.getAccounts()
const address = accounts[0]

if (this.getThreeBoxSyncingState()) {
this.store.updateState({ threeBoxSynced: false })
this.address = await this.keyringController.getAppKeyAddress(address, 'wallet://3box.metamask.io')

let timedOut = false
const syncTimeout = setTimeout(() => {
log.error(`3Box sync timed out after ${SYNC_TIMEOUT} ms`)
timedOut = true
this.store.updateState({
threeBoxDisabled: true,
threeBoxSyncingAllowed: false,
})
}, SYNC_TIMEOUT)
try {
this.box = await Box.openBox(address, this.provider)
await this._waitForOnSyncDone()
this.space = await this.box.openSpace('metamask', {
onSyncDone: async () => {
const stateUpdate = {
threeBoxSynced: true,
threeBoxAddress: address,
}
if (timedOut) {
log.info(`3Box sync completed after timeout; no longer disabled`)
stateUpdate.threeBoxDisabled = false
}

clearTimeout(syncTimeout)
this.store.updateState(stateUpdate)
log.debug('3Box space sync done')
},
})
} catch (e) {
console.error(e)
throw e
}
}
}

async getLastUpdated () {
return await this.space.private.get('lastUpdated')
}

async restoreFromThreeBox () {
this.setRestoredFromThreeBoxToTrue()
const backedUpPreferences = await this.space.private.get('preferences')
backedUpPreferences && this.preferencesController.store.updateState(JSON.parse(backedUpPreferences))
const backedUpAddressBook = await this.space.private.get('addressBook')
backedUpAddressBook && this.addressBookController.update(JSON.parse(backedUpAddressBook), true)
}

turnThreeBoxSyncingOn () {
this._registerUpdates()
}

turnThreeBoxSyncingOff () {
this.box.logout()
}

setRestoredFromThreeBoxToTrue () {
this.store.updateState({ restoredFromThreeBox: true })
}

setRestoredFromThreeBoxToFalse () {
this.store.updateState({ restoredFromThreeBox: false })
}

setThreeBoxSyncingPermission (newThreeboxSyncingState) {
if (this.store.getState().threeBoxDisabled) {
return
}
this.store.updateState({
threeBoxSyncingAllowed: newThreeboxSyncingState,
})

if (newThreeboxSyncingState && this.box) {
this.turnThreeBoxSyncingOn()
}

if (!newThreeboxSyncingState && this.box) {
this.turnThreeBoxSyncingOff()
}
}

getThreeBoxSyncingState () {
return this.store.getState().threeBoxSyncingAllowed
}

getThreeBoxAddress () {
return this.store.getState().threeBoxAddress
}

_registerUpdates () {
if (!this.registeringUpdates) {
const updatePreferences = this._update3Box.bind(this, { type: 'preferences' })
this.preferencesController.store.subscribe(updatePreferences)
const updateAddressBook = this._update3Box.bind(this, { type: 'addressBook' })
this.addressBookController.subscribe(updateAddressBook)
this.registeringUpdates = true
}
}
}

module.exports = ThreeBoxController
2 changes: 1 addition & 1 deletion app/scripts/lib/personal-message-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ module.exports = class PersonalMessageManager extends EventEmitter {
*/
_setMsgStatus (msgId, status) {
const msg = this.getMsg(msgId)
if (!msg) throw new Error('PersonalMessageManager - Message not found for id: "${msgId}".')
if (!msg) throw new Error(`PersonalMessageManager - Message not found for id: "${msgId}".`)
msg.status = status
this._updateMsg(msg)
this.emit(`${msgId}:${status}`, msg)
Expand Down
Loading

0 comments on commit 7985f4f

Please sign in to comment.