@metamask/name-controller 1.0.0 → 3.0.0

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 CHANGED
@@ -6,9 +6,33 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.0.0]
10
+ ### Changed
11
+ - **BREAKING**: Normalize addresses and chain IDs ([#1732](https://github.com/MetaMask/core/pull/1732))
12
+ - Save addresses and chain IDs as lowercase in state
13
+ - Remove `getChainId` constructor callback
14
+ - Require a `variation` property when calling `setName` or `updateProposedNames` with the `ethereumAddress` type
15
+
16
+ ## [2.0.0]
17
+ ### Changed
18
+ - **BREAKING**: Support rate limiting in name providers ([#1715](https://github.com/MetaMask/core/pull/1715))
19
+ - Breaking changes:
20
+ - Change `proposedNames` property in `NameEntry` type from string array to new `ProposedNamesEntry` type
21
+ - Remove `proposedNamesLastUpdated` property from `NameEntry` type
22
+ - Add `onlyUpdateAfterDelay` option to `UpdateProposedNamesRequest` type
23
+ - Add `updateDelay` constructor option
24
+ - Add `updateDelay` property to `NameProviderSourceResult` type
25
+ - Add `isEnabled` callback option to `ENSNameProvider`, `EtherscanNameProvider`, `LensNameProvider`, and `TokenNameProvider`
26
+ - Existing proposed names in state are only updated if the `NameProvider` has no errors and the `proposedNames` property is not `undefined`
27
+ - Dormant proposed names are automatically removed when calling `updateProposedNames` ([#1688](https://github.com/MetaMask/core/pull/1688))
28
+ - The `setName` method accepts a `null` value for the `name` property to enable removing saved names ([#1688](https://github.com/MetaMask/core/pull/1688))
29
+ - Update TypeScript to v4.8.x ([#1718](https://github.com/MetaMask/core/pull/1718))
30
+
9
31
  ## [1.0.0]
10
32
  ### Added
11
33
  - Initial Release ([#1647](https://github.com/MetaMask/core/pull/1647))
12
34
 
13
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/name-controller@1.0.0...HEAD
35
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/name-controller@3.0.0...HEAD
36
+ [3.0.0]: https://github.com/MetaMask/core/compare/@metamask/name-controller@2.0.0...@metamask/name-controller@3.0.0
37
+ [2.0.0]: https://github.com/MetaMask/core/compare/@metamask/name-controller@1.0.0...@metamask/name-controller@2.0.0
14
38
  [1.0.0]: https://github.com/MetaMask/core/releases/tag/@metamask/name-controller@1.0.0
@@ -4,11 +4,15 @@ import type { Patch } from 'immer';
4
4
  import type { NameProvider } from './types';
5
5
  import { NameType } from './types';
6
6
  declare const controllerName = "NameController";
7
+ export declare type ProposedNamesEntry = {
8
+ proposedNames: string[];
9
+ lastRequestTime: number | null;
10
+ updateDelay: number | null;
11
+ };
7
12
  export declare type NameEntry = {
8
13
  name: string | null;
9
14
  sourceId: string | null;
10
- proposedNames: Record<string, string[] | null>;
11
- proposedNamesLastUpdated: number | null;
15
+ proposedNames: Record<string, ProposedNamesEntry>;
12
16
  };
13
17
  export declare type SourceEntry = {
14
18
  label: string;
@@ -29,15 +33,17 @@ export declare type NameControllerActions = GetNameState;
29
33
  export declare type NameControllerEvents = NameStateChange;
30
34
  export declare type NameControllerMessenger = RestrictedControllerMessenger<typeof controllerName, NameControllerActions, NameControllerEvents, never, never>;
31
35
  export declare type NameControllerOptions = {
32
- getChainId: () => string;
33
36
  messenger: NameControllerMessenger;
34
37
  providers: NameProvider[];
35
38
  state?: Partial<NameControllerState>;
39
+ updateDelay?: number;
36
40
  };
37
41
  export declare type UpdateProposedNamesRequest = {
38
42
  value: string;
39
43
  type: NameType;
40
44
  sourceIds?: string[];
45
+ onlyUpdateAfterDelay?: boolean;
46
+ variation?: string;
41
47
  };
42
48
  export declare type UpdateProposedNamesResult = {
43
49
  results: Record<string, {
@@ -48,8 +54,9 @@ export declare type UpdateProposedNamesResult = {
48
54
  export declare type SetNameRequest = {
49
55
  value: string;
50
56
  type: NameType;
51
- name: string;
57
+ name: string | null;
52
58
  sourceId?: string;
59
+ variation?: string;
53
60
  };
54
61
  /**
55
62
  * Controller for storing and deriving names for values such as Ethereum addresses.
@@ -60,12 +67,12 @@ export declare class NameController extends BaseControllerV2<typeof controllerNa
60
67
  * Construct a Name controller.
61
68
  *
62
69
  * @param options - Controller options.
63
- * @param options.getChainId - Callback that returns the chain ID of the current network.
64
70
  * @param options.messenger - Restricted controller messenger for the name controller.
65
71
  * @param options.providers - Array of name provider instances to propose names.
66
72
  * @param options.state - Initial state to set on the controller.
73
+ * @param options.updateDelay - The delay in seconds before a new request to a source should be made.
67
74
  */
68
- constructor({ getChainId, messenger, providers, state, }: NameControllerOptions);
75
+ constructor({ messenger, providers, state, updateDelay, }: NameControllerOptions);
69
76
  /**
70
77
  * Set the user specified name for a value.
71
78
  *
@@ -74,6 +81,7 @@ export declare class NameController extends BaseControllerV2<typeof controllerNa
74
81
  * @param request.sourceId - Optional ID of the source of the proposed name.
75
82
  * @param request.type - Type of value to set the name for.
76
83
  * @param request.value - Value to set the name for.
84
+ * @param request.variation - Variation of the raw value to set the name for. The chain ID if the type is Ethereum address.
77
85
  */
78
86
  setName(request: SetNameRequest): void;
79
87
  /**
@@ -83,6 +91,7 @@ export declare class NameController extends BaseControllerV2<typeof controllerNa
83
91
  * @param request.value - Value to update the proposed names for.
84
92
  * @param request.type - Type of value to update the proposed names for.
85
93
  * @param request.sourceIds - Optional array of source IDs to limit which sources are used by the providers. If not provided, all sources in all providers will be used.
94
+ * @param request.variation - Variation of the raw value to update proposed names for. The chain ID if the type is Ethereum address.
86
95
  * @returns The updated proposed names for the value.
87
96
  */
88
97
  updateProposedNames(request: UpdateProposedNamesRequest): Promise<UpdateProposedNamesResult>;
@@ -1 +1 @@
1
- {"version":3,"file":"NameController.d.ts","sourceRoot":"","sources":["../src/NameController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,2BAA2B,CAAC;AAC/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,KAAK,EAAE,KAAK,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) || function (
19
19
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
20
20
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
21
  };
22
- var _NameController_instances, _NameController_getChainId, _NameController_providers, _NameController_updateProposedNameState, _NameController_updateSourceState, _NameController_getUpdateProposedNamesResult, _NameController_getProviderResponse, _NameController_updateEntry, _NameController_getTypeVariationKey, _NameController_getCurrentTimeSeconds, _NameController_validateSetNameRequest, _NameController_validateUpdateProposedNamesRequest, _NameController_validateValue, _NameController_validateType, _NameController_validateName, _NameController_validateSourceIds, _NameController_validateSourceId, _NameController_validateDuplicateSourceIds, _NameController_getAllSourceIds, _NameController_getSourceIds;
22
+ var _NameController_instances, _NameController_providers, _NameController_updateDelay, _NameController_updateProposedNameState, _NameController_updateSourceState, _NameController_getUpdateProposedNamesResult, _NameController_getProviderResponse, _NameController_normalizeProviderResult, _NameController_normalizeProviderSourceResult, _NameController_normalizeValue, _NameController_normalizeVariation, _NameController_updateEntry, _NameController_getCurrentTimeSeconds, _NameController_validateSetNameRequest, _NameController_validateUpdateProposedNamesRequest, _NameController_validateValue, _NameController_validateType, _NameController_validateName, _NameController_validateSourceIds, _NameController_validateSourceId, _NameController_validateDuplicateSourceIds, _NameController_validateVariation, _NameController_getAllSourceIds, _NameController_getSourceIds, _NameController_removeDormantProposedNames;
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.NameController = void 0;
25
25
  const base_controller_1 = require("@metamask/base-controller");
26
26
  const types_1 = require("./types");
27
+ const DEFAULT_UPDATE_DELAY = 60 * 2; // 2 Minutes
28
+ const DEFAULT_VARIATION = '';
27
29
  const controllerName = 'NameController';
28
30
  const stateMetadata = {
29
31
  names: { persist: true, anonymous: false },
@@ -43,12 +45,12 @@ class NameController extends base_controller_1.BaseControllerV2 {
43
45
  * Construct a Name controller.
44
46
  *
45
47
  * @param options - Controller options.
46
- * @param options.getChainId - Callback that returns the chain ID of the current network.
47
48
  * @param options.messenger - Restricted controller messenger for the name controller.
48
49
  * @param options.providers - Array of name provider instances to propose names.
49
50
  * @param options.state - Initial state to set on the controller.
51
+ * @param options.updateDelay - The delay in seconds before a new request to a source should be made.
50
52
  */
51
- constructor({ getChainId, messenger, providers, state, }) {
53
+ constructor({ messenger, providers, state, updateDelay, }) {
52
54
  super({
53
55
  name: controllerName,
54
56
  metadata: stateMetadata,
@@ -56,10 +58,10 @@ class NameController extends base_controller_1.BaseControllerV2 {
56
58
  state: Object.assign(Object.assign({}, getDefaultState()), state),
57
59
  });
58
60
  _NameController_instances.add(this);
59
- _NameController_getChainId.set(this, void 0);
60
61
  _NameController_providers.set(this, void 0);
61
- __classPrivateFieldSet(this, _NameController_getChainId, getChainId, "f");
62
+ _NameController_updateDelay.set(this, void 0);
62
63
  __classPrivateFieldSet(this, _NameController_providers, providers, "f");
64
+ __classPrivateFieldSet(this, _NameController_updateDelay, updateDelay !== null && updateDelay !== void 0 ? updateDelay : DEFAULT_UPDATE_DELAY, "f");
63
65
  }
64
66
  /**
65
67
  * Set the user specified name for a value.
@@ -69,11 +71,16 @@ class NameController extends base_controller_1.BaseControllerV2 {
69
71
  * @param request.sourceId - Optional ID of the source of the proposed name.
70
72
  * @param request.type - Type of value to set the name for.
71
73
  * @param request.value - Value to set the name for.
74
+ * @param request.variation - Variation of the raw value to set the name for. The chain ID if the type is Ethereum address.
72
75
  */
73
76
  setName(request) {
74
77
  __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateSetNameRequest).call(this, request);
75
- const { value, type, name, sourceId } = request;
76
- __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_updateEntry).call(this, value, type, { name, sourceId: sourceId !== null && sourceId !== void 0 ? sourceId : null });
78
+ const { value, type, name, sourceId: requestSourceId, variation } = request;
79
+ const sourceId = requestSourceId !== null && requestSourceId !== void 0 ? requestSourceId : null;
80
+ __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_updateEntry).call(this, value, type, variation, (entry) => {
81
+ entry.name = name;
82
+ entry.sourceId = sourceId;
83
+ });
77
84
  }
78
85
  /**
79
86
  * Generate the proposed names for a value using the name providers and store them in the state.
@@ -82,13 +89,13 @@ class NameController extends base_controller_1.BaseControllerV2 {
82
89
  * @param request.value - Value to update the proposed names for.
83
90
  * @param request.type - Type of value to update the proposed names for.
84
91
  * @param request.sourceIds - Optional array of source IDs to limit which sources are used by the providers. If not provided, all sources in all providers will be used.
92
+ * @param request.variation - Variation of the raw value to update proposed names for. The chain ID if the type is Ethereum address.
85
93
  * @returns The updated proposed names for the value.
86
94
  */
87
95
  updateProposedNames(request) {
88
96
  return __awaiter(this, void 0, void 0, function* () {
89
97
  __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateUpdateProposedNamesRequest).call(this, request);
90
- const chainId = __classPrivateFieldGet(this, _NameController_getChainId, "f").call(this);
91
- const providerResponses = (yield Promise.all(__classPrivateFieldGet(this, _NameController_providers, "f").map((provider) => __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_getProviderResponse).call(this, request, chainId, provider)))).filter((response) => Boolean(response));
98
+ const providerResponses = (yield Promise.all(__classPrivateFieldGet(this, _NameController_providers, "f").map((provider) => __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_getProviderResponse).call(this, request, provider)))).filter((response) => Boolean(response));
92
99
  __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_updateProposedNameState).call(this, request, providerResponses);
93
100
  __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_updateSourceState).call(this, __classPrivateFieldGet(this, _NameController_providers, "f"));
94
101
  return __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_getUpdateProposedNamesResult).call(this, providerResponses);
@@ -96,30 +103,31 @@ class NameController extends base_controller_1.BaseControllerV2 {
96
103
  }
97
104
  }
98
105
  exports.NameController = NameController;
99
- _NameController_getChainId = new WeakMap(), _NameController_providers = new WeakMap(), _NameController_instances = new WeakSet(), _NameController_updateProposedNameState = function _NameController_updateProposedNameState(request, providerResponses) {
100
- var _a, _b, _c;
101
- const { value, type } = request;
102
- const newProposedNames = {};
103
- for (const providerResponse of providerResponses) {
104
- const { results, error: responseError } = providerResponse;
105
- if (responseError) {
106
- continue;
107
- }
108
- for (const sourceId of Object.keys(providerResponse.results)) {
109
- const result = results[sourceId];
110
- const { proposedNames } = result;
111
- let finalProposedNames = result.error ? null : proposedNames !== null && proposedNames !== void 0 ? proposedNames : [];
112
- if (finalProposedNames) {
113
- finalProposedNames = finalProposedNames.filter((proposedName) => proposedName === null || proposedName === void 0 ? void 0 : proposedName.length);
106
+ _NameController_providers = new WeakMap(), _NameController_updateDelay = new WeakMap(), _NameController_instances = new WeakSet(), _NameController_updateProposedNameState = function _NameController_updateProposedNameState(request, providerResponses) {
107
+ const { value, type, variation } = request;
108
+ const currentTime = __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_getCurrentTimeSeconds).call(this);
109
+ __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_updateEntry).call(this, value, type, variation, (entry) => {
110
+ var _a;
111
+ __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_removeDormantProposedNames).call(this, entry.proposedNames, type);
112
+ for (const providerResponse of providerResponses) {
113
+ const { results } = providerResponse;
114
+ for (const sourceId of Object.keys(providerResponse.results)) {
115
+ const result = results[sourceId];
116
+ const { proposedNames, updateDelay } = result;
117
+ const proposedNameEntry = (_a = entry.proposedNames[sourceId]) !== null && _a !== void 0 ? _a : {
118
+ proposedNames: [],
119
+ lastRequestTime: null,
120
+ updateDelay: null,
121
+ };
122
+ entry.proposedNames[sourceId] = proposedNameEntry;
123
+ if (proposedNames) {
124
+ proposedNameEntry.proposedNames = proposedNames;
125
+ }
126
+ proposedNameEntry.lastRequestTime = currentTime;
127
+ proposedNameEntry.updateDelay = updateDelay !== null && updateDelay !== void 0 ? updateDelay : null;
114
128
  }
115
- newProposedNames[sourceId] = finalProposedNames;
116
129
  }
117
- }
118
- const variationKey = __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_getTypeVariationKey).call(this, type);
119
- const existingProposedNames = (_c = (_b = (_a = this.state.names[type]) === null || _a === void 0 ? void 0 : _a[value]) === null || _b === void 0 ? void 0 : _b[variationKey]) === null || _c === void 0 ? void 0 : _c.proposedNames;
120
- const proposedNames = Object.assign(Object.assign({}, existingProposedNames), newProposedNames);
121
- const proposedNamesLastUpdated = __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_getCurrentTimeSeconds).call(this);
122
- __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_updateEntry).call(this, value, type, { proposedNames, proposedNamesLastUpdated });
130
+ });
123
131
  }, _NameController_updateSourceState = function _NameController_updateSourceState(providers) {
124
132
  const newNameSources = Object.assign({}, this.state.nameSources);
125
133
  for (const provider of providers) {
@@ -137,34 +145,46 @@ _NameController_getChainId = new WeakMap(), _NameController_providers = new Weak
137
145
  return providerResponses.reduce((acc, providerResponse) => {
138
146
  const { results } = providerResponse;
139
147
  for (const sourceId of Object.keys(results)) {
140
- const { proposedNames: resultProposedNames, error: resultError } = results[sourceId];
141
- let proposedNames = resultError
142
- ? undefined
143
- : resultProposedNames !== null && resultProposedNames !== void 0 ? resultProposedNames : [];
144
- if (proposedNames) {
145
- proposedNames = proposedNames.filter((proposedName) => proposedName === null || proposedName === void 0 ? void 0 : proposedName.length);
146
- }
148
+ const { proposedNames, error } = results[sourceId];
147
149
  acc.results[sourceId] = {
148
150
  proposedNames,
149
- error: resultError,
151
+ error,
150
152
  };
151
153
  }
152
154
  return acc;
153
155
  }, { results: {} });
154
- }, _NameController_getProviderResponse = function _NameController_getProviderResponse(request, chainId, provider) {
155
- var _a;
156
+ }, _NameController_getProviderResponse = function _NameController_getProviderResponse(request, provider) {
156
157
  return __awaiter(this, void 0, void 0, function* () {
157
- const { value, type, sourceIds: requestedSourceIds } = request;
158
+ const { value, type, sourceIds: requestedSourceIds, onlyUpdateAfterDelay, variation, } = request;
159
+ /* istanbul ignore next */
160
+ const variationKey = variation !== null && variation !== void 0 ? variation : DEFAULT_VARIATION;
158
161
  const supportedSourceIds = __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_getSourceIds).call(this, provider, type);
159
- const matchingSourceIds = (_a = requestedSourceIds === null || requestedSourceIds === void 0 ? void 0 : requestedSourceIds.filter((sourceId) => supportedSourceIds.includes(sourceId))) !== null && _a !== void 0 ? _a : supportedSourceIds;
160
- if (requestedSourceIds && !matchingSourceIds.length) {
162
+ const currentTime = __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_getCurrentTimeSeconds).call(this);
163
+ const normalizedValue = __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_normalizeValue).call(this, value, type);
164
+ const matchingSourceIds = supportedSourceIds.filter((sourceId) => {
165
+ var _a, _b, _c, _d, _e, _f, _g;
166
+ if (requestedSourceIds && !requestedSourceIds.includes(sourceId)) {
167
+ return false;
168
+ }
169
+ if (onlyUpdateAfterDelay) {
170
+ const entry = (_c = (_b = (_a = this.state.names[type]) === null || _a === void 0 ? void 0 : _a[normalizedValue]) === null || _b === void 0 ? void 0 : _b[variationKey]) !== null && _c !== void 0 ? _c : {};
171
+ const proposedNamesEntry = (_e = (_d = entry.proposedNames) === null || _d === void 0 ? void 0 : _d[sourceId]) !== null && _e !== void 0 ? _e : {};
172
+ const lastRequestTime = (_f = proposedNamesEntry.lastRequestTime) !== null && _f !== void 0 ? _f : 0;
173
+ const updateDelay = (_g = proposedNamesEntry.updateDelay) !== null && _g !== void 0 ? _g : __classPrivateFieldGet(this, _NameController_updateDelay, "f");
174
+ if (currentTime - lastRequestTime < updateDelay) {
175
+ return false;
176
+ }
177
+ }
178
+ return true;
179
+ });
180
+ if (!matchingSourceIds.length) {
161
181
  return undefined;
162
182
  }
163
183
  const providerRequest = {
164
- chainId,
165
- value,
184
+ value: __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_normalizeValue).call(this, value, type),
166
185
  type,
167
186
  sourceIds: requestedSourceIds ? matchingSourceIds : undefined,
187
+ variation: __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_normalizeVariation).call(this, variationKey, type),
168
188
  };
169
189
  let responseError;
170
190
  let response;
@@ -175,65 +195,86 @@ _NameController_getChainId = new WeakMap(), _NameController_providers = new Weak
175
195
  catch (error) {
176
196
  responseError = error;
177
197
  }
178
- let results = {};
179
- if (response === null || response === void 0 ? void 0 : response.results) {
180
- results = Object.keys(response.results).reduce((acc, sourceId) => {
181
- if (!requestedSourceIds || requestedSourceIds.includes(sourceId)) {
182
- acc[sourceId] = response.results[sourceId];
183
- }
184
- return acc;
185
- }, {});
186
- }
187
- if (responseError) {
188
- results = supportedSourceIds.reduce((acc, sourceId) => {
189
- acc[sourceId] = { proposedNames: [], error: responseError };
190
- return acc;
191
- }, {});
192
- }
193
- return { results, error: responseError };
198
+ return __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_normalizeProviderResult).call(this, response, responseError, matchingSourceIds);
194
199
  });
195
- }, _NameController_updateEntry = function _NameController_updateEntry(value, type, data) {
196
- const variationKey = __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_getTypeVariationKey).call(this, type);
200
+ }, _NameController_normalizeProviderResult = function _NameController_normalizeProviderResult(result, responseError, matchingSourceIds) {
201
+ const error = responseError !== null && responseError !== void 0 ? responseError : undefined;
202
+ const results = matchingSourceIds.reduce((acc, sourceId) => {
203
+ var _a;
204
+ const sourceResult = (_a = result === null || result === void 0 ? void 0 : result.results) === null || _a === void 0 ? void 0 : _a[sourceId];
205
+ const normalizedSourceResult = __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_normalizeProviderSourceResult).call(this, sourceResult, responseError);
206
+ return Object.assign(Object.assign({}, acc), { [sourceId]: normalizedSourceResult });
207
+ }, {});
208
+ return { results, error };
209
+ }, _NameController_normalizeProviderSourceResult = function _NameController_normalizeProviderSourceResult(result, responseError) {
210
+ var _a, _b, _c, _d;
211
+ const error = (_b = (_a = result === null || result === void 0 ? void 0 : result.error) !== null && _a !== void 0 ? _a : responseError) !== null && _b !== void 0 ? _b : undefined;
212
+ const updateDelay = (_c = result === null || result === void 0 ? void 0 : result.updateDelay) !== null && _c !== void 0 ? _c : undefined;
213
+ let proposedNames = error ? undefined : (_d = result === null || result === void 0 ? void 0 : result.proposedNames) !== null && _d !== void 0 ? _d : undefined;
214
+ if (proposedNames) {
215
+ proposedNames = proposedNames.filter((proposedName) => proposedName === null || proposedName === void 0 ? void 0 : proposedName.length);
216
+ }
217
+ return {
218
+ proposedNames,
219
+ error,
220
+ updateDelay,
221
+ };
222
+ }, _NameController_normalizeValue = function _NameController_normalizeValue(value, type) {
223
+ /* istanbul ignore next */
224
+ switch (type) {
225
+ case types_1.NameType.ETHEREUM_ADDRESS:
226
+ return value.toLowerCase();
227
+ default:
228
+ return value;
229
+ }
230
+ }, _NameController_normalizeVariation = function _NameController_normalizeVariation(variation, type) {
231
+ /* istanbul ignore next */
232
+ switch (type) {
233
+ case types_1.NameType.ETHEREUM_ADDRESS:
234
+ return variation.toLowerCase();
235
+ default:
236
+ return variation;
237
+ }
238
+ }, _NameController_updateEntry = function _NameController_updateEntry(value, type, variation, callback) {
239
+ /* istanbul ignore next */
240
+ const variationKey = variation !== null && variation !== void 0 ? variation : DEFAULT_VARIATION;
241
+ const normalizedValue = __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_normalizeValue).call(this, value, type);
242
+ const normalizedVariation = __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_normalizeVariation).call(this, variationKey, type);
197
243
  this.update((state) => {
198
244
  var _a;
199
245
  const typeEntries = state.names[type] || {};
200
246
  state.names[type] = typeEntries;
201
- const variationEntries = typeEntries[value] || {};
202
- typeEntries[value] = variationEntries;
203
- const currentEntry = (_a = variationEntries[variationKey]) !== null && _a !== void 0 ? _a : {
247
+ const variationEntries = typeEntries[normalizedValue] || {};
248
+ typeEntries[normalizedValue] = variationEntries;
249
+ const entry = (_a = variationEntries[normalizedVariation]) !== null && _a !== void 0 ? _a : {
204
250
  proposedNames: {},
205
- proposedNamesLastUpdated: null,
206
251
  name: null,
207
252
  sourceId: null,
208
253
  };
209
- const updatedEntry = Object.assign(Object.assign({}, currentEntry), data);
210
- variationEntries[variationKey] = updatedEntry;
254
+ variationEntries[normalizedVariation] = entry;
255
+ callback(entry);
211
256
  });
212
- }, _NameController_getTypeVariationKey = function _NameController_getTypeVariationKey(type) {
213
- switch (type) {
214
- default: {
215
- return __classPrivateFieldGet(this, _NameController_getChainId, "f").call(this);
216
- }
217
- }
218
257
  }, _NameController_getCurrentTimeSeconds = function _NameController_getCurrentTimeSeconds() {
219
258
  return Math.round(Date.now() / 1000);
220
259
  }, _NameController_validateSetNameRequest = function _NameController_validateSetNameRequest(request) {
221
- const { name, value, type, sourceId } = request;
260
+ const { name, value, type, sourceId, variation } = request;
222
261
  const errorMessages = [];
223
262
  __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateValue).call(this, value, errorMessages);
224
263
  __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateType).call(this, type, errorMessages);
225
264
  __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateName).call(this, name, errorMessages);
226
- __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateSourceId).call(this, sourceId, type, errorMessages);
265
+ __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateSourceId).call(this, sourceId, type, name, errorMessages);
266
+ __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateVariation).call(this, variation, type, errorMessages);
227
267
  if (errorMessages.length) {
228
268
  throw new Error(errorMessages.join(' '));
229
269
  }
230
270
  }, _NameController_validateUpdateProposedNamesRequest = function _NameController_validateUpdateProposedNamesRequest(request) {
231
- const { value, type, sourceIds } = request;
271
+ const { value, type, sourceIds, variation } = request;
232
272
  const errorMessages = [];
233
273
  __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateValue).call(this, value, errorMessages);
234
274
  __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateType).call(this, type, errorMessages);
235
275
  __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateSourceIds).call(this, sourceIds, type, errorMessages);
236
276
  __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateDuplicateSourceIds).call(this, type, errorMessages);
277
+ __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_validateVariation).call(this, variation, type, errorMessages);
237
278
  if (errorMessages.length) {
238
279
  throw new Error(errorMessages.join(' '));
239
280
  }
@@ -246,8 +287,11 @@ _NameController_getChainId = new WeakMap(), _NameController_providers = new Weak
246
287
  errorMessages.push(`Must specify one of the following types: ${Object.values(types_1.NameType).join(', ')}`);
247
288
  }
248
289
  }, _NameController_validateName = function _NameController_validateName(name, errorMessages) {
290
+ if (name === null) {
291
+ return;
292
+ }
249
293
  if (!(name === null || name === void 0 ? void 0 : name.length) || typeof name !== 'string') {
250
- errorMessages.push('Must specify a non-empty string for name.');
294
+ errorMessages.push('Must specify a non-empty string or null for name.');
251
295
  }
252
296
  }, _NameController_validateSourceIds = function _NameController_validateSourceIds(sourceIds, type, errorMessages) {
253
297
  if (!sourceIds) {
@@ -264,10 +308,14 @@ _NameController_getChainId = new WeakMap(), _NameController_providers = new Weak
264
308
  if (missingSourceIds.length) {
265
309
  errorMessages.push(`Unknown source IDs for type '${type}': ${missingSourceIds.join(', ')}`);
266
310
  }
267
- }, _NameController_validateSourceId = function _NameController_validateSourceId(sourceId, type, errorMessages) {
311
+ }, _NameController_validateSourceId = function _NameController_validateSourceId(sourceId, type, name, errorMessages) {
268
312
  if (sourceId === null || sourceId === undefined) {
269
313
  return;
270
314
  }
315
+ if (name === null) {
316
+ errorMessages.push(`Cannot specify a source ID when clearing the saved name: ${sourceId}`);
317
+ return;
318
+ }
271
319
  const allSourceIds = __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_getAllSourceIds).call(this, type);
272
320
  if (!sourceId.length || typeof sourceId !== 'string') {
273
321
  errorMessages.push('Must specify a non-empty string for sourceId.');
@@ -282,6 +330,15 @@ _NameController_getChainId = new WeakMap(), _NameController_providers = new Weak
282
330
  if (duplicateSourceIds.length) {
283
331
  errorMessages.push(`Duplicate source IDs found for type '${type}': ${duplicateSourceIds.join(', ')}`);
284
332
  }
333
+ }, _NameController_validateVariation = function _NameController_validateVariation(variation, type, errorMessages) {
334
+ if (type !== types_1.NameType.ETHEREUM_ADDRESS) {
335
+ return;
336
+ }
337
+ if (!(variation === null || variation === void 0 ? void 0 : variation.length) ||
338
+ typeof variation !== 'string' ||
339
+ !variation.match(/^0x[0-9A-Fa-f]+$/u)) {
340
+ errorMessages.push(`Must specify a chain ID in hexidecimal format for variation when using '${type}' type.`);
341
+ }
285
342
  }, _NameController_getAllSourceIds = function _NameController_getAllSourceIds(type) {
286
343
  return (__classPrivateFieldGet(this, _NameController_providers, "f")
287
344
  /* istanbul ignore next */
@@ -289,5 +346,14 @@ _NameController_getChainId = new WeakMap(), _NameController_providers = new Weak
289
346
  .flat());
290
347
  }, _NameController_getSourceIds = function _NameController_getSourceIds(provider, type) {
291
348
  return provider.getMetadata().sourceIds[type];
349
+ }, _NameController_removeDormantProposedNames = function _NameController_removeDormantProposedNames(proposedNames, type) {
350
+ if (Object.keys(proposedNames).length === 0) {
351
+ return;
352
+ }
353
+ const typeSourceIds = __classPrivateFieldGet(this, _NameController_instances, "m", _NameController_getAllSourceIds).call(this, type);
354
+ const dormantSourceIds = Object.keys(proposedNames).filter((sourceId) => !typeSourceIds.includes(sourceId));
355
+ for (const dormantSourceId of dormantSourceIds) {
356
+ delete proposedNames[dormantSourceId];
357
+ }
292
358
  };
293
359
  //# sourceMappingURL=NameController.js.map