@metamask-previews/address-book-controller 6.0.3-preview-7bb49dcb → 6.0.3-preview-748e5947

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
@@ -7,10 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Added
11
+
12
+ - Add `AddressBookControllerContactUpdatedEvent` and `AddressBookControllerContactDeletedEvent` types for contact events ([#5779](https://github.com/MetaMask/core/pull/5779))
13
+ - Add `list` method on `AddressBookController` to get all address book entries as an array ([#5779](https://github.com/MetaMask/core/pull/5779))
14
+ - Register message handlers for `list`, `set`, and `delete` actions ([#5779](https://github.com/MetaMask/core/pull/5779))
15
+
10
16
  ### Changed
11
17
 
12
18
  - Bump `@metamask/base-controller` from ^8.0.0 to ^8.0.1 ([#5722](https://github.com/MetaMask/core/pull/5722))
13
19
  - Bump `@metamask/controller-utils` to `^11.9.0` ([#5583](https://github.com/MetaMask/core/pull/5583), [#5765](https://github.com/MetaMask/core/pull/5765), [#5812](https://github.com/MetaMask/core/pull/5812))
20
+ - Modified `delete` method to clean up empty chainId objects ([#5779](https://github.com/MetaMask/core/pull/5779))
14
21
 
15
22
  ## [6.0.3]
16
23
 
@@ -1,4 +1,10 @@
1
1
  "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ 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");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var _AddressBookController_instances, _AddressBookController_registerMessageHandlers;
2
8
  Object.defineProperty(exports, "__esModule", { value: true });
3
9
  exports.AddressBookController = exports.getDefaultAddressBookControllerState = exports.controllerName = exports.AddressType = void 0;
4
10
  const base_controller_1 = require("@metamask/base-controller");
@@ -8,14 +14,8 @@ const controller_utils_1 = require("@metamask/controller-utils");
8
14
  */
9
15
  var AddressType;
10
16
  (function (AddressType) {
11
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
12
- // eslint-disable-next-line @typescript-eslint/naming-convention
13
17
  AddressType["externallyOwnedAccounts"] = "EXTERNALLY_OWNED_ACCOUNTS";
14
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
15
- // eslint-disable-next-line @typescript-eslint/naming-convention
16
18
  AddressType["contractAccounts"] = "CONTRACT_ACCOUNTS";
17
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
18
- // eslint-disable-next-line @typescript-eslint/naming-convention
19
19
  AddressType["nonAccounts"] = "NON_ACCOUNTS";
20
20
  })(AddressType || (exports.AddressType = AddressType = {}));
21
21
  /**
@@ -55,6 +55,25 @@ class AddressBookController extends base_controller_1.BaseController {
55
55
  name: exports.controllerName,
56
56
  state: mergedState,
57
57
  });
58
+ _AddressBookController_instances.add(this);
59
+ __classPrivateFieldGet(this, _AddressBookController_instances, "m", _AddressBookController_registerMessageHandlers).call(this);
60
+ }
61
+ /**
62
+ * Returns all address book entries as an array.
63
+ *
64
+ * @returns Array of all address book entries.
65
+ */
66
+ list() {
67
+ const { addressBook } = this.state;
68
+ const contacts = [];
69
+ Object.keys(addressBook).forEach((chainId) => {
70
+ const chainIdHex = chainId;
71
+ Object.keys(addressBook[chainIdHex]).forEach((address) => {
72
+ const contact = addressBook[chainIdHex][address];
73
+ contacts.push(contact);
74
+ });
75
+ });
76
+ return contacts;
58
77
  }
59
78
  /**
60
79
  * Remove all contract entries.
@@ -79,12 +98,20 @@ class AddressBookController extends base_controller_1.BaseController {
79
98
  !this.state.addressBook[chainId][address]) {
80
99
  return false;
81
100
  }
101
+ const deletedEntry = { ...this.state.addressBook[chainId][address] };
82
102
  this.update((state) => {
83
- delete state.addressBook[chainId][address];
84
- if (Object.keys(state.addressBook[chainId]).length === 0) {
85
- delete state.addressBook[chainId];
103
+ if (state.addressBook[chainId] && state.addressBook[chainId][address]) {
104
+ delete state.addressBook[chainId][address];
105
+ // Clean up empty chainId objects
106
+ if (Object.keys(state.addressBook[chainId]).length === 0) {
107
+ delete state.addressBook[chainId];
108
+ }
86
109
  }
87
110
  });
111
+ // Skip sending delete event for global contacts with chainId '*'
112
+ if (String(chainId) !== '*') {
113
+ this.messagingSystem.publish('AddressBookController:contactDeleted', deletedEntry);
114
+ }
88
115
  return true;
89
116
  }
90
117
  /**
@@ -109,6 +136,7 @@ class AddressBookController extends base_controller_1.BaseController {
109
136
  memo,
110
137
  name,
111
138
  addressType,
139
+ lastUpdatedAt: Date.now(),
112
140
  };
113
141
  const ensName = (0, controller_utils_1.normalizeEnsName)(name);
114
142
  if (ensName) {
@@ -124,9 +152,18 @@ class AddressBookController extends base_controller_1.BaseController {
124
152
  },
125
153
  };
126
154
  });
155
+ // Skip sending update event for global contacts with chainId '*'
156
+ if (String(chainId) !== '*') {
157
+ this.messagingSystem.publish('AddressBookController:contactUpdated', entry);
158
+ }
127
159
  return true;
128
160
  }
129
161
  }
130
162
  exports.AddressBookController = AddressBookController;
163
+ _AddressBookController_instances = new WeakSet(), _AddressBookController_registerMessageHandlers = function _AddressBookController_registerMessageHandlers() {
164
+ this.messagingSystem.registerActionHandler(`${exports.controllerName}:list`, this.list.bind(this));
165
+ this.messagingSystem.registerActionHandler(`${exports.controllerName}:set`, this.set.bind(this));
166
+ this.messagingSystem.registerActionHandler(`${exports.controllerName}:delete`, this.delete.bind(this));
167
+ };
131
168
  exports.default = AddressBookController;
132
169
  //# sourceMappingURL=AddressBookController.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"AddressBookController.cjs","sourceRoot":"","sources":["../src/AddressBookController.ts"],"names":[],"mappings":";;;AAKA,+DAA2D;AAC3D,iEAMoC;AAiBpC;;GAEG;AACH,IAAY,WAUX;AAVD,WAAY,WAAW;IACrB,gFAAgF;IAChF,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AK,CAAC,WAAW;gBACzB,CAAC,OAAO,CAAC,EAAE;oBACT,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,OAAO,CAAC;oBAClC,CAAC,OAAO,CAAC,EAAE,KAAK;iBACjB;aACF,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAlHD,sDAkHC;AAED,kBAAe,qBAAqB,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport {\n normalizeEnsName,\n isValidHexAddress,\n isSafeDynamicKey,\n toChecksumHexAddress,\n toHex,\n} from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\n/**\n * @type ContactEntry\n *\n * ContactEntry representation\n * @property address - Hex address of a recipient account\n * @property name - Nickname associated with this address\n * @property importTime - Data time when an account as created/imported\n */\nexport type ContactEntry = {\n address: string;\n name: string;\n importTime?: number;\n};\n\n/**\n * The type of address.\n */\nexport enum AddressType {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n externallyOwnedAccounts = 'EXTERNALLY_OWNED_ACCOUNTS',\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n contractAccounts = 'CONTRACT_ACCOUNTS',\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n nonAccounts = 'NON_ACCOUNTS',\n}\n\n/**\n * @type AddressBookEntry\n *\n * AddressBookEntry representation\n * @property address - Hex address of a recipient account\n * @property name - Nickname associated with this address\n * @property chainId - Chain id identifies the current chain\n * @property memo - User's note about address\n * @property isEns - is the entry an ENS name\n * @property addressType - is the type of this address\n */\nexport type AddressBookEntry = {\n address: string;\n name: string;\n chainId: Hex;\n memo: string;\n isEns: boolean;\n addressType?: AddressType;\n};\n\n/**\n * @type AddressBookState\n *\n * Address book controller state\n * @property addressBook - Array of contact entry objects\n */\nexport type AddressBookControllerState = {\n addressBook: { [chainId: Hex]: { [address: string]: AddressBookEntry } };\n};\n\n/**\n * The name of the {@link AddressBookController}.\n */\nexport const controllerName = 'AddressBookController';\n\n/**\n * The action that can be performed to get the state of the {@link AddressBookController}.\n */\nexport type AddressBookControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AddressBookControllerState\n>;\n\n/**\n * The actions that can be performed using the {@link AddressBookController}.\n */\nexport type AddressBookControllerActions = AddressBookControllerGetStateAction;\n\n/**\n * The event that {@link AddressBookController} can emit.\n */\nexport type AddressBookControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AddressBookControllerState\n>;\n\n/**\n * The events that {@link AddressBookController} can emit.\n */\nexport type AddressBookControllerEvents = AddressBookControllerStateChangeEvent;\n\nconst addressBookControllerMetadata = {\n addressBook: { persist: true, anonymous: false },\n};\n\n/**\n * Get the default {@link AddressBookController} state.\n *\n * @returns The default {@link AddressBookController} state.\n */\nexport const getDefaultAddressBookControllerState =\n (): AddressBookControllerState => {\n return {\n addressBook: {},\n };\n };\n\n/**\n * The messenger of the {@link AddressBookController} for communication.\n */\nexport type AddressBookControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n AddressBookControllerActions,\n AddressBookControllerEvents,\n never,\n never\n>;\n\n/**\n * Controller that manages a list of recipient addresses associated with nicknames.\n */\nexport class AddressBookController extends BaseController<\n typeof controllerName,\n AddressBookControllerState,\n AddressBookControllerMessenger\n> {\n /**\n * Creates an AddressBookController instance.\n *\n * @param args - The {@link AddressBookController} arguments.\n * @param args.messenger - The controller messenger instance for communication.\n * @param args.state - Initial state to set on this controller.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: AddressBookControllerMessenger;\n state?: Partial<AddressBookControllerState>;\n }) {\n const mergedState = { ...getDefaultAddressBookControllerState(), ...state };\n super({\n messenger,\n metadata: addressBookControllerMetadata,\n name: controllerName,\n state: mergedState,\n });\n }\n\n /**\n * Remove all contract entries.\n */\n clear() {\n this.update((state) => {\n state.addressBook = {};\n });\n }\n\n /**\n * Remove a contract entry by address.\n *\n * @param chainId - Chain id identifies the current chain.\n * @param address - Recipient address to delete.\n * @returns Whether the entry was deleted.\n */\n delete(chainId: Hex, address: string) {\n address = toChecksumHexAddress(address);\n if (\n ![chainId, address].every((key) => isSafeDynamicKey(key)) ||\n !isValidHexAddress(address) ||\n !this.state.addressBook[chainId] ||\n !this.state.addressBook[chainId][address]\n ) {\n return false;\n }\n\n this.update((state) => {\n delete state.addressBook[chainId][address];\n if (Object.keys(state.addressBook[chainId]).length === 0) {\n delete state.addressBook[chainId];\n }\n });\n\n return true;\n }\n\n /**\n * Add or update a contact entry by address.\n *\n * @param address - Recipient address to add or update.\n * @param name - Nickname to associate with this address.\n * @param chainId - Chain id identifies the current chain.\n * @param memo - User's note about address.\n * @param addressType - Contact's address type.\n * @returns Boolean indicating if the address was successfully set.\n */\n set(\n address: string,\n name: string,\n chainId = toHex(1),\n memo = '',\n addressType?: AddressType,\n ) {\n address = toChecksumHexAddress(address);\n if (!isValidHexAddress(address)) {\n return false;\n }\n\n const entry = {\n address,\n chainId,\n isEns: false,\n memo,\n name,\n addressType,\n };\n\n const ensName = normalizeEnsName(name);\n if (ensName) {\n entry.name = ensName;\n entry.isEns = true;\n }\n\n this.update((state) => {\n state.addressBook = {\n ...this.state.addressBook,\n [chainId]: {\n ...this.state.addressBook[chainId],\n [address]: entry,\n },\n };\n });\n\n return true;\n }\n}\n\nexport default AddressBookController;\n"]}
1
+ {"version":3,"file":"AddressBookController.cjs","sourceRoot":"","sources":["../src/AddressBookController.ts"],"names":[],"mappings":";;;;;;;;;AAKA,+DAA2D;AAC3D,iEAMoC;AAGpC;;GAEG;AACH,IAAY,WAIX;AAJD,WAAY,WAAW;IACrB,oEAAqD,CAAA;IACrD,qDAAsC,CAAA;IACtC,2CAA4B,CAAA;AAC9B,CAAC,EA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,CAAC;SACH;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CAmBF;AAhLD,sDAgLC;;IAbG,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,sBAAc,OAAO,EACxB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CACrB,CAAC;IACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,sBAAc,MAAM,EACvB,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CACpB,CAAC;IACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,sBAAc,SAAS,EAC1B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CACvB,CAAC;AACJ,CAAC;AAGH,kBAAe,qBAAqB,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport {\n normalizeEnsName,\n isValidHexAddress,\n isSafeDynamicKey,\n toChecksumHexAddress,\n toHex,\n} from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\n/**\n * The type of address.\n */\nexport enum AddressType {\n externallyOwnedAccounts = 'EXTERNALLY_OWNED_ACCOUNTS',\n contractAccounts = 'CONTRACT_ACCOUNTS',\n nonAccounts = 'NON_ACCOUNTS',\n}\n\n/**\n * AddressBookEntry\n *\n * AddressBookEntry representation\n *\n * address - Hex address of a recipient account\n *\n * name - Nickname associated with this address\n *\n * chainId - Chain id identifies the current chain\n *\n * memo - User's note about address\n *\n * isEns - is the entry an ENS name\n *\n * addressType - is the type of this address\n *\n * lastUpdatedAt - timestamp of when this entry was last updated\n */\nexport type AddressBookEntry = {\n address: string;\n name: string;\n chainId: Hex;\n memo: string;\n isEns: boolean;\n addressType?: AddressType;\n lastUpdatedAt?: number;\n};\n\n/**\n * AddressBookState\n *\n * Address book controller state\n *\n * addressBook - Array of contact entry objects\n */\nexport type AddressBookControllerState = {\n addressBook: { [chainId: Hex]: { [address: string]: AddressBookEntry } };\n};\n\n/**\n * The name of the {@link AddressBookController}.\n */\nexport const controllerName = 'AddressBookController';\n\n/**\n * The action that can be performed to get the state of the {@link AddressBookController}.\n */\nexport type AddressBookControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AddressBookControllerState\n>;\n\n/**\n * The action that can be performed to list contacts from the {@link AddressBookController}.\n */\nexport type AddressBookControllerListAction = {\n type: `${typeof controllerName}:list`;\n handler: AddressBookController['list'];\n};\n\n/**\n * The action that can be performed to set a contact in the {@link AddressBookController}.\n */\nexport type AddressBookControllerSetAction = {\n type: `${typeof controllerName}:set`;\n handler: AddressBookController['set'];\n};\n\n/**\n * The action that can be performed to delete a contact from the {@link AddressBookController}.\n */\nexport type AddressBookControllerDeleteAction = {\n type: `${typeof controllerName}:delete`;\n handler: AddressBookController['delete'];\n};\n\n/**\n * Event emitted when a contact is added or updated\n */\nexport type AddressBookControllerContactUpdatedEvent = {\n type: `${typeof controllerName}:contactUpdated`;\n payload: [AddressBookEntry];\n};\n\n/**\n * Event emitted when a contact is deleted\n */\nexport type AddressBookControllerContactDeletedEvent = {\n type: `${typeof controllerName}:contactDeleted`;\n payload: [AddressBookEntry];\n};\n\n/**\n * The actions that can be performed using the {@link AddressBookController}.\n */\nexport type AddressBookControllerActions =\n | AddressBookControllerGetStateAction\n | AddressBookControllerListAction\n | AddressBookControllerSetAction\n | AddressBookControllerDeleteAction;\n\n/**\n * The event that {@link AddressBookController} can emit.\n */\nexport type AddressBookControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AddressBookControllerState\n>;\n\n/**\n * The events that {@link AddressBookController} can emit.\n */\nexport type AddressBookControllerEvents =\n | AddressBookControllerStateChangeEvent\n | AddressBookControllerContactUpdatedEvent\n | AddressBookControllerContactDeletedEvent;\n\nconst addressBookControllerMetadata = {\n addressBook: { persist: true, anonymous: false },\n};\n\n/**\n * Get the default {@link AddressBookController} state.\n *\n * @returns The default {@link AddressBookController} state.\n */\nexport const getDefaultAddressBookControllerState =\n (): AddressBookControllerState => {\n return {\n addressBook: {},\n };\n };\n\n/**\n * The messenger of the {@link AddressBookController} for communication.\n */\nexport type AddressBookControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n AddressBookControllerActions,\n AddressBookControllerEvents,\n never,\n never\n>;\n\n/**\n * Controller that manages a list of recipient addresses associated with nicknames.\n */\nexport class AddressBookController extends BaseController<\n typeof controllerName,\n AddressBookControllerState,\n AddressBookControllerMessenger\n> {\n /**\n * Creates an AddressBookController instance.\n *\n * @param args - The {@link AddressBookController} arguments.\n * @param args.messenger - The controller messenger instance for communication.\n * @param args.state - Initial state to set on this controller.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: AddressBookControllerMessenger;\n state?: Partial<AddressBookControllerState>;\n }) {\n const mergedState = { ...getDefaultAddressBookControllerState(), ...state };\n super({\n messenger,\n metadata: addressBookControllerMetadata,\n name: controllerName,\n state: mergedState,\n });\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Returns all address book entries as an array.\n *\n * @returns Array of all address book entries.\n */\n list(): AddressBookEntry[] {\n const { addressBook } = this.state;\n const contacts: AddressBookEntry[] = [];\n\n Object.keys(addressBook).forEach((chainId) => {\n const chainIdHex = chainId as Hex;\n Object.keys(addressBook[chainIdHex]).forEach((address) => {\n const contact = addressBook[chainIdHex][address];\n contacts.push(contact);\n });\n });\n\n return contacts;\n }\n\n /**\n * Remove all contract entries.\n */\n clear() {\n this.update((state) => {\n state.addressBook = {};\n });\n }\n\n /**\n * Remove a contract entry by address.\n *\n * @param chainId - Chain id identifies the current chain.\n * @param address - Recipient address to delete.\n * @returns Whether the entry was deleted.\n */\n delete(chainId: Hex, address: string) {\n address = toChecksumHexAddress(address);\n if (\n ![chainId, address].every((key) => isSafeDynamicKey(key)) ||\n !isValidHexAddress(address) ||\n !this.state.addressBook[chainId] ||\n !this.state.addressBook[chainId][address]\n ) {\n return false;\n }\n\n const deletedEntry = { ...this.state.addressBook[chainId][address] };\n\n this.update((state) => {\n if (state.addressBook[chainId] && state.addressBook[chainId][address]) {\n delete state.addressBook[chainId][address];\n\n // Clean up empty chainId objects\n if (Object.keys(state.addressBook[chainId]).length === 0) {\n delete state.addressBook[chainId];\n }\n }\n });\n\n // Skip sending delete event for global contacts with chainId '*'\n if (String(chainId) !== '*') {\n this.messagingSystem.publish(\n 'AddressBookController:contactDeleted',\n deletedEntry,\n );\n }\n\n return true;\n }\n\n /**\n * Add or update a contact entry by address.\n *\n * @param address - Recipient address to add or update.\n * @param name - Nickname to associate with this address.\n * @param chainId - Chain id identifies the current chain.\n * @param memo - User's note about address.\n * @param addressType - Contact's address type.\n * @returns Boolean indicating if the address was successfully set.\n */\n set(\n address: string,\n name: string,\n chainId = toHex(1),\n memo = '',\n addressType?: AddressType,\n ) {\n address = toChecksumHexAddress(address);\n if (!isValidHexAddress(address)) {\n return false;\n }\n\n const entry = {\n address,\n chainId,\n isEns: false,\n memo,\n name,\n addressType,\n lastUpdatedAt: Date.now(),\n };\n const ensName = normalizeEnsName(name);\n if (ensName) {\n entry.name = ensName;\n entry.isEns = true;\n }\n\n this.update((state) => {\n state.addressBook = {\n ...this.state.addressBook,\n [chainId]: {\n ...this.state.addressBook[chainId],\n [address]: entry,\n },\n };\n });\n\n // Skip sending update event for global contacts with chainId '*'\n if (String(chainId) !== '*') {\n this.messagingSystem.publish(\n 'AddressBookController:contactUpdated',\n entry,\n );\n }\n\n return true;\n }\n\n /**\n * Registers message handlers for the AddressBookController.\n */\n #registerMessageHandlers() {\n this.messagingSystem.registerActionHandler(\n `${controllerName}:list`,\n this.list.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${controllerName}:set`,\n this.set.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${controllerName}:delete`,\n this.delete.bind(this),\n );\n }\n}\n\nexport default AddressBookController;\n"]}
@@ -1,19 +1,6 @@
1
1
  import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
2
2
  import { BaseController } from "@metamask/base-controller";
3
3
  import type { Hex } from "@metamask/utils";
4
- /**
5
- * @type ContactEntry
6
- *
7
- * ContactEntry representation
8
- * @property address - Hex address of a recipient account
9
- * @property name - Nickname associated with this address
10
- * @property importTime - Data time when an account as created/imported
11
- */
12
- export type ContactEntry = {
13
- address: string;
14
- name: string;
15
- importTime?: number;
16
- };
17
4
  /**
18
5
  * The type of address.
19
6
  */
@@ -23,15 +10,23 @@ export declare enum AddressType {
23
10
  nonAccounts = "NON_ACCOUNTS"
24
11
  }
25
12
  /**
26
- * @type AddressBookEntry
13
+ * AddressBookEntry
27
14
  *
28
15
  * AddressBookEntry representation
29
- * @property address - Hex address of a recipient account
30
- * @property name - Nickname associated with this address
31
- * @property chainId - Chain id identifies the current chain
32
- * @property memo - User's note about address
33
- * @property isEns - is the entry an ENS name
34
- * @property addressType - is the type of this address
16
+ *
17
+ * address - Hex address of a recipient account
18
+ *
19
+ * name - Nickname associated with this address
20
+ *
21
+ * chainId - Chain id identifies the current chain
22
+ *
23
+ * memo - User's note about address
24
+ *
25
+ * isEns - is the entry an ENS name
26
+ *
27
+ * addressType - is the type of this address
28
+ *
29
+ * lastUpdatedAt - timestamp of when this entry was last updated
35
30
  */
36
31
  export type AddressBookEntry = {
37
32
  address: string;
@@ -40,12 +35,14 @@ export type AddressBookEntry = {
40
35
  memo: string;
41
36
  isEns: boolean;
42
37
  addressType?: AddressType;
38
+ lastUpdatedAt?: number;
43
39
  };
44
40
  /**
45
- * @type AddressBookState
41
+ * AddressBookState
46
42
  *
47
43
  * Address book controller state
48
- * @property addressBook - Array of contact entry objects
44
+ *
45
+ * addressBook - Array of contact entry objects
49
46
  */
50
47
  export type AddressBookControllerState = {
51
48
  addressBook: {
@@ -62,10 +59,45 @@ export declare const controllerName = "AddressBookController";
62
59
  * The action that can be performed to get the state of the {@link AddressBookController}.
63
60
  */
64
61
  export type AddressBookControllerGetStateAction = ControllerGetStateAction<typeof controllerName, AddressBookControllerState>;
62
+ /**
63
+ * The action that can be performed to list contacts from the {@link AddressBookController}.
64
+ */
65
+ export type AddressBookControllerListAction = {
66
+ type: `${typeof controllerName}:list`;
67
+ handler: AddressBookController['list'];
68
+ };
69
+ /**
70
+ * The action that can be performed to set a contact in the {@link AddressBookController}.
71
+ */
72
+ export type AddressBookControllerSetAction = {
73
+ type: `${typeof controllerName}:set`;
74
+ handler: AddressBookController['set'];
75
+ };
76
+ /**
77
+ * The action that can be performed to delete a contact from the {@link AddressBookController}.
78
+ */
79
+ export type AddressBookControllerDeleteAction = {
80
+ type: `${typeof controllerName}:delete`;
81
+ handler: AddressBookController['delete'];
82
+ };
83
+ /**
84
+ * Event emitted when a contact is added or updated
85
+ */
86
+ export type AddressBookControllerContactUpdatedEvent = {
87
+ type: `${typeof controllerName}:contactUpdated`;
88
+ payload: [AddressBookEntry];
89
+ };
90
+ /**
91
+ * Event emitted when a contact is deleted
92
+ */
93
+ export type AddressBookControllerContactDeletedEvent = {
94
+ type: `${typeof controllerName}:contactDeleted`;
95
+ payload: [AddressBookEntry];
96
+ };
65
97
  /**
66
98
  * The actions that can be performed using the {@link AddressBookController}.
67
99
  */
68
- export type AddressBookControllerActions = AddressBookControllerGetStateAction;
100
+ export type AddressBookControllerActions = AddressBookControllerGetStateAction | AddressBookControllerListAction | AddressBookControllerSetAction | AddressBookControllerDeleteAction;
69
101
  /**
70
102
  * The event that {@link AddressBookController} can emit.
71
103
  */
@@ -73,7 +105,7 @@ export type AddressBookControllerStateChangeEvent = ControllerStateChangeEvent<t
73
105
  /**
74
106
  * The events that {@link AddressBookController} can emit.
75
107
  */
76
- export type AddressBookControllerEvents = AddressBookControllerStateChangeEvent;
108
+ export type AddressBookControllerEvents = AddressBookControllerStateChangeEvent | AddressBookControllerContactUpdatedEvent | AddressBookControllerContactDeletedEvent;
77
109
  /**
78
110
  * Get the default {@link AddressBookController} state.
79
111
  *
@@ -88,6 +120,7 @@ export type AddressBookControllerMessenger = RestrictedMessenger<typeof controll
88
120
  * Controller that manages a list of recipient addresses associated with nicknames.
89
121
  */
90
122
  export declare class AddressBookController extends BaseController<typeof controllerName, AddressBookControllerState, AddressBookControllerMessenger> {
123
+ #private;
91
124
  /**
92
125
  * Creates an AddressBookController instance.
93
126
  *
@@ -99,6 +132,12 @@ export declare class AddressBookController extends BaseController<typeof control
99
132
  messenger: AddressBookControllerMessenger;
100
133
  state?: Partial<AddressBookControllerState>;
101
134
  });
135
+ /**
136
+ * Returns all address book entries as an array.
137
+ *
138
+ * @returns Array of all address book entries.
139
+ */
140
+ list(): AddressBookEntry[];
102
141
  /**
103
142
  * Remove all contract entries.
104
143
  */
@@ -1 +1 @@
1
- {"version":3,"file":"AddressBookController.d.cts","sourceRoot":"","sources":["../src/AddressBookController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EACpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAQ3D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C;;;;;;;GAOG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;GAEG;AACH,oBAAY,WAAW;IAGrB,uBAAuB,8BAA8B;IAGrD,gBAAgB,sBAAsB;IAGtC,WAAW,iBAAiB;CAC7B;AAED;;;;;;;;;;GAUG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,GAAG,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,OAAO,CAAC;IACf,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,0BAA0B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C;QAC1C,KAAK,CAAC,EAAE,OAAO,CAAC,0BAA0B,CAAC,CAAC;KAC7C;IAYD;;;;OAIG;IACH,IAAI,IAAI,gBAAgB,EAAE;IAe1B;;OAEG;IACH,KAAK;IAML;;;;;;OAMG;IACH,MAAM,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM;IAmCpC;;;;;;;;;OASG;IACH,GAAG,CACD,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,OAAO,gBAAW,EAClB,IAAI,SAAK,EACT,WAAW,CAAC,EAAE,WAAW;CA4D5B;AAED,eAAe,qBAAqB,CAAC"}
@@ -1,19 +1,6 @@
1
1
  import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
2
2
  import { BaseController } from "@metamask/base-controller";
3
3
  import type { Hex } from "@metamask/utils";
4
- /**
5
- * @type ContactEntry
6
- *
7
- * ContactEntry representation
8
- * @property address - Hex address of a recipient account
9
- * @property name - Nickname associated with this address
10
- * @property importTime - Data time when an account as created/imported
11
- */
12
- export type ContactEntry = {
13
- address: string;
14
- name: string;
15
- importTime?: number;
16
- };
17
4
  /**
18
5
  * The type of address.
19
6
  */
@@ -23,15 +10,23 @@ export declare enum AddressType {
23
10
  nonAccounts = "NON_ACCOUNTS"
24
11
  }
25
12
  /**
26
- * @type AddressBookEntry
13
+ * AddressBookEntry
27
14
  *
28
15
  * AddressBookEntry representation
29
- * @property address - Hex address of a recipient account
30
- * @property name - Nickname associated with this address
31
- * @property chainId - Chain id identifies the current chain
32
- * @property memo - User's note about address
33
- * @property isEns - is the entry an ENS name
34
- * @property addressType - is the type of this address
16
+ *
17
+ * address - Hex address of a recipient account
18
+ *
19
+ * name - Nickname associated with this address
20
+ *
21
+ * chainId - Chain id identifies the current chain
22
+ *
23
+ * memo - User's note about address
24
+ *
25
+ * isEns - is the entry an ENS name
26
+ *
27
+ * addressType - is the type of this address
28
+ *
29
+ * lastUpdatedAt - timestamp of when this entry was last updated
35
30
  */
36
31
  export type AddressBookEntry = {
37
32
  address: string;
@@ -40,12 +35,14 @@ export type AddressBookEntry = {
40
35
  memo: string;
41
36
  isEns: boolean;
42
37
  addressType?: AddressType;
38
+ lastUpdatedAt?: number;
43
39
  };
44
40
  /**
45
- * @type AddressBookState
41
+ * AddressBookState
46
42
  *
47
43
  * Address book controller state
48
- * @property addressBook - Array of contact entry objects
44
+ *
45
+ * addressBook - Array of contact entry objects
49
46
  */
50
47
  export type AddressBookControllerState = {
51
48
  addressBook: {
@@ -62,10 +59,45 @@ export declare const controllerName = "AddressBookController";
62
59
  * The action that can be performed to get the state of the {@link AddressBookController}.
63
60
  */
64
61
  export type AddressBookControllerGetStateAction = ControllerGetStateAction<typeof controllerName, AddressBookControllerState>;
62
+ /**
63
+ * The action that can be performed to list contacts from the {@link AddressBookController}.
64
+ */
65
+ export type AddressBookControllerListAction = {
66
+ type: `${typeof controllerName}:list`;
67
+ handler: AddressBookController['list'];
68
+ };
69
+ /**
70
+ * The action that can be performed to set a contact in the {@link AddressBookController}.
71
+ */
72
+ export type AddressBookControllerSetAction = {
73
+ type: `${typeof controllerName}:set`;
74
+ handler: AddressBookController['set'];
75
+ };
76
+ /**
77
+ * The action that can be performed to delete a contact from the {@link AddressBookController}.
78
+ */
79
+ export type AddressBookControllerDeleteAction = {
80
+ type: `${typeof controllerName}:delete`;
81
+ handler: AddressBookController['delete'];
82
+ };
83
+ /**
84
+ * Event emitted when a contact is added or updated
85
+ */
86
+ export type AddressBookControllerContactUpdatedEvent = {
87
+ type: `${typeof controllerName}:contactUpdated`;
88
+ payload: [AddressBookEntry];
89
+ };
90
+ /**
91
+ * Event emitted when a contact is deleted
92
+ */
93
+ export type AddressBookControllerContactDeletedEvent = {
94
+ type: `${typeof controllerName}:contactDeleted`;
95
+ payload: [AddressBookEntry];
96
+ };
65
97
  /**
66
98
  * The actions that can be performed using the {@link AddressBookController}.
67
99
  */
68
- export type AddressBookControllerActions = AddressBookControllerGetStateAction;
100
+ export type AddressBookControllerActions = AddressBookControllerGetStateAction | AddressBookControllerListAction | AddressBookControllerSetAction | AddressBookControllerDeleteAction;
69
101
  /**
70
102
  * The event that {@link AddressBookController} can emit.
71
103
  */
@@ -73,7 +105,7 @@ export type AddressBookControllerStateChangeEvent = ControllerStateChangeEvent<t
73
105
  /**
74
106
  * The events that {@link AddressBookController} can emit.
75
107
  */
76
- export type AddressBookControllerEvents = AddressBookControllerStateChangeEvent;
108
+ export type AddressBookControllerEvents = AddressBookControllerStateChangeEvent | AddressBookControllerContactUpdatedEvent | AddressBookControllerContactDeletedEvent;
77
109
  /**
78
110
  * Get the default {@link AddressBookController} state.
79
111
  *
@@ -88,6 +120,7 @@ export type AddressBookControllerMessenger = RestrictedMessenger<typeof controll
88
120
  * Controller that manages a list of recipient addresses associated with nicknames.
89
121
  */
90
122
  export declare class AddressBookController extends BaseController<typeof controllerName, AddressBookControllerState, AddressBookControllerMessenger> {
123
+ #private;
91
124
  /**
92
125
  * Creates an AddressBookController instance.
93
126
  *
@@ -99,6 +132,12 @@ export declare class AddressBookController extends BaseController<typeof control
99
132
  messenger: AddressBookControllerMessenger;
100
133
  state?: Partial<AddressBookControllerState>;
101
134
  });
135
+ /**
136
+ * Returns all address book entries as an array.
137
+ *
138
+ * @returns Array of all address book entries.
139
+ */
140
+ list(): AddressBookEntry[];
102
141
  /**
103
142
  * Remove all contract entries.
104
143
  */
@@ -1 +1 @@
1
- {"version":3,"file":"AddressBookController.d.mts","sourceRoot":"","sources":["../src/AddressBookController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EACpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAQ3D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C;;;;;;;GAOG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,C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c,EACrB,0BAA0B,EAC1B,8BAA8B,CAC/B;IACC;;;;;;OAMG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,8BAA8B,CAAC;QAC1C,KAAK,CAAC,EAAE,OAAO,CAAC,0BAA0B,CAAC,CAAC;KAC7C;IAUD;;OAEG;IACH,KAAK;IAML;;;;;;OAMG;IACH,MAAM,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM;IAqBpC;;;;;;;;;OASG;IACH,GAAG,CACD,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,OAAO,gBAAW,EAClB,IAAI,SAAK,EACT,WAAW,CAAC,EAAE,WAAW;CAkC5B;AAED,eAAe,qBAAqB,CAAC"}
1
+ {"version":3,"file":"AddressBookController.d.mts","sourceRoot":"","sources":["../src/AddressBookController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EACpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAQ3D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C;;GAEG;AACH,oBAAY,WAAW;IACrB,uBAAuB,8BAA8B;IACrD,gBAAgB,sBAAsB;IACtC,WAAW,iBAAiB;CAC7B;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,GAAG,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,OAAO,CAAC;IACf,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACvC,WAAW,EAAE;QAAE,CAAC,OAAO,EAAE,GAAG,GAAG;YAAE,CAAC,OAA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AM,2BAA2B,GACnC,qCAAqC,GACrC,wCAAwC,GACxC,wCAAwC,CAAC;AAM7C;;;;GAIG;AACH,eAAO,MAAM,oCAAoC,QAC3C,0BAIH,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,8BAA8B,GAAG,mBAAmB,CAC9D,OAAO,cAAc,EACrB,4BAA4B,EAC5B,2BAA2B,EAC3B,KAAK,EACL,KAAK,CACN,CAAC;AAEF;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,cAAc,CACvD,OAAO,cAAc,EACrB,0BAA0B,EAC1B,8BAA8B,CAC/B;;IACC;;;;;;OAMG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,8BAA8B,CAAC;QAC1C,KAAK,CAAC,EAAE,OAAO,CAAC,0BAA0B,CAAC,CAAC;KAC7C;IAYD;;;;OAIG;IACH,IAAI,IAAI,gBAAgB,EAAE;IAe1B;;OAEG;IACH,KAAK;IAML;;;;;;OAMG;IACH,MAAM,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM;IAmCpC;;;;;;;;;OASG;IACH,GAAG,CACD,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,OAAO,gBAAW,EAClB,IAAI,SAAK,EACT,WAAW,CAAC,EAAE,WAAW;CA4D5B;AAED,eAAe,qBAAqB,CAAC"}
@@ -1,3 +1,9 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ 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");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var _AddressBookController_instances, _AddressBookController_registerMessageHandlers;
1
7
  import { BaseController } from "@metamask/base-controller";
2
8
  import { normalizeEnsName, isValidHexAddress, isSafeDynamicKey, toChecksumHexAddress, toHex } from "@metamask/controller-utils";
3
9
  /**
@@ -5,14 +11,8 @@ import { normalizeEnsName, isValidHexAddress, isSafeDynamicKey, toChecksumHexAdd
5
11
  */
6
12
  export var AddressType;
7
13
  (function (AddressType) {
8
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
9
- // eslint-disable-next-line @typescript-eslint/naming-convention
10
14
  AddressType["externallyOwnedAccounts"] = "EXTERNALLY_OWNED_ACCOUNTS";
11
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
12
- // eslint-disable-next-line @typescript-eslint/naming-convention
13
15
  AddressType["contractAccounts"] = "CONTRACT_ACCOUNTS";
14
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
15
- // eslint-disable-next-line @typescript-eslint/naming-convention
16
16
  AddressType["nonAccounts"] = "NON_ACCOUNTS";
17
17
  })(AddressType || (AddressType = {}));
18
18
  /**
@@ -51,6 +51,25 @@ export class AddressBookController extends BaseController {
51
51
  name: controllerName,
52
52
  state: mergedState,
53
53
  });
54
+ _AddressBookController_instances.add(this);
55
+ __classPrivateFieldGet(this, _AddressBookController_instances, "m", _AddressBookController_registerMessageHandlers).call(this);
56
+ }
57
+ /**
58
+ * Returns all address book entries as an array.
59
+ *
60
+ * @returns Array of all address book entries.
61
+ */
62
+ list() {
63
+ const { addressBook } = this.state;
64
+ const contacts = [];
65
+ Object.keys(addressBook).forEach((chainId) => {
66
+ const chainIdHex = chainId;
67
+ Object.keys(addressBook[chainIdHex]).forEach((address) => {
68
+ const contact = addressBook[chainIdHex][address];
69
+ contacts.push(contact);
70
+ });
71
+ });
72
+ return contacts;
54
73
  }
55
74
  /**
56
75
  * Remove all contract entries.
@@ -75,12 +94,20 @@ export class AddressBookController extends BaseController {
75
94
  !this.state.addressBook[chainId][address]) {
76
95
  return false;
77
96
  }
97
+ const deletedEntry = { ...this.state.addressBook[chainId][address] };
78
98
  this.update((state) => {
79
- delete state.addressBook[chainId][address];
80
- if (Object.keys(state.addressBook[chainId]).length === 0) {
81
- delete state.addressBook[chainId];
99
+ if (state.addressBook[chainId] && state.addressBook[chainId][address]) {
100
+ delete state.addressBook[chainId][address];
101
+ // Clean up empty chainId objects
102
+ if (Object.keys(state.addressBook[chainId]).length === 0) {
103
+ delete state.addressBook[chainId];
104
+ }
82
105
  }
83
106
  });
107
+ // Skip sending delete event for global contacts with chainId '*'
108
+ if (String(chainId) !== '*') {
109
+ this.messagingSystem.publish('AddressBookController:contactDeleted', deletedEntry);
110
+ }
84
111
  return true;
85
112
  }
86
113
  /**
@@ -105,6 +132,7 @@ export class AddressBookController extends BaseController {
105
132
  memo,
106
133
  name,
107
134
  addressType,
135
+ lastUpdatedAt: Date.now(),
108
136
  };
109
137
  const ensName = normalizeEnsName(name);
110
138
  if (ensName) {
@@ -120,8 +148,17 @@ export class AddressBookController extends BaseController {
120
148
  },
121
149
  };
122
150
  });
151
+ // Skip sending update event for global contacts with chainId '*'
152
+ if (String(chainId) !== '*') {
153
+ this.messagingSystem.publish('AddressBookController:contactUpdated', entry);
154
+ }
123
155
  return true;
124
156
  }
125
157
  }
158
+ _AddressBookController_instances = new WeakSet(), _AddressBookController_registerMessageHandlers = function _AddressBookController_registerMessageHandlers() {
159
+ this.messagingSystem.registerActionHandler(`${controllerName}:list`, this.list.bind(this));
160
+ this.messagingSystem.registerActionHandler(`${controllerName}:set`, this.set.bind(this));
161
+ this.messagingSystem.registerActionHandler(`${controllerName}:delete`, this.delete.bind(this));
162
+ };
126
163
  export default AddressBookController;
127
164
  //# sourceMappingURL=AddressBookController.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"AddressBookController.mjs","sourceRoot":"","sources":["../src/AddressBookController.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,oBAAoB,EACpB,KAAK,EACN,mCAAmC;AAiBpC;;GAEG;AACH,MAAM,CAAN,IAAY,WAUX;AAVD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BACzB,CAAC,OAAO,CAAC,EAAE;oBACT,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,OAAO,CAAC;oBAClC,CAAC,OAAO,CAAC,EAAE,KAAK;iBACjB;aACF,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAED,eAAe,qBAAqB,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport {\n normalizeEnsName,\n isValidHexAddress,\n isSafeDynamicKey,\n toChecksumHexAddress,\n toHex,\n} from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\n/**\n * @type ContactEntry\n *\n * ContactEntry representation\n * @property address - Hex address of a recipient account\n * @property name - Nickname associated with this address\n * @property importTime - Data time when an account as created/imported\n */\nexport type ContactEntry = {\n address: string;\n name: string;\n importTime?: number;\n};\n\n/**\n * The type of address.\n */\nexport enum AddressType {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n externallyOwnedAccounts = 'EXTERNALLY_OWNED_ACCOUNTS',\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n contractAccounts = 'CONTRACT_ACCOUNTS',\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n nonAccounts = 'NON_ACCOUNTS',\n}\n\n/**\n * @type AddressBookEntry\n *\n * AddressBookEntry representation\n * @property address - Hex address of a recipient account\n * @property name - Nickname associated with this address\n * @property chainId - Chain id identifies the current chain\n * @property memo - User's note about address\n * @property isEns - is the entry an ENS name\n * @property addressType - is the type of this address\n */\nexport type AddressBookEntry = {\n address: string;\n name: string;\n chainId: Hex;\n memo: string;\n isEns: boolean;\n addressType?: AddressType;\n};\n\n/**\n * @type AddressBookState\n *\n * Address book controller state\n * @property addressBook - Array of contact entry objects\n */\nexport type AddressBookControllerState = {\n addressBook: { [chainId: Hex]: { [address: string]: AddressBookEntry } };\n};\n\n/**\n * The name of the {@link AddressBookController}.\n */\nexport const controllerName = 'AddressBookController';\n\n/**\n * The action that can be performed to get the state of the {@link AddressBookController}.\n */\nexport type AddressBookControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AddressBookControllerState\n>;\n\n/**\n * The actions that can be performed using the {@link AddressBookController}.\n */\nexport type AddressBookControllerActions = AddressBookControllerGetStateAction;\n\n/**\n * The event that {@link AddressBookController} can emit.\n */\nexport type AddressBookControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AddressBookControllerState\n>;\n\n/**\n * The events that {@link AddressBookController} can emit.\n */\nexport type AddressBookControllerEvents = AddressBookControllerStateChangeEvent;\n\nconst addressBookControllerMetadata = {\n addressBook: { persist: true, anonymous: false },\n};\n\n/**\n * Get the default {@link AddressBookController} state.\n *\n * @returns The default {@link AddressBookController} state.\n */\nexport const getDefaultAddressBookControllerState =\n (): AddressBookControllerState => {\n return {\n addressBook: {},\n };\n };\n\n/**\n * The messenger of the {@link AddressBookController} for communication.\n */\nexport type AddressBookControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n AddressBookControllerActions,\n AddressBookControllerEvents,\n never,\n never\n>;\n\n/**\n * Controller that manages a list of recipient addresses associated with nicknames.\n */\nexport class AddressBookController extends BaseController<\n typeof controllerName,\n AddressBookControllerState,\n AddressBookControllerMessenger\n> {\n /**\n * Creates an AddressBookController instance.\n *\n * @param args - The {@link AddressBookController} arguments.\n * @param args.messenger - The controller messenger instance for communication.\n * @param args.state - Initial state to set on this controller.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: AddressBookControllerMessenger;\n state?: Partial<AddressBookControllerState>;\n }) {\n const mergedState = { ...getDefaultAddressBookControllerState(), ...state };\n super({\n messenger,\n metadata: addressBookControllerMetadata,\n name: controllerName,\n state: mergedState,\n });\n }\n\n /**\n * Remove all contract entries.\n */\n clear() {\n this.update((state) => {\n state.addressBook = {};\n });\n }\n\n /**\n * Remove a contract entry by address.\n *\n * @param chainId - Chain id identifies the current chain.\n * @param address - Recipient address to delete.\n * @returns Whether the entry was deleted.\n */\n delete(chainId: Hex, address: string) {\n address = toChecksumHexAddress(address);\n if (\n ![chainId, address].every((key) => isSafeDynamicKey(key)) ||\n !isValidHexAddress(address) ||\n !this.state.addressBook[chainId] ||\n !this.state.addressBook[chainId][address]\n ) {\n return false;\n }\n\n this.update((state) => {\n delete state.addressBook[chainId][address];\n if (Object.keys(state.addressBook[chainId]).length === 0) {\n delete state.addressBook[chainId];\n }\n });\n\n return true;\n }\n\n /**\n * Add or update a contact entry by address.\n *\n * @param address - Recipient address to add or update.\n * @param name - Nickname to associate with this address.\n * @param chainId - Chain id identifies the current chain.\n * @param memo - User's note about address.\n * @param addressType - Contact's address type.\n * @returns Boolean indicating if the address was successfully set.\n */\n set(\n address: string,\n name: string,\n chainId = toHex(1),\n memo = '',\n addressType?: AddressType,\n ) {\n address = toChecksumHexAddress(address);\n if (!isValidHexAddress(address)) {\n return false;\n }\n\n const entry = {\n address,\n chainId,\n isEns: false,\n memo,\n name,\n addressType,\n };\n\n const ensName = normalizeEnsName(name);\n if (ensName) {\n entry.name = ensName;\n entry.isEns = true;\n }\n\n this.update((state) => {\n state.addressBook = {\n ...this.state.addressBook,\n [chainId]: {\n ...this.state.addressBook[chainId],\n [address]: entry,\n },\n };\n });\n\n return true;\n }\n}\n\nexport default AddressBookController;\n"]}
1
+ {"version":3,"file":"AddressBookController.mjs","sourceRoot":"","sources":["../src/AddressBookController.ts"],"names":[],"mappings":";;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,oBAAoB,EACpB,KAAK,EACN,mCAAmC;AAGpC;;GAEG;AACH,MAAM,CAAN,IAAY,WAIX;AAJD,WAAY,WAAW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1B,sCAAsC,EACtC,KAAK,CACN,CAAC;SACH;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CAmBF;;IAbG,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,cAAc,OAAO,EACxB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CACrB,CAAC;IACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,cAAc,MAAM,EACvB,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CACpB,CAAC;IACF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,cAAc,SAAS,EAC1B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CACvB,CAAC;AACJ,CAAC;AAGH,eAAe,qBAAqB,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport {\n normalizeEnsName,\n isValidHexAddress,\n isSafeDynamicKey,\n toChecksumHexAddress,\n toHex,\n} from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\n/**\n * The type of address.\n */\nexport enum AddressType {\n externallyOwnedAccounts = 'EXTERNALLY_OWNED_ACCOUNTS',\n contractAccounts = 'CONTRACT_ACCOUNTS',\n nonAccounts = 'NON_ACCOUNTS',\n}\n\n/**\n * AddressBookEntry\n *\n * AddressBookEntry representation\n *\n * address - Hex address of a recipient account\n *\n * name - Nickname associated with this address\n *\n * chainId - Chain id identifies the current chain\n *\n * memo - User's note about address\n *\n * isEns - is the entry an ENS name\n *\n * addressType - is the type of this address\n *\n * lastUpdatedAt - timestamp of when this entry was last updated\n */\nexport type AddressBookEntry = {\n address: string;\n name: string;\n chainId: Hex;\n memo: string;\n isEns: boolean;\n addressType?: AddressType;\n lastUpdatedAt?: number;\n};\n\n/**\n * AddressBookState\n *\n * Address book controller state\n *\n * addressBook - Array of contact entry objects\n */\nexport type AddressBookControllerState = {\n addressBook: { [chainId: Hex]: { [address: string]: AddressBookEntry } };\n};\n\n/**\n * The name of the {@link AddressBookController}.\n */\nexport const controllerName = 'AddressBookController';\n\n/**\n * The action that can be performed to get the state of the {@link AddressBookController}.\n */\nexport type AddressBookControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AddressBookControllerState\n>;\n\n/**\n * The action that can be performed to list contacts from the {@link AddressBookController}.\n */\nexport type AddressBookControllerListAction = {\n type: `${typeof controllerName}:list`;\n handler: AddressBookController['list'];\n};\n\n/**\n * The action that can be performed to set a contact in the {@link AddressBookController}.\n */\nexport type AddressBookControllerSetAction = {\n type: `${typeof controllerName}:set`;\n handler: AddressBookController['set'];\n};\n\n/**\n * The action that can be performed to delete a contact from the {@link AddressBookController}.\n */\nexport type AddressBookControllerDeleteAction = {\n type: `${typeof controllerName}:delete`;\n handler: AddressBookController['delete'];\n};\n\n/**\n * Event emitted when a contact is added or updated\n */\nexport type AddressBookControllerContactUpdatedEvent = {\n type: `${typeof controllerName}:contactUpdated`;\n payload: [AddressBookEntry];\n};\n\n/**\n * Event emitted when a contact is deleted\n */\nexport type AddressBookControllerContactDeletedEvent = {\n type: `${typeof controllerName}:contactDeleted`;\n payload: [AddressBookEntry];\n};\n\n/**\n * The actions that can be performed using the {@link AddressBookController}.\n */\nexport type AddressBookControllerActions =\n | AddressBookControllerGetStateAction\n | AddressBookControllerListAction\n | AddressBookControllerSetAction\n | AddressBookControllerDeleteAction;\n\n/**\n * The event that {@link AddressBookController} can emit.\n */\nexport type AddressBookControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AddressBookControllerState\n>;\n\n/**\n * The events that {@link AddressBookController} can emit.\n */\nexport type AddressBookControllerEvents =\n | AddressBookControllerStateChangeEvent\n | AddressBookControllerContactUpdatedEvent\n | AddressBookControllerContactDeletedEvent;\n\nconst addressBookControllerMetadata = {\n addressBook: { persist: true, anonymous: false },\n};\n\n/**\n * Get the default {@link AddressBookController} state.\n *\n * @returns The default {@link AddressBookController} state.\n */\nexport const getDefaultAddressBookControllerState =\n (): AddressBookControllerState => {\n return {\n addressBook: {},\n };\n };\n\n/**\n * The messenger of the {@link AddressBookController} for communication.\n */\nexport type AddressBookControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n AddressBookControllerActions,\n AddressBookControllerEvents,\n never,\n never\n>;\n\n/**\n * Controller that manages a list of recipient addresses associated with nicknames.\n */\nexport class AddressBookController extends BaseController<\n typeof controllerName,\n AddressBookControllerState,\n AddressBookControllerMessenger\n> {\n /**\n * Creates an AddressBookController instance.\n *\n * @param args - The {@link AddressBookController} arguments.\n * @param args.messenger - The controller messenger instance for communication.\n * @param args.state - Initial state to set on this controller.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: AddressBookControllerMessenger;\n state?: Partial<AddressBookControllerState>;\n }) {\n const mergedState = { ...getDefaultAddressBookControllerState(), ...state };\n super({\n messenger,\n metadata: addressBookControllerMetadata,\n name: controllerName,\n state: mergedState,\n });\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Returns all address book entries as an array.\n *\n * @returns Array of all address book entries.\n */\n list(): AddressBookEntry[] {\n const { addressBook } = this.state;\n const contacts: AddressBookEntry[] = [];\n\n Object.keys(addressBook).forEach((chainId) => {\n const chainIdHex = chainId as Hex;\n Object.keys(addressBook[chainIdHex]).forEach((address) => {\n const contact = addressBook[chainIdHex][address];\n contacts.push(contact);\n });\n });\n\n return contacts;\n }\n\n /**\n * Remove all contract entries.\n */\n clear() {\n this.update((state) => {\n state.addressBook = {};\n });\n }\n\n /**\n * Remove a contract entry by address.\n *\n * @param chainId - Chain id identifies the current chain.\n * @param address - Recipient address to delete.\n * @returns Whether the entry was deleted.\n */\n delete(chainId: Hex, address: string) {\n address = toChecksumHexAddress(address);\n if (\n ![chainId, address].every((key) => isSafeDynamicKey(key)) ||\n !isValidHexAddress(address) ||\n !this.state.addressBook[chainId] ||\n !this.state.addressBook[chainId][address]\n ) {\n return false;\n }\n\n const deletedEntry = { ...this.state.addressBook[chainId][address] };\n\n this.update((state) => {\n if (state.addressBook[chainId] && state.addressBook[chainId][address]) {\n delete state.addressBook[chainId][address];\n\n // Clean up empty chainId objects\n if (Object.keys(state.addressBook[chainId]).length === 0) {\n delete state.addressBook[chainId];\n }\n }\n });\n\n // Skip sending delete event for global contacts with chainId '*'\n if (String(chainId) !== '*') {\n this.messagingSystem.publish(\n 'AddressBookController:contactDeleted',\n deletedEntry,\n );\n }\n\n return true;\n }\n\n /**\n * Add or update a contact entry by address.\n *\n * @param address - Recipient address to add or update.\n * @param name - Nickname to associate with this address.\n * @param chainId - Chain id identifies the current chain.\n * @param memo - User's note about address.\n * @param addressType - Contact's address type.\n * @returns Boolean indicating if the address was successfully set.\n */\n set(\n address: string,\n name: string,\n chainId = toHex(1),\n memo = '',\n addressType?: AddressType,\n ) {\n address = toChecksumHexAddress(address);\n if (!isValidHexAddress(address)) {\n return false;\n }\n\n const entry = {\n address,\n chainId,\n isEns: false,\n memo,\n name,\n addressType,\n lastUpdatedAt: Date.now(),\n };\n const ensName = normalizeEnsName(name);\n if (ensName) {\n entry.name = ensName;\n entry.isEns = true;\n }\n\n this.update((state) => {\n state.addressBook = {\n ...this.state.addressBook,\n [chainId]: {\n ...this.state.addressBook[chainId],\n [address]: entry,\n },\n };\n });\n\n // Skip sending update event for global contacts with chainId '*'\n if (String(chainId) !== '*') {\n this.messagingSystem.publish(\n 'AddressBookController:contactUpdated',\n entry,\n );\n }\n\n return true;\n }\n\n /**\n * Registers message handlers for the AddressBookController.\n */\n #registerMessageHandlers() {\n this.messagingSystem.registerActionHandler(\n `${controllerName}:list`,\n this.list.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${controllerName}:set`,\n this.set.bind(this),\n );\n this.messagingSystem.registerActionHandler(\n `${controllerName}:delete`,\n this.delete.bind(this),\n );\n }\n}\n\nexport default AddressBookController;\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAWA,qEAGiC;AAF/B,6IAAA,oCAAoC,OAAA;AACpC,8HAAA,qBAAqB,OAAA","sourcesContent":["export type {\n AddressType,\n AddressBookEntry,\n AddressBookControllerState,\n AddressBookControllerGetStateAction,\n AddressBookControllerActions,\n AddressBookControllerStateChangeEvent,\n AddressBookControllerEvents,\n AddressBookControllerMessenger,\n ContactEntry,\n} from './AddressBookController';\nexport {\n getDefaultAddressBookControllerState,\n AddressBookController,\n} from './AddressBookController';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAeA,qEAGiC;AAF/B,6IAAA,oCAAoC,OAAA;AACpC,8HAAA,qBAAqB,OAAA","sourcesContent":["export type {\n AddressType,\n AddressBookEntry,\n AddressBookControllerState,\n AddressBookControllerGetStateAction,\n AddressBookControllerListAction,\n AddressBookControllerSetAction,\n AddressBookControllerDeleteAction,\n AddressBookControllerActions,\n AddressBookControllerStateChangeEvent,\n AddressBookControllerContactUpdatedEvent,\n AddressBookControllerContactDeletedEvent,\n AddressBookControllerEvents,\n AddressBookControllerMessenger,\n} from './AddressBookController';\nexport {\n getDefaultAddressBookControllerState,\n AddressBookController,\n} from './AddressBookController';\n"]}
package/dist/index.d.cts CHANGED
@@ -1,3 +1,3 @@
1
- export type { AddressType, AddressBookEntry, AddressBookControllerState, AddressBookControllerGetStateAction, AddressBookControllerActions, AddressBookControllerStateChangeEvent, AddressBookControllerEvents, AddressBookControllerMessenger, ContactEntry, } from "./AddressBookController.cjs";
1
+ export type { AddressType, AddressBookEntry, AddressBookControllerState, AddressBookControllerGetStateAction, AddressBookControllerListAction, AddressBookControllerSetAction, AddressBookControllerDeleteAction, AddressBookControllerActions, AddressBookControllerStateChangeEvent, AddressBookControllerContactUpdatedEvent, AddressBookControllerContactDeletedEvent, AddressBookControllerEvents, AddressBookControllerMessenger, } from "./AddressBookController.cjs";
2
2
  export { getDefaultAddressBookControllerState, AddressBookController, } from "./AddressBookController.cjs";
3
3
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,WAAW,EACX,gBAAgB,EAChB,0BAA0B,EAC1B,mCAAmC,EACnC,4BAA4B,EAC5B,qCAAqC,EACrC,2BAA2B,EAC3B,8BAA8B,EAC9B,YAAY,GACb,oCAAgC;AACjC,OAAO,EACL,oCAAoC,EACpC,qBAAqB,GACtB,oCAAgC"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,WAAW,EACX,gBAAgB,EAChB,0BAA0B,EAC1B,mCAAmC,EACnC,+BAA+B,EAC/B,8BAA8B,EAC9B,iCAAiC,EACjC,4BAA4B,EAC5B,qCAAqC,EACrC,wCAAwC,EACxC,wCAAwC,EACxC,2BAA2B,EAC3B,8BAA8B,GAC/B,oCAAgC;AACjC,OAAO,EACL,oCAAoC,EACpC,qBAAqB,GACtB,oCAAgC"}
package/dist/index.d.mts CHANGED
@@ -1,3 +1,3 @@
1
- export type { AddressType, AddressBookEntry, AddressBookControllerState, AddressBookControllerGetStateAction, AddressBookControllerActions, AddressBookControllerStateChangeEvent, AddressBookControllerEvents, AddressBookControllerMessenger, ContactEntry, } from "./AddressBookController.mjs";
1
+ export type { AddressType, AddressBookEntry, AddressBookControllerState, AddressBookControllerGetStateAction, AddressBookControllerListAction, AddressBookControllerSetAction, AddressBookControllerDeleteAction, AddressBookControllerActions, AddressBookControllerStateChangeEvent, AddressBookControllerContactUpdatedEvent, AddressBookControllerContactDeletedEvent, AddressBookControllerEvents, AddressBookControllerMessenger, } from "./AddressBookController.mjs";
2
2
  export { getDefaultAddressBookControllerState, AddressBookController, } from "./AddressBookController.mjs";
3
3
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,WAAW,EACX,gBAAgB,EAChB,0BAA0B,EAC1B,mCAAmC,EACnC,4BAA4B,EAC5B,qCAAqC,EACrC,2BAA2B,EAC3B,8BAA8B,EAC9B,YAAY,GACb,oCAAgC;AACjC,OAAO,EACL,oCAAoC,EACpC,qBAAqB,GACtB,oCAAgC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,WAAW,EACX,gBAAgB,EAChB,0BAA0B,EAC1B,mCAAmC,EACnC,+BAA+B,EAC/B,8BAA8B,EAC9B,iCAAiC,EACjC,4BAA4B,EAC5B,qCAAqC,EACrC,wCAAwC,EACxC,wCAAwC,EACxC,2BAA2B,EAC3B,8BAA8B,GAC/B,oCAAgC;AACjC,OAAO,EACL,oCAAoC,EACpC,qBAAqB,GACtB,oCAAgC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAWA,OAAO,EACL,oCAAoC,EACpC,qBAAqB,EACtB,oCAAgC","sourcesContent":["export type {\n AddressType,\n AddressBookEntry,\n AddressBookControllerState,\n AddressBookControllerGetStateAction,\n AddressBookControllerActions,\n AddressBookControllerStateChangeEvent,\n AddressBookControllerEvents,\n AddressBookControllerMessenger,\n ContactEntry,\n} from './AddressBookController';\nexport {\n getDefaultAddressBookControllerState,\n AddressBookController,\n} from './AddressBookController';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAeA,OAAO,EACL,oCAAoC,EACpC,qBAAqB,EACtB,oCAAgC","sourcesContent":["export type {\n AddressType,\n AddressBookEntry,\n AddressBookControllerState,\n AddressBookControllerGetStateAction,\n AddressBookControllerListAction,\n AddressBookControllerSetAction,\n AddressBookControllerDeleteAction,\n AddressBookControllerActions,\n AddressBookControllerStateChangeEvent,\n AddressBookControllerContactUpdatedEvent,\n AddressBookControllerContactDeletedEvent,\n AddressBookControllerEvents,\n AddressBookControllerMessenger,\n} from './AddressBookController';\nexport {\n getDefaultAddressBookControllerState,\n AddressBookController,\n} from './AddressBookController';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/address-book-controller",
3
- "version": "6.0.3-preview-7bb49dcb",
3
+ "version": "6.0.3-preview-748e5947",
4
4
  "description": "Manages a list of recipient addresses associated with nicknames",
5
5
  "keywords": [
6
6
  "MetaMask",