@metamask-previews/chain-agnostic-permission 0.1.0-preview-b69c669
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 +17 -0
- package/LICENSE +20 -0
- package/README.md +15 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.cjs +103 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.cjs.map +1 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.d.cts +20 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.d.cts.map +1 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.d.mts +20 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.d.mts.map +1 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.mjs +98 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.mjs.map +1 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs +115 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs.map +1 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts +27 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts.map +1 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts +27 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts.map +1 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs +109 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs.map +1 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.cjs +85 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.cjs.map +1 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.d.cts +23 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.d.cts.map +1 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.d.mts +23 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.d.mts.map +1 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.mjs +80 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.mjs.map +1 -0
- package/dist/caip25Permission.cjs +285 -0
- package/dist/caip25Permission.cjs.map +1 -0
- package/dist/caip25Permission.d.cts +131 -0
- package/dist/caip25Permission.d.cts.map +1 -0
- package/dist/caip25Permission.d.mts +131 -0
- package/dist/caip25Permission.d.mts.map +1 -0
- package/dist/caip25Permission.mjs +280 -0
- package/dist/caip25Permission.mjs.map +1 -0
- package/dist/index.cjs +43 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +14 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +14 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +11 -0
- package/dist/index.mjs.map +1 -0
- package/dist/scope/assert.cjs +170 -0
- package/dist/scope/assert.cjs.map +1 -0
- package/dist/scope/assert.d.cts +50 -0
- package/dist/scope/assert.d.cts.map +1 -0
- package/dist/scope/assert.d.mts +50 -0
- package/dist/scope/assert.d.mts.map +1 -0
- package/dist/scope/assert.mjs +162 -0
- package/dist/scope/assert.mjs.map +1 -0
- package/dist/scope/authorization.cjs +51 -0
- package/dist/scope/authorization.cjs.map +1 -0
- package/dist/scope/authorization.d.cts +50 -0
- package/dist/scope/authorization.d.cts.map +1 -0
- package/dist/scope/authorization.d.mts +50 -0
- package/dist/scope/authorization.d.mts.map +1 -0
- package/dist/scope/authorization.mjs +46 -0
- package/dist/scope/authorization.mjs.map +1 -0
- package/dist/scope/constants.cjs +83 -0
- package/dist/scope/constants.cjs.map +1 -0
- package/dist/scope/constants.d.cts +32 -0
- package/dist/scope/constants.d.cts.map +1 -0
- package/dist/scope/constants.d.mts +32 -0
- package/dist/scope/constants.d.mts.map +1 -0
- package/dist/scope/constants.mjs +84 -0
- package/dist/scope/constants.mjs.map +1 -0
- package/dist/scope/errors.cjs +47 -0
- package/dist/scope/errors.cjs.map +1 -0
- package/dist/scope/errors.d.cts +44 -0
- package/dist/scope/errors.d.cts.map +1 -0
- package/dist/scope/errors.d.mts +44 -0
- package/dist/scope/errors.d.mts.map +1 -0
- package/dist/scope/errors.mjs +44 -0
- package/dist/scope/errors.mjs.map +1 -0
- package/dist/scope/filter.cjs +76 -0
- package/dist/scope/filter.cjs.map +1 -0
- package/dist/scope/filter.d.cts +35 -0
- package/dist/scope/filter.d.cts.map +1 -0
- package/dist/scope/filter.d.mts +35 -0
- package/dist/scope/filter.d.mts.map +1 -0
- package/dist/scope/filter.mjs +71 -0
- package/dist/scope/filter.mjs.map +1 -0
- package/dist/scope/supported.cjs +116 -0
- package/dist/scope/supported.cjs.map +1 -0
- package/dist/scope/supported.d.cts +52 -0
- package/dist/scope/supported.d.cts.map +1 -0
- package/dist/scope/supported.d.mts +52 -0
- package/dist/scope/supported.d.mts.map +1 -0
- package/dist/scope/supported.mjs +109 -0
- package/dist/scope/supported.mjs.map +1 -0
- package/dist/scope/transform.cjs +152 -0
- package/dist/scope/transform.cjs.map +1 -0
- package/dist/scope/transform.d.cts +50 -0
- package/dist/scope/transform.d.cts.map +1 -0
- package/dist/scope/transform.d.mts +50 -0
- package/dist/scope/transform.d.mts.map +1 -0
- package/dist/scope/transform.mjs +144 -0
- package/dist/scope/transform.mjs.map +1 -0
- package/dist/scope/types.cjs +23 -0
- package/dist/scope/types.cjs.map +1 -0
- package/dist/scope/types.d.cts +81 -0
- package/dist/scope/types.d.cts.map +1 -0
- package/dist/scope/types.d.mts +81 -0
- package/dist/scope/types.d.mts.map +1 -0
- package/dist/scope/types.mjs +19 -0
- package/dist/scope/types.mjs.map +1 -0
- package/dist/scope/validation.cjs +87 -0
- package/dist/scope/validation.cjs.map +1 -0
- package/dist/scope/validation.d.cts +21 -0
- package/dist/scope/validation.d.cts.map +1 -0
- package/dist/scope/validation.d.mts +21 -0
- package/dist/scope/validation.d.mts.map +1 -0
- package/dist/scope/validation.mjs +82 -0
- package/dist/scope/validation.mjs.map +1 -0
- package/package.json +75 -0
@@ -0,0 +1,280 @@
|
|
1
|
+
import { CaveatMutatorOperation, PermissionType } from "@metamask/permission-controller";
|
2
|
+
import { hasProperty, KnownCaipNamespace, parseCaipAccountId } from "@metamask/utils";
|
3
|
+
import $lodash from "lodash";
|
4
|
+
const { cloneDeep, isEqual } = $lodash;
|
5
|
+
import { assertIsInternalScopesObject } from "./scope/assert.mjs";
|
6
|
+
import { isSupportedAccount, isSupportedScopeString } from "./scope/supported.mjs";
|
7
|
+
import { mergeInternalScopes } from "./scope/transform.mjs";
|
8
|
+
import { parseScopeString } from "./scope/types.mjs";
|
9
|
+
/**
|
10
|
+
* The name of the CAIP-25 permission caveat.
|
11
|
+
*/
|
12
|
+
export const Caip25CaveatType = 'authorizedScopes';
|
13
|
+
/**
|
14
|
+
* The target name of the CAIP-25 endowment permission.
|
15
|
+
*/
|
16
|
+
export const Caip25EndowmentPermissionName = 'endowment:caip25';
|
17
|
+
/**
|
18
|
+
* Creates a CAIP-25 permission caveat.
|
19
|
+
*
|
20
|
+
* @param value - The CAIP-25 permission caveat value.
|
21
|
+
* @returns The CAIP-25 permission caveat (now including the type).
|
22
|
+
*/
|
23
|
+
export const createCaip25Caveat = (value) => {
|
24
|
+
return {
|
25
|
+
type: Caip25CaveatType,
|
26
|
+
value,
|
27
|
+
};
|
28
|
+
};
|
29
|
+
/**
|
30
|
+
* Calculates the difference between two provided CAIP-25 permission caveat values, but only considering a single scope property at a time.
|
31
|
+
*
|
32
|
+
* @param originalValue - The existing CAIP-25 permission caveat value.
|
33
|
+
* @param mergedValue - The result from merging existing and incoming CAIP-25 permission caveat values.
|
34
|
+
* @param scopeToDiff - The required or optional scopes from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.
|
35
|
+
* @returns The difference between original and merged CAIP-25 permission caveat values.
|
36
|
+
*/
|
37
|
+
export function diffScopesForCaip25CaveatValue(originalValue, mergedValue, scopeToDiff) {
|
38
|
+
const diff = cloneDeep(originalValue);
|
39
|
+
const mergedScopeToDiff = mergedValue[scopeToDiff];
|
40
|
+
for (const [scopeString, mergedScopeObject] of Object.entries(mergedScopeToDiff)) {
|
41
|
+
const internalScopeString = scopeString;
|
42
|
+
const originalScopeObject = diff[scopeToDiff][internalScopeString];
|
43
|
+
if (originalScopeObject) {
|
44
|
+
const newAccounts = mergedScopeObject.accounts.filter((account) => !originalScopeObject?.accounts.includes(account));
|
45
|
+
if (newAccounts.length > 0) {
|
46
|
+
diff[scopeToDiff][internalScopeString] = {
|
47
|
+
accounts: newAccounts,
|
48
|
+
};
|
49
|
+
continue;
|
50
|
+
}
|
51
|
+
delete diff[scopeToDiff][internalScopeString];
|
52
|
+
}
|
53
|
+
else {
|
54
|
+
diff[scopeToDiff][internalScopeString] = mergedScopeObject;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
return diff;
|
58
|
+
}
|
59
|
+
/**
|
60
|
+
* Checks if every account in the given scopes object is supported.
|
61
|
+
*
|
62
|
+
* @param scopesObject - The scopes object to iterate over.
|
63
|
+
* @param listAccounts - The hook for getting internalAccount objects for all evm accounts.
|
64
|
+
* @param getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope.
|
65
|
+
* addresses.
|
66
|
+
* @returns True if every account in the scopes object is supported, false otherwise.
|
67
|
+
*/
|
68
|
+
function isEveryAccountInScopesObjectSupported(scopesObject, listAccounts, getNonEvmAccountAddresses) {
|
69
|
+
return Object.values(scopesObject).every((scopeObject) => scopeObject.accounts.every((account) => isSupportedAccount(account, {
|
70
|
+
getEvmInternalAccounts: listAccounts,
|
71
|
+
getNonEvmAccountAddresses,
|
72
|
+
})));
|
73
|
+
}
|
74
|
+
/**
|
75
|
+
* Helper that returns a `authorizedScopes` CAIP-25 caveat specification
|
76
|
+
* that can be passed into the PermissionController constructor.
|
77
|
+
*
|
78
|
+
* @param options - The specification builder options.
|
79
|
+
* @param options.findNetworkClientIdByChainId - The hook for getting the networkClientId that serves a chainId.
|
80
|
+
* @param options.listAccounts - The hook for getting internalAccount objects for all evm accounts.
|
81
|
+
* @param options.isNonEvmScopeSupported - The hook that determines if an non EVM scopeString is supported.
|
82
|
+
* @param options.getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope.
|
83
|
+
* @returns The specification for the `caip25` caveat.
|
84
|
+
*/
|
85
|
+
export const caip25CaveatBuilder = ({ findNetworkClientIdByChainId, listAccounts, isNonEvmScopeSupported, getNonEvmAccountAddresses, }) => {
|
86
|
+
return {
|
87
|
+
type: Caip25CaveatType,
|
88
|
+
validator: (caveat, _origin, _target) => {
|
89
|
+
if (!caveat.value ||
|
90
|
+
!hasProperty(caveat.value, 'requiredScopes') ||
|
91
|
+
!hasProperty(caveat.value, 'optionalScopes') ||
|
92
|
+
!hasProperty(caveat.value, 'isMultichainOrigin') ||
|
93
|
+
typeof caveat.value.isMultichainOrigin !== 'boolean') {
|
94
|
+
throw new Error(`${Caip25EndowmentPermissionName} error: Received invalid value for caveat of type "${Caip25CaveatType}".`);
|
95
|
+
}
|
96
|
+
const { requiredScopes, optionalScopes } = caveat.value;
|
97
|
+
assertIsInternalScopesObject(requiredScopes);
|
98
|
+
assertIsInternalScopesObject(optionalScopes);
|
99
|
+
const isEvmChainIdSupported = (chainId) => {
|
100
|
+
try {
|
101
|
+
findNetworkClientIdByChainId(chainId);
|
102
|
+
return true;
|
103
|
+
}
|
104
|
+
catch {
|
105
|
+
return false;
|
106
|
+
}
|
107
|
+
};
|
108
|
+
const allRequiredScopesSupported = Object.keys(requiredScopes).every((scopeString) => isSupportedScopeString(scopeString, {
|
109
|
+
isEvmChainIdSupported,
|
110
|
+
isNonEvmScopeSupported,
|
111
|
+
}));
|
112
|
+
const allOptionalScopesSupported = Object.keys(optionalScopes).every((scopeString) => isSupportedScopeString(scopeString, {
|
113
|
+
isEvmChainIdSupported,
|
114
|
+
isNonEvmScopeSupported,
|
115
|
+
}));
|
116
|
+
if (!allRequiredScopesSupported || !allOptionalScopesSupported) {
|
117
|
+
throw new Error(`${Caip25EndowmentPermissionName} error: Received scopeString value(s) for caveat of type "${Caip25CaveatType}" that are not supported by the wallet.`);
|
118
|
+
}
|
119
|
+
const allRequiredAccountsSupported = isEveryAccountInScopesObjectSupported(requiredScopes, listAccounts, getNonEvmAccountAddresses);
|
120
|
+
const allOptionalAccountsSupported = isEveryAccountInScopesObjectSupported(optionalScopes, listAccounts, getNonEvmAccountAddresses);
|
121
|
+
if (!allRequiredAccountsSupported || !allOptionalAccountsSupported) {
|
122
|
+
throw new Error(`${Caip25EndowmentPermissionName} error: Received account value(s) for caveat of type "${Caip25CaveatType}" that are not supported by the wallet.`);
|
123
|
+
}
|
124
|
+
},
|
125
|
+
merger: (leftValue, rightValue) => {
|
126
|
+
const mergedRequiredScopes = mergeInternalScopes(leftValue.requiredScopes, rightValue.requiredScopes);
|
127
|
+
const mergedOptionalScopes = mergeInternalScopes(leftValue.optionalScopes, rightValue.optionalScopes);
|
128
|
+
const mergedValue = {
|
129
|
+
requiredScopes: mergedRequiredScopes,
|
130
|
+
optionalScopes: mergedOptionalScopes,
|
131
|
+
isMultichainOrigin: leftValue.isMultichainOrigin,
|
132
|
+
};
|
133
|
+
const partialDiff = diffScopesForCaip25CaveatValue(leftValue, mergedValue, 'requiredScopes');
|
134
|
+
const diff = diffScopesForCaip25CaveatValue(partialDiff, mergedValue, 'optionalScopes');
|
135
|
+
return [mergedValue, diff];
|
136
|
+
},
|
137
|
+
};
|
138
|
+
};
|
139
|
+
/**
|
140
|
+
* Helper that returns a `endowment:caip25` specification that
|
141
|
+
* can be passed into the PermissionController constructor.
|
142
|
+
*
|
143
|
+
* @returns The specification for the `caip25` endowment.
|
144
|
+
*/
|
145
|
+
const specificationBuilder = () => {
|
146
|
+
return {
|
147
|
+
permissionType: PermissionType.Endowment,
|
148
|
+
targetName: Caip25EndowmentPermissionName,
|
149
|
+
allowedCaveats: [Caip25CaveatType],
|
150
|
+
endowmentGetter: (_getterOptions) => null,
|
151
|
+
validator: (permission) => {
|
152
|
+
if (permission.caveats?.length !== 1 ||
|
153
|
+
permission.caveats?.[0]?.type !== Caip25CaveatType) {
|
154
|
+
throw new Error(`${Caip25EndowmentPermissionName} error: Invalid caveats. There must be a single caveat of type "${Caip25CaveatType}".`);
|
155
|
+
}
|
156
|
+
},
|
157
|
+
};
|
158
|
+
};
|
159
|
+
/**
|
160
|
+
* The `caip25` endowment specification builder. Passed to the
|
161
|
+
* `PermissionController` for constructing and validating the
|
162
|
+
* `endowment:caip25` permission.
|
163
|
+
*/
|
164
|
+
export const caip25EndowmentBuilder = Object.freeze({
|
165
|
+
targetName: Caip25EndowmentPermissionName,
|
166
|
+
specificationBuilder,
|
167
|
+
});
|
168
|
+
/**
|
169
|
+
* Factories that construct caveat mutator functions that are passed to
|
170
|
+
* PermissionController.updatePermissionsByCaveat.
|
171
|
+
*/
|
172
|
+
export const Caip25CaveatMutators = {
|
173
|
+
[Caip25CaveatType]: {
|
174
|
+
removeScope,
|
175
|
+
removeAccount,
|
176
|
+
},
|
177
|
+
};
|
178
|
+
/**
|
179
|
+
* Removes the account from the scope object.
|
180
|
+
*
|
181
|
+
* @param targetAddress - The address to remove from the scope object.
|
182
|
+
* @returns A function that removes the account from the scope object.
|
183
|
+
*/
|
184
|
+
function removeAccountFilterFn(targetAddress) {
|
185
|
+
return (account) => {
|
186
|
+
const parsed = parseCaipAccountId(account);
|
187
|
+
return parsed.address !== targetAddress;
|
188
|
+
};
|
189
|
+
}
|
190
|
+
/**
|
191
|
+
* Removes the account from the scope object.
|
192
|
+
*
|
193
|
+
* @param scopeObject - The scope object to remove the account from.
|
194
|
+
* @param targetAddress - The address to remove from the scope object.
|
195
|
+
*/
|
196
|
+
function removeAccountFromScopeObject(scopeObject, targetAddress) {
|
197
|
+
if (scopeObject.accounts) {
|
198
|
+
scopeObject.accounts = scopeObject.accounts.filter(removeAccountFilterFn(targetAddress));
|
199
|
+
}
|
200
|
+
}
|
201
|
+
/**
|
202
|
+
* Removes the target account from the scope object.
|
203
|
+
*
|
204
|
+
* @param caip25CaveatValue - The CAIP-25 permission caveat value from which to remove the account (across all chain scopes).
|
205
|
+
* @param targetAddress - The address to remove from the scope object. Not a CAIP-10 formatted address because it will be removed across each chain scope.
|
206
|
+
* @returns The updated scope object.
|
207
|
+
*/
|
208
|
+
function removeAccount(caip25CaveatValue, targetAddress) {
|
209
|
+
const updatedCaveatValue = cloneDeep(caip25CaveatValue);
|
210
|
+
[
|
211
|
+
updatedCaveatValue.requiredScopes,
|
212
|
+
updatedCaveatValue.optionalScopes,
|
213
|
+
].forEach((scopes) => {
|
214
|
+
Object.entries(scopes).forEach(([, scopeObject]) => {
|
215
|
+
removeAccountFromScopeObject(scopeObject, targetAddress);
|
216
|
+
});
|
217
|
+
});
|
218
|
+
const noChange = isEqual(updatedCaveatValue, caip25CaveatValue);
|
219
|
+
if (noChange) {
|
220
|
+
return {
|
221
|
+
operation: CaveatMutatorOperation.Noop,
|
222
|
+
};
|
223
|
+
}
|
224
|
+
const hasAccounts = [
|
225
|
+
...Object.values(updatedCaveatValue.requiredScopes),
|
226
|
+
...Object.values(updatedCaveatValue.optionalScopes),
|
227
|
+
].some(({ accounts }) => accounts.length > 0);
|
228
|
+
if (hasAccounts) {
|
229
|
+
return {
|
230
|
+
operation: CaveatMutatorOperation.UpdateValue,
|
231
|
+
value: updatedCaveatValue,
|
232
|
+
};
|
233
|
+
}
|
234
|
+
return {
|
235
|
+
operation: CaveatMutatorOperation.RevokePermission,
|
236
|
+
};
|
237
|
+
}
|
238
|
+
/**
|
239
|
+
* Removes the target scope from the value arrays of the given
|
240
|
+
* `endowment:caip25` caveat. No-ops if the target scopeString is not in
|
241
|
+
* the existing scopes.
|
242
|
+
*
|
243
|
+
* @param caip25CaveatValue - The CAIP-25 permission caveat value to remove the scope from.
|
244
|
+
* @param targetScopeString - The scope that is being removed.
|
245
|
+
* @returns The updated CAIP-25 permission caveat value.
|
246
|
+
*/
|
247
|
+
function removeScope(caip25CaveatValue, targetScopeString) {
|
248
|
+
const newRequiredScopes = Object.entries(caip25CaveatValue.requiredScopes).filter(([scope]) => scope !== targetScopeString);
|
249
|
+
const newOptionalScopes = Object.entries(caip25CaveatValue.optionalScopes).filter(([scope]) => {
|
250
|
+
return scope !== targetScopeString;
|
251
|
+
});
|
252
|
+
const requiredScopesRemoved = newRequiredScopes.length !==
|
253
|
+
Object.keys(caip25CaveatValue.requiredScopes).length;
|
254
|
+
const optionalScopesRemoved = newOptionalScopes.length !==
|
255
|
+
Object.keys(caip25CaveatValue.optionalScopes).length;
|
256
|
+
if (!requiredScopesRemoved && !optionalScopesRemoved) {
|
257
|
+
return {
|
258
|
+
operation: CaveatMutatorOperation.Noop,
|
259
|
+
};
|
260
|
+
}
|
261
|
+
const updatedCaveatValue = {
|
262
|
+
...caip25CaveatValue,
|
263
|
+
requiredScopes: Object.fromEntries(newRequiredScopes),
|
264
|
+
optionalScopes: Object.fromEntries(newOptionalScopes),
|
265
|
+
};
|
266
|
+
const hasNonWalletScopes = [...newRequiredScopes, ...newOptionalScopes].some(([scopeString]) => {
|
267
|
+
const { namespace } = parseScopeString(scopeString);
|
268
|
+
return namespace !== KnownCaipNamespace.Wallet;
|
269
|
+
});
|
270
|
+
if (hasNonWalletScopes) {
|
271
|
+
return {
|
272
|
+
operation: CaveatMutatorOperation.UpdateValue,
|
273
|
+
value: updatedCaveatValue,
|
274
|
+
};
|
275
|
+
}
|
276
|
+
return {
|
277
|
+
operation: CaveatMutatorOperation.RevokePermission,
|
278
|
+
};
|
279
|
+
}
|
280
|
+
//# sourceMappingURL=caip25Permission.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"caip25Permission.mjs","sourceRoot":"","sources":["../src/caip25Permission.ts"],"names":[],"mappings":"AASA,OAAO,EACL,sBAAsB,EACtB,cAAc,EACf,wCAAwC;AAEzC,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,kBAAkB,EAGnB,wBAAwB;;;AAGzB,OAAO,EAAE,4BAA4B,EAAE,2BAAuB;AAC9D,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,8BAA0B;AAC/E,OAAO,EAAE,mB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AiB,EAAE,GAAG,iBAAiB,CAAC,CAAC,IAAI,CAC1E,CAAC,CAAC,WAAW,CAAC,EAAE,EAAE;QAChB,MAAM,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC;QACpD,OAAO,SAAS,KAAK,kBAAkB,CAAC,MAAM,CAAC;IACjD,CAAC,CACF,CAAC;IAEF,IAAI,kBAAkB,EAAE;QACtB,OAAO;YACL,SAAS,EAAE,sBAAsB,CAAC,WAAW;YAC7C,KAAK,EAAE,kBAAkB;SAC1B,CAAC;KACH;IAED,OAAO;QACL,SAAS,EAAE,sBAAsB,CAAC,gBAAgB;KACnD,CAAC;AACJ,CAAC","sourcesContent":["import type { NetworkClientId } from '@metamask/network-controller';\nimport type {\n PermissionSpecificationBuilder,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n PermissionValidatorConstraint,\n PermissionConstraint,\n EndowmentCaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport {\n CaveatMutatorOperation,\n PermissionType,\n} from '@metamask/permission-controller';\nimport type { CaipAccountId, CaipChainId, Json } from '@metamask/utils';\nimport {\n hasProperty,\n KnownCaipNamespace,\n parseCaipAccountId,\n type Hex,\n type NonEmptyArray,\n} from '@metamask/utils';\nimport { cloneDeep, isEqual } from 'lodash';\n\nimport { assertIsInternalScopesObject } from './scope/assert';\nimport { isSupportedAccount, isSupportedScopeString } from './scope/supported';\nimport { mergeInternalScopes } from './scope/transform';\nimport {\n parseScopeString,\n type ExternalScopeString,\n type InternalScopeObject,\n type InternalScopesObject,\n} from './scope/types';\n\n/**\n * The CAIP-25 permission caveat value.\n * This permission contains the required and optional scopes and session properties from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request that initiated the permission session.\n * It also contains a boolean (isMultichainOrigin) indicating if the permission session is multichain, which may be needed to determine implicit permissioning.\n */\nexport type Caip25CaveatValue = {\n requiredScopes: InternalScopesObject;\n optionalScopes: InternalScopesObject;\n sessionProperties?: Record<string, Json>;\n isMultichainOrigin: boolean;\n};\n\n/**\n * The name of the CAIP-25 permission caveat.\n */\nexport const Caip25CaveatType = 'authorizedScopes';\n\n/**\n * The target name of the CAIP-25 endowment permission.\n */\nexport const Caip25EndowmentPermissionName = 'endowment:caip25';\n\n/**\n * Creates a CAIP-25 permission caveat.\n *\n * @param value - The CAIP-25 permission caveat value.\n * @returns The CAIP-25 permission caveat (now including the type).\n */\nexport const createCaip25Caveat = (value: Caip25CaveatValue) => {\n return {\n type: Caip25CaveatType,\n value,\n };\n};\n\ntype Caip25EndowmentCaveatSpecificationBuilderOptions = {\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId;\n listAccounts: () => { type: string; address: Hex }[];\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n getNonEvmAccountAddresses: (scope: CaipChainId) => string[];\n};\n\n/**\n * Calculates the difference between two provided CAIP-25 permission caveat values, but only considering a single scope property at a time.\n *\n * @param originalValue - The existing CAIP-25 permission caveat value.\n * @param mergedValue - The result from merging existing and incoming CAIP-25 permission caveat values.\n * @param scopeToDiff - The required or optional scopes from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.\n * @returns The difference between original and merged CAIP-25 permission caveat values.\n */\nexport function diffScopesForCaip25CaveatValue(\n originalValue: Caip25CaveatValue,\n mergedValue: Caip25CaveatValue,\n scopeToDiff: 'optionalScopes' | 'requiredScopes',\n): Caip25CaveatValue {\n const diff = cloneDeep(originalValue);\n\n const mergedScopeToDiff = mergedValue[scopeToDiff];\n for (const [scopeString, mergedScopeObject] of Object.entries(\n mergedScopeToDiff,\n )) {\n const internalScopeString = scopeString as keyof typeof mergedScopeToDiff;\n const originalScopeObject = diff[scopeToDiff][internalScopeString];\n\n if (originalScopeObject) {\n const newAccounts = mergedScopeObject.accounts.filter(\n (account) => !originalScopeObject?.accounts.includes(account),\n );\n if (newAccounts.length > 0) {\n diff[scopeToDiff][internalScopeString] = {\n accounts: newAccounts,\n };\n continue;\n }\n delete diff[scopeToDiff][internalScopeString];\n } else {\n diff[scopeToDiff][internalScopeString] = mergedScopeObject;\n }\n }\n\n return diff;\n}\n\n/**\n * Checks if every account in the given scopes object is supported.\n *\n * @param scopesObject - The scopes object to iterate over.\n * @param listAccounts - The hook for getting internalAccount objects for all evm accounts.\n * @param getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope.\n * addresses.\n * @returns True if every account in the scopes object is supported, false otherwise.\n */\nfunction isEveryAccountInScopesObjectSupported(\n scopesObject: InternalScopesObject,\n listAccounts: () => { type: string; address: Hex }[],\n getNonEvmAccountAddresses: (scope: CaipChainId) => string[],\n) {\n return Object.values(scopesObject).every((scopeObject) =>\n scopeObject.accounts.every((account) =>\n isSupportedAccount(account, {\n getEvmInternalAccounts: listAccounts,\n getNonEvmAccountAddresses,\n }),\n ),\n );\n}\n\n/**\n * Helper that returns a `authorizedScopes` CAIP-25 caveat specification\n * that can be passed into the PermissionController constructor.\n *\n * @param options - The specification builder options.\n * @param options.findNetworkClientIdByChainId - The hook for getting the networkClientId that serves a chainId.\n * @param options.listAccounts - The hook for getting internalAccount objects for all evm accounts.\n * @param options.isNonEvmScopeSupported - The hook that determines if an non EVM scopeString is supported.\n * @param options.getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope.\n * @returns The specification for the `caip25` caveat.\n */\nexport const caip25CaveatBuilder = ({\n findNetworkClientIdByChainId,\n listAccounts,\n isNonEvmScopeSupported,\n getNonEvmAccountAddresses,\n}: Caip25EndowmentCaveatSpecificationBuilderOptions): EndowmentCaveatSpecificationConstraint &\n Required<\n Pick<EndowmentCaveatSpecificationConstraint, 'validator' | 'merger'>\n > => {\n return {\n type: Caip25CaveatType,\n validator: (\n caveat: { type: typeof Caip25CaveatType; value: unknown },\n _origin?: string,\n _target?: string,\n ) => {\n if (\n !caveat.value ||\n !hasProperty(caveat.value, 'requiredScopes') ||\n !hasProperty(caveat.value, 'optionalScopes') ||\n !hasProperty(caveat.value, 'isMultichainOrigin') ||\n typeof caveat.value.isMultichainOrigin !== 'boolean'\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received invalid value for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n const { requiredScopes, optionalScopes } = caveat.value;\n\n assertIsInternalScopesObject(requiredScopes);\n assertIsInternalScopesObject(optionalScopes);\n\n const isEvmChainIdSupported = (chainId: Hex) => {\n try {\n findNetworkClientIdByChainId(chainId);\n return true;\n } catch {\n return false;\n }\n };\n\n const allRequiredScopesSupported = Object.keys(requiredScopes).every(\n (scopeString) =>\n isSupportedScopeString(scopeString, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n }),\n );\n const allOptionalScopesSupported = Object.keys(optionalScopes).every(\n (scopeString) =>\n isSupportedScopeString(scopeString, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n }),\n );\n if (!allRequiredScopesSupported || !allOptionalScopesSupported) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received scopeString value(s) for caveat of type \"${Caip25CaveatType}\" that are not supported by the wallet.`,\n );\n }\n\n const allRequiredAccountsSupported =\n isEveryAccountInScopesObjectSupported(\n requiredScopes,\n listAccounts,\n getNonEvmAccountAddresses,\n );\n const allOptionalAccountsSupported =\n isEveryAccountInScopesObjectSupported(\n optionalScopes,\n listAccounts,\n getNonEvmAccountAddresses,\n );\n if (!allRequiredAccountsSupported || !allOptionalAccountsSupported) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received account value(s) for caveat of type \"${Caip25CaveatType}\" that are not supported by the wallet.`,\n );\n }\n },\n merger: (\n leftValue: Caip25CaveatValue,\n rightValue: Caip25CaveatValue,\n ): [Caip25CaveatValue, Caip25CaveatValue] => {\n const mergedRequiredScopes = mergeInternalScopes(\n leftValue.requiredScopes,\n rightValue.requiredScopes,\n );\n const mergedOptionalScopes = mergeInternalScopes(\n leftValue.optionalScopes,\n rightValue.optionalScopes,\n );\n\n const mergedValue: Caip25CaveatValue = {\n requiredScopes: mergedRequiredScopes,\n optionalScopes: mergedOptionalScopes,\n isMultichainOrigin: leftValue.isMultichainOrigin,\n };\n\n const partialDiff = diffScopesForCaip25CaveatValue(\n leftValue,\n mergedValue,\n 'requiredScopes',\n );\n\n const diff = diffScopesForCaip25CaveatValue(\n partialDiff,\n mergedValue,\n 'optionalScopes',\n );\n\n return [mergedValue, diff];\n },\n };\n};\n\ntype Caip25EndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof Caip25EndowmentPermissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => null;\n validator: PermissionValidatorConstraint;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * Helper that returns a `endowment:caip25` specification that\n * can be passed into the PermissionController constructor.\n *\n * @returns The specification for the `caip25` endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n Record<never, never>,\n Caip25EndowmentSpecification\n> = () => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: Caip25EndowmentPermissionName,\n allowedCaveats: [Caip25CaveatType],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n validator: (permission: PermissionConstraint) => {\n if (\n permission.caveats?.length !== 1 ||\n permission.caveats?.[0]?.type !== Caip25CaveatType\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Invalid caveats. There must be a single caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n },\n };\n};\n\n/**\n * The `caip25` endowment specification builder. Passed to the\n * `PermissionController` for constructing and validating the\n * `endowment:caip25` permission.\n */\nexport const caip25EndowmentBuilder = Object.freeze({\n targetName: Caip25EndowmentPermissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Factories that construct caveat mutator functions that are passed to\n * PermissionController.updatePermissionsByCaveat.\n */\nexport const Caip25CaveatMutators = {\n [Caip25CaveatType]: {\n removeScope,\n removeAccount,\n },\n};\n\n/**\n * Removes the account from the scope object.\n *\n * @param targetAddress - The address to remove from the scope object.\n * @returns A function that removes the account from the scope object.\n */\nfunction removeAccountFilterFn(targetAddress: string) {\n return (account: CaipAccountId) => {\n const parsed = parseCaipAccountId(account);\n return parsed.address !== targetAddress;\n };\n}\n\n/**\n * Removes the account from the scope object.\n *\n * @param scopeObject - The scope object to remove the account from.\n * @param targetAddress - The address to remove from the scope object.\n */\nfunction removeAccountFromScopeObject(\n scopeObject: InternalScopeObject,\n targetAddress: string,\n) {\n if (scopeObject.accounts) {\n scopeObject.accounts = scopeObject.accounts.filter(\n removeAccountFilterFn(targetAddress),\n );\n }\n}\n\n/**\n * Removes the target account from the scope object.\n *\n * @param caip25CaveatValue - The CAIP-25 permission caveat value from which to remove the account (across all chain scopes).\n * @param targetAddress - The address to remove from the scope object. Not a CAIP-10 formatted address because it will be removed across each chain scope.\n * @returns The updated scope object.\n */\nfunction removeAccount(\n caip25CaveatValue: Caip25CaveatValue,\n targetAddress: Hex,\n) {\n const updatedCaveatValue = cloneDeep(caip25CaveatValue);\n\n [\n updatedCaveatValue.requiredScopes,\n updatedCaveatValue.optionalScopes,\n ].forEach((scopes) => {\n Object.entries(scopes).forEach(([, scopeObject]) => {\n removeAccountFromScopeObject(scopeObject, targetAddress);\n });\n });\n\n const noChange = isEqual(updatedCaveatValue, caip25CaveatValue);\n\n if (noChange) {\n return {\n operation: CaveatMutatorOperation.Noop,\n };\n }\n\n const hasAccounts = [\n ...Object.values(updatedCaveatValue.requiredScopes),\n ...Object.values(updatedCaveatValue.optionalScopes),\n ].some(({ accounts }) => accounts.length > 0);\n\n if (hasAccounts) {\n return {\n operation: CaveatMutatorOperation.UpdateValue,\n value: updatedCaveatValue,\n };\n }\n\n return {\n operation: CaveatMutatorOperation.RevokePermission,\n };\n}\n\n/**\n * Removes the target scope from the value arrays of the given\n * `endowment:caip25` caveat. No-ops if the target scopeString is not in\n * the existing scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 permission caveat value to remove the scope from.\n * @param targetScopeString - The scope that is being removed.\n * @returns The updated CAIP-25 permission caveat value.\n */\nfunction removeScope(\n caip25CaveatValue: Caip25CaveatValue,\n targetScopeString: ExternalScopeString,\n) {\n const newRequiredScopes = Object.entries(\n caip25CaveatValue.requiredScopes,\n ).filter(([scope]) => scope !== targetScopeString);\n const newOptionalScopes = Object.entries(\n caip25CaveatValue.optionalScopes,\n ).filter(([scope]) => {\n return scope !== targetScopeString;\n });\n\n const requiredScopesRemoved =\n newRequiredScopes.length !==\n Object.keys(caip25CaveatValue.requiredScopes).length;\n const optionalScopesRemoved =\n newOptionalScopes.length !==\n Object.keys(caip25CaveatValue.optionalScopes).length;\n\n if (!requiredScopesRemoved && !optionalScopesRemoved) {\n return {\n operation: CaveatMutatorOperation.Noop,\n };\n }\n\n const updatedCaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: Object.fromEntries(newRequiredScopes),\n optionalScopes: Object.fromEntries(newOptionalScopes),\n };\n\n const hasNonWalletScopes = [...newRequiredScopes, ...newOptionalScopes].some(\n ([scopeString]) => {\n const { namespace } = parseScopeString(scopeString);\n return namespace !== KnownCaipNamespace.Wallet;\n },\n );\n\n if (hasNonWalletScopes) {\n return {\n operation: CaveatMutatorOperation.UpdateValue,\n value: updatedCaveatValue,\n };\n }\n\n return {\n operation: CaveatMutatorOperation.RevokePermission,\n };\n}\n"]}
|
package/dist/index.cjs
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Caip25CaveatMutators = exports.caip25EndowmentBuilder = exports.Caip25EndowmentPermissionName = exports.createCaip25Caveat = exports.Caip25CaveatType = exports.caip25CaveatBuilder = exports.normalizeAndMergeScopes = exports.mergeInternalScopes = exports.mergeNormalizedScopes = exports.mergeScopeObject = exports.normalizeScope = exports.getUniqueArrayItems = exports.parseScopeString = exports.getSupportedScopeObjects = exports.KnownWalletScopeString = exports.KnownNotifications = exports.KnownWalletNamespaceRpcMethods = exports.KnownRpcMethods = exports.KnownWalletRpcMethods = exports.assertIsInternalScopeString = exports.bucketScopes = exports.validateAndNormalizeScopes = exports.getSessionScopes = exports.getInternalScopesObject = exports.setPermittedEthChainIds = exports.addPermittedEthChainId = exports.getPermittedEthChainIds = exports.setEthAccounts = exports.getEthAccounts = void 0;
|
4
|
+
var caip_permission_adapter_eth_accounts_1 = require("./adapters/caip-permission-adapter-eth-accounts.cjs");
|
5
|
+
Object.defineProperty(exports, "getEthAccounts", { enumerable: true, get: function () { return caip_permission_adapter_eth_accounts_1.getEthAccounts; } });
|
6
|
+
Object.defineProperty(exports, "setEthAccounts", { enumerable: true, get: function () { return caip_permission_adapter_eth_accounts_1.setEthAccounts; } });
|
7
|
+
var caip_permission_adapter_permittedChains_1 = require("./adapters/caip-permission-adapter-permittedChains.cjs");
|
8
|
+
Object.defineProperty(exports, "getPermittedEthChainIds", { enumerable: true, get: function () { return caip_permission_adapter_permittedChains_1.getPermittedEthChainIds; } });
|
9
|
+
Object.defineProperty(exports, "addPermittedEthChainId", { enumerable: true, get: function () { return caip_permission_adapter_permittedChains_1.addPermittedEthChainId; } });
|
10
|
+
Object.defineProperty(exports, "setPermittedEthChainIds", { enumerable: true, get: function () { return caip_permission_adapter_permittedChains_1.setPermittedEthChainIds; } });
|
11
|
+
var caip_permission_adapter_session_scopes_1 = require("./adapters/caip-permission-adapter-session-scopes.cjs");
|
12
|
+
Object.defineProperty(exports, "getInternalScopesObject", { enumerable: true, get: function () { return caip_permission_adapter_session_scopes_1.getInternalScopesObject; } });
|
13
|
+
Object.defineProperty(exports, "getSessionScopes", { enumerable: true, get: function () { return caip_permission_adapter_session_scopes_1.getSessionScopes; } });
|
14
|
+
var authorization_1 = require("./scope/authorization.cjs");
|
15
|
+
Object.defineProperty(exports, "validateAndNormalizeScopes", { enumerable: true, get: function () { return authorization_1.validateAndNormalizeScopes; } });
|
16
|
+
Object.defineProperty(exports, "bucketScopes", { enumerable: true, get: function () { return authorization_1.bucketScopes; } });
|
17
|
+
var assert_1 = require("./scope/assert.cjs");
|
18
|
+
Object.defineProperty(exports, "assertIsInternalScopeString", { enumerable: true, get: function () { return assert_1.assertIsInternalScopeString; } });
|
19
|
+
var constants_1 = require("./scope/constants.cjs");
|
20
|
+
Object.defineProperty(exports, "KnownWalletRpcMethods", { enumerable: true, get: function () { return constants_1.KnownWalletRpcMethods; } });
|
21
|
+
Object.defineProperty(exports, "KnownRpcMethods", { enumerable: true, get: function () { return constants_1.KnownRpcMethods; } });
|
22
|
+
Object.defineProperty(exports, "KnownWalletNamespaceRpcMethods", { enumerable: true, get: function () { return constants_1.KnownWalletNamespaceRpcMethods; } });
|
23
|
+
Object.defineProperty(exports, "KnownNotifications", { enumerable: true, get: function () { return constants_1.KnownNotifications; } });
|
24
|
+
Object.defineProperty(exports, "KnownWalletScopeString", { enumerable: true, get: function () { return constants_1.KnownWalletScopeString; } });
|
25
|
+
var filter_1 = require("./scope/filter.cjs");
|
26
|
+
Object.defineProperty(exports, "getSupportedScopeObjects", { enumerable: true, get: function () { return filter_1.getSupportedScopeObjects; } });
|
27
|
+
var types_1 = require("./scope/types.cjs");
|
28
|
+
Object.defineProperty(exports, "parseScopeString", { enumerable: true, get: function () { return types_1.parseScopeString; } });
|
29
|
+
var transform_1 = require("./scope/transform.cjs");
|
30
|
+
Object.defineProperty(exports, "getUniqueArrayItems", { enumerable: true, get: function () { return transform_1.getUniqueArrayItems; } });
|
31
|
+
Object.defineProperty(exports, "normalizeScope", { enumerable: true, get: function () { return transform_1.normalizeScope; } });
|
32
|
+
Object.defineProperty(exports, "mergeScopeObject", { enumerable: true, get: function () { return transform_1.mergeScopeObject; } });
|
33
|
+
Object.defineProperty(exports, "mergeNormalizedScopes", { enumerable: true, get: function () { return transform_1.mergeNormalizedScopes; } });
|
34
|
+
Object.defineProperty(exports, "mergeInternalScopes", { enumerable: true, get: function () { return transform_1.mergeInternalScopes; } });
|
35
|
+
Object.defineProperty(exports, "normalizeAndMergeScopes", { enumerable: true, get: function () { return transform_1.normalizeAndMergeScopes; } });
|
36
|
+
var caip25Permission_1 = require("./caip25Permission.cjs");
|
37
|
+
Object.defineProperty(exports, "caip25CaveatBuilder", { enumerable: true, get: function () { return caip25Permission_1.caip25CaveatBuilder; } });
|
38
|
+
Object.defineProperty(exports, "Caip25CaveatType", { enumerable: true, get: function () { return caip25Permission_1.Caip25CaveatType; } });
|
39
|
+
Object.defineProperty(exports, "createCaip25Caveat", { enumerable: true, get: function () { return caip25Permission_1.createCaip25Caveat; } });
|
40
|
+
Object.defineProperty(exports, "Caip25EndowmentPermissionName", { enumerable: true, get: function () { return caip25Permission_1.Caip25EndowmentPermissionName; } });
|
41
|
+
Object.defineProperty(exports, "caip25EndowmentBuilder", { enumerable: true, get: function () { return caip25Permission_1.caip25EndowmentBuilder; } });
|
42
|
+
Object.defineProperty(exports, "Caip25CaveatMutators", { enumerable: true, get: function () { return caip25Permission_1.Caip25CaveatMutators; } });
|
43
|
+
//# sourceMappingURL=index.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,4GAGyD;AAFvD,sIAAA,cAAc,OAAA;AACd,sIAAA,cAAc,OAAA;AAEhB,kHAI4D;AAH1D,kJAAA,uBAAuB,OAAA;AACvB,iJAAA,sBAAsB,OAAA;AACtB,kJAAA,uBAAuB,OAAA;AAEzB,gHAG2D;AAFzD,iJAAA,uBAAuB,OAAA;AACvB,0IAAA,gBAAgB,OAAA;AAIlB,2DAG+B;AAF7B,2HAAA,0BAA0B,OAAA;AAC1B,6GAAA,YAAY,OAAA;AAEd,6CAA6D;AAApD,qHAAA,2BAA2B,OAAA;AACpC,mDAM2B;AALzB,kHAAA,qBAAqB,OAAA;AACrB,4GAAA,eAAe,OAAA;AACf,2HAAA,8BAA8B,OAAA;AAC9B,+GAAA,kBAAkB,OAAA;AAClB,mHAAA,sBAAsB,OAAA;AAExB,6CAA0D;AAAjD,kHAAA,wBAAwB,OAAA;AAajC,2CAAiD;AAAxC,yGAAA,gBAAgB,OAAA;AACzB,mDAO2B;AANzB,gHAAA,mBAAmB,OAAA;AACnB,2GAAA,cAAc,OAAA;AACd,6GAAA,gBAAgB,OAAA;AAChB,kHAAA,qBAAqB,OAAA;AACrB,gHAAA,mBAAmB,OAAA;AACnB,oHAAA,uBAAuB,OAAA;AAIzB,2DAO4B;AAN1B,uHAAA,mBAAmB,OAAA;AACnB,oHAAA,gBAAgB,OAAA;AAChB,sHAAA,kBAAkB,OAAA;AAClB,iIAAA,6BAA6B,OAAA;AAC7B,0HAAA,sBAAsB,OAAA;AACtB,wHAAA,oBAAoB,OAAA","sourcesContent":["export {\n getEthAccounts,\n setEthAccounts,\n} from './adapters/caip-permission-adapter-eth-accounts';\nexport {\n getPermittedEthChainIds,\n addPermittedEthChainId,\n setPermittedEthChainIds,\n} from './adapters/caip-permission-adapter-permittedChains';\nexport {\n getInternalScopesObject,\n getSessionScopes,\n} from './adapters/caip-permission-adapter-session-scopes';\n\nexport type { Caip25Authorization } from './scope/authorization';\nexport {\n validateAndNormalizeScopes,\n bucketScopes,\n} from './scope/authorization';\nexport { assertIsInternalScopeString } from './scope/assert';\nexport {\n KnownWalletRpcMethods,\n KnownRpcMethods,\n KnownWalletNamespaceRpcMethods,\n KnownNotifications,\n KnownWalletScopeString,\n} from './scope/constants';\nexport { getSupportedScopeObjects } from './scope/filter';\nexport type {\n ExternalScopeString,\n ExternalScopeObject,\n ExternalScopesObject,\n InternalScopeString,\n InternalScopeObject,\n InternalScopesObject,\n NormalizedScopeObject,\n NormalizedScopesObject,\n ScopedProperties,\n NonWalletKnownCaipNamespace,\n} from './scope/types';\nexport { parseScopeString } from './scope/types';\nexport {\n getUniqueArrayItems,\n normalizeScope,\n mergeScopeObject,\n mergeNormalizedScopes,\n mergeInternalScopes,\n normalizeAndMergeScopes,\n} from './scope/transform';\n\nexport type { Caip25CaveatValue } from './caip25Permission';\nexport {\n caip25CaveatBuilder,\n Caip25CaveatType,\n createCaip25Caveat,\n Caip25EndowmentPermissionName,\n caip25EndowmentBuilder,\n Caip25CaveatMutators,\n} from './caip25Permission';\n"]}
|
package/dist/index.d.cts
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
export { getEthAccounts, setEthAccounts, } from "./adapters/caip-permission-adapter-eth-accounts.cjs";
|
2
|
+
export { getPermittedEthChainIds, addPermittedEthChainId, setPermittedEthChainIds, } from "./adapters/caip-permission-adapter-permittedChains.cjs";
|
3
|
+
export { getInternalScopesObject, getSessionScopes, } from "./adapters/caip-permission-adapter-session-scopes.cjs";
|
4
|
+
export type { Caip25Authorization } from "./scope/authorization.cjs";
|
5
|
+
export { validateAndNormalizeScopes, bucketScopes, } from "./scope/authorization.cjs";
|
6
|
+
export { assertIsInternalScopeString } from "./scope/assert.cjs";
|
7
|
+
export { KnownWalletRpcMethods, KnownRpcMethods, KnownWalletNamespaceRpcMethods, KnownNotifications, KnownWalletScopeString, } from "./scope/constants.cjs";
|
8
|
+
export { getSupportedScopeObjects } from "./scope/filter.cjs";
|
9
|
+
export type { ExternalScopeString, ExternalScopeObject, ExternalScopesObject, InternalScopeString, InternalScopeObject, InternalScopesObject, NormalizedScopeObject, NormalizedScopesObject, ScopedProperties, NonWalletKnownCaipNamespace, } from "./scope/types.cjs";
|
10
|
+
export { parseScopeString } from "./scope/types.cjs";
|
11
|
+
export { getUniqueArrayItems, normalizeScope, mergeScopeObject, mergeNormalizedScopes, mergeInternalScopes, normalizeAndMergeScopes, } from "./scope/transform.cjs";
|
12
|
+
export type { Caip25CaveatValue } from "./caip25Permission.cjs";
|
13
|
+
export { caip25CaveatBuilder, Caip25CaveatType, createCaip25Caveat, Caip25EndowmentPermissionName, caip25EndowmentBuilder, Caip25CaveatMutators, } from "./caip25Permission.cjs";
|
14
|
+
//# sourceMappingURL=index.d.cts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,GACf,4DAAwD;AACzD,OAAO,EACL,uBAAuB,EACvB,sBAAsB,EACtB,uBAAuB,GACxB,+DAA2D;AAC5D,OAAO,EACL,uBAAuB,EACvB,gBAAgB,GACjB,8DAA0D;AAE3D,YAAY,EAAE,mBAAmB,EAAE,kCAA8B;AACjE,OAAO,EACL,0BAA0B,EAC1B,YAAY,GACb,kCAA8B;AAC/B,OAAO,EAAE,2BAA2B,EAAE,2BAAuB;AAC7D,OAAO,EACL,qBAAqB,EACrB,eAAe,EACf,8BAA8B,EAC9B,kBAAkB,EAClB,sBAAsB,GACvB,8BAA0B;AAC3B,OAAO,EAAE,wBAAwB,EAAE,2BAAuB;AAC1D,YAAY,EACV,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACtB,gBAAgB,EAChB,2BAA2B,GAC5B,0BAAsB;AACvB,OAAO,EAAE,gBAAgB,EAAE,0BAAsB;AACjD,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EACnB,uBAAuB,GACxB,8BAA0B;AAE3B,YAAY,EAAE,iBAAiB,EAAE,+BAA2B;AAC5D,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,EAClB,6BAA6B,EAC7B,sBAAsB,EACtB,oBAAoB,GACrB,+BAA2B"}
|
package/dist/index.d.mts
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
export { getEthAccounts, setEthAccounts, } from "./adapters/caip-permission-adapter-eth-accounts.mjs";
|
2
|
+
export { getPermittedEthChainIds, addPermittedEthChainId, setPermittedEthChainIds, } from "./adapters/caip-permission-adapter-permittedChains.mjs";
|
3
|
+
export { getInternalScopesObject, getSessionScopes, } from "./adapters/caip-permission-adapter-session-scopes.mjs";
|
4
|
+
export type { Caip25Authorization } from "./scope/authorization.mjs";
|
5
|
+
export { validateAndNormalizeScopes, bucketScopes, } from "./scope/authorization.mjs";
|
6
|
+
export { assertIsInternalScopeString } from "./scope/assert.mjs";
|
7
|
+
export { KnownWalletRpcMethods, KnownRpcMethods, KnownWalletNamespaceRpcMethods, KnownNotifications, KnownWalletScopeString, } from "./scope/constants.mjs";
|
8
|
+
export { getSupportedScopeObjects } from "./scope/filter.mjs";
|
9
|
+
export type { ExternalScopeString, ExternalScopeObject, ExternalScopesObject, InternalScopeString, InternalScopeObject, InternalScopesObject, NormalizedScopeObject, NormalizedScopesObject, ScopedProperties, NonWalletKnownCaipNamespace, } from "./scope/types.mjs";
|
10
|
+
export { parseScopeString } from "./scope/types.mjs";
|
11
|
+
export { getUniqueArrayItems, normalizeScope, mergeScopeObject, mergeNormalizedScopes, mergeInternalScopes, normalizeAndMergeScopes, } from "./scope/transform.mjs";
|
12
|
+
export type { Caip25CaveatValue } from "./caip25Permission.mjs";
|
13
|
+
export { caip25CaveatBuilder, Caip25CaveatType, createCaip25Caveat, Caip25EndowmentPermissionName, caip25EndowmentBuilder, Caip25CaveatMutators, } from "./caip25Permission.mjs";
|
14
|
+
//# sourceMappingURL=index.d.mts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,GACf,4DAAwD;AACzD,OAAO,EACL,uBAAuB,EACvB,sBAAsB,EACtB,uBAAuB,GACxB,+DAA2D;AAC5D,OAAO,EACL,uBAAuB,EACvB,gBAAgB,GACjB,8DAA0D;AAE3D,YAAY,EAAE,mBAAmB,EAAE,kCAA8B;AACjE,OAAO,EACL,0BAA0B,EAC1B,YAAY,GACb,kCAA8B;AAC/B,OAAO,EAAE,2BAA2B,EAAE,2BAAuB;AAC7D,OAAO,EACL,qBAAqB,EACrB,eAAe,EACf,8BAA8B,EAC9B,kBAAkB,EAClB,sBAAsB,GACvB,8BAA0B;AAC3B,OAAO,EAAE,wBAAwB,EAAE,2BAAuB;AAC1D,YAAY,EACV,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACtB,gBAAgB,EAChB,2BAA2B,GAC5B,0BAAsB;AACvB,OAAO,EAAE,gBAAgB,EAAE,0BAAsB;AACjD,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EACnB,uBAAuB,GACxB,8BAA0B;AAE3B,YAAY,EAAE,iBAAiB,EAAE,+BAA2B;AAC5D,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,EAClB,6BAA6B,EAC7B,sBAAsB,EACtB,oBAAoB,GACrB,+BAA2B"}
|
package/dist/index.mjs
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
export { getEthAccounts, setEthAccounts } from "./adapters/caip-permission-adapter-eth-accounts.mjs";
|
2
|
+
export { getPermittedEthChainIds, addPermittedEthChainId, setPermittedEthChainIds } from "./adapters/caip-permission-adapter-permittedChains.mjs";
|
3
|
+
export { getInternalScopesObject, getSessionScopes } from "./adapters/caip-permission-adapter-session-scopes.mjs";
|
4
|
+
export { validateAndNormalizeScopes, bucketScopes } from "./scope/authorization.mjs";
|
5
|
+
export { assertIsInternalScopeString } from "./scope/assert.mjs";
|
6
|
+
export { KnownWalletRpcMethods, KnownRpcMethods, KnownWalletNamespaceRpcMethods, KnownNotifications, KnownWalletScopeString } from "./scope/constants.mjs";
|
7
|
+
export { getSupportedScopeObjects } from "./scope/filter.mjs";
|
8
|
+
export { parseScopeString } from "./scope/types.mjs";
|
9
|
+
export { getUniqueArrayItems, normalizeScope, mergeScopeObject, mergeNormalizedScopes, mergeInternalScopes, normalizeAndMergeScopes } from "./scope/transform.mjs";
|
10
|
+
export { caip25CaveatBuilder, Caip25CaveatType, createCaip25Caveat, Caip25EndowmentPermissionName, caip25EndowmentBuilder, Caip25CaveatMutators } from "./caip25Permission.mjs";
|
11
|
+
//# sourceMappingURL=index.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,EACf,4DAAwD;AACzD,OAAO,EACL,uBAAuB,EACvB,sBAAsB,EACtB,uBAAuB,EACxB,+DAA2D;AAC5D,OAAO,EACL,uBAAuB,EACvB,gBAAgB,EACjB,8DAA0D;AAG3D,OAAO,EACL,0BAA0B,EAC1B,YAAY,EACb,kCAA8B;AAC/B,OAAO,EAAE,2BAA2B,EAAE,2BAAuB;AAC7D,OAAO,EACL,qBAAqB,EACrB,eAAe,EACf,8BAA8B,EAC9B,kBAAkB,EAClB,sBAAsB,EACvB,8BAA0B;AAC3B,OAAO,EAAE,wBAAwB,EAAE,2BAAuB;AAa1D,OAAO,EAAE,gBAAgB,EAAE,0BAAsB;AACjD,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EACnB,uBAAuB,EACxB,8BAA0B;AAG3B,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,EAClB,6BAA6B,EAC7B,sBAAsB,EACtB,oBAAoB,EACrB,+BAA2B","sourcesContent":["export {\n getEthAccounts,\n setEthAccounts,\n} from './adapters/caip-permission-adapter-eth-accounts';\nexport {\n getPermittedEthChainIds,\n addPermittedEthChainId,\n setPermittedEthChainIds,\n} from './adapters/caip-permission-adapter-permittedChains';\nexport {\n getInternalScopesObject,\n getSessionScopes,\n} from './adapters/caip-permission-adapter-session-scopes';\n\nexport type { Caip25Authorization } from './scope/authorization';\nexport {\n validateAndNormalizeScopes,\n bucketScopes,\n} from './scope/authorization';\nexport { assertIsInternalScopeString } from './scope/assert';\nexport {\n KnownWalletRpcMethods,\n KnownRpcMethods,\n KnownWalletNamespaceRpcMethods,\n KnownNotifications,\n KnownWalletScopeString,\n} from './scope/constants';\nexport { getSupportedScopeObjects } from './scope/filter';\nexport type {\n ExternalScopeString,\n ExternalScopeObject,\n ExternalScopesObject,\n InternalScopeString,\n InternalScopeObject,\n InternalScopesObject,\n NormalizedScopeObject,\n NormalizedScopesObject,\n ScopedProperties,\n NonWalletKnownCaipNamespace,\n} from './scope/types';\nexport { parseScopeString } from './scope/types';\nexport {\n getUniqueArrayItems,\n normalizeScope,\n mergeScopeObject,\n mergeNormalizedScopes,\n mergeInternalScopes,\n normalizeAndMergeScopes,\n} from './scope/transform';\n\nexport type { Caip25CaveatValue } from './caip25Permission';\nexport {\n caip25CaveatBuilder,\n Caip25CaveatType,\n createCaip25Caveat,\n Caip25EndowmentPermissionName,\n caip25EndowmentBuilder,\n Caip25CaveatMutators,\n} from './caip25Permission';\n"]}
|
@@ -0,0 +1,170 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.assertIsInternalScopesObject = exports.assertIsInternalScopeString = exports.assertIsExternalScopesObject = exports.assertScopesSupported = exports.assertScopeSupported = void 0;
|
4
|
+
const utils_1 = require("@metamask/utils");
|
5
|
+
const errors_1 = require("./errors.cjs");
|
6
|
+
const supported_1 = require("./supported.cjs");
|
7
|
+
/**
|
8
|
+
* Asserts that a scope string and its associated scope object are supported.
|
9
|
+
*
|
10
|
+
* @param scopeString - The scope string against which to assert support.
|
11
|
+
* @param scopeObject - The scope object against which to assert support.
|
12
|
+
* @param hooks - An object containing the following properties:
|
13
|
+
* @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.
|
14
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
15
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
16
|
+
*/
|
17
|
+
const assertScopeSupported = (scopeString, scopeObject, { isEvmChainIdSupported, isNonEvmScopeSupported, getNonEvmSupportedMethods, }) => {
|
18
|
+
const { methods, notifications } = scopeObject;
|
19
|
+
if (!(0, supported_1.isSupportedScopeString)(scopeString, {
|
20
|
+
isEvmChainIdSupported,
|
21
|
+
isNonEvmScopeSupported,
|
22
|
+
})) {
|
23
|
+
throw errors_1.Caip25Errors.requestedChainsNotSupportedError();
|
24
|
+
}
|
25
|
+
const allMethodsSupported = methods.every((method) => (0, supported_1.isSupportedMethod)(scopeString, method, { getNonEvmSupportedMethods }));
|
26
|
+
if (!allMethodsSupported) {
|
27
|
+
throw errors_1.Caip25Errors.requestedMethodsNotSupportedError();
|
28
|
+
}
|
29
|
+
if (notifications &&
|
30
|
+
!notifications.every((notification) => (0, supported_1.isSupportedNotification)(scopeString, notification))) {
|
31
|
+
throw errors_1.Caip25Errors.requestedNotificationsNotSupportedError();
|
32
|
+
}
|
33
|
+
};
|
34
|
+
exports.assertScopeSupported = assertScopeSupported;
|
35
|
+
/**
|
36
|
+
* Asserts that all scope strings and their associated scope objects are supported.
|
37
|
+
*
|
38
|
+
* @param scopes - The scopes object against which to assert support.
|
39
|
+
* @param hooks - An object containing the following properties:
|
40
|
+
* @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.
|
41
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
42
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
43
|
+
*/
|
44
|
+
const assertScopesSupported = (scopes, { isEvmChainIdSupported, isNonEvmScopeSupported, getNonEvmSupportedMethods, }) => {
|
45
|
+
for (const [scopeString, scopeObject] of Object.entries(scopes)) {
|
46
|
+
(0, exports.assertScopeSupported)(scopeString, scopeObject, {
|
47
|
+
isEvmChainIdSupported,
|
48
|
+
isNonEvmScopeSupported,
|
49
|
+
getNonEvmSupportedMethods,
|
50
|
+
});
|
51
|
+
}
|
52
|
+
};
|
53
|
+
exports.assertScopesSupported = assertScopesSupported;
|
54
|
+
/**
|
55
|
+
* Asserts that an object is a valid ExternalScopeObject.
|
56
|
+
*
|
57
|
+
* @param obj - The object to assert.
|
58
|
+
*/
|
59
|
+
function assertIsExternalScopeObject(obj) {
|
60
|
+
if (typeof obj !== 'object' || obj === null) {
|
61
|
+
throw new Error('ExternalScopeObject must be an object');
|
62
|
+
}
|
63
|
+
if ((0, utils_1.hasProperty)(obj, 'references')) {
|
64
|
+
if (!Array.isArray(obj.references) ||
|
65
|
+
!obj.references.every(utils_1.isCaipReference)) {
|
66
|
+
throw new Error('ExternalScopeObject.references must be an array of CaipReference');
|
67
|
+
}
|
68
|
+
}
|
69
|
+
if ((0, utils_1.hasProperty)(obj, 'accounts')) {
|
70
|
+
if (!Array.isArray(obj.accounts) || !obj.accounts.every(utils_1.isCaipAccountId)) {
|
71
|
+
throw new Error('ExternalScopeObject.accounts must be an array of CaipAccountId');
|
72
|
+
}
|
73
|
+
}
|
74
|
+
if ((0, utils_1.hasProperty)(obj, 'methods')) {
|
75
|
+
if (!Array.isArray(obj.methods) ||
|
76
|
+
!obj.methods.every((method) => typeof method === 'string')) {
|
77
|
+
throw new Error('ExternalScopeObject.methods must be an array of strings');
|
78
|
+
}
|
79
|
+
}
|
80
|
+
if ((0, utils_1.hasProperty)(obj, 'notifications')) {
|
81
|
+
if (!Array.isArray(obj.notifications) ||
|
82
|
+
!obj.notifications.every((notification) => typeof notification === 'string')) {
|
83
|
+
throw new Error('ExternalScopeObject.notifications must be an array of strings');
|
84
|
+
}
|
85
|
+
}
|
86
|
+
if ((0, utils_1.hasProperty)(obj, 'rpcDocuments')) {
|
87
|
+
if (!Array.isArray(obj.rpcDocuments) ||
|
88
|
+
!obj.rpcDocuments.every((doc) => typeof doc === 'string')) {
|
89
|
+
throw new Error('ExternalScopeObject.rpcDocuments must be an array of strings');
|
90
|
+
}
|
91
|
+
}
|
92
|
+
if ((0, utils_1.hasProperty)(obj, 'rpcEndpoints')) {
|
93
|
+
if (!Array.isArray(obj.rpcEndpoints) ||
|
94
|
+
!obj.rpcEndpoints.every((endpoint) => typeof endpoint === 'string')) {
|
95
|
+
throw new Error('ExternalScopeObject.rpcEndpoints must be an array of strings');
|
96
|
+
}
|
97
|
+
}
|
98
|
+
}
|
99
|
+
/**
|
100
|
+
* Asserts that a scope string is a valid ExternalScopeString.
|
101
|
+
*
|
102
|
+
* @param scopeString - The scope string to assert.
|
103
|
+
*/
|
104
|
+
function assertIsExternalScopeString(scopeString) {
|
105
|
+
if (typeof scopeString !== 'string' ||
|
106
|
+
(!(0, utils_1.isCaipNamespace)(scopeString) && !(0, utils_1.isCaipChainId)(scopeString))) {
|
107
|
+
throw new Error('scopeString is not a valid ExternalScopeString');
|
108
|
+
}
|
109
|
+
}
|
110
|
+
/**
|
111
|
+
* Asserts that an object is a valid ExternalScopesObject.
|
112
|
+
*
|
113
|
+
* @param obj - The object to assert.
|
114
|
+
*/
|
115
|
+
function assertIsExternalScopesObject(obj) {
|
116
|
+
if (typeof obj !== 'object' || obj === null) {
|
117
|
+
throw new Error('ExternalScopesObject must be an object');
|
118
|
+
}
|
119
|
+
for (const [scopeString, scopeObject] of Object.entries(obj)) {
|
120
|
+
assertIsExternalScopeString(scopeString);
|
121
|
+
assertIsExternalScopeObject(scopeObject);
|
122
|
+
}
|
123
|
+
}
|
124
|
+
exports.assertIsExternalScopesObject = assertIsExternalScopesObject;
|
125
|
+
/**
|
126
|
+
* Asserts that an object is a valid InternalScopeObject.
|
127
|
+
*
|
128
|
+
* @param obj - The object to assert.
|
129
|
+
*/
|
130
|
+
function assertIsInternalScopeObject(obj) {
|
131
|
+
if (typeof obj !== 'object' || obj === null) {
|
132
|
+
throw new Error('InternalScopeObject must be an object');
|
133
|
+
}
|
134
|
+
if (!(0, utils_1.hasProperty)(obj, 'accounts') ||
|
135
|
+
!Array.isArray(obj.accounts) ||
|
136
|
+
!obj.accounts.every(utils_1.isCaipAccountId)) {
|
137
|
+
throw new Error('InternalScopeObject.accounts must be an array of CaipAccountId');
|
138
|
+
}
|
139
|
+
}
|
140
|
+
/**
|
141
|
+
* Asserts that a scope string is a valid InternalScopeString.
|
142
|
+
*
|
143
|
+
* @param scopeString - The scope string to assert.
|
144
|
+
*/
|
145
|
+
function assertIsInternalScopeString(scopeString) {
|
146
|
+
if (typeof scopeString !== 'string' ||
|
147
|
+
// `InternalScopeString` is defined as either `KnownCaipNamespace.Wallet` or
|
148
|
+
// `CaipChainId`, so our conditions intentionally match the type.
|
149
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
|
150
|
+
(scopeString !== utils_1.KnownCaipNamespace.Wallet && !(0, utils_1.isCaipChainId)(scopeString))) {
|
151
|
+
throw new Error('scopeString is not a valid InternalScopeString');
|
152
|
+
}
|
153
|
+
}
|
154
|
+
exports.assertIsInternalScopeString = assertIsInternalScopeString;
|
155
|
+
/**
|
156
|
+
* Asserts that an object is a valid InternalScopesObject.
|
157
|
+
*
|
158
|
+
* @param obj - The object to assert.
|
159
|
+
*/
|
160
|
+
function assertIsInternalScopesObject(obj) {
|
161
|
+
if (typeof obj !== 'object' || obj === null) {
|
162
|
+
throw new Error('InternalScopesObject must be an object');
|
163
|
+
}
|
164
|
+
for (const [scopeString, scopeObject] of Object.entries(obj)) {
|
165
|
+
assertIsInternalScopeString(scopeString);
|
166
|
+
assertIsInternalScopeObject(scopeObject);
|
167
|
+
}
|
168
|
+
}
|
169
|
+
exports.assertIsInternalScopesObject = assertIsInternalScopesObject;
|
170
|
+
//# sourceMappingURL=assert.cjs.map
|