-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathmultichain-account-service-init.ts
More file actions
98 lines (92 loc) · 3.14 KB
/
multichain-account-service-init.ts
File metadata and controls
98 lines (92 loc) · 3.14 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
import {
MultichainAccountService,
SOL_ACCOUNT_PROVIDER_NAME,
TRX_ACCOUNT_PROVIDER_NAME,
BTC_ACCOUNT_PROVIDER_NAME,
} from '@metamask/multichain-account-service';
import { ControllerInitFunction } from '../types';
import {
MultichainAccountServiceMessenger,
MultichainAccountServiceInitMessenger,
} from '../messengers/accounts';
import { previousValueComparator } from '../../lib/util';
import { trace } from '../../../../shared/lib/trace';
/**
* Initialize the multichain account service.
*
* @param request - The request object.
* @param request.controllerMessenger - The messenger to use for the controller.
* @param request.initMessenger - The messenger to use for initialization.
* @param request.ensureOnboardingComplete - Ensure onboarding is complete before initializing.
* @returns The initialized service.
*/
export const MultichainAccountServiceInit: ControllerInitFunction<
MultichainAccountService,
MultichainAccountServiceMessenger,
MultichainAccountServiceInitMessenger
> = ({ controllerMessenger, initMessenger, ensureOnboardingComplete }) => {
const snapAccountProviderConfig = {
// READ THIS CAREFULLY:
// We are using 1 to prevent any concurrent `keyring_createAccount` requests. This ensures
// we prevent any desync between Snap's accounts and Metamask's accounts.
maxConcurrency: 1,
// Re-use the default config for the rest:
discovery: {
timeoutMs: 2000,
maxAttempts: 3,
backOffMs: 1000,
},
createAccounts: {
timeoutMs: 3000,
batched: false,
},
resyncAccounts: {
autoRemoveExtraSnapAccounts: false,
},
};
const controller = new MultichainAccountService({
messenger: controllerMessenger,
providerConfigs: {
[SOL_ACCOUNT_PROVIDER_NAME]: {
...snapAccountProviderConfig,
createAccounts: {
...snapAccountProviderConfig.createAccounts,
batched: true,
},
},
[BTC_ACCOUNT_PROVIDER_NAME]: snapAccountProviderConfig,
[TRX_ACCOUNT_PROVIDER_NAME]: snapAccountProviderConfig,
},
config: {
// @ts-expect-error Controller uses string for names rather than enum
trace,
},
ensureOnboardingComplete,
});
const preferencesState = initMessenger.call('PreferencesController:getState');
initMessenger.subscribe(
'PreferencesController:stateChange',
previousValueComparator((prevState, currState) => {
const { useExternalServices: prevUseExternalServices } = prevState;
const { useExternalServices: currUseExternalServices } = currState;
if (prevUseExternalServices !== currUseExternalServices) {
// Set basic functionality and trigger alignment when enabled
// This single call handles both provider disable/enable and alignment.
controller
.setBasicFunctionality(currUseExternalServices)
.catch((error) => {
console.error(
'Failed to set basic functionality on MultichainAccountService:',
error,
);
});
}
return true;
}, preferencesState),
);
return {
memStateKey: null,
persistedStateKey: null,
controller,
};
};