@metamask-previews/delegation-controller 0.1.0-preview-c67b57f2

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.
Files changed (44) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/LICENSE +20 -0
  3. package/README.md +15 -0
  4. package/dist/DelegationController.cjs +177 -0
  5. package/dist/DelegationController.cjs.map +1 -0
  6. package/dist/DelegationController.d.cts +73 -0
  7. package/dist/DelegationController.d.cts.map +1 -0
  8. package/dist/DelegationController.d.mts +73 -0
  9. package/dist/DelegationController.d.mts.map +1 -0
  10. package/dist/DelegationController.mjs +172 -0
  11. package/dist/DelegationController.mjs.map +1 -0
  12. package/dist/constants.cjs +28 -0
  13. package/dist/constants.cjs.map +1 -0
  14. package/dist/constants.d.cts +31 -0
  15. package/dist/constants.d.cts.map +1 -0
  16. package/dist/constants.d.mts +31 -0
  17. package/dist/constants.d.mts.map +1 -0
  18. package/dist/constants.mjs +25 -0
  19. package/dist/constants.mjs.map +1 -0
  20. package/dist/index.cjs +7 -0
  21. package/dist/index.cjs.map +1 -0
  22. package/dist/index.d.cts +3 -0
  23. package/dist/index.d.cts.map +1 -0
  24. package/dist/index.d.mts +3 -0
  25. package/dist/index.d.mts.map +1 -0
  26. package/dist/index.mjs +2 -0
  27. package/dist/index.mjs.map +1 -0
  28. package/dist/types.cjs +3 -0
  29. package/dist/types.cjs.map +1 -0
  30. package/dist/types.d.cts +55 -0
  31. package/dist/types.d.cts.map +1 -0
  32. package/dist/types.d.mts +55 -0
  33. package/dist/types.d.mts.map +1 -0
  34. package/dist/types.mjs +2 -0
  35. package/dist/types.mjs.map +1 -0
  36. package/dist/utils.cjs +65 -0
  37. package/dist/utils.cjs.map +1 -0
  38. package/dist/utils.d.cts +32 -0
  39. package/dist/utils.d.cts.map +1 -0
  40. package/dist/utils.d.mts +32 -0
  41. package/dist/utils.d.mts.map +1 -0
  42. package/dist/utils.mjs +59 -0
  43. package/dist/utils.mjs.map +1 -0
  44. package/package.json +76 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ [Unreleased]: https://github.com/MetaMask/core/
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 MetaMask
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # `@metamask/delegation-controller`
2
+
3
+ Centralized place to store and sign delegations.
4
+
5
+ ## Installation
6
+
7
+ `yarn add @metamask/delegation-controller`
8
+
9
+ or
10
+
11
+ `npm install @metamask/delegation-controller`
12
+
13
+ ## Contributing
14
+
15
+ This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
@@ -0,0 +1,177 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DelegationController = exports.getDefaultDelegationControllerState = exports.controllerName = void 0;
4
+ const base_controller_1 = require("@metamask/base-controller");
5
+ const keyring_controller_1 = require("@metamask/keyring-controller");
6
+ const constants_1 = require("./constants.cjs");
7
+ const utils_1 = require("./utils.cjs");
8
+ exports.controllerName = 'DelegationController';
9
+ const delegationControllerMetadata = {
10
+ delegations: {
11
+ persist: true,
12
+ anonymous: false,
13
+ },
14
+ };
15
+ /**
16
+ * Constructs the default {@link DelegationController} state. This allows
17
+ * consumers to provide a partial state object when initializing the controller
18
+ * and also helps in constructing complete state objects for this controller in
19
+ * tests.
20
+ *
21
+ * @returns The default {@link DelegationController} state.
22
+ */
23
+ function getDefaultDelegationControllerState() {
24
+ return {
25
+ delegations: {},
26
+ };
27
+ }
28
+ exports.getDefaultDelegationControllerState = getDefaultDelegationControllerState;
29
+ /**
30
+ * The {@link DelegationController} class.
31
+ * This controller is meant to be a centralized place to store and sign delegations.
32
+ */
33
+ class DelegationController extends base_controller_1.BaseController {
34
+ constructor({ messenger, state, }) {
35
+ super({
36
+ messenger,
37
+ metadata: delegationControllerMetadata,
38
+ name: exports.controllerName,
39
+ state: {
40
+ ...getDefaultDelegationControllerState(),
41
+ ...state,
42
+ },
43
+ });
44
+ }
45
+ /**
46
+ * Signs a delegation.
47
+ *
48
+ * @param params - The parameters for signing the delegation.
49
+ * @param params.delegation - The delegation to sign.
50
+ * @param params.verifyingContract - The address of the verifying contract (DelegationManager).
51
+ * @param params.chainId - The chainId of the chain to sign the delegation for.
52
+ * @returns The signature of the delegation.
53
+ */
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,
62
+ });
63
+ // TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.
64
+ // Waiting on confirmations team to implement this.
65
+ const signature = await this.messagingSystem.call('KeyringController:signTypedMessage', data, keyring_controller_1.SignTypedDataVersion.V4);
66
+ return signature;
67
+ }
68
+ /**
69
+ * Stores a delegation in storage.
70
+ *
71
+ * @param hash - The hash of the delegation to store.
72
+ * @param entry - The delegation entry to store.
73
+ */
74
+ store(hash, entry) {
75
+ // If the authority is not the root authority, validate that the
76
+ // parent entry does exist.
77
+ if (!(0, utils_1.isAddressEqual)(entry.data.authority, constants_1.ROOT_AUTHORITY) &&
78
+ !this.state.delegations[entry.data.authority]) {
79
+ throw new Error('Invalid authority');
80
+ }
81
+ this.update((state) => {
82
+ state.delegations[hash] = entry;
83
+ });
84
+ }
85
+ /**
86
+ * Lists delegation entries.
87
+ *
88
+ * @param filter - The filter to use to list the delegation entries.
89
+ * @returns A list of delegation entries that match the filter.
90
+ */
91
+ list(filter) {
92
+ const account = this.messagingSystem.call('AccountsController:getSelectedAccount');
93
+ const requester = account.address;
94
+ let list = Object.values(this.state.delegations);
95
+ if (filter?.from) {
96
+ list = list.filter((entry) => (0, utils_1.isAddressEqual)(entry.data.delegator, filter.from));
97
+ }
98
+ 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));
101
+ }
102
+ if (filter?.chainId) {
103
+ list = list.filter((entry) => entry.chainId === filter.chainId);
104
+ }
105
+ const tags = filter?.tags;
106
+ if (tags && tags.length > 0) {
107
+ // Filter entries that contain all of the filter tags
108
+ list = list.filter((entry) => tags.every((tag) => entry.tags.includes(tag)));
109
+ }
110
+ return list;
111
+ }
112
+ /**
113
+ * Retrieves the delegation entry for a given delegation hash.
114
+ *
115
+ * @param hash - The hash of the delegation to retrieve.
116
+ * @returns The delegation entry, or null if not found.
117
+ */
118
+ retrieve(hash) {
119
+ return this.state.delegations[hash] ?? null;
120
+ }
121
+ /**
122
+ * Retrieves a delegation chain from a delegation hash.
123
+ *
124
+ * @param hash - The hash of the delegation to retrieve.
125
+ * @returns The delegation chain, or null if not found.
126
+ */
127
+ chain(hash) {
128
+ const chain = [];
129
+ const entry = this.retrieve(hash);
130
+ if (!entry) {
131
+ return null;
132
+ }
133
+ chain.push(entry);
134
+ for (let _hash = entry.data.authority; _hash !== constants_1.ROOT_AUTHORITY;) {
135
+ const parent = this.retrieve(_hash);
136
+ if (!parent) {
137
+ throw new Error('Invalid delegation chain');
138
+ }
139
+ chain.push(parent);
140
+ _hash = parent.data.authority;
141
+ }
142
+ return chain;
143
+ }
144
+ /**
145
+ * Deletes a delegation entrie from storage, along with any other entries
146
+ * that are redelegated from it.
147
+ *
148
+ * @param hash - The hash of the delegation to delete.
149
+ * @returns The number of entries deleted.
150
+ */
151
+ delete(hash) {
152
+ const root = this.retrieve(hash);
153
+ if (!root) {
154
+ return 0;
155
+ }
156
+ const entries = Object.entries(this.state.delegations);
157
+ let count = 0;
158
+ const nextHashes = [hash];
159
+ while (nextHashes.length > 0) {
160
+ const currentHash = nextHashes.pop();
161
+ // Find all delegations that have this hash as their authority
162
+ const children = entries.filter(([_, v]) => v.data.authority === currentHash);
163
+ // Add the hashes of all child delegations to be processed next
164
+ children.forEach(([k]) => {
165
+ nextHashes.push(k);
166
+ });
167
+ // Delete the current delegation
168
+ this.update((state) => {
169
+ delete state.delegations[currentHash];
170
+ });
171
+ count += 1;
172
+ }
173
+ return count;
174
+ }
175
+ }
176
+ exports.DelegationController = DelegationController;
177
+ //# sourceMappingURL=DelegationController.cjs.map
@@ -0,0 +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,WA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,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;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"]}
@@ -0,0 +1,73 @@
1
+ import { BaseController } from "@metamask/base-controller";
2
+ import type { Address, Delegation, DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DelegationFilter, Hex } from "./types.cjs";
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
+ /**
14
+ * The {@link DelegationController} class.
15
+ * This controller is meant to be a centralized place to store and sign delegations.
16
+ */
17
+ export declare class DelegationController extends BaseController<typeof controllerName, DelegationControllerState, DelegationControllerMessenger> {
18
+ constructor({ messenger, state, }: {
19
+ messenger: DelegationControllerMessenger;
20
+ state?: Partial<DelegationControllerState>;
21
+ });
22
+ /**
23
+ * Signs a delegation.
24
+ *
25
+ * @param params - The parameters for signing the delegation.
26
+ * @param params.delegation - The delegation to sign.
27
+ * @param params.verifyingContract - The address of the verifying contract (DelegationManager).
28
+ * @param params.chainId - The chainId of the chain to sign the delegation for.
29
+ * @returns The signature of the delegation.
30
+ */
31
+ sign(params: {
32
+ delegation: Delegation;
33
+ verifyingContract: Address;
34
+ chainId: number;
35
+ }): Promise<string>;
36
+ /**
37
+ * Stores a delegation in storage.
38
+ *
39
+ * @param hash - The hash of the delegation to store.
40
+ * @param entry - The delegation entry to store.
41
+ */
42
+ store(hash: Hex, entry: DelegationEntry): void;
43
+ /**
44
+ * Lists delegation entries.
45
+ *
46
+ * @param filter - The filter to use to list the delegation entries.
47
+ * @returns A list of delegation entries that match the filter.
48
+ */
49
+ list(filter?: DelegationFilter): DelegationEntry[];
50
+ /**
51
+ * Retrieves the delegation entry for a given delegation hash.
52
+ *
53
+ * @param hash - The hash of the delegation to retrieve.
54
+ * @returns The delegation entry, or null if not found.
55
+ */
56
+ retrieve(hash: Hex): DelegationEntry;
57
+ /**
58
+ * Retrieves a delegation chain from a delegation hash.
59
+ *
60
+ * @param hash - The hash of the delegation to retrieve.
61
+ * @returns The delegation chain, or null if not found.
62
+ */
63
+ chain(hash: Hex): DelegationEntry[] | null;
64
+ /**
65
+ * Deletes a delegation entrie from storage, along with any other entries
66
+ * that are redelegated from it.
67
+ *
68
+ * @param hash - The hash of the delegation to delete.
69
+ * @returns The number of entries deleted.
70
+ */
71
+ delete(hash: Hex): number;
72
+ }
73
+ //# sourceMappingURL=DelegationController.d.cts.map
@@ -0,0 +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"}
@@ -0,0 +1,73 @@
1
+ import { BaseController } from "@metamask/base-controller";
2
+ import type { Address, Delegation, DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DelegationFilter, Hex } from "./types.mjs";
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
+ /**
14
+ * The {@link DelegationController} class.
15
+ * This controller is meant to be a centralized place to store and sign delegations.
16
+ */
17
+ export declare class DelegationController extends BaseController<typeof controllerName, DelegationControllerState, DelegationControllerMessenger> {
18
+ constructor({ messenger, state, }: {
19
+ messenger: DelegationControllerMessenger;
20
+ state?: Partial<DelegationControllerState>;
21
+ });
22
+ /**
23
+ * Signs a delegation.
24
+ *
25
+ * @param params - The parameters for signing the delegation.
26
+ * @param params.delegation - The delegation to sign.
27
+ * @param params.verifyingContract - The address of the verifying contract (DelegationManager).
28
+ * @param params.chainId - The chainId of the chain to sign the delegation for.
29
+ * @returns The signature of the delegation.
30
+ */
31
+ sign(params: {
32
+ delegation: Delegation;
33
+ verifyingContract: Address;
34
+ chainId: number;
35
+ }): Promise<string>;
36
+ /**
37
+ * Stores a delegation in storage.
38
+ *
39
+ * @param hash - The hash of the delegation to store.
40
+ * @param entry - The delegation entry to store.
41
+ */
42
+ store(hash: Hex, entry: DelegationEntry): void;
43
+ /**
44
+ * Lists delegation entries.
45
+ *
46
+ * @param filter - The filter to use to list the delegation entries.
47
+ * @returns A list of delegation entries that match the filter.
48
+ */
49
+ list(filter?: DelegationFilter): DelegationEntry[];
50
+ /**
51
+ * Retrieves the delegation entry for a given delegation hash.
52
+ *
53
+ * @param hash - The hash of the delegation to retrieve.
54
+ * @returns The delegation entry, or null if not found.
55
+ */
56
+ retrieve(hash: Hex): DelegationEntry;
57
+ /**
58
+ * Retrieves a delegation chain from a delegation hash.
59
+ *
60
+ * @param hash - The hash of the delegation to retrieve.
61
+ * @returns The delegation chain, or null if not found.
62
+ */
63
+ chain(hash: Hex): DelegationEntry[] | null;
64
+ /**
65
+ * Deletes a delegation entrie from storage, along with any other entries
66
+ * that are redelegated from it.
67
+ *
68
+ * @param hash - The hash of the delegation to delete.
69
+ * @returns The number of entries deleted.
70
+ */
71
+ delete(hash: Hex): number;
72
+ }
73
+ //# sourceMappingURL=DelegationController.d.mts.map
@@ -0,0 +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"}
@@ -0,0 +1,172 @@
1
+ import { BaseController } from "@metamask/base-controller";
2
+ import { SignTypedDataVersion } from "@metamask/keyring-controller";
3
+ import { ROOT_AUTHORITY } from "./constants.mjs";
4
+ import { createTypedMessageParams, isAddressEqual } from "./utils.mjs";
5
+ export const controllerName = 'DelegationController';
6
+ const delegationControllerMetadata = {
7
+ delegations: {
8
+ persist: true,
9
+ anonymous: false,
10
+ },
11
+ };
12
+ /**
13
+ * Constructs the default {@link DelegationController} state. This allows
14
+ * consumers to provide a partial state object when initializing the controller
15
+ * and also helps in constructing complete state objects for this controller in
16
+ * tests.
17
+ *
18
+ * @returns The default {@link DelegationController} state.
19
+ */
20
+ export function getDefaultDelegationControllerState() {
21
+ return {
22
+ delegations: {},
23
+ };
24
+ }
25
+ /**
26
+ * The {@link DelegationController} class.
27
+ * This controller is meant to be a centralized place to store and sign delegations.
28
+ */
29
+ export class DelegationController extends BaseController {
30
+ constructor({ messenger, state, }) {
31
+ super({
32
+ messenger,
33
+ metadata: delegationControllerMetadata,
34
+ name: controllerName,
35
+ state: {
36
+ ...getDefaultDelegationControllerState(),
37
+ ...state,
38
+ },
39
+ });
40
+ }
41
+ /**
42
+ * Signs a delegation.
43
+ *
44
+ * @param params - The parameters for signing the delegation.
45
+ * @param params.delegation - The delegation to sign.
46
+ * @param params.verifyingContract - The address of the verifying contract (DelegationManager).
47
+ * @param params.chainId - The chainId of the chain to sign the delegation for.
48
+ * @returns The signature of the delegation.
49
+ */
50
+ async sign(params) {
51
+ const { delegation, verifyingContract, chainId } = params;
52
+ const account = this.messagingSystem.call('AccountsController:getSelectedAccount');
53
+ const data = createTypedMessageParams({
54
+ chainId,
55
+ from: account.address,
56
+ delegation,
57
+ verifyingContract,
58
+ });
59
+ // TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.
60
+ // Waiting on confirmations team to implement this.
61
+ const signature = await this.messagingSystem.call('KeyringController:signTypedMessage', data, SignTypedDataVersion.V4);
62
+ return signature;
63
+ }
64
+ /**
65
+ * Stores a delegation in storage.
66
+ *
67
+ * @param hash - The hash of the delegation to store.
68
+ * @param entry - The delegation entry to store.
69
+ */
70
+ store(hash, entry) {
71
+ // If the authority is not the root authority, validate that the
72
+ // parent entry does exist.
73
+ if (!isAddressEqual(entry.data.authority, ROOT_AUTHORITY) &&
74
+ !this.state.delegations[entry.data.authority]) {
75
+ throw new Error('Invalid authority');
76
+ }
77
+ this.update((state) => {
78
+ state.delegations[hash] = entry;
79
+ });
80
+ }
81
+ /**
82
+ * Lists delegation entries.
83
+ *
84
+ * @param filter - The filter to use to list the delegation entries.
85
+ * @returns A list of delegation entries that match the filter.
86
+ */
87
+ list(filter) {
88
+ const account = this.messagingSystem.call('AccountsController:getSelectedAccount');
89
+ const requester = account.address;
90
+ let list = Object.values(this.state.delegations);
91
+ if (filter?.from) {
92
+ list = list.filter((entry) => isAddressEqual(entry.data.delegator, filter.from));
93
+ }
94
+ if (!filter?.from ||
95
+ (filter?.from && !isAddressEqual(filter.from, requester))) {
96
+ list = list.filter((entry) => isAddressEqual(entry.data.delegate, requester));
97
+ }
98
+ if (filter?.chainId) {
99
+ list = list.filter((entry) => entry.chainId === filter.chainId);
100
+ }
101
+ const tags = filter?.tags;
102
+ if (tags && tags.length > 0) {
103
+ // Filter entries that contain all of the filter tags
104
+ list = list.filter((entry) => tags.every((tag) => entry.tags.includes(tag)));
105
+ }
106
+ return list;
107
+ }
108
+ /**
109
+ * Retrieves the delegation entry for a given delegation hash.
110
+ *
111
+ * @param hash - The hash of the delegation to retrieve.
112
+ * @returns The delegation entry, or null if not found.
113
+ */
114
+ retrieve(hash) {
115
+ return this.state.delegations[hash] ?? null;
116
+ }
117
+ /**
118
+ * Retrieves a delegation chain from a delegation hash.
119
+ *
120
+ * @param hash - The hash of the delegation to retrieve.
121
+ * @returns The delegation chain, or null if not found.
122
+ */
123
+ chain(hash) {
124
+ const chain = [];
125
+ const entry = this.retrieve(hash);
126
+ if (!entry) {
127
+ return null;
128
+ }
129
+ chain.push(entry);
130
+ for (let _hash = entry.data.authority; _hash !== ROOT_AUTHORITY;) {
131
+ const parent = this.retrieve(_hash);
132
+ if (!parent) {
133
+ throw new Error('Invalid delegation chain');
134
+ }
135
+ chain.push(parent);
136
+ _hash = parent.data.authority;
137
+ }
138
+ return chain;
139
+ }
140
+ /**
141
+ * Deletes a delegation entrie from storage, along with any other entries
142
+ * that are redelegated from it.
143
+ *
144
+ * @param hash - The hash of the delegation to delete.
145
+ * @returns The number of entries deleted.
146
+ */
147
+ delete(hash) {
148
+ const root = this.retrieve(hash);
149
+ if (!root) {
150
+ return 0;
151
+ }
152
+ const entries = Object.entries(this.state.delegations);
153
+ let count = 0;
154
+ const nextHashes = [hash];
155
+ while (nextHashes.length > 0) {
156
+ const currentHash = nextHashes.pop();
157
+ // Find all delegations that have this hash as their authority
158
+ const children = entries.filter(([_, v]) => v.data.authority === currentHash);
159
+ // Add the hashes of all child delegations to be processed next
160
+ children.forEach(([k]) => {
161
+ nextHashes.push(k);
162
+ });
163
+ // Delete the current delegation
164
+ this.update((state) => {
165
+ delete state.delegations[currentHash];
166
+ });
167
+ count += 1;
168
+ }
169
+ return count;
170
+ }
171
+ }
172
+ //# sourceMappingURL=DelegationController.mjs.map
@@ -0,0 +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,wBAAoB;AAU7C,OAAO,EAAE,wBAAwB,EAAE,cAAc,EAAE,oBAAgB;AAEnE,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,MAAM,UAAU,mCAAmC;IACjD,OAAO;QACL,WAAW,EAAE,EAAE;KAChB,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,oBAAqB,SAAQ,cAIzC;IACC,YAAY,EACV,SAAS,EACT,KAAK,GAIN;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;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,CACxC,CAAC;QAEF,MAAM,IAAI,GAAG,wBAAwB,CAAC;YACpC,OAAO;YACP,IAAI,EAAE,OAAO,CAAC,OAAkB;YAChC,UAAU;YACV,iBAAiB;SAClB,CAAC,CAAC;QAEH,qEAAqE;QACrE,mDAAmD;QACnD,MA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@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"]}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SIGNABLE_DELEGATION_TYPED_DATA = exports.ROOT_AUTHORITY = void 0;
4
+ exports.ROOT_AUTHORITY = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
5
+ const EIP712Domain = [
6
+ { name: 'name', type: 'string' },
7
+ { name: 'version', type: 'string' },
8
+ { name: 'chainId', type: 'uint256' },
9
+ { name: 'verifyingContract', type: 'address' },
10
+ ];
11
+ const SDK_SIGNABLE_DELEGATION_TYPED_DATA = {
12
+ Caveat: [
13
+ { name: 'enforcer', type: 'address' },
14
+ { name: 'terms', type: 'bytes' },
15
+ ],
16
+ Delegation: [
17
+ { name: 'delegate', type: 'address' },
18
+ { name: 'delegator', type: 'address' },
19
+ { name: 'authority', type: 'bytes32' },
20
+ { name: 'caveats', type: 'Caveat[]' },
21
+ { name: 'salt', type: 'uint256' },
22
+ ],
23
+ };
24
+ exports.SIGNABLE_DELEGATION_TYPED_DATA = {
25
+ EIP712Domain,
26
+ ...SDK_SIGNABLE_DELEGATION_TYPED_DATA,
27
+ };
28
+ //# sourceMappingURL=constants.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.cjs","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAEa,QAAA,cAAc,GACzB,oEAA2E,CAAC;AAE9E,MAAM,YAAY,GAAG;IACnB,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE;IAChC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE;IACnC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;IACpC,EAAE,IAAI,EAAE,mBAAmB,EAAE,IAAI,EAAE,SAAS,EAAE;CAC/C,CAAC;AAEF,MAAM,kCAAkC,GAAG;IACzC,MAAM,EAAE;QACN,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE;QACrC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE;KACjC;IACD,UAAU,EAAE;QACV,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE;QACrC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE;QACtC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE;QACtC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE;QACrC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE;KAClC;CACO,CAAC;AAEE,QAAA,8BAA8B,GAAG;IAC5C,YAAY;IACZ,GAAG,kCAAkC;CACtC,CAAC","sourcesContent":["import type { Hex } from './types';\n\nexport const ROOT_AUTHORITY =\n '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' as Hex;\n\nconst EIP712Domain = [\n { name: 'name', type: 'string' },\n { name: 'version', type: 'string' },\n { name: 'chainId', type: 'uint256' },\n { name: 'verifyingContract', type: 'address' },\n];\n\nconst SDK_SIGNABLE_DELEGATION_TYPED_DATA = {\n Caveat: [\n { name: 'enforcer', type: 'address' },\n { name: 'terms', type: 'bytes' },\n ],\n Delegation: [\n { name: 'delegate', type: 'address' },\n { name: 'delegator', type: 'address' },\n { name: 'authority', type: 'bytes32' },\n { name: 'caveats', type: 'Caveat[]' },\n { name: 'salt', type: 'uint256' },\n ],\n} as const;\n\nexport const SIGNABLE_DELEGATION_TYPED_DATA = {\n EIP712Domain,\n ...SDK_SIGNABLE_DELEGATION_TYPED_DATA,\n};\n"]}
@@ -0,0 +1,31 @@
1
+ export declare const ROOT_AUTHORITY: `0x${string}`;
2
+ export declare const SIGNABLE_DELEGATION_TYPED_DATA: {
3
+ Caveat: readonly [{
4
+ readonly name: "enforcer";
5
+ readonly type: "address";
6
+ }, {
7
+ readonly name: "terms";
8
+ readonly type: "bytes";
9
+ }];
10
+ Delegation: readonly [{
11
+ readonly name: "delegate";
12
+ readonly type: "address";
13
+ }, {
14
+ readonly name: "delegator";
15
+ readonly type: "address";
16
+ }, {
17
+ readonly name: "authority";
18
+ readonly type: "bytes32";
19
+ }, {
20
+ readonly name: "caveats";
21
+ readonly type: "Caveat[]";
22
+ }, {
23
+ readonly name: "salt";
24
+ readonly type: "uint256";
25
+ }];
26
+ EIP712Domain: {
27
+ name: string;
28
+ type: string;
29
+ }[];
30
+ };
31
+ //# sourceMappingURL=constants.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.cts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,cAAc,eACkD,CAAC;AAuB9E,eAAO,MAAM,8BAA8B;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAG1C,CAAC"}
@@ -0,0 +1,31 @@
1
+ export declare const ROOT_AUTHORITY: `0x${string}`;
2
+ export declare const SIGNABLE_DELEGATION_TYPED_DATA: {
3
+ Caveat: readonly [{
4
+ readonly name: "enforcer";
5
+ readonly type: "address";
6
+ }, {
7
+ readonly name: "terms";
8
+ readonly type: "bytes";
9
+ }];
10
+ Delegation: readonly [{
11
+ readonly name: "delegate";
12
+ readonly type: "address";
13
+ }, {
14
+ readonly name: "delegator";
15
+ readonly type: "address";
16
+ }, {
17
+ readonly name: "authority";
18
+ readonly type: "bytes32";
19
+ }, {
20
+ readonly name: "caveats";
21
+ readonly type: "Caveat[]";
22
+ }, {
23
+ readonly name: "salt";
24
+ readonly type: "uint256";
25
+ }];
26
+ EIP712Domain: {
27
+ name: string;
28
+ type: string;
29
+ }[];
30
+ };
31
+ //# sourceMappingURL=constants.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.mts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,cAAc,eACkD,CAAC;AAuB9E,eAAO,MAAM,8BAA8B;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAG1C,CAAC"}
@@ -0,0 +1,25 @@
1
+ export const ROOT_AUTHORITY = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
2
+ const EIP712Domain = [
3
+ { name: 'name', type: 'string' },
4
+ { name: 'version', type: 'string' },
5
+ { name: 'chainId', type: 'uint256' },
6
+ { name: 'verifyingContract', type: 'address' },
7
+ ];
8
+ const SDK_SIGNABLE_DELEGATION_TYPED_DATA = {
9
+ Caveat: [
10
+ { name: 'enforcer', type: 'address' },
11
+ { name: 'terms', type: 'bytes' },
12
+ ],
13
+ Delegation: [
14
+ { name: 'delegate', type: 'address' },
15
+ { name: 'delegator', type: 'address' },
16
+ { name: 'authority', type: 'bytes32' },
17
+ { name: 'caveats', type: 'Caveat[]' },
18
+ { name: 'salt', type: 'uint256' },
19
+ ],
20
+ };
21
+ export const SIGNABLE_DELEGATION_TYPED_DATA = {
22
+ EIP712Domain,
23
+ ...SDK_SIGNABLE_DELEGATION_TYPED_DATA,
24
+ };
25
+ //# sourceMappingURL=constants.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.mjs","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,cAAc,GACzB,oEAA2E,CAAC;AAE9E,MAAM,YAAY,GAAG;IACnB,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE;IAChC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE;IACnC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;IACpC,EAAE,IAAI,EAAE,mBAAmB,EAAE,IAAI,EAAE,SAAS,EAAE;CAC/C,CAAC;AAEF,MAAM,kCAAkC,GAAG;IACzC,MAAM,EAAE;QACN,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE;QACrC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE;KACjC;IACD,UAAU,EAAE;QACV,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE;QACrC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE;QACtC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE;QACtC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE;QACrC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE;KAClC;CACO,CAAC;AAEX,MAAM,CAAC,MAAM,8BAA8B,GAAG;IAC5C,YAAY;IACZ,GAAG,kCAAkC;CACtC,CAAC","sourcesContent":["import type { Hex } from './types';\n\nexport const ROOT_AUTHORITY =\n '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' as Hex;\n\nconst EIP712Domain = [\n { name: 'name', type: 'string' },\n { name: 'version', type: 'string' },\n { name: 'chainId', type: 'uint256' },\n { name: 'verifyingContract', type: 'address' },\n];\n\nconst SDK_SIGNABLE_DELEGATION_TYPED_DATA = {\n Caveat: [\n { name: 'enforcer', type: 'address' },\n { name: 'terms', type: 'bytes' },\n ],\n Delegation: [\n { name: 'delegate', type: 'address' },\n { name: 'delegator', type: 'address' },\n { name: 'authority', type: 'bytes32' },\n { name: 'caveats', type: 'Caveat[]' },\n { name: 'salt', type: 'uint256' },\n ],\n} as const;\n\nexport const SIGNABLE_DELEGATION_TYPED_DATA = {\n EIP712Domain,\n ...SDK_SIGNABLE_DELEGATION_TYPED_DATA,\n};\n"]}
package/dist/index.cjs ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDefaultDelegationControllerState = exports.DelegationController = void 0;
4
+ var DelegationController_1 = require("./DelegationController.cjs");
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
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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"]}
@@ -0,0 +1,3 @@
1
+ export type { DelegationControllerActions, DelegationControllerEvents, DelegationControllerMessenger, DelegationEntry, DelegationFilter, } from "./types.cjs";
2
+ export { DelegationController, getDefaultDelegationControllerState, } from "./DelegationController.cjs";
3
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +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"}
@@ -0,0 +1,3 @@
1
+ export type { DelegationControllerActions, DelegationControllerEvents, DelegationControllerMessenger, DelegationEntry, DelegationFilter, } from "./types.mjs";
2
+ export { DelegationController, getDefaultDelegationControllerState, } from "./DelegationController.mjs";
3
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +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"}
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export { DelegationController, getDefaultDelegationControllerState } from "./DelegationController.mjs";
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +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"]}
package/dist/types.cjs ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.cjs.map
@@ -0,0 +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"]}
@@ -0,0 +1,55 @@
1
+ import type { AccountsControllerGetSelectedAccountAction } from "@metamask/accounts-controller";
2
+ import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
3
+ import type { KeyringControllerSignTypedMessageAction } from "@metamask/keyring-controller";
4
+ import type { controllerName, DelegationController } from "./DelegationController.cjs";
5
+ type Hex = `0x${string}`;
6
+ type Address = `0x${string}`;
7
+ export type { Address, Hex };
8
+ export type Caveat = {
9
+ enforcer: Hex;
10
+ terms: Hex;
11
+ args: Hex;
12
+ };
13
+ export type Delegation = {
14
+ delegate: Hex;
15
+ delegator: Hex;
16
+ authority: Hex;
17
+ caveats: Caveat[];
18
+ salt: Hex;
19
+ signature: Hex;
20
+ };
21
+ export type DelegationStruct = Omit<Delegation, 'salt'> & {
22
+ salt: bigint;
23
+ };
24
+ export type DelegationEntry = {
25
+ tags: string[];
26
+ chainId: number;
27
+ data: Delegation;
28
+ meta?: string;
29
+ };
30
+ export type DelegationFilter = {
31
+ chainId?: number;
32
+ tags?: string[];
33
+ from?: Address;
34
+ };
35
+ export type DelegationControllerState = {
36
+ delegations: {
37
+ [hash: Hex]: DelegationEntry;
38
+ };
39
+ };
40
+ export type DelegationControllerGetStateAction = ControllerGetStateAction<typeof controllerName, DelegationControllerState>;
41
+ export type DelegationControllerStoreAction = {
42
+ type: `${typeof controllerName}:store`;
43
+ handler: DelegationController['store'];
44
+ };
45
+ export type DelegationControllerSignAction = {
46
+ type: `${typeof controllerName}:sign`;
47
+ handler: DelegationController['sign'];
48
+ };
49
+ export type DelegationControllerActions = DelegationControllerGetStateAction | DelegationControllerStoreAction | DelegationControllerSignAction;
50
+ export type DelegationControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, DelegationControllerState>;
51
+ export type DelegationControllerEvents = DelegationControllerStateChangeEvent;
52
+ type AllowedActions = KeyringControllerSignTypedMessageAction | AccountsControllerGetSelectedAccountAction;
53
+ type AllowedEvents = never;
54
+ export type DelegationControllerMessenger = RestrictedMessenger<typeof controllerName, DelegationControllerActions | AllowedActions, DelegationControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
55
+ //# sourceMappingURL=types.d.cts.map
@@ -0,0 +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,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,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACtC,WAAW,EAAE;QACX,CAAC,IAAI,EAAE,GAAG,GAAG,eAAe,CAAC;KAC9B,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,kCAAkC,GAAG,wBAAwB,CACvE,OAAO,cAAc,EACrB,yBAAyB,CAC1B,CAAC;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,8BAA8B,GAAG;IAC3C,IAAI,EAAE,GAAG,OAAO,cAAc,OAAO,CAAC;IACtC,OAAO,EAAE,oBAAoB,CAAC,MAAM,CAAC,CAAC;CACvC,CAAC;AAEF,MAAM,MAAM,2BAA2B,GACnC,kCAAkC,GAClC,+BAA+B,GAC/B,8BAA8B,CAAC;AAEnC,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"}
@@ -0,0 +1,55 @@
1
+ import type { AccountsControllerGetSelectedAccountAction } from "@metamask/accounts-controller";
2
+ import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
3
+ import type { KeyringControllerSignTypedMessageAction } from "@metamask/keyring-controller";
4
+ import type { controllerName, DelegationController } from "./DelegationController.mjs";
5
+ type Hex = `0x${string}`;
6
+ type Address = `0x${string}`;
7
+ export type { Address, Hex };
8
+ export type Caveat = {
9
+ enforcer: Hex;
10
+ terms: Hex;
11
+ args: Hex;
12
+ };
13
+ export type Delegation = {
14
+ delegate: Hex;
15
+ delegator: Hex;
16
+ authority: Hex;
17
+ caveats: Caveat[];
18
+ salt: Hex;
19
+ signature: Hex;
20
+ };
21
+ export type DelegationStruct = Omit<Delegation, 'salt'> & {
22
+ salt: bigint;
23
+ };
24
+ export type DelegationEntry = {
25
+ tags: string[];
26
+ chainId: number;
27
+ data: Delegation;
28
+ meta?: string;
29
+ };
30
+ export type DelegationFilter = {
31
+ chainId?: number;
32
+ tags?: string[];
33
+ from?: Address;
34
+ };
35
+ export type DelegationControllerState = {
36
+ delegations: {
37
+ [hash: Hex]: DelegationEntry;
38
+ };
39
+ };
40
+ export type DelegationControllerGetStateAction = ControllerGetStateAction<typeof controllerName, DelegationControllerState>;
41
+ export type DelegationControllerStoreAction = {
42
+ type: `${typeof controllerName}:store`;
43
+ handler: DelegationController['store'];
44
+ };
45
+ export type DelegationControllerSignAction = {
46
+ type: `${typeof controllerName}:sign`;
47
+ handler: DelegationController['sign'];
48
+ };
49
+ export type DelegationControllerActions = DelegationControllerGetStateAction | DelegationControllerStoreAction | DelegationControllerSignAction;
50
+ export type DelegationControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, DelegationControllerState>;
51
+ export type DelegationControllerEvents = DelegationControllerStateChangeEvent;
52
+ type AllowedActions = KeyringControllerSignTypedMessageAction | AccountsControllerGetSelectedAccountAction;
53
+ type AllowedEvents = never;
54
+ export type DelegationControllerMessenger = RestrictedMessenger<typeof controllerName, DelegationControllerActions | AllowedActions, DelegationControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
55
+ //# sourceMappingURL=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AI,CAAC,EAAE,OAAO,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACtC,WAAW,EAAE;QACX,CAAC,IAAI,EAAE,GAAG,GAAG,eAAe,CAAC;KAC9B,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,kCAAkC,GAAG,wBAAwB,CACvE,OAAO,cAAc,EACrB,yBAAyB,CAC1B,CAAC;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,8BAA8B,GAAG;IAC3C,IAAI,EAAE,GAAG,OAAO,cAAc,OAAO,CAAC;IACtC,OAAO,EAAE,oBAAoB,CAAC,MAAM,CAAC,CAAC;CACvC,CAAC;AAEF,MAAM,MAAM,2BAA2B,GACnC,kCAAkC,GAClC,+BAA+B,GAC/B,8BAA8B,CAAC;AAEnC,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"}
package/dist/types.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.mjs.map
@@ -0,0 +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"]}
package/dist/utils.cjs ADDED
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTypedMessageParams = exports.toDelegationStruct = exports.isAddressEqual = void 0;
4
+ const utils_1 = require("@metamask/utils");
5
+ const constants_1 = require("./constants.cjs");
6
+ /**
7
+ * Checks if two addresses are equal.
8
+ *
9
+ * @param a - The first address.
10
+ * @param b - The second address.
11
+ * @returns True if the addresses are equal, false otherwise.
12
+ */
13
+ function isAddressEqual(a, b) {
14
+ return a.toLowerCase() === b.toLowerCase();
15
+ }
16
+ exports.isAddressEqual = isAddressEqual;
17
+ /**
18
+ * Converts a Delegation to a DelegationStruct.
19
+ * The DelegationStruct is the format used in the Delegation Framework.
20
+ *
21
+ * @param delegation the delegation to format
22
+ * @returns the formatted delegation
23
+ */
24
+ const toDelegationStruct = (delegation) => {
25
+ const caveats = delegation.caveats.map((caveat) => ({
26
+ enforcer: (0, utils_1.getChecksumAddress)(caveat.enforcer),
27
+ terms: caveat.terms,
28
+ args: caveat.args,
29
+ }));
30
+ const salt = delegation.salt === '0x' ? 0n : BigInt(delegation.salt);
31
+ return {
32
+ delegate: (0, utils_1.getChecksumAddress)(delegation.delegate),
33
+ delegator: (0, utils_1.getChecksumAddress)(delegation.delegator),
34
+ authority: delegation.authority,
35
+ caveats,
36
+ salt,
37
+ signature: delegation.signature,
38
+ };
39
+ };
40
+ exports.toDelegationStruct = toDelegationStruct;
41
+ /**
42
+ *
43
+ * @param opts - The options for creating typed message params.
44
+ * @returns The typed message params.
45
+ */
46
+ function createTypedMessageParams(opts) {
47
+ const { chainId, from, delegation, verifyingContract } = opts;
48
+ const data = {
49
+ data: {
50
+ types: constants_1.SIGNABLE_DELEGATION_TYPED_DATA,
51
+ primaryType: 'Delegation',
52
+ domain: {
53
+ chainId,
54
+ name: 'DelegationManager',
55
+ version: '1',
56
+ verifyingContract,
57
+ },
58
+ message: (0, exports.toDelegationStruct)(delegation),
59
+ },
60
+ from,
61
+ };
62
+ return data;
63
+ }
64
+ exports.createTypedMessageParams = createTypedMessageParams;
65
+ //# sourceMappingURL=utils.cjs.map
@@ -0,0 +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"]}
@@ -0,0 +1,32 @@
1
+ import type { TypedMessageParams } from "@metamask/keyring-controller";
2
+ import type { Address, Delegation, DelegationStruct } from "./types.cjs";
3
+ /**
4
+ * Checks if two addresses are equal.
5
+ *
6
+ * @param a - The first address.
7
+ * @param b - The second address.
8
+ * @returns True if the addresses are equal, false otherwise.
9
+ */
10
+ export declare function isAddressEqual(a: Address, b: Address): boolean;
11
+ type CreateTypedMessageParamsOptions = {
12
+ chainId: number;
13
+ from: Address;
14
+ delegation: Delegation;
15
+ verifyingContract: Address;
16
+ };
17
+ /**
18
+ * Converts a Delegation to a DelegationStruct.
19
+ * The DelegationStruct is the format used in the Delegation Framework.
20
+ *
21
+ * @param delegation the delegation to format
22
+ * @returns the formatted delegation
23
+ */
24
+ export declare const toDelegationStruct: (delegation: Delegation) => DelegationStruct;
25
+ /**
26
+ *
27
+ * @param opts - The options for creating typed message params.
28
+ * @returns The typed message params.
29
+ */
30
+ export declare function createTypedMessageParams(opts: CreateTypedMessageParamsOptions): TypedMessageParams;
31
+ export {};
32
+ //# sourceMappingURL=utils.d.cts.map
@@ -0,0 +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"}
@@ -0,0 +1,32 @@
1
+ import type { TypedMessageParams } from "@metamask/keyring-controller";
2
+ import type { Address, Delegation, DelegationStruct } from "./types.mjs";
3
+ /**
4
+ * Checks if two addresses are equal.
5
+ *
6
+ * @param a - The first address.
7
+ * @param b - The second address.
8
+ * @returns True if the addresses are equal, false otherwise.
9
+ */
10
+ export declare function isAddressEqual(a: Address, b: Address): boolean;
11
+ type CreateTypedMessageParamsOptions = {
12
+ chainId: number;
13
+ from: Address;
14
+ delegation: Delegation;
15
+ verifyingContract: Address;
16
+ };
17
+ /**
18
+ * Converts a Delegation to a DelegationStruct.
19
+ * The DelegationStruct is the format used in the Delegation Framework.
20
+ *
21
+ * @param delegation the delegation to format
22
+ * @returns the formatted delegation
23
+ */
24
+ export declare const toDelegationStruct: (delegation: Delegation) => DelegationStruct;
25
+ /**
26
+ *
27
+ * @param opts - The options for creating typed message params.
28
+ * @returns The typed message params.
29
+ */
30
+ export declare function createTypedMessageParams(opts: CreateTypedMessageParamsOptions): TypedMessageParams;
31
+ export {};
32
+ //# sourceMappingURL=utils.d.mts.map
@@ -0,0 +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"}
package/dist/utils.mjs ADDED
@@ -0,0 +1,59 @@
1
+ import { getChecksumAddress } from "@metamask/utils";
2
+ import { SIGNABLE_DELEGATION_TYPED_DATA } from "./constants.mjs";
3
+ /**
4
+ * Checks if two addresses are equal.
5
+ *
6
+ * @param a - The first address.
7
+ * @param b - The second address.
8
+ * @returns True if the addresses are equal, false otherwise.
9
+ */
10
+ export function isAddressEqual(a, b) {
11
+ return a.toLowerCase() === b.toLowerCase();
12
+ }
13
+ /**
14
+ * Converts a Delegation to a DelegationStruct.
15
+ * The DelegationStruct is the format used in the Delegation Framework.
16
+ *
17
+ * @param delegation the delegation to format
18
+ * @returns the formatted delegation
19
+ */
20
+ export const toDelegationStruct = (delegation) => {
21
+ const caveats = delegation.caveats.map((caveat) => ({
22
+ enforcer: getChecksumAddress(caveat.enforcer),
23
+ terms: caveat.terms,
24
+ args: caveat.args,
25
+ }));
26
+ const salt = delegation.salt === '0x' ? 0n : BigInt(delegation.salt);
27
+ return {
28
+ delegate: getChecksumAddress(delegation.delegate),
29
+ delegator: getChecksumAddress(delegation.delegator),
30
+ authority: delegation.authority,
31
+ caveats,
32
+ salt,
33
+ signature: delegation.signature,
34
+ };
35
+ };
36
+ /**
37
+ *
38
+ * @param opts - The options for creating typed message params.
39
+ * @returns The typed message params.
40
+ */
41
+ export function createTypedMessageParams(opts) {
42
+ const { chainId, from, delegation, verifyingContract } = opts;
43
+ const data = {
44
+ data: {
45
+ types: SIGNABLE_DELEGATION_TYPED_DATA,
46
+ primaryType: 'Delegation',
47
+ domain: {
48
+ chainId,
49
+ name: 'DelegationManager',
50
+ version: '1',
51
+ verifyingContract,
52
+ },
53
+ message: toDelegationStruct(delegation),
54
+ },
55
+ from,
56
+ };
57
+ return data;
58
+ }
59
+ //# sourceMappingURL=utils.mjs.map
@@ -0,0 +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"]}
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@metamask-previews/delegation-controller",
3
+ "version": "0.1.0-preview-c67b57f2",
4
+ "description": "Manages delegations for MetaMask",
5
+ "keywords": [
6
+ "MetaMask",
7
+ "Ethereum"
8
+ ],
9
+ "homepage": "https://github.com/MetaMask/core/tree/main/packages/delegation-controller#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/MetaMask/core/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/MetaMask/core.git"
16
+ },
17
+ "license": "MIT",
18
+ "sideEffects": false,
19
+ "exports": {
20
+ ".": {
21
+ "import": {
22
+ "types": "./dist/index.d.mts",
23
+ "default": "./dist/index.mjs"
24
+ },
25
+ "require": {
26
+ "types": "./dist/index.d.cts",
27
+ "default": "./dist/index.cjs"
28
+ }
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "main": "./dist/index.cjs",
33
+ "types": "./dist/index.d.cts",
34
+ "files": [
35
+ "dist/"
36
+ ],
37
+ "scripts": {
38
+ "build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
39
+ "build:docs": "typedoc",
40
+ "changelog:update": "../../scripts/update-changelog.sh @metamask/delegation-controller",
41
+ "changelog:validate": "../../scripts/validate-changelog.sh @metamask/delegation-controller",
42
+ "publish:preview": "yarn npm publish --tag preview",
43
+ "since-latest-release": "../../scripts/since-latest-release.sh",
44
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
45
+ "test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
46
+ "test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
47
+ "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
48
+ },
49
+ "dependencies": {
50
+ "@metamask/base-controller": "^8.0.0",
51
+ "@metamask/keyring-controller": "^21.0.2",
52
+ "@metamask/utils": "^11.2.0"
53
+ },
54
+ "devDependencies": {
55
+ "@metamask/accounts-controller": "^27.0.0",
56
+ "@metamask/auto-changelog": "^3.4.4",
57
+ "@ts-bridge/cli": "^0.6.1",
58
+ "@types/jest": "^27.4.1",
59
+ "deepmerge": "^4.2.2",
60
+ "jest": "^27.5.1",
61
+ "ts-jest": "^27.1.4",
62
+ "typedoc": "^0.24.8",
63
+ "typedoc-plugin-missing-exports": "^2.0.0",
64
+ "typescript": "~5.2.2"
65
+ },
66
+ "peerDependencies": {
67
+ "@metamask/accounts-controller": "^27.0.0"
68
+ },
69
+ "engines": {
70
+ "node": "^18.18 || >=20"
71
+ },
72
+ "publishConfig": {
73
+ "access": "public",
74
+ "registry": "https://registry.npmjs.org/"
75
+ }
76
+ }