-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathremixAppManager.ts
More file actions
486 lines (449 loc) · 13 KB
/
Copy pathremixAppManager.ts
File metadata and controls
486 lines (449 loc) · 13 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import { Plugin, PluginManager } from '@remixproject/engine'
import { EventEmitter } from 'events'
import { QueryParams } from '@remix-project/remix-lib'
import { IframePlugin } from '@remixproject/engine-web'
import { Registry } from '@remix-project/remix-lib'
import { RemixNavigator } from './types'
import { Profile } from '@remixproject/plugin-utils'
const _paq = (window._paq = window._paq || [])
// requiredModule removes the plugin from the plugin manager list on UI
let requiredModules = [
// services + layout views + system views
'manager',
'config',
'compilerArtefacts',
'compilerMetadata',
'compilerloader',
'contextualListener',
'editor',
'offsetToLineColumnConverter',
'network',
'theme',
'locale',
'fileManager',
'contentImport',
'blockchain',
'web3Provider',
'scriptRunner',
'scriptRunnerBridge',
'fetchAndCompile',
'mainPanel',
'hiddenPanel',
'sidePanel',
'menuicons',
'filePanel',
'terminal',
'statusBar',
'settings',
'pluginManager',
'tabs',
'udapp',
'dgitApi',
'solidity',
'solidity-logic',
'gistHandler',
'layout',
'notification',
'permissionhandler',
'walkthrough',
'storage',
'restorebackupzip',
'link-libraries',
'deploy-libraries',
'openzeppelin-proxy',
'hardhat-provider',
'ganache-provider',
'foundry-provider',
'basic-http-provider',
'vm-custom-fork',
'vm-goerli-fork',
'vm-mainnet-fork',
'vm-sepolia-fork',
'vm-paris',
'vm-london',
'vm-berlin',
'vm-shanghai',
'compileAndRun',
'search',
'recorder',
'fileDecorator',
'codeParser',
'codeFormatter',
'solidityumlgen',
'compilationDetails',
'vyperCompilationDetails',
'contractflattener',
'solidity-script',
'home',
'doc-viewer',
// 'doc-gen',
'remix-templates',
'remixAID',
'solhint',
'dgit',
'pinnedPanel',
'pluginStateLogger',
'environmentExplorer',
'templateSelection',
'matomo',
'walletconnect',
'popupPanel',
'remixAI',
'remixAID',
'remixaiassistant',
'topbar',
'githubAuthHandler',
'desktopClient'
]
// dependentModules shouldn't be manually activated (e.g hardhat is activated by remixd)
const dependentModules = ['foundry', 'hardhat', 'truffle', 'slither']
const loadLocalPlugins = ['doc-gen', 'doc-viewer', 'contract-verification', 'vyper', 'solhint', 'circuit-compiler', 'learneth', 'quick-dapp', 'noir-compiler']
const partnerPlugins = ['cookbookdev']
const sensitiveCalls = {
fileManager: ['writeFile', 'copyFile', 'rename', 'copyDir'],
contentImport: ['resolveAndSave'],
web3Provider: ['sendAsync'],
}
const isInjectedProvider = (name) => {
return name.startsWith('injected')
}
const isVM = (name) => {
return name.startsWith('vm')
}
const isScriptRunner = (name) => {
return name.startsWith('scriptRunner')
}
export function isNative(name) {
// nativePlugin allows to bypass the permission request
const nativePlugins = [
'vyper',
'workshops',
'debugger',
'remixd',
'menuicons',
'solidity',
'solidity-logic',
'solidityStaticAnalysis',
'solhint',
'solidityUnitTesting',
'layout',
'statusBar',
'topbar',
'notification',
'hardhat-provider',
'ganache-provider',
'foundry-provider',
'basic-http-provider',
'tabs',
'doc-gen',
'doc-viewer',
'circuit-compiler',
'compilationDetails',
'vyperCompilationDetails',
'remixGuide',
'environmentExplorer',
'templateSelection',
'walletconnect',
'contract-verification',
'popupPanel',
'desktopClient',
'LearnEth',
'noir-compiler',
'remixaiassistant'
]
return nativePlugins.includes(name) || requiredModules.includes(name) || isInjectedProvider(name) || isVM(name) || isScriptRunner(name)
}
/**
* Checks if plugin caller 'from' is allowed to activate plugin 'to'
* The caller can have 'canActivate' as an optional property in the plugin profile.
* This is an array containing the 'name' property of the plugin it wants to call.
* canActivate = ['plugin1-to-call','plugin2-to-call',....]
* or the plugin is allowed by default because it is native
*
* @param {any, any}
* @returns {boolean}
*/
export function canActivate(from, to) {
return ['ethdoc'].includes(from.name) || isNative(from.name) || (to && from && from.canActivate && from.canActivate.includes(to.name))
}
class BaseRemixAppManager extends PluginManager {
canDeactivate(from: Profile): Promise<boolean>;
canDeactivate(from: Profile, to: Profile): Promise<boolean>;
canDeactivate(from: Profile, to?: Profile): Promise<boolean> {
return this.canDeactivatePlugin(from, to)
}
}
export class RemixAppManager extends BaseRemixAppManager {
actives = []
pluginsDirectory: string
event: EventEmitter
pluginLoader: PluginLoader
constructor() {
super()
this.event = new EventEmitter()
this.pluginsDirectory = 'https://raw.githubusercontent.com/ethereum/remix-plugins-directory/master/build/metadata.json'
this.pluginLoader = new PluginLoader()
if (Registry.getInstance().get('platform').api.isDesktop()) {
requiredModules = [...requiredModules, 'fs', 'electronTemplates', 'isogit', 'remix-templates', 'electronconfig', 'xterm', 'compilerloader', 'ripgrep', 'slither', 'remixAID', 'circom']
}
}
async canActivatePlugin (from, to) {
return canActivate(from, to)
}
async canDeactivatePlugin (from, to) {
if (this.isRequired(to.name)) return false
return isNative(from.name)
}
async canDeactivate (from: Profile<any>, to?: Profile<any>): Promise<boolean> {
return this.canDeactivatePlugin(from, to)
}
async deactivatePlugin(name) {
const profile = await this.getProfile(name)
const [to, from] = [profile, await this.getProfile(this.requestFrom)]
if (this.canDeactivatePlugin(from, to)) {
if (profile.methods.includes('deactivate')) {
try {
await this.call(name, 'deactivate')
} catch (e) {
console.log(e)
}
}
await this.toggleActive(name)
} else {
console.log('cannot deactivate', name)
}
}
async canCall(from, to, method, message) {
const isSensitiveCall = sensitiveCalls[to] && sensitiveCalls[to].includes(method)
// Make sure the caller of this methods is the target plugin
if (to !== this.currentRequest.from) {
return false
}
// skipping native plugins' requests
if (isNative(from)) {
return true
}
// skipping partner plugins' requests
if (partnerPlugins[from]) {
return true
}
// ask the user for permission
return await this.call('permissionhandler', 'askPermission', this.profiles[from], this.profiles[to], method, message, isSensitiveCall)
}
onPluginActivated(plugin) {
this.pluginLoader.set(
plugin,
this.actives.filter((plugin) => !this.isDependent(plugin))
)
this.event.emit('activate', plugin)
this.emit('activate', plugin)
if (!this.isRequired(plugin.name)) _paq.push(['trackEvent', 'pluginManager', 'activate', plugin.name])
}
getAll() {
return Object.keys(this.profiles).map((p) => {
return this.profiles[p]
})
}
getIds() {
return Object.keys(this.profiles)
}
onPluginDeactivated(plugin) {
this.pluginLoader.set(
plugin,
this.actives.filter((plugin) => !this.isDependent(plugin))
)
this.event.emit('deactivate', plugin)
_paq.push(['trackEvent', 'pluginManager', 'deactivate', plugin.name])
}
isDependent(name: string): boolean {
return dependentModules.includes(name)
}
isRequired(name: string): boolean {
// excluding internal use plugins
return requiredModules.includes(name) || isInjectedProvider(name) || isVM(name) || isScriptRunner(name)
}
async registeredPlugins(): Promise<IframePlugin[]> {
let plugins
try {
const res = await fetch(this.pluginsDirectory)
plugins = await res.json()
plugins = plugins.filter((plugin) => {
if (plugin.name === 'dgit' || plugin.name === 'walletconnect') return false
if (plugin.targets && Array.isArray(plugin.targets) && plugin.targets.length > 0) {
return plugin.targets.includes('remix')
}
return true
})
localStorage.setItem('plugins-directory', JSON.stringify(plugins))
} catch (e) {
console.log('getting plugins list from localstorage...')
const savedPlugins = localStorage.getItem('plugins-directory')
if (savedPlugins) {
try {
plugins = JSON.parse(savedPlugins)
} catch (e) {
console.error(e)
}
}
}
const testPluginName = localStorage.getItem('test-plugin-name')
const testPluginUrl = localStorage.getItem('test-plugin-url')
for (const plugin of loadLocalPlugins) {
// fetch the profile from the local plugin
try {
const profile = await fetch(`plugins/${plugin}/profile.json`)
const profileJson = await profile.json()
// remove duplicates
plugins = plugins.filter((p) => p.name !== profileJson.name && p.displayName !== profileJson.displayName)
// change url
profileJson.url = `plugins/${plugin}/index.html`
// add the local plugin
plugins.push(profileJson)
} catch (e) {
console.log(e)
}
}
return plugins.map(plugin => {
if (plugin.name === 'dgit' && Registry.getInstance().get('platform').api.isDesktop()) { plugin.url = 'https://dgit4-76cc9.web.app/' }
if (plugin.name === testPluginName) plugin.url = testPluginUrl
return new IframePlugin(plugin)
})
}
async registerContextMenuItems() {
await this.call('filePanel', 'registerContextMenuItem', {
id: 'contractflattener',
name: 'flattenAContract',
label: 'Flatten',
type: [],
extension: ['.sol'],
path: [],
pattern: [],
sticky: true,
group: 5,
})
await this.call('filePanel', 'registerContextMenuItem', {
id: 'nahmii-compiler',
name: 'compileCustomAction',
label: 'Compile for Nahmii',
type: [],
extension: ['.sol'],
path: [],
pattern: [],
sticky: true,
group: 6,
})
await this.call('filePanel', 'registerContextMenuItem', {
id: 'solidityumlgen',
name: 'generateCustomAction',
label: 'Generate UML',
type: [],
extension: ['.sol'],
path: [],
pattern: [],
sticky: true,
group: 7,
})
await this.call('filePanel', 'registerContextMenuItem', {
id: 'doc-gen',
name: 'generateDocsCustomAction',
label: 'Generate Docs',
type: [],
extension: ['.sol'],
path: [],
pattern: [],
sticky: true,
group: 7,
})
await this.call('filePanel', 'registerContextMenuItem', {
id: 'vyper',
name: 'vyperCompileCustomAction',
label: 'Compile for Vyper',
type: [],
extension: ['.vy'],
path: [],
pattern: [],
sticky: true,
group: 7,
})
if (Registry.getInstance().get('platform').api.isDesktop()) {
await this.call('filePanel', 'registerContextMenuItem', {
id: 'fs',
name: 'revealInExplorer',
label: (navigator as RemixNavigator).userAgentData.platform.indexOf('mac') > -1 ? 'Reveal in Finder' : 'Reveal in Explorer',
type: ['folder', 'file'],
extension: [],
path: [],
pattern: [],
sticky: true,
group: 8,
})
await this.call('filePanel', 'registerContextMenuItem', {
id: 'fs',
name: 'openInVSCode',
label: 'Open in VSCode',
type: ['folder', 'file'],
extension: [],
path: [],
pattern: [],
sticky: true,
group: 8,
})
}
}
}
/** @class Reference loaders.
* A loader is a get,set based object which load a workspace from a defined sources.
* (localStorage, queryParams)
**/
class PluginLoader {
loaders: any
current: any
donotAutoReload: string[]
get currentLoader() {
return this.loaders[this.current]
}
constructor() {
const queryParams = new QueryParams()
// some plugins should not be activated at page load.
this.donotAutoReload = [
'remixd',
'environmentExplorer',
'templateSelection',
'compilationDetails',
'vyperCompilationDetails',
'walletconnect',
'dapp-draft',
'solidityumlgen',
'remixGuide',
'doc-viewer',
'UIScriptRunner'
]
this.loaders = {}
this.loaders.localStorage = {
set: (plugin, actives) => {
const saved = actives.filter((name) => !this.donotAutoReload.includes(name))
localStorage.setItem('workspace', JSON.stringify(saved))
},
get: () => {
return JSON.parse(localStorage.getItem('workspace'))
},
}
this.loaders.queryParams = {
set: () => {
/* Do nothing. */
},
get: () => {
const getContents = queryParams.get()
if (!(getContents as any).activate) return []
return (getContents as any).activate.split(',')
},
}
this.current = (queryParams.get() as any).activate ? 'queryParams' : 'localStorage'
}
set(plugin, actives) {
this.currentLoader.set(plugin, actives)
}
get() {
return this.currentLoader.get()
}
}