@metamask-previews/gator-permissions-controller 0.1.0-preview-0c2c1149 → 0.1.0-preview-463efec
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 +2 -0
- package/dist/GatorPermissionsController.cjs +68 -1
- package/dist/GatorPermissionsController.cjs.map +1 -1
- package/dist/GatorPermissionsController.d.cts +41 -2
- package/dist/GatorPermissionsController.d.cts.map +1 -1
- package/dist/GatorPermissionsController.d.mts +41 -2
- package/dist/GatorPermissionsController.d.mts.map +1 -1
- package/dist/GatorPermissionsController.mjs +68 -1
- package/dist/GatorPermissionsController.mjs.map +1 -1
- package/dist/decodePermission/decodePermission.cjs +196 -0
- package/dist/decodePermission/decodePermission.cjs.map +1 -0
- package/dist/decodePermission/decodePermission.d.cts +87 -0
- package/dist/decodePermission/decodePermission.d.cts.map +1 -0
- package/dist/decodePermission/decodePermission.d.mts +87 -0
- package/dist/decodePermission/decodePermission.d.mts.map +1 -0
- package/dist/decodePermission/decodePermission.mjs +190 -0
- package/dist/decodePermission/decodePermission.mjs.map +1 -0
- package/dist/decodePermission/index.cjs +8 -0
- package/dist/decodePermission/index.cjs.map +1 -0
- package/dist/decodePermission/index.d.cts +3 -0
- package/dist/decodePermission/index.d.cts.map +1 -0
- package/dist/decodePermission/index.d.mts +3 -0
- package/dist/decodePermission/index.d.mts.map +1 -0
- package/dist/decodePermission/index.mjs +2 -0
- package/dist/decodePermission/index.mjs.map +1 -0
- package/dist/decodePermission/types.cjs +3 -0
- package/dist/decodePermission/types.cjs.map +1 -0
- package/dist/decodePermission/types.d.cts +23 -0
- package/dist/decodePermission/types.d.cts.map +1 -0
- package/dist/decodePermission/types.d.mts +23 -0
- package/dist/decodePermission/types.d.mts.map +1 -0
- package/dist/decodePermission/types.mjs +2 -0
- package/dist/decodePermission/types.mjs.map +1 -0
- package/dist/decodePermission/utils.cjs +181 -0
- package/dist/decodePermission/utils.cjs.map +1 -0
- package/dist/decodePermission/utils.d.cts +83 -0
- package/dist/decodePermission/utils.d.cts.map +1 -0
- package/dist/decodePermission/utils.d.mts +83 -0
- package/dist/decodePermission/utils.d.mts.map +1 -0
- package/dist/decodePermission/utils.mjs +173 -0
- package/dist/decodePermission/utils.mjs.map +1 -0
- package/dist/errors.cjs +22 -1
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts +10 -0
- package/dist/errors.d.cts.map +1 -1
- package/dist/errors.d.mts +10 -0
- package/dist/errors.d.mts.map +1 -1
- package/dist/errors.mjs +19 -0
- package/dist/errors.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/types.cjs +2 -0
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +8 -1
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +8 -1
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -1
- package/package.json +4 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/decodePermission/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,6BAA6B,EAC7B,0BAA0B,EAC1B,4BAA4B,GAC7B,+BAA2B;AAE5B,YAAY,EAAE,iBAAiB,EAAE,oBAAgB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/decodePermission/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,6BAA6B,EAC7B,0BAA0B,EAC1B,4BAA4B,EAC7B,+BAA2B","sourcesContent":["export {\n identifyPermissionByEnforcers,\n getPermissionDataAndExpiry,\n reconstructDecodedPermission,\n} from './decodePermission';\n\nexport type { DecodedPermission } from './types';\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.cjs","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n PermissionRequest,\n PermissionTypes,\n Signer,\n} from '@metamask/7715-permission-types';\nimport type { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';\n\nexport type DeployedContractsByName =\n (typeof DELEGATOR_CONTRACTS)[number][number];\n\n// This is a somewhat convoluted type - it includes all of the fields that are decoded from the permission context.\n/**\n * A partially reconstructed permission object decoded from a permission context.\n *\n * This mirrors the shape of {@link PermissionRequest} for fields that can be\n * deterministically recovered from the encoded permission context, and it\n * augments the result with an explicit `expiry` property derived from the\n * `TimestampEnforcer` terms, as well as the `origin` property.\n */\nexport type DecodedPermission = Pick<\n PermissionRequest<Signer, PermissionTypes>,\n 'chainId' | 'address' | 'signer'\n> & {\n permission: Omit<\n PermissionRequest<Signer, PermissionTypes>['permission'],\n 'isAdjustmentAllowed'\n > & {\n // PermissionRequest type does not work well without the specific permission type, so we amend it here\n justification?: string;\n };\n expiry: number | null;\n origin: string;\n};\n\n/**\n * Supported permission type identifiers that can be decoded from a permission context.\n */\nexport type PermissionType = DecodedPermission['permission']['type'];\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PermissionRequest, PermissionTypes, Signer } from "@metamask/7715-permission-types";
|
|
2
|
+
import type { DELEGATOR_CONTRACTS } from "@metamask/delegation-deployments";
|
|
3
|
+
export type DeployedContractsByName = (typeof DELEGATOR_CONTRACTS)[number][number];
|
|
4
|
+
/**
|
|
5
|
+
* A partially reconstructed permission object decoded from a permission context.
|
|
6
|
+
*
|
|
7
|
+
* This mirrors the shape of {@link PermissionRequest} for fields that can be
|
|
8
|
+
* deterministically recovered from the encoded permission context, and it
|
|
9
|
+
* augments the result with an explicit `expiry` property derived from the
|
|
10
|
+
* `TimestampEnforcer` terms, as well as the `origin` property.
|
|
11
|
+
*/
|
|
12
|
+
export type DecodedPermission = Pick<PermissionRequest<Signer, PermissionTypes>, 'chainId' | 'address' | 'signer'> & {
|
|
13
|
+
permission: Omit<PermissionRequest<Signer, PermissionTypes>['permission'], 'isAdjustmentAllowed'> & {
|
|
14
|
+
justification?: string;
|
|
15
|
+
};
|
|
16
|
+
expiry: number | null;
|
|
17
|
+
origin: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Supported permission type identifiers that can be decoded from a permission context.
|
|
21
|
+
*/
|
|
22
|
+
export type PermissionType = DecodedPermission['permission']['type'];
|
|
23
|
+
//# sourceMappingURL=types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,eAAe,EACf,MAAM,EACP,wCAAwC;AACzC,OAAO,KAAK,EAAE,mBAAmB,EAAE,yCAAyC;AAE5E,MAAM,MAAM,uBAAuB,GACjC,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AAG/C;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAClC,iBAAiB,CAAC,MAAM,EAAE,eAAe,CAAC,EAC1C,SAAS,GAAG,SAAS,GAAG,QAAQ,CACjC,GAAG;IACF,UAAU,EAAE,IAAI,CACd,iBAAiB,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,YAAY,CAAC,EACxD,qBAAqB,CACtB,GAAG;QAEF,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB,CAAC;IACF,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,iBAAiB,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PermissionRequest, PermissionTypes, Signer } from "@metamask/7715-permission-types";
|
|
2
|
+
import type { DELEGATOR_CONTRACTS } from "@metamask/delegation-deployments";
|
|
3
|
+
export type DeployedContractsByName = (typeof DELEGATOR_CONTRACTS)[number][number];
|
|
4
|
+
/**
|
|
5
|
+
* A partially reconstructed permission object decoded from a permission context.
|
|
6
|
+
*
|
|
7
|
+
* This mirrors the shape of {@link PermissionRequest} for fields that can be
|
|
8
|
+
* deterministically recovered from the encoded permission context, and it
|
|
9
|
+
* augments the result with an explicit `expiry` property derived from the
|
|
10
|
+
* `TimestampEnforcer` terms, as well as the `origin` property.
|
|
11
|
+
*/
|
|
12
|
+
export type DecodedPermission = Pick<PermissionRequest<Signer, PermissionTypes>, 'chainId' | 'address' | 'signer'> & {
|
|
13
|
+
permission: Omit<PermissionRequest<Signer, PermissionTypes>['permission'], 'isAdjustmentAllowed'> & {
|
|
14
|
+
justification?: string;
|
|
15
|
+
};
|
|
16
|
+
expiry: number | null;
|
|
17
|
+
origin: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Supported permission type identifiers that can be decoded from a permission context.
|
|
21
|
+
*/
|
|
22
|
+
export type PermissionType = DecodedPermission['permission']['type'];
|
|
23
|
+
//# sourceMappingURL=types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,eAAe,EACf,MAAM,EACP,wCAAwC;AACzC,OAAO,KAAK,EAAE,mBAAmB,EAAE,yCAAyC;AAE5E,MAAM,MAAM,uBAAuB,GACjC,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AAG/C;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAClC,iBAAiB,CAAC,MAAM,EAAE,eAAe,CAAC,EAC1C,SAAS,GAAG,SAAS,GAAG,QAAQ,CACjC,GAAG;IACF,UAAU,EAAE,IAAI,CACd,iBAAiB,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,YAAY,CAAC,EACxD,qBAAqB,CACtB,GAAG;QAEF,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB,CAAC;IACF,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,iBAAiB,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n PermissionRequest,\n PermissionTypes,\n Signer,\n} from '@metamask/7715-permission-types';\nimport type { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';\n\nexport type DeployedContractsByName =\n (typeof DELEGATOR_CONTRACTS)[number][number];\n\n// This is a somewhat convoluted type - it includes all of the fields that are decoded from the permission context.\n/**\n * A partially reconstructed permission object decoded from a permission context.\n *\n * This mirrors the shape of {@link PermissionRequest} for fields that can be\n * deterministically recovered from the encoded permission context, and it\n * augments the result with an explicit `expiry` property derived from the\n * `TimestampEnforcer` terms, as well as the `origin` property.\n */\nexport type DecodedPermission = Pick<\n PermissionRequest<Signer, PermissionTypes>,\n 'chainId' | 'address' | 'signer'\n> & {\n permission: Omit<\n PermissionRequest<Signer, PermissionTypes>['permission'],\n 'isAdjustmentAllowed'\n > & {\n // PermissionRequest type does not work well without the specific permission type, so we amend it here\n justification?: string;\n };\n expiry: number | null;\n origin: string;\n};\n\n/**\n * Supported permission type identifiers that can be decoded from a permission context.\n */\nexport type PermissionType = DecodedPermission['permission']['type'];\n"]}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.splitHex = exports.getTermsByEnforcer = exports.isSubset = exports.createPermissionRulesForChainId = exports.getChecksumEnforcersByChainId = void 0;
|
|
4
|
+
const utils_1 = require("@metamask/utils");
|
|
5
|
+
/**
|
|
6
|
+
* The names of the enforcer contracts for each permission type.
|
|
7
|
+
*/
|
|
8
|
+
const ENFORCER_CONTRACT_NAMES = {
|
|
9
|
+
ERC20PeriodTransferEnforcer: 'ERC20PeriodTransferEnforcer',
|
|
10
|
+
ERC20StreamingEnforcer: 'ERC20StreamingEnforcer',
|
|
11
|
+
ExactCalldataEnforcer: 'ExactCalldataEnforcer',
|
|
12
|
+
NativeTokenPeriodTransferEnforcer: 'NativeTokenPeriodTransferEnforcer',
|
|
13
|
+
NativeTokenStreamingEnforcer: 'NativeTokenStreamingEnforcer',
|
|
14
|
+
TimestampEnforcer: 'TimestampEnforcer',
|
|
15
|
+
ValueLteEnforcer: 'ValueLteEnforcer',
|
|
16
|
+
NonceEnforcer: 'NonceEnforcer',
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Resolves and returns checksummed addresses of all known enforcer contracts
|
|
20
|
+
* for a given `chainId` under the current delegation framework version.
|
|
21
|
+
*
|
|
22
|
+
* @param contracts - The deployed contracts for the chain.
|
|
23
|
+
* @returns An object mapping enforcer names to checksummed contract addresses.
|
|
24
|
+
* @throws If the chain or an expected enforcer contract is not found.
|
|
25
|
+
*/
|
|
26
|
+
const getChecksumEnforcersByChainId = (contracts) => {
|
|
27
|
+
const getChecksumContractAddress = (contractName) => {
|
|
28
|
+
const address = contracts[contractName];
|
|
29
|
+
if (!address) {
|
|
30
|
+
throw new Error(`Contract not found: ${contractName}`);
|
|
31
|
+
}
|
|
32
|
+
return (0, utils_1.getChecksumAddress)(address);
|
|
33
|
+
};
|
|
34
|
+
// permission type specific enforcers
|
|
35
|
+
const erc20StreamingEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ERC20StreamingEnforcer);
|
|
36
|
+
const erc20PeriodicEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ERC20PeriodTransferEnforcer);
|
|
37
|
+
const nativeTokenStreamingEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NativeTokenStreamingEnforcer);
|
|
38
|
+
const nativeTokenPeriodicEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NativeTokenPeriodTransferEnforcer);
|
|
39
|
+
// general enforcers
|
|
40
|
+
const exactCalldataEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ExactCalldataEnforcer);
|
|
41
|
+
const valueLteEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ValueLteEnforcer);
|
|
42
|
+
const timestampEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.TimestampEnforcer);
|
|
43
|
+
const nonceEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NonceEnforcer);
|
|
44
|
+
return {
|
|
45
|
+
erc20StreamingEnforcer,
|
|
46
|
+
erc20PeriodicEnforcer,
|
|
47
|
+
nativeTokenStreamingEnforcer,
|
|
48
|
+
nativeTokenPeriodicEnforcer,
|
|
49
|
+
exactCalldataEnforcer,
|
|
50
|
+
valueLteEnforcer,
|
|
51
|
+
timestampEnforcer,
|
|
52
|
+
nonceEnforcer,
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
exports.getChecksumEnforcersByChainId = getChecksumEnforcersByChainId;
|
|
56
|
+
/**
|
|
57
|
+
* Builds the canonical set of permission matching rules for a chain.
|
|
58
|
+
*
|
|
59
|
+
* Each rule specifies the `permissionType`, the set of `requiredEnforcers`
|
|
60
|
+
* that must be present, and the set of `allowedEnforcers` that may appear in
|
|
61
|
+
* addition to the required set.
|
|
62
|
+
*
|
|
63
|
+
* @param contracts - The deployed contracts for the chain.
|
|
64
|
+
* @returns A list of permission rules used to identify permission types.
|
|
65
|
+
* @throws Propagates any errors from resolving enforcer addresses.
|
|
66
|
+
*/
|
|
67
|
+
const createPermissionRulesForChainId = (contracts) => {
|
|
68
|
+
const { erc20StreamingEnforcer, erc20PeriodicEnforcer, nativeTokenStreamingEnforcer, nativeTokenPeriodicEnforcer, exactCalldataEnforcer, valueLteEnforcer, timestampEnforcer, nonceEnforcer, } = (0, exports.getChecksumEnforcersByChainId)(contracts);
|
|
69
|
+
// the allowed enforcers are the same for all permission types
|
|
70
|
+
const allowedEnforcers = new Set([timestampEnforcer]);
|
|
71
|
+
const permissionRules = [
|
|
72
|
+
{
|
|
73
|
+
requiredEnforcers: new Set([
|
|
74
|
+
nativeTokenStreamingEnforcer,
|
|
75
|
+
exactCalldataEnforcer,
|
|
76
|
+
nonceEnforcer,
|
|
77
|
+
]),
|
|
78
|
+
allowedEnforcers,
|
|
79
|
+
permissionType: 'native-token-stream',
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
requiredEnforcers: new Set([
|
|
83
|
+
nativeTokenPeriodicEnforcer,
|
|
84
|
+
exactCalldataEnforcer,
|
|
85
|
+
nonceEnforcer,
|
|
86
|
+
]),
|
|
87
|
+
allowedEnforcers,
|
|
88
|
+
permissionType: 'native-token-periodic',
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
requiredEnforcers: new Set([
|
|
92
|
+
erc20StreamingEnforcer,
|
|
93
|
+
valueLteEnforcer,
|
|
94
|
+
nonceEnforcer,
|
|
95
|
+
]),
|
|
96
|
+
allowedEnforcers,
|
|
97
|
+
permissionType: 'erc20-token-stream',
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
requiredEnforcers: new Set([
|
|
101
|
+
erc20PeriodicEnforcer,
|
|
102
|
+
valueLteEnforcer,
|
|
103
|
+
nonceEnforcer,
|
|
104
|
+
]),
|
|
105
|
+
allowedEnforcers,
|
|
106
|
+
permissionType: 'erc20-token-periodic',
|
|
107
|
+
},
|
|
108
|
+
];
|
|
109
|
+
return permissionRules;
|
|
110
|
+
};
|
|
111
|
+
exports.createPermissionRulesForChainId = createPermissionRulesForChainId;
|
|
112
|
+
/**
|
|
113
|
+
* Determines whether all elements of `subset` are contained within `superset`.
|
|
114
|
+
*
|
|
115
|
+
* @param subset - The candidate subset to test.
|
|
116
|
+
* @param superset - The set expected to contain all elements of `subset`.
|
|
117
|
+
* @returns `true` if `subset` ⊆ `superset`, otherwise `false`.
|
|
118
|
+
*/
|
|
119
|
+
const isSubset = (subset, superset) => {
|
|
120
|
+
for (const x of subset) {
|
|
121
|
+
if (!superset.has(x)) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return true;
|
|
126
|
+
};
|
|
127
|
+
exports.isSubset = isSubset;
|
|
128
|
+
/**
|
|
129
|
+
* Gets the terms for a given enforcer from a list of caveats.
|
|
130
|
+
*
|
|
131
|
+
* @param args - The arguments to this function.
|
|
132
|
+
* @param args.throwIfNotFound - Whether to throw an error if no matching enforcer is found. Default is true.
|
|
133
|
+
* @param args.caveats - The list of caveats to search.
|
|
134
|
+
* @param args.enforcer - The enforcer to search for.
|
|
135
|
+
* @returns The terms for the given enforcer.
|
|
136
|
+
*/
|
|
137
|
+
function getTermsByEnforcer({ caveats, enforcer, throwIfNotFound, }) {
|
|
138
|
+
const matchingCaveats = caveats.filter((caveat) => caveat.enforcer === enforcer);
|
|
139
|
+
if (matchingCaveats.length === 0) {
|
|
140
|
+
if (throwIfNotFound ?? true) {
|
|
141
|
+
throw new Error('Invalid caveats');
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
if (matchingCaveats.length > 1) {
|
|
146
|
+
throw new Error('Invalid caveats');
|
|
147
|
+
}
|
|
148
|
+
return matchingCaveats[0].terms;
|
|
149
|
+
}
|
|
150
|
+
exports.getTermsByEnforcer = getTermsByEnforcer;
|
|
151
|
+
/**
|
|
152
|
+
* Splits a 0x-prefixed hex string into parts according to the provided byte lengths.
|
|
153
|
+
*
|
|
154
|
+
* Each entry in `lengths` represents a part length in bytes; internally this is
|
|
155
|
+
* multiplied by 2 to derive the number of hexadecimal characters to slice. The
|
|
156
|
+
* returned substrings do not include the `0x` prefix and preserve leading zeros.
|
|
157
|
+
*
|
|
158
|
+
* Note: This function does not perform input validation (e.g., verifying the
|
|
159
|
+
* payload length equals the sum of requested lengths). Callers are expected to
|
|
160
|
+
* provide well-formed inputs.
|
|
161
|
+
*
|
|
162
|
+
* Example:
|
|
163
|
+
* splitHex('0x12345678', [1, 3]) => ['0x12', '0x345678']
|
|
164
|
+
*
|
|
165
|
+
* @param value - The 0x-prefixed hex string to split.
|
|
166
|
+
* @param lengths - The lengths of each part, in bytes.
|
|
167
|
+
* @returns An array of hex substrings (each with `0x` prefix), one for each part.
|
|
168
|
+
*/
|
|
169
|
+
function splitHex(value, lengths) {
|
|
170
|
+
let start = 2;
|
|
171
|
+
const parts = [];
|
|
172
|
+
for (const partLength of lengths) {
|
|
173
|
+
const partCharLength = partLength * 2;
|
|
174
|
+
const part = value.slice(start, start + partCharLength);
|
|
175
|
+
start += partCharLength;
|
|
176
|
+
parts.push(`0x${part}`);
|
|
177
|
+
}
|
|
178
|
+
return parts;
|
|
179
|
+
}
|
|
180
|
+
exports.splitHex = splitHex;
|
|
181
|
+
//# sourceMappingURL=utils.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":"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","sourcesContent":["import type { Caveat } from '@metamask/delegation-core';\nimport { getChecksumAddress, type Hex } from '@metamask/utils';\n\nimport type { DeployedContractsByName, PermissionType } from './types';\n\n/**\n * A rule that defines the required and allowed enforcers for a permission type.\n */\nexport type PermissionRule = {\n permissionType: PermissionType;\n requiredEnforcers: Set<Hex>;\n allowedEnforcers: Set<Hex>;\n};\n\n/**\n * The names of the enforcer contracts for each permission type.\n */\nconst ENFORCER_CONTRACT_NAMES = {\n ERC20PeriodTransferEnforcer: 'ERC20PeriodTransferEnforcer',\n ERC20StreamingEnforcer: 'ERC20StreamingEnforcer',\n ExactCalldataEnforcer: 'ExactCalldataEnforcer',\n NativeTokenPeriodTransferEnforcer: 'NativeTokenPeriodTransferEnforcer',\n NativeTokenStreamingEnforcer: 'NativeTokenStreamingEnforcer',\n TimestampEnforcer: 'TimestampEnforcer',\n ValueLteEnforcer: 'ValueLteEnforcer',\n NonceEnforcer: 'NonceEnforcer',\n};\n\n/**\n * Resolves and returns checksummed addresses of all known enforcer contracts\n * for a given `chainId` under the current delegation framework version.\n *\n * @param contracts - The deployed contracts for the chain.\n * @returns An object mapping enforcer names to checksummed contract addresses.\n * @throws If the chain or an expected enforcer contract is not found.\n */\nexport const getChecksumEnforcersByChainId = (\n contracts: DeployedContractsByName,\n) => {\n const getChecksumContractAddress = (contractName: string) => {\n const address = contracts[contractName];\n\n if (!address) {\n throw new Error(`Contract not found: ${contractName}`);\n }\n\n return getChecksumAddress(address);\n };\n\n // permission type specific enforcers\n const erc20StreamingEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.ERC20StreamingEnforcer,\n );\n const erc20PeriodicEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.ERC20PeriodTransferEnforcer,\n );\n const nativeTokenStreamingEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.NativeTokenStreamingEnforcer,\n );\n const nativeTokenPeriodicEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.NativeTokenPeriodTransferEnforcer,\n );\n\n // general enforcers\n const exactCalldataEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.ExactCalldataEnforcer,\n );\n const valueLteEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.ValueLteEnforcer,\n );\n const timestampEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.TimestampEnforcer,\n );\n const nonceEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.NonceEnforcer,\n );\n\n return {\n erc20StreamingEnforcer,\n erc20PeriodicEnforcer,\n nativeTokenStreamingEnforcer,\n nativeTokenPeriodicEnforcer,\n exactCalldataEnforcer,\n valueLteEnforcer,\n timestampEnforcer,\n nonceEnforcer,\n };\n};\n\n/**\n * Builds the canonical set of permission matching rules for a chain.\n *\n * Each rule specifies the `permissionType`, the set of `requiredEnforcers`\n * that must be present, and the set of `allowedEnforcers` that may appear in\n * addition to the required set.\n *\n * @param contracts - The deployed contracts for the chain.\n * @returns A list of permission rules used to identify permission types.\n * @throws Propagates any errors from resolving enforcer addresses.\n */\nexport const createPermissionRulesForChainId: (\n contracts: DeployedContractsByName,\n) => PermissionRule[] = (contracts: DeployedContractsByName) => {\n const {\n erc20StreamingEnforcer,\n erc20PeriodicEnforcer,\n nativeTokenStreamingEnforcer,\n nativeTokenPeriodicEnforcer,\n exactCalldataEnforcer,\n valueLteEnforcer,\n timestampEnforcer,\n nonceEnforcer,\n } = getChecksumEnforcersByChainId(contracts);\n\n // the allowed enforcers are the same for all permission types\n const allowedEnforcers = new Set<Hex>([timestampEnforcer]);\n\n const permissionRules: PermissionRule[] = [\n {\n requiredEnforcers: new Set<Hex>([\n nativeTokenStreamingEnforcer,\n exactCalldataEnforcer,\n nonceEnforcer,\n ]),\n allowedEnforcers,\n permissionType: 'native-token-stream',\n },\n {\n requiredEnforcers: new Set<Hex>([\n nativeTokenPeriodicEnforcer,\n exactCalldataEnforcer,\n nonceEnforcer,\n ]),\n allowedEnforcers,\n permissionType: 'native-token-periodic',\n },\n {\n requiredEnforcers: new Set<Hex>([\n erc20StreamingEnforcer,\n valueLteEnforcer,\n nonceEnforcer,\n ]),\n allowedEnforcers,\n permissionType: 'erc20-token-stream',\n },\n {\n requiredEnforcers: new Set<Hex>([\n erc20PeriodicEnforcer,\n valueLteEnforcer,\n nonceEnforcer,\n ]),\n allowedEnforcers,\n permissionType: 'erc20-token-periodic',\n },\n ];\n\n return permissionRules;\n};\n\n/**\n * Determines whether all elements of `subset` are contained within `superset`.\n *\n * @param subset - The candidate subset to test.\n * @param superset - The set expected to contain all elements of `subset`.\n * @returns `true` if `subset` ⊆ `superset`, otherwise `false`.\n */\nexport const isSubset = <T>(subset: Set<T>, superset: Set<T>): boolean => {\n for (const x of subset) {\n if (!superset.has(x)) {\n return false;\n }\n }\n return true;\n};\n\n/**\n * Gets the terms for a given enforcer from a list of caveats.\n *\n * @param args - The arguments to this function.\n * @param args.throwIfNotFound - Whether to throw an error if no matching enforcer is found. Default is true.\n * @param args.caveats - The list of caveats to search.\n * @param args.enforcer - The enforcer to search for.\n * @returns The terms for the given enforcer.\n */\nexport function getTermsByEnforcer<TThrowIfNotFound extends boolean = true>({\n caveats,\n enforcer,\n throwIfNotFound,\n}: {\n caveats: Caveat<Hex>[];\n enforcer: Hex;\n throwIfNotFound?: TThrowIfNotFound;\n}): TThrowIfNotFound extends true ? Hex : Hex | null {\n const matchingCaveats = caveats.filter(\n (caveat) => caveat.enforcer === enforcer,\n );\n\n if (matchingCaveats.length === 0) {\n if (throwIfNotFound ?? true) {\n throw new Error('Invalid caveats');\n }\n return null as TThrowIfNotFound extends true ? Hex : Hex | null;\n }\n\n if (matchingCaveats.length > 1) {\n throw new Error('Invalid caveats');\n }\n\n return matchingCaveats[0].terms;\n}\n\n/**\n * Splits a 0x-prefixed hex string into parts according to the provided byte lengths.\n *\n * Each entry in `lengths` represents a part length in bytes; internally this is\n * multiplied by 2 to derive the number of hexadecimal characters to slice. The\n * returned substrings do not include the `0x` prefix and preserve leading zeros.\n *\n * Note: This function does not perform input validation (e.g., verifying the\n * payload length equals the sum of requested lengths). Callers are expected to\n * provide well-formed inputs.\n *\n * Example:\n * splitHex('0x12345678', [1, 3]) => ['0x12', '0x345678']\n *\n * @param value - The 0x-prefixed hex string to split.\n * @param lengths - The lengths of each part, in bytes.\n * @returns An array of hex substrings (each with `0x` prefix), one for each part.\n */\nexport function splitHex(value: Hex, lengths: number[]): Hex[] {\n let start = 2;\n const parts: Hex[] = [];\n for (const partLength of lengths) {\n const partCharLength = partLength * 2;\n const part = value.slice(start, start + partCharLength);\n start += partCharLength;\n parts.push(`0x${part}` as Hex);\n }\n return parts;\n}\n"]}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { Caveat } from "@metamask/delegation-core";
|
|
2
|
+
import { type Hex } from "@metamask/utils";
|
|
3
|
+
import type { DeployedContractsByName, PermissionType } from "./types.cjs";
|
|
4
|
+
/**
|
|
5
|
+
* A rule that defines the required and allowed enforcers for a permission type.
|
|
6
|
+
*/
|
|
7
|
+
export type PermissionRule = {
|
|
8
|
+
permissionType: PermissionType;
|
|
9
|
+
requiredEnforcers: Set<Hex>;
|
|
10
|
+
allowedEnforcers: Set<Hex>;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Resolves and returns checksummed addresses of all known enforcer contracts
|
|
14
|
+
* for a given `chainId` under the current delegation framework version.
|
|
15
|
+
*
|
|
16
|
+
* @param contracts - The deployed contracts for the chain.
|
|
17
|
+
* @returns An object mapping enforcer names to checksummed contract addresses.
|
|
18
|
+
* @throws If the chain or an expected enforcer contract is not found.
|
|
19
|
+
*/
|
|
20
|
+
export declare const getChecksumEnforcersByChainId: (contracts: DeployedContractsByName) => {
|
|
21
|
+
erc20StreamingEnforcer: `0x${string}`;
|
|
22
|
+
erc20PeriodicEnforcer: `0x${string}`;
|
|
23
|
+
nativeTokenStreamingEnforcer: `0x${string}`;
|
|
24
|
+
nativeTokenPeriodicEnforcer: `0x${string}`;
|
|
25
|
+
exactCalldataEnforcer: `0x${string}`;
|
|
26
|
+
valueLteEnforcer: `0x${string}`;
|
|
27
|
+
timestampEnforcer: `0x${string}`;
|
|
28
|
+
nonceEnforcer: `0x${string}`;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Builds the canonical set of permission matching rules for a chain.
|
|
32
|
+
*
|
|
33
|
+
* Each rule specifies the `permissionType`, the set of `requiredEnforcers`
|
|
34
|
+
* that must be present, and the set of `allowedEnforcers` that may appear in
|
|
35
|
+
* addition to the required set.
|
|
36
|
+
*
|
|
37
|
+
* @param contracts - The deployed contracts for the chain.
|
|
38
|
+
* @returns A list of permission rules used to identify permission types.
|
|
39
|
+
* @throws Propagates any errors from resolving enforcer addresses.
|
|
40
|
+
*/
|
|
41
|
+
export declare const createPermissionRulesForChainId: (contracts: DeployedContractsByName) => PermissionRule[];
|
|
42
|
+
/**
|
|
43
|
+
* Determines whether all elements of `subset` are contained within `superset`.
|
|
44
|
+
*
|
|
45
|
+
* @param subset - The candidate subset to test.
|
|
46
|
+
* @param superset - The set expected to contain all elements of `subset`.
|
|
47
|
+
* @returns `true` if `subset` ⊆ `superset`, otherwise `false`.
|
|
48
|
+
*/
|
|
49
|
+
export declare const isSubset: <T>(subset: Set<T>, superset: Set<T>) => boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Gets the terms for a given enforcer from a list of caveats.
|
|
52
|
+
*
|
|
53
|
+
* @param args - The arguments to this function.
|
|
54
|
+
* @param args.throwIfNotFound - Whether to throw an error if no matching enforcer is found. Default is true.
|
|
55
|
+
* @param args.caveats - The list of caveats to search.
|
|
56
|
+
* @param args.enforcer - The enforcer to search for.
|
|
57
|
+
* @returns The terms for the given enforcer.
|
|
58
|
+
*/
|
|
59
|
+
export declare function getTermsByEnforcer<TThrowIfNotFound extends boolean = true>({ caveats, enforcer, throwIfNotFound, }: {
|
|
60
|
+
caveats: Caveat<Hex>[];
|
|
61
|
+
enforcer: Hex;
|
|
62
|
+
throwIfNotFound?: TThrowIfNotFound;
|
|
63
|
+
}): TThrowIfNotFound extends true ? Hex : Hex | null;
|
|
64
|
+
/**
|
|
65
|
+
* Splits a 0x-prefixed hex string into parts according to the provided byte lengths.
|
|
66
|
+
*
|
|
67
|
+
* Each entry in `lengths` represents a part length in bytes; internally this is
|
|
68
|
+
* multiplied by 2 to derive the number of hexadecimal characters to slice. The
|
|
69
|
+
* returned substrings do not include the `0x` prefix and preserve leading zeros.
|
|
70
|
+
*
|
|
71
|
+
* Note: This function does not perform input validation (e.g., verifying the
|
|
72
|
+
* payload length equals the sum of requested lengths). Callers are expected to
|
|
73
|
+
* provide well-formed inputs.
|
|
74
|
+
*
|
|
75
|
+
* Example:
|
|
76
|
+
* splitHex('0x12345678', [1, 3]) => ['0x12', '0x345678']
|
|
77
|
+
*
|
|
78
|
+
* @param value - The 0x-prefixed hex string to split.
|
|
79
|
+
* @param lengths - The lengths of each part, in bytes.
|
|
80
|
+
* @returns An array of hex substrings (each with `0x` prefix), one for each part.
|
|
81
|
+
*/
|
|
82
|
+
export declare function splitHex(value: Hex, lengths: number[]): Hex[];
|
|
83
|
+
//# sourceMappingURL=utils.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,kCAAkC;AACxD,OAAO,EAAsB,KAAK,GAAG,EAAE,wBAAwB;AAE/D,OAAO,KAAK,EAAE,uBAAuB,EAAE,cAAc,EAAE,oBAAgB;AAEvE;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,cAAc,EAAE,cAAc,CAAC;IAC/B,iBAAiB,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;IAC5B,gBAAgB,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;CAC5B,CAAC;AAgBF;;;;;;;GAOG;AACH,eAAO,MAAM,6BAA6B,cAC7B,uBAAuB;;;;;;;;;CAkDnC,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,+BAA+B,EAAE,CAC5C,SAAS,EAAE,uBAAuB,KAC/B,cAAc,EAuDlB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,2CAA0C,OAO9D,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,gBAAgB,SAAS,OAAO,GAAG,IAAI,EAAE,EAC1E,OAAO,EACP,QAAQ,EACR,eAAe,GAChB,EAAE;IACD,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;IACvB,QAAQ,EAAE,GAAG,CAAC;IACd,eAAe,CAAC,EAAE,gBAAgB,CAAC;CACpC,GAAG,gBAAgB,SAAS,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,CAiBnD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,GAAG,EAAE,CAU7D"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { Caveat } from "@metamask/delegation-core";
|
|
2
|
+
import { type Hex } from "@metamask/utils";
|
|
3
|
+
import type { DeployedContractsByName, PermissionType } from "./types.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* A rule that defines the required and allowed enforcers for a permission type.
|
|
6
|
+
*/
|
|
7
|
+
export type PermissionRule = {
|
|
8
|
+
permissionType: PermissionType;
|
|
9
|
+
requiredEnforcers: Set<Hex>;
|
|
10
|
+
allowedEnforcers: Set<Hex>;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Resolves and returns checksummed addresses of all known enforcer contracts
|
|
14
|
+
* for a given `chainId` under the current delegation framework version.
|
|
15
|
+
*
|
|
16
|
+
* @param contracts - The deployed contracts for the chain.
|
|
17
|
+
* @returns An object mapping enforcer names to checksummed contract addresses.
|
|
18
|
+
* @throws If the chain or an expected enforcer contract is not found.
|
|
19
|
+
*/
|
|
20
|
+
export declare const getChecksumEnforcersByChainId: (contracts: DeployedContractsByName) => {
|
|
21
|
+
erc20StreamingEnforcer: `0x${string}`;
|
|
22
|
+
erc20PeriodicEnforcer: `0x${string}`;
|
|
23
|
+
nativeTokenStreamingEnforcer: `0x${string}`;
|
|
24
|
+
nativeTokenPeriodicEnforcer: `0x${string}`;
|
|
25
|
+
exactCalldataEnforcer: `0x${string}`;
|
|
26
|
+
valueLteEnforcer: `0x${string}`;
|
|
27
|
+
timestampEnforcer: `0x${string}`;
|
|
28
|
+
nonceEnforcer: `0x${string}`;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Builds the canonical set of permission matching rules for a chain.
|
|
32
|
+
*
|
|
33
|
+
* Each rule specifies the `permissionType`, the set of `requiredEnforcers`
|
|
34
|
+
* that must be present, and the set of `allowedEnforcers` that may appear in
|
|
35
|
+
* addition to the required set.
|
|
36
|
+
*
|
|
37
|
+
* @param contracts - The deployed contracts for the chain.
|
|
38
|
+
* @returns A list of permission rules used to identify permission types.
|
|
39
|
+
* @throws Propagates any errors from resolving enforcer addresses.
|
|
40
|
+
*/
|
|
41
|
+
export declare const createPermissionRulesForChainId: (contracts: DeployedContractsByName) => PermissionRule[];
|
|
42
|
+
/**
|
|
43
|
+
* Determines whether all elements of `subset` are contained within `superset`.
|
|
44
|
+
*
|
|
45
|
+
* @param subset - The candidate subset to test.
|
|
46
|
+
* @param superset - The set expected to contain all elements of `subset`.
|
|
47
|
+
* @returns `true` if `subset` ⊆ `superset`, otherwise `false`.
|
|
48
|
+
*/
|
|
49
|
+
export declare const isSubset: <T>(subset: Set<T>, superset: Set<T>) => boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Gets the terms for a given enforcer from a list of caveats.
|
|
52
|
+
*
|
|
53
|
+
* @param args - The arguments to this function.
|
|
54
|
+
* @param args.throwIfNotFound - Whether to throw an error if no matching enforcer is found. Default is true.
|
|
55
|
+
* @param args.caveats - The list of caveats to search.
|
|
56
|
+
* @param args.enforcer - The enforcer to search for.
|
|
57
|
+
* @returns The terms for the given enforcer.
|
|
58
|
+
*/
|
|
59
|
+
export declare function getTermsByEnforcer<TThrowIfNotFound extends boolean = true>({ caveats, enforcer, throwIfNotFound, }: {
|
|
60
|
+
caveats: Caveat<Hex>[];
|
|
61
|
+
enforcer: Hex;
|
|
62
|
+
throwIfNotFound?: TThrowIfNotFound;
|
|
63
|
+
}): TThrowIfNotFound extends true ? Hex : Hex | null;
|
|
64
|
+
/**
|
|
65
|
+
* Splits a 0x-prefixed hex string into parts according to the provided byte lengths.
|
|
66
|
+
*
|
|
67
|
+
* Each entry in `lengths` represents a part length in bytes; internally this is
|
|
68
|
+
* multiplied by 2 to derive the number of hexadecimal characters to slice. The
|
|
69
|
+
* returned substrings do not include the `0x` prefix and preserve leading zeros.
|
|
70
|
+
*
|
|
71
|
+
* Note: This function does not perform input validation (e.g., verifying the
|
|
72
|
+
* payload length equals the sum of requested lengths). Callers are expected to
|
|
73
|
+
* provide well-formed inputs.
|
|
74
|
+
*
|
|
75
|
+
* Example:
|
|
76
|
+
* splitHex('0x12345678', [1, 3]) => ['0x12', '0x345678']
|
|
77
|
+
*
|
|
78
|
+
* @param value - The 0x-prefixed hex string to split.
|
|
79
|
+
* @param lengths - The lengths of each part, in bytes.
|
|
80
|
+
* @returns An array of hex substrings (each with `0x` prefix), one for each part.
|
|
81
|
+
*/
|
|
82
|
+
export declare function splitHex(value: Hex, lengths: number[]): Hex[];
|
|
83
|
+
//# sourceMappingURL=utils.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,kCAAkC;AACxD,OAAO,EAAsB,KAAK,GAAG,EAAE,wBAAwB;AAE/D,OAAO,KAAK,EAAE,uBAAuB,EAAE,cAAc,EAAE,oBAAgB;AAEvE;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,cAAc,EAAE,cAAc,CAAC;IAC/B,iBAAiB,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;IAC5B,gBAAgB,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;CAC5B,CAAC;AAgBF;;;;;;;GAOG;AACH,eAAO,MAAM,6BAA6B,cAC7B,uBAAuB;;;;;;;;;CAkDnC,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,+BAA+B,EAAE,CAC5C,SAAS,EAAE,uBAAuB,KAC/B,cAAc,EAuDlB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,2CAA0C,OAO9D,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,gBAAgB,SAAS,OAAO,GAAG,IAAI,EAAE,EAC1E,OAAO,EACP,QAAQ,EACR,eAAe,GAChB,EAAE;IACD,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;IACvB,QAAQ,EAAE,GAAG,CAAC;IACd,eAAe,CAAC,EAAE,gBAAgB,CAAC;CACpC,GAAG,gBAAgB,SAAS,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,CAiBnD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,GAAG,EAAE,CAU7D"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { getChecksumAddress } from "@metamask/utils";
|
|
2
|
+
/**
|
|
3
|
+
* The names of the enforcer contracts for each permission type.
|
|
4
|
+
*/
|
|
5
|
+
const ENFORCER_CONTRACT_NAMES = {
|
|
6
|
+
ERC20PeriodTransferEnforcer: 'ERC20PeriodTransferEnforcer',
|
|
7
|
+
ERC20StreamingEnforcer: 'ERC20StreamingEnforcer',
|
|
8
|
+
ExactCalldataEnforcer: 'ExactCalldataEnforcer',
|
|
9
|
+
NativeTokenPeriodTransferEnforcer: 'NativeTokenPeriodTransferEnforcer',
|
|
10
|
+
NativeTokenStreamingEnforcer: 'NativeTokenStreamingEnforcer',
|
|
11
|
+
TimestampEnforcer: 'TimestampEnforcer',
|
|
12
|
+
ValueLteEnforcer: 'ValueLteEnforcer',
|
|
13
|
+
NonceEnforcer: 'NonceEnforcer',
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Resolves and returns checksummed addresses of all known enforcer contracts
|
|
17
|
+
* for a given `chainId` under the current delegation framework version.
|
|
18
|
+
*
|
|
19
|
+
* @param contracts - The deployed contracts for the chain.
|
|
20
|
+
* @returns An object mapping enforcer names to checksummed contract addresses.
|
|
21
|
+
* @throws If the chain or an expected enforcer contract is not found.
|
|
22
|
+
*/
|
|
23
|
+
export const getChecksumEnforcersByChainId = (contracts) => {
|
|
24
|
+
const getChecksumContractAddress = (contractName) => {
|
|
25
|
+
const address = contracts[contractName];
|
|
26
|
+
if (!address) {
|
|
27
|
+
throw new Error(`Contract not found: ${contractName}`);
|
|
28
|
+
}
|
|
29
|
+
return getChecksumAddress(address);
|
|
30
|
+
};
|
|
31
|
+
// permission type specific enforcers
|
|
32
|
+
const erc20StreamingEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ERC20StreamingEnforcer);
|
|
33
|
+
const erc20PeriodicEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ERC20PeriodTransferEnforcer);
|
|
34
|
+
const nativeTokenStreamingEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NativeTokenStreamingEnforcer);
|
|
35
|
+
const nativeTokenPeriodicEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NativeTokenPeriodTransferEnforcer);
|
|
36
|
+
// general enforcers
|
|
37
|
+
const exactCalldataEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ExactCalldataEnforcer);
|
|
38
|
+
const valueLteEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ValueLteEnforcer);
|
|
39
|
+
const timestampEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.TimestampEnforcer);
|
|
40
|
+
const nonceEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NonceEnforcer);
|
|
41
|
+
return {
|
|
42
|
+
erc20StreamingEnforcer,
|
|
43
|
+
erc20PeriodicEnforcer,
|
|
44
|
+
nativeTokenStreamingEnforcer,
|
|
45
|
+
nativeTokenPeriodicEnforcer,
|
|
46
|
+
exactCalldataEnforcer,
|
|
47
|
+
valueLteEnforcer,
|
|
48
|
+
timestampEnforcer,
|
|
49
|
+
nonceEnforcer,
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Builds the canonical set of permission matching rules for a chain.
|
|
54
|
+
*
|
|
55
|
+
* Each rule specifies the `permissionType`, the set of `requiredEnforcers`
|
|
56
|
+
* that must be present, and the set of `allowedEnforcers` that may appear in
|
|
57
|
+
* addition to the required set.
|
|
58
|
+
*
|
|
59
|
+
* @param contracts - The deployed contracts for the chain.
|
|
60
|
+
* @returns A list of permission rules used to identify permission types.
|
|
61
|
+
* @throws Propagates any errors from resolving enforcer addresses.
|
|
62
|
+
*/
|
|
63
|
+
export const createPermissionRulesForChainId = (contracts) => {
|
|
64
|
+
const { erc20StreamingEnforcer, erc20PeriodicEnforcer, nativeTokenStreamingEnforcer, nativeTokenPeriodicEnforcer, exactCalldataEnforcer, valueLteEnforcer, timestampEnforcer, nonceEnforcer, } = getChecksumEnforcersByChainId(contracts);
|
|
65
|
+
// the allowed enforcers are the same for all permission types
|
|
66
|
+
const allowedEnforcers = new Set([timestampEnforcer]);
|
|
67
|
+
const permissionRules = [
|
|
68
|
+
{
|
|
69
|
+
requiredEnforcers: new Set([
|
|
70
|
+
nativeTokenStreamingEnforcer,
|
|
71
|
+
exactCalldataEnforcer,
|
|
72
|
+
nonceEnforcer,
|
|
73
|
+
]),
|
|
74
|
+
allowedEnforcers,
|
|
75
|
+
permissionType: 'native-token-stream',
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
requiredEnforcers: new Set([
|
|
79
|
+
nativeTokenPeriodicEnforcer,
|
|
80
|
+
exactCalldataEnforcer,
|
|
81
|
+
nonceEnforcer,
|
|
82
|
+
]),
|
|
83
|
+
allowedEnforcers,
|
|
84
|
+
permissionType: 'native-token-periodic',
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
requiredEnforcers: new Set([
|
|
88
|
+
erc20StreamingEnforcer,
|
|
89
|
+
valueLteEnforcer,
|
|
90
|
+
nonceEnforcer,
|
|
91
|
+
]),
|
|
92
|
+
allowedEnforcers,
|
|
93
|
+
permissionType: 'erc20-token-stream',
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
requiredEnforcers: new Set([
|
|
97
|
+
erc20PeriodicEnforcer,
|
|
98
|
+
valueLteEnforcer,
|
|
99
|
+
nonceEnforcer,
|
|
100
|
+
]),
|
|
101
|
+
allowedEnforcers,
|
|
102
|
+
permissionType: 'erc20-token-periodic',
|
|
103
|
+
},
|
|
104
|
+
];
|
|
105
|
+
return permissionRules;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Determines whether all elements of `subset` are contained within `superset`.
|
|
109
|
+
*
|
|
110
|
+
* @param subset - The candidate subset to test.
|
|
111
|
+
* @param superset - The set expected to contain all elements of `subset`.
|
|
112
|
+
* @returns `true` if `subset` ⊆ `superset`, otherwise `false`.
|
|
113
|
+
*/
|
|
114
|
+
export const isSubset = (subset, superset) => {
|
|
115
|
+
for (const x of subset) {
|
|
116
|
+
if (!superset.has(x)) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Gets the terms for a given enforcer from a list of caveats.
|
|
124
|
+
*
|
|
125
|
+
* @param args - The arguments to this function.
|
|
126
|
+
* @param args.throwIfNotFound - Whether to throw an error if no matching enforcer is found. Default is true.
|
|
127
|
+
* @param args.caveats - The list of caveats to search.
|
|
128
|
+
* @param args.enforcer - The enforcer to search for.
|
|
129
|
+
* @returns The terms for the given enforcer.
|
|
130
|
+
*/
|
|
131
|
+
export function getTermsByEnforcer({ caveats, enforcer, throwIfNotFound, }) {
|
|
132
|
+
const matchingCaveats = caveats.filter((caveat) => caveat.enforcer === enforcer);
|
|
133
|
+
if (matchingCaveats.length === 0) {
|
|
134
|
+
if (throwIfNotFound ?? true) {
|
|
135
|
+
throw new Error('Invalid caveats');
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
if (matchingCaveats.length > 1) {
|
|
140
|
+
throw new Error('Invalid caveats');
|
|
141
|
+
}
|
|
142
|
+
return matchingCaveats[0].terms;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Splits a 0x-prefixed hex string into parts according to the provided byte lengths.
|
|
146
|
+
*
|
|
147
|
+
* Each entry in `lengths` represents a part length in bytes; internally this is
|
|
148
|
+
* multiplied by 2 to derive the number of hexadecimal characters to slice. The
|
|
149
|
+
* returned substrings do not include the `0x` prefix and preserve leading zeros.
|
|
150
|
+
*
|
|
151
|
+
* Note: This function does not perform input validation (e.g., verifying the
|
|
152
|
+
* payload length equals the sum of requested lengths). Callers are expected to
|
|
153
|
+
* provide well-formed inputs.
|
|
154
|
+
*
|
|
155
|
+
* Example:
|
|
156
|
+
* splitHex('0x12345678', [1, 3]) => ['0x12', '0x345678']
|
|
157
|
+
*
|
|
158
|
+
* @param value - The 0x-prefixed hex string to split.
|
|
159
|
+
* @param lengths - The lengths of each part, in bytes.
|
|
160
|
+
* @returns An array of hex substrings (each with `0x` prefix), one for each part.
|
|
161
|
+
*/
|
|
162
|
+
export function splitHex(value, lengths) {
|
|
163
|
+
let start = 2;
|
|
164
|
+
const parts = [];
|
|
165
|
+
for (const partLength of lengths) {
|
|
166
|
+
const partCharLength = partLength * 2;
|
|
167
|
+
const part = value.slice(start, start + partCharLength);
|
|
168
|
+
start += partCharLength;
|
|
169
|
+
parts.push(`0x${part}`);
|
|
170
|
+
}
|
|
171
|
+
return parts;
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=utils.mjs.map
|