@metamask-previews/delegation-controller 0.0.0-preview-31a5eea2
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 +14 -0
- package/LICENSE +20 -0
- package/README.md +15 -0
- package/dist/DelegationController.cjs +210 -0
- package/dist/DelegationController.cjs.map +1 -0
- package/dist/DelegationController.d.cts +76 -0
- package/dist/DelegationController.d.cts.map +1 -0
- package/dist/DelegationController.d.mts +76 -0
- package/dist/DelegationController.d.mts.map +1 -0
- package/dist/DelegationController.mjs +206 -0
- package/dist/DelegationController.mjs.map +1 -0
- package/dist/constants.cjs +28 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.cts +31 -0
- package/dist/constants.d.cts.map +1 -0
- package/dist/constants.d.mts +31 -0
- package/dist/constants.d.mts.map +1 -0
- package/dist/constants.mjs +25 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/index.cjs +6 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +3 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types.cjs +3 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +107 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +107 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/dist/utils.cjs +65 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +32 -0
- package/dist/utils.d.cts.map +1 -0
- package/dist/utils.d.mts +32 -0
- package/dist/utils.d.mts.map +1 -0
- package/dist/utils.mjs +59 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +77 -0
package/CHANGELOG.md
ADDED
@@ -0,0 +1,14 @@
|
|
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
|
+
### Added
|
11
|
+
|
12
|
+
- Initial release ([#5592](https://github.com/MetaMask/core/pull/5592))
|
13
|
+
|
14
|
+
[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,210 @@
|
|
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;
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
+
exports.DelegationController = exports.controllerName = void 0;
|
16
|
+
const base_controller_1 = require("@metamask/base-controller");
|
17
|
+
const keyring_controller_1 = require("@metamask/keyring-controller");
|
18
|
+
const utils_1 = require("@metamask/utils");
|
19
|
+
const constants_1 = require("./constants.cjs");
|
20
|
+
const utils_2 = require("./utils.cjs");
|
21
|
+
exports.controllerName = 'DelegationController';
|
22
|
+
const delegationControllerMetadata = {
|
23
|
+
delegations: {
|
24
|
+
persist: true,
|
25
|
+
anonymous: false,
|
26
|
+
},
|
27
|
+
};
|
28
|
+
/**
|
29
|
+
* Constructs the default {@link DelegationController} state. This allows
|
30
|
+
* consumers to provide a partial state object when initializing the controller
|
31
|
+
* and also helps in constructing complete state objects for this controller in
|
32
|
+
* tests.
|
33
|
+
*
|
34
|
+
* @returns The default {@link DelegationController} state.
|
35
|
+
*/
|
36
|
+
function getDefaultDelegationControllerState() {
|
37
|
+
return {
|
38
|
+
delegations: {},
|
39
|
+
};
|
40
|
+
}
|
41
|
+
/**
|
42
|
+
* The {@link DelegationController} class.
|
43
|
+
* This controller is meant to be a centralized place to store and sign delegations.
|
44
|
+
*/
|
45
|
+
class DelegationController extends base_controller_1.BaseController {
|
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, }) {
|
56
|
+
super({
|
57
|
+
messenger,
|
58
|
+
metadata: delegationControllerMetadata,
|
59
|
+
name: exports.controllerName,
|
60
|
+
state: {
|
61
|
+
...getDefaultDelegationControllerState(),
|
62
|
+
...state,
|
63
|
+
},
|
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");
|
69
|
+
}
|
70
|
+
/**
|
71
|
+
* Signs a delegation.
|
72
|
+
*
|
73
|
+
* @param params - The parameters for signing the delegation.
|
74
|
+
* @param params.delegation - The delegation to sign.
|
75
|
+
* @param params.chainId - The chainId of the chain to sign the delegation for.
|
76
|
+
* @returns The signature of the delegation.
|
77
|
+
*/
|
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,
|
89
|
+
});
|
90
|
+
// TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.
|
91
|
+
// Waiting on confirmations team to implement this.
|
92
|
+
const signature = await this.messagingSystem.call('KeyringController:signTypedMessage', data, keyring_controller_1.SignTypedDataVersion.V4);
|
93
|
+
return signature;
|
94
|
+
}
|
95
|
+
/**
|
96
|
+
* Stores a delegation in storage.
|
97
|
+
*
|
98
|
+
* @param params - The parameters for storing the delegation.
|
99
|
+
* @param params.entry - The delegation entry to store.
|
100
|
+
*/
|
101
|
+
store(params) {
|
102
|
+
const { entry } = params;
|
103
|
+
const hash = __classPrivateFieldGet(this, _DelegationController_hashDelegation, "f").call(this, entry.delegation);
|
104
|
+
// If the authority is not the root authority, validate that the
|
105
|
+
// parent entry does exist.
|
106
|
+
if (!(0, utils_2.isHexEqual)(entry.delegation.authority, constants_1.ROOT_AUTHORITY) &&
|
107
|
+
!this.state.delegations[entry.delegation.authority]) {
|
108
|
+
throw new Error('Invalid authority');
|
109
|
+
}
|
110
|
+
this.update((state) => {
|
111
|
+
state.delegations[hash] = entry;
|
112
|
+
});
|
113
|
+
}
|
114
|
+
/**
|
115
|
+
* Lists delegation entries.
|
116
|
+
*
|
117
|
+
* @param filter - The filter to use to list the delegation entries.
|
118
|
+
* @returns A list of delegation entries that match the filter.
|
119
|
+
*/
|
120
|
+
list(filter) {
|
121
|
+
const account = this.messagingSystem.call('AccountsController:getSelectedAccount');
|
122
|
+
const requester = account.address;
|
123
|
+
let list = Object.values(this.state.delegations);
|
124
|
+
if (filter?.from) {
|
125
|
+
list = list.filter((entry) => (0, utils_2.isHexEqual)(entry.delegation.delegator, filter.from));
|
126
|
+
}
|
127
|
+
if (!filter?.from ||
|
128
|
+
(filter?.from && !(0, utils_2.isHexEqual)(filter.from, requester))) {
|
129
|
+
list = list.filter((entry) => (0, utils_2.isHexEqual)(entry.delegation.delegate, requester));
|
130
|
+
}
|
131
|
+
const filterChainId = filter?.chainId;
|
132
|
+
if (filterChainId) {
|
133
|
+
list = list.filter((entry) => (0, utils_2.isHexEqual)(entry.chainId, filterChainId));
|
134
|
+
}
|
135
|
+
const tags = filter?.tags;
|
136
|
+
if (tags && tags.length > 0) {
|
137
|
+
// Filter entries that contain all of the filter tags
|
138
|
+
list = list.filter((entry) => tags.every((tag) => entry.tags.includes(tag)));
|
139
|
+
}
|
140
|
+
return list;
|
141
|
+
}
|
142
|
+
/**
|
143
|
+
* Retrieves the delegation entry for a given delegation hash.
|
144
|
+
*
|
145
|
+
* @param hash - The hash of the delegation to retrieve.
|
146
|
+
* @returns The delegation entry, or null if not found.
|
147
|
+
*/
|
148
|
+
retrieve(hash) {
|
149
|
+
return this.state.delegations[hash] ?? null;
|
150
|
+
}
|
151
|
+
/**
|
152
|
+
* Retrieves a delegation chain from a delegation hash.
|
153
|
+
*
|
154
|
+
* @param hash - The hash of the delegation to retrieve.
|
155
|
+
* @returns The delegation chain, or null if not found.
|
156
|
+
*/
|
157
|
+
chain(hash) {
|
158
|
+
const chain = [];
|
159
|
+
const entry = this.retrieve(hash);
|
160
|
+
if (!entry) {
|
161
|
+
return null;
|
162
|
+
}
|
163
|
+
chain.push(entry);
|
164
|
+
for (let _hash = entry.delegation.authority; _hash !== constants_1.ROOT_AUTHORITY;) {
|
165
|
+
const parent = this.retrieve(_hash);
|
166
|
+
if (!parent) {
|
167
|
+
throw new Error('Invalid delegation chain');
|
168
|
+
}
|
169
|
+
chain.push(parent);
|
170
|
+
_hash = parent.delegation.authority;
|
171
|
+
}
|
172
|
+
return chain;
|
173
|
+
}
|
174
|
+
/**
|
175
|
+
* Deletes a delegation entrie from storage, along with any other entries
|
176
|
+
* that are redelegated from it.
|
177
|
+
*
|
178
|
+
* @param hash - The hash of the delegation to delete.
|
179
|
+
* @returns The number of entries deleted.
|
180
|
+
*/
|
181
|
+
delete(hash) {
|
182
|
+
const root = this.retrieve(hash);
|
183
|
+
if (!root) {
|
184
|
+
return 0;
|
185
|
+
}
|
186
|
+
const entries = Object.entries(this.state.delegations);
|
187
|
+
const nextHashes = [hash];
|
188
|
+
const deletedHashes = [];
|
189
|
+
while (nextHashes.length > 0) {
|
190
|
+
const currentHash = nextHashes.pop();
|
191
|
+
// Find all delegations that have this hash as their authority
|
192
|
+
const children = entries.filter(([_, v]) => v.delegation.authority === currentHash);
|
193
|
+
// Add the hashes of all child delegations to be processed next
|
194
|
+
children.forEach(([k]) => {
|
195
|
+
nextHashes.push(k);
|
196
|
+
});
|
197
|
+
deletedHashes.push(currentHash);
|
198
|
+
}
|
199
|
+
// Delete delegations
|
200
|
+
this.update((state) => {
|
201
|
+
deletedHashes.forEach((h) => {
|
202
|
+
delete state.delegations[h];
|
203
|
+
});
|
204
|
+
});
|
205
|
+
return deletedHashes.length;
|
206
|
+
}
|
207
|
+
}
|
208
|
+
exports.DelegationController = DelegationController;
|
209
|
+
_DelegationController_hashDelegation = new WeakMap(), _DelegationController_getDelegationEnvironment = new WeakMap();
|
210
|
+
//# sourceMappingURL=DelegationController.cjs.map
|
@@ -0,0 +1 @@
|
|
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,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AC,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;AAjOD,oDAiOC","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"]}
|
@@ -0,0 +1,76 @@
|
|
1
|
+
import { BaseController } from "@metamask/base-controller";
|
2
|
+
import type { Delegation, DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DelegationFilter, DeleGatorEnvironment, Hex, UnsignedDelegation } from "./types.cjs";
|
3
|
+
export declare const controllerName = "DelegationController";
|
4
|
+
/**
|
5
|
+
* The {@link DelegationController} class.
|
6
|
+
* This controller is meant to be a centralized place to store and sign delegations.
|
7
|
+
*/
|
8
|
+
export declare class DelegationController extends BaseController<typeof controllerName, DelegationControllerState, DelegationControllerMessenger> {
|
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, }: {
|
20
|
+
messenger: DelegationControllerMessenger;
|
21
|
+
state?: Partial<DelegationControllerState>;
|
22
|
+
hashDelegation: (delegation: Delegation) => Hex;
|
23
|
+
getDelegationEnvironment: (chainId: Hex) => DeleGatorEnvironment;
|
24
|
+
});
|
25
|
+
/**
|
26
|
+
* Signs a delegation.
|
27
|
+
*
|
28
|
+
* @param params - The parameters for signing the delegation.
|
29
|
+
* @param params.delegation - The delegation to sign.
|
30
|
+
* @param params.chainId - The chainId of the chain to sign the delegation for.
|
31
|
+
* @returns The signature of the delegation.
|
32
|
+
*/
|
33
|
+
signDelegation(params: {
|
34
|
+
delegation: UnsignedDelegation;
|
35
|
+
chainId: Hex;
|
36
|
+
}): Promise<string>;
|
37
|
+
/**
|
38
|
+
* Stores a delegation in storage.
|
39
|
+
*
|
40
|
+
* @param params - The parameters for storing the delegation.
|
41
|
+
* @param params.entry - The delegation entry to store.
|
42
|
+
*/
|
43
|
+
store(params: {
|
44
|
+
entry: DelegationEntry;
|
45
|
+
}): void;
|
46
|
+
/**
|
47
|
+
* Lists delegation entries.
|
48
|
+
*
|
49
|
+
* @param filter - The filter to use to list the delegation entries.
|
50
|
+
* @returns A list of delegation entries that match the filter.
|
51
|
+
*/
|
52
|
+
list(filter?: DelegationFilter): DelegationEntry[];
|
53
|
+
/**
|
54
|
+
* Retrieves the delegation entry for a given delegation hash.
|
55
|
+
*
|
56
|
+
* @param hash - The hash of the delegation to retrieve.
|
57
|
+
* @returns The delegation entry, or null if not found.
|
58
|
+
*/
|
59
|
+
retrieve(hash: Hex): DelegationEntry;
|
60
|
+
/**
|
61
|
+
* Retrieves a delegation chain from a delegation hash.
|
62
|
+
*
|
63
|
+
* @param hash - The hash of the delegation to retrieve.
|
64
|
+
* @returns The delegation chain, or null if not found.
|
65
|
+
*/
|
66
|
+
chain(hash: Hex): DelegationEntry[] | null;
|
67
|
+
/**
|
68
|
+
* Deletes a delegation entrie from storage, along with any other entries
|
69
|
+
* that are redelegated from it.
|
70
|
+
*
|
71
|
+
* @param hash - The hash of the delegation to delete.
|
72
|
+
* @returns The number of entries deleted.
|
73
|
+
*/
|
74
|
+
delete(hash: Hex): number;
|
75
|
+
}
|
76
|
+
//# 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;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"}
|
@@ -0,0 +1,76 @@
|
|
1
|
+
import { BaseController } from "@metamask/base-controller";
|
2
|
+
import type { Delegation, DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DelegationFilter, DeleGatorEnvironment, Hex, UnsignedDelegation } from "./types.mjs";
|
3
|
+
export declare const controllerName = "DelegationController";
|
4
|
+
/**
|
5
|
+
* The {@link DelegationController} class.
|
6
|
+
* This controller is meant to be a centralized place to store and sign delegations.
|
7
|
+
*/
|
8
|
+
export declare class DelegationController extends BaseController<typeof controllerName, DelegationControllerState, DelegationControllerMessenger> {
|
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, }: {
|
20
|
+
messenger: DelegationControllerMessenger;
|
21
|
+
state?: Partial<DelegationControllerState>;
|
22
|
+
hashDelegation: (delegation: Delegation) => Hex;
|
23
|
+
getDelegationEnvironment: (chainId: Hex) => DeleGatorEnvironment;
|
24
|
+
});
|
25
|
+
/**
|
26
|
+
* Signs a delegation.
|
27
|
+
*
|
28
|
+
* @param params - The parameters for signing the delegation.
|
29
|
+
* @param params.delegation - The delegation to sign.
|
30
|
+
* @param params.chainId - The chainId of the chain to sign the delegation for.
|
31
|
+
* @returns The signature of the delegation.
|
32
|
+
*/
|
33
|
+
signDelegation(params: {
|
34
|
+
delegation: UnsignedDelegation;
|
35
|
+
chainId: Hex;
|
36
|
+
}): Promise<string>;
|
37
|
+
/**
|
38
|
+
* Stores a delegation in storage.
|
39
|
+
*
|
40
|
+
* @param params - The parameters for storing the delegation.
|
41
|
+
* @param params.entry - The delegation entry to store.
|
42
|
+
*/
|
43
|
+
store(params: {
|
44
|
+
entry: DelegationEntry;
|
45
|
+
}): void;
|
46
|
+
/**
|
47
|
+
* Lists delegation entries.
|
48
|
+
*
|
49
|
+
* @param filter - The filter to use to list the delegation entries.
|
50
|
+
* @returns A list of delegation entries that match the filter.
|
51
|
+
*/
|
52
|
+
list(filter?: DelegationFilter): DelegationEntry[];
|
53
|
+
/**
|
54
|
+
* Retrieves the delegation entry for a given delegation hash.
|
55
|
+
*
|
56
|
+
* @param hash - The hash of the delegation to retrieve.
|
57
|
+
* @returns The delegation entry, or null if not found.
|
58
|
+
*/
|
59
|
+
retrieve(hash: Hex): DelegationEntry;
|
60
|
+
/**
|
61
|
+
* Retrieves a delegation chain from a delegation hash.
|
62
|
+
*
|
63
|
+
* @param hash - The hash of the delegation to retrieve.
|
64
|
+
* @returns The delegation chain, or null if not found.
|
65
|
+
*/
|
66
|
+
chain(hash: Hex): DelegationEntry[] | null;
|
67
|
+
/**
|
68
|
+
* Deletes a delegation entrie from storage, along with any other entries
|
69
|
+
* that are redelegated from it.
|
70
|
+
*
|
71
|
+
* @param hash - The hash of the delegation to delete.
|
72
|
+
* @returns The number of entries deleted.
|
73
|
+
*/
|
74
|
+
delete(hash: Hex): number;
|
75
|
+
}
|
76
|
+
//# 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;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"}
|
@@ -0,0 +1,206 @@
|
|
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;
|
13
|
+
import { BaseController } from "@metamask/base-controller";
|
14
|
+
import { SignTypedDataVersion } from "@metamask/keyring-controller";
|
15
|
+
import { hexToNumber } from "@metamask/utils";
|
16
|
+
import { ROOT_AUTHORITY } from "./constants.mjs";
|
17
|
+
import { createTypedMessageParams, isHexEqual } from "./utils.mjs";
|
18
|
+
export const controllerName = 'DelegationController';
|
19
|
+
const delegationControllerMetadata = {
|
20
|
+
delegations: {
|
21
|
+
persist: true,
|
22
|
+
anonymous: false,
|
23
|
+
},
|
24
|
+
};
|
25
|
+
/**
|
26
|
+
* Constructs the default {@link DelegationController} state. This allows
|
27
|
+
* consumers to provide a partial state object when initializing the controller
|
28
|
+
* and also helps in constructing complete state objects for this controller in
|
29
|
+
* tests.
|
30
|
+
*
|
31
|
+
* @returns The default {@link DelegationController} state.
|
32
|
+
*/
|
33
|
+
function getDefaultDelegationControllerState() {
|
34
|
+
return {
|
35
|
+
delegations: {},
|
36
|
+
};
|
37
|
+
}
|
38
|
+
/**
|
39
|
+
* The {@link DelegationController} class.
|
40
|
+
* This controller is meant to be a centralized place to store and sign delegations.
|
41
|
+
*/
|
42
|
+
export class DelegationController extends BaseController {
|
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, }) {
|
53
|
+
super({
|
54
|
+
messenger,
|
55
|
+
metadata: delegationControllerMetadata,
|
56
|
+
name: controllerName,
|
57
|
+
state: {
|
58
|
+
...getDefaultDelegationControllerState(),
|
59
|
+
...state,
|
60
|
+
},
|
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");
|
66
|
+
}
|
67
|
+
/**
|
68
|
+
* Signs a delegation.
|
69
|
+
*
|
70
|
+
* @param params - The parameters for signing the delegation.
|
71
|
+
* @param params.delegation - The delegation to sign.
|
72
|
+
* @param params.chainId - The chainId of the chain to sign the delegation for.
|
73
|
+
* @returns The signature of the delegation.
|
74
|
+
*/
|
75
|
+
async signDelegation(params) {
|
76
|
+
const { delegation, chainId } = params;
|
77
|
+
const { DelegationManager } = __classPrivateFieldGet(this, _DelegationController_getDelegationEnvironment, "f").call(this, chainId);
|
78
|
+
const data = createTypedMessageParams({
|
79
|
+
chainId: hexToNumber(chainId),
|
80
|
+
from: delegation.delegator,
|
81
|
+
delegation: {
|
82
|
+
...delegation,
|
83
|
+
signature: '0x',
|
84
|
+
},
|
85
|
+
verifyingContract: DelegationManager,
|
86
|
+
});
|
87
|
+
// TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.
|
88
|
+
// Waiting on confirmations team to implement this.
|
89
|
+
const signature = await this.messagingSystem.call('KeyringController:signTypedMessage', data, SignTypedDataVersion.V4);
|
90
|
+
return signature;
|
91
|
+
}
|
92
|
+
/**
|
93
|
+
* Stores a delegation in storage.
|
94
|
+
*
|
95
|
+
* @param params - The parameters for storing the delegation.
|
96
|
+
* @param params.entry - The delegation entry to store.
|
97
|
+
*/
|
98
|
+
store(params) {
|
99
|
+
const { entry } = params;
|
100
|
+
const hash = __classPrivateFieldGet(this, _DelegationController_hashDelegation, "f").call(this, entry.delegation);
|
101
|
+
// If the authority is not the root authority, validate that the
|
102
|
+
// parent entry does exist.
|
103
|
+
if (!isHexEqual(entry.delegation.authority, ROOT_AUTHORITY) &&
|
104
|
+
!this.state.delegations[entry.delegation.authority]) {
|
105
|
+
throw new Error('Invalid authority');
|
106
|
+
}
|
107
|
+
this.update((state) => {
|
108
|
+
state.delegations[hash] = entry;
|
109
|
+
});
|
110
|
+
}
|
111
|
+
/**
|
112
|
+
* Lists delegation entries.
|
113
|
+
*
|
114
|
+
* @param filter - The filter to use to list the delegation entries.
|
115
|
+
* @returns A list of delegation entries that match the filter.
|
116
|
+
*/
|
117
|
+
list(filter) {
|
118
|
+
const account = this.messagingSystem.call('AccountsController:getSelectedAccount');
|
119
|
+
const requester = account.address;
|
120
|
+
let list = Object.values(this.state.delegations);
|
121
|
+
if (filter?.from) {
|
122
|
+
list = list.filter((entry) => isHexEqual(entry.delegation.delegator, filter.from));
|
123
|
+
}
|
124
|
+
if (!filter?.from ||
|
125
|
+
(filter?.from && !isHexEqual(filter.from, requester))) {
|
126
|
+
list = list.filter((entry) => isHexEqual(entry.delegation.delegate, requester));
|
127
|
+
}
|
128
|
+
const filterChainId = filter?.chainId;
|
129
|
+
if (filterChainId) {
|
130
|
+
list = list.filter((entry) => isHexEqual(entry.chainId, filterChainId));
|
131
|
+
}
|
132
|
+
const tags = filter?.tags;
|
133
|
+
if (tags && tags.length > 0) {
|
134
|
+
// Filter entries that contain all of the filter tags
|
135
|
+
list = list.filter((entry) => tags.every((tag) => entry.tags.includes(tag)));
|
136
|
+
}
|
137
|
+
return list;
|
138
|
+
}
|
139
|
+
/**
|
140
|
+
* Retrieves the delegation entry for a given delegation hash.
|
141
|
+
*
|
142
|
+
* @param hash - The hash of the delegation to retrieve.
|
143
|
+
* @returns The delegation entry, or null if not found.
|
144
|
+
*/
|
145
|
+
retrieve(hash) {
|
146
|
+
return this.state.delegations[hash] ?? null;
|
147
|
+
}
|
148
|
+
/**
|
149
|
+
* Retrieves a delegation chain from a delegation hash.
|
150
|
+
*
|
151
|
+
* @param hash - The hash of the delegation to retrieve.
|
152
|
+
* @returns The delegation chain, or null if not found.
|
153
|
+
*/
|
154
|
+
chain(hash) {
|
155
|
+
const chain = [];
|
156
|
+
const entry = this.retrieve(hash);
|
157
|
+
if (!entry) {
|
158
|
+
return null;
|
159
|
+
}
|
160
|
+
chain.push(entry);
|
161
|
+
for (let _hash = entry.delegation.authority; _hash !== ROOT_AUTHORITY;) {
|
162
|
+
const parent = this.retrieve(_hash);
|
163
|
+
if (!parent) {
|
164
|
+
throw new Error('Invalid delegation chain');
|
165
|
+
}
|
166
|
+
chain.push(parent);
|
167
|
+
_hash = parent.delegation.authority;
|
168
|
+
}
|
169
|
+
return chain;
|
170
|
+
}
|
171
|
+
/**
|
172
|
+
* Deletes a delegation entrie from storage, along with any other entries
|
173
|
+
* that are redelegated from it.
|
174
|
+
*
|
175
|
+
* @param hash - The hash of the delegation to delete.
|
176
|
+
* @returns The number of entries deleted.
|
177
|
+
*/
|
178
|
+
delete(hash) {
|
179
|
+
const root = this.retrieve(hash);
|
180
|
+
if (!root) {
|
181
|
+
return 0;
|
182
|
+
}
|
183
|
+
const entries = Object.entries(this.state.delegations);
|
184
|
+
const nextHashes = [hash];
|
185
|
+
const deletedHashes = [];
|
186
|
+
while (nextHashes.length > 0) {
|
187
|
+
const currentHash = nextHashes.pop();
|
188
|
+
// Find all delegations that have this hash as their authority
|
189
|
+
const children = entries.filter(([_, v]) => v.delegation.authority === currentHash);
|
190
|
+
// Add the hashes of all child delegations to be processed next
|
191
|
+
children.forEach(([k]) => {
|
192
|
+
nextHashes.push(k);
|
193
|
+
});
|
194
|
+
deletedHashes.push(currentHash);
|
195
|
+
}
|
196
|
+
// Delete delegations
|
197
|
+
this.update((state) => {
|
198
|
+
deletedHashes.forEach((h) => {
|
199
|
+
delete state.delegations[h];
|
200
|
+
});
|
201
|
+
});
|
202
|
+
return deletedHashes.length;
|
203
|
+
}
|
204
|
+
}
|
205
|
+
_DelegationController_hashDelegation = new WeakMap(), _DelegationController_getDelegationEnvironment = new WeakMap();
|
206
|
+
//# sourceMappingURL=DelegationController.mjs.map
|