@metamask-previews/delegation-controller 0.1.0-preview-c67b57f2 → 0.1.0-preview-c555b24d
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 +4 -0
- package/dist/DelegationController.cjs +37 -26
- package/dist/DelegationController.cjs.map +1 -1
- package/dist/DelegationController.d.cts +13 -18
- package/dist/DelegationController.d.cts.map +1 -1
- package/dist/DelegationController.d.mts +13 -18
- package/dist/DelegationController.d.mts.map +1 -1
- package/dist/DelegationController.mjs +36 -24
- package/dist/DelegationController.mjs.map +1 -1
- package/dist/index.cjs +1 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +39 -7
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +39 -7
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils.cjs +7 -7
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +6 -6
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +6 -6
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +5 -5
- package/dist/utils.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.DelegationController = exports.
|
3
|
+
exports.DelegationController = exports.controllerName = void 0;
|
4
4
|
const base_controller_1 = require("@metamask/base-controller");
|
5
5
|
const keyring_controller_1 = require("@metamask/keyring-controller");
|
6
|
+
const utils_1 = require("@metamask/utils");
|
6
7
|
const constants_1 = require("./constants.cjs");
|
7
|
-
const
|
8
|
+
const utils_2 = require("./utils.cjs");
|
8
9
|
exports.controllerName = 'DelegationController';
|
9
10
|
const delegationControllerMetadata = {
|
10
11
|
delegations: {
|
@@ -25,13 +26,12 @@ function getDefaultDelegationControllerState() {
|
|
25
26
|
delegations: {},
|
26
27
|
};
|
27
28
|
}
|
28
|
-
exports.getDefaultDelegationControllerState = getDefaultDelegationControllerState;
|
29
29
|
/**
|
30
30
|
* The {@link DelegationController} class.
|
31
31
|
* This controller is meant to be a centralized place to store and sign delegations.
|
32
32
|
*/
|
33
33
|
class DelegationController extends base_controller_1.BaseController {
|
34
|
-
constructor({ messenger, state, }) {
|
34
|
+
constructor({ messenger, state, hashDelegation, }) {
|
35
35
|
super({
|
36
36
|
messenger,
|
37
37
|
metadata: delegationControllerMetadata,
|
@@ -41,23 +41,27 @@ class DelegationController extends base_controller_1.BaseController {
|
|
41
41
|
...state,
|
42
42
|
},
|
43
43
|
});
|
44
|
+
this.hashDelegation = hashDelegation;
|
44
45
|
}
|
45
46
|
/**
|
46
47
|
* Signs a delegation.
|
47
48
|
*
|
48
49
|
* @param params - The parameters for signing the delegation.
|
49
50
|
* @param params.delegation - The delegation to sign.
|
50
|
-
* @param params.verifyingContract - The address of the verifying contract (DelegationManager).
|
51
51
|
* @param params.chainId - The chainId of the chain to sign the delegation for.
|
52
|
+
* @param params.verifyingContract - The address of the verifying contract (DelegationManager).
|
52
53
|
* @returns The signature of the delegation.
|
53
54
|
*/
|
54
|
-
async
|
55
|
+
async signDelegation(params) {
|
55
56
|
const { delegation, verifyingContract, chainId } = params;
|
56
57
|
const account = this.messagingSystem.call('AccountsController:getSelectedAccount');
|
57
|
-
const data = (0,
|
58
|
-
chainId,
|
58
|
+
const data = (0, utils_2.createTypedMessageParams)({
|
59
|
+
chainId: (0, utils_1.hexToNumber)(chainId),
|
59
60
|
from: account.address,
|
60
|
-
delegation
|
61
|
+
delegation: {
|
62
|
+
...delegation,
|
63
|
+
signature: '0x',
|
64
|
+
},
|
61
65
|
verifyingContract,
|
62
66
|
});
|
63
67
|
// TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.
|
@@ -68,14 +72,16 @@ class DelegationController extends base_controller_1.BaseController {
|
|
68
72
|
/**
|
69
73
|
* Stores a delegation in storage.
|
70
74
|
*
|
71
|
-
* @param
|
72
|
-
* @param entry - The delegation entry to store.
|
75
|
+
* @param params - The parameters for storing the delegation.
|
76
|
+
* @param params.entry - The delegation entry to store.
|
73
77
|
*/
|
74
|
-
store(
|
78
|
+
store(params) {
|
79
|
+
const { entry } = params;
|
80
|
+
const hash = this.hashDelegation(entry.delegation);
|
75
81
|
// If the authority is not the root authority, validate that the
|
76
82
|
// parent entry does exist.
|
77
|
-
if (!(0,
|
78
|
-
!this.state.delegations[entry.
|
83
|
+
if (!(0, utils_2.isHexEqual)(entry.delegation.authority, constants_1.ROOT_AUTHORITY) &&
|
84
|
+
!this.state.delegations[entry.delegation.authority]) {
|
79
85
|
throw new Error('Invalid authority');
|
80
86
|
}
|
81
87
|
this.update((state) => {
|
@@ -93,14 +99,15 @@ class DelegationController extends base_controller_1.BaseController {
|
|
93
99
|
const requester = account.address;
|
94
100
|
let list = Object.values(this.state.delegations);
|
95
101
|
if (filter?.from) {
|
96
|
-
list = list.filter((entry) => (0,
|
102
|
+
list = list.filter((entry) => (0, utils_2.isHexEqual)(entry.delegation.delegator, filter.from));
|
97
103
|
}
|
98
104
|
if (!filter?.from ||
|
99
|
-
(filter?.from && !(0,
|
100
|
-
list = list.filter((entry) => (0,
|
105
|
+
(filter?.from && !(0, utils_2.isHexEqual)(filter.from, requester))) {
|
106
|
+
list = list.filter((entry) => (0, utils_2.isHexEqual)(entry.delegation.delegate, requester));
|
101
107
|
}
|
102
|
-
|
103
|
-
|
108
|
+
const filterChainId = filter?.chainId;
|
109
|
+
if (filterChainId) {
|
110
|
+
list = list.filter((entry) => (0, utils_2.isHexEqual)(entry.chainId, filterChainId));
|
104
111
|
}
|
105
112
|
const tags = filter?.tags;
|
106
113
|
if (tags && tags.length > 0) {
|
@@ -131,13 +138,13 @@ class DelegationController extends base_controller_1.BaseController {
|
|
131
138
|
return null;
|
132
139
|
}
|
133
140
|
chain.push(entry);
|
134
|
-
for (let _hash = entry.
|
141
|
+
for (let _hash = entry.delegation.authority; _hash !== constants_1.ROOT_AUTHORITY;) {
|
135
142
|
const parent = this.retrieve(_hash);
|
136
143
|
if (!parent) {
|
137
144
|
throw new Error('Invalid delegation chain');
|
138
145
|
}
|
139
146
|
chain.push(parent);
|
140
|
-
_hash = parent.
|
147
|
+
_hash = parent.delegation.authority;
|
141
148
|
}
|
142
149
|
return chain;
|
143
150
|
}
|
@@ -156,20 +163,24 @@ class DelegationController extends base_controller_1.BaseController {
|
|
156
163
|
const entries = Object.entries(this.state.delegations);
|
157
164
|
let count = 0;
|
158
165
|
const nextHashes = [hash];
|
166
|
+
const deletedHashes = [];
|
159
167
|
while (nextHashes.length > 0) {
|
160
168
|
const currentHash = nextHashes.pop();
|
161
169
|
// Find all delegations that have this hash as their authority
|
162
|
-
const children = entries.filter(([_, v]) => v.
|
170
|
+
const children = entries.filter(([_, v]) => v.delegation.authority === currentHash);
|
163
171
|
// Add the hashes of all child delegations to be processed next
|
164
172
|
children.forEach(([k]) => {
|
165
173
|
nextHashes.push(k);
|
166
174
|
});
|
167
|
-
|
168
|
-
this.update((state) => {
|
169
|
-
delete state.delegations[currentHash];
|
170
|
-
});
|
175
|
+
deletedHashes.push(currentHash);
|
171
176
|
count += 1;
|
172
177
|
}
|
178
|
+
// Delete delegations
|
179
|
+
this.update((state) => {
|
180
|
+
deletedHashes.forEach((h) => {
|
181
|
+
delete state.delegations[h];
|
182
|
+
});
|
183
|
+
});
|
173
184
|
return count;
|
174
185
|
}
|
175
186
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"DelegationController.cjs","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":";;;AACA,+DAA2D;AAC3D,qEAAoE;AAEpE,+CAA6C;AAU7C,uCAAmE;AAEtD,QAAA,cA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AI,EAAE;YACT,OAAO,CAAC,CAAC;SACV;QAED,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QACvD,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,MAAM,UAAU,GAAU,CAAC,IAAI,CAAC,CAAC;QAEjC,OAAO,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;YAC5B,MAAM,WAAW,GAAG,UAAU,CAAC,GAAG,EAAS,CAAC;YAE5C,8DAA8D;YAC9D,MAAM,QAAQ,GAAG,OAAO,CAAC,MAAM,CAC7B,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,KAAK,WAAW,CAC7C,CAAC;YAEF,+DAA+D;YAC/D,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;gBACvB,UAAU,CAAC,IAAI,CAAC,CAAQ,CAAC,CAAC;YAC5B,CAAC,CAAC,CAAC;YAEH,gCAAgC;YAChC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,OAAO,KAAK,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;YACxC,CAAC,CAAC,CAAC;YACH,KAAK,IAAI,CAAC,CAAC;SACZ;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAzMD,oDAyMC","sourcesContent":["import type { StateMetadata } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { SignTypedDataVersion } from '@metamask/keyring-controller';\n\nimport { ROOT_AUTHORITY } from './constants';\nimport type {\n Address,\n Delegation,\n DelegationControllerMessenger,\n DelegationControllerState,\n DelegationEntry,\n DelegationFilter,\n Hex,\n} from './types';\nimport { createTypedMessageParams, isAddressEqual } from './utils';\n\nexport const controllerName = 'DelegationController';\n\nconst delegationControllerMetadata = {\n delegations: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<DelegationControllerState>;\n\n/**\n * Constructs the default {@link DelegationController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link DelegationController} state.\n */\nexport function getDefaultDelegationControllerState(): DelegationControllerState {\n return {\n delegations: {},\n };\n}\n\n/**\n * The {@link DelegationController} class.\n * This controller is meant to be a centralized place to store and sign delegations.\n */\nexport class DelegationController extends BaseController<\n typeof controllerName,\n DelegationControllerState,\n DelegationControllerMessenger\n> {\n constructor({\n messenger,\n state,\n }: {\n messenger: DelegationControllerMessenger;\n state?: Partial<DelegationControllerState>;\n }) {\n super({\n messenger,\n metadata: delegationControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultDelegationControllerState(),\n ...state,\n },\n });\n }\n\n /**\n * Signs a delegation.\n *\n * @param params - The parameters for signing the delegation.\n * @param params.delegation - The delegation to sign.\n * @param params.verifyingContract - The address of the verifying contract (DelegationManager).\n * @param params.chainId - The chainId of the chain to sign the delegation for.\n * @returns The signature of the delegation.\n */\n async sign(params: {\n delegation: Delegation;\n verifyingContract: Address;\n chainId: number;\n }) {\n const { delegation, verifyingContract, chainId } = params;\n\n const account = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n\n const data = createTypedMessageParams({\n chainId,\n from: account.address as Address,\n delegation,\n verifyingContract,\n });\n\n // TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.\n // Waiting on confirmations team to implement this.\n const signature: string = await this.messagingSystem.call(\n 'KeyringController:signTypedMessage',\n data,\n SignTypedDataVersion.V4,\n );\n\n return signature;\n }\n\n /**\n * Stores a delegation in storage.\n *\n * @param hash - The hash of the delegation to store.\n * @param entry - The delegation entry to store.\n */\n store(hash: Hex, entry: DelegationEntry) {\n // If the authority is not the root authority, validate that the\n // parent entry does exist.\n if (\n !isAddressEqual(entry.data.authority, ROOT_AUTHORITY) &&\n !this.state.delegations[entry.data.authority]\n ) {\n throw new Error('Invalid authority');\n }\n this.update((state) => {\n state.delegations[hash] = entry;\n });\n }\n\n /**\n * Lists delegation entries.\n *\n * @param filter - The filter to use to list the delegation entries.\n * @returns A list of delegation entries that match the filter.\n */\n list(filter?: DelegationFilter) {\n const account = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n const requester = account.address as Address;\n\n let list: DelegationEntry[] = Object.values(this.state.delegations);\n\n if (filter?.from) {\n list = list.filter((entry) =>\n isAddressEqual(entry.data.delegator, filter.from as Address),\n );\n }\n\n if (\n !filter?.from ||\n (filter?.from && !isAddressEqual(filter.from, requester))\n ) {\n list = list.filter((entry) =>\n isAddressEqual(entry.data.delegate, requester),\n );\n }\n\n if (filter?.chainId) {\n list = list.filter((entry) => entry.chainId === filter.chainId);\n }\n\n const tags = filter?.tags;\n if (tags && tags.length > 0) {\n // Filter entries that contain all of the filter tags\n list = list.filter((entry) =>\n tags.every((tag) => entry.tags.includes(tag)),\n );\n }\n\n return list;\n }\n\n /**\n * Retrieves the delegation entry for a given delegation hash.\n *\n * @param hash - The hash of the delegation to retrieve.\n * @returns The delegation entry, or null if not found.\n */\n retrieve(hash: Hex) {\n return this.state.delegations[hash] ?? null;\n }\n\n /**\n * Retrieves a delegation chain from a delegation hash.\n *\n * @param hash - The hash of the delegation to retrieve.\n * @returns The delegation chain, or null if not found.\n */\n chain(hash: Hex) {\n const chain: DelegationEntry[] = [];\n\n const entry = this.retrieve(hash);\n if (!entry) {\n return null;\n }\n chain.push(entry);\n\n for (let _hash = entry.data.authority; _hash !== ROOT_AUTHORITY; ) {\n const parent = this.retrieve(_hash);\n if (!parent) {\n throw new Error('Invalid delegation chain');\n }\n chain.push(parent);\n _hash = parent.data.authority;\n }\n\n return chain;\n }\n\n /**\n * Deletes a delegation entrie from storage, along with any other entries\n * that are redelegated from it.\n *\n * @param hash - The hash of the delegation to delete.\n * @returns The number of entries deleted.\n */\n delete(hash: Hex): number {\n const root = this.retrieve(hash);\n if (!root) {\n return 0;\n }\n\n const entries = Object.entries(this.state.delegations);\n let count = 0;\n const nextHashes: Hex[] = [hash];\n\n while (nextHashes.length > 0) {\n const currentHash = nextHashes.pop() as Hex;\n\n // Find all delegations that have this hash as their authority\n const children = entries.filter(\n ([_, v]) => v.data.authority === currentHash,\n );\n\n // Add the hashes of all child delegations to be processed next\n children.forEach(([k]) => {\n nextHashes.push(k as Hex);\n });\n\n // Delete the current delegation\n this.update((state) => {\n delete state.delegations[currentHash];\n });\n count += 1;\n }\n\n return count;\n }\n}\n"]}
|
1
|
+
{"version":3,"file":"DelegationController.cjs","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":";;;AACA,+DAA2D;AAC3D,qEAAoE;AACpE,2CAA8C;AAE9C,+CAA6C;AAW7C,uCAA+D;AAElD,QAAA,cAAc,GAAG,sBAAsB,CAAC;AAErD,MAAM,4BAA4B,GAAG;IACnC,WAAW,EAAE;QACX,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACiD,CAAC;AAErD;;;;;;;GAOG;AACH,SAAS,mCAAmC;IAC1C,OAAO;QACL,WAAW,EAAE,EAAE;KAChB,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAa,oBAAqB,SAAQ,gCAIzC;IAGC,YAAY,EACV,SAAS,EACT,KAAK,EACL,cAAc,GAKf;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,4BAA4B;YACtC,IAAI,EAAE,sBAAc;YACpB,KAAK,EAAE;gBACL,GAAG,mCAAmC,EAAE;gBACxC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;QACH,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;IACvC,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,cAAc,CAAC,MAIpB;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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AAC,CAAC,CAAC,UAAU,CAAC,SAAS,KAAK,WAAW,CACnD,CAAC;YAEF,+DAA+D;YAC/D,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;gBACvB,UAAU,CAAC,IAAI,CAAC,CAAQ,CAAC,CAAC;YAC5B,CAAC,CAAC,CAAC;YAEH,aAAa,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;YAChC,KAAK,IAAI,CAAC,CAAC;SACZ;QAED,qBAAqB;QACrB,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;gBAC1B,OAAO,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;YAC9B,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AA1ND,oDA0NC","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 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 private readonly hashDelegation: (delegation: Delegation) => Hex;\n\n constructor({\n messenger,\n state,\n hashDelegation,\n }: {\n messenger: DelegationControllerMessenger;\n state?: Partial<DelegationControllerState>;\n hashDelegation: (delegation: Delegation) => Hex;\n }) {\n super({\n messenger,\n metadata: delegationControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultDelegationControllerState(),\n ...state,\n },\n });\n this.hashDelegation = hashDelegation;\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 * @param params.verifyingContract - The address of the verifying contract (DelegationManager).\n * @returns The signature of the delegation.\n */\n async signDelegation(params: {\n delegation: UnsignedDelegation;\n chainId: Hex;\n verifyingContract: Address;\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: hexToNumber(chainId),\n from: account.address as Address,\n delegation: {\n ...delegation,\n signature: '0x',\n },\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 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 let count = 0;\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 count += 1;\n }\n\n // Delete delegations\n this.update((state) => {\n deletedHashes.forEach((h) => {\n delete state.delegations[h];\n });\n });\n\n return count;\n }\n}\n"]}
|
@@ -1,45 +1,40 @@
|
|
1
1
|
import { BaseController } from "@metamask/base-controller";
|
2
|
-
import type { Address, Delegation, DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DelegationFilter, Hex } from "./types.cjs";
|
2
|
+
import type { Address, Delegation, DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DelegationFilter, Hex, UnsignedDelegation } from "./types.cjs";
|
3
3
|
export declare const controllerName = "DelegationController";
|
4
|
-
/**
|
5
|
-
* Constructs the default {@link DelegationController} state. This allows
|
6
|
-
* consumers to provide a partial state object when initializing the controller
|
7
|
-
* and also helps in constructing complete state objects for this controller in
|
8
|
-
* tests.
|
9
|
-
*
|
10
|
-
* @returns The default {@link DelegationController} state.
|
11
|
-
*/
|
12
|
-
export declare function getDefaultDelegationControllerState(): DelegationControllerState;
|
13
4
|
/**
|
14
5
|
* The {@link DelegationController} class.
|
15
6
|
* This controller is meant to be a centralized place to store and sign delegations.
|
16
7
|
*/
|
17
8
|
export declare class DelegationController extends BaseController<typeof controllerName, DelegationControllerState, DelegationControllerMessenger> {
|
18
|
-
|
9
|
+
private readonly hashDelegation;
|
10
|
+
constructor({ messenger, state, hashDelegation, }: {
|
19
11
|
messenger: DelegationControllerMessenger;
|
20
12
|
state?: Partial<DelegationControllerState>;
|
13
|
+
hashDelegation: (delegation: Delegation) => Hex;
|
21
14
|
});
|
22
15
|
/**
|
23
16
|
* Signs a delegation.
|
24
17
|
*
|
25
18
|
* @param params - The parameters for signing the delegation.
|
26
19
|
* @param params.delegation - The delegation to sign.
|
27
|
-
* @param params.verifyingContract - The address of the verifying contract (DelegationManager).
|
28
20
|
* @param params.chainId - The chainId of the chain to sign the delegation for.
|
21
|
+
* @param params.verifyingContract - The address of the verifying contract (DelegationManager).
|
29
22
|
* @returns The signature of the delegation.
|
30
23
|
*/
|
31
|
-
|
32
|
-
delegation:
|
24
|
+
signDelegation(params: {
|
25
|
+
delegation: UnsignedDelegation;
|
26
|
+
chainId: Hex;
|
33
27
|
verifyingContract: Address;
|
34
|
-
chainId: number;
|
35
28
|
}): Promise<string>;
|
36
29
|
/**
|
37
30
|
* Stores a delegation in storage.
|
38
31
|
*
|
39
|
-
* @param
|
40
|
-
* @param entry - The delegation entry to store.
|
32
|
+
* @param params - The parameters for storing the delegation.
|
33
|
+
* @param params.entry - The delegation entry to store.
|
41
34
|
*/
|
42
|
-
store(
|
35
|
+
store(params: {
|
36
|
+
entry: DelegationEntry;
|
37
|
+
}): void;
|
43
38
|
/**
|
44
39
|
* Lists delegation entries.
|
45
40
|
*
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"DelegationController.d.cts","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;
|
1
|
+
{"version":3,"file":"DelegationController.d.cts","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAK3D,OAAO,KAAK,EACV,OAAO,EACP,UAAU,EACV,6BAA6B,EAC7B,yBAAyB,EACzB,eAAe,EACf,gBAAgB,EAChB,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;IACC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAkC;gBAErD,EACV,SAAS,EACT,KAAK,EACL,cAAc,GACf,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;KACjD;IAaD;;;;;;;;OAQG;IACG,cAAc,CAAC,MAAM,EAAE;QAC3B,UAAU,EAAE,kBAAkB,CAAC;QAC/B,OAAO,EAAE,GAAG,CAAC;QACb,iBAAiB,EAAE,OAAO,CAAC;KAC5B;IA4BD;;;;;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;CAqC1B"}
|
@@ -1,45 +1,40 @@
|
|
1
1
|
import { BaseController } from "@metamask/base-controller";
|
2
|
-
import type { Address, Delegation, DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DelegationFilter, Hex } from "./types.mjs";
|
2
|
+
import type { Address, Delegation, DelegationControllerMessenger, DelegationControllerState, DelegationEntry, DelegationFilter, Hex, UnsignedDelegation } from "./types.mjs";
|
3
3
|
export declare const controllerName = "DelegationController";
|
4
|
-
/**
|
5
|
-
* Constructs the default {@link DelegationController} state. This allows
|
6
|
-
* consumers to provide a partial state object when initializing the controller
|
7
|
-
* and also helps in constructing complete state objects for this controller in
|
8
|
-
* tests.
|
9
|
-
*
|
10
|
-
* @returns The default {@link DelegationController} state.
|
11
|
-
*/
|
12
|
-
export declare function getDefaultDelegationControllerState(): DelegationControllerState;
|
13
4
|
/**
|
14
5
|
* The {@link DelegationController} class.
|
15
6
|
* This controller is meant to be a centralized place to store and sign delegations.
|
16
7
|
*/
|
17
8
|
export declare class DelegationController extends BaseController<typeof controllerName, DelegationControllerState, DelegationControllerMessenger> {
|
18
|
-
|
9
|
+
private readonly hashDelegation;
|
10
|
+
constructor({ messenger, state, hashDelegation, }: {
|
19
11
|
messenger: DelegationControllerMessenger;
|
20
12
|
state?: Partial<DelegationControllerState>;
|
13
|
+
hashDelegation: (delegation: Delegation) => Hex;
|
21
14
|
});
|
22
15
|
/**
|
23
16
|
* Signs a delegation.
|
24
17
|
*
|
25
18
|
* @param params - The parameters for signing the delegation.
|
26
19
|
* @param params.delegation - The delegation to sign.
|
27
|
-
* @param params.verifyingContract - The address of the verifying contract (DelegationManager).
|
28
20
|
* @param params.chainId - The chainId of the chain to sign the delegation for.
|
21
|
+
* @param params.verifyingContract - The address of the verifying contract (DelegationManager).
|
29
22
|
* @returns The signature of the delegation.
|
30
23
|
*/
|
31
|
-
|
32
|
-
delegation:
|
24
|
+
signDelegation(params: {
|
25
|
+
delegation: UnsignedDelegation;
|
26
|
+
chainId: Hex;
|
33
27
|
verifyingContract: Address;
|
34
|
-
chainId: number;
|
35
28
|
}): Promise<string>;
|
36
29
|
/**
|
37
30
|
* Stores a delegation in storage.
|
38
31
|
*
|
39
|
-
* @param
|
40
|
-
* @param entry - The delegation entry to store.
|
32
|
+
* @param params - The parameters for storing the delegation.
|
33
|
+
* @param params.entry - The delegation entry to store.
|
41
34
|
*/
|
42
|
-
store(
|
35
|
+
store(params: {
|
36
|
+
entry: DelegationEntry;
|
37
|
+
}): void;
|
43
38
|
/**
|
44
39
|
* Lists delegation entries.
|
45
40
|
*
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"DelegationController.d.mts","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;
|
1
|
+
{"version":3,"file":"DelegationController.d.mts","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAK3D,OAAO,KAAK,EACV,OAAO,EACP,UAAU,EACV,6BAA6B,EAC7B,yBAAyB,EACzB,eAAe,EACf,gBAAgB,EAChB,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;IACC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAkC;gBAErD,EACV,SAAS,EACT,KAAK,EACL,cAAc,GACf,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;KACjD;IAaD;;;;;;;;OAQG;IACG,cAAc,CAAC,MAAM,EAAE;QAC3B,UAAU,EAAE,kBAAkB,CAAC;QAC/B,OAAO,EAAE,GAAG,CAAC;QACb,iBAAiB,EAAE,OAAO,CAAC;KAC5B;IA4BD;;;;;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;CAqC1B"}
|
@@ -1,7 +1,8 @@
|
|
1
1
|
import { BaseController } from "@metamask/base-controller";
|
2
2
|
import { SignTypedDataVersion } from "@metamask/keyring-controller";
|
3
|
+
import { hexToNumber } from "@metamask/utils";
|
3
4
|
import { ROOT_AUTHORITY } from "./constants.mjs";
|
4
|
-
import { createTypedMessageParams,
|
5
|
+
import { createTypedMessageParams, isHexEqual } from "./utils.mjs";
|
5
6
|
export const controllerName = 'DelegationController';
|
6
7
|
const delegationControllerMetadata = {
|
7
8
|
delegations: {
|
@@ -17,7 +18,7 @@ const delegationControllerMetadata = {
|
|
17
18
|
*
|
18
19
|
* @returns The default {@link DelegationController} state.
|
19
20
|
*/
|
20
|
-
|
21
|
+
function getDefaultDelegationControllerState() {
|
21
22
|
return {
|
22
23
|
delegations: {},
|
23
24
|
};
|
@@ -27,7 +28,7 @@ export function getDefaultDelegationControllerState() {
|
|
27
28
|
* This controller is meant to be a centralized place to store and sign delegations.
|
28
29
|
*/
|
29
30
|
export class DelegationController extends BaseController {
|
30
|
-
constructor({ messenger, state, }) {
|
31
|
+
constructor({ messenger, state, hashDelegation, }) {
|
31
32
|
super({
|
32
33
|
messenger,
|
33
34
|
metadata: delegationControllerMetadata,
|
@@ -37,23 +38,27 @@ export class DelegationController extends BaseController {
|
|
37
38
|
...state,
|
38
39
|
},
|
39
40
|
});
|
41
|
+
this.hashDelegation = hashDelegation;
|
40
42
|
}
|
41
43
|
/**
|
42
44
|
* Signs a delegation.
|
43
45
|
*
|
44
46
|
* @param params - The parameters for signing the delegation.
|
45
47
|
* @param params.delegation - The delegation to sign.
|
46
|
-
* @param params.verifyingContract - The address of the verifying contract (DelegationManager).
|
47
48
|
* @param params.chainId - The chainId of the chain to sign the delegation for.
|
49
|
+
* @param params.verifyingContract - The address of the verifying contract (DelegationManager).
|
48
50
|
* @returns The signature of the delegation.
|
49
51
|
*/
|
50
|
-
async
|
52
|
+
async signDelegation(params) {
|
51
53
|
const { delegation, verifyingContract, chainId } = params;
|
52
54
|
const account = this.messagingSystem.call('AccountsController:getSelectedAccount');
|
53
55
|
const data = createTypedMessageParams({
|
54
|
-
chainId,
|
56
|
+
chainId: hexToNumber(chainId),
|
55
57
|
from: account.address,
|
56
|
-
delegation
|
58
|
+
delegation: {
|
59
|
+
...delegation,
|
60
|
+
signature: '0x',
|
61
|
+
},
|
57
62
|
verifyingContract,
|
58
63
|
});
|
59
64
|
// TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.
|
@@ -64,14 +69,16 @@ export class DelegationController extends BaseController {
|
|
64
69
|
/**
|
65
70
|
* Stores a delegation in storage.
|
66
71
|
*
|
67
|
-
* @param
|
68
|
-
* @param entry - The delegation entry to store.
|
72
|
+
* @param params - The parameters for storing the delegation.
|
73
|
+
* @param params.entry - The delegation entry to store.
|
69
74
|
*/
|
70
|
-
store(
|
75
|
+
store(params) {
|
76
|
+
const { entry } = params;
|
77
|
+
const hash = this.hashDelegation(entry.delegation);
|
71
78
|
// If the authority is not the root authority, validate that the
|
72
79
|
// parent entry does exist.
|
73
|
-
if (!
|
74
|
-
!this.state.delegations[entry.
|
80
|
+
if (!isHexEqual(entry.delegation.authority, ROOT_AUTHORITY) &&
|
81
|
+
!this.state.delegations[entry.delegation.authority]) {
|
75
82
|
throw new Error('Invalid authority');
|
76
83
|
}
|
77
84
|
this.update((state) => {
|
@@ -89,14 +96,15 @@ export class DelegationController extends BaseController {
|
|
89
96
|
const requester = account.address;
|
90
97
|
let list = Object.values(this.state.delegations);
|
91
98
|
if (filter?.from) {
|
92
|
-
list = list.filter((entry) =>
|
99
|
+
list = list.filter((entry) => isHexEqual(entry.delegation.delegator, filter.from));
|
93
100
|
}
|
94
101
|
if (!filter?.from ||
|
95
|
-
(filter?.from && !
|
96
|
-
list = list.filter((entry) =>
|
102
|
+
(filter?.from && !isHexEqual(filter.from, requester))) {
|
103
|
+
list = list.filter((entry) => isHexEqual(entry.delegation.delegate, requester));
|
97
104
|
}
|
98
|
-
|
99
|
-
|
105
|
+
const filterChainId = filter?.chainId;
|
106
|
+
if (filterChainId) {
|
107
|
+
list = list.filter((entry) => isHexEqual(entry.chainId, filterChainId));
|
100
108
|
}
|
101
109
|
const tags = filter?.tags;
|
102
110
|
if (tags && tags.length > 0) {
|
@@ -127,13 +135,13 @@ export class DelegationController extends BaseController {
|
|
127
135
|
return null;
|
128
136
|
}
|
129
137
|
chain.push(entry);
|
130
|
-
for (let _hash = entry.
|
138
|
+
for (let _hash = entry.delegation.authority; _hash !== ROOT_AUTHORITY;) {
|
131
139
|
const parent = this.retrieve(_hash);
|
132
140
|
if (!parent) {
|
133
141
|
throw new Error('Invalid delegation chain');
|
134
142
|
}
|
135
143
|
chain.push(parent);
|
136
|
-
_hash = parent.
|
144
|
+
_hash = parent.delegation.authority;
|
137
145
|
}
|
138
146
|
return chain;
|
139
147
|
}
|
@@ -152,20 +160,24 @@ export class DelegationController extends BaseController {
|
|
152
160
|
const entries = Object.entries(this.state.delegations);
|
153
161
|
let count = 0;
|
154
162
|
const nextHashes = [hash];
|
163
|
+
const deletedHashes = [];
|
155
164
|
while (nextHashes.length > 0) {
|
156
165
|
const currentHash = nextHashes.pop();
|
157
166
|
// Find all delegations that have this hash as their authority
|
158
|
-
const children = entries.filter(([_, v]) => v.
|
167
|
+
const children = entries.filter(([_, v]) => v.delegation.authority === currentHash);
|
159
168
|
// Add the hashes of all child delegations to be processed next
|
160
169
|
children.forEach(([k]) => {
|
161
170
|
nextHashes.push(k);
|
162
171
|
});
|
163
|
-
|
164
|
-
this.update((state) => {
|
165
|
-
delete state.delegations[currentHash];
|
166
|
-
});
|
172
|
+
deletedHashes.push(currentHash);
|
167
173
|
count += 1;
|
168
174
|
}
|
175
|
+
// Delete delegations
|
176
|
+
this.update((state) => {
|
177
|
+
deletedHashes.forEach((h) => {
|
178
|
+
delete state.delegations[h];
|
179
|
+
});
|
180
|
+
});
|
169
181
|
return count;
|
170
182
|
}
|
171
183
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"DelegationController.mjs","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EAAE,oBAAoB,EAAE,qCAAqC;AAEpE,OAAO,EAAE,cAAc,EAAE,wBAAoB;AA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AAI,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI,EAAE;YACT,OAAO,CAAC,CAAC;SACV;QAED,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QACvD,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,MAAM,UAAU,GAAU,CAAC,IAAI,CAAC,CAAC;QAEjC,OAAO,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;YAC5B,MAAM,WAAW,GAAG,UAAU,CAAC,GAAG,EAAS,CAAC;YAE5C,8DAA8D;YAC9D,MAAM,QAAQ,GAAG,OAAO,CAAC,MAAM,CAC7B,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,KAAK,WAAW,CAC7C,CAAC;YAEF,+DAA+D;YAC/D,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;gBACvB,UAAU,CAAC,IAAI,CAAC,CAAQ,CAAC,CAAC;YAC5B,CAAC,CAAC,CAAC;YAEH,gCAAgC;YAChC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,OAAO,KAAK,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;YACxC,CAAC,CAAC,CAAC;YACH,KAAK,IAAI,CAAC,CAAC;SACZ;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF","sourcesContent":["import type { StateMetadata } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { SignTypedDataVersion } from '@metamask/keyring-controller';\n\nimport { ROOT_AUTHORITY } from './constants';\nimport type {\n Address,\n Delegation,\n DelegationControllerMessenger,\n DelegationControllerState,\n DelegationEntry,\n DelegationFilter,\n Hex,\n} from './types';\nimport { createTypedMessageParams, isAddressEqual } from './utils';\n\nexport const controllerName = 'DelegationController';\n\nconst delegationControllerMetadata = {\n delegations: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<DelegationControllerState>;\n\n/**\n * Constructs the default {@link DelegationController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link DelegationController} state.\n */\nexport function getDefaultDelegationControllerState(): DelegationControllerState {\n return {\n delegations: {},\n };\n}\n\n/**\n * The {@link DelegationController} class.\n * This controller is meant to be a centralized place to store and sign delegations.\n */\nexport class DelegationController extends BaseController<\n typeof controllerName,\n DelegationControllerState,\n DelegationControllerMessenger\n> {\n constructor({\n messenger,\n state,\n }: {\n messenger: DelegationControllerMessenger;\n state?: Partial<DelegationControllerState>;\n }) {\n super({\n messenger,\n metadata: delegationControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultDelegationControllerState(),\n ...state,\n },\n });\n }\n\n /**\n * Signs a delegation.\n *\n * @param params - The parameters for signing the delegation.\n * @param params.delegation - The delegation to sign.\n * @param params.verifyingContract - The address of the verifying contract (DelegationManager).\n * @param params.chainId - The chainId of the chain to sign the delegation for.\n * @returns The signature of the delegation.\n */\n async sign(params: {\n delegation: Delegation;\n verifyingContract: Address;\n chainId: number;\n }) {\n const { delegation, verifyingContract, chainId } = params;\n\n const account = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n\n const data = createTypedMessageParams({\n chainId,\n from: account.address as Address,\n delegation,\n verifyingContract,\n });\n\n // TODO:: Replace with `SignatureController:newUnsignedTypedMessage`.\n // Waiting on confirmations team to implement this.\n const signature: string = await this.messagingSystem.call(\n 'KeyringController:signTypedMessage',\n data,\n SignTypedDataVersion.V4,\n );\n\n return signature;\n }\n\n /**\n * Stores a delegation in storage.\n *\n * @param hash - The hash of the delegation to store.\n * @param entry - The delegation entry to store.\n */\n store(hash: Hex, entry: DelegationEntry) {\n // If the authority is not the root authority, validate that the\n // parent entry does exist.\n if (\n !isAddressEqual(entry.data.authority, ROOT_AUTHORITY) &&\n !this.state.delegations[entry.data.authority]\n ) {\n throw new Error('Invalid authority');\n }\n this.update((state) => {\n state.delegations[hash] = entry;\n });\n }\n\n /**\n * Lists delegation entries.\n *\n * @param filter - The filter to use to list the delegation entries.\n * @returns A list of delegation entries that match the filter.\n */\n list(filter?: DelegationFilter) {\n const account = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n const requester = account.address as Address;\n\n let list: DelegationEntry[] = Object.values(this.state.delegations);\n\n if (filter?.from) {\n list = list.filter((entry) =>\n isAddressEqual(entry.data.delegator, filter.from as Address),\n );\n }\n\n if (\n !filter?.from ||\n (filter?.from && !isAddressEqual(filter.from, requester))\n ) {\n list = list.filter((entry) =>\n isAddressEqual(entry.data.delegate, requester),\n );\n }\n\n if (filter?.chainId) {\n list = list.filter((entry) => entry.chainId === filter.chainId);\n }\n\n const tags = filter?.tags;\n if (tags && tags.length > 0) {\n // Filter entries that contain all of the filter tags\n list = list.filter((entry) =>\n tags.every((tag) => entry.tags.includes(tag)),\n );\n }\n\n return list;\n }\n\n /**\n * Retrieves the delegation entry for a given delegation hash.\n *\n * @param hash - The hash of the delegation to retrieve.\n * @returns The delegation entry, or null if not found.\n */\n retrieve(hash: Hex) {\n return this.state.delegations[hash] ?? null;\n }\n\n /**\n * Retrieves a delegation chain from a delegation hash.\n *\n * @param hash - The hash of the delegation to retrieve.\n * @returns The delegation chain, or null if not found.\n */\n chain(hash: Hex) {\n const chain: DelegationEntry[] = [];\n\n const entry = this.retrieve(hash);\n if (!entry) {\n return null;\n }\n chain.push(entry);\n\n for (let _hash = entry.data.authority; _hash !== ROOT_AUTHORITY; ) {\n const parent = this.retrieve(_hash);\n if (!parent) {\n throw new Error('Invalid delegation chain');\n }\n chain.push(parent);\n _hash = parent.data.authority;\n }\n\n return chain;\n }\n\n /**\n * Deletes a delegation entrie from storage, along with any other entries\n * that are redelegated from it.\n *\n * @param hash - The hash of the delegation to delete.\n * @returns The number of entries deleted.\n */\n delete(hash: Hex): number {\n const root = this.retrieve(hash);\n if (!root) {\n return 0;\n }\n\n const entries = Object.entries(this.state.delegations);\n let count = 0;\n const nextHashes: Hex[] = [hash];\n\n while (nextHashes.length > 0) {\n const currentHash = nextHashes.pop() as Hex;\n\n // Find all delegations that have this hash as their authority\n const children = entries.filter(\n ([_, v]) => v.data.authority === currentHash,\n );\n\n // Add the hashes of all child delegations to be processed next\n children.forEach(([k]) => {\n nextHashes.push(k as Hex);\n });\n\n // Delete the current delegation\n this.update((state) => {\n delete state.delegations[currentHash];\n });\n count += 1;\n }\n\n return count;\n }\n}\n"]}
|
1
|
+
{"version":3,"file":"DelegationController.mjs","sourceRoot":"","sources":["../src/DelegationController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EAAE,oBAAoB,EAAE,qCAAqC;AACpE,OAAO,EAAE,WAAW,EAAE,wBAAwB;AAE9C,OAAO,EAAE,cAAc,EAAE,wBAAoB;AAW7C,OAAO,EAAE,wBAAwB,EAAE,UAAU,EAAE,oBAAgB;AAE/D,MAAM,CAAC,MAAM,cAAc,GAAG,sBAAsB,CAAC;AAErD,MAAM,4BAA4B,GAAG;IACnC,WAAW,EAAE;QACX,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACiD,CAAC;AAErD;;;;;;;GAOG;AACH,SAAS,mCAAmC;IAC1C,OAAO;QACL,WAAW,EAAE,EAAE;KAChB,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,oBAAqB,SAAQ,cAIzC;IAGC,YAAY,EACV,SAAS,EACT,KAAK,EACL,cAAc,GAKf;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;QACH,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;IACvC,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,cAAc,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AAC;YAE5C,8DAA8D;YAC9D,MAAM,QAAQ,GAAG,OAAO,CAAC,MAAM,CAC7B,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,SAAS,KAAK,WAAW,CACnD,CAAC;YAEF,+DAA+D;YAC/D,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;gBACvB,UAAU,CAAC,IAAI,CAAC,CAAQ,CAAC,CAAC;YAC5B,CAAC,CAAC,CAAC;YAEH,aAAa,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;YAChC,KAAK,IAAI,CAAC,CAAC;SACZ;QAED,qBAAqB;QACrB,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;gBAC1B,OAAO,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;YAC9B,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,KAAK,CAAC;IACf,CAAC;CACF","sourcesContent":["import type { StateMetadata } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { SignTypedDataVersion } from '@metamask/keyring-controller';\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 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 private readonly hashDelegation: (delegation: Delegation) => Hex;\n\n constructor({\n messenger,\n state,\n hashDelegation,\n }: {\n messenger: DelegationControllerMessenger;\n state?: Partial<DelegationControllerState>;\n hashDelegation: (delegation: Delegation) => Hex;\n }) {\n super({\n messenger,\n metadata: delegationControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultDelegationControllerState(),\n ...state,\n },\n });\n this.hashDelegation = hashDelegation;\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 * @param params.verifyingContract - The address of the verifying contract (DelegationManager).\n * @returns The signature of the delegation.\n */\n async signDelegation(params: {\n delegation: UnsignedDelegation;\n chainId: Hex;\n verifyingContract: Address;\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: hexToNumber(chainId),\n from: account.address as Address,\n delegation: {\n ...delegation,\n signature: '0x',\n },\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 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 let count = 0;\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 count += 1;\n }\n\n // Delete delegations\n this.update((state) => {\n deletedHashes.forEach((h) => {\n delete state.delegations[h];\n });\n });\n\n return count;\n }\n}\n"]}
|
package/dist/index.cjs
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.DelegationController = void 0;
|
4
4
|
var DelegationController_1 = require("./DelegationController.cjs");
|
5
5
|
Object.defineProperty(exports, "DelegationController", { enumerable: true, get: function () { return DelegationController_1.DelegationController; } });
|
6
|
-
Object.defineProperty(exports, "getDefaultDelegationControllerState", { enumerable: true, get: function () { return DelegationController_1.getDefaultDelegationControllerState; } });
|
7
6
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAcA,mEAA8D;AAArD,4HAAA,oBAAoB,OAAA","sourcesContent":["export type {\n DelegationControllerSignDelegationAction,\n DelegationControllerStoreAction,\n DelegationControllerListAction,\n DelegationControllerRetrieveAction,\n DelegationControllerChainAction,\n DelegationControllerDeleteAction,\n DelegationControllerActions,\n DelegationControllerEvents,\n DelegationControllerMessenger,\n DelegationEntry,\n DelegationFilter,\n} from './types';\n\nexport { DelegationController } from './DelegationController';\n"]}
|
package/dist/index.d.cts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
export type { DelegationControllerActions, DelegationControllerEvents, DelegationControllerMessenger, DelegationEntry, DelegationFilter, } from "./types.cjs";
|
2
|
-
export { DelegationController
|
1
|
+
export type { DelegationControllerSignDelegationAction, DelegationControllerStoreAction, DelegationControllerListAction, DelegationControllerRetrieveAction, DelegationControllerChainAction, DelegationControllerDeleteAction, DelegationControllerActions, DelegationControllerEvents, DelegationControllerMessenger, DelegationEntry, DelegationFilter, } from "./types.cjs";
|
2
|
+
export { DelegationController } from "./DelegationController.cjs";
|
3
3
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,2BAA2B,EAC3B,0BAA0B,EAC1B,6BAA6B,EAC7B,eAAe,EACf,gBAAgB,GACjB,oBAAgB;AAEjB,OAAO,
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,wCAAwC,EACxC,+BAA+B,EAC/B,8BAA8B,EAC9B,kCAAkC,EAClC,+BAA+B,EAC/B,gCAAgC,EAChC,2BAA2B,EAC3B,0BAA0B,EAC1B,6BAA6B,EAC7B,eAAe,EACf,gBAAgB,GACjB,oBAAgB;AAEjB,OAAO,EAAE,oBAAoB,EAAE,mCAA+B"}
|
package/dist/index.d.mts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
export type { DelegationControllerActions, DelegationControllerEvents, DelegationControllerMessenger, DelegationEntry, DelegationFilter, } from "./types.mjs";
|
2
|
-
export { DelegationController
|
1
|
+
export type { DelegationControllerSignDelegationAction, DelegationControllerStoreAction, DelegationControllerListAction, DelegationControllerRetrieveAction, DelegationControllerChainAction, DelegationControllerDeleteAction, DelegationControllerActions, DelegationControllerEvents, DelegationControllerMessenger, DelegationEntry, DelegationFilter, } from "./types.mjs";
|
2
|
+
export { DelegationController } from "./DelegationController.mjs";
|
3
3
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,2BAA2B,EAC3B,0BAA0B,EAC1B,6BAA6B,EAC7B,eAAe,EACf,gBAAgB,GACjB,oBAAgB;AAEjB,OAAO,
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,wCAAwC,EACxC,+BAA+B,EAC/B,8BAA8B,EAC9B,kCAAkC,EAClC,+BAA+B,EAC/B,gCAAgC,EAChC,2BAA2B,EAC3B,0BAA0B,EAC1B,6BAA6B,EAC7B,eAAe,EACf,gBAAgB,GACjB,oBAAgB;AAEjB,OAAO,EAAE,oBAAoB,EAAE,mCAA+B"}
|
package/dist/index.mjs
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
export { DelegationController
|
1
|
+
export { DelegationController } from "./DelegationController.mjs";
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,oBAAoB,EAAE,mCAA+B","sourcesContent":["export type {\n DelegationControllerSignDelegationAction,\n DelegationControllerStoreAction,\n DelegationControllerListAction,\n DelegationControllerRetrieveAction,\n DelegationControllerChainAction,\n DelegationControllerDeleteAction,\n DelegationControllerActions,\n DelegationControllerEvents,\n DelegationControllerMessenger,\n DelegationEntry,\n DelegationFilter,\n} from './types';\n\nexport { DelegationController } from './DelegationController';\n"]}
|
package/dist/types.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"types.cjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n} from '@metamask/base-controller';\nimport type { KeyringControllerSignTypedMessageAction } from '@metamask/keyring-controller';\n\nimport type {\n controllerName,\n DelegationController,\n} from './DelegationController';\n\ntype Hex = `0x${string}`;\ntype Address = `0x${string}`;\n\nexport type { Address, Hex };\n\nexport type Caveat = {\n enforcer: Hex;\n terms: Hex;\n args: Hex;\n};\n\nexport type Delegation = {\n delegate: Hex;\n delegator: Hex;\n authority: Hex;\n caveats: Caveat[];\n salt: Hex;\n signature: Hex;\n};\n\nexport type DelegationStruct = Omit<Delegation, 'salt'> & {\n salt: bigint;\n};\n\nexport type DelegationEntry = {\n tags: string[];\n chainId:
|
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\n/**\n * A delegation is a signed statement that gives a delegate permission to\n * act on behalf of a delegator. The permissions are defined by a set of caveats.\n * The caveats are a set of conditions that must be met in order for the delegation\n * to be valid.\n *\n * @see https://docs.gator.metamask.io/concepts/delegation\n */\nexport type Delegation = {\n /** The address of the delegate. */\n delegate: Hex;\n /** The address of the delegator. */\n delegator: Hex;\n /** The hash of the parent delegation, or the root authority if this is the root delegation. */\n authority: Hex;\n /** The terms of the delegation. */\n caveats: Caveat[];\n /** The salt used to generate the delegation signature. */\n salt: Hex;\n /** The signature of the delegation. */\n signature: Hex;\n};\n\n/** An unsigned delegation is a delegation without a signature. */\nexport type UnsignedDelegation = Omit<Delegation, 'signature'>;\n\nexport type DelegationStruct = Omit<Delegation, 'salt'> & {\n salt: bigint;\n};\n\nexport type DelegationEntry = {\n tags: string[];\n chainId: Hex;\n delegation: Delegation;\n meta?: string;\n};\n\nexport type DelegationFilter = {\n chainId?: Hex;\n tags?: string[];\n from?: Address;\n};\n\nexport type DelegationControllerState = {\n delegations: {\n [hash: Hex]: DelegationEntry;\n };\n};\n\nexport type DelegationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n DelegationControllerState\n>;\n\nexport type DelegationControllerSignDelegationAction = {\n type: `${typeof controllerName}:signDelegation`;\n handler: DelegationController['signDelegation'];\n};\n\nexport type DelegationControllerStoreAction = {\n type: `${typeof controllerName}:store`;\n handler: DelegationController['store'];\n};\n\nexport type DelegationControllerListAction = {\n type: `${typeof controllerName}:list`;\n handler: DelegationController['list'];\n};\n\nexport type DelegationControllerRetrieveAction = {\n type: `${typeof controllerName}:retrieve`;\n handler: DelegationController['retrieve'];\n};\n\nexport type DelegationControllerChainAction = {\n type: `${typeof controllerName}:chain`;\n handler: DelegationController['chain'];\n};\n\nexport type DelegationControllerDeleteAction = {\n type: `${typeof controllerName}:delete`;\n handler: DelegationController['delete'];\n};\n\nexport type DelegationControllerActions =\n | DelegationControllerGetStateAction\n | DelegationControllerSignDelegationAction\n | DelegationControllerStoreAction\n | DelegationControllerListAction\n | DelegationControllerRetrieveAction\n | DelegationControllerChainAction\n | DelegationControllerDeleteAction;\n\nexport type DelegationControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n DelegationControllerState\n>;\n\nexport type DelegationControllerEvents = DelegationControllerStateChangeEvent;\n\ntype AllowedActions =\n | KeyringControllerSignTypedMessageAction\n | AccountsControllerGetSelectedAccountAction;\n\ntype AllowedEvents = never;\n\nexport type DelegationControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n DelegationControllerActions | AllowedActions,\n DelegationControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n"]}
|
package/dist/types.d.cts
CHANGED
@@ -10,25 +10,41 @@ export type Caveat = {
|
|
10
10
|
terms: Hex;
|
11
11
|
args: Hex;
|
12
12
|
};
|
13
|
+
/**
|
14
|
+
* A delegation is a signed statement that gives a delegate permission to
|
15
|
+
* act on behalf of a delegator. The permissions are defined by a set of caveats.
|
16
|
+
* The caveats are a set of conditions that must be met in order for the delegation
|
17
|
+
* to be valid.
|
18
|
+
*
|
19
|
+
* @see https://docs.gator.metamask.io/concepts/delegation
|
20
|
+
*/
|
13
21
|
export type Delegation = {
|
22
|
+
/** The address of the delegate. */
|
14
23
|
delegate: Hex;
|
24
|
+
/** The address of the delegator. */
|
15
25
|
delegator: Hex;
|
26
|
+
/** The hash of the parent delegation, or the root authority if this is the root delegation. */
|
16
27
|
authority: Hex;
|
28
|
+
/** The terms of the delegation. */
|
17
29
|
caveats: Caveat[];
|
30
|
+
/** The salt used to generate the delegation signature. */
|
18
31
|
salt: Hex;
|
32
|
+
/** The signature of the delegation. */
|
19
33
|
signature: Hex;
|
20
34
|
};
|
35
|
+
/** An unsigned delegation is a delegation without a signature. */
|
36
|
+
export type UnsignedDelegation = Omit<Delegation, 'signature'>;
|
21
37
|
export type DelegationStruct = Omit<Delegation, 'salt'> & {
|
22
38
|
salt: bigint;
|
23
39
|
};
|
24
40
|
export type DelegationEntry = {
|
25
41
|
tags: string[];
|
26
|
-
chainId:
|
27
|
-
|
42
|
+
chainId: Hex;
|
43
|
+
delegation: Delegation;
|
28
44
|
meta?: string;
|
29
45
|
};
|
30
46
|
export type DelegationFilter = {
|
31
|
-
chainId?:
|
47
|
+
chainId?: Hex;
|
32
48
|
tags?: string[];
|
33
49
|
from?: Address;
|
34
50
|
};
|
@@ -38,15 +54,31 @@ export type DelegationControllerState = {
|
|
38
54
|
};
|
39
55
|
};
|
40
56
|
export type DelegationControllerGetStateAction = ControllerGetStateAction<typeof controllerName, DelegationControllerState>;
|
57
|
+
export type DelegationControllerSignDelegationAction = {
|
58
|
+
type: `${typeof controllerName}:signDelegation`;
|
59
|
+
handler: DelegationController['signDelegation'];
|
60
|
+
};
|
41
61
|
export type DelegationControllerStoreAction = {
|
42
62
|
type: `${typeof controllerName}:store`;
|
43
63
|
handler: DelegationController['store'];
|
44
64
|
};
|
45
|
-
export type
|
46
|
-
type: `${typeof controllerName}:
|
47
|
-
handler: DelegationController['
|
65
|
+
export type DelegationControllerListAction = {
|
66
|
+
type: `${typeof controllerName}:list`;
|
67
|
+
handler: DelegationController['list'];
|
68
|
+
};
|
69
|
+
export type DelegationControllerRetrieveAction = {
|
70
|
+
type: `${typeof controllerName}:retrieve`;
|
71
|
+
handler: DelegationController['retrieve'];
|
72
|
+
};
|
73
|
+
export type DelegationControllerChainAction = {
|
74
|
+
type: `${typeof controllerName}:chain`;
|
75
|
+
handler: DelegationController['chain'];
|
76
|
+
};
|
77
|
+
export type DelegationControllerDeleteAction = {
|
78
|
+
type: `${typeof controllerName}:delete`;
|
79
|
+
handler: DelegationController['delete'];
|
48
80
|
};
|
49
|
-
export type DelegationControllerActions = DelegationControllerGetStateAction | DelegationControllerStoreAction |
|
81
|
+
export type DelegationControllerActions = DelegationControllerGetStateAction | DelegationControllerSignDelegationAction | DelegationControllerStoreAction | DelegationControllerListAction | DelegationControllerRetrieveAction | DelegationControllerChainAction | DelegationControllerDeleteAction;
|
50
82
|
export type DelegationControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, DelegationControllerState>;
|
51
83
|
export type DelegationControllerEvents = DelegationControllerStateChangeEvent;
|
52
84
|
type AllowedActions = KeyringControllerSignTypedMessageAction | AccountsControllerGetSelectedAccountAction;
|
package/dist/types.d.cts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0CAA0C,EAAE,sCAAsC;AAChG,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EACpB,kCAAkC;AACnC,OAAO,KAAK,EAAE,uCAAuC,EAAE,qCAAqC;AAE5F,OAAO,KAAK,EACV,cAAc,EACd,oBAAoB,EACrB,mCAA+B;AAEhC,KAAK,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AACzB,KAAK,OAAO,GAAG,KAAK,MAAM,EAAE,CAAC;AAE7B,YAAY,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC;AAE7B,MAAM,MAAM,MAAM,GAAG;IACnB,QAAQ,EAAE,GAAG,CAAC;IACd,KAAK,EAAE,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,
|
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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AM,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.d.mts
CHANGED
@@ -10,25 +10,41 @@ export type Caveat = {
|
|
10
10
|
terms: Hex;
|
11
11
|
args: Hex;
|
12
12
|
};
|
13
|
+
/**
|
14
|
+
* A delegation is a signed statement that gives a delegate permission to
|
15
|
+
* act on behalf of a delegator. The permissions are defined by a set of caveats.
|
16
|
+
* The caveats are a set of conditions that must be met in order for the delegation
|
17
|
+
* to be valid.
|
18
|
+
*
|
19
|
+
* @see https://docs.gator.metamask.io/concepts/delegation
|
20
|
+
*/
|
13
21
|
export type Delegation = {
|
22
|
+
/** The address of the delegate. */
|
14
23
|
delegate: Hex;
|
24
|
+
/** The address of the delegator. */
|
15
25
|
delegator: Hex;
|
26
|
+
/** The hash of the parent delegation, or the root authority if this is the root delegation. */
|
16
27
|
authority: Hex;
|
28
|
+
/** The terms of the delegation. */
|
17
29
|
caveats: Caveat[];
|
30
|
+
/** The salt used to generate the delegation signature. */
|
18
31
|
salt: Hex;
|
32
|
+
/** The signature of the delegation. */
|
19
33
|
signature: Hex;
|
20
34
|
};
|
35
|
+
/** An unsigned delegation is a delegation without a signature. */
|
36
|
+
export type UnsignedDelegation = Omit<Delegation, 'signature'>;
|
21
37
|
export type DelegationStruct = Omit<Delegation, 'salt'> & {
|
22
38
|
salt: bigint;
|
23
39
|
};
|
24
40
|
export type DelegationEntry = {
|
25
41
|
tags: string[];
|
26
|
-
chainId:
|
27
|
-
|
42
|
+
chainId: Hex;
|
43
|
+
delegation: Delegation;
|
28
44
|
meta?: string;
|
29
45
|
};
|
30
46
|
export type DelegationFilter = {
|
31
|
-
chainId?:
|
47
|
+
chainId?: Hex;
|
32
48
|
tags?: string[];
|
33
49
|
from?: Address;
|
34
50
|
};
|
@@ -38,15 +54,31 @@ export type DelegationControllerState = {
|
|
38
54
|
};
|
39
55
|
};
|
40
56
|
export type DelegationControllerGetStateAction = ControllerGetStateAction<typeof controllerName, DelegationControllerState>;
|
57
|
+
export type DelegationControllerSignDelegationAction = {
|
58
|
+
type: `${typeof controllerName}:signDelegation`;
|
59
|
+
handler: DelegationController['signDelegation'];
|
60
|
+
};
|
41
61
|
export type DelegationControllerStoreAction = {
|
42
62
|
type: `${typeof controllerName}:store`;
|
43
63
|
handler: DelegationController['store'];
|
44
64
|
};
|
45
|
-
export type
|
46
|
-
type: `${typeof controllerName}:
|
47
|
-
handler: DelegationController['
|
65
|
+
export type DelegationControllerListAction = {
|
66
|
+
type: `${typeof controllerName}:list`;
|
67
|
+
handler: DelegationController['list'];
|
68
|
+
};
|
69
|
+
export type DelegationControllerRetrieveAction = {
|
70
|
+
type: `${typeof controllerName}:retrieve`;
|
71
|
+
handler: DelegationController['retrieve'];
|
72
|
+
};
|
73
|
+
export type DelegationControllerChainAction = {
|
74
|
+
type: `${typeof controllerName}:chain`;
|
75
|
+
handler: DelegationController['chain'];
|
76
|
+
};
|
77
|
+
export type DelegationControllerDeleteAction = {
|
78
|
+
type: `${typeof controllerName}:delete`;
|
79
|
+
handler: DelegationController['delete'];
|
48
80
|
};
|
49
|
-
export type DelegationControllerActions = DelegationControllerGetStateAction | DelegationControllerStoreAction |
|
81
|
+
export type DelegationControllerActions = DelegationControllerGetStateAction | DelegationControllerSignDelegationAction | DelegationControllerStoreAction | DelegationControllerListAction | DelegationControllerRetrieveAction | DelegationControllerChainAction | DelegationControllerDeleteAction;
|
50
82
|
export type DelegationControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, DelegationControllerState>;
|
51
83
|
export type DelegationControllerEvents = DelegationControllerStateChangeEvent;
|
52
84
|
type AllowedActions = KeyringControllerSignTypedMessageAction | AccountsControllerGetSelectedAccountAction;
|
package/dist/types.d.mts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0CAA0C,EAAE,sCAAsC;AAChG,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EACpB,kCAAkC;AACnC,OAAO,KAAK,EAAE,uCAAuC,EAAE,qCAAqC;AAE5F,OAAO,KAAK,EACV,cAAc,EACd,oBAAoB,EACrB,mCAA+B;AAEhC,KAAK,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AACzB,KAAK,OAAO,GAAG,KAAK,MAAM,EAAE,CAAC;AAE7B,YAAY,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC;AAE7B,MAAM,MAAM,MAAM,GAAG;IACnB,QAAQ,EAAE,GAAG,CAAC;IACd,KAAK,EAAE,GAAG,CAAC;IACX,IAAI,EAAE,GAAG,CAAC;CACX,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,EAAE,GAAG,CAAC;IACd,SAAS,EAAE,GAAG,CAAC;IACf,SAAS,EAAE,GAAG,CAAC;IACf,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,IAAI,EAAE,GAAG,CAAC;IACV,SAAS,EAAE,GAAG,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,GAAG;IACxD,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,OAAO,EAAE,
|
1
|
+
{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,EAAE,oBAAoB,CAAC,QAAQ,CAAC,CAAC;CACzC,CAAC;AAEF,MAAM,MAAM,2BAA2B,GACnC,kCAAkC,GAClC,wCAAwC,GACxC,+BAA+B,GAC/B,8BAA8B,GAC9B,kCAAkC,GAClC,+BAA+B,GAC/B,gCAAgC,CAAC;AAErC,MAAM,MAAM,oCAAoC,GAAG,0BAA0B,CAC3E,OAAO,cAAc,EACrB,yBAAyB,CAC1B,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG,oCAAoC,CAAC;AAE9E,KAAK,cAAc,GACf,uCAAuC,GACvC,0CAA0C,CAAC;AAE/C,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B,MAAM,MAAM,6BAA6B,GAAG,mBAAmB,CAC7D,OAAO,cAAc,EACrB,2BAA2B,GAAG,cAAc,EAC5C,0BAA0B,GAAG,aAAa,EAC1C,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC"}
|
package/dist/types.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n} from '@metamask/base-controller';\nimport type { KeyringControllerSignTypedMessageAction } from '@metamask/keyring-controller';\n\nimport type {\n controllerName,\n DelegationController,\n} from './DelegationController';\n\ntype Hex = `0x${string}`;\ntype Address = `0x${string}`;\n\nexport type { Address, Hex };\n\nexport type Caveat = {\n enforcer: Hex;\n terms: Hex;\n args: Hex;\n};\n\nexport type Delegation = {\n delegate: Hex;\n delegator: Hex;\n authority: Hex;\n caveats: Caveat[];\n salt: Hex;\n signature: Hex;\n};\n\nexport type DelegationStruct = Omit<Delegation, 'salt'> & {\n salt: bigint;\n};\n\nexport type DelegationEntry = {\n tags: string[];\n chainId:
|
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\n/**\n * A delegation is a signed statement that gives a delegate permission to\n * act on behalf of a delegator. The permissions are defined by a set of caveats.\n * The caveats are a set of conditions that must be met in order for the delegation\n * to be valid.\n *\n * @see https://docs.gator.metamask.io/concepts/delegation\n */\nexport type Delegation = {\n /** The address of the delegate. */\n delegate: Hex;\n /** The address of the delegator. */\n delegator: Hex;\n /** The hash of the parent delegation, or the root authority if this is the root delegation. */\n authority: Hex;\n /** The terms of the delegation. */\n caveats: Caveat[];\n /** The salt used to generate the delegation signature. */\n salt: Hex;\n /** The signature of the delegation. */\n signature: Hex;\n};\n\n/** An unsigned delegation is a delegation without a signature. */\nexport type UnsignedDelegation = Omit<Delegation, 'signature'>;\n\nexport type DelegationStruct = Omit<Delegation, 'salt'> & {\n salt: bigint;\n};\n\nexport type DelegationEntry = {\n tags: string[];\n chainId: Hex;\n delegation: Delegation;\n meta?: string;\n};\n\nexport type DelegationFilter = {\n chainId?: Hex;\n tags?: string[];\n from?: Address;\n};\n\nexport type DelegationControllerState = {\n delegations: {\n [hash: Hex]: DelegationEntry;\n };\n};\n\nexport type DelegationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n DelegationControllerState\n>;\n\nexport type DelegationControllerSignDelegationAction = {\n type: `${typeof controllerName}:signDelegation`;\n handler: DelegationController['signDelegation'];\n};\n\nexport type DelegationControllerStoreAction = {\n type: `${typeof controllerName}:store`;\n handler: DelegationController['store'];\n};\n\nexport type DelegationControllerListAction = {\n type: `${typeof controllerName}:list`;\n handler: DelegationController['list'];\n};\n\nexport type DelegationControllerRetrieveAction = {\n type: `${typeof controllerName}:retrieve`;\n handler: DelegationController['retrieve'];\n};\n\nexport type DelegationControllerChainAction = {\n type: `${typeof controllerName}:chain`;\n handler: DelegationController['chain'];\n};\n\nexport type DelegationControllerDeleteAction = {\n type: `${typeof controllerName}:delete`;\n handler: DelegationController['delete'];\n};\n\nexport type DelegationControllerActions =\n | DelegationControllerGetStateAction\n | DelegationControllerSignDelegationAction\n | DelegationControllerStoreAction\n | DelegationControllerListAction\n | DelegationControllerRetrieveAction\n | DelegationControllerChainAction\n | DelegationControllerDeleteAction;\n\nexport type DelegationControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n DelegationControllerState\n>;\n\nexport type DelegationControllerEvents = DelegationControllerStateChangeEvent;\n\ntype AllowedActions =\n | KeyringControllerSignTypedMessageAction\n | AccountsControllerGetSelectedAccountAction;\n\ntype AllowedEvents = never;\n\nexport type DelegationControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n DelegationControllerActions | AllowedActions,\n DelegationControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n"]}
|
package/dist/utils.cjs
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.createTypedMessageParams = exports.toDelegationStruct = exports.
|
3
|
+
exports.createTypedMessageParams = exports.toDelegationStruct = exports.isHexEqual = void 0;
|
4
4
|
const utils_1 = require("@metamask/utils");
|
5
5
|
const constants_1 = require("./constants.cjs");
|
6
6
|
/**
|
7
|
-
* Checks if two
|
7
|
+
* Checks if two hex strings are equal.
|
8
8
|
*
|
9
|
-
* @param a - The first
|
10
|
-
* @param b - The second
|
11
|
-
* @returns True if the
|
9
|
+
* @param a - The first hex string.
|
10
|
+
* @param b - The second hex string.
|
11
|
+
* @returns True if the hex strings are equal, false otherwise.
|
12
12
|
*/
|
13
|
-
function
|
13
|
+
function isHexEqual(a, b) {
|
14
14
|
return a.toLowerCase() === b.toLowerCase();
|
15
15
|
}
|
16
|
-
exports.
|
16
|
+
exports.isHexEqual = isHexEqual;
|
17
17
|
/**
|
18
18
|
* Converts a Delegation to a DelegationStruct.
|
19
19
|
* The DelegationStruct is the format used in the Delegation Framework.
|
package/dist/utils.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AACA,2CAAqD;AAErD,+CAA6D;AAG7D;;;;;;GAMG;AACH,SAAgB,
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AACA,2CAAqD;AAErD,+CAA6D;AAG7D;;;;;;GAMG;AACH,SAAgB,UAAU,CAAC,CAAM,EAAE,CAAM;IACvC,OAAO,CAAC,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;AAC7C,CAAC;AAFD,gCAEC;AASD;;;;;;GAMG;AACI,MAAM,kBAAkB,GAAG,CAChC,UAAsB,EACJ,EAAE;IACpB,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAClD,QAAQ,EAAE,IAAA,0BAAkB,EAAC,MAAM,CAAC,QAAQ,CAAC;QAC7C,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,IAAI,EAAE,MAAM,CAAC,IAAI;KAClB,CAAC,CAAC,CAAC;IAEJ,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAErE,OAAO;QACL,QAAQ,EAAE,IAAA,0BAAkB,EAAC,UAAU,CAAC,QAAQ,CAAC;QACjD,SAAS,EAAE,IAAA,0BAAkB,EAAC,UAAU,CAAC,SAAS,CAAC;QACnD,SAAS,EAAE,UAAU,CAAC,SAAS;QAC/B,OAAO;QACP,IAAI;QACJ,SAAS,EAAE,UAAU,CAAC,SAAS;KAChC,CAAC;AACJ,CAAC,CAAC;AAnBW,QAAA,kBAAkB,sBAmB7B;AAEF;;;;GAIG;AACH,SAAgB,wBAAwB,CACtC,IAAqC;IAErC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC;IAE9D,MAAM,IAAI,GAAuB;QAC/B,IAAI,EAAE;YACJ,KAAK,EAAE,0CAA8B;YACrC,WAAW,EAAE,YAAY;YACzB,MAAM,EAAE;gBACN,OAAO;gBACP,IAAI,EAAE,mBAAmB;gBACzB,OAAO,EAAE,GAAG;gBACZ,iBAAiB;aAClB;YACD,OAAO,EAAE,IAAA,0BAAkB,EAAC,UAAU,CAAC;SACxC;QACD,IAAI;KACL,CAAC;IAEF,OAAO,IAAI,CAAC;AACd,CAAC;AArBD,4DAqBC","sourcesContent":["import type { TypedMessageParams } from '@metamask/keyring-controller';\nimport { getChecksumAddress } from '@metamask/utils';\n\nimport { SIGNABLE_DELEGATION_TYPED_DATA } from './constants';\nimport type { Address, Delegation, DelegationStruct, Hex } from './types';\n\n/**\n * Checks if two hex strings are equal.\n *\n * @param a - The first hex string.\n * @param b - The second hex string.\n * @returns True if the hex strings are equal, false otherwise.\n */\nexport function isHexEqual(a: Hex, b: Hex) {\n return a.toLowerCase() === b.toLowerCase();\n}\n\ntype CreateTypedMessageParamsOptions = {\n chainId: number;\n from: Address;\n delegation: Delegation;\n verifyingContract: Address;\n};\n\n/**\n * Converts a Delegation to a DelegationStruct.\n * The DelegationStruct is the format used in the Delegation Framework.\n *\n * @param delegation the delegation to format\n * @returns the formatted delegation\n */\nexport const toDelegationStruct = (\n delegation: Delegation,\n): DelegationStruct => {\n const caveats = delegation.caveats.map((caveat) => ({\n enforcer: getChecksumAddress(caveat.enforcer),\n terms: caveat.terms,\n args: caveat.args,\n }));\n\n const salt = delegation.salt === '0x' ? 0n : BigInt(delegation.salt);\n\n return {\n delegate: getChecksumAddress(delegation.delegate),\n delegator: getChecksumAddress(delegation.delegator),\n authority: delegation.authority,\n caveats,\n salt,\n signature: delegation.signature,\n };\n};\n\n/**\n *\n * @param opts - The options for creating typed message params.\n * @returns The typed message params.\n */\nexport function createTypedMessageParams(\n opts: CreateTypedMessageParamsOptions,\n): TypedMessageParams {\n const { chainId, from, delegation, verifyingContract } = opts;\n\n const data: TypedMessageParams = {\n data: {\n types: SIGNABLE_DELEGATION_TYPED_DATA,\n primaryType: 'Delegation',\n domain: {\n chainId,\n name: 'DelegationManager',\n version: '1',\n verifyingContract,\n },\n message: toDelegationStruct(delegation),\n },\n from,\n };\n\n return data;\n}\n"]}
|
package/dist/utils.d.cts
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
import type { TypedMessageParams } from "@metamask/keyring-controller";
|
2
|
-
import type { Address, Delegation, DelegationStruct } from "./types.cjs";
|
2
|
+
import type { Address, Delegation, DelegationStruct, Hex } from "./types.cjs";
|
3
3
|
/**
|
4
|
-
* Checks if two
|
4
|
+
* Checks if two hex strings are equal.
|
5
5
|
*
|
6
|
-
* @param a - The first
|
7
|
-
* @param b - The second
|
8
|
-
* @returns True if the
|
6
|
+
* @param a - The first hex string.
|
7
|
+
* @param b - The second hex string.
|
8
|
+
* @returns True if the hex strings are equal, false otherwise.
|
9
9
|
*/
|
10
|
-
export declare function
|
10
|
+
export declare function isHexEqual(a: Hex, b: Hex): boolean;
|
11
11
|
type CreateTypedMessageParamsOptions = {
|
12
12
|
chainId: number;
|
13
13
|
from: Address;
|
package/dist/utils.d.cts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,qCAAqC;AAIvE,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,oBAAgB;
|
1
|
+
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,qCAAqC;AAIvE,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,GAAG,EAAE,oBAAgB;AAE1E;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,WAExC;AAED,KAAK,+BAA+B,GAAG;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,OAAO,CAAC;IACd,UAAU,EAAE,UAAU,CAAC;IACvB,iBAAiB,EAAE,OAAO,CAAC;CAC5B,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,eACjB,UAAU,KACrB,gBAiBF,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,+BAA+B,GACpC,kBAAkB,CAmBpB"}
|
package/dist/utils.d.mts
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
import type { TypedMessageParams } from "@metamask/keyring-controller";
|
2
|
-
import type { Address, Delegation, DelegationStruct } from "./types.mjs";
|
2
|
+
import type { Address, Delegation, DelegationStruct, Hex } from "./types.mjs";
|
3
3
|
/**
|
4
|
-
* Checks if two
|
4
|
+
* Checks if two hex strings are equal.
|
5
5
|
*
|
6
|
-
* @param a - The first
|
7
|
-
* @param b - The second
|
8
|
-
* @returns True if the
|
6
|
+
* @param a - The first hex string.
|
7
|
+
* @param b - The second hex string.
|
8
|
+
* @returns True if the hex strings are equal, false otherwise.
|
9
9
|
*/
|
10
|
-
export declare function
|
10
|
+
export declare function isHexEqual(a: Hex, b: Hex): boolean;
|
11
11
|
type CreateTypedMessageParamsOptions = {
|
12
12
|
chainId: number;
|
13
13
|
from: Address;
|
package/dist/utils.d.mts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,qCAAqC;AAIvE,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,oBAAgB;
|
1
|
+
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,qCAAqC;AAIvE,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,GAAG,EAAE,oBAAgB;AAE1E;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,WAExC;AAED,KAAK,+BAA+B,GAAG;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,OAAO,CAAC;IACd,UAAU,EAAE,UAAU,CAAC;IACvB,iBAAiB,EAAE,OAAO,CAAC;CAC5B,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,eACjB,UAAU,KACrB,gBAiBF,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,+BAA+B,GACpC,kBAAkB,CAmBpB"}
|
package/dist/utils.mjs
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
import { getChecksumAddress } from "@metamask/utils";
|
2
2
|
import { SIGNABLE_DELEGATION_TYPED_DATA } from "./constants.mjs";
|
3
3
|
/**
|
4
|
-
* Checks if two
|
4
|
+
* Checks if two hex strings are equal.
|
5
5
|
*
|
6
|
-
* @param a - The first
|
7
|
-
* @param b - The second
|
8
|
-
* @returns True if the
|
6
|
+
* @param a - The first hex string.
|
7
|
+
* @param b - The second hex string.
|
8
|
+
* @returns True if the hex strings are equal, false otherwise.
|
9
9
|
*/
|
10
|
-
export function
|
10
|
+
export function isHexEqual(a, b) {
|
11
11
|
return a.toLowerCase() === b.toLowerCase();
|
12
12
|
}
|
13
13
|
/**
|
package/dist/utils.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AAErD,OAAO,EAAE,8BAA8B,EAAE,wBAAoB;AAG7D;;;;;;GAMG;AACH,MAAM,UAAU,
|
1
|
+
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AAErD,OAAO,EAAE,8BAA8B,EAAE,wBAAoB;AAG7D;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,CAAM,EAAE,CAAM;IACvC,OAAO,CAAC,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;AAC7C,CAAC;AASD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,UAAsB,EACJ,EAAE;IACpB,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAClD,QAAQ,EAAE,kBAAkB,CAAC,MAAM,CAAC,QAAQ,CAAC;QAC7C,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,IAAI,EAAE,MAAM,CAAC,IAAI;KAClB,CAAC,CAAC,CAAC;IAEJ,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAErE,OAAO;QACL,QAAQ,EAAE,kBAAkB,CAAC,UAAU,CAAC,QAAQ,CAAC;QACjD,SAAS,EAAE,kBAAkB,CAAC,UAAU,CAAC,SAAS,CAAC;QACnD,SAAS,EAAE,UAAU,CAAC,SAAS;QAC/B,OAAO;QACP,IAAI;QACJ,SAAS,EAAE,UAAU,CAAC,SAAS;KAChC,CAAC;AACJ,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CACtC,IAAqC;IAErC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC;IAE9D,MAAM,IAAI,GAAuB;QAC/B,IAAI,EAAE;YACJ,KAAK,EAAE,8BAA8B;YACrC,WAAW,EAAE,YAAY;YACzB,MAAM,EAAE;gBACN,OAAO;gBACP,IAAI,EAAE,mBAAmB;gBACzB,OAAO,EAAE,GAAG;gBACZ,iBAAiB;aAClB;YACD,OAAO,EAAE,kBAAkB,CAAC,UAAU,CAAC;SACxC;QACD,IAAI;KACL,CAAC;IAEF,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import type { TypedMessageParams } from '@metamask/keyring-controller';\nimport { getChecksumAddress } from '@metamask/utils';\n\nimport { SIGNABLE_DELEGATION_TYPED_DATA } from './constants';\nimport type { Address, Delegation, DelegationStruct, Hex } from './types';\n\n/**\n * Checks if two hex strings are equal.\n *\n * @param a - The first hex string.\n * @param b - The second hex string.\n * @returns True if the hex strings are equal, false otherwise.\n */\nexport function isHexEqual(a: Hex, b: Hex) {\n return a.toLowerCase() === b.toLowerCase();\n}\n\ntype CreateTypedMessageParamsOptions = {\n chainId: number;\n from: Address;\n delegation: Delegation;\n verifyingContract: Address;\n};\n\n/**\n * Converts a Delegation to a DelegationStruct.\n * The DelegationStruct is the format used in the Delegation Framework.\n *\n * @param delegation the delegation to format\n * @returns the formatted delegation\n */\nexport const toDelegationStruct = (\n delegation: Delegation,\n): DelegationStruct => {\n const caveats = delegation.caveats.map((caveat) => ({\n enforcer: getChecksumAddress(caveat.enforcer),\n terms: caveat.terms,\n args: caveat.args,\n }));\n\n const salt = delegation.salt === '0x' ? 0n : BigInt(delegation.salt);\n\n return {\n delegate: getChecksumAddress(delegation.delegate),\n delegator: getChecksumAddress(delegation.delegator),\n authority: delegation.authority,\n caveats,\n salt,\n signature: delegation.signature,\n };\n};\n\n/**\n *\n * @param opts - The options for creating typed message params.\n * @returns The typed message params.\n */\nexport function createTypedMessageParams(\n opts: CreateTypedMessageParamsOptions,\n): TypedMessageParams {\n const { chainId, from, delegation, verifyingContract } = opts;\n\n const data: TypedMessageParams = {\n data: {\n types: SIGNABLE_DELEGATION_TYPED_DATA,\n primaryType: 'Delegation',\n domain: {\n chainId,\n name: 'DelegationManager',\n version: '1',\n verifyingContract,\n },\n message: toDelegationStruct(delegation),\n },\n from,\n };\n\n return data;\n}\n"]}
|