@exodus/headless 2.0.0-alpha.14 → 2.0.0-alpha.140
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +979 -0
- package/README.md +16 -4
- package/package.json +61 -31
- package/src/api.js +5 -75
- package/src/application.js +59 -28
- package/src/atoms/base-asset-names-to-monitor.js +40 -0
- package/src/constants.js +18 -8
- package/src/dependencies/atoms.js +2 -108
- package/src/dependencies/index.js +2 -9
- package/src/dependencies/modules.js +4 -43
- package/src/dependencies/utils.js +0 -4
- package/src/index.js +89 -80
- package/src/ioc.js +54 -10
- package/src/migrations/attach.js +62 -0
- package/src/plugins/attach.js +28 -7
- package/src/plugins/index.js +3 -1
- package/src/plugins/log-lifecycle.js +1 -0
- package/src/reporting.js +36 -0
- package/src/unlock-encrypted-storage.js +2 -0
- package/src/utils/fusion.js +17 -0
- package/src/utils/promises.js +7 -0
- package/src/atoms/attach.js +0 -40
- package/src/dependencies/monitors.js +0 -33
- package/src/utils/blockchain-metadata.js +0 -48
|
@@ -1,21 +1,11 @@
|
|
|
1
|
-
import EventEmitter from 'events/'
|
|
2
|
-
import createRemoteConfig from '@exodus/config/remote'
|
|
3
|
-
import keychainDefinition from '@exodus/keychain/module'
|
|
4
|
-
import walletDefinition from '@exodus/wallet/module'
|
|
5
|
-
import walletAccountsDefinition from '@exodus/wallet-accounts/module'
|
|
6
|
-
import blockchainMetadataDefinition from '@exodus/blockchain-metadata/module'
|
|
7
|
-
import enabledAssetsModuleDefinition from '@exodus/enabled-assets/module'
|
|
8
|
-
import availableAssetsModuleDefinition from '@exodus/available-assets/module'
|
|
9
1
|
import createKeyIdentifierProvider from '@exodus/key-identifier-provider'
|
|
10
2
|
import walletCompatibilityModesDefinition from '@exodus/wallet-compatibility-modes/module'
|
|
11
|
-
import balancesDefinition from '@exodus/balances/module'
|
|
12
3
|
|
|
13
4
|
import createApplication from '../application'
|
|
14
|
-
import { withType } from './utils'
|
|
15
5
|
import unlockEncryptedStorageDefinition from '../unlock-encrypted-storage'
|
|
16
|
-
import
|
|
6
|
+
import { withType } from './utils'
|
|
17
7
|
|
|
18
|
-
const createModuleDependencies = () =>
|
|
8
|
+
const createModuleDependencies = ({ config }) =>
|
|
19
9
|
[
|
|
20
10
|
{
|
|
21
11
|
definition: {
|
|
@@ -31,41 +21,12 @@ const createModuleDependencies = () =>
|
|
|
31
21
|
dependencies: [],
|
|
32
22
|
},
|
|
33
23
|
},
|
|
34
|
-
{
|
|
35
|
-
definition: keychainDefinition,
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
definition: walletDefinition,
|
|
39
|
-
},
|
|
40
24
|
{
|
|
41
25
|
definition: walletCompatibilityModesDefinition,
|
|
26
|
+
storage: { namespace: 'walletCompatibilityModes' },
|
|
27
|
+
aliases: [{ implementationId: 'unsafeStorage', interfaceId: 'storage' }],
|
|
42
28
|
},
|
|
43
29
|
{ definition: unlockEncryptedStorageDefinition },
|
|
44
|
-
{ definition: walletAccountsDefinition },
|
|
45
|
-
{
|
|
46
|
-
definition: blockchainMetadataDefinition,
|
|
47
|
-
storage: { namespace: ['blockchain', 'v1'] },
|
|
48
|
-
},
|
|
49
|
-
{ definition: enabledAssetsModuleDefinition },
|
|
50
|
-
{
|
|
51
|
-
definition: {
|
|
52
|
-
id: 'remoteConfig',
|
|
53
|
-
factory: (deps) => {
|
|
54
|
-
const eventEmitter = new EventEmitter().setMaxListeners(Number.POSITIVE_INFINITY)
|
|
55
|
-
return createRemoteConfig({ eventEmitter, ...deps })
|
|
56
|
-
},
|
|
57
|
-
dependencies: ['fetch', 'freeze', 'config', 'logger'],
|
|
58
|
-
},
|
|
59
|
-
},
|
|
60
|
-
{ definition: availableAssetsModuleDefinition },
|
|
61
|
-
{
|
|
62
|
-
definition: {
|
|
63
|
-
id: 'pricingClient',
|
|
64
|
-
factory: createExodusPricingClient,
|
|
65
|
-
dependencies: ['fetch', 'pricingServerUrlAtom'],
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
{ definition: balancesDefinition },
|
|
69
30
|
].map(withType('module'))
|
|
70
31
|
|
|
71
32
|
export default createModuleDependencies
|
|
@@ -2,10 +2,6 @@ export const wrapConstant = ({ id, type, value }) => ({
|
|
|
2
2
|
definition: { id, type, factory: () => value },
|
|
3
3
|
})
|
|
4
4
|
|
|
5
|
-
export const insertIf = (module, enabled) => {
|
|
6
|
-
return enabled ? [module] : []
|
|
7
|
-
}
|
|
8
|
-
|
|
9
5
|
export const withType =
|
|
10
6
|
(type) =>
|
|
11
7
|
({ definition, ...rest }) => ({
|
package/src/index.js
CHANGED
|
@@ -1,114 +1,123 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import addressProvider from '@exodus/address-provider'
|
|
2
|
+
import assetsFeature from '@exodus/assets-feature'
|
|
3
|
+
import availableAssets from '@exodus/available-assets'
|
|
4
|
+
import balances from '@exodus/balances'
|
|
5
|
+
import blockchainMetadata from '@exodus/blockchain-metadata'
|
|
6
|
+
import enabledAssets from '@exodus/enabled-assets'
|
|
7
|
+
import featureFlags from '@exodus/feature-flags'
|
|
8
|
+
import fees from '@exodus/fee-data-monitors'
|
|
9
|
+
import geolocation from '@exodus/geolocation'
|
|
10
|
+
import keychain from '@exodus/keychain'
|
|
11
|
+
import locale from '@exodus/locale'
|
|
12
|
+
import pricing from '@exodus/pricing'
|
|
13
|
+
import rates from '@exodus/rates-monitor'
|
|
14
|
+
import remoteConfig from '@exodus/remote-config'
|
|
15
|
+
import restoreProgressTracker from '@exodus/restore-progress-tracker'
|
|
16
|
+
import transactionSigner from '@exodus/tx-signer'
|
|
17
|
+
import wallet from '@exodus/wallet'
|
|
18
|
+
import walletAccounts from '@exodus/wallet-accounts'
|
|
8
19
|
|
|
20
|
+
import createApi from './api'
|
|
21
|
+
import createIOC from './ioc'
|
|
22
|
+
import attachMigrations from './migrations/attach'
|
|
9
23
|
import attachPlugins from './plugins/attach'
|
|
10
|
-
import attachAtoms from './atoms/attach'
|
|
11
|
-
import { atomsToAttach } from './constants'
|
|
12
24
|
|
|
13
25
|
const createExodus = ({ adapters, config, port }) => {
|
|
14
26
|
const ioc = createIOC({ adapters, config })
|
|
15
|
-
|
|
27
|
+
|
|
28
|
+
ioc.use(assetsFeature())
|
|
29
|
+
ioc.use(addressProvider({ config: config.addressProvider }))
|
|
30
|
+
ioc.use(availableAssets())
|
|
31
|
+
ioc.use(balances(config.balances))
|
|
32
|
+
ioc.use(blockchainMetadata())
|
|
33
|
+
ioc.use(enabledAssets())
|
|
34
|
+
ioc.use(featureFlags())
|
|
35
|
+
ioc.use(fees())
|
|
36
|
+
ioc.use(geolocation())
|
|
37
|
+
ioc.use(keychain(config.keychain))
|
|
38
|
+
ioc.use(locale())
|
|
39
|
+
ioc.use(pricing())
|
|
40
|
+
ioc.use(transactionSigner())
|
|
41
|
+
ioc.use(rates())
|
|
42
|
+
ioc.use(remoteConfig())
|
|
43
|
+
ioc.use(restoreProgressTracker())
|
|
44
|
+
ioc.use(wallet())
|
|
45
|
+
ioc.use(walletAccounts())
|
|
46
|
+
|
|
47
|
+
ioc.register({ definition: { id: 'port', type: 'port', factory: () => port } })
|
|
16
48
|
|
|
17
49
|
const resolve = () => {
|
|
18
50
|
ioc.resolve()
|
|
19
51
|
|
|
20
|
-
const {
|
|
52
|
+
const { storage, migrateableStorage } = ioc.getByType('adapter')
|
|
21
53
|
|
|
22
|
-
const {
|
|
23
|
-
application,
|
|
24
|
-
blockchainMetadata,
|
|
25
|
-
enabledAssets,
|
|
26
|
-
remoteConfig,
|
|
27
|
-
unlockEncryptedStorage,
|
|
28
|
-
walletAccounts,
|
|
29
|
-
} = ioc.getByType('module')
|
|
30
|
-
|
|
31
|
-
const { marketHistory, ratesMonitor } = ioc.getByType('monitor')
|
|
32
|
-
|
|
33
|
-
const handleLoadWalletAccounts = createLoadWalletAccountsHandler({
|
|
34
|
-
blockchainMetadata,
|
|
35
|
-
port,
|
|
36
|
-
config,
|
|
37
|
-
})
|
|
54
|
+
const { lockedAtom } = ioc.getByType('atom')
|
|
38
55
|
|
|
39
|
-
const
|
|
40
|
-
blockchainMetadata,
|
|
41
|
-
port,
|
|
42
|
-
config,
|
|
43
|
-
})
|
|
56
|
+
const { application, wallet, unlockEncryptedStorage } = ioc.getByType('module')
|
|
44
57
|
|
|
45
|
-
|
|
46
|
-
blockchainMetadata.on('tx-logs-update', (payload) => port.emit('tx-logs-update', payload))
|
|
47
|
-
blockchainMetadata.on('account-states-update', handleAccountStatesUpdate)
|
|
58
|
+
const { migrations } = ioc.getAll()
|
|
48
59
|
|
|
49
|
-
|
|
60
|
+
application.on('start', (payload) => port.emit('start', payload))
|
|
50
61
|
|
|
51
|
-
|
|
52
|
-
marketHistory.on('market-history', (payload) => port.emit('market-history', payload))
|
|
62
|
+
application.hook('load', (args) => port.emit('pre-load', args))
|
|
53
63
|
|
|
54
|
-
|
|
55
|
-
ratesMonitor.on('rates', (payload) => port.emit('rates', payload))
|
|
64
|
+
application.on('load', (args) => port.emit('load', args))
|
|
56
65
|
|
|
57
|
-
application.
|
|
58
|
-
|
|
66
|
+
application.on('create', async ({ hasPassphraseSet }) => {
|
|
67
|
+
const isLocked = await wallet.isLocked()
|
|
59
68
|
|
|
60
|
-
port.emit('
|
|
69
|
+
port.emit('create', {
|
|
70
|
+
walletExists: true,
|
|
71
|
+
hasPassphraseSet,
|
|
72
|
+
isLocked,
|
|
73
|
+
isBackedUp: false,
|
|
74
|
+
isRestoring: false,
|
|
75
|
+
})
|
|
61
76
|
})
|
|
62
77
|
|
|
63
|
-
application.
|
|
64
|
-
if (typeof storage.unlock === 'function') unlockEncryptedStorage(storage)
|
|
78
|
+
application.on('unlock', () => port.emit('unlock'))
|
|
65
79
|
|
|
66
|
-
|
|
67
|
-
marketHistory.start()
|
|
68
|
-
ratesMonitor.start()
|
|
69
|
-
|
|
70
|
-
await assetsModule.load()
|
|
71
|
-
await walletAccounts.load()
|
|
72
|
-
await Promise.all([
|
|
73
|
-
//
|
|
74
|
-
blockchainMetadata.load(),
|
|
75
|
-
enabledAssets.load(),
|
|
76
|
-
])
|
|
77
|
-
})
|
|
80
|
+
application.on('lock', () => port.emit('lock'))
|
|
78
81
|
|
|
79
|
-
application.on('
|
|
80
|
-
port.emit('unlock')
|
|
81
|
-
})
|
|
82
|
+
application.on('backup', () => port.emit('backup'))
|
|
82
83
|
|
|
83
|
-
application.
|
|
84
|
-
await Promise.all([
|
|
85
|
-
//
|
|
86
|
-
assetsModule.clear(),
|
|
87
|
-
walletAccounts.clear(),
|
|
88
|
-
blockchainMetadata.clear(),
|
|
89
|
-
enabledAssets.clear(),
|
|
90
|
-
])
|
|
84
|
+
application.on('restore', () => port.emit('restore'))
|
|
91
85
|
|
|
92
|
-
|
|
86
|
+
application.on('restore-completed', () => {
|
|
87
|
+
port.emit('restore-completed')
|
|
93
88
|
})
|
|
94
89
|
|
|
95
|
-
application.on('
|
|
96
|
-
|
|
90
|
+
application.on('change-passphrase', () => port.emit('passphrase-changed'))
|
|
91
|
+
|
|
92
|
+
application.on('import', () => port.emit('import', { walletExists: true }))
|
|
93
|
+
|
|
94
|
+
application.hook('unlock', async () => {
|
|
95
|
+
if (typeof storage.unlock === 'function') unlockEncryptedStorage(storage)
|
|
96
|
+
|
|
97
|
+
// normally unlocked during migrations, also unlock here just in case
|
|
98
|
+
if (typeof migrateableStorage.unlock === 'function')
|
|
99
|
+
unlockEncryptedStorage(migrateableStorage)
|
|
97
100
|
})
|
|
98
101
|
|
|
99
|
-
|
|
100
|
-
|
|
102
|
+
application.on('clear', () => port.emit('clear'))
|
|
103
|
+
|
|
104
|
+
application.on('restart', (payload) => port.emit('restart', payload))
|
|
105
|
+
|
|
106
|
+
attachMigrations({
|
|
107
|
+
migrations,
|
|
108
|
+
adapters,
|
|
101
109
|
application,
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
lifecycleEvents: headlessConfig?.attachAtomsLifecycleEvents,
|
|
110
|
+
atoms: ioc.getByType('atom'),
|
|
111
|
+
modules: ioc.getByType('module'),
|
|
105
112
|
})
|
|
106
113
|
|
|
107
|
-
attachPlugins({
|
|
108
|
-
|
|
109
|
-
|
|
114
|
+
attachPlugins({
|
|
115
|
+
application,
|
|
116
|
+
plugins: ioc.getByType('plugin'),
|
|
117
|
+
logger: ioc.get('createLogger')('attachPlugins'),
|
|
118
|
+
})
|
|
110
119
|
|
|
111
|
-
return createApi({ ioc, port })
|
|
120
|
+
return createApi({ ioc, port, config, lockedAtom })
|
|
112
121
|
}
|
|
113
122
|
|
|
114
123
|
return { ...ioc, resolve }
|
package/src/ioc.js
CHANGED
|
@@ -1,30 +1,74 @@
|
|
|
1
1
|
import createIocContainer from '@exodus/dependency-injection'
|
|
2
2
|
import preprocess from '@exodus/dependency-preprocessors'
|
|
3
3
|
import alias from '@exodus/dependency-preprocessors/src/preprocessors/alias'
|
|
4
|
+
import configPreprocessor from '@exodus/dependency-preprocessors/src/preprocessors/config'
|
|
5
|
+
import devModeAtoms from '@exodus/dependency-preprocessors/src/preprocessors/dev-mode-atoms'
|
|
4
6
|
import logify from '@exodus/dependency-preprocessors/src/preprocessors/logify'
|
|
5
|
-
import namespaceConfig from '@exodus/dependency-preprocessors/src/preprocessors/namespace-config'
|
|
6
7
|
import namespaceStorage from '@exodus/dependency-preprocessors/src/preprocessors/namespace-storage'
|
|
8
|
+
import optional from '@exodus/dependency-preprocessors/src/preprocessors/optional'
|
|
9
|
+
import performanceMonitor from '@exodus/dependency-preprocessors/src/preprocessors/performance-monitor'
|
|
10
|
+
import readOnlyAtoms from '@exodus/dependency-preprocessors/src/preprocessors/read-only-atoms'
|
|
11
|
+
import assert from 'minimalistic-assert'
|
|
7
12
|
|
|
8
13
|
import createDependencies from './dependencies'
|
|
9
14
|
|
|
10
15
|
const createIOC = ({ adapters, config }) => {
|
|
11
|
-
const { createLogger } = adapters
|
|
16
|
+
const { createLogger, performance = {} } = adapters
|
|
17
|
+
const {
|
|
18
|
+
readOnlyAtoms: readOnlyAtomsConfig,
|
|
19
|
+
devModeAtoms: devModeAtomsConfig,
|
|
20
|
+
performanceMonitor: performanceMonitorConfig,
|
|
21
|
+
} = config.ioc ?? {}
|
|
22
|
+
|
|
23
|
+
const ioc = createIocContainer({ logger: createLogger('exodus:ioc') })
|
|
24
|
+
|
|
25
|
+
const performanceLogger = createLogger('exodus:performance')
|
|
12
26
|
|
|
13
|
-
const logger = createLogger('exodus:ioc')
|
|
14
|
-
const dependencies = createDependencies({ adapters, config })
|
|
15
27
|
const preprocessors = [
|
|
16
28
|
logify({ createLogger }),
|
|
17
|
-
|
|
29
|
+
performanceMonitorConfig?.enabled &&
|
|
30
|
+
performanceMonitor({
|
|
31
|
+
now: performance.now,
|
|
32
|
+
onAboveThreshold:
|
|
33
|
+
performance.onAboveThreshold ??
|
|
34
|
+
(({ id, method, async, duration }) =>
|
|
35
|
+
performanceLogger.log(
|
|
36
|
+
`${id}.${method} ${async ? 'resolved after' : 'took'} ${duration}ms`
|
|
37
|
+
)),
|
|
38
|
+
config: performanceMonitorConfig,
|
|
39
|
+
}),
|
|
40
|
+
configPreprocessor(),
|
|
18
41
|
alias(),
|
|
19
42
|
// NOTE: order matters, this should come after `alias`
|
|
20
43
|
namespaceStorage(),
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
44
|
+
readOnlyAtoms({
|
|
45
|
+
logger: createLogger('exodus:read-only-atoms'),
|
|
46
|
+
warn: true,
|
|
47
|
+
...readOnlyAtomsConfig,
|
|
48
|
+
}),
|
|
49
|
+
optional(),
|
|
50
|
+
devModeAtomsConfig && devModeAtoms(devModeAtomsConfig),
|
|
51
|
+
].filter(Boolean)
|
|
52
|
+
|
|
53
|
+
const registerMultiple = (dependencies) => {
|
|
54
|
+
ioc.registerMultiple(preprocess({ dependencies, preprocessors }))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const register = (dependency) => {
|
|
58
|
+
registerMultiple([dependency])
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const use = (module) => {
|
|
62
|
+
for (const { definition } of module.definitions) {
|
|
63
|
+
assert(definition.type, `ioc.use: "${definition.id}" is missing type field`)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
registerMultiple(module.definitions)
|
|
67
|
+
}
|
|
24
68
|
|
|
25
|
-
|
|
69
|
+
registerMultiple(createDependencies({ adapters, config }))
|
|
26
70
|
|
|
27
|
-
return ioc
|
|
71
|
+
return { ...ioc, register, registerMultiple, use }
|
|
28
72
|
}
|
|
29
73
|
|
|
30
74
|
export default createIOC
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { deriveSyncKeys } from '../utils/fusion'
|
|
2
|
+
|
|
3
|
+
const attachMigrations = ({ migrations = [], application, modules, adapters, ...deps }) => {
|
|
4
|
+
const { unsafeStorage, migrateableStorage } = adapters
|
|
5
|
+
const { analytics, unlockEncryptedStorage, wallet, fusionKeysDeferred, migrateableFusion } =
|
|
6
|
+
modules
|
|
7
|
+
|
|
8
|
+
// Override encrypted storage with migrations own instance to make sure no modules reads from it before migrations ran
|
|
9
|
+
const migrationFlagsStorage = unsafeStorage.namespace('migrations')
|
|
10
|
+
const migrationAdapters = { ...adapters, storage: migrateableStorage }
|
|
11
|
+
const migrationModules = { ...modules, fusion: migrateableFusion }
|
|
12
|
+
|
|
13
|
+
const attachMigration = async (migration) => {
|
|
14
|
+
const { name, factory } = migration
|
|
15
|
+
const logger = adapters.createLogger(`exodus:migration:${name}`)
|
|
16
|
+
|
|
17
|
+
logger.log('running migration')
|
|
18
|
+
|
|
19
|
+
let success = false
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
const start = performance.now()
|
|
23
|
+
|
|
24
|
+
await factory({ ...deps, adapters: migrationAdapters, modules: migrationModules, logger })
|
|
25
|
+
|
|
26
|
+
const time = performance.now() - start
|
|
27
|
+
|
|
28
|
+
logger.log(`migration successful in ${time.toFixed(2)}ms`)
|
|
29
|
+
|
|
30
|
+
await migrationFlagsStorage.set(name, true)
|
|
31
|
+
|
|
32
|
+
success = true
|
|
33
|
+
} catch (error) {
|
|
34
|
+
logger.log(`migration failed: ${error.stack}`)
|
|
35
|
+
} finally {
|
|
36
|
+
analytics.track({
|
|
37
|
+
event: 'ClientMigrationRun',
|
|
38
|
+
properties: { migrationId: name, success },
|
|
39
|
+
force: true,
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
application.hook('migrate', async () => {
|
|
45
|
+
await unlockEncryptedStorage(migrateableStorage)
|
|
46
|
+
if (fusionKeysDeferred) await deriveSyncKeys(wallet.seed).then(fusionKeysDeferred.resolve)
|
|
47
|
+
|
|
48
|
+
const migrationNames = migrations.map((migration) => migration.name)
|
|
49
|
+
const migrationFlags = await migrationFlagsStorage.batchGet(migrationNames)
|
|
50
|
+
const migrationsDiff = migrations.filter((v, k) => !migrationFlags[k])
|
|
51
|
+
|
|
52
|
+
for (const migration of migrationsDiff) {
|
|
53
|
+
await attachMigration(migration)
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
application.hook('clear', async () => {
|
|
58
|
+
await migrationFlagsStorage.clear()
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default attachMigrations
|
package/src/plugins/attach.js
CHANGED
|
@@ -1,17 +1,38 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LifecycleHook } from '../constants'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const LIFECYCLE_METHOD_TO_HOOK_NAME = Object.fromEntries(
|
|
4
|
+
Object.entries(LifecycleHook).map(([pascalCaseName, hookName]) => [
|
|
5
|
+
`on${pascalCaseName}`,
|
|
6
|
+
hookName,
|
|
7
|
+
])
|
|
6
8
|
)
|
|
7
9
|
|
|
8
|
-
const attachPlugins = ({ plugins, application }) => {
|
|
10
|
+
const attachPlugins = ({ plugins, application, logger }) => {
|
|
11
|
+
const timeFn = (fn, name) => {
|
|
12
|
+
const pluginWrapper = async (...args) => {
|
|
13
|
+
const start = Date.now()
|
|
14
|
+
try {
|
|
15
|
+
return await fn(...args)
|
|
16
|
+
} finally {
|
|
17
|
+
logger.debug(name, `${Date.now() - start}ms`)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
Object.defineProperty(pluginWrapper, 'name', { value: name, writable: false })
|
|
22
|
+
|
|
23
|
+
return pluginWrapper
|
|
24
|
+
}
|
|
25
|
+
|
|
9
26
|
Object.entries(plugins).forEach(([name, lifecycleMethods]) => {
|
|
10
27
|
const entries = Object.entries(lifecycleMethods || {})
|
|
11
28
|
|
|
12
29
|
for (const [lifecycleMethod, fn] of entries) {
|
|
13
|
-
const hookName =
|
|
14
|
-
|
|
30
|
+
const hookName = LIFECYCLE_METHOD_TO_HOOK_NAME[lifecycleMethod]
|
|
31
|
+
if (hookName) {
|
|
32
|
+
application.hook(hookName, timeFn(fn, `plugin ${name}.${lifecycleMethod}`))
|
|
33
|
+
} else {
|
|
34
|
+
logger.error(`plugin "${name}" declares unsupported lifecycle method "${lifecycleMethod}"`)
|
|
35
|
+
}
|
|
15
36
|
}
|
|
16
37
|
})
|
|
17
38
|
}
|
package/src/plugins/index.js
CHANGED
|
@@ -9,6 +9,7 @@ const logLifecycle = {
|
|
|
9
9
|
onImport: () => logger.debug('onImport'),
|
|
10
10
|
onMigrate: () => logger.debug('onMigrate'),
|
|
11
11
|
onStart: () => logger.debug('onStart'),
|
|
12
|
+
onRestart: () => logger.debug('onRestart'),
|
|
12
13
|
onLoad: () => logger.debug('onLoad'),
|
|
13
14
|
onUnload: () => logger.debug('onUnload'),
|
|
14
15
|
onCreate: () => logger.debug('onCreate'),
|
package/src/reporting.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { zipObject } from 'lodash'
|
|
2
|
+
|
|
3
|
+
import { rejectAfter } from './utils/promises'
|
|
4
|
+
|
|
5
|
+
const createReporting = ({ ioc, lockedAtom, config: { exportTimeout = 2000 } }) => {
|
|
6
|
+
const getReports = async () => {
|
|
7
|
+
if (await lockedAtom.get()) throw new Error('Unable to export when locked')
|
|
8
|
+
|
|
9
|
+
const nodes = ioc.getByType('report')
|
|
10
|
+
const reports = Object.values(nodes)
|
|
11
|
+
|
|
12
|
+
const timeoutPromise = rejectAfter(
|
|
13
|
+
exportTimeout,
|
|
14
|
+
`Export took longer than the maximum export timeout of ${Math.ceil(exportTimeout / 1000)}s`
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
const resolvedReports = await Promise.allSettled(
|
|
18
|
+
reports.map((report) => Promise.race([report.export(), timeoutPromise]))
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
const namespaces = reports.map((report) => report.namespace)
|
|
22
|
+
const data = resolvedReports.map((outcome) =>
|
|
23
|
+
outcome.status === 'fulfilled' ? outcome.value : { error: outcome.reason }
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
return zipObject(namespaces, data)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
reporting: {
|
|
31
|
+
export: getReports,
|
|
32
|
+
},
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default createReporting
|
|
@@ -12,8 +12,10 @@ const createUnlockEncryptedStorage = ({ keychain }) => {
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
// eslint-disable-next-line @exodus/export-default/named
|
|
15
16
|
export default {
|
|
16
17
|
id: 'unlockEncryptedStorage',
|
|
18
|
+
type: 'module',
|
|
17
19
|
factory: createUnlockEncryptedStorage,
|
|
18
20
|
dependencies: ['keychain'],
|
|
19
21
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { pick } from '@exodus/basic-utils'
|
|
2
|
+
import HDKeySlip10 from '@exodus/hd-key-slip-10'
|
|
3
|
+
import sodium from '@exodus/sodium-crypto'
|
|
4
|
+
|
|
5
|
+
const EXODUS_PURPOSE = Number.parseInt(Buffer.from('exo').toString('hex'), '16')
|
|
6
|
+
|
|
7
|
+
const EXO_2_PATH = `m/${EXODUS_PURPOSE}'/2'`
|
|
8
|
+
|
|
9
|
+
const BIP43_PURPOSE_SYNC = `${EXO_2_PATH}/0'`
|
|
10
|
+
|
|
11
|
+
export const deriveSyncKeys = async (seed) => {
|
|
12
|
+
const derived = HDKeySlip10.fromSeed(seed).derive(BIP43_PURPOSE_SYNC).key
|
|
13
|
+
|
|
14
|
+
const keys = await sodium.getSodiumKeysFromSeed(derived)
|
|
15
|
+
|
|
16
|
+
return pick(keys, ['box', 'sign', 'secret', 'derived'])
|
|
17
|
+
}
|
package/src/atoms/attach.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
const emitAtomValue = async (opts) => {
|
|
2
|
-
const { port, atomId, atom } = opts
|
|
3
|
-
const value = 'value' in opts ? opts.value : await atom.get()
|
|
4
|
-
port.emit(atomId, value)
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export const emitFromAtoms = ({ atoms, port }) =>
|
|
8
|
-
Object.entries(atoms).forEach(async ([atomId, atom]) => emitAtomValue({ port, atomId, atom }))
|
|
9
|
-
|
|
10
|
-
const attachAtom = ({ port, application, logger, atom, atomId, lifecycleEvents }) => {
|
|
11
|
-
let loaded = false
|
|
12
|
-
|
|
13
|
-
lifecycleEvents.forEach((event) =>
|
|
14
|
-
application.on(event, async () => {
|
|
15
|
-
loaded = true
|
|
16
|
-
await emitAtomValue({ port, atomId, atom })
|
|
17
|
-
})
|
|
18
|
-
)
|
|
19
|
-
|
|
20
|
-
application.on('clear', async () => {
|
|
21
|
-
try {
|
|
22
|
-
await atom?.set(undefined)
|
|
23
|
-
} catch (error) {
|
|
24
|
-
logger.debug('failed to clear atom', error)
|
|
25
|
-
// noop. atom might not support set
|
|
26
|
-
}
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
atom.observe((value) => {
|
|
30
|
-
if (loaded) emitAtomValue({ port, atomId, atom, value })
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const attachAtoms = ({ port, application, logger, atoms, lifecycleEvents = ['start'] }) => {
|
|
35
|
-
for (const [atomId, atom] of Object.entries(atoms)) {
|
|
36
|
-
attachAtom({ port, application, logger, atom, atomId, lifecycleEvents })
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export default attachAtoms
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import marketHistoryMonitorDefinition from '@exodus/market-history/module'
|
|
2
|
-
import ratesMonitorDefinition from '@exodus/rates-monitor/module'
|
|
3
|
-
|
|
4
|
-
import { withType } from './utils'
|
|
5
|
-
|
|
6
|
-
const createModuleDependencies = () =>
|
|
7
|
-
[
|
|
8
|
-
{
|
|
9
|
-
definition: marketHistoryMonitorDefinition,
|
|
10
|
-
storage: { namespace: 'marketHistory' },
|
|
11
|
-
aliases: [
|
|
12
|
-
{
|
|
13
|
-
implementationId: 'unsafeStorage',
|
|
14
|
-
interfaceId: 'storage',
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
implementationId: 'marketHistoryClearCacheAtom',
|
|
18
|
-
interfaceId: 'clearCacheAtom',
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
implementationId: 'remoteConfigClearMarketHistoryCacheAtom',
|
|
22
|
-
interfaceId: 'remoteConfigClearCacheAtom',
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
implementationId: 'marketHistoryRefreshIntervalAtom',
|
|
26
|
-
interfaceId: 'remoteConfigRefreshIntervalAtom',
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
},
|
|
30
|
-
{ definition: ratesMonitorDefinition },
|
|
31
|
-
].map(withType('monitor'))
|
|
32
|
-
|
|
33
|
-
export default createModuleDependencies
|