2021-02-04 19:15:23 +01:00
|
|
|
import { ObservableStore } from '@metamask/obs-store';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2020-08-19 18:27:05 +02:00
|
|
|
/* eslint-disable import/first,import/order */
|
2019-09-16 19:11:01 +02:00
|
|
|
const Box = process.env.IN_TEST
|
|
|
|
? require('../../../development/mock-3box')
|
2021-02-04 19:15:23 +01:00
|
|
|
: require('3box');
|
2020-08-18 21:18:25 +02:00
|
|
|
/* eslint-enable import/order */
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import log from 'loglevel';
|
|
|
|
import { JsonRpcEngine } from 'json-rpc-engine';
|
2021-11-11 21:26:49 +01:00
|
|
|
import { providerFromEngine } from 'eth-json-rpc-middleware';
|
2021-02-04 19:15:23 +01:00
|
|
|
import Migrator from '../lib/migrator';
|
|
|
|
import migrations from '../migrations';
|
|
|
|
import createOriginMiddleware from '../lib/createOriginMiddleware';
|
|
|
|
import createMetamaskMiddleware from './network/createMetamaskMiddleware';
|
2020-08-19 18:27:05 +02:00
|
|
|
/* eslint-enable import/first */
|
2019-09-16 19:11:01 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const SYNC_TIMEOUT = 60 * 1000; // one minute
|
2019-09-16 19:11:01 +02:00
|
|
|
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class ThreeBoxController {
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(opts = {}) {
|
2019-09-16 19:11:01 +02:00
|
|
|
const {
|
|
|
|
preferencesController,
|
|
|
|
keyringController,
|
|
|
|
addressBookController,
|
|
|
|
version,
|
|
|
|
getKeyringControllerState,
|
2021-07-02 21:30:40 +02:00
|
|
|
trackMetaMetricsEvent,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = opts;
|
2019-09-16 19:11:01 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.preferencesController = preferencesController;
|
|
|
|
this.addressBookController = addressBookController;
|
|
|
|
this.keyringController = keyringController;
|
2019-09-16 19:11:01 +02:00
|
|
|
this.provider = this._createProvider({
|
|
|
|
version,
|
|
|
|
getAccounts: async ({ origin }) => {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (origin !== '3Box') {
|
2021-02-04 19:15:23 +01:00
|
|
|
return [];
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const { isUnlocked } = getKeyringControllerState();
|
2019-09-16 19:11:01 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const accounts = await this.keyringController.getAccounts();
|
2019-09-16 19:11:01 +02:00
|
|
|
|
2019-10-01 15:03:13 +02:00
|
|
|
if (isUnlocked && accounts[0]) {
|
2020-11-03 00:41:28 +01:00
|
|
|
const appKeyAddress = await this.keyringController.getAppKeyAddress(
|
|
|
|
accounts[0],
|
|
|
|
'wallet://3box.metamask.io',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return [appKeyAddress];
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return [];
|
2019-09-16 19:11:01 +02:00
|
|
|
},
|
2019-10-01 15:03:13 +02:00
|
|
|
processPersonalMessage: async (msgParams) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const accounts = await this.keyringController.getAccounts();
|
2020-11-03 00:41:28 +01:00
|
|
|
return keyringController.signPersonalMessage(
|
|
|
|
{ ...msgParams, from: accounts[0] },
|
|
|
|
{
|
|
|
|
withAppKeyOrigin: 'wallet://3box.metamask.io',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-09-16 19:11:01 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-07-02 21:30:40 +02:00
|
|
|
this._trackMetaMetricsEvent = trackMetaMetricsEvent;
|
2019-09-16 19:11:01 +02:00
|
|
|
|
|
|
|
const initState = {
|
2019-09-26 09:24:52 +02:00
|
|
|
threeBoxSyncingAllowed: false,
|
|
|
|
showRestorePrompt: true,
|
|
|
|
threeBoxLastUpdated: 0,
|
2019-09-16 19:11:01 +02:00
|
|
|
...opts.initState,
|
|
|
|
threeBoxAddress: null,
|
|
|
|
threeBoxSynced: false,
|
|
|
|
threeBoxDisabled: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
this.store = new ObservableStore(initState);
|
|
|
|
this.registeringUpdates = false;
|
2020-11-03 00:41:28 +01:00
|
|
|
this.lastMigration = migrations
|
|
|
|
.sort((a, b) => a.version - b.version)
|
2021-02-04 19:15:23 +01:00
|
|
|
.slice(-1)[0];
|
2019-09-16 19:11:01 +02:00
|
|
|
|
2019-09-26 09:24:52 +02:00
|
|
|
if (initState.threeBoxSyncingAllowed) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.init();
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async init() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const accounts = await this.keyringController.getAccounts();
|
|
|
|
this.address = accounts[0];
|
2021-07-02 21:30:40 +02:00
|
|
|
|
|
|
|
this._trackMetaMetricsEvent({
|
|
|
|
event: '3Box Initiated',
|
|
|
|
category: '3Box',
|
|
|
|
});
|
|
|
|
|
2019-09-16 19:11:01 +02:00
|
|
|
if (this.address && !(this.box && this.store.getState().threeBoxSynced)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await this.new3Box();
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async _update3Box() {
|
2019-09-16 19:11:01 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { threeBoxSyncingAllowed, threeBoxSynced } = this.store.getState();
|
2019-09-16 19:11:01 +02:00
|
|
|
if (threeBoxSyncingAllowed && threeBoxSynced) {
|
2019-09-26 09:24:52 +02:00
|
|
|
const newState = {
|
|
|
|
preferences: this.preferencesController.store.getState(),
|
|
|
|
addressBook: this.addressBookController.state,
|
|
|
|
lastUpdated: Date.now(),
|
|
|
|
lastMigration: this.lastMigration,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-09-26 09:24:52 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
await this.space.private.set(
|
|
|
|
'metamaskBackup',
|
|
|
|
JSON.stringify(newState),
|
|
|
|
);
|
|
|
|
await this.setShowRestorePromptToFalse();
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error(error);
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_createProvider(providerOpts) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const metamaskMiddleware = createMetamaskMiddleware(providerOpts);
|
|
|
|
const engine = new JsonRpcEngine();
|
|
|
|
engine.push(createOriginMiddleware({ origin: '3Box' }));
|
|
|
|
engine.push(metamaskMiddleware);
|
|
|
|
const provider = providerFromEngine(engine);
|
|
|
|
return provider;
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_waitForOnSyncDone() {
|
2019-09-16 19:11:01 +02:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
this.box.onSyncDone(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.debug('3Box box sync done');
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
});
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async new3Box() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const accounts = await this.keyringController.getAccounts();
|
2020-11-03 00:41:28 +01:00
|
|
|
this.address = await this.keyringController.getAppKeyAddress(
|
|
|
|
accounts[0],
|
|
|
|
'wallet://3box.metamask.io',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
let backupExists;
|
2019-09-26 09:24:52 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
const threeBoxConfig = await Box.getConfig(this.address);
|
|
|
|
backupExists = threeBoxConfig.spaces && threeBoxConfig.spaces.metamask;
|
2019-09-26 09:24:52 +02:00
|
|
|
} catch (e) {
|
2020-08-14 13:48:42 +02:00
|
|
|
if (e.message.match(/^Error: Invalid response \(404\)/u)) {
|
2021-07-02 21:30:40 +02:00
|
|
|
this._trackMetaMetricsEvent({
|
|
|
|
event: '3Box Backup does not exist',
|
|
|
|
category: '3Box',
|
|
|
|
});
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
backupExists = false;
|
2019-09-26 09:24:52 +02:00
|
|
|
} else {
|
2021-07-02 21:30:40 +02:00
|
|
|
this._trackMetaMetricsEvent({
|
|
|
|
event: '3Box Config Error',
|
|
|
|
category: '3Box',
|
|
|
|
});
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
throw e;
|
2019-09-26 09:24:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.getThreeBoxSyncingState() || backupExists) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ threeBoxSynced: false });
|
2019-09-16 19:11:01 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let timedOut = false;
|
2019-09-16 19:11:01 +02:00
|
|
|
const syncTimeout = setTimeout(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error(`3Box sync timed out after ${SYNC_TIMEOUT} ms`);
|
|
|
|
timedOut = true;
|
2019-09-16 19:11:01 +02:00
|
|
|
this.store.updateState({
|
|
|
|
threeBoxDisabled: true,
|
|
|
|
threeBoxSyncingAllowed: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
}, SYNC_TIMEOUT);
|
2019-09-16 19:11:01 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.box = await Box.openBox(this.address, this.provider);
|
|
|
|
await this._waitForOnSyncDone();
|
2019-09-16 19:11:01 +02:00
|
|
|
this.space = await this.box.openSpace('metamask', {
|
|
|
|
onSyncDone: async () => {
|
|
|
|
const stateUpdate = {
|
|
|
|
threeBoxSynced: true,
|
2019-09-26 09:24:52 +02:00
|
|
|
threeBoxAddress: this.address,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-09-16 19:11:01 +02:00
|
|
|
if (timedOut) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.info(`3Box sync completed after timeout; no longer disabled`);
|
|
|
|
stateUpdate.threeBoxDisabled = false;
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
clearTimeout(syncTimeout);
|
|
|
|
this.store.updateState(stateUpdate);
|
2019-09-26 09:24:52 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
log.debug('3Box space sync done');
|
2021-07-02 21:30:40 +02:00
|
|
|
|
|
|
|
this._trackMetaMetricsEvent({
|
|
|
|
event: '3Box Synced',
|
|
|
|
category: '3Box',
|
|
|
|
});
|
2019-09-16 19:11:01 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-09-16 19:11:01 +02:00
|
|
|
} catch (e) {
|
2021-07-02 21:30:40 +02:00
|
|
|
this._trackMetaMetricsEvent({
|
|
|
|
event: '3Box Initiation Error',
|
|
|
|
category: '3Box',
|
|
|
|
});
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error(e);
|
|
|
|
throw e;
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async getLastUpdated() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const res = await this.space.private.get('metamaskBackup');
|
|
|
|
const parsedRes = JSON.parse(res || '{}');
|
|
|
|
return parsedRes.lastUpdated;
|
2019-09-26 09:24:52 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async migrateBackedUpState(backedUpState) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const migrator = new Migrator({ migrations });
|
|
|
|
const { preferences, addressBook } = JSON.parse(backedUpState);
|
2019-09-26 09:24:52 +02:00
|
|
|
const formattedStateBackup = {
|
2020-09-29 18:47:55 +02:00
|
|
|
PreferencesController: preferences,
|
|
|
|
AddressBookController: addressBook,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-11-03 00:41:28 +01:00
|
|
|
const initialMigrationState = migrator.generateInitialState(
|
|
|
|
formattedStateBackup,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const migratedState = await migrator.migrateData(initialMigrationState);
|
2019-09-26 09:24:52 +02:00
|
|
|
return {
|
2020-09-29 18:47:55 +02:00
|
|
|
preferences: migratedState.data.PreferencesController,
|
|
|
|
addressBook: migratedState.data.AddressBookController,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async restoreFromThreeBox() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const backedUpState = await this.space.private.get('metamaskBackup');
|
2020-11-03 00:41:28 +01:00
|
|
|
const { preferences, addressBook } = await this.migrateBackedUpState(
|
|
|
|
backedUpState,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
this.store.updateState({ threeBoxLastUpdated: backedUpState.lastUpdated });
|
|
|
|
preferences && this.preferencesController.store.updateState(preferences);
|
|
|
|
addressBook && this.addressBookController.update(addressBook, true);
|
|
|
|
this.setShowRestorePromptToFalse();
|
2021-07-02 21:30:40 +02:00
|
|
|
|
|
|
|
this._trackMetaMetricsEvent({
|
|
|
|
event: '3Box Restored Data',
|
|
|
|
category: '3Box',
|
|
|
|
});
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
turnThreeBoxSyncingOn() {
|
2021-07-02 21:30:40 +02:00
|
|
|
this._trackMetaMetricsEvent({
|
|
|
|
event: '3Box Sync Turned On',
|
|
|
|
category: '3Box',
|
|
|
|
});
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this._registerUpdates();
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
turnThreeBoxSyncingOff() {
|
2021-07-02 21:30:40 +02:00
|
|
|
this._trackMetaMetricsEvent({
|
|
|
|
event: '3Box Sync Turned Off',
|
|
|
|
category: '3Box',
|
|
|
|
});
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.box.logout();
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
setShowRestorePromptToFalse() {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.store.updateState({ showRestorePrompt: false });
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
setThreeBoxSyncingPermission(newThreeboxSyncingState) {
|
2019-09-16 19:11:01 +02:00
|
|
|
if (this.store.getState().threeBoxDisabled) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
this.store.updateState({
|
|
|
|
threeBoxSyncingAllowed: newThreeboxSyncingState,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-09-16 19:11:01 +02:00
|
|
|
|
|
|
|
if (newThreeboxSyncingState && this.box) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.turnThreeBoxSyncingOn();
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!newThreeboxSyncingState && this.box) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.turnThreeBoxSyncingOff();
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
getThreeBoxSyncingState() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return this.store.getState().threeBoxSyncingAllowed;
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_registerUpdates() {
|
2019-09-16 19:11:01 +02:00
|
|
|
if (!this.registeringUpdates) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const updatePreferences = this._update3Box.bind(this);
|
|
|
|
this.preferencesController.store.subscribe(updatePreferences);
|
|
|
|
const updateAddressBook = this._update3Box.bind(this);
|
|
|
|
this.addressBookController.subscribe(updateAddressBook);
|
|
|
|
this.registeringUpdates = true;
|
2019-09-16 19:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|