-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy paththreebox.js
260 lines (231 loc) · 7.63 KB
/
threebox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import ObservableStore from 'obs-store'
/* eslint-disable import/first,import/order */
const Box = process.env.IN_TEST
? require('../../../development/mock-3box')
: require('3box')
/* eslint-enable import/order */
import log from 'loglevel'
import JsonRpcEngine from 'json-rpc-engine'
import providerFromEngine from 'eth-json-rpc-middleware/providerFromEngine'
import Migrator from '../lib/migrator'
import migrations from '../migrations'
import createOriginMiddleware from '../lib/createOriginMiddleware'
import createMetamaskMiddleware from './network/createMetamaskMiddleware'
/* eslint-enable import/first */
const SYNC_TIMEOUT = 60 * 1000 // one minute
export default class ThreeBoxController {
constructor(opts = {}) {
const {
preferencesController,
keyringController,
addressBookController,
version,
getKeyringControllerState,
} = 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()
const accounts = await this.keyringController.getAccounts()
if (isUnlocked && accounts[0]) {
const appKeyAddress = await this.keyringController.getAppKeyAddress(
accounts[0],
'wallet://3box.metamask.io',
)
return [appKeyAddress]
}
return []
},
processPersonalMessage: async (msgParams) => {
const accounts = await this.keyringController.getAccounts()
return keyringController.signPersonalMessage(
{ ...msgParams, from: accounts[0] },
{
withAppKeyOrigin: 'wallet://3box.metamask.io',
},
)
},
})
const initState = {
threeBoxSyncingAllowed: false,
showRestorePrompt: true,
threeBoxLastUpdated: 0,
...opts.initState,
threeBoxAddress: null,
threeBoxSynced: false,
threeBoxDisabled: false,
}
this.store = new ObservableStore(initState)
this.registeringUpdates = false
this.lastMigration = migrations
.sort((a, b) => a.version - b.version)
.slice(-1)[0]
if (initState.threeBoxSyncingAllowed) {
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() {
try {
const { threeBoxSyncingAllowed, threeBoxSynced } = this.store.getState()
if (threeBoxSyncingAllowed && threeBoxSynced) {
const newState = {
preferences: this.preferencesController.store.getState(),
addressBook: this.addressBookController.state,
lastUpdated: Date.now(),
lastMigration: this.lastMigration,
}
await this.space.private.set('metamaskBackup', JSON.stringify(newState))
await this.setShowRestorePromptToFalse()
}
} 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()
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) {
if (e.message.match(/^Error: Invalid response \(404\)/u)) {
backupExists = false
} else {
throw e
}
}
if (this.getThreeBoxSyncingState() || backupExists) {
this.store.updateState({ threeBoxSynced: false })
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(this.address, this.provider)
await this._waitForOnSyncDone()
this.space = await this.box.openSpace('metamask', {
onSyncDone: async () => {
const stateUpdate = {
threeBoxSynced: true,
threeBoxAddress: this.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() {
const res = await this.space.private.get('metamaskBackup')
const parsedRes = JSON.parse(res || '{}')
return parsedRes.lastUpdated
}
async migrateBackedUpState(backedUpState) {
const migrator = new Migrator({ migrations })
const { preferences, addressBook } = JSON.parse(backedUpState)
const formattedStateBackup = {
PreferencesController: preferences,
AddressBookController: addressBook,
}
const initialMigrationState = migrator.generateInitialState(
formattedStateBackup,
)
const migratedState = await migrator.migrateData(initialMigrationState)
return {
preferences: migratedState.data.PreferencesController,
addressBook: migratedState.data.AddressBookController,
}
}
async restoreFromThreeBox() {
const backedUpState = await this.space.private.get('metamaskBackup')
const { preferences, addressBook } = await this.migrateBackedUpState(
backedUpState,
)
this.store.updateState({ threeBoxLastUpdated: backedUpState.lastUpdated })
preferences && this.preferencesController.store.updateState(preferences)
addressBook && this.addressBookController.update(addressBook, true)
this.setShowRestorePromptToFalse()
}
turnThreeBoxSyncingOn() {
this._registerUpdates()
}
turnThreeBoxSyncingOff() {
this.box.logout()
}
setShowRestorePromptToFalse() {
this.store.updateState({ showRestorePrompt: 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
}
_registerUpdates() {
if (!this.registeringUpdates) {
const updatePreferences = this._update3Box.bind(this)
this.preferencesController.store.subscribe(updatePreferences)
const updateAddressBook = this._update3Box.bind(this)
this.addressBookController.subscribe(updateAddressBook)
this.registeringUpdates = true
}
}
}