@metamask-previews/preferences-controller 5.0.1-preview.353ee83b → 5.0.1-preview.fa529884
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.
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { BaseControllerV1 } from '@metamask/base-controller';
|
|
1
|
+
import { BaseController, type ControllerStateChangeEvent, type ControllerGetStateAction, type RestrictedControllerMessenger } from '@metamask/base-controller';
|
|
3
2
|
import { ETHERSCAN_SUPPORTED_CHAIN_IDS } from './constants';
|
|
4
3
|
/**
|
|
5
4
|
* A representation of a MetaMask identity
|
|
@@ -68,12 +67,6 @@ export declare type PreferencesState = {
|
|
|
68
67
|
lostIdentities: {
|
|
69
68
|
[address: string]: Identity;
|
|
70
69
|
};
|
|
71
|
-
/**
|
|
72
|
-
* The name of the controller
|
|
73
|
-
*
|
|
74
|
-
* @deprecated This property is never set, and will be removed in a future release
|
|
75
|
-
*/
|
|
76
|
-
name?: string;
|
|
77
70
|
/**
|
|
78
71
|
* Controls whether the OpenSea API is used
|
|
79
72
|
*/
|
|
@@ -105,6 +98,12 @@ export declare type PreferencesState = {
|
|
|
105
98
|
*/
|
|
106
99
|
useTokenDetection: boolean;
|
|
107
100
|
};
|
|
101
|
+
declare const name = "PreferencesController";
|
|
102
|
+
export declare type PreferencesControllerGetStateAction = ControllerGetStateAction<typeof name, PreferencesState>;
|
|
103
|
+
export declare type PreferencesControllerStateChangeEvent = ControllerStateChangeEvent<typeof name, PreferencesState>;
|
|
104
|
+
export declare type PreferencesControllerActions = PreferencesControllerGetStateAction;
|
|
105
|
+
export declare type PreferencesControllerEvents = PreferencesControllerStateChangeEvent;
|
|
106
|
+
export declare type PreferencesControllerMessenger = RestrictedControllerMessenger<typeof name, PreferencesControllerActions, PreferencesControllerEvents, never, never>;
|
|
108
107
|
/**
|
|
109
108
|
* Get the default PreferencesController state.
|
|
110
109
|
*
|
|
@@ -151,18 +150,18 @@ export declare function getDefaultPreferencesState(): {
|
|
|
151
150
|
/**
|
|
152
151
|
* Controller that stores shared settings and exposes convenience methods
|
|
153
152
|
*/
|
|
154
|
-
export declare class PreferencesController extends
|
|
155
|
-
/**
|
|
156
|
-
* Name of this controller used during composition
|
|
157
|
-
*/
|
|
158
|
-
name: string;
|
|
153
|
+
export declare class PreferencesController extends BaseController<typeof name, PreferencesState, PreferencesControllerMessenger> {
|
|
159
154
|
/**
|
|
160
155
|
* Creates a PreferencesController instance.
|
|
161
156
|
*
|
|
162
|
-
* @param
|
|
163
|
-
* @param
|
|
164
|
-
|
|
165
|
-
|
|
157
|
+
* @param args - Arguments
|
|
158
|
+
* @param args.messenger - The preferences controller messenger.
|
|
159
|
+
* @param args.state - Preferences controller state.
|
|
160
|
+
*/
|
|
161
|
+
constructor({ messenger, state, }: {
|
|
162
|
+
messenger: PreferencesControllerMessenger;
|
|
163
|
+
state?: Partial<PreferencesState>;
|
|
164
|
+
});
|
|
166
165
|
/**
|
|
167
166
|
* Adds identities to state.
|
|
168
167
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PreferencesController.d.ts","sourceRoot":"","sources":["../src/PreferencesController.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"PreferencesController.d.ts","sourceRoot":"","sources":["../src/PreferencesController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,KAAK,0BAA0B,EAC/B,KAAK,wBAAwB,EAC7B,KAAK,6BAA6B,EACnC,MAAM,2BAA2B,CAAC;AAGnC,OAAO,EAAE,6BAA6B,EAAE,MAAM,aAAa,CAAC;AAE5D;;GAEG;AACH,oBAAY,QAAQ,GAAG;IACrB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF;;;GAGG;AACH,oBAAY,wBAA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AC,qBAAqB,EAAE,OAAO;IAMvD;;;;;OAKG;IACH,8BAA8B,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO;IAWrE;;;;OAIG;IACH,gCAAgC,CAAC,6BAA6B,EAAE,OAAO;IAMvE;;;;OAIG;IACH,mBAAmB,CAAC,gBAAgB,EAAE,OAAO;IAM7C;;;;OAIG;IACH,uBAAuB,CAAC,oBAAoB,EAAE,OAAO;IAMrD;;;;;OAKG;IACH,oCAAoC,CAClC,OAAO,EAAE,4BAA4B,EACrC,kCAAkC,EAAE,OAAO;CAW9C;AAED,eAAe,qBAAqB,CAAC"}
|
|
@@ -4,6 +4,23 @@ exports.PreferencesController = exports.getDefaultPreferencesState = void 0;
|
|
|
4
4
|
const base_controller_1 = require("@metamask/base-controller");
|
|
5
5
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
6
6
|
const constants_1 = require("./constants");
|
|
7
|
+
const metadata = {
|
|
8
|
+
disabledRpcMethodPreferences: { persist: true, anonymous: true },
|
|
9
|
+
featureFlags: { persist: true, anonymous: true },
|
|
10
|
+
identities: { persist: true, anonymous: false },
|
|
11
|
+
ipfsGateway: { persist: true, anonymous: false },
|
|
12
|
+
isIpfsGatewayEnabled: { persist: true, anonymous: true },
|
|
13
|
+
isMultiAccountBalancesEnabled: { persist: true, anonymous: true },
|
|
14
|
+
lostIdentities: { persist: true, anonymous: false },
|
|
15
|
+
openSeaEnabled: { persist: true, anonymous: true },
|
|
16
|
+
securityAlertsEnabled: { persist: true, anonymous: true },
|
|
17
|
+
selectedAddress: { persist: true, anonymous: false },
|
|
18
|
+
showTestNetworks: { persist: true, anonymous: true },
|
|
19
|
+
showIncomingTransactions: { persist: true, anonymous: true },
|
|
20
|
+
useNftDetection: { persist: true, anonymous: true },
|
|
21
|
+
useTokenDetection: { persist: true, anonymous: true },
|
|
22
|
+
};
|
|
23
|
+
const name = 'PreferencesController';
|
|
7
24
|
/**
|
|
8
25
|
* Get the default PreferencesController state.
|
|
9
26
|
*
|
|
@@ -53,21 +70,21 @@ exports.getDefaultPreferencesState = getDefaultPreferencesState;
|
|
|
53
70
|
/**
|
|
54
71
|
* Controller that stores shared settings and exposes convenience methods
|
|
55
72
|
*/
|
|
56
|
-
class PreferencesController extends base_controller_1.
|
|
73
|
+
class PreferencesController extends base_controller_1.BaseController {
|
|
57
74
|
/**
|
|
58
75
|
* Creates a PreferencesController instance.
|
|
59
76
|
*
|
|
60
|
-
* @param
|
|
61
|
-
* @param
|
|
77
|
+
* @param args - Arguments
|
|
78
|
+
* @param args.messenger - The preferences controller messenger.
|
|
79
|
+
* @param args.state - Preferences controller state.
|
|
62
80
|
*/
|
|
63
|
-
constructor(
|
|
64
|
-
super(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
this.initialize();
|
|
81
|
+
constructor({ messenger, state, }) {
|
|
82
|
+
super({
|
|
83
|
+
name,
|
|
84
|
+
metadata,
|
|
85
|
+
messenger,
|
|
86
|
+
state: Object.assign(Object.assign({}, getDefaultPreferencesState()), state),
|
|
87
|
+
});
|
|
71
88
|
}
|
|
72
89
|
/**
|
|
73
90
|
* Adds identities to state.
|
|
@@ -75,20 +92,21 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
75
92
|
* @param addresses - List of addresses to use to generate new identities.
|
|
76
93
|
*/
|
|
77
94
|
addIdentities(addresses) {
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
95
|
+
const checksummedAddresses = addresses.map(controller_utils_1.toChecksumHexAddress);
|
|
96
|
+
this.update((state) => {
|
|
97
|
+
const { identities } = state;
|
|
98
|
+
for (const address of checksummedAddresses) {
|
|
99
|
+
if (identities[address]) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const identityCount = Object.keys(identities).length;
|
|
103
|
+
identities[address] = {
|
|
104
|
+
name: `Account ${identityCount + 1}`,
|
|
105
|
+
address,
|
|
106
|
+
importTime: Date.now(),
|
|
107
|
+
};
|
|
83
108
|
}
|
|
84
|
-
const identityCount = Object.keys(identities).length;
|
|
85
|
-
identities[address] = {
|
|
86
|
-
name: `Account ${identityCount + 1}`,
|
|
87
|
-
address,
|
|
88
|
-
importTime: Date.now(),
|
|
89
|
-
};
|
|
90
109
|
});
|
|
91
|
-
this.update({ identities: Object.assign({}, identities) });
|
|
92
110
|
}
|
|
93
111
|
/**
|
|
94
112
|
* Removes an identity from state.
|
|
@@ -101,11 +119,12 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
101
119
|
if (!identities[address]) {
|
|
102
120
|
return;
|
|
103
121
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
122
|
+
this.update((state) => {
|
|
123
|
+
delete state.identities[address];
|
|
124
|
+
if (address === state.selectedAddress) {
|
|
125
|
+
state.selectedAddress = Object.keys(state.identities)[0];
|
|
126
|
+
}
|
|
127
|
+
});
|
|
109
128
|
}
|
|
110
129
|
/**
|
|
111
130
|
* Associates a new label with an identity.
|
|
@@ -115,10 +134,11 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
115
134
|
*/
|
|
116
135
|
setAccountLabel(address, label) {
|
|
117
136
|
address = (0, controller_utils_1.toChecksumHexAddress)(address);
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
137
|
+
this.update((state) => {
|
|
138
|
+
const identity = state.identities[address] || {};
|
|
139
|
+
identity.name = label;
|
|
140
|
+
state.identities[address] = identity;
|
|
141
|
+
});
|
|
122
142
|
}
|
|
123
143
|
/**
|
|
124
144
|
* Enable or disable a specific feature flag.
|
|
@@ -127,9 +147,9 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
127
147
|
* @param activated - Value to assign.
|
|
128
148
|
*/
|
|
129
149
|
setFeatureFlag(feature, activated) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
150
|
+
this.update((state) => {
|
|
151
|
+
state.featureFlags[feature] = activated;
|
|
152
|
+
});
|
|
133
153
|
}
|
|
134
154
|
/**
|
|
135
155
|
* Synchronizes the current identity list with new identities.
|
|
@@ -139,24 +159,24 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
139
159
|
*/
|
|
140
160
|
syncIdentities(addresses) {
|
|
141
161
|
addresses = addresses.map((address) => (0, controller_utils_1.toChecksumHexAddress)(address));
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
162
|
+
this.update((state) => {
|
|
163
|
+
const { identities } = state;
|
|
164
|
+
const newlyLost = {};
|
|
165
|
+
for (const [address, identity] of Object.entries(identities)) {
|
|
166
|
+
if (!addresses.includes(address)) {
|
|
167
|
+
newlyLost[address] = identity;
|
|
168
|
+
delete identities[address];
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
for (const [address, identity] of Object.entries(newlyLost)) {
|
|
172
|
+
state.lostIdentities[address] = identity;
|
|
148
173
|
}
|
|
149
|
-
}
|
|
150
|
-
for (const [address, identity] of Object.entries(newlyLost)) {
|
|
151
|
-
lostIdentities[address] = identity;
|
|
152
|
-
}
|
|
153
|
-
this.update({
|
|
154
|
-
identities: Object.assign({}, identities),
|
|
155
|
-
lostIdentities: Object.assign({}, lostIdentities),
|
|
156
174
|
});
|
|
157
175
|
this.addIdentities(addresses);
|
|
158
176
|
if (!addresses.includes(this.state.selectedAddress)) {
|
|
159
|
-
this.update(
|
|
177
|
+
this.update((state) => {
|
|
178
|
+
state.selectedAddress = addresses[0];
|
|
179
|
+
});
|
|
160
180
|
}
|
|
161
181
|
return this.state.selectedAddress;
|
|
162
182
|
}
|
|
@@ -169,20 +189,20 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
169
189
|
*/
|
|
170
190
|
updateIdentities(addresses) {
|
|
171
191
|
addresses = addresses.map((address) => (0, controller_utils_1.toChecksumHexAddress)(address));
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
192
|
+
this.update((state) => {
|
|
193
|
+
const identities = addresses.reduce((ids, address, index) => {
|
|
194
|
+
ids[address] = state.identities[address] || {
|
|
195
|
+
address,
|
|
196
|
+
name: `Account ${index + 1}`,
|
|
197
|
+
importTime: Date.now(),
|
|
198
|
+
};
|
|
199
|
+
return ids;
|
|
200
|
+
}, {});
|
|
201
|
+
state.identities = identities;
|
|
202
|
+
if (!Object.keys(identities).includes(state.selectedAddress)) {
|
|
203
|
+
state.selectedAddress = Object.keys(identities)[0];
|
|
204
|
+
}
|
|
205
|
+
});
|
|
186
206
|
}
|
|
187
207
|
/**
|
|
188
208
|
* Sets selected address.
|
|
@@ -190,7 +210,9 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
190
210
|
* @param selectedAddress - Ethereum address.
|
|
191
211
|
*/
|
|
192
212
|
setSelectedAddress(selectedAddress) {
|
|
193
|
-
this.update(
|
|
213
|
+
this.update((state) => {
|
|
214
|
+
state.selectedAddress = (0, controller_utils_1.toChecksumHexAddress)(selectedAddress);
|
|
215
|
+
});
|
|
194
216
|
}
|
|
195
217
|
/**
|
|
196
218
|
* Sets new IPFS gateway.
|
|
@@ -198,7 +220,9 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
198
220
|
* @param ipfsGateway - IPFS gateway string.
|
|
199
221
|
*/
|
|
200
222
|
setIpfsGateway(ipfsGateway) {
|
|
201
|
-
this.update(
|
|
223
|
+
this.update((state) => {
|
|
224
|
+
state.ipfsGateway = ipfsGateway;
|
|
225
|
+
});
|
|
202
226
|
}
|
|
203
227
|
/**
|
|
204
228
|
* Toggle the token detection setting.
|
|
@@ -206,7 +230,9 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
206
230
|
* @param useTokenDetection - Boolean indicating user preference on token detection.
|
|
207
231
|
*/
|
|
208
232
|
setUseTokenDetection(useTokenDetection) {
|
|
209
|
-
this.update(
|
|
233
|
+
this.update((state) => {
|
|
234
|
+
state.useTokenDetection = useTokenDetection;
|
|
235
|
+
});
|
|
210
236
|
}
|
|
211
237
|
/**
|
|
212
238
|
* Toggle the NFT detection setting.
|
|
@@ -217,7 +243,9 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
217
243
|
if (useNftDetection && !this.state.openSeaEnabled) {
|
|
218
244
|
throw new Error('useNftDetection cannot be enabled if openSeaEnabled is false');
|
|
219
245
|
}
|
|
220
|
-
this.update(
|
|
246
|
+
this.update((state) => {
|
|
247
|
+
state.useNftDetection = useNftDetection;
|
|
248
|
+
});
|
|
221
249
|
}
|
|
222
250
|
/**
|
|
223
251
|
* Toggle the opensea enabled setting.
|
|
@@ -225,10 +253,12 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
225
253
|
* @param openSeaEnabled - Boolean indicating user preference on using OpenSea's API.
|
|
226
254
|
*/
|
|
227
255
|
setOpenSeaEnabled(openSeaEnabled) {
|
|
228
|
-
this.update(
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
256
|
+
this.update((state) => {
|
|
257
|
+
state.openSeaEnabled = openSeaEnabled;
|
|
258
|
+
if (!openSeaEnabled) {
|
|
259
|
+
state.useNftDetection = false;
|
|
260
|
+
}
|
|
261
|
+
});
|
|
232
262
|
}
|
|
233
263
|
/**
|
|
234
264
|
* Toggle the security alert enabled setting.
|
|
@@ -236,7 +266,9 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
236
266
|
* @param securityAlertsEnabled - Boolean indicating user preference on using security alerts.
|
|
237
267
|
*/
|
|
238
268
|
setSecurityAlertsEnabled(securityAlertsEnabled) {
|
|
239
|
-
this.update(
|
|
269
|
+
this.update((state) => {
|
|
270
|
+
state.securityAlertsEnabled = securityAlertsEnabled;
|
|
271
|
+
});
|
|
240
272
|
}
|
|
241
273
|
/**
|
|
242
274
|
* A setter for the user preferences to enable/disable rpc methods.
|
|
@@ -247,7 +279,9 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
247
279
|
setDisabledRpcMethodPreference(methodName, isEnabled) {
|
|
248
280
|
const { disabledRpcMethodPreferences } = this.state;
|
|
249
281
|
const newDisabledRpcMethods = Object.assign(Object.assign({}, disabledRpcMethodPreferences), { [methodName]: isEnabled });
|
|
250
|
-
this.update(
|
|
282
|
+
this.update((state) => {
|
|
283
|
+
state.disabledRpcMethodPreferences = newDisabledRpcMethods;
|
|
284
|
+
});
|
|
251
285
|
}
|
|
252
286
|
/**
|
|
253
287
|
* A setter for the user preferences to enable/disable fetch of multiple accounts balance.
|
|
@@ -255,7 +289,9 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
255
289
|
* @param isMultiAccountBalancesEnabled - true to enable multiple accounts balance fetch, false to fetch only selectedAddress.
|
|
256
290
|
*/
|
|
257
291
|
setIsMultiAccountBalancesEnabled(isMultiAccountBalancesEnabled) {
|
|
258
|
-
this.update(
|
|
292
|
+
this.update((state) => {
|
|
293
|
+
state.isMultiAccountBalancesEnabled = isMultiAccountBalancesEnabled;
|
|
294
|
+
});
|
|
259
295
|
}
|
|
260
296
|
/**
|
|
261
297
|
* A setter for the user have the test networks visible/hidden.
|
|
@@ -263,7 +299,9 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
263
299
|
* @param showTestNetworks - true to show test networks, false to hidden.
|
|
264
300
|
*/
|
|
265
301
|
setShowTestNetworks(showTestNetworks) {
|
|
266
|
-
this.update(
|
|
302
|
+
this.update((state) => {
|
|
303
|
+
state.showTestNetworks = showTestNetworks;
|
|
304
|
+
});
|
|
267
305
|
}
|
|
268
306
|
/**
|
|
269
307
|
* A setter for the user allow to be fetched IPFS content
|
|
@@ -271,7 +309,9 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
271
309
|
* @param isIpfsGatewayEnabled - true to enable ipfs source
|
|
272
310
|
*/
|
|
273
311
|
setIsIpfsGatewayEnabled(isIpfsGatewayEnabled) {
|
|
274
|
-
this.update(
|
|
312
|
+
this.update((state) => {
|
|
313
|
+
state.isIpfsGatewayEnabled = isIpfsGatewayEnabled;
|
|
314
|
+
});
|
|
275
315
|
}
|
|
276
316
|
/**
|
|
277
317
|
* A setter for the user allow to be fetched IPFS content
|
|
@@ -281,8 +321,8 @@ class PreferencesController extends base_controller_1.BaseControllerV1 {
|
|
|
281
321
|
*/
|
|
282
322
|
setEnableNetworkIncomingTransactions(chainId, isIncomingTransactionNetworkEnable) {
|
|
283
323
|
if (Object.values(constants_1.ETHERSCAN_SUPPORTED_CHAIN_IDS).includes(chainId)) {
|
|
284
|
-
this.update({
|
|
285
|
-
showIncomingTransactions
|
|
324
|
+
this.update((state) => {
|
|
325
|
+
state.showIncomingTransactions = Object.assign(Object.assign({}, this.state.showIncomingTransactions), { [chainId]: isIncomingTransactionNetworkEnable });
|
|
286
326
|
});
|
|
287
327
|
}
|
|
288
328
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PreferencesController.js","sourceRoot":"","sources":["../src/PreferencesController.ts"],"names":[],"mappings":";;;AACA,+DAA6D;AAC7D,iEAAkE;AAElE,2CAA4D;AA0G5D;;;;GAIG;AACH,SAAgB,0BAA0B;IACxC,OAAO;QACL,4BAA4B,EAAE;YAC5B,QAAQ,EAAE,KAAK;SAChB;QACD,YAAY,EAAE,EAAE;QAChB,UAAU,EAAE,EAAE;QACd,WAAW,EAAE,uBAAuB;QACpC,oBAAoB,EAAE,IAAI;QAC1B,6BAA6B,EAAE,IAAI;QACnC,cAAc,EAAE,EAAE;QAClB,cAAc,EAAE,KAAK;QACrB,qBAAqB,EAAE,KAAK;QAC5B,eAAe,EAAE,EAAE;QACnB,wBAAwB,EAAE;YACxB,CAAC,yCAA6B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C;IAED;;;;;OAKG;IACH,8BAA8B,CAAC,UAAkB,EAAE,SAAkB;QACnE,MAAM,EAAE,4BAA4B,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QACpD,MAAM,qBAAqB,mCACtB,4BAA4B,KAC/B,CAAC,UAAU,CAAC,EAAE,SAAS,GACxB,CAAC;QACF,IAAI,CAAC,MAAM,CAAC,EAAE,4BAA4B,EAAE,qBAAqB,EAAE,CAAC,CAAC;IACvE,CAAC;IAED;;;;OAIG;IACH,gCAAgC,CAAC,6BAAsC;QACrE,IAAI,CAAC,MAAM,CAAC,EAAE,6BAA6B,EAAE,CAAC,CAAC;IACjD,CAAC;IAED;;;;OAIG;IACH,mBAAmB,CAAC,gBAAyB;QAC3C,IAAI,CAAC,MAAM,CAAC,EAAE,gBAAgB,EAAE,CAAC,CAAC;IACpC,CAAC;IAED;;;;OAIG;IACH,uBAAuB,CAAC,oBAA6B;QACnD,IAAI,CAAC,MAAM,CAAC,EAAE,oBAAoB,EAAE,CAAC,CAAC;IACxC,CAAC;IAED;;;;;OAKG;IACH,oCAAoC,CAClC,OAAqC,EACrC,kCAA2C;QAE3C,IAAI,MAAM,CAAC,MAAM,CAAC,yCAA6B,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;YAClE,IAAI,CAAC,MAAM,CAAC;gBACV,wBAAwB,kCACnB,IAAI,CAAC,KAAK,CAAC,wBAAwB,KACtC,CAAC,OAAO,CAAC,EAAE,kCAAkC,GAC9C;aACF,CAAC,CAAC;SACJ;IACH,CAAC;CACF;AAtRD,sDAsRC;AAED,kBAAe,qBAAqB,CAAC","sourcesContent":["import type { BaseConfig } from '@metamask/base-controller';\nimport { BaseControllerV1 } from '@metamask/base-controller';\nimport { toChecksumHexAddress } from '@metamask/controller-utils';\n\nimport { ETHERSCAN_SUPPORTED_CHAIN_IDS } from './constants';\n\n/**\n * A representation of a MetaMask identity\n */\nexport type Identity = {\n /**\n * The address of the identity\n */\n address: string;\n /**\n * The timestamp for when this identity was first added\n */\n importTime?: number;\n /**\n * The name of the identity\n */\n name: string;\n};\n\n/**\n * A type union of the name for each chain that is supported by Etherscan or\n * an Etherscan-compatible service.\n */\nexport type EtherscanSupportedChains =\n keyof typeof ETHERSCAN_SUPPORTED_CHAIN_IDS;\n\n/**\n * A type union of the chain ID for each chain that is supported by Etherscan\n * or an Etherscan-compatible service.\n */\nexport type EtherscanSupportedHexChainId =\n (typeof ETHERSCAN_SUPPORTED_CHAIN_IDS)[EtherscanSupportedChains];\n\n/**\n * Preferences controller state\n */\nexport type PreferencesState = {\n /**\n * A map of RPC method names to enabled state (true is enabled, false is disabled)\n */\n disabledRpcMethodPreferences: {\n [methodName: string]: boolean;\n };\n /**\n * Map of specific features to enable or disable\n */\n featureFlags: { [feature: string]: boolean };\n /**\n * Map of addresses to Identity objects\n */\n identities: { [address: string]: Identity };\n /**\n * The configured IPFS gateway\n */\n ipfsGateway: string;\n /**\n * Controls whether IPFS is enabled or not\n */\n isIpfsGatewayEnabled: boolean;\n /**\n * Controls whether multi-account balances are enabled or not\n */\n isMultiAccountBalancesEnabled: boolean;\n /**\n * Map of lost addresses to Identity objects\n */\n lostIdentities: { [address: string]: Identity };\n /**\n * The name of the controller\n *\n * @deprecated This property is never set, and will be removed in a future release\n */\n name?: string;\n /**\n * Controls whether the OpenSea API is used\n */\n openSeaEnabled: boolean;\n /**\n * Controls whether \"security alerts\" are enabled\n */\n securityAlertsEnabled: boolean;\n /**\n * The current selected address\n */\n selectedAddress: string;\n /**\n * Controls whether incoming transactions are enabled, per-chain (for Etherscan-supported chains)\n */\n showIncomingTransactions: {\n [chainId in EtherscanSupportedHexChainId]: boolean;\n };\n /**\n * Controls whether test networks are shown in the wallet\n */\n showTestNetworks: boolean;\n /**\n * Controls whether NFT detection is enabled\n */\n useNftDetection: boolean;\n /**\n * Controls whether token detection is enabled\n */\n useTokenDetection: boolean;\n};\n\n/**\n * Get the default PreferencesController state.\n *\n * @returns The default PreferencesController state.\n */\nexport function getDefaultPreferencesState() {\n return {\n disabledRpcMethodPreferences: {\n eth_sign: false,\n },\n featureFlags: {},\n identities: {},\n ipfsGateway: 'https://ipfs.io/ipfs/',\n isIpfsGatewayEnabled: true,\n isMultiAccountBalancesEnabled: true,\n lostIdentities: {},\n openSeaEnabled: false,\n securityAlertsEnabled: false,\n selectedAddress: '',\n showIncomingTransactions: {\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.MAINNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.GOERLI]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.BSC]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.BSC_TESTNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.OPTIMISM]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.OPTIMISM_TESTNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.POLYGON]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.POLYGON_TESTNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.AVALANCHE]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.AVALANCHE_TESTNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.FANTOM]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.FANTOM_TESTNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.SEPOLIA]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.LINEA_GOERLI]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.LINEA_MAINNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONBEAM]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONBEAM_TESTNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONRIVER]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.GNOSIS]: true,\n },\n showTestNetworks: false,\n useNftDetection: false,\n useTokenDetection: true,\n };\n}\n\n/**\n * Controller that stores shared settings and exposes convenience methods\n */\nexport class PreferencesController extends BaseControllerV1<\n BaseConfig,\n PreferencesState\n> {\n /**\n * Name of this controller used during composition\n */\n override name = 'PreferencesController';\n\n /**\n * Creates a PreferencesController instance.\n *\n * @param config - Initial options used to configure this controller.\n * @param state - Initial state to set on this controller.\n */\n constructor(config?: Partial<BaseConfig>, state?: Partial<PreferencesState>) {\n super(config, state);\n this.defaultState = getDefaultPreferencesState();\n this.initialize();\n }\n\n /**\n * Adds identities to state.\n *\n * @param addresses - List of addresses to use to generate new identities.\n */\n addIdentities(addresses: string[]) {\n const { identities } = this.state;\n addresses.forEach((address) => {\n address = toChecksumHexAddress(address);\n if (identities[address]) {\n return;\n }\n const identityCount = Object.keys(identities).length;\n\n identities[address] = {\n name: `Account ${identityCount + 1}`,\n address,\n importTime: Date.now(),\n };\n });\n this.update({ identities: { ...identities } });\n }\n\n /**\n * Removes an identity from state.\n *\n * @param address - Address of the identity to remove.\n */\n removeIdentity(address: string) {\n address = toChecksumHexAddress(address);\n const { identities } = this.state;\n if (!identities[address]) {\n return;\n }\n delete identities[address];\n this.update({ identities: { ...identities } });\n if (address === this.state.selectedAddress) {\n this.update({ selectedAddress: Object.keys(identities)[0] });\n }\n }\n\n /**\n * Associates a new label with an identity.\n *\n * @param address - Address of the identity to associate.\n * @param label - New label to assign.\n */\n setAccountLabel(address: string, label: string) {\n address = toChecksumHexAddress(address);\n const { identities } = this.state;\n identities[address] = identities[address] || {};\n identities[address].name = label;\n this.update({ identities: { ...identities } });\n }\n\n /**\n * Enable or disable a specific feature flag.\n *\n * @param feature - Feature to toggle.\n * @param activated - Value to assign.\n */\n setFeatureFlag(feature: string, activated: boolean) {\n const oldFeatureFlags = this.state.featureFlags;\n const featureFlags = { ...oldFeatureFlags, ...{ [feature]: activated } };\n this.update({ featureFlags: { ...featureFlags } });\n }\n\n /**\n * Synchronizes the current identity list with new identities.\n *\n * @param addresses - List of addresses corresponding to identities to sync.\n * @returns Newly-selected address after syncing.\n */\n syncIdentities(addresses: string[]) {\n addresses = addresses.map((address: string) =>\n toChecksumHexAddress(address),\n );\n const { identities, lostIdentities } = this.state;\n const newlyLost: { [address: string]: Identity } = {};\n\n for (const [address, identity] of Object.entries(identities)) {\n if (!addresses.includes(address)) {\n newlyLost[address] = identity;\n delete identities[address];\n }\n }\n\n for (const [address, identity] of Object.entries(newlyLost)) {\n lostIdentities[address] = identity;\n }\n\n this.update({\n identities: { ...identities },\n lostIdentities: { ...lostIdentities },\n });\n this.addIdentities(addresses);\n\n if (!addresses.includes(this.state.selectedAddress)) {\n this.update({ selectedAddress: addresses[0] });\n }\n\n return this.state.selectedAddress;\n }\n\n /**\n * Generates and stores a new list of stored identities based on address. If the selected address\n * is unset, or if it refers to an identity that was removed, it will be set to the first\n * identity.\n *\n * @param addresses - List of addresses to use as a basis for each identity.\n */\n updateIdentities(addresses: string[]) {\n addresses = addresses.map((address: string) =>\n toChecksumHexAddress(address),\n );\n const oldIdentities = this.state.identities;\n const identities = addresses.reduce(\n (ids: { [address: string]: Identity }, address, index) => {\n ids[address] = oldIdentities[address] || {\n address,\n name: `Account ${index + 1}`,\n importTime: Date.now(),\n };\n return ids;\n },\n {},\n );\n let { selectedAddress } = this.state;\n if (!Object.keys(identities).includes(selectedAddress)) {\n selectedAddress = Object.keys(identities)[0];\n }\n this.update({ identities: { ...identities }, selectedAddress });\n }\n\n /**\n * Sets selected address.\n *\n * @param selectedAddress - Ethereum address.\n */\n setSelectedAddress(selectedAddress: string) {\n this.update({ selectedAddress: toChecksumHexAddress(selectedAddress) });\n }\n\n /**\n * Sets new IPFS gateway.\n *\n * @param ipfsGateway - IPFS gateway string.\n */\n setIpfsGateway(ipfsGateway: string) {\n this.update({ ipfsGateway });\n }\n\n /**\n * Toggle the token detection setting.\n *\n * @param useTokenDetection - Boolean indicating user preference on token detection.\n */\n setUseTokenDetection(useTokenDetection: boolean) {\n this.update({ useTokenDetection });\n }\n\n /**\n * Toggle the NFT detection setting.\n *\n * @param useNftDetection - Boolean indicating user preference on NFT detection.\n */\n setUseNftDetection(useNftDetection: boolean) {\n if (useNftDetection && !this.state.openSeaEnabled) {\n throw new Error(\n 'useNftDetection cannot be enabled if openSeaEnabled is false',\n );\n }\n this.update({ useNftDetection });\n }\n\n /**\n * Toggle the opensea enabled setting.\n *\n * @param openSeaEnabled - Boolean indicating user preference on using OpenSea's API.\n */\n setOpenSeaEnabled(openSeaEnabled: boolean) {\n this.update({ openSeaEnabled });\n if (!openSeaEnabled) {\n this.update({ useNftDetection: false });\n }\n }\n\n /**\n * Toggle the security alert enabled setting.\n *\n * @param securityAlertsEnabled - Boolean indicating user preference on using security alerts.\n */\n setSecurityAlertsEnabled(securityAlertsEnabled: boolean) {\n this.update({ securityAlertsEnabled });\n }\n\n /**\n * A setter for the user preferences to enable/disable rpc methods.\n *\n * @param methodName - The RPC method name to change the setting of.\n * @param isEnabled - true to enable the rpc method, false to disable it.\n */\n setDisabledRpcMethodPreference(methodName: string, isEnabled: boolean) {\n const { disabledRpcMethodPreferences } = this.state;\n const newDisabledRpcMethods = {\n ...disabledRpcMethodPreferences,\n [methodName]: isEnabled,\n };\n this.update({ disabledRpcMethodPreferences: newDisabledRpcMethods });\n }\n\n /**\n * A setter for the user preferences to enable/disable fetch of multiple accounts balance.\n *\n * @param isMultiAccountBalancesEnabled - true to enable multiple accounts balance fetch, false to fetch only selectedAddress.\n */\n setIsMultiAccountBalancesEnabled(isMultiAccountBalancesEnabled: boolean) {\n this.update({ isMultiAccountBalancesEnabled });\n }\n\n /**\n * A setter for the user have the test networks visible/hidden.\n *\n * @param showTestNetworks - true to show test networks, false to hidden.\n */\n setShowTestNetworks(showTestNetworks: boolean) {\n this.update({ showTestNetworks });\n }\n\n /**\n * A setter for the user allow to be fetched IPFS content\n *\n * @param isIpfsGatewayEnabled - true to enable ipfs source\n */\n setIsIpfsGatewayEnabled(isIpfsGatewayEnabled: boolean) {\n this.update({ isIpfsGatewayEnabled });\n }\n\n /**\n * A setter for the user allow to be fetched IPFS content\n *\n * @param chainId - On hexadecimal format to enable the incoming transaction network\n * @param isIncomingTransactionNetworkEnable - true to enable incoming transactions\n */\n setEnableNetworkIncomingTransactions(\n chainId: EtherscanSupportedHexChainId,\n isIncomingTransactionNetworkEnable: boolean,\n ) {\n if (Object.values(ETHERSCAN_SUPPORTED_CHAIN_IDS).includes(chainId)) {\n this.update({\n showIncomingTransactions: {\n ...this.state.showIncomingTransactions,\n [chainId]: isIncomingTransactionNetworkEnable,\n },\n });\n }\n }\n}\n\nexport default PreferencesController;\n"]}
|
|
1
|
+
{"version":3,"file":"PreferencesController.js","sourceRoot":"","sources":["../src/PreferencesController.ts"],"names":[],"mappings":";;;AAAA,+DAKmC;AACnC,iEAAkE;AAElE,2CAA4D;AAoG5D,MAAM,QAAQ,GAAG;IACf,4BAA4B,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;IAChE,YAAY,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;IAChD,UAAU,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE;IAC/C,WAAW,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE;IAChD,oBAAoB,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;IACxD,6BAA6B,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;IACjE,cAAc,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE;IACnD,cAAc,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;IAClD,qBAAqB,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;IACzD,eAAe,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,S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,EAAE,EAAE;YACpB,KAAK,CAAC,6BAA6B,GAAG,6BAA6B,CAAC;QACtE,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,mBAAmB,CAAC,gBAAyB;QAC3C,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QAC5C,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,uBAAuB,CAAC,oBAA6B;QACnD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,oBAAoB,GAAG,oBAAoB,CAAC;QACpD,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACH,oCAAoC,CAClC,OAAqC,EACrC,kCAA2C;QAE3C,IAAI,MAAM,CAAC,MAAM,CAAC,yCAA6B,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;YAClE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,KAAK,CAAC,wBAAwB,mCACzB,IAAI,CAAC,KAAK,CAAC,wBAAwB,KACtC,CAAC,OAAO,CAAC,EAAE,kCAAkC,GAC9C,CAAC;YACJ,CAAC,CAAC,CAAC;SACJ;IACH,CAAC;CACF;AAtTD,sDAsTC;AAED,kBAAe,qBAAqB,CAAC","sourcesContent":["import {\n BaseController,\n type ControllerStateChangeEvent,\n type ControllerGetStateAction,\n type RestrictedControllerMessenger,\n} from '@metamask/base-controller';\nimport { toChecksumHexAddress } from '@metamask/controller-utils';\n\nimport { ETHERSCAN_SUPPORTED_CHAIN_IDS } from './constants';\n\n/**\n * A representation of a MetaMask identity\n */\nexport type Identity = {\n /**\n * The address of the identity\n */\n address: string;\n /**\n * The timestamp for when this identity was first added\n */\n importTime?: number;\n /**\n * The name of the identity\n */\n name: string;\n};\n\n/**\n * A type union of the name for each chain that is supported by Etherscan or\n * an Etherscan-compatible service.\n */\nexport type EtherscanSupportedChains =\n keyof typeof ETHERSCAN_SUPPORTED_CHAIN_IDS;\n\n/**\n * A type union of the chain ID for each chain that is supported by Etherscan\n * or an Etherscan-compatible service.\n */\nexport type EtherscanSupportedHexChainId =\n (typeof ETHERSCAN_SUPPORTED_CHAIN_IDS)[EtherscanSupportedChains];\n\n/**\n * Preferences controller state\n */\nexport type PreferencesState = {\n /**\n * A map of RPC method names to enabled state (true is enabled, false is disabled)\n */\n disabledRpcMethodPreferences: {\n [methodName: string]: boolean;\n };\n /**\n * Map of specific features to enable or disable\n */\n featureFlags: { [feature: string]: boolean };\n /**\n * Map of addresses to Identity objects\n */\n identities: { [address: string]: Identity };\n /**\n * The configured IPFS gateway\n */\n ipfsGateway: string;\n /**\n * Controls whether IPFS is enabled or not\n */\n isIpfsGatewayEnabled: boolean;\n /**\n * Controls whether multi-account balances are enabled or not\n */\n isMultiAccountBalancesEnabled: boolean;\n /**\n * Map of lost addresses to Identity objects\n */\n lostIdentities: { [address: string]: Identity };\n /**\n * Controls whether the OpenSea API is used\n */\n openSeaEnabled: boolean;\n /**\n * Controls whether \"security alerts\" are enabled\n */\n securityAlertsEnabled: boolean;\n /**\n * The current selected address\n */\n selectedAddress: string;\n /**\n * Controls whether incoming transactions are enabled, per-chain (for Etherscan-supported chains)\n */\n showIncomingTransactions: {\n [chainId in EtherscanSupportedHexChainId]: boolean;\n };\n /**\n * Controls whether test networks are shown in the wallet\n */\n showTestNetworks: boolean;\n /**\n * Controls whether NFT detection is enabled\n */\n useNftDetection: boolean;\n /**\n * Controls whether token detection is enabled\n */\n useTokenDetection: boolean;\n};\n\nconst metadata = {\n disabledRpcMethodPreferences: { persist: true, anonymous: true },\n featureFlags: { persist: true, anonymous: true },\n identities: { persist: true, anonymous: false },\n ipfsGateway: { persist: true, anonymous: false },\n isIpfsGatewayEnabled: { persist: true, anonymous: true },\n isMultiAccountBalancesEnabled: { persist: true, anonymous: true },\n lostIdentities: { persist: true, anonymous: false },\n openSeaEnabled: { persist: true, anonymous: true },\n securityAlertsEnabled: { persist: true, anonymous: true },\n selectedAddress: { persist: true, anonymous: false },\n showTestNetworks: { persist: true, anonymous: true },\n showIncomingTransactions: { persist: true, anonymous: true },\n useNftDetection: { persist: true, anonymous: true },\n useTokenDetection: { persist: true, anonymous: true },\n};\n\nconst name = 'PreferencesController';\n\nexport type PreferencesControllerGetStateAction = ControllerGetStateAction<\n typeof name,\n PreferencesState\n>;\n\nexport type PreferencesControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof name,\n PreferencesState\n>;\n\nexport type PreferencesControllerActions = PreferencesControllerGetStateAction;\n\nexport type PreferencesControllerEvents = PreferencesControllerStateChangeEvent;\n\nexport type PreferencesControllerMessenger = RestrictedControllerMessenger<\n typeof name,\n PreferencesControllerActions,\n PreferencesControllerEvents,\n never,\n never\n>;\n\n/**\n * Get the default PreferencesController state.\n *\n * @returns The default PreferencesController state.\n */\nexport function getDefaultPreferencesState() {\n return {\n disabledRpcMethodPreferences: {\n eth_sign: false,\n },\n featureFlags: {},\n identities: {},\n ipfsGateway: 'https://ipfs.io/ipfs/',\n isIpfsGatewayEnabled: true,\n isMultiAccountBalancesEnabled: true,\n lostIdentities: {},\n openSeaEnabled: false,\n securityAlertsEnabled: false,\n selectedAddress: '',\n showIncomingTransactions: {\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.MAINNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.GOERLI]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.BSC]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.BSC_TESTNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.OPTIMISM]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.OPTIMISM_TESTNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.POLYGON]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.POLYGON_TESTNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.AVALANCHE]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.AVALANCHE_TESTNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.FANTOM]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.FANTOM_TESTNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.SEPOLIA]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.LINEA_GOERLI]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.LINEA_MAINNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONBEAM]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONBEAM_TESTNET]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONRIVER]: true,\n [ETHERSCAN_SUPPORTED_CHAIN_IDS.GNOSIS]: true,\n },\n showTestNetworks: false,\n useNftDetection: false,\n useTokenDetection: true,\n };\n}\n\n/**\n * Controller that stores shared settings and exposes convenience methods\n */\nexport class PreferencesController extends BaseController<\n typeof name,\n PreferencesState,\n PreferencesControllerMessenger\n> {\n /**\n * Creates a PreferencesController instance.\n *\n * @param args - Arguments\n * @param args.messenger - The preferences controller messenger.\n * @param args.state - Preferences controller state.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: PreferencesControllerMessenger;\n state?: Partial<PreferencesState>;\n }) {\n super({\n name,\n metadata,\n messenger,\n state: {\n ...getDefaultPreferencesState(),\n ...state,\n },\n });\n }\n\n /**\n * Adds identities to state.\n *\n * @param addresses - List of addresses to use to generate new identities.\n */\n addIdentities(addresses: string[]) {\n const checksummedAddresses = addresses.map(toChecksumHexAddress);\n this.update((state) => {\n const { identities } = state;\n for (const address of checksummedAddresses) {\n if (identities[address]) {\n continue;\n }\n const identityCount = Object.keys(identities).length;\n\n identities[address] = {\n name: `Account ${identityCount + 1}`,\n address,\n importTime: Date.now(),\n };\n }\n });\n }\n\n /**\n * Removes an identity from state.\n *\n * @param address - Address of the identity to remove.\n */\n removeIdentity(address: string) {\n address = toChecksumHexAddress(address);\n const { identities } = this.state;\n if (!identities[address]) {\n return;\n }\n this.update((state) => {\n delete state.identities[address];\n if (address === state.selectedAddress) {\n state.selectedAddress = Object.keys(state.identities)[0];\n }\n });\n }\n\n /**\n * Associates a new label with an identity.\n *\n * @param address - Address of the identity to associate.\n * @param label - New label to assign.\n */\n setAccountLabel(address: string, label: string) {\n address = toChecksumHexAddress(address);\n this.update((state) => {\n const identity = state.identities[address] || {};\n identity.name = label;\n state.identities[address] = identity;\n });\n }\n\n /**\n * Enable or disable a specific feature flag.\n *\n * @param feature - Feature to toggle.\n * @param activated - Value to assign.\n */\n setFeatureFlag(feature: string, activated: boolean) {\n this.update((state) => {\n state.featureFlags[feature] = activated;\n });\n }\n\n /**\n * Synchronizes the current identity list with new identities.\n *\n * @param addresses - List of addresses corresponding to identities to sync.\n * @returns Newly-selected address after syncing.\n */\n syncIdentities(addresses: string[]) {\n addresses = addresses.map((address: string) =>\n toChecksumHexAddress(address),\n );\n\n this.update((state) => {\n const { identities } = state;\n const newlyLost: { [address: string]: Identity } = {};\n\n for (const [address, identity] of Object.entries(identities)) {\n if (!addresses.includes(address)) {\n newlyLost[address] = identity;\n delete identities[address];\n }\n }\n\n for (const [address, identity] of Object.entries(newlyLost)) {\n state.lostIdentities[address] = identity;\n }\n });\n this.addIdentities(addresses);\n\n if (!addresses.includes(this.state.selectedAddress)) {\n this.update((state) => {\n state.selectedAddress = addresses[0];\n });\n }\n\n return this.state.selectedAddress;\n }\n\n /**\n * Generates and stores a new list of stored identities based on address. If the selected address\n * is unset, or if it refers to an identity that was removed, it will be set to the first\n * identity.\n *\n * @param addresses - List of addresses to use as a basis for each identity.\n */\n updateIdentities(addresses: string[]) {\n addresses = addresses.map((address: string) =>\n toChecksumHexAddress(address),\n );\n this.update((state) => {\n const identities = addresses.reduce(\n (ids: { [address: string]: Identity }, address, index) => {\n ids[address] = state.identities[address] || {\n address,\n name: `Account ${index + 1}`,\n importTime: Date.now(),\n };\n return ids;\n },\n {},\n );\n state.identities = identities;\n if (!Object.keys(identities).includes(state.selectedAddress)) {\n state.selectedAddress = Object.keys(identities)[0];\n }\n });\n }\n\n /**\n * Sets selected address.\n *\n * @param selectedAddress - Ethereum address.\n */\n setSelectedAddress(selectedAddress: string) {\n this.update((state) => {\n state.selectedAddress = toChecksumHexAddress(selectedAddress);\n });\n }\n\n /**\n * Sets new IPFS gateway.\n *\n * @param ipfsGateway - IPFS gateway string.\n */\n setIpfsGateway(ipfsGateway: string) {\n this.update((state) => {\n state.ipfsGateway = ipfsGateway;\n });\n }\n\n /**\n * Toggle the token detection setting.\n *\n * @param useTokenDetection - Boolean indicating user preference on token detection.\n */\n setUseTokenDetection(useTokenDetection: boolean) {\n this.update((state) => {\n state.useTokenDetection = useTokenDetection;\n });\n }\n\n /**\n * Toggle the NFT detection setting.\n *\n * @param useNftDetection - Boolean indicating user preference on NFT detection.\n */\n setUseNftDetection(useNftDetection: boolean) {\n if (useNftDetection && !this.state.openSeaEnabled) {\n throw new Error(\n 'useNftDetection cannot be enabled if openSeaEnabled is false',\n );\n }\n this.update((state) => {\n state.useNftDetection = useNftDetection;\n });\n }\n\n /**\n * Toggle the opensea enabled setting.\n *\n * @param openSeaEnabled - Boolean indicating user preference on using OpenSea's API.\n */\n setOpenSeaEnabled(openSeaEnabled: boolean) {\n this.update((state) => {\n state.openSeaEnabled = openSeaEnabled;\n if (!openSeaEnabled) {\n state.useNftDetection = false;\n }\n });\n }\n\n /**\n * Toggle the security alert enabled setting.\n *\n * @param securityAlertsEnabled - Boolean indicating user preference on using security alerts.\n */\n setSecurityAlertsEnabled(securityAlertsEnabled: boolean) {\n this.update((state) => {\n state.securityAlertsEnabled = securityAlertsEnabled;\n });\n }\n\n /**\n * A setter for the user preferences to enable/disable rpc methods.\n *\n * @param methodName - The RPC method name to change the setting of.\n * @param isEnabled - true to enable the rpc method, false to disable it.\n */\n setDisabledRpcMethodPreference(methodName: string, isEnabled: boolean) {\n const { disabledRpcMethodPreferences } = this.state;\n const newDisabledRpcMethods = {\n ...disabledRpcMethodPreferences,\n [methodName]: isEnabled,\n };\n this.update((state) => {\n state.disabledRpcMethodPreferences = newDisabledRpcMethods;\n });\n }\n\n /**\n * A setter for the user preferences to enable/disable fetch of multiple accounts balance.\n *\n * @param isMultiAccountBalancesEnabled - true to enable multiple accounts balance fetch, false to fetch only selectedAddress.\n */\n setIsMultiAccountBalancesEnabled(isMultiAccountBalancesEnabled: boolean) {\n this.update((state) => {\n state.isMultiAccountBalancesEnabled = isMultiAccountBalancesEnabled;\n });\n }\n\n /**\n * A setter for the user have the test networks visible/hidden.\n *\n * @param showTestNetworks - true to show test networks, false to hidden.\n */\n setShowTestNetworks(showTestNetworks: boolean) {\n this.update((state) => {\n state.showTestNetworks = showTestNetworks;\n });\n }\n\n /**\n * A setter for the user allow to be fetched IPFS content\n *\n * @param isIpfsGatewayEnabled - true to enable ipfs source\n */\n setIsIpfsGatewayEnabled(isIpfsGatewayEnabled: boolean) {\n this.update((state) => {\n state.isIpfsGatewayEnabled = isIpfsGatewayEnabled;\n });\n }\n\n /**\n * A setter for the user allow to be fetched IPFS content\n *\n * @param chainId - On hexadecimal format to enable the incoming transaction network\n * @param isIncomingTransactionNetworkEnable - true to enable incoming transactions\n */\n setEnableNetworkIncomingTransactions(\n chainId: EtherscanSupportedHexChainId,\n isIncomingTransactionNetworkEnable: boolean,\n ) {\n if (Object.values(ETHERSCAN_SUPPORTED_CHAIN_IDS).includes(chainId)) {\n this.update((state) => {\n state.showIncomingTransactions = {\n ...this.state.showIncomingTransactions,\n [chainId]: isIncomingTransactionNetworkEnable,\n };\n });\n }\n }\n}\n\nexport default PreferencesController;\n"]}
|