@metamask-previews/delegation-controller 0.1.0-preview-c67b57f2 → 0.1.0-preview-6c44e568

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,4 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
- [Unreleased]: https://github.com/MetaMask/core/
10
+ ## [0.1.0]
11
+
12
+ ### Added
13
+
14
+ - Initial release ([#5592](https://github.com/MetaMask/core/pull/5592))
15
+
16
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/delegation-controller@0.1.0...HEAD
17
+ [0.1.0]: https://github.com/MetaMask/core/releases/tag/@metamask/delegation-controller@0.1.0
@@ -1,10 +1,23 @@
1
1
  "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ 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");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _DelegationController_hashDelegation, _DelegationController_getDelegationEnvironment;
2
14
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DelegationController = exports.getDefaultDelegationControllerState = exports.controllerName = void 0;
15
+ exports.DelegationController = exports.controllerName = void 0;
4
16
  const base_controller_1 = require("@metamask/base-controller");
5
17
  const keyring_controller_1 = require("@metamask/keyring-controller");
18
+ const utils_1 = require("@metamask/utils");
6
19
  const constants_1 = require("./constants.cjs");
7
- const utils_1 = require("./utils.cjs");
20
+ const utils_2 = require("./utils.cjs");
8
21
  exports.controllerName = 'DelegationController';
9
22
  const delegationControllerMetadata = {
10
23
  delegations: {
@@ -25,13 +38,21 @@ function getDefaultDelegationControllerState() {
25
38
  delegations: {},
26
39
  };
27
40
  }
28
- exports.getDefaultDelegationControllerState = getDefaultDelegationControllerState;
29
41
  /**
30
42
  * The {@link DelegationController} class.
31
43
  * This controller is meant to be a centralized place to store and sign delegations.
32
44
  */
33
45
  class DelegationController extends base_controller_1.BaseController {
34
- constructor({ messenger, state, }) {
46
+ /**
47
+ * Constructs a new {@link DelegationController} instance.
48
+ *
49
+ * @param params - The parameters for constructing the controller.
50
+ * @param params.messenger - The messenger instance to use for the controller.
51
+ * @param params.state - The initial state for the controller.
52
+ * @param params.hashDelegation - A function to hash delegations.
53
+ * @param params.getDelegationEnvironment - A function to get the delegation environment for a given chainId.
54
+ */
55
+ constructor({ messenger, state, hashDelegation, getDelegationEnvironment, }) {
35
56
  super({
36
57
  messenger,
37
58
  metadata: delegationControllerMetadata,
@@ -41,24 +62,30 @@ class DelegationController extends base_controller_1.BaseController {
41
62
  ...state,
42
63
  },
43
64
  });
65
+ _DelegationController_hashDelegation.set(this, void 0);
66
+ _DelegationController_getDelegationEnvironment.set(this, void 0);
67
+ __classPrivateFieldSet(this, _DelegationController_hashDelegation, hashDelegation, "f");
68
+ __classPrivateFieldSet(this, _DelegationController_getDelegationEnvironment, getDelegationEnvironment, "f");
44
69
  }
45
70
  /**
46
71
  * Signs a delegation.
47
72
  *
48
73
  * @param params - The parameters for signing the delegation.
49
74
  * @param params.delegation - The delegation to sign.
50
- * @param params.verifyingContract - The address of the verifying contract (DelegationManager).
51
75
  * @param params.chainId - The chainId of the chain to sign the delegation for.
52
76
  * @returns The signature of the delegation.
53
77
  */
54
- async sign(params) {
55
- const { delegation, verifyingContract, chainId } = params;
56
- const account = this.messagingSystem.call('AccountsController:getSelectedAccount');
57
- const data = (0, utils_1.createTypedMessageParams)({
58
- chainId,
59
- from: account.address,
60
- delegation,
61
- verifyingContract,
78
+ async signDelegation(params) {
79
+ const { delegation, chainId } = params;
80
+ const { DelegationManager } = __classPrivateFieldGet(this, _DelegationController_getDelegationEnvironment, "f").call(this, chainId);
81
+ const data = (0, utils_2.createTypedMessageParams)({
82
+ chainId: (0, utils_1.hexToNumber)(chainId),
83
+ from: delegation.delegator,
84
+ delegation: {
85
+ ...delegation,
86
+ signature: '0x',
87
+ },
88
+ verifyingContract: DelegationManager,
62
89
  });
63
90
  // TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.
64
91
  // Waiting on confirmations team to implement this.
@@ -68,14 +95,16 @@ class DelegationController extends base_controller_1.BaseController {
68
95
  /**
69
96
  * Stores a delegation in storage.
70
97
  *
71
- * @param hash - The hash of the delegation to store.
72
- * @param entry - The delegation entry to store.
98
+ * @param params - The parameters for storing the delegation.
99
+ * @param params.entry - The delegation entry to store.
73
100
  */
74
- store(hash, entry) {
101
+ store(params) {
102
+ const { entry } = params;
103
+ const hash = __classPrivateFieldGet(this, _DelegationController_hashDelegation, "f").call(this, entry.delegation);
75
104
  // If the authority is not the root authority, validate that the
76
105
  // parent entry does exist.
77
- if (!(0, utils_1.isAddressEqual)(entry.data.authority, constants_1.ROOT_AUTHORITY) &&
78
- !this.state.delegations[entry.data.authority]) {
106
+ if (!(0, utils_2.isHexEqual)(entry.delegation.authority, constants_1.ROOT_AUTHORITY) &&
107
+ !this.state.delegations[entry.delegation.authority]) {
79
108
  throw new Error('Invalid authority');
80
109
  }
81
110
  this.update((state) => {
@@ -93,14 +122,15 @@ class DelegationController extends base_controller_1.BaseController {
93
122
  const requester = account.address;
94
123
  let list = Object.values(this.state.delegations);
95
124
  if (filter?.from) {
96
- list = list.filter((entry) => (0, utils_1.isAddressEqual)(entry.data.delegator, filter.from));
125
+ list = list.filter((entry) => (0, utils_2.isHexEqual)(entry.delegation.delegator, filter.from));
97
126
  }
98
127
  if (!filter?.from ||
99
- (filter?.from && !(0, utils_1.isAddressEqual)(filter.from, requester))) {
100
- list = list.filter((entry) => (0, utils_1.isAddressEqual)(entry.data.delegate, requester));
128
+ (filter?.from && !(0, utils_2.isHexEqual)(filter.from, requester))) {
129
+ list = list.filter((entry) => (0, utils_2.isHexEqual)(entry.delegation.delegate, requester));
101
130
  }
102
- if (filter?.chainId) {
103
- list = list.filter((entry) => entry.chainId === filter.chainId);
131
+ const filterChainId = filter?.chainId;
132
+ if (filterChainId) {
133
+ list = list.filter((entry) => (0, utils_2.isHexEqual)(entry.chainId, filterChainId));
104
134
  }
105
135
  const tags = filter?.tags;
106
136
  if (tags && tags.length > 0) {
@@ -131,13 +161,13 @@ class DelegationController extends base_controller_1.BaseController {
131
161
  return null;
132
162
  }
133
163
  chain.push(entry);
134
- for (let _hash = entry.data.authority; _hash !== constants_1.ROOT_AUTHORITY;) {
164
+ for (let _hash = entry.delegation.authority; _hash !== constants_1.ROOT_AUTHORITY;) {
135
165
  const parent = this.retrieve(_hash);
136
166
  if (!parent) {
137
167
  throw new Error('Invalid delegation chain');
138
168
  }
139
169
  chain.push(parent);
140
- _hash = parent.data.authority;
170
+ _hash = parent.delegation.authority;
141
171
  }
142
172
  return chain;
143
173
  }
@@ -154,24 +184,27 @@ class DelegationController extends base_controller_1.BaseController {
154
184
  return 0;
155
185
  }
156
186
  const entries = Object.entries(this.state.delegations);
157
- let count = 0;
158
187
  const nextHashes = [hash];
188
+ const deletedHashes = [];
159
189
  while (nextHashes.length > 0) {
160
190
  const currentHash = nextHashes.pop();
161
191
  // Find all delegations that have this hash as their authority
162
- const children = entries.filter(([_, v]) => v.data.authority === currentHash);
192
+ const children = entries.filter(([_, v]) => v.delegation.authority === currentHash);
163
193
  // Add the hashes of all child delegations to be processed next
164
194
  children.forEach(([k]) => {
165
195
  nextHashes.push(k);
166
196
  });
167
- // Delete the current delegation
168
- this.update((state) => {
169
- delete state.delegations[currentHash];
170
- });
171
- count += 1;
197
+ deletedHashes.push(currentHash);
172
198
  }
173
- return count;
199
+ // Delete delegations
200
+ this.update((state) => {
201
+ deletedHashes.forEach((h) => {
202
+ delete state.delegations[h];
203
+ });
204
+ });
205
+ return deletedHashes.length;
174
206
  }
175
207
  }
176
208
  exports.DelegationController = DelegationController;
209
+ _DelegationController_hashDelegation = new WeakMap(), _DelegationController_getDelegationEnvironment = new WeakMap();
177
210
  //# sourceMappingURL=DelegationController.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"DelegationController.cjs","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":";;;AACA,+DAA2D;AAC3D,qEAAoE;AAEpE,+CAA6C;AAU7C,uCAAmE;AAEtD,QAAA,cAAc,GAAG,sBAAsB,CAAC;AAErD,MAAM,4BAA4B,GAAG;IACnC,WAAW,EAAE;QACX,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACiD,CAAC;AAErD;;;;;;;GAOG;AACH,SAAgB,mCAAmC;IACjD,OAAO;QACL,WAAW,EAAE,EAAE;KAChB,CAAC;AACJ,CAAC;AAJD,kFAIC;AAED;;;GAGG;AACH,MAAa,oBAAqB,SAAQ,gCAIzC;IACC,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,4BAA4B;YACtC,IAAI,EAAE,sBAAc;YACpB,KAAK,EAAE;gBACL,GAAG,mCAAmC,EAAE;gBACxC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,IAAI,CAAC,MAIV;QACC,MAAM,EAAE,UAAU,EAAE,iBAAiB,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;QAE1D,MAAM,OAAO,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CACvC,uCAAuC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K,EAAE,EAAE;gBACpB,OAAO,KAAK,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;YACxC,CAAC,CAAC,CAAC;YACH,KAAK,IAAI,CAAC,CAAC;SACZ;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAzMD,oDAyMC","sourcesContent":["import type { StateMetadata } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { SignTypedDataVersion } from '@metamask/keyring-controller';\n\nimport { ROOT_AUTHORITY } from './constants';\nimport type {\n Address,\n Delegation,\n DelegationControllerMessenger,\n DelegationControllerState,\n DelegationEntry,\n DelegationFilter,\n Hex,\n} from './types';\nimport { createTypedMessageParams, isAddressEqual } from './utils';\n\nexport const controllerName = 'DelegationController';\n\nconst delegationControllerMetadata = {\n delegations: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<DelegationControllerState>;\n\n/**\n * Constructs the default {@link DelegationController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link DelegationController} state.\n */\nexport function getDefaultDelegationControllerState(): DelegationControllerState {\n return {\n delegations: {},\n };\n}\n\n/**\n * The {@link DelegationController} class.\n * This controller is meant to be a centralized place to store and sign delegations.\n */\nexport class DelegationController extends BaseController<\n typeof controllerName,\n DelegationControllerState,\n DelegationControllerMessenger\n> {\n constructor({\n messenger,\n state,\n }: {\n messenger: DelegationControllerMessenger;\n state?: Partial<DelegationControllerState>;\n }) {\n super({\n messenger,\n metadata: delegationControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultDelegationControllerState(),\n ...state,\n },\n });\n }\n\n /**\n * Signs a delegation.\n *\n * @param params - The parameters for signing the delegation.\n * @param params.delegation - The delegation to sign.\n * @param params.verifyingContract - The address of the verifying contract (DelegationManager).\n * @param params.chainId - The chainId of the chain to sign the delegation for.\n * @returns The signature of the delegation.\n */\n async sign(params: {\n delegation: Delegation;\n verifyingContract: Address;\n chainId: number;\n }) {\n const { delegation, verifyingContract, chainId } = params;\n\n const account = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n\n const data = createTypedMessageParams({\n chainId,\n from: account.address as Address,\n delegation,\n verifyingContract,\n });\n\n // TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.\n // Waiting on confirmations team to implement this.\n const signature: string = await this.messagingSystem.call(\n 'KeyringController:signTypedMessage',\n data,\n SignTypedDataVersion.V4,\n );\n\n return signature;\n }\n\n /**\n * Stores a delegation in storage.\n *\n * @param hash - The hash of the delegation to store.\n * @param entry - The delegation entry to store.\n */\n store(hash: Hex, entry: DelegationEntry) {\n // If the authority is not the root authority, validate that the\n // parent entry does exist.\n if (\n !isAddressEqual(entry.data.authority, ROOT_AUTHORITY) &&\n !this.state.delegations[entry.data.authority]\n ) {\n throw new Error('Invalid authority');\n }\n this.update((state) => {\n state.delegations[hash] = entry;\n });\n }\n\n /**\n * Lists delegation entries.\n *\n * @param filter - The filter to use to list the delegation entries.\n * @returns A list of delegation entries that match the filter.\n */\n list(filter?: DelegationFilter) {\n const account = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n const requester = account.address as Address;\n\n let list: DelegationEntry[] = Object.values(this.state.delegations);\n\n if (filter?.from) {\n list = list.filter((entry) =>\n isAddressEqual(entry.data.delegator, filter.from as Address),\n );\n }\n\n if (\n !filter?.from ||\n (filter?.from && !isAddressEqual(filter.from, requester))\n ) {\n list = list.filter((entry) =>\n isAddressEqual(entry.data.delegate, requester),\n );\n }\n\n if (filter?.chainId) {\n list = list.filter((entry) => entry.chainId === filter.chainId);\n }\n\n const tags = filter?.tags;\n if (tags && tags.length > 0) {\n // Filter entries that contain all of the filter tags\n list = list.filter((entry) =>\n tags.every((tag) => entry.tags.includes(tag)),\n );\n }\n\n return list;\n }\n\n /**\n * Retrieves the delegation entry for a given delegation hash.\n *\n * @param hash - The hash of the delegation to retrieve.\n * @returns The delegation entry, or null if not found.\n */\n retrieve(hash: Hex) {\n return this.state.delegations[hash] ?? null;\n }\n\n /**\n * Retrieves a delegation chain from a delegation hash.\n *\n * @param hash - The hash of the delegation to retrieve.\n * @returns The delegation chain, or null if not found.\n */\n chain(hash: Hex) {\n const chain: DelegationEntry[] = [];\n\n const entry = this.retrieve(hash);\n if (!entry) {\n return null;\n }\n chain.push(entry);\n\n for (let _hash = entry.data.authority; _hash !== ROOT_AUTHORITY; ) {\n const parent = this.retrieve(_hash);\n if (!parent) {\n throw new Error('Invalid delegation chain');\n }\n chain.push(parent);\n _hash = parent.data.authority;\n }\n\n return chain;\n }\n\n /**\n * Deletes a delegation entrie from storage, along with any other entries\n * that are redelegated from it.\n *\n * @param hash - The hash of the delegation to delete.\n * @returns The number of entries deleted.\n */\n delete(hash: Hex): number {\n const root = this.retrieve(hash);\n if (!root) {\n return 0;\n }\n\n const entries = Object.entries(this.state.delegations);\n let count = 0;\n const nextHashes: Hex[] = [hash];\n\n while (nextHashes.length > 0) {\n const currentHash = nextHashes.pop() as Hex;\n\n // Find all delegations that have this hash as their authority\n const children = entries.filter(\n ([_, v]) => v.data.authority === currentHash,\n );\n\n // Add the hashes of all child delegations to be processed next\n children.forEach(([k]) => {\n nextHashes.push(k as Hex);\n });\n\n // Delete the current delegation\n this.update((state) => {\n delete state.delegations[currentHash];\n });\n count += 1;\n }\n\n return count;\n }\n}\n"]}
1
+ {"version":3,"file":"DelegationController.cjs","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,+DAA2D;AAC3D,qEAAoE;AACpE,2CAA8C;AAE9C,+CAA6C;AAY7C,uCAA+D;AAElD,QAAA,cAAc,GAAG,sBAAsB,CAAC;AAErD,MAAM,4BAA4B,GAAG;IACnC,WAAW,EAAE;QACX,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACiD,CAAC;AAErD;;;;;;;GAOG;AACH,SAAS,mCAAmC;IAC1C,OAAO;QACL,WAAW,EAAE,EAAE;KAChB,CAAC;AACJ,C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ent":["import type { StateMetadata } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { SignTypedDataVersion } from '@metamask/keyring-controller';\nimport { hexToNumber } from '@metamask/utils';\n\nimport { ROOT_AUTHORITY } from './constants';\nimport type {\n Address,\n Delegation,\n DelegationControllerMessenger,\n DelegationControllerState,\n DelegationEntry,\n DelegationFilter,\n DeleGatorEnvironment,\n Hex,\n UnsignedDelegation,\n} from './types';\nimport { createTypedMessageParams, isHexEqual } from './utils';\n\nexport const controllerName = 'DelegationController';\n\nconst delegationControllerMetadata = {\n delegations: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<DelegationControllerState>;\n\n/**\n * Constructs the default {@link DelegationController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link DelegationController} state.\n */\nfunction getDefaultDelegationControllerState(): DelegationControllerState {\n return {\n delegations: {},\n };\n}\n\n/**\n * The {@link DelegationController} class.\n * This controller is meant to be a centralized place to store and sign delegations.\n */\nexport class DelegationController extends BaseController<\n typeof controllerName,\n DelegationControllerState,\n DelegationControllerMessenger\n> {\n readonly #hashDelegation: (delegation: Delegation) => Hex;\n\n readonly #getDelegationEnvironment: (chainId: Hex) => DeleGatorEnvironment;\n\n /**\n * Constructs a new {@link DelegationController} instance.\n *\n * @param params - The parameters for constructing the controller.\n * @param params.messenger - The messenger instance to use for the controller.\n * @param params.state - The initial state for the controller.\n * @param params.hashDelegation - A function to hash delegations.\n * @param params.getDelegationEnvironment - A function to get the delegation environment for a given chainId.\n */\n constructor({\n messenger,\n state,\n hashDelegation,\n getDelegationEnvironment,\n }: {\n messenger: DelegationControllerMessenger;\n state?: Partial<DelegationControllerState>;\n hashDelegation: (delegation: Delegation) => Hex;\n getDelegationEnvironment: (chainId: Hex) => DeleGatorEnvironment;\n }) {\n super({\n messenger,\n metadata: delegationControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultDelegationControllerState(),\n ...state,\n },\n });\n this.#hashDelegation = hashDelegation;\n this.#getDelegationEnvironment = getDelegationEnvironment;\n }\n\n /**\n * Signs a delegation.\n *\n * @param params - The parameters for signing the delegation.\n * @param params.delegation - The delegation to sign.\n * @param params.chainId - The chainId of the chain to sign the delegation for.\n * @returns The signature of the delegation.\n */\n async signDelegation(params: {\n delegation: UnsignedDelegation;\n chainId: Hex;\n }) {\n const { delegation, chainId } = params;\n const { DelegationManager } = this.#getDelegationEnvironment(chainId);\n\n const data = createTypedMessageParams({\n chainId: hexToNumber(chainId),\n from: delegation.delegator,\n delegation: {\n ...delegation,\n signature: '0x',\n },\n verifyingContract: DelegationManager,\n });\n\n // TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.\n // Waiting on confirmations team to implement this.\n const signature: string = await this.messagingSystem.call(\n 'KeyringController:signTypedMessage',\n data,\n SignTypedDataVersion.V4,\n );\n\n return signature;\n }\n\n /**\n * Stores a delegation in storage.\n *\n * @param params - The parameters for storing the delegation.\n * @param params.entry - The delegation entry to store.\n */\n store(params: { entry: DelegationEntry }) {\n const { entry } = params;\n const hash = this.#hashDelegation(entry.delegation);\n\n // If the authority is not the root authority, validate that the\n // parent entry does exist.\n if (\n !isHexEqual(entry.delegation.authority, ROOT_AUTHORITY) &&\n !this.state.delegations[entry.delegation.authority]\n ) {\n throw new Error('Invalid authority');\n }\n this.update((state) => {\n state.delegations[hash] = entry;\n });\n }\n\n /**\n * Lists delegation entries.\n *\n * @param filter - The filter to use to list the delegation entries.\n * @returns A list of delegation entries that match the filter.\n */\n list(filter?: DelegationFilter) {\n const account = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n const requester = account.address as Address;\n\n let list: DelegationEntry[] = Object.values(this.state.delegations);\n\n if (filter?.from) {\n list = list.filter((entry) =>\n isHexEqual(entry.delegation.delegator, filter.from as Address),\n );\n }\n\n if (\n !filter?.from ||\n (filter?.from && !isHexEqual(filter.from, requester))\n ) {\n list = list.filter((entry) =>\n isHexEqual(entry.delegation.delegate, requester),\n );\n }\n\n const filterChainId = filter?.chainId;\n if (filterChainId) {\n list = list.filter((entry) => isHexEqual(entry.chainId, filterChainId));\n }\n\n const tags = filter?.tags;\n if (tags && tags.length > 0) {\n // Filter entries that contain all of the filter tags\n list = list.filter((entry) =>\n tags.every((tag) => entry.tags.includes(tag)),\n );\n }\n\n return list;\n }\n\n /**\n * Retrieves the delegation entry for a given delegation hash.\n *\n * @param hash - The hash of the delegation to retrieve.\n * @returns The delegation entry, or null if not found.\n */\n retrieve(hash: Hex) {\n return this.state.delegations[hash] ?? null;\n }\n\n /**\n * Retrieves a delegation chain from a delegation hash.\n *\n * @param hash - The hash of the delegation to retrieve.\n * @returns The delegation chain, or null if not found.\n */\n chain(hash: Hex) {\n const chain: DelegationEntry[] = [];\n\n const entry = this.retrieve(hash);\n if (!entry) {\n return null;\n }\n chain.push(entry);\n\n for (let _hash = entry.delegation.authority; _hash !== ROOT_AUTHORITY; ) {\n const parent = this.retrieve(_hash);\n if (!parent) {\n throw new Error('Invalid delegation chain');\n }\n chain.push(parent);\n _hash = parent.delegation.authority;\n }\n\n return chain;\n }\n\n /**\n * Deletes a delegation entrie from storage, along with any other entries\n * that are redelegated from it.\n *\n * @param hash - The hash of the delegation to delete.\n * @returns The number of entries deleted.\n */\n delete(hash: Hex): number {\n const root = this.retrieve(hash);\n if (!root) {\n return 0;\n }\n\n const entries = Object.entries(this.state.delegations);\n const nextHashes: Hex[] = [hash];\n const deletedHashes: Hex[] = [];\n\n while (nextHashes.length > 0) {\n const currentHash = nextHashes.pop() as Hex;\n\n // Find all delegations that have this hash as their authority\n const children = entries.filter(\n ([_, v]) => v.delegation.authority === currentHash,\n );\n\n // Add the hashes of all child delegations to be processed next\n children.forEach(([k]) => {\n nextHashes.push(k as Hex);\n });\n\n deletedHashes.push(currentHash);\n }\n\n // Delete delegations\n this.update((state) => {\n deletedHashes.forEach((h) => {\n delete state.delegations[h];\n });\n });\n\n return deletedHashes.length;\n }\n}\n"]}
@@ -1,45 +1,48 @@
1
1
  import { BaseController } from "@metamask/base-controller";
2
- import type { Address, Delegation, DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DelegationFilter, Hex } from "./types.cjs";
2
+ import type { Delegation, DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DelegationFilter, DeleGatorEnvironment, Hex, UnsignedDelegation } from "./types.cjs";
3
3
  export declare const controllerName = "DelegationController";
4
- /**
5
- * Constructs the default {@link DelegationController} state. This allows
6
- * consumers to provide a partial state object when initializing the controller
7
- * and also helps in constructing complete state objects for this controller in
8
- * tests.
9
- *
10
- * @returns The default {@link DelegationController} state.
11
- */
12
- export declare function getDefaultDelegationControllerState(): DelegationControllerState;
13
4
  /**
14
5
  * The {@link DelegationController} class.
15
6
  * This controller is meant to be a centralized place to store and sign delegations.
16
7
  */
17
8
  export declare class DelegationController extends BaseController<typeof controllerName, DelegationControllerState, DelegationControllerMessenger> {
18
- constructor({ messenger, state, }: {
9
+ #private;
10
+ /**
11
+ * Constructs a new {@link DelegationController} instance.
12
+ *
13
+ * @param params - The parameters for constructing the controller.
14
+ * @param params.messenger - The messenger instance to use for the controller.
15
+ * @param params.state - The initial state for the controller.
16
+ * @param params.hashDelegation - A function to hash delegations.
17
+ * @param params.getDelegationEnvironment - A function to get the delegation environment for a given chainId.
18
+ */
19
+ constructor({ messenger, state, hashDelegation, getDelegationEnvironment, }: {
19
20
  messenger: DelegationControllerMessenger;
20
21
  state?: Partial<DelegationControllerState>;
22
+ hashDelegation: (delegation: Delegation) => Hex;
23
+ getDelegationEnvironment: (chainId: Hex) => DeleGatorEnvironment;
21
24
  });
22
25
  /**
23
26
  * Signs a delegation.
24
27
  *
25
28
  * @param params - The parameters for signing the delegation.
26
29
  * @param params.delegation - The delegation to sign.
27
- * @param params.verifyingContract - The address of the verifying contract (DelegationManager).
28
30
  * @param params.chainId - The chainId of the chain to sign the delegation for.
29
31
  * @returns The signature of the delegation.
30
32
  */
31
- sign(params: {
32
- delegation: Delegation;
33
- verifyingContract: Address;
34
- chainId: number;
33
+ signDelegation(params: {
34
+ delegation: UnsignedDelegation;
35
+ chainId: Hex;
35
36
  }): Promise<string>;
36
37
  /**
37
38
  * Stores a delegation in storage.
38
39
  *
39
- * @param hash - The hash of the delegation to store.
40
- * @param entry - The delegation entry to store.
40
+ * @param params - The parameters for storing the delegation.
41
+ * @param params.entry - The delegation entry to store.
41
42
  */
42
- store(hash: Hex, entry: DelegationEntry): void;
43
+ store(params: {
44
+ entry: DelegationEntry;
45
+ }): void;
43
46
  /**
44
47
  * Lists delegation entries.
45
48
  *
@@ -1 +1 @@
1
- {"version":3,"file":"DelegationController.d.cts","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAI3D,OAAO,KAAK,EACV,OAAO,EACP,UAAU,EACV,6BAA6B,EAC7B,yBAAyB,EACzB,eAAe,EACf,gBAAgB,EAChB,GAAG,EACJ,oBAAgB;AAGjB,eAAO,MAAM,cAAc,yBAAyB,CAAC;AASrD;;;;;;;GAOG;AACH,wBAAgB,mCAAmC,IAAI,yBAAyB,CAI/E;AAED;;;GAGG;AACH,qBAAa,oBAAqB,SAAQ,cAAc,CACtD,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAC9B;gBACa,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,6BAA6B,CAAC;QACzC,KAAK,CAAC,EAAE,OAAO,CAAC,yBAAyB,CAAC,CAAC;KAC5C;IAYD;;;;;;;;OAQG;IACG,IAAI,CAAC,MAAM,EAAE;QACjB,UAAU,EAAE,UAAU,CAAC;QACvB,iBAAiB,EAAE,OAAO,CAAC;QAC3B,OAAO,EAAE,MAAM,CAAC;KACjB;IAyBD;;;;;OAKG;IACH,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,eAAe;IAcvC;;;;;OAKG;IACH,IAAI,CAAC,MAAM,CAAC,EAAE,gBAAgB;IAsC9B;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,EAAE,GAAG;IAIlB;;;;;OAKG;IACH,KAAK,CAAC,IAAI,EAAE,GAAG;IAqBf;;;;;;OAMG;IACH,MAAM,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM;CAgC1B"}
1
+ {"version":3,"file":"DelegationController.d.cts","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAK3D,OAAO,KAAK,EAEV,UAAU,EACV,6BAA6B,EAC7B,yBAAyB,EACzB,eAAe,EACf,gBAAgB,EAChB,oBAAoB,EACpB,GAAG,EACH,kBAAkB,EACnB,oBAAgB;AAGjB,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAuBrD;;;GAGG;AACH,qBAAa,oBAAqB,SAAQ,cAAc,CACtD,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAC9B;;IAKC;;;;;;;;OAQG;gBACS,EACV,SAAS,EACT,KAAK,EACL,cAAc,EACd,wBAAwB,GACzB,EAAE;QACD,SAAS,EAAE,6BAA6B,CAAC;QACzC,KAAK,CAAC,EAAE,OAAO,CAAC,yBAAyB,CAAC,CAAC;QAC3C,cAAc,EAAE,CAAC,UAAU,EAAE,UAAU,KAAK,GAAG,CAAC;QAChD,wBAAwB,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,oBAAoB,CAAC;KAClE;IAcD;;;;;;;OAOG;IACG,cAAc,CAAC,MAAM,EAAE;QAC3B,UAAU,EAAE,kBAAkB,CAAC;QAC/B,OAAO,EAAE,GAAG,CAAC;KACd;IAyBD;;;;;OAKG;IACH,KAAK,CAAC,MAAM,EAAE;QAAE,KAAK,EAAE,eAAe,CAAA;KAAE;IAiBxC;;;;;OAKG;IACH,IAAI,CAAC,MAAM,CAAC,EAAE,gBAAgB;IAuC9B;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,EAAE,GAAG;IAIlB;;;;;OAKG;IACH,KAAK,CAAC,IAAI,EAAE,GAAG;IAqBf;;;;;;OAMG;IACH,MAAM,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM;CAmC1B"}
@@ -1,45 +1,48 @@
1
1
  import { BaseController } from "@metamask/base-controller";
2
- import type { Address, Delegation, DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DelegationFilter, Hex } from "./types.mjs";
2
+ import type { Delegation, DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DelegationFilter, DeleGatorEnvironment, Hex, UnsignedDelegation } from "./types.mjs";
3
3
  export declare const controllerName = "DelegationController";
4
- /**
5
- * Constructs the default {@link DelegationController} state. This allows
6
- * consumers to provide a partial state object when initializing the controller
7
- * and also helps in constructing complete state objects for this controller in
8
- * tests.
9
- *
10
- * @returns The default {@link DelegationController} state.
11
- */
12
- export declare function getDefaultDelegationControllerState(): DelegationControllerState;
13
4
  /**
14
5
  * The {@link DelegationController} class.
15
6
  * This controller is meant to be a centralized place to store and sign delegations.
16
7
  */
17
8
  export declare class DelegationController extends BaseController<typeof controllerName, DelegationControllerState, DelegationControllerMessenger> {
18
- constructor({ messenger, state, }: {
9
+ #private;
10
+ /**
11
+ * Constructs a new {@link DelegationController} instance.
12
+ *
13
+ * @param params - The parameters for constructing the controller.
14
+ * @param params.messenger - The messenger instance to use for the controller.
15
+ * @param params.state - The initial state for the controller.
16
+ * @param params.hashDelegation - A function to hash delegations.
17
+ * @param params.getDelegationEnvironment - A function to get the delegation environment for a given chainId.
18
+ */
19
+ constructor({ messenger, state, hashDelegation, getDelegationEnvironment, }: {
19
20
  messenger: DelegationControllerMessenger;
20
21
  state?: Partial<DelegationControllerState>;
22
+ hashDelegation: (delegation: Delegation) => Hex;
23
+ getDelegationEnvironment: (chainId: Hex) => DeleGatorEnvironment;
21
24
  });
22
25
  /**
23
26
  * Signs a delegation.
24
27
  *
25
28
  * @param params - The parameters for signing the delegation.
26
29
  * @param params.delegation - The delegation to sign.
27
- * @param params.verifyingContract - The address of the verifying contract (DelegationManager).
28
30
  * @param params.chainId - The chainId of the chain to sign the delegation for.
29
31
  * @returns The signature of the delegation.
30
32
  */
31
- sign(params: {
32
- delegation: Delegation;
33
- verifyingContract: Address;
34
- chainId: number;
33
+ signDelegation(params: {
34
+ delegation: UnsignedDelegation;
35
+ chainId: Hex;
35
36
  }): Promise<string>;
36
37
  /**
37
38
  * Stores a delegation in storage.
38
39
  *
39
- * @param hash - The hash of the delegation to store.
40
- * @param entry - The delegation entry to store.
40
+ * @param params - The parameters for storing the delegation.
41
+ * @param params.entry - The delegation entry to store.
41
42
  */
42
- store(hash: Hex, entry: DelegationEntry): void;
43
+ store(params: {
44
+ entry: DelegationEntry;
45
+ }): void;
43
46
  /**
44
47
  * Lists delegation entries.
45
48
  *
@@ -1 +1 @@
1
- {"version":3,"file":"DelegationController.d.mts","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAI3D,OAAO,KAAK,EACV,OAAO,EACP,UAAU,EACV,6BAA6B,EAC7B,yBAAyB,EACzB,eAAe,EACf,gBAAgB,EAChB,GAAG,EACJ,oBAAgB;AAGjB,eAAO,MAAM,cAAc,yBAAyB,CAAC;AASrD;;;;;;;GAOG;AACH,wBAAgB,mCAAmC,IAAI,yBAAyB,CAI/E;AAED;;;GAGG;AACH,qBAAa,oBAAqB,SAAQ,cAAc,CACtD,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAC9B;gBACa,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,6BAA6B,CAAC;QACzC,KAAK,CAAC,EAAE,OAAO,CAAC,yBAAyB,CAAC,CAAC;KAC5C;IAYD;;;;;;;;OAQG;IACG,IAAI,CAAC,MAAM,EAAE;QACjB,UAAU,EAAE,UAAU,CAAC;QACvB,iBAAiB,EAAE,OAAO,CAAC;QAC3B,OAAO,EAAE,MAAM,CAAC;KACjB;IAyBD;;;;;OAKG;IACH,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,eAAe;IAcvC;;;;;OAKG;IACH,IAAI,CAAC,MAAM,CAAC,EAAE,gBAAgB;IAsC9B;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,EAAE,GAAG;IAIlB;;;;;OAKG;IACH,KAAK,CAAC,IAAI,EAAE,GAAG;IAqBf;;;;;;OAMG;IACH,MAAM,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM;CAgC1B"}
1
+ {"version":3,"file":"DelegationController.d.mts","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAK3D,OAAO,KAAK,EAEV,UAAU,EACV,6BAA6B,EAC7B,yBAAyB,EACzB,eAAe,EACf,gBAAgB,EAChB,oBAAoB,EACpB,GAAG,EACH,kBAAkB,EACnB,oBAAgB;AAGjB,eAAO,MAAM,cAAc,yBAAyB,CAAC;AAuBrD;;;GAGG;AACH,qBAAa,oBAAqB,SAAQ,cAAc,CACtD,OAAO,cAAc,EACrB,yBAAyB,EACzB,6BAA6B,CAC9B;;IAKC;;;;;;;;OAQG;gBACS,EACV,SAAS,EACT,KAAK,EACL,cAAc,EACd,wBAAwB,GACzB,EAAE;QACD,SAAS,EAAE,6BAA6B,CAAC;QACzC,KAAK,CAAC,EAAE,OAAO,CAAC,yBAAyB,CAAC,CAAC;QAC3C,cAAc,EAAE,CAAC,UAAU,EAAE,UAAU,KAAK,GAAG,CAAC;QAChD,wBAAwB,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,oBAAoB,CAAC;KAClE;IAcD;;;;;;;OAOG;IACG,cAAc,CAAC,MAAM,EAAE;QAC3B,UAAU,EAAE,kBAAkB,CAAC;QAC/B,OAAO,EAAE,GAAG,CAAC;KACd;IAyBD;;;;;OAKG;IACH,KAAK,CAAC,MAAM,EAAE;QAAE,KAAK,EAAE,eAAe,CAAA;KAAE;IAiBxC;;;;;OAKG;IACH,IAAI,CAAC,MAAM,CAAC,EAAE,gBAAgB;IAuC9B;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,EAAE,GAAG;IAIlB;;;;;OAKG;IACH,KAAK,CAAC,IAAI,EAAE,GAAG;IAqBf;;;;;;OAMG;IACH,MAAM,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM;CAmC1B"}
@@ -1,7 +1,20 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ 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");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _DelegationController_hashDelegation, _DelegationController_getDelegationEnvironment;
1
13
  import { BaseController } from "@metamask/base-controller";
2
14
  import { SignTypedDataVersion } from "@metamask/keyring-controller";
15
+ import { hexToNumber } from "@metamask/utils";
3
16
  import { ROOT_AUTHORITY } from "./constants.mjs";
4
- import { createTypedMessageParams, isAddressEqual } from "./utils.mjs";
17
+ import { createTypedMessageParams, isHexEqual } from "./utils.mjs";
5
18
  export const controllerName = 'DelegationController';
6
19
  const delegationControllerMetadata = {
7
20
  delegations: {
@@ -17,7 +30,7 @@ const delegationControllerMetadata = {
17
30
  *
18
31
  * @returns The default {@link DelegationController} state.
19
32
  */
20
- export function getDefaultDelegationControllerState() {
33
+ function getDefaultDelegationControllerState() {
21
34
  return {
22
35
  delegations: {},
23
36
  };
@@ -27,7 +40,16 @@ export function getDefaultDelegationControllerState() {
27
40
  * This controller is meant to be a centralized place to store and sign delegations.
28
41
  */
29
42
  export class DelegationController extends BaseController {
30
- constructor({ messenger, state, }) {
43
+ /**
44
+ * Constructs a new {@link DelegationController} instance.
45
+ *
46
+ * @param params - The parameters for constructing the controller.
47
+ * @param params.messenger - The messenger instance to use for the controller.
48
+ * @param params.state - The initial state for the controller.
49
+ * @param params.hashDelegation - A function to hash delegations.
50
+ * @param params.getDelegationEnvironment - A function to get the delegation environment for a given chainId.
51
+ */
52
+ constructor({ messenger, state, hashDelegation, getDelegationEnvironment, }) {
31
53
  super({
32
54
  messenger,
33
55
  metadata: delegationControllerMetadata,
@@ -37,24 +59,30 @@ export class DelegationController extends BaseController {
37
59
  ...state,
38
60
  },
39
61
  });
62
+ _DelegationController_hashDelegation.set(this, void 0);
63
+ _DelegationController_getDelegationEnvironment.set(this, void 0);
64
+ __classPrivateFieldSet(this, _DelegationController_hashDelegation, hashDelegation, "f");
65
+ __classPrivateFieldSet(this, _DelegationController_getDelegationEnvironment, getDelegationEnvironment, "f");
40
66
  }
41
67
  /**
42
68
  * Signs a delegation.
43
69
  *
44
70
  * @param params - The parameters for signing the delegation.
45
71
  * @param params.delegation - The delegation to sign.
46
- * @param params.verifyingContract - The address of the verifying contract (DelegationManager).
47
72
  * @param params.chainId - The chainId of the chain to sign the delegation for.
48
73
  * @returns The signature of the delegation.
49
74
  */
50
- async sign(params) {
51
- const { delegation, verifyingContract, chainId } = params;
52
- const account = this.messagingSystem.call('AccountsController:getSelectedAccount');
75
+ async signDelegation(params) {
76
+ const { delegation, chainId } = params;
77
+ const { DelegationManager } = __classPrivateFieldGet(this, _DelegationController_getDelegationEnvironment, "f").call(this, chainId);
53
78
  const data = createTypedMessageParams({
54
- chainId,
55
- from: account.address,
56
- delegation,
57
- verifyingContract,
79
+ chainId: hexToNumber(chainId),
80
+ from: delegation.delegator,
81
+ delegation: {
82
+ ...delegation,
83
+ signature: '0x',
84
+ },
85
+ verifyingContract: DelegationManager,
58
86
  });
59
87
  // TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.
60
88
  // Waiting on confirmations team to implement this.
@@ -64,14 +92,16 @@ export class DelegationController extends BaseController {
64
92
  /**
65
93
  * Stores a delegation in storage.
66
94
  *
67
- * @param hash - The hash of the delegation to store.
68
- * @param entry - The delegation entry to store.
95
+ * @param params - The parameters for storing the delegation.
96
+ * @param params.entry - The delegation entry to store.
69
97
  */
70
- store(hash, entry) {
98
+ store(params) {
99
+ const { entry } = params;
100
+ const hash = __classPrivateFieldGet(this, _DelegationController_hashDelegation, "f").call(this, entry.delegation);
71
101
  // If the authority is not the root authority, validate that the
72
102
  // parent entry does exist.
73
- if (!isAddressEqual(entry.data.authority, ROOT_AUTHORITY) &&
74
- !this.state.delegations[entry.data.authority]) {
103
+ if (!isHexEqual(entry.delegation.authority, ROOT_AUTHORITY) &&
104
+ !this.state.delegations[entry.delegation.authority]) {
75
105
  throw new Error('Invalid authority');
76
106
  }
77
107
  this.update((state) => {
@@ -89,14 +119,15 @@ export class DelegationController extends BaseController {
89
119
  const requester = account.address;
90
120
  let list = Object.values(this.state.delegations);
91
121
  if (filter?.from) {
92
- list = list.filter((entry) => isAddressEqual(entry.data.delegator, filter.from));
122
+ list = list.filter((entry) => isHexEqual(entry.delegation.delegator, filter.from));
93
123
  }
94
124
  if (!filter?.from ||
95
- (filter?.from && !isAddressEqual(filter.from, requester))) {
96
- list = list.filter((entry) => isAddressEqual(entry.data.delegate, requester));
125
+ (filter?.from && !isHexEqual(filter.from, requester))) {
126
+ list = list.filter((entry) => isHexEqual(entry.delegation.delegate, requester));
97
127
  }
98
- if (filter?.chainId) {
99
- list = list.filter((entry) => entry.chainId === filter.chainId);
128
+ const filterChainId = filter?.chainId;
129
+ if (filterChainId) {
130
+ list = list.filter((entry) => isHexEqual(entry.chainId, filterChainId));
100
131
  }
101
132
  const tags = filter?.tags;
102
133
  if (tags && tags.length > 0) {
@@ -127,13 +158,13 @@ export class DelegationController extends BaseController {
127
158
  return null;
128
159
  }
129
160
  chain.push(entry);
130
- for (let _hash = entry.data.authority; _hash !== ROOT_AUTHORITY;) {
161
+ for (let _hash = entry.delegation.authority; _hash !== ROOT_AUTHORITY;) {
131
162
  const parent = this.retrieve(_hash);
132
163
  if (!parent) {
133
164
  throw new Error('Invalid delegation chain');
134
165
  }
135
166
  chain.push(parent);
136
- _hash = parent.data.authority;
167
+ _hash = parent.delegation.authority;
137
168
  }
138
169
  return chain;
139
170
  }
@@ -150,23 +181,26 @@ export class DelegationController extends BaseController {
150
181
  return 0;
151
182
  }
152
183
  const entries = Object.entries(this.state.delegations);
153
- let count = 0;
154
184
  const nextHashes = [hash];
185
+ const deletedHashes = [];
155
186
  while (nextHashes.length > 0) {
156
187
  const currentHash = nextHashes.pop();
157
188
  // Find all delegations that have this hash as their authority
158
- const children = entries.filter(([_, v]) => v.data.authority === currentHash);
189
+ const children = entries.filter(([_, v]) => v.delegation.authority === currentHash);
159
190
  // Add the hashes of all child delegations to be processed next
160
191
  children.forEach(([k]) => {
161
192
  nextHashes.push(k);
162
193
  });
163
- // Delete the current delegation
164
- this.update((state) => {
165
- delete state.delegations[currentHash];
166
- });
167
- count += 1;
194
+ deletedHashes.push(currentHash);
168
195
  }
169
- return count;
196
+ // Delete delegations
197
+ this.update((state) => {
198
+ deletedHashes.forEach((h) => {
199
+ delete state.delegations[h];
200
+ });
201
+ });
202
+ return deletedHashes.length;
170
203
  }
171
204
  }
205
+ _DelegationController_hashDelegation = new WeakMap(), _DelegationController_getDelegationEnvironment = new WeakMap();
172
206
  //# sourceMappingURL=DelegationController.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"DelegationController.mjs","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EAAE,oBAAoB,EAAE,qCAAqC;AAEpE,OAAO,EAAE,cAAc,EAAE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AQ,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI,EAAE;YACT,OAAO,CAAC,CAAC;SACV;QAED,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QACvD,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,MAAM,UAAU,GAAU,CAAC,IAAI,CAAC,CAAC;QAEjC,OAAO,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;YAC5B,MAAM,WAAW,GAAG,UAAU,CAAC,GAAG,EAAS,CAAC;YAE5C,8DAA8D;YAC9D,MAAM,QAAQ,GAAG,OAAO,CAAC,MAAM,CAC7B,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,KAAK,WAAW,CAC7C,CAAC;YAEF,+DAA+D;YAC/D,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;gBACvB,UAAU,CAAC,IAAI,CAAC,CAAQ,CAAC,CAAC;YAC5B,CAAC,CAAC,CAAC;YAEH,gCAAgC;YAChC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,OAAO,KAAK,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;YACxC,CAAC,CAAC,CAAC;YACH,KAAK,IAAI,CAAC,CAAC;SACZ;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF","sourcesContent":["import type { StateMetadata } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { SignTypedDataVersion } from '@metamask/keyring-controller';\n\nimport { ROOT_AUTHORITY } from './constants';\nimport type {\n Address,\n Delegation,\n DelegationControllerMessenger,\n DelegationControllerState,\n DelegationEntry,\n DelegationFilter,\n Hex,\n} from './types';\nimport { createTypedMessageParams, isAddressEqual } from './utils';\n\nexport const controllerName = 'DelegationController';\n\nconst delegationControllerMetadata = {\n delegations: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<DelegationControllerState>;\n\n/**\n * Constructs the default {@link DelegationController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link DelegationController} state.\n */\nexport function getDefaultDelegationControllerState(): DelegationControllerState {\n return {\n delegations: {},\n };\n}\n\n/**\n * The {@link DelegationController} class.\n * This controller is meant to be a centralized place to store and sign delegations.\n */\nexport class DelegationController extends BaseController<\n typeof controllerName,\n DelegationControllerState,\n DelegationControllerMessenger\n> {\n constructor({\n messenger,\n state,\n }: {\n messenger: DelegationControllerMessenger;\n state?: Partial<DelegationControllerState>;\n }) {\n super({\n messenger,\n metadata: delegationControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultDelegationControllerState(),\n ...state,\n },\n });\n }\n\n /**\n * Signs a delegation.\n *\n * @param params - The parameters for signing the delegation.\n * @param params.delegation - The delegation to sign.\n * @param params.verifyingContract - The address of the verifying contract (DelegationManager).\n * @param params.chainId - The chainId of the chain to sign the delegation for.\n * @returns The signature of the delegation.\n */\n async sign(params: {\n delegation: Delegation;\n verifyingContract: Address;\n chainId: number;\n }) {\n const { delegation, verifyingContract, chainId } = params;\n\n const account = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n\n const data = createTypedMessageParams({\n chainId,\n from: account.address as Address,\n delegation,\n verifyingContract,\n });\n\n // TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.\n // Waiting on confirmations team to implement this.\n const signature: string = await this.messagingSystem.call(\n 'KeyringController:signTypedMessage',\n data,\n SignTypedDataVersion.V4,\n );\n\n return signature;\n }\n\n /**\n * Stores a delegation in storage.\n *\n * @param hash - The hash of the delegation to store.\n * @param entry - The delegation entry to store.\n */\n store(hash: Hex, entry: DelegationEntry) {\n // If the authority is not the root authority, validate that the\n // parent entry does exist.\n if (\n !isAddressEqual(entry.data.authority, ROOT_AUTHORITY) &&\n !this.state.delegations[entry.data.authority]\n ) {\n throw new Error('Invalid authority');\n }\n this.update((state) => {\n state.delegations[hash] = entry;\n });\n }\n\n /**\n * Lists delegation entries.\n *\n * @param filter - The filter to use to list the delegation entries.\n * @returns A list of delegation entries that match the filter.\n */\n list(filter?: DelegationFilter) {\n const account = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n const requester = account.address as Address;\n\n let list: DelegationEntry[] = Object.values(this.state.delegations);\n\n if (filter?.from) {\n list = list.filter((entry) =>\n isAddressEqual(entry.data.delegator, filter.from as Address),\n );\n }\n\n if (\n !filter?.from ||\n (filter?.from && !isAddressEqual(filter.from, requester))\n ) {\n list = list.filter((entry) =>\n isAddressEqual(entry.data.delegate, requester),\n );\n }\n\n if (filter?.chainId) {\n list = list.filter((entry) => entry.chainId === filter.chainId);\n }\n\n const tags = filter?.tags;\n if (tags && tags.length > 0) {\n // Filter entries that contain all of the filter tags\n list = list.filter((entry) =>\n tags.every((tag) => entry.tags.includes(tag)),\n );\n }\n\n return list;\n }\n\n /**\n * Retrieves the delegation entry for a given delegation hash.\n *\n * @param hash - The hash of the delegation to retrieve.\n * @returns The delegation entry, or null if not found.\n */\n retrieve(hash: Hex) {\n return this.state.delegations[hash] ?? null;\n }\n\n /**\n * Retrieves a delegation chain from a delegation hash.\n *\n * @param hash - The hash of the delegation to retrieve.\n * @returns The delegation chain, or null if not found.\n */\n chain(hash: Hex) {\n const chain: DelegationEntry[] = [];\n\n const entry = this.retrieve(hash);\n if (!entry) {\n return null;\n }\n chain.push(entry);\n\n for (let _hash = entry.data.authority; _hash !== ROOT_AUTHORITY; ) {\n const parent = this.retrieve(_hash);\n if (!parent) {\n throw new Error('Invalid delegation chain');\n }\n chain.push(parent);\n _hash = parent.data.authority;\n }\n\n return chain;\n }\n\n /**\n * Deletes a delegation entrie from storage, along with any other entries\n * that are redelegated from it.\n *\n * @param hash - The hash of the delegation to delete.\n * @returns The number of entries deleted.\n */\n delete(hash: Hex): number {\n const root = this.retrieve(hash);\n if (!root) {\n return 0;\n }\n\n const entries = Object.entries(this.state.delegations);\n let count = 0;\n const nextHashes: Hex[] = [hash];\n\n while (nextHashes.length > 0) {\n const currentHash = nextHashes.pop() as Hex;\n\n // Find all delegations that have this hash as their authority\n const children = entries.filter(\n ([_, v]) => v.data.authority === currentHash,\n );\n\n // Add the hashes of all child delegations to be processed next\n children.forEach(([k]) => {\n nextHashes.push(k as Hex);\n });\n\n // Delete the current delegation\n this.update((state) => {\n delete state.delegations[currentHash];\n });\n count += 1;\n }\n\n return count;\n }\n}\n"]}
1
+ {"version":3,"file":"DelegationController.mjs","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EAAE,oBAAoB,EAAE,qCAAqC;AACpE,OAAO,EAAE,WAAW,EAAE,wBAAwB;AAE9C,OAAO,EAAE,cAAc,EAAE,wBAAoB;AAY7C,OAAO,EAAE,wBAAwB,EAAE,UAAU,EAAE,oBAAgB;AAE/D,MAAM,CAAC,MAAM,cAAc,GAAG,sBAAsB,CAAC;AAErD,MAAM,4BAA4B,GAAG;IACnC,WAAW,EAAE;QACX,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACiD,CAAC;AAErD;;;;;;;GAOG;AACH,SAAS,mCAAmC;IAC1C,OAAO;QACL,WAAW,EAAE,EAAE;KAChB,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,oBAAqB,SAAQ,cAIzC;IAKC;;;;;;;;OAQG;IACH,YAAY,EACV,SAAS,EACT,KAAK,EACL,cAAc,EACd,wBAAwB,GAMzB;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,4BAA4B;YACtC,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE;gBACL,GAAG,mCAAmC,EAAE;gBACxC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;QAhCI,uDAAiD;QAEjD,iEAAkE;QA+BzE,u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AAC;QACjC,MAAM,aAAa,GAAU,EAAE,CAAC;QAEhC,OAAO,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;YAC5B,MAAM,WAAW,GAAG,UAAU,CAAC,GAAG,EAAS,CAAC;YAE5C,8DAA8D;YAC9D,MAAM,QAAQ,GAAG,OAAO,CAAC,MAAM,CAC7B,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,SAAS,KAAK,WAAW,CACnD,CAAC;YAEF,+DAA+D;YAC/D,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;gBACvB,UAAU,CAAC,IAAI,CAAC,CAAQ,CAAC,CAAC;YAC5B,CAAC,CAAC,CAAC;YAEH,aAAa,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;SACjC;QAED,qBAAqB;QACrB,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;gBAC1B,OAAO,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;YAC9B,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,aAAa,CAAC,MAAM,CAAC;IAC9B,CAAC;CACF","sourcesContent":["import type { StateMetadata } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { SignTypedDataVersion } from '@metamask/keyring-controller';\nimport { hexToNumber } from '@metamask/utils';\n\nimport { ROOT_AUTHORITY } from './constants';\nimport type {\n Address,\n Delegation,\n DelegationControllerMessenger,\n DelegationControllerState,\n DelegationEntry,\n DelegationFilter,\n DeleGatorEnvironment,\n Hex,\n UnsignedDelegation,\n} from './types';\nimport { createTypedMessageParams, isHexEqual } from './utils';\n\nexport const controllerName = 'DelegationController';\n\nconst delegationControllerMetadata = {\n delegations: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<DelegationControllerState>;\n\n/**\n * Constructs the default {@link DelegationController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link DelegationController} state.\n */\nfunction getDefaultDelegationControllerState(): DelegationControllerState {\n return {\n delegations: {},\n };\n}\n\n/**\n * The {@link DelegationController} class.\n * This controller is meant to be a centralized place to store and sign delegations.\n */\nexport class DelegationController extends BaseController<\n typeof controllerName,\n DelegationControllerState,\n DelegationControllerMessenger\n> {\n readonly #hashDelegation: (delegation: Delegation) => Hex;\n\n readonly #getDelegationEnvironment: (chainId: Hex) => DeleGatorEnvironment;\n\n /**\n * Constructs a new {@link DelegationController} instance.\n *\n * @param params - The parameters for constructing the controller.\n * @param params.messenger - The messenger instance to use for the controller.\n * @param params.state - The initial state for the controller.\n * @param params.hashDelegation - A function to hash delegations.\n * @param params.getDelegationEnvironment - A function to get the delegation environment for a given chainId.\n */\n constructor({\n messenger,\n state,\n hashDelegation,\n getDelegationEnvironment,\n }: {\n messenger: DelegationControllerMessenger;\n state?: Partial<DelegationControllerState>;\n hashDelegation: (delegation: Delegation) => Hex;\n getDelegationEnvironment: (chainId: Hex) => DeleGatorEnvironment;\n }) {\n super({\n messenger,\n metadata: delegationControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultDelegationControllerState(),\n ...state,\n },\n });\n this.#hashDelegation = hashDelegation;\n this.#getDelegationEnvironment = getDelegationEnvironment;\n }\n\n /**\n * Signs a delegation.\n *\n * @param params - The parameters for signing the delegation.\n * @param params.delegation - The delegation to sign.\n * @param params.chainId - The chainId of the chain to sign the delegation for.\n * @returns The signature of the delegation.\n */\n async signDelegation(params: {\n delegation: UnsignedDelegation;\n chainId: Hex;\n }) {\n const { delegation, chainId } = params;\n const { DelegationManager } = this.#getDelegationEnvironment(chainId);\n\n const data = createTypedMessageParams({\n chainId: hexToNumber(chainId),\n from: delegation.delegator,\n delegation: {\n ...delegation,\n signature: '0x',\n },\n verifyingContract: DelegationManager,\n });\n\n // TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.\n // Waiting on confirmations team to implement this.\n const signature: string = await this.messagingSystem.call(\n 'KeyringController:signTypedMessage',\n data,\n SignTypedDataVersion.V4,\n );\n\n return signature;\n }\n\n /**\n * Stores a delegation in storage.\n *\n * @param params - The parameters for storing the delegation.\n * @param params.entry - The delegation entry to store.\n */\n store(params: { entry: DelegationEntry }) {\n const { entry } = params;\n const hash = this.#hashDelegation(entry.delegation);\n\n // If the authority is not the root authority, validate that the\n // parent entry does exist.\n if (\n !isHexEqual(entry.delegation.authority, ROOT_AUTHORITY) &&\n !this.state.delegations[entry.delegation.authority]\n ) {\n throw new Error('Invalid authority');\n }\n this.update((state) => {\n state.delegations[hash] = entry;\n });\n }\n\n /**\n * Lists delegation entries.\n *\n * @param filter - The filter to use to list the delegation entries.\n * @returns A list of delegation entries that match the filter.\n */\n list(filter?: DelegationFilter) {\n const account = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n const requester = account.address as Address;\n\n let list: DelegationEntry[] = Object.values(this.state.delegations);\n\n if (filter?.from) {\n list = list.filter((entry) =>\n isHexEqual(entry.delegation.delegator, filter.from as Address),\n );\n }\n\n if (\n !filter?.from ||\n (filter?.from && !isHexEqual(filter.from, requester))\n ) {\n list = list.filter((entry) =>\n isHexEqual(entry.delegation.delegate, requester),\n );\n }\n\n const filterChainId = filter?.chainId;\n if (filterChainId) {\n list = list.filter((entry) => isHexEqual(entry.chainId, filterChainId));\n }\n\n const tags = filter?.tags;\n if (tags && tags.length > 0) {\n // Filter entries that contain all of the filter tags\n list = list.filter((entry) =>\n tags.every((tag) => entry.tags.includes(tag)),\n );\n }\n\n return list;\n }\n\n /**\n * Retrieves the delegation entry for a given delegation hash.\n *\n * @param hash - The hash of the delegation to retrieve.\n * @returns The delegation entry, or null if not found.\n */\n retrieve(hash: Hex) {\n return this.state.delegations[hash] ?? null;\n }\n\n /**\n * Retrieves a delegation chain from a delegation hash.\n *\n * @param hash - The hash of the delegation to retrieve.\n * @returns The delegation chain, or null if not found.\n */\n chain(hash: Hex) {\n const chain: DelegationEntry[] = [];\n\n const entry = this.retrieve(hash);\n if (!entry) {\n return null;\n }\n chain.push(entry);\n\n for (let _hash = entry.delegation.authority; _hash !== ROOT_AUTHORITY; ) {\n const parent = this.retrieve(_hash);\n if (!parent) {\n throw new Error('Invalid delegation chain');\n }\n chain.push(parent);\n _hash = parent.delegation.authority;\n }\n\n return chain;\n }\n\n /**\n * Deletes a delegation entrie from storage, along with any other entries\n * that are redelegated from it.\n *\n * @param hash - The hash of the delegation to delete.\n * @returns The number of entries deleted.\n */\n delete(hash: Hex): number {\n const root = this.retrieve(hash);\n if (!root) {\n return 0;\n }\n\n const entries = Object.entries(this.state.delegations);\n const nextHashes: Hex[] = [hash];\n const deletedHashes: Hex[] = [];\n\n while (nextHashes.length > 0) {\n const currentHash = nextHashes.pop() as Hex;\n\n // Find all delegations that have this hash as their authority\n const children = entries.filter(\n ([_, v]) => v.delegation.authority === currentHash,\n );\n\n // Add the hashes of all child delegations to be processed next\n children.forEach(([k]) => {\n nextHashes.push(k as Hex);\n });\n\n deletedHashes.push(currentHash);\n }\n\n // Delete delegations\n this.update((state) => {\n deletedHashes.forEach((h) => {\n delete state.delegations[h];\n });\n });\n\n return deletedHashes.length;\n }\n}\n"]}
package/dist/index.cjs CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDefaultDelegationControllerState = exports.DelegationController = void 0;
3
+ exports.DelegationController = void 0;
4
4
  var DelegationController_1 = require("./DelegationController.cjs");
5
5
  Object.defineProperty(exports, "DelegationController", { enumerable: true, get: function () { return DelegationController_1.DelegationController; } });
6
- Object.defineProperty(exports, "getDefaultDelegationControllerState", { enumerable: true, get: function () { return DelegationController_1.getDefaultDelegationControllerState; } });
7
6
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAQA,mEAGgC;AAF9B,4HAAA,oBAAoB,OAAA;AACpB,2IAAA,mCAAmC,OAAA","sourcesContent":["export type {\n DelegationControllerActions,\n DelegationControllerEvents,\n DelegationControllerMessenger,\n DelegationEntry,\n DelegationFilter,\n} from './types';\n\nexport {\n DelegationController,\n getDefaultDelegationControllerState,\n} from './DelegationController';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAcA,mEAA8D;AAArD,4HAAA,oBAAoB,OAAA","sourcesContent":["export type {\n DelegationControllerSignDelegationAction,\n DelegationControllerStoreAction,\n DelegationControllerListAction,\n DelegationControllerRetrieveAction,\n DelegationControllerChainAction,\n DelegationControllerDeleteAction,\n DelegationControllerActions,\n DelegationControllerEvents,\n DelegationControllerMessenger,\n DelegationEntry,\n DelegationFilter,\n} from './types';\n\nexport { DelegationController } from './DelegationController';\n"]}
package/dist/index.d.cts CHANGED
@@ -1,3 +1,3 @@
1
- export type { DelegationControllerActions, DelegationControllerEvents, DelegationControllerMessenger, DelegationEntry, DelegationFilter, } from "./types.cjs";
2
- export { DelegationController, getDefaultDelegationControllerState, } from "./DelegationController.cjs";
1
+ export type { DelegationControllerSignDelegationAction, DelegationControllerStoreAction, DelegationControllerListAction, DelegationControllerRetrieveAction, DelegationControllerChainAction, DelegationControllerDeleteAction, DelegationControllerActions, DelegationControllerEvents, DelegationControllerMessenger, DelegationEntry, DelegationFilter, } from "./types.cjs";
2
+ export { DelegationController } from "./DelegationController.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,2BAA2B,EAC3B,0BAA0B,EAC1B,6BAA6B,EAC7B,eAAe,EACf,gBAAgB,GACjB,oBAAgB;AAEjB,OAAO,EACL,oBAAoB,EACpB,mCAAmC,GACpC,mCAA+B"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,wCAAwC,EACxC,+BAA+B,EAC/B,8BAA8B,EAC9B,kCAAkC,EAClC,+BAA+B,EAC/B,gCAAgC,EAChC,2BAA2B,EAC3B,0BAA0B,EAC1B,6BAA6B,EAC7B,eAAe,EACf,gBAAgB,GACjB,oBAAgB;AAEjB,OAAO,EAAE,oBAAoB,EAAE,mCAA+B"}
package/dist/index.d.mts CHANGED
@@ -1,3 +1,3 @@
1
- export type { DelegationControllerActions, DelegationControllerEvents, DelegationControllerMessenger, DelegationEntry, DelegationFilter, } from "./types.mjs";
2
- export { DelegationController, getDefaultDelegationControllerState, } from "./DelegationController.mjs";
1
+ export type { DelegationControllerSignDelegationAction, DelegationControllerStoreAction, DelegationControllerListAction, DelegationControllerRetrieveAction, DelegationControllerChainAction, DelegationControllerDeleteAction, DelegationControllerActions, DelegationControllerEvents, DelegationControllerMessenger, DelegationEntry, DelegationFilter, } from "./types.mjs";
2
+ export { DelegationController } from "./DelegationController.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,2BAA2B,EAC3B,0BAA0B,EAC1B,6BAA6B,EAC7B,eAAe,EACf,gBAAgB,GACjB,oBAAgB;AAEjB,OAAO,EACL,oBAAoB,EACpB,mCAAmC,GACpC,mCAA+B"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,wCAAwC,EACxC,+BAA+B,EAC/B,8BAA8B,EAC9B,kCAAkC,EAClC,+BAA+B,EAC/B,gCAAgC,EAChC,2BAA2B,EAC3B,0BAA0B,EAC1B,6BAA6B,EAC7B,eAAe,EACf,gBAAgB,GACjB,oBAAgB;AAEjB,OAAO,EAAE,oBAAoB,EAAE,mCAA+B"}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export { DelegationController, getDefaultDelegationControllerState } from "./DelegationController.mjs";
1
+ export { DelegationController } from "./DelegationController.mjs";
2
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,oBAAoB,EACpB,mCAAmC,EACpC,mCAA+B","sourcesContent":["export type {\n DelegationControllerActions,\n DelegationControllerEvents,\n DelegationControllerMessenger,\n DelegationEntry,\n DelegationFilter,\n} from './types';\n\nexport {\n DelegationController,\n getDefaultDelegationControllerState,\n} from './DelegationController';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,oBAAoB,EAAE,mCAA+B","sourcesContent":["export type {\n DelegationControllerSignDelegationAction,\n DelegationControllerStoreAction,\n DelegationControllerListAction,\n DelegationControllerRetrieveAction,\n DelegationControllerChainAction,\n DelegationControllerDeleteAction,\n DelegationControllerActions,\n DelegationControllerEvents,\n DelegationControllerMessenger,\n DelegationEntry,\n DelegationFilter,\n} from './types';\n\nexport { DelegationController } from './DelegationController';\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"types.cjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n} from '@metamask/base-controller';\nimport type { KeyringControllerSignTypedMessageAction } from '@metamask/keyring-controller';\n\nimport type {\n controllerName,\n DelegationController,\n} from './DelegationController';\n\ntype Hex = `0x${string}`;\ntype Address = `0x${string}`;\n\nexport type { Address, Hex };\n\nexport type Caveat = {\n enforcer: Hex;\n terms: Hex;\n args: Hex;\n};\n\nexport type Delegation = {\n delegate: Hex;\n delegator: Hex;\n authority: Hex;\n caveats: Caveat[];\n salt: Hex;\n signature: Hex;\n};\n\nexport type DelegationStruct = Omit<Delegation, 'salt'> & {\n salt: bigint;\n};\n\nexport type DelegationEntry = {\n tags: string[];\n chainId: number;\n data: Delegation;\n meta?: string;\n};\n\nexport type DelegationFilter = {\n chainId?: number;\n tags?: string[];\n from?: Address;\n};\n\nexport type DelegationControllerState = {\n delegations: {\n [hash: Hex]: DelegationEntry;\n };\n};\n\nexport type DelegationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n DelegationControllerState\n>;\n\nexport type DelegationControllerStoreAction = {\n type: `${typeof controllerName}:store`;\n handler: DelegationController['store'];\n};\n\nexport type DelegationControllerSignAction = {\n type: `${typeof controllerName}:sign`;\n handler: DelegationController['sign'];\n};\n\nexport type DelegationControllerActions =\n | DelegationControllerGetStateAction\n | DelegationControllerStoreAction\n | DelegationControllerSignAction;\n\nexport type DelegationControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n DelegationControllerState\n>;\n\nexport type DelegationControllerEvents = DelegationControllerStateChangeEvent;\n\ntype AllowedActions =\n | KeyringControllerSignTypedMessageAction\n | AccountsControllerGetSelectedAccountAction;\n\ntype AllowedEvents = never;\n\nexport type DelegationControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n DelegationControllerActions | AllowedActions,\n DelegationControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n"]}
1
+ {"version":3,"file":"types.cjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n} from '@metamask/base-controller';\nimport type { KeyringControllerSignTypedMessageAction } from '@metamask/keyring-controller';\n\nimport type {\n controllerName,\n DelegationController,\n} from './DelegationController';\n\ntype Hex = `0x${string}`;\ntype Address = `0x${string}`;\n\nexport type { Address, Hex };\n\n/**\n * A version agnostic blob of contract addresses required for the DeleGator system to function.\n */\nexport type DeleGatorEnvironment = {\n DelegationManager: Hex;\n EntryPoint: Hex;\n SimpleFactory: Hex;\n implementations: {\n [implementation: string]: Hex;\n };\n caveatEnforcers: {\n [enforcer: string]: Hex;\n };\n};\n\n/**\n * A delegation caveat is a condition that must be met in order for a delegation\n * to be valid. The caveat is defined by an enforcer, terms, and arguments.\n *\n * @see https://docs.gator.metamask.io/concepts/caveat-enforcers\n */\nexport type Caveat = {\n enforcer: Hex;\n terms: Hex;\n args: Hex;\n};\n\n/**\n * A delegation is a signed statement that gives a delegate permission to\n * act on behalf of a delegator. The permissions are defined by a set of caveats.\n * The caveats are a set of conditions that must be met in order for the delegation\n * to be valid.\n *\n * @see https://docs.gator.metamask.io/concepts/delegation\n */\nexport type Delegation = {\n /** The address of the delegate. */\n delegate: Hex;\n /** The address of the delegator. */\n delegator: Hex;\n /** The hash of the parent delegation, or the root authority if this is the root delegation. */\n authority: Hex;\n /** The terms of the delegation. */\n caveats: Caveat[];\n /** The salt used to generate the delegation signature. */\n salt: Hex;\n /** The signature of the delegation. */\n signature: Hex;\n};\n\n/** An unsigned delegation is a delegation without a signature. */\nexport type UnsignedDelegation = Omit<Delegation, 'signature'>;\n\nexport type DelegationStruct = Omit<Delegation, 'salt'> & {\n salt: bigint;\n};\n\nexport type DelegationEntry = {\n tags: string[];\n chainId: Hex;\n delegation: Delegation;\n meta?: string;\n};\n\nexport type DelegationFilter = {\n chainId?: Hex;\n tags?: string[];\n from?: Address;\n};\n\nexport type DelegationControllerState = {\n delegations: {\n [hash: Hex]: DelegationEntry;\n };\n};\n\nexport type DelegationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n DelegationControllerState\n>;\n\nexport type DelegationControllerSignDelegationAction = {\n type: `${typeof controllerName}:signDelegation`;\n handler: DelegationController['signDelegation'];\n};\n\nexport type DelegationControllerStoreAction = {\n type: `${typeof controllerName}:store`;\n handler: DelegationController['store'];\n};\n\nexport type DelegationControllerListAction = {\n type: `${typeof controllerName}:list`;\n handler: DelegationController['list'];\n};\n\nexport type DelegationControllerRetrieveAction = {\n type: `${typeof controllerName}:retrieve`;\n handler: DelegationController['retrieve'];\n};\n\nexport type DelegationControllerChainAction = {\n type: `${typeof controllerName}:chain`;\n handler: DelegationController['chain'];\n};\n\nexport type DelegationControllerDeleteAction = {\n type: `${typeof controllerName}:delete`;\n handler: DelegationController['delete'];\n};\n\nexport type DelegationControllerActions =\n | DelegationControllerGetStateAction\n | DelegationControllerSignDelegationAction\n | DelegationControllerStoreAction\n | DelegationControllerListAction\n | DelegationControllerRetrieveAction\n | DelegationControllerChainAction\n | DelegationControllerDeleteAction;\n\nexport type DelegationControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n DelegationControllerState\n>;\n\nexport type DelegationControllerEvents = DelegationControllerStateChangeEvent;\n\ntype AllowedActions =\n | KeyringControllerSignTypedMessageAction\n | AccountsControllerGetSelectedAccountAction;\n\ntype AllowedEvents = never;\n\nexport type DelegationControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n DelegationControllerActions | AllowedActions,\n DelegationControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n"]}
package/dist/types.d.cts CHANGED
@@ -5,30 +5,66 @@ import type { controllerName, DelegationController } from "./DelegationControlle
5
5
  type Hex = `0x${string}`;
6
6
  type Address = `0x${string}`;
7
7
  export type { Address, Hex };
8
+ /**
9
+ * A version agnostic blob of contract addresses required for the DeleGator system to function.
10
+ */
11
+ export type DeleGatorEnvironment = {
12
+ DelegationManager: Hex;
13
+ EntryPoint: Hex;
14
+ SimpleFactory: Hex;
15
+ implementations: {
16
+ [implementation: string]: Hex;
17
+ };
18
+ caveatEnforcers: {
19
+ [enforcer: string]: Hex;
20
+ };
21
+ };
22
+ /**
23
+ * A delegation caveat is a condition that must be met in order for a delegation
24
+ * to be valid. The caveat is defined by an enforcer, terms, and arguments.
25
+ *
26
+ * @see https://docs.gator.metamask.io/concepts/caveat-enforcers
27
+ */
8
28
  export type Caveat = {
9
29
  enforcer: Hex;
10
30
  terms: Hex;
11
31
  args: Hex;
12
32
  };
33
+ /**
34
+ * A delegation is a signed statement that gives a delegate permission to
35
+ * act on behalf of a delegator. The permissions are defined by a set of caveats.
36
+ * The caveats are a set of conditions that must be met in order for the delegation
37
+ * to be valid.
38
+ *
39
+ * @see https://docs.gator.metamask.io/concepts/delegation
40
+ */
13
41
  export type Delegation = {
42
+ /** The address of the delegate. */
14
43
  delegate: Hex;
44
+ /** The address of the delegator. */
15
45
  delegator: Hex;
46
+ /** The hash of the parent delegation, or the root authority if this is the root delegation. */
16
47
  authority: Hex;
48
+ /** The terms of the delegation. */
17
49
  caveats: Caveat[];
50
+ /** The salt used to generate the delegation signature. */
18
51
  salt: Hex;
52
+ /** The signature of the delegation. */
19
53
  signature: Hex;
20
54
  };
55
+ /** An unsigned delegation is a delegation without a signature. */
56
+ export type UnsignedDelegation = Omit<Delegation, 'signature'>;
21
57
  export type DelegationStruct = Omit<Delegation, 'salt'> & {
22
58
  salt: bigint;
23
59
  };
24
60
  export type DelegationEntry = {
25
61
  tags: string[];
26
- chainId: number;
27
- data: Delegation;
62
+ chainId: Hex;
63
+ delegation: Delegation;
28
64
  meta?: string;
29
65
  };
30
66
  export type DelegationFilter = {
31
- chainId?: number;
67
+ chainId?: Hex;
32
68
  tags?: string[];
33
69
  from?: Address;
34
70
  };
@@ -38,15 +74,31 @@ export type DelegationControllerState = {
38
74
  };
39
75
  };
40
76
  export type DelegationControllerGetStateAction = ControllerGetStateAction<typeof controllerName, DelegationControllerState>;
77
+ export type DelegationControllerSignDelegationAction = {
78
+ type: `${typeof controllerName}:signDelegation`;
79
+ handler: DelegationController['signDelegation'];
80
+ };
41
81
  export type DelegationControllerStoreAction = {
42
82
  type: `${typeof controllerName}:store`;
43
83
  handler: DelegationController['store'];
44
84
  };
45
- export type DelegationControllerSignAction = {
46
- type: `${typeof controllerName}:sign`;
47
- handler: DelegationController['sign'];
85
+ export type DelegationControllerListAction = {
86
+ type: `${typeof controllerName}:list`;
87
+ handler: DelegationController['list'];
88
+ };
89
+ export type DelegationControllerRetrieveAction = {
90
+ type: `${typeof controllerName}:retrieve`;
91
+ handler: DelegationController['retrieve'];
92
+ };
93
+ export type DelegationControllerChainAction = {
94
+ type: `${typeof controllerName}:chain`;
95
+ handler: DelegationController['chain'];
96
+ };
97
+ export type DelegationControllerDeleteAction = {
98
+ type: `${typeof controllerName}:delete`;
99
+ handler: DelegationController['delete'];
48
100
  };
49
- export type DelegationControllerActions = DelegationControllerGetStateAction | DelegationControllerStoreAction | DelegationControllerSignAction;
101
+ export type DelegationControllerActions = DelegationControllerGetStateAction | DelegationControllerSignDelegationAction | DelegationControllerStoreAction | DelegationControllerListAction | DelegationControllerRetrieveAction | DelegationControllerChainAction | DelegationControllerDeleteAction;
50
102
  export type DelegationControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, DelegationControllerState>;
51
103
  export type DelegationControllerEvents = DelegationControllerStateChangeEvent;
52
104
  type AllowedActions = KeyringControllerSignTypedMessageAction | AccountsControllerGetSelectedAccountAction;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0CAA0C,EAAE,sCAAsC;AAChG,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EACpB,kCAAkC;AACnC,OAAO,KAAK,EAAE,uCAAuC,EAAE,qCAAqC;AAE5F,OAAO,KAAK,EACV,cAAc,EACd,oBAAoB,EACrB,mCAA+B;AAEhC,KAAK,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AACzB,KAAK,OAAO,GAAG,KAAK,MAAM,EAAE,CAAC;AAE7B,YAAY,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC;AAE7B,MAAM,MAAM,MAAM,GAAG;IACnB,QAAQ,EAAE,GAAG,CAAC;IACd,KAAK,EAAE,GAA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AM,MAAM,6BAA6B,GAAG,mBAAmB,CAC7D,OAAO,cAAc,EACrB,2BAA2B,GAAG,cAAc,EAC5C,0BAA0B,GAAG,aAAa,EAC1C,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC"}
package/dist/types.d.mts CHANGED
@@ -5,30 +5,66 @@ import type { controllerName, DelegationController } from "./DelegationControlle
5
5
  type Hex = `0x${string}`;
6
6
  type Address = `0x${string}`;
7
7
  export type { Address, Hex };
8
+ /**
9
+ * A version agnostic blob of contract addresses required for the DeleGator system to function.
10
+ */
11
+ export type DeleGatorEnvironment = {
12
+ DelegationManager: Hex;
13
+ EntryPoint: Hex;
14
+ SimpleFactory: Hex;
15
+ implementations: {
16
+ [implementation: string]: Hex;
17
+ };
18
+ caveatEnforcers: {
19
+ [enforcer: string]: Hex;
20
+ };
21
+ };
22
+ /**
23
+ * A delegation caveat is a condition that must be met in order for a delegation
24
+ * to be valid. The caveat is defined by an enforcer, terms, and arguments.
25
+ *
26
+ * @see https://docs.gator.metamask.io/concepts/caveat-enforcers
27
+ */
8
28
  export type Caveat = {
9
29
  enforcer: Hex;
10
30
  terms: Hex;
11
31
  args: Hex;
12
32
  };
33
+ /**
34
+ * A delegation is a signed statement that gives a delegate permission to
35
+ * act on behalf of a delegator. The permissions are defined by a set of caveats.
36
+ * The caveats are a set of conditions that must be met in order for the delegation
37
+ * to be valid.
38
+ *
39
+ * @see https://docs.gator.metamask.io/concepts/delegation
40
+ */
13
41
  export type Delegation = {
42
+ /** The address of the delegate. */
14
43
  delegate: Hex;
44
+ /** The address of the delegator. */
15
45
  delegator: Hex;
46
+ /** The hash of the parent delegation, or the root authority if this is the root delegation. */
16
47
  authority: Hex;
48
+ /** The terms of the delegation. */
17
49
  caveats: Caveat[];
50
+ /** The salt used to generate the delegation signature. */
18
51
  salt: Hex;
52
+ /** The signature of the delegation. */
19
53
  signature: Hex;
20
54
  };
55
+ /** An unsigned delegation is a delegation without a signature. */
56
+ export type UnsignedDelegation = Omit<Delegation, 'signature'>;
21
57
  export type DelegationStruct = Omit<Delegation, 'salt'> & {
22
58
  salt: bigint;
23
59
  };
24
60
  export type DelegationEntry = {
25
61
  tags: string[];
26
- chainId: number;
27
- data: Delegation;
62
+ chainId: Hex;
63
+ delegation: Delegation;
28
64
  meta?: string;
29
65
  };
30
66
  export type DelegationFilter = {
31
- chainId?: number;
67
+ chainId?: Hex;
32
68
  tags?: string[];
33
69
  from?: Address;
34
70
  };
@@ -38,15 +74,31 @@ export type DelegationControllerState = {
38
74
  };
39
75
  };
40
76
  export type DelegationControllerGetStateAction = ControllerGetStateAction<typeof controllerName, DelegationControllerState>;
77
+ export type DelegationControllerSignDelegationAction = {
78
+ type: `${typeof controllerName}:signDelegation`;
79
+ handler: DelegationController['signDelegation'];
80
+ };
41
81
  export type DelegationControllerStoreAction = {
42
82
  type: `${typeof controllerName}:store`;
43
83
  handler: DelegationController['store'];
44
84
  };
45
- export type DelegationControllerSignAction = {
46
- type: `${typeof controllerName}:sign`;
47
- handler: DelegationController['sign'];
85
+ export type DelegationControllerListAction = {
86
+ type: `${typeof controllerName}:list`;
87
+ handler: DelegationController['list'];
88
+ };
89
+ export type DelegationControllerRetrieveAction = {
90
+ type: `${typeof controllerName}:retrieve`;
91
+ handler: DelegationController['retrieve'];
92
+ };
93
+ export type DelegationControllerChainAction = {
94
+ type: `${typeof controllerName}:chain`;
95
+ handler: DelegationController['chain'];
96
+ };
97
+ export type DelegationControllerDeleteAction = {
98
+ type: `${typeof controllerName}:delete`;
99
+ handler: DelegationController['delete'];
48
100
  };
49
- export type DelegationControllerActions = DelegationControllerGetStateAction | DelegationControllerStoreAction | DelegationControllerSignAction;
101
+ export type DelegationControllerActions = DelegationControllerGetStateAction | DelegationControllerSignDelegationAction | DelegationControllerStoreAction | DelegationControllerListAction | DelegationControllerRetrieveAction | DelegationControllerChainAction | DelegationControllerDeleteAction;
50
102
  export type DelegationControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, DelegationControllerState>;
51
103
  export type DelegationControllerEvents = DelegationControllerStateChangeEvent;
52
104
  type AllowedActions = KeyringControllerSignTypedMessageAction | AccountsControllerGetSelectedAccountAction;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0CAA0C,EAAE,sCAAsC;AAChG,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EACpB,kCAAkC;AACnC,OAAO,KAAK,EAAE,uCAAuC,EAAE,qCAAqC;AAE5F,OAAO,KAAK,EACV,cAAc,EACd,oBAAoB,EACrB,mCAA+B;AAEhC,KAAK,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AACzB,KAAK,OAAO,GAAG,KAAK,MAAM,EAAE,CAAC;AAE7B,YAAY,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC;AAE7B,MAAM,MAAM,MAAM,GAAG;IACnB,QAAQ,EAAE,GAAG,CAAC;IACd,KAAK,EAAE,GAAG,CAAC;IACX,IAAI,EAAE,GAAG,CAAC;CACX,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,EAAE,GAAG,CAAC;IACd,SAAS,EAAE,GAAG,CAAC;IACf,SAAS,EAAE,GAAG,CAAC;IACf,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,IAAI,EAAE,GAAG,CAAC;IACV,SAAS,EAAE,GAAG,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,GAAG;IACxD,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF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C;AAEF,MAAM,MAAM,+BAA+B,GAAG;IAC5C,IAAI,EAAE,GAAG,OAAO,cAAc,QAAQ,CAAC;IACvC,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,CAAC;CACxC,CAAC;AAEF,MAAM,MAAM,gCAAgC,GAAG;IAC7C,IAAI,EAAE,GAAG,OAAO,cAAc,SAAS,CAAC;IACxC,OAAO,EAAE,oBAAoB,CAAC,QAAQ,CAAC,CAAC;CACzC,CAAC;AAEF,MAAM,MAAM,2BAA2B,GACnC,kCAAkC,GAClC,wCAAwC,GACxC,+BAA+B,GAC/B,8BAA8B,GAC9B,kCAAkC,GAClC,+BAA+B,GAC/B,gCAAgC,CAAC;AAErC,MAAM,MAAM,oCAAoC,GAAG,0BAA0B,CAC3E,OAAO,cAAc,EACrB,yBAAyB,CAC1B,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG,oCAAoC,CAAC;AAE9E,KAAK,cAAc,GACf,uCAAuC,GACvC,0CAA0C,CAAC;AAE/C,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B,MAAM,MAAM,6BAA6B,GAAG,mBAAmB,CAC7D,OAAO,cAAc,EACrB,2BAA2B,GAAG,cAAc,EAC5C,0BAA0B,GAAG,aAAa,EAC1C,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n} from '@metamask/base-controller';\nimport type { KeyringControllerSignTypedMessageAction } from '@metamask/keyring-controller';\n\nimport type {\n controllerName,\n DelegationController,\n} from './DelegationController';\n\ntype Hex = `0x${string}`;\ntype Address = `0x${string}`;\n\nexport type { Address, Hex };\n\nexport type Caveat = {\n enforcer: Hex;\n terms: Hex;\n args: Hex;\n};\n\nexport type Delegation = {\n delegate: Hex;\n delegator: Hex;\n authority: Hex;\n caveats: Caveat[];\n salt: Hex;\n signature: Hex;\n};\n\nexport type DelegationStruct = Omit<Delegation, 'salt'> & {\n salt: bigint;\n};\n\nexport type DelegationEntry = {\n tags: string[];\n chainId: number;\n data: Delegation;\n meta?: string;\n};\n\nexport type DelegationFilter = {\n chainId?: number;\n tags?: string[];\n from?: Address;\n};\n\nexport type DelegationControllerState = {\n delegations: {\n [hash: Hex]: DelegationEntry;\n };\n};\n\nexport type DelegationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n DelegationControllerState\n>;\n\nexport type DelegationControllerStoreAction = {\n type: `${typeof controllerName}:store`;\n handler: DelegationController['store'];\n};\n\nexport type DelegationControllerSignAction = {\n type: `${typeof controllerName}:sign`;\n handler: DelegationController['sign'];\n};\n\nexport type DelegationControllerActions =\n | DelegationControllerGetStateAction\n | DelegationControllerStoreAction\n | DelegationControllerSignAction;\n\nexport type DelegationControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n DelegationControllerState\n>;\n\nexport type DelegationControllerEvents = DelegationControllerStateChangeEvent;\n\ntype AllowedActions =\n | KeyringControllerSignTypedMessageAction\n | AccountsControllerGetSelectedAccountAction;\n\ntype AllowedEvents = never;\n\nexport type DelegationControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n DelegationControllerActions | AllowedActions,\n DelegationControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n"]}
1
+ {"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n} from '@metamask/base-controller';\nimport type { KeyringControllerSignTypedMessageAction } from '@metamask/keyring-controller';\n\nimport type {\n controllerName,\n DelegationController,\n} from './DelegationController';\n\ntype Hex = `0x${string}`;\ntype Address = `0x${string}`;\n\nexport type { Address, Hex };\n\n/**\n * A version agnostic blob of contract addresses required for the DeleGator system to function.\n */\nexport type DeleGatorEnvironment = {\n DelegationManager: Hex;\n EntryPoint: Hex;\n SimpleFactory: Hex;\n implementations: {\n [implementation: string]: Hex;\n };\n caveatEnforcers: {\n [enforcer: string]: Hex;\n };\n};\n\n/**\n * A delegation caveat is a condition that must be met in order for a delegation\n * to be valid. The caveat is defined by an enforcer, terms, and arguments.\n *\n * @see https://docs.gator.metamask.io/concepts/caveat-enforcers\n */\nexport type Caveat = {\n enforcer: Hex;\n terms: Hex;\n args: Hex;\n};\n\n/**\n * A delegation is a signed statement that gives a delegate permission to\n * act on behalf of a delegator. The permissions are defined by a set of caveats.\n * The caveats are a set of conditions that must be met in order for the delegation\n * to be valid.\n *\n * @see https://docs.gator.metamask.io/concepts/delegation\n */\nexport type Delegation = {\n /** The address of the delegate. */\n delegate: Hex;\n /** The address of the delegator. */\n delegator: Hex;\n /** The hash of the parent delegation, or the root authority if this is the root delegation. */\n authority: Hex;\n /** The terms of the delegation. */\n caveats: Caveat[];\n /** The salt used to generate the delegation signature. */\n salt: Hex;\n /** The signature of the delegation. */\n signature: Hex;\n};\n\n/** An unsigned delegation is a delegation without a signature. */\nexport type UnsignedDelegation = Omit<Delegation, 'signature'>;\n\nexport type DelegationStruct = Omit<Delegation, 'salt'> & {\n salt: bigint;\n};\n\nexport type DelegationEntry = {\n tags: string[];\n chainId: Hex;\n delegation: Delegation;\n meta?: string;\n};\n\nexport type DelegationFilter = {\n chainId?: Hex;\n tags?: string[];\n from?: Address;\n};\n\nexport type DelegationControllerState = {\n delegations: {\n [hash: Hex]: DelegationEntry;\n };\n};\n\nexport type DelegationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n DelegationControllerState\n>;\n\nexport type DelegationControllerSignDelegationAction = {\n type: `${typeof controllerName}:signDelegation`;\n handler: DelegationController['signDelegation'];\n};\n\nexport type DelegationControllerStoreAction = {\n type: `${typeof controllerName}:store`;\n handler: DelegationController['store'];\n};\n\nexport type DelegationControllerListAction = {\n type: `${typeof controllerName}:list`;\n handler: DelegationController['list'];\n};\n\nexport type DelegationControllerRetrieveAction = {\n type: `${typeof controllerName}:retrieve`;\n handler: DelegationController['retrieve'];\n};\n\nexport type DelegationControllerChainAction = {\n type: `${typeof controllerName}:chain`;\n handler: DelegationController['chain'];\n};\n\nexport type DelegationControllerDeleteAction = {\n type: `${typeof controllerName}:delete`;\n handler: DelegationController['delete'];\n};\n\nexport type DelegationControllerActions =\n | DelegationControllerGetStateAction\n | DelegationControllerSignDelegationAction\n | DelegationControllerStoreAction\n | DelegationControllerListAction\n | DelegationControllerRetrieveAction\n | DelegationControllerChainAction\n | DelegationControllerDeleteAction;\n\nexport type DelegationControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n DelegationControllerState\n>;\n\nexport type DelegationControllerEvents = DelegationControllerStateChangeEvent;\n\ntype AllowedActions =\n | KeyringControllerSignTypedMessageAction\n | AccountsControllerGetSelectedAccountAction;\n\ntype AllowedEvents = never;\n\nexport type DelegationControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n DelegationControllerActions | AllowedActions,\n DelegationControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n"]}
package/dist/utils.cjs CHANGED
@@ -1,19 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createTypedMessageParams = exports.toDelegationStruct = exports.isAddressEqual = void 0;
3
+ exports.createTypedMessageParams = exports.toDelegationStruct = exports.isHexEqual = void 0;
4
4
  const utils_1 = require("@metamask/utils");
5
5
  const constants_1 = require("./constants.cjs");
6
6
  /**
7
- * Checks if two addresses are equal.
7
+ * Checks if two hex strings are equal.
8
8
  *
9
- * @param a - The first address.
10
- * @param b - The second address.
11
- * @returns True if the addresses are equal, false otherwise.
9
+ * @param a - The first hex string.
10
+ * @param b - The second hex string.
11
+ * @returns True if the hex strings are equal, false otherwise.
12
12
  */
13
- function isAddressEqual(a, b) {
13
+ function isHexEqual(a, b) {
14
14
  return a.toLowerCase() === b.toLowerCase();
15
15
  }
16
- exports.isAddressEqual = isAddressEqual;
16
+ exports.isHexEqual = isHexEqual;
17
17
  /**
18
18
  * Converts a Delegation to a DelegationStruct.
19
19
  * The DelegationStruct is the format used in the Delegation Framework.
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AACA,2CAAqD;AAErD,+CAA6D;AAG7D;;;;;;GAMG;AACH,SAAgB,cAAc,CAAC,CAAU,EAAE,CAAU;IACnD,OAAO,CAAC,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;AAC7C,CAAC;AAFD,wCAEC;AASD;;;;;;GAMG;AACI,MAAM,kBAAkB,GAAG,CAChC,UAAsB,EACJ,EAAE;IACpB,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAClD,QAAQ,EAAE,IAAA,0BAAkB,EAAC,MAAM,CAAC,QAAQ,CAAC;QAC7C,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,IAAI,EAAE,MAAM,CAAC,IAAI;KAClB,CAAC,CAAC,CAAC;IAEJ,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAErE,OAAO;QACL,QAAQ,EAAE,IAAA,0BAAkB,EAAC,UAAU,CAAC,QAAQ,CAAC;QACjD,SAAS,EAAE,IAAA,0BAAkB,EAAC,UAAU,CAAC,SAAS,CAAC;QACnD,SAAS,EAAE,UAAU,CAAC,SAAS;QAC/B,OAAO;QACP,IAAI;QACJ,SAAS,EAAE,UAAU,CAAC,SAAS;KAChC,CAAC;AACJ,CAAC,CAAC;AAnBW,QAAA,kBAAkB,sBAmB7B;AAEF;;;;GAIG;AACH,SAAgB,wBAAwB,CACtC,IAAqC;IAErC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC;IAE9D,MAAM,IAAI,GAAuB;QAC/B,IAAI,EAAE;YACJ,KAAK,EAAE,0CAA8B;YACrC,WAAW,EAAE,YAAY;YACzB,MAAM,EAAE;gBACN,OAAO;gBACP,IAAI,EAAE,mBAAmB;gBACzB,OAAO,EAAE,GAAG;gBACZ,iBAAiB;aAClB;YACD,OAAO,EAAE,IAAA,0BAAkB,EAAC,UAAU,CAAC;SACxC;QACD,IAAI;KACL,CAAC;IAEF,OAAO,IAAI,CAAC;AACd,CAAC;AArBD,4DAqBC","sourcesContent":["import type { TypedMessageParams } from '@metamask/keyring-controller';\nimport { getChecksumAddress } from '@metamask/utils';\n\nimport { SIGNABLE_DELEGATION_TYPED_DATA } from './constants';\nimport type { Address, Delegation, DelegationStruct } from './types';\n\n/**\n * Checks if two addresses are equal.\n *\n * @param a - The first address.\n * @param b - The second address.\n * @returns True if the addresses are equal, false otherwise.\n */\nexport function isAddressEqual(a: Address, b: Address) {\n return a.toLowerCase() === b.toLowerCase();\n}\n\ntype CreateTypedMessageParamsOptions = {\n chainId: number;\n from: Address;\n delegation: Delegation;\n verifyingContract: Address;\n};\n\n/**\n * Converts a Delegation to a DelegationStruct.\n * The DelegationStruct is the format used in the Delegation Framework.\n *\n * @param delegation the delegation to format\n * @returns the formatted delegation\n */\nexport const toDelegationStruct = (\n delegation: Delegation,\n): DelegationStruct => {\n const caveats = delegation.caveats.map((caveat) => ({\n enforcer: getChecksumAddress(caveat.enforcer),\n terms: caveat.terms,\n args: caveat.args,\n }));\n\n const salt = delegation.salt === '0x' ? 0n : BigInt(delegation.salt);\n\n return {\n delegate: getChecksumAddress(delegation.delegate),\n delegator: getChecksumAddress(delegation.delegator),\n authority: delegation.authority,\n caveats,\n salt,\n signature: delegation.signature,\n };\n};\n\n/**\n *\n * @param opts - The options for creating typed message params.\n * @returns The typed message params.\n */\nexport function createTypedMessageParams(\n opts: CreateTypedMessageParamsOptions,\n): TypedMessageParams {\n const { chainId, from, delegation, verifyingContract } = opts;\n\n const data: TypedMessageParams = {\n data: {\n types: SIGNABLE_DELEGATION_TYPED_DATA,\n primaryType: 'Delegation',\n domain: {\n chainId,\n name: 'DelegationManager',\n version: '1',\n verifyingContract,\n },\n message: toDelegationStruct(delegation),\n },\n from,\n };\n\n return data;\n}\n"]}
1
+ {"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AACA,2CAAqD;AAErD,+CAA6D;AAG7D;;;;;;GAMG;AACH,SAAgB,UAAU,CAAC,CAAM,EAAE,CAAM;IACvC,OAAO,CAAC,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;AAC7C,CAAC;AAFD,gCAEC;AASD;;;;;;GAMG;AACI,MAAM,kBAAkB,GAAG,CAChC,UAAsB,EACJ,EAAE;IACpB,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAClD,QAAQ,EAAE,IAAA,0BAAkB,EAAC,MAAM,CAAC,QAAQ,CAAC;QAC7C,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,IAAI,EAAE,MAAM,CAAC,IAAI;KAClB,CAAC,CAAC,CAAC;IAEJ,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAErE,OAAO;QACL,QAAQ,EAAE,IAAA,0BAAkB,EAAC,UAAU,CAAC,QAAQ,CAAC;QACjD,SAAS,EAAE,IAAA,0BAAkB,EAAC,UAAU,CAAC,SAAS,CAAC;QACnD,SAAS,EAAE,UAAU,CAAC,SAAS;QAC/B,OAAO;QACP,IAAI;QACJ,SAAS,EAAE,UAAU,CAAC,SAAS;KAChC,CAAC;AACJ,CAAC,CAAC;AAnBW,QAAA,kBAAkB,sBAmB7B;AAEF;;;;GAIG;AACH,SAAgB,wBAAwB,CACtC,IAAqC;IAErC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC;IAE9D,MAAM,IAAI,GAAuB;QAC/B,IAAI,EAAE;YACJ,KAAK,EAAE,0CAA8B;YACrC,WAAW,EAAE,YAAY;YACzB,MAAM,EAAE;gBACN,OAAO;gBACP,IAAI,EAAE,mBAAmB;gBACzB,OAAO,EAAE,GAAG;gBACZ,iBAAiB;aAClB;YACD,OAAO,EAAE,IAAA,0BAAkB,EAAC,UAAU,CAAC;SACxC;QACD,IAAI;KACL,CAAC;IAEF,OAAO,IAAI,CAAC;AACd,CAAC;AArBD,4DAqBC","sourcesContent":["import type { TypedMessageParams } from '@metamask/keyring-controller';\nimport { getChecksumAddress } from '@metamask/utils';\n\nimport { SIGNABLE_DELEGATION_TYPED_DATA } from './constants';\nimport type { Address, Delegation, DelegationStruct, Hex } from './types';\n\n/**\n * Checks if two hex strings are equal.\n *\n * @param a - The first hex string.\n * @param b - The second hex string.\n * @returns True if the hex strings are equal, false otherwise.\n */\nexport function isHexEqual(a: Hex, b: Hex) {\n return a.toLowerCase() === b.toLowerCase();\n}\n\ntype CreateTypedMessageParamsOptions = {\n chainId: number;\n from: Address;\n delegation: Delegation;\n verifyingContract: Address;\n};\n\n/**\n * Converts a Delegation to a DelegationStruct.\n * The DelegationStruct is the format used in the Delegation Framework.\n *\n * @param delegation the delegation to format\n * @returns the formatted delegation\n */\nexport const toDelegationStruct = (\n delegation: Delegation,\n): DelegationStruct => {\n const caveats = delegation.caveats.map((caveat) => ({\n enforcer: getChecksumAddress(caveat.enforcer),\n terms: caveat.terms,\n args: caveat.args,\n }));\n\n const salt = delegation.salt === '0x' ? 0n : BigInt(delegation.salt);\n\n return {\n delegate: getChecksumAddress(delegation.delegate),\n delegator: getChecksumAddress(delegation.delegator),\n authority: delegation.authority,\n caveats,\n salt,\n signature: delegation.signature,\n };\n};\n\n/**\n *\n * @param opts - The options for creating typed message params.\n * @returns The typed message params.\n */\nexport function createTypedMessageParams(\n opts: CreateTypedMessageParamsOptions,\n): TypedMessageParams {\n const { chainId, from, delegation, verifyingContract } = opts;\n\n const data: TypedMessageParams = {\n data: {\n types: SIGNABLE_DELEGATION_TYPED_DATA,\n primaryType: 'Delegation',\n domain: {\n chainId,\n name: 'DelegationManager',\n version: '1',\n verifyingContract,\n },\n message: toDelegationStruct(delegation),\n },\n from,\n };\n\n return data;\n}\n"]}
package/dist/utils.d.cts CHANGED
@@ -1,13 +1,13 @@
1
1
  import type { TypedMessageParams } from "@metamask/keyring-controller";
2
- import type { Address, Delegation, DelegationStruct } from "./types.cjs";
2
+ import type { Address, Delegation, DelegationStruct, Hex } from "./types.cjs";
3
3
  /**
4
- * Checks if two addresses are equal.
4
+ * Checks if two hex strings are equal.
5
5
  *
6
- * @param a - The first address.
7
- * @param b - The second address.
8
- * @returns True if the addresses are equal, false otherwise.
6
+ * @param a - The first hex string.
7
+ * @param b - The second hex string.
8
+ * @returns True if the hex strings are equal, false otherwise.
9
9
  */
10
- export declare function isAddressEqual(a: Address, b: Address): boolean;
10
+ export declare function isHexEqual(a: Hex, b: Hex): boolean;
11
11
  type CreateTypedMessageParamsOptions = {
12
12
  chainId: number;
13
13
  from: Address;
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,qCAAqC;AAIvE,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,oBAAgB;AAErE;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,WAEpD;AAED,KAAK,+BAA+B,GAAG;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,OAAO,CAAC;IACd,UAAU,EAAE,UAAU,CAAC;IACvB,iBAAiB,EAAE,OAAO,CAAC;CAC5B,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,eACjB,UAAU,KACrB,gBAiBF,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,+BAA+B,GACpC,kBAAkB,CAmBpB"}
1
+ {"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,qCAAqC;AAIvE,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,GAAG,EAAE,oBAAgB;AAE1E;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,WAExC;AAED,KAAK,+BAA+B,GAAG;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,OAAO,CAAC;IACd,UAAU,EAAE,UAAU,CAAC;IACvB,iBAAiB,EAAE,OAAO,CAAC;CAC5B,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,eACjB,UAAU,KACrB,gBAiBF,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,+BAA+B,GACpC,kBAAkB,CAmBpB"}
package/dist/utils.d.mts CHANGED
@@ -1,13 +1,13 @@
1
1
  import type { TypedMessageParams } from "@metamask/keyring-controller";
2
- import type { Address, Delegation, DelegationStruct } from "./types.mjs";
2
+ import type { Address, Delegation, DelegationStruct, Hex } from "./types.mjs";
3
3
  /**
4
- * Checks if two addresses are equal.
4
+ * Checks if two hex strings are equal.
5
5
  *
6
- * @param a - The first address.
7
- * @param b - The second address.
8
- * @returns True if the addresses are equal, false otherwise.
6
+ * @param a - The first hex string.
7
+ * @param b - The second hex string.
8
+ * @returns True if the hex strings are equal, false otherwise.
9
9
  */
10
- export declare function isAddressEqual(a: Address, b: Address): boolean;
10
+ export declare function isHexEqual(a: Hex, b: Hex): boolean;
11
11
  type CreateTypedMessageParamsOptions = {
12
12
  chainId: number;
13
13
  from: Address;
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,qCAAqC;AAIvE,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,oBAAgB;AAErE;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,WAEpD;AAED,KAAK,+BAA+B,GAAG;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,OAAO,CAAC;IACd,UAAU,EAAE,UAAU,CAAC;IACvB,iBAAiB,EAAE,OAAO,CAAC;CAC5B,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,eACjB,UAAU,KACrB,gBAiBF,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,+BAA+B,GACpC,kBAAkB,CAmBpB"}
1
+ {"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,qCAAqC;AAIvE,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,GAAG,EAAE,oBAAgB;AAE1E;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,WAExC;AAED,KAAK,+BAA+B,GAAG;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,OAAO,CAAC;IACd,UAAU,EAAE,UAAU,CAAC;IACvB,iBAAiB,EAAE,OAAO,CAAC;CAC5B,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,eACjB,UAAU,KACrB,gBAiBF,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,+BAA+B,GACpC,kBAAkB,CAmBpB"}
package/dist/utils.mjs CHANGED
@@ -1,13 +1,13 @@
1
1
  import { getChecksumAddress } from "@metamask/utils";
2
2
  import { SIGNABLE_DELEGATION_TYPED_DATA } from "./constants.mjs";
3
3
  /**
4
- * Checks if two addresses are equal.
4
+ * Checks if two hex strings are equal.
5
5
  *
6
- * @param a - The first address.
7
- * @param b - The second address.
8
- * @returns True if the addresses are equal, false otherwise.
6
+ * @param a - The first hex string.
7
+ * @param b - The second hex string.
8
+ * @returns True if the hex strings are equal, false otherwise.
9
9
  */
10
- export function isAddressEqual(a, b) {
10
+ export function isHexEqual(a, b) {
11
11
  return a.toLowerCase() === b.toLowerCase();
12
12
  }
13
13
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AAErD,OAAO,EAAE,8BAA8B,EAAE,wBAAoB;AAG7D;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,CAAU,EAAE,CAAU;IACnD,OAAO,CAAC,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;AAC7C,CAAC;AASD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,UAAsB,EACJ,EAAE;IACpB,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAClD,QAAQ,EAAE,kBAAkB,CAAC,MAAM,CAAC,QAAQ,CAAC;QAC7C,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,IAAI,EAAE,MAAM,CAAC,IAAI;KAClB,CAAC,CAAC,CAAC;IAEJ,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAErE,OAAO;QACL,QAAQ,EAAE,kBAAkB,CAAC,UAAU,CAAC,QAAQ,CAAC;QACjD,SAAS,EAAE,kBAAkB,CAAC,UAAU,CAAC,SAAS,CAAC;QACnD,SAAS,EAAE,UAAU,CAAC,SAAS;QAC/B,OAAO;QACP,IAAI;QACJ,SAAS,EAAE,UAAU,CAAC,SAAS;KAChC,CAAC;AACJ,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CACtC,IAAqC;IAErC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC;IAE9D,MAAM,IAAI,GAAuB;QAC/B,IAAI,EAAE;YACJ,KAAK,EAAE,8BAA8B;YACrC,WAAW,EAAE,YAAY;YACzB,MAAM,EAAE;gBACN,OAAO;gBACP,IAAI,EAAE,mBAAmB;gBACzB,OAAO,EAAE,GAAG;gBACZ,iBAAiB;aAClB;YACD,OAAO,EAAE,kBAAkB,CAAC,UAAU,CAAC;SACxC;QACD,IAAI;KACL,CAAC;IAEF,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import type { TypedMessageParams } from '@metamask/keyring-controller';\nimport { getChecksumAddress } from '@metamask/utils';\n\nimport { SIGNABLE_DELEGATION_TYPED_DATA } from './constants';\nimport type { Address, Delegation, DelegationStruct } from './types';\n\n/**\n * Checks if two addresses are equal.\n *\n * @param a - The first address.\n * @param b - The second address.\n * @returns True if the addresses are equal, false otherwise.\n */\nexport function isAddressEqual(a: Address, b: Address) {\n return a.toLowerCase() === b.toLowerCase();\n}\n\ntype CreateTypedMessageParamsOptions = {\n chainId: number;\n from: Address;\n delegation: Delegation;\n verifyingContract: Address;\n};\n\n/**\n * Converts a Delegation to a DelegationStruct.\n * The DelegationStruct is the format used in the Delegation Framework.\n *\n * @param delegation the delegation to format\n * @returns the formatted delegation\n */\nexport const toDelegationStruct = (\n delegation: Delegation,\n): DelegationStruct => {\n const caveats = delegation.caveats.map((caveat) => ({\n enforcer: getChecksumAddress(caveat.enforcer),\n terms: caveat.terms,\n args: caveat.args,\n }));\n\n const salt = delegation.salt === '0x' ? 0n : BigInt(delegation.salt);\n\n return {\n delegate: getChecksumAddress(delegation.delegate),\n delegator: getChecksumAddress(delegation.delegator),\n authority: delegation.authority,\n caveats,\n salt,\n signature: delegation.signature,\n };\n};\n\n/**\n *\n * @param opts - The options for creating typed message params.\n * @returns The typed message params.\n */\nexport function createTypedMessageParams(\n opts: CreateTypedMessageParamsOptions,\n): TypedMessageParams {\n const { chainId, from, delegation, verifyingContract } = opts;\n\n const data: TypedMessageParams = {\n data: {\n types: SIGNABLE_DELEGATION_TYPED_DATA,\n primaryType: 'Delegation',\n domain: {\n chainId,\n name: 'DelegationManager',\n version: '1',\n verifyingContract,\n },\n message: toDelegationStruct(delegation),\n },\n from,\n };\n\n return data;\n}\n"]}
1
+ {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AAErD,OAAO,EAAE,8BAA8B,EAAE,wBAAoB;AAG7D;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,CAAM,EAAE,CAAM;IACvC,OAAO,CAAC,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;AAC7C,CAAC;AASD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,UAAsB,EACJ,EAAE;IACpB,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAClD,QAAQ,EAAE,kBAAkB,CAAC,MAAM,CAAC,QAAQ,CAAC;QAC7C,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,IAAI,EAAE,MAAM,CAAC,IAAI;KAClB,CAAC,CAAC,CAAC;IAEJ,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAErE,OAAO;QACL,QAAQ,EAAE,kBAAkB,CAAC,UAAU,CAAC,QAAQ,CAAC;QACjD,SAAS,EAAE,kBAAkB,CAAC,UAAU,CAAC,SAAS,CAAC;QACnD,SAAS,EAAE,UAAU,CAAC,SAAS;QAC/B,OAAO;QACP,IAAI;QACJ,SAAS,EAAE,UAAU,CAAC,SAAS;KAChC,CAAC;AACJ,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CACtC,IAAqC;IAErC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC;IAE9D,MAAM,IAAI,GAAuB;QAC/B,IAAI,EAAE;YACJ,KAAK,EAAE,8BAA8B;YACrC,WAAW,EAAE,YAAY;YACzB,MAAM,EAAE;gBACN,OAAO;gBACP,IAAI,EAAE,mBAAmB;gBACzB,OAAO,EAAE,GAAG;gBACZ,iBAAiB;aAClB;YACD,OAAO,EAAE,kBAAkB,CAAC,UAAU,CAAC;SACxC;QACD,IAAI;KACL,CAAC;IAEF,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import type { TypedMessageParams } from '@metamask/keyring-controller';\nimport { getChecksumAddress } from '@metamask/utils';\n\nimport { SIGNABLE_DELEGATION_TYPED_DATA } from './constants';\nimport type { Address, Delegation, DelegationStruct, Hex } from './types';\n\n/**\n * Checks if two hex strings are equal.\n *\n * @param a - The first hex string.\n * @param b - The second hex string.\n * @returns True if the hex strings are equal, false otherwise.\n */\nexport function isHexEqual(a: Hex, b: Hex) {\n return a.toLowerCase() === b.toLowerCase();\n}\n\ntype CreateTypedMessageParamsOptions = {\n chainId: number;\n from: Address;\n delegation: Delegation;\n verifyingContract: Address;\n};\n\n/**\n * Converts a Delegation to a DelegationStruct.\n * The DelegationStruct is the format used in the Delegation Framework.\n *\n * @param delegation the delegation to format\n * @returns the formatted delegation\n */\nexport const toDelegationStruct = (\n delegation: Delegation,\n): DelegationStruct => {\n const caveats = delegation.caveats.map((caveat) => ({\n enforcer: getChecksumAddress(caveat.enforcer),\n terms: caveat.terms,\n args: caveat.args,\n }));\n\n const salt = delegation.salt === '0x' ? 0n : BigInt(delegation.salt);\n\n return {\n delegate: getChecksumAddress(delegation.delegate),\n delegator: getChecksumAddress(delegation.delegator),\n authority: delegation.authority,\n caveats,\n salt,\n signature: delegation.signature,\n };\n};\n\n/**\n *\n * @param opts - The options for creating typed message params.\n * @returns The typed message params.\n */\nexport function createTypedMessageParams(\n opts: CreateTypedMessageParamsOptions,\n): TypedMessageParams {\n const { chainId, from, delegation, verifyingContract } = opts;\n\n const data: TypedMessageParams = {\n data: {\n types: SIGNABLE_DELEGATION_TYPED_DATA,\n primaryType: 'Delegation',\n domain: {\n chainId,\n name: 'DelegationManager',\n version: '1',\n verifyingContract,\n },\n message: toDelegationStruct(delegation),\n },\n from,\n };\n\n return data;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/delegation-controller",
3
- "version": "0.1.0-preview-c67b57f2",
3
+ "version": "0.1.0-preview-6c44e568",
4
4
  "description": "Manages delegations for MetaMask",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -48,12 +48,12 @@
48
48
  },
49
49
  "dependencies": {
50
50
  "@metamask/base-controller": "^8.0.0",
51
- "@metamask/keyring-controller": "^21.0.2",
52
51
  "@metamask/utils": "^11.2.0"
53
52
  },
54
53
  "devDependencies": {
55
54
  "@metamask/accounts-controller": "^27.0.0",
56
55
  "@metamask/auto-changelog": "^3.4.4",
56
+ "@metamask/keyring-controller": "^21.0.3",
57
57
  "@ts-bridge/cli": "^0.6.1",
58
58
  "@types/jest": "^27.4.1",
59
59
  "deepmerge": "^4.2.2",
@@ -64,7 +64,8 @@
64
64
  "typescript": "~5.2.2"
65
65
  },
66
66
  "peerDependencies": {
67
- "@metamask/accounts-controller": "^27.0.0"
67
+ "@metamask/accounts-controller": "^27.0.0",
68
+ "@metamask/keyring-controller": "^21.0.2"
68
69
  },
69
70
  "engines": {
70
71
  "node": "^18.18 || >=20"