@metamask-previews/gator-permissions-controller 4.1.2-preview-bfa94c060 → 4.2.0-preview-1275d0fda
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 +9 -2
- package/dist/decodePermission/decoders/index.cjs +2 -0
- package/dist/decodePermission/decoders/index.cjs.map +1 -1
- package/dist/decodePermission/decoders/index.d.cts.map +1 -1
- package/dist/decodePermission/decoders/index.d.mts.map +1 -1
- package/dist/decodePermission/decoders/index.mjs +2 -0
- package/dist/decodePermission/decoders/index.mjs.map +1 -1
- package/dist/decodePermission/decoders/tokenApprovalRevocation.cjs +81 -0
- package/dist/decodePermission/decoders/tokenApprovalRevocation.cjs.map +1 -0
- package/dist/decodePermission/decoders/tokenApprovalRevocation.d.cts +10 -0
- package/dist/decodePermission/decoders/tokenApprovalRevocation.d.cts.map +1 -0
- package/dist/decodePermission/decoders/tokenApprovalRevocation.d.mts +10 -0
- package/dist/decodePermission/decoders/tokenApprovalRevocation.d.mts.map +1 -0
- package/dist/decodePermission/decoders/tokenApprovalRevocation.mjs +77 -0
- package/dist/decodePermission/decoders/tokenApprovalRevocation.mjs.map +1 -0
- package/dist/decodePermission/types.cjs.map +1 -1
- package/dist/decodePermission/types.d.cts +16 -4
- package/dist/decodePermission/types.d.cts.map +1 -1
- package/dist/decodePermission/types.d.mts +16 -4
- package/dist/decodePermission/types.d.mts.map +1 -1
- package/dist/decodePermission/types.mjs.map +1 -1
- package/dist/decodePermission/utils.cjs +3 -0
- package/dist/decodePermission/utils.cjs.map +1 -1
- package/dist/decodePermission/utils.d.cts.map +1 -1
- package/dist/decodePermission/utils.d.mts.map +1 -1
- package/dist/decodePermission/utils.mjs +3 -0
- package/dist/decodePermission/utils.mjs.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -7,9 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [4.2.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Add `token-approval-revocation` execution permission type decoding ([#8823](https://github.com/MetaMask/core/pull/8823))
|
|
15
|
+
|
|
10
16
|
### Changed
|
|
11
17
|
|
|
12
|
-
- Bump `@metamask/transaction-controller` from `^65.3.0` to `^
|
|
18
|
+
- Bump `@metamask/transaction-controller` from `^65.3.0` to `^66.0.0` ([#8796](https://github.com/MetaMask/core/pull/8796), [#8848](https://github.com/MetaMask/core/pull/8848))
|
|
13
19
|
|
|
14
20
|
## [4.1.2]
|
|
15
21
|
|
|
@@ -250,7 +256,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
250
256
|
|
|
251
257
|
- Initial release ([#6033](https://github.com/MetaMask/core/pull/6033))
|
|
252
258
|
|
|
253
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/gator-permissions-controller@4.
|
|
259
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/gator-permissions-controller@4.2.0...HEAD
|
|
260
|
+
[4.2.0]: https://github.com/MetaMask/core/compare/@metamask/gator-permissions-controller@4.1.2...@metamask/gator-permissions-controller@4.2.0
|
|
254
261
|
[4.1.2]: https://github.com/MetaMask/core/compare/@metamask/gator-permissions-controller@4.1.1...@metamask/gator-permissions-controller@4.1.2
|
|
255
262
|
[4.1.1]: https://github.com/MetaMask/core/compare/@metamask/gator-permissions-controller@4.1.0...@metamask/gator-permissions-controller@4.1.1
|
|
256
263
|
[4.1.0]: https://github.com/MetaMask/core/compare/@metamask/gator-permissions-controller@4.0.0...@metamask/gator-permissions-controller@4.1.0
|
|
@@ -10,6 +10,7 @@ const makePermissionDecoder_1 = require("./makePermissionDecoder.cjs");
|
|
|
10
10
|
const nativeTokenAllowance_1 = require("./nativeTokenAllowance.cjs");
|
|
11
11
|
const nativeTokenPeriodic_1 = require("./nativeTokenPeriodic.cjs");
|
|
12
12
|
const nativeTokenStream_1 = require("./nativeTokenStream.cjs");
|
|
13
|
+
const tokenApprovalRevocation_1 = require("./tokenApprovalRevocation.cjs");
|
|
13
14
|
/**
|
|
14
15
|
* Builds the canonical set of permission decoders for a chain.
|
|
15
16
|
*
|
|
@@ -31,6 +32,7 @@ const createPermissionDecodersForContracts = (contracts) => {
|
|
|
31
32
|
(0, erc20TokenPeriodic_1.makeErc20TokenPeriodicDecoderConfig)(contractAddresses),
|
|
32
33
|
(0, erc20TokenAllowance_1.makeErc20TokenAllowanceDecoderConfig)(contractAddresses),
|
|
33
34
|
(0, erc20TokenRevocation_1.makeErc20TokenRevocationDecoderConfig)(contractAddresses),
|
|
35
|
+
(0, tokenApprovalRevocation_1.makeTokenApprovalRevocationDecoderConfig)(contractAddresses),
|
|
34
36
|
].map(makePermissionDecoder_1.makePermissionDecoder);
|
|
35
37
|
};
|
|
36
38
|
exports.createPermissionDecodersForContracts = createPermissionDecodersForContracts;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/decodePermission/decoders/index.ts"],"names":[],"mappings":";;;AACA,wCAAyD;AACzD,mEAA6E;AAC7E,iEAA2E;AAC3E,qEAA+E;AAC/E,6DAAuE;AACvE,uEAAgE;AAChE,qEAA+E;AAC/E,mEAA6E;AAC7E,+DAAyE;
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/decodePermission/decoders/index.ts"],"names":[],"mappings":";;;AACA,wCAAyD;AACzD,mEAA6E;AAC7E,iEAA2E;AAC3E,qEAA+E;AAC/E,6DAAuE;AACvE,uEAAgE;AAChE,qEAA+E;AAC/E,mEAA6E;AAC7E,+DAAyE;AACzE,2EAAqF;AAErF;;;;;;;;;;GAUG;AACI,MAAM,oCAAoC,GAAG,CAClD,SAAkC,EACb,EAAE;IACvB,MAAM,iBAAiB,GAAG,IAAA,qCAA6B,EAAC,SAAS,CAAC,CAAC;IACnE,OAAO;QACL,IAAA,sDAAkC,EAAC,iBAAiB,CAAC;QACrD,IAAA,0DAAoC,EAAC,iBAAiB,CAAC;QACvD,IAAA,4DAAqC,EAAC,iBAAiB,CAAC;QACxD,IAAA,oDAAiC,EAAC,iBAAiB,CAAC;QACpD,IAAA,wDAAmC,EAAC,iBAAiB,CAAC;QACtD,IAAA,0DAAoC,EAAC,iBAAiB,CAAC;QACvD,IAAA,4DAAqC,EAAC,iBAAiB,CAAC;QACxD,IAAA,kEAAwC,EAAC,iBAAiB,CAAC;KAC5D,CAAC,GAAG,CAAC,6CAAqB,CAAC,CAAC;AAC/B,CAAC,CAAC;AAdW,QAAA,oCAAoC,wCAc/C","sourcesContent":["import type { DeployedContractsByName, PermissionDecoder } from '../types';\nimport { getChecksumEnforcersByChainId } from '../utils';\nimport { makeErc20TokenAllowanceDecoderConfig } from './erc20TokenAllowance';\nimport { makeErc20TokenPeriodicDecoderConfig } from './erc20TokenPeriodic';\nimport { makeErc20TokenRevocationDecoderConfig } from './erc20TokenRevocation';\nimport { makeErc20TokenStreamDecoderConfig } from './erc20TokenStream';\nimport { makePermissionDecoder } from './makePermissionDecoder';\nimport { makeNativeTokenAllowanceDecoderConfig } from './nativeTokenAllowance';\nimport { makeNativeTokenPeriodicDecoderConfig } from './nativeTokenPeriodic';\nimport { makeNativeTokenStreamDecoderConfig } from './nativeTokenStream';\nimport { makeTokenApprovalRevocationDecoderConfig } from './tokenApprovalRevocation';\n\n/**\n * Builds the canonical set of permission decoders for a chain.\n *\n * Each decoder specifies the `permissionType`, required/optional enforcers,\n * and provides `caveatAddressesMatch` and `validateAndDecodePermission` so the\n * entire decode flow can be driven by the decoders.\n *\n * @param contracts - The deployed contracts for the chain.\n * @returns A list of permission decoders used to identify and decode permission types.\n * @throws Propagates any errors from resolving enforcer addresses.\n */\nexport const createPermissionDecodersForContracts = (\n contracts: DeployedContractsByName,\n): PermissionDecoder[] => {\n const contractAddresses = getChecksumEnforcersByChainId(contracts);\n return [\n makeNativeTokenStreamDecoderConfig(contractAddresses),\n makeNativeTokenPeriodicDecoderConfig(contractAddresses),\n makeNativeTokenAllowanceDecoderConfig(contractAddresses),\n makeErc20TokenStreamDecoderConfig(contractAddresses),\n makeErc20TokenPeriodicDecoderConfig(contractAddresses),\n makeErc20TokenAllowanceDecoderConfig(contractAddresses),\n makeErc20TokenRevocationDecoderConfig(contractAddresses),\n makeTokenApprovalRevocationDecoderConfig(contractAddresses),\n ].map(makePermissionDecoder);\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/decodePermission/decoders/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,qBAAiB;
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/decodePermission/decoders/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,qBAAiB;AAY3E;;;;;;;;;;GAUG;AACH,eAAO,MAAM,oCAAoC,cACpC,uBAAuB,KACjC,iBAAiB,EAYnB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/decodePermission/decoders/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,qBAAiB;
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/decodePermission/decoders/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,qBAAiB;AAY3E;;;;;;;;;;GAUG;AACH,eAAO,MAAM,oCAAoC,cACpC,uBAAuB,KACjC,iBAAiB,EAYnB,CAAC"}
|
|
@@ -7,6 +7,7 @@ import { makePermissionDecoder } from "./makePermissionDecoder.mjs";
|
|
|
7
7
|
import { makeNativeTokenAllowanceDecoderConfig } from "./nativeTokenAllowance.mjs";
|
|
8
8
|
import { makeNativeTokenPeriodicDecoderConfig } from "./nativeTokenPeriodic.mjs";
|
|
9
9
|
import { makeNativeTokenStreamDecoderConfig } from "./nativeTokenStream.mjs";
|
|
10
|
+
import { makeTokenApprovalRevocationDecoderConfig } from "./tokenApprovalRevocation.mjs";
|
|
10
11
|
/**
|
|
11
12
|
* Builds the canonical set of permission decoders for a chain.
|
|
12
13
|
*
|
|
@@ -28,6 +29,7 @@ export const createPermissionDecodersForContracts = (contracts) => {
|
|
|
28
29
|
makeErc20TokenPeriodicDecoderConfig(contractAddresses),
|
|
29
30
|
makeErc20TokenAllowanceDecoderConfig(contractAddresses),
|
|
30
31
|
makeErc20TokenRevocationDecoderConfig(contractAddresses),
|
|
32
|
+
makeTokenApprovalRevocationDecoderConfig(contractAddresses),
|
|
31
33
|
].map(makePermissionDecoder);
|
|
32
34
|
};
|
|
33
35
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/decodePermission/decoders/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,6BAA6B,EAAE,qBAAiB;AACzD,OAAO,EAAE,oCAAoC,EAAE,kCAA8B;AAC7E,OAAO,EAAE,mCAAmC,EAAE,iCAA6B;AAC3E,OAAO,EAAE,qCAAqC,EAAE,mCAA+B;AAC/E,OAAO,EAAE,iCAAiC,EAAE,+BAA2B;AACvE,OAAO,EAAE,qBAAqB,EAAE,oCAAgC;AAChE,OAAO,EAAE,qCAAqC,EAAE,mCAA+B;AAC/E,OAAO,EAAE,oCAAoC,EAAE,kCAA8B;AAC7E,OAAO,EAAE,kCAAkC,EAAE,gCAA4B;
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/decodePermission/decoders/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,6BAA6B,EAAE,qBAAiB;AACzD,OAAO,EAAE,oCAAoC,EAAE,kCAA8B;AAC7E,OAAO,EAAE,mCAAmC,EAAE,iCAA6B;AAC3E,OAAO,EAAE,qCAAqC,EAAE,mCAA+B;AAC/E,OAAO,EAAE,iCAAiC,EAAE,+BAA2B;AACvE,OAAO,EAAE,qBAAqB,EAAE,oCAAgC;AAChE,OAAO,EAAE,qCAAqC,EAAE,mCAA+B;AAC/E,OAAO,EAAE,oCAAoC,EAAE,kCAA8B;AAC7E,OAAO,EAAE,kCAAkC,EAAE,gCAA4B;AACzE,OAAO,EAAE,wCAAwC,EAAE,sCAAkC;AAErF;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,oCAAoC,GAAG,CAClD,SAAkC,EACb,EAAE;IACvB,MAAM,iBAAiB,GAAG,6BAA6B,CAAC,SAAS,CAAC,CAAC;IACnE,OAAO;QACL,kCAAkC,CAAC,iBAAiB,CAAC;QACrD,oCAAoC,CAAC,iBAAiB,CAAC;QACvD,qCAAqC,CAAC,iBAAiB,CAAC;QACxD,iCAAiC,CAAC,iBAAiB,CAAC;QACpD,mCAAmC,CAAC,iBAAiB,CAAC;QACtD,oCAAoC,CAAC,iBAAiB,CAAC;QACvD,qCAAqC,CAAC,iBAAiB,CAAC;QACxD,wCAAwC,CAAC,iBAAiB,CAAC;KAC5D,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;AAC/B,CAAC,CAAC","sourcesContent":["import type { DeployedContractsByName, PermissionDecoder } from '../types';\nimport { getChecksumEnforcersByChainId } from '../utils';\nimport { makeErc20TokenAllowanceDecoderConfig } from './erc20TokenAllowance';\nimport { makeErc20TokenPeriodicDecoderConfig } from './erc20TokenPeriodic';\nimport { makeErc20TokenRevocationDecoderConfig } from './erc20TokenRevocation';\nimport { makeErc20TokenStreamDecoderConfig } from './erc20TokenStream';\nimport { makePermissionDecoder } from './makePermissionDecoder';\nimport { makeNativeTokenAllowanceDecoderConfig } from './nativeTokenAllowance';\nimport { makeNativeTokenPeriodicDecoderConfig } from './nativeTokenPeriodic';\nimport { makeNativeTokenStreamDecoderConfig } from './nativeTokenStream';\nimport { makeTokenApprovalRevocationDecoderConfig } from './tokenApprovalRevocation';\n\n/**\n * Builds the canonical set of permission decoders for a chain.\n *\n * Each decoder specifies the `permissionType`, required/optional enforcers,\n * and provides `caveatAddressesMatch` and `validateAndDecodePermission` so the\n * entire decode flow can be driven by the decoders.\n *\n * @param contracts - The deployed contracts for the chain.\n * @returns A list of permission decoders used to identify and decode permission types.\n * @throws Propagates any errors from resolving enforcer addresses.\n */\nexport const createPermissionDecodersForContracts = (\n contracts: DeployedContractsByName,\n): PermissionDecoder[] => {\n const contractAddresses = getChecksumEnforcersByChainId(contracts);\n return [\n makeNativeTokenStreamDecoderConfig(contractAddresses),\n makeNativeTokenPeriodicDecoderConfig(contractAddresses),\n makeNativeTokenAllowanceDecoderConfig(contractAddresses),\n makeErc20TokenStreamDecoderConfig(contractAddresses),\n makeErc20TokenPeriodicDecoderConfig(contractAddresses),\n makeErc20TokenAllowanceDecoderConfig(contractAddresses),\n makeErc20TokenRevocationDecoderConfig(contractAddresses),\n makeTokenApprovalRevocationDecoderConfig(contractAddresses),\n ].map(makePermissionDecoder);\n};\n"]}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeTokenApprovalRevocationDecoderConfig = void 0;
|
|
4
|
+
/* eslint-disable no-bitwise */
|
|
5
|
+
const utils_1 = require("@metamask/utils");
|
|
6
|
+
const utils_2 = require("../utils.cjs");
|
|
7
|
+
const expiryRule_1 = require("./expiryRule.cjs");
|
|
8
|
+
var ApprovalRevocationFlag;
|
|
9
|
+
(function (ApprovalRevocationFlag) {
|
|
10
|
+
ApprovalRevocationFlag[ApprovalRevocationFlag["Erc20Approve"] = 1] = "Erc20Approve";
|
|
11
|
+
ApprovalRevocationFlag[ApprovalRevocationFlag["Erc721Approve"] = 2] = "Erc721Approve";
|
|
12
|
+
ApprovalRevocationFlag[ApprovalRevocationFlag["Erc721SetApprovalForAll"] = 4] = "Erc721SetApprovalForAll";
|
|
13
|
+
ApprovalRevocationFlag[ApprovalRevocationFlag["Permit2Approve"] = 8] = "Permit2Approve";
|
|
14
|
+
ApprovalRevocationFlag[ApprovalRevocationFlag["Permit2Lockdown"] = 16] = "Permit2Lockdown";
|
|
15
|
+
ApprovalRevocationFlag[ApprovalRevocationFlag["Permit2InvalidateNonces"] = 32] = "Permit2InvalidateNonces";
|
|
16
|
+
})(ApprovalRevocationFlag || (ApprovalRevocationFlag = {}));
|
|
17
|
+
const MAX_APPROVAL_REVOCATION_MASK = ApprovalRevocationFlag.Permit2InvalidateNonces |
|
|
18
|
+
ApprovalRevocationFlag.Permit2Lockdown |
|
|
19
|
+
ApprovalRevocationFlag.Permit2Approve |
|
|
20
|
+
ApprovalRevocationFlag.Erc721SetApprovalForAll |
|
|
21
|
+
ApprovalRevocationFlag.Erc721Approve |
|
|
22
|
+
ApprovalRevocationFlag.Erc20Approve;
|
|
23
|
+
/**
|
|
24
|
+
* Builds the configuration for the token-approval-revocation permission decoder.
|
|
25
|
+
*
|
|
26
|
+
* @param contractAddresses - Checksummed enforcer addresses for the chain.
|
|
27
|
+
* @returns The token-approval-revocation permission decoder configuration.
|
|
28
|
+
*/
|
|
29
|
+
function makeTokenApprovalRevocationDecoderConfig(contractAddresses) {
|
|
30
|
+
const { timestampEnforcer, approvalRevocationEnforcer, nonceEnforcer } = contractAddresses;
|
|
31
|
+
return {
|
|
32
|
+
permissionType: 'token-approval-revocation',
|
|
33
|
+
contractAddresses,
|
|
34
|
+
optionalEnforcers: [
|
|
35
|
+
timestampEnforcer, // expiry rule
|
|
36
|
+
],
|
|
37
|
+
requiredEnforcers: {
|
|
38
|
+
[approvalRevocationEnforcer]: 1,
|
|
39
|
+
[nonceEnforcer]: 1,
|
|
40
|
+
},
|
|
41
|
+
rules: [expiryRule_1.expiryRule],
|
|
42
|
+
validateAndDecodeData,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
exports.makeTokenApprovalRevocationDecoderConfig = makeTokenApprovalRevocationDecoderConfig;
|
|
46
|
+
/**
|
|
47
|
+
* Decodes token-approval-revocation permission data from caveats; throws on invalid.
|
|
48
|
+
*
|
|
49
|
+
* @param caveats - Caveats from the permission context (checksummed).
|
|
50
|
+
* @param contractAddresses - Checksummed enforcer addresses for the chain.
|
|
51
|
+
* @returns Decoded approval-revocation capability flags.
|
|
52
|
+
*/
|
|
53
|
+
function validateAndDecodeData(caveats, contractAddresses) {
|
|
54
|
+
const { approvalRevocationEnforcer } = contractAddresses;
|
|
55
|
+
const terms = (0, utils_2.getTermsByEnforcer)({
|
|
56
|
+
caveats,
|
|
57
|
+
enforcer: approvalRevocationEnforcer,
|
|
58
|
+
});
|
|
59
|
+
if (terms === '0x') {
|
|
60
|
+
throw new Error('Invalid ApprovalRevocation terms: must be greater than 0');
|
|
61
|
+
}
|
|
62
|
+
const mask = (0, utils_1.hexToNumber)(terms);
|
|
63
|
+
if (mask > MAX_APPROVAL_REVOCATION_MASK) {
|
|
64
|
+
throw new Error(`Invalid ApprovalRevocation terms: must be less than or equal to ${MAX_APPROVAL_REVOCATION_MASK}`);
|
|
65
|
+
}
|
|
66
|
+
if (mask === 0) {
|
|
67
|
+
throw new Error('Invalid ApprovalRevocation terms: must be greater than 0');
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
erc20Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Erc20Approve),
|
|
71
|
+
erc721Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Erc721Approve),
|
|
72
|
+
erc721SetApprovalForAll: isFlagEnabled(mask, ApprovalRevocationFlag.Erc721SetApprovalForAll),
|
|
73
|
+
permit2Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Permit2Approve),
|
|
74
|
+
permit2Lockdown: isFlagEnabled(mask, ApprovalRevocationFlag.Permit2Lockdown),
|
|
75
|
+
permit2InvalidateNonces: isFlagEnabled(mask, ApprovalRevocationFlag.Permit2InvalidateNonces),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function isFlagEnabled(mask, flag) {
|
|
79
|
+
return (mask & flag) === flag;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=tokenApprovalRevocation.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokenApprovalRevocation.cjs","sourceRoot":"","sources":["../../../src/decodePermission/decoders/tokenApprovalRevocation.ts"],"names":[],"mappings":";;;AAAA,+BAA+B;AAC/B,2CAA8C;AAO9C,wCAA8C;AAC9C,iDAA0C;AAG1C,IAAK,sBAOJ;AAPD,WAAK,sBAAsB;IACzB,mFAAmB,CAAA;IACnB,qFAAoB,CAAA;IACpB,yGAA8B,CAAA;IAC9B,uFAAqB,CAAA;IACrB,0FAAsB,CAAA;IACtB,0GAA8B,CAAA;AAChC,CAAC,EAPI,sBAAsB,KAAtB,sBAAsB,QAO1B;AAED,MAAM,4BAA4B,GAChC,sBAAsB,CAAC,uBAAuB;IAC9C,sBAAsB,CAAC,eAAe;IACtC,sBAAsB,CAAC,cAAc;IACrC,sBAAsB,CAAC,uBAAuB;IAC9C,sBAAsB,CAAC,aAAa;IACpC,sBAAsB,CAAC,YAAY,CAAC;AAEtC;;;;;GAKG;AACH,SAAgB,wCAAwC,CACtD,iBAA6C;IAE7C,MAAM,EAAE,iBAAiB,EAAE,0BAA0B,EAAE,aAAa,EAAE,GACpE,iBAAiB,CAAC;IAEpB,OAAO;QACL,cAAc,EAAE,2BAA2B;QAC3C,iBAAiB;QACjB,iBAAiB,EAAE;YACjB,iBAAiB,EAAE,cAAc;SAClC;QACD,iBAAiB,EAAE;YACjB,CAAC,0BAA0B,CAAC,EAAE,CAAC;YAC/B,CAAC,aAAa,CAAC,EAAE,CAAC;SACnB;QACD,KAAK,EAAE,CAAC,uBAAU,CAAC;QACnB,qBAAqB;KACtB,CAAC;AACJ,CAAC;AAnBD,4FAmBC;AAED;;;;;;GAM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{\n ChecksumCaveat,\n ChecksumEnforcersByChainId,\n DecodedPermission,\n} from '../types';\nimport { getTermsByEnforcer } from '../utils';\nimport { expiryRule } from './expiryRule';\nimport type { MakePermissionDecoderConfig } from './makePermissionDecoder';\n\nenum ApprovalRevocationFlag {\n Erc20Approve = 0x01,\n Erc721Approve = 0x02,\n Erc721SetApprovalForAll = 0x04,\n Permit2Approve = 0x08,\n Permit2Lockdown = 0x10,\n Permit2InvalidateNonces = 0x20,\n}\n\nconst MAX_APPROVAL_REVOCATION_MASK =\n ApprovalRevocationFlag.Permit2InvalidateNonces |\n ApprovalRevocationFlag.Permit2Lockdown |\n ApprovalRevocationFlag.Permit2Approve |\n ApprovalRevocationFlag.Erc721SetApprovalForAll |\n ApprovalRevocationFlag.Erc721Approve |\n ApprovalRevocationFlag.Erc20Approve;\n\n/**\n * Builds the configuration for the token-approval-revocation permission decoder.\n *\n * @param contractAddresses - Checksummed enforcer addresses for the chain.\n * @returns The token-approval-revocation permission decoder configuration.\n */\nexport function makeTokenApprovalRevocationDecoderConfig(\n contractAddresses: ChecksumEnforcersByChainId,\n): MakePermissionDecoderConfig {\n const { timestampEnforcer, approvalRevocationEnforcer, nonceEnforcer } =\n contractAddresses;\n\n return {\n permissionType: 'token-approval-revocation',\n contractAddresses,\n optionalEnforcers: [\n timestampEnforcer, // expiry rule\n ],\n requiredEnforcers: {\n [approvalRevocationEnforcer]: 1,\n [nonceEnforcer]: 1,\n },\n rules: [expiryRule],\n validateAndDecodeData,\n };\n}\n\n/**\n * Decodes token-approval-revocation permission data from caveats; throws on invalid.\n *\n * @param caveats - Caveats from the permission context (checksummed).\n * @param contractAddresses - Checksummed enforcer addresses for the chain.\n * @returns Decoded approval-revocation capability flags.\n */\nfunction validateAndDecodeData(\n caveats: ChecksumCaveat[],\n contractAddresses: ChecksumEnforcersByChainId,\n): DecodedPermission['permission']['data'] {\n const { approvalRevocationEnforcer } = contractAddresses;\n\n const terms = getTermsByEnforcer({\n caveats,\n enforcer: approvalRevocationEnforcer,\n });\n\n if (terms === '0x') {\n throw new Error('Invalid ApprovalRevocation terms: must be greater than 0');\n }\n\n const mask = hexToNumber(terms);\n\n if (mask > MAX_APPROVAL_REVOCATION_MASK) {\n throw new Error(\n `Invalid ApprovalRevocation terms: must be less than or equal to ${MAX_APPROVAL_REVOCATION_MASK}`,\n );\n }\n\n if (mask === 0) {\n throw new Error('Invalid ApprovalRevocation terms: must be greater than 0');\n }\n\n return {\n erc20Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Erc20Approve),\n erc721Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Erc721Approve),\n erc721SetApprovalForAll: isFlagEnabled(\n mask,\n ApprovalRevocationFlag.Erc721SetApprovalForAll,\n ),\n permit2Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Permit2Approve),\n permit2Lockdown: isFlagEnabled(\n mask,\n ApprovalRevocationFlag.Permit2Lockdown,\n ),\n permit2InvalidateNonces: isFlagEnabled(\n mask,\n ApprovalRevocationFlag.Permit2InvalidateNonces,\n ),\n };\n}\n\nfunction isFlagEnabled(mask: number, flag: number): boolean {\n return (mask & flag) === flag;\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ChecksumEnforcersByChainId } from "../types.cjs";
|
|
2
|
+
import type { MakePermissionDecoderConfig } from "./makePermissionDecoder.cjs";
|
|
3
|
+
/**
|
|
4
|
+
* Builds the configuration for the token-approval-revocation permission decoder.
|
|
5
|
+
*
|
|
6
|
+
* @param contractAddresses - Checksummed enforcer addresses for the chain.
|
|
7
|
+
* @returns The token-approval-revocation permission decoder configuration.
|
|
8
|
+
*/
|
|
9
|
+
export declare function makeTokenApprovalRevocationDecoderConfig(contractAddresses: ChecksumEnforcersByChainId): MakePermissionDecoderConfig;
|
|
10
|
+
//# sourceMappingURL=tokenApprovalRevocation.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokenApprovalRevocation.d.cts","sourceRoot":"","sources":["../../../src/decodePermission/decoders/tokenApprovalRevocation.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAEV,0BAA0B,EAE3B,qBAAiB;AAGlB,OAAO,KAAK,EAAE,2BAA2B,EAAE,oCAAgC;AAmB3E;;;;;GAKG;AACH,wBAAgB,wCAAwC,CACtD,iBAAiB,EAAE,0BAA0B,GAC5C,2BAA2B,CAiB7B"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ChecksumEnforcersByChainId } from "../types.mjs";
|
|
2
|
+
import type { MakePermissionDecoderConfig } from "./makePermissionDecoder.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Builds the configuration for the token-approval-revocation permission decoder.
|
|
5
|
+
*
|
|
6
|
+
* @param contractAddresses - Checksummed enforcer addresses for the chain.
|
|
7
|
+
* @returns The token-approval-revocation permission decoder configuration.
|
|
8
|
+
*/
|
|
9
|
+
export declare function makeTokenApprovalRevocationDecoderConfig(contractAddresses: ChecksumEnforcersByChainId): MakePermissionDecoderConfig;
|
|
10
|
+
//# sourceMappingURL=tokenApprovalRevocation.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokenApprovalRevocation.d.mts","sourceRoot":"","sources":["../../../src/decodePermission/decoders/tokenApprovalRevocation.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAEV,0BAA0B,EAE3B,qBAAiB;AAGlB,OAAO,KAAK,EAAE,2BAA2B,EAAE,oCAAgC;AAmB3E;;;;;GAKG;AACH,wBAAgB,wCAAwC,CACtD,iBAAiB,EAAE,0BAA0B,GAC5C,2BAA2B,CAiB7B"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/* eslint-disable no-bitwise */
|
|
2
|
+
import { hexToNumber } from "@metamask/utils";
|
|
3
|
+
import { getTermsByEnforcer } from "../utils.mjs";
|
|
4
|
+
import { expiryRule } from "./expiryRule.mjs";
|
|
5
|
+
var ApprovalRevocationFlag;
|
|
6
|
+
(function (ApprovalRevocationFlag) {
|
|
7
|
+
ApprovalRevocationFlag[ApprovalRevocationFlag["Erc20Approve"] = 1] = "Erc20Approve";
|
|
8
|
+
ApprovalRevocationFlag[ApprovalRevocationFlag["Erc721Approve"] = 2] = "Erc721Approve";
|
|
9
|
+
ApprovalRevocationFlag[ApprovalRevocationFlag["Erc721SetApprovalForAll"] = 4] = "Erc721SetApprovalForAll";
|
|
10
|
+
ApprovalRevocationFlag[ApprovalRevocationFlag["Permit2Approve"] = 8] = "Permit2Approve";
|
|
11
|
+
ApprovalRevocationFlag[ApprovalRevocationFlag["Permit2Lockdown"] = 16] = "Permit2Lockdown";
|
|
12
|
+
ApprovalRevocationFlag[ApprovalRevocationFlag["Permit2InvalidateNonces"] = 32] = "Permit2InvalidateNonces";
|
|
13
|
+
})(ApprovalRevocationFlag || (ApprovalRevocationFlag = {}));
|
|
14
|
+
const MAX_APPROVAL_REVOCATION_MASK = ApprovalRevocationFlag.Permit2InvalidateNonces |
|
|
15
|
+
ApprovalRevocationFlag.Permit2Lockdown |
|
|
16
|
+
ApprovalRevocationFlag.Permit2Approve |
|
|
17
|
+
ApprovalRevocationFlag.Erc721SetApprovalForAll |
|
|
18
|
+
ApprovalRevocationFlag.Erc721Approve |
|
|
19
|
+
ApprovalRevocationFlag.Erc20Approve;
|
|
20
|
+
/**
|
|
21
|
+
* Builds the configuration for the token-approval-revocation permission decoder.
|
|
22
|
+
*
|
|
23
|
+
* @param contractAddresses - Checksummed enforcer addresses for the chain.
|
|
24
|
+
* @returns The token-approval-revocation permission decoder configuration.
|
|
25
|
+
*/
|
|
26
|
+
export function makeTokenApprovalRevocationDecoderConfig(contractAddresses) {
|
|
27
|
+
const { timestampEnforcer, approvalRevocationEnforcer, nonceEnforcer } = contractAddresses;
|
|
28
|
+
return {
|
|
29
|
+
permissionType: 'token-approval-revocation',
|
|
30
|
+
contractAddresses,
|
|
31
|
+
optionalEnforcers: [
|
|
32
|
+
timestampEnforcer, // expiry rule
|
|
33
|
+
],
|
|
34
|
+
requiredEnforcers: {
|
|
35
|
+
[approvalRevocationEnforcer]: 1,
|
|
36
|
+
[nonceEnforcer]: 1,
|
|
37
|
+
},
|
|
38
|
+
rules: [expiryRule],
|
|
39
|
+
validateAndDecodeData,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Decodes token-approval-revocation permission data from caveats; throws on invalid.
|
|
44
|
+
*
|
|
45
|
+
* @param caveats - Caveats from the permission context (checksummed).
|
|
46
|
+
* @param contractAddresses - Checksummed enforcer addresses for the chain.
|
|
47
|
+
* @returns Decoded approval-revocation capability flags.
|
|
48
|
+
*/
|
|
49
|
+
function validateAndDecodeData(caveats, contractAddresses) {
|
|
50
|
+
const { approvalRevocationEnforcer } = contractAddresses;
|
|
51
|
+
const terms = getTermsByEnforcer({
|
|
52
|
+
caveats,
|
|
53
|
+
enforcer: approvalRevocationEnforcer,
|
|
54
|
+
});
|
|
55
|
+
if (terms === '0x') {
|
|
56
|
+
throw new Error('Invalid ApprovalRevocation terms: must be greater than 0');
|
|
57
|
+
}
|
|
58
|
+
const mask = hexToNumber(terms);
|
|
59
|
+
if (mask > MAX_APPROVAL_REVOCATION_MASK) {
|
|
60
|
+
throw new Error(`Invalid ApprovalRevocation terms: must be less than or equal to ${MAX_APPROVAL_REVOCATION_MASK}`);
|
|
61
|
+
}
|
|
62
|
+
if (mask === 0) {
|
|
63
|
+
throw new Error('Invalid ApprovalRevocation terms: must be greater than 0');
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
erc20Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Erc20Approve),
|
|
67
|
+
erc721Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Erc721Approve),
|
|
68
|
+
erc721SetApprovalForAll: isFlagEnabled(mask, ApprovalRevocationFlag.Erc721SetApprovalForAll),
|
|
69
|
+
permit2Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Permit2Approve),
|
|
70
|
+
permit2Lockdown: isFlagEnabled(mask, ApprovalRevocationFlag.Permit2Lockdown),
|
|
71
|
+
permit2InvalidateNonces: isFlagEnabled(mask, ApprovalRevocationFlag.Permit2InvalidateNonces),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function isFlagEnabled(mask, flag) {
|
|
75
|
+
return (mask & flag) === flag;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=tokenApprovalRevocation.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokenApprovalRevocation.mjs","sourceRoot":"","sources":["../../../src/decodePermission/decoders/tokenApprovalRevocation.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,OAAO,EAAE,WAAW,EAAE,wBAAwB;AAO9C,OAAO,EAAE,kBAAkB,EAAE,qBAAiB;AAC9C,OAAO,EAAE,UAAU,EAAE,yBAAqB;AAG1C,IAAK,sBAOJ;AAPD,WAAK,sBAAsB;IACzB,mFAAmB,CAAA;IACnB,qFAAoB,CAAA;IACpB,yGAA8B,CAAA;IAC9B,uFAAqB,CAAA;IACrB,0FAAsB,CAAA;IACtB,0GAA8B,CAAA;AAChC,CAAC,EAPI,sBAAsB,KAAtB,sBAAsB,QAO1B;AAED,MAAM,4BAA4B,GAChC,sBAAsB,CAAC,uBAAuB;IAC9C,sBAAsB,CAAC,eAAe;IACtC,sBAAsB,CAAC,cAAc;IACrC,sBAAsB,CAAC,uBAAuB;IAC9C,sBAAsB,CAAC,aAAa;IACpC,sBAAsB,CAAC,YAAY,CAAC;AAEtC;;;;;GAKG;AACH,MAAM,UAAU,wCAAwC,CACtD,iBAA6C;IAE7C,MAAM,EAAE,iBAAiB,EAAE,0BAA0B,EAAE,aAAa,EAAE,GACpE,iBAAiB,CAAC;IAEpB,OAAO;QACL,cAAc,EAAE,2BAA2B;QAC3C,iBAAiB;QACjB,iBAAiB,EAAE;YACjB,iBAAiB,EAAE,cAAc;SAClC;QACD,iBAAiB,EAAE;YACjB,CAAC,0BAA0B,CAAC,EA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AAO,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,IAAI,CAAC;AAChC,CAAC","sourcesContent":["/* eslint-disable no-bitwise */\nimport { hexToNumber } from '@metamask/utils';\n\nimport type {\n ChecksumCaveat,\n ChecksumEnforcersByChainId,\n DecodedPermission,\n} from '../types';\nimport { getTermsByEnforcer } from '../utils';\nimport { expiryRule } from './expiryRule';\nimport type { MakePermissionDecoderConfig } from './makePermissionDecoder';\n\nenum ApprovalRevocationFlag {\n Erc20Approve = 0x01,\n Erc721Approve = 0x02,\n Erc721SetApprovalForAll = 0x04,\n Permit2Approve = 0x08,\n Permit2Lockdown = 0x10,\n Permit2InvalidateNonces = 0x20,\n}\n\nconst MAX_APPROVAL_REVOCATION_MASK =\n ApprovalRevocationFlag.Permit2InvalidateNonces |\n ApprovalRevocationFlag.Permit2Lockdown |\n ApprovalRevocationFlag.Permit2Approve |\n ApprovalRevocationFlag.Erc721SetApprovalForAll |\n ApprovalRevocationFlag.Erc721Approve |\n ApprovalRevocationFlag.Erc20Approve;\n\n/**\n * Builds the configuration for the token-approval-revocation permission decoder.\n *\n * @param contractAddresses - Checksummed enforcer addresses for the chain.\n * @returns The token-approval-revocation permission decoder configuration.\n */\nexport function makeTokenApprovalRevocationDecoderConfig(\n contractAddresses: ChecksumEnforcersByChainId,\n): MakePermissionDecoderConfig {\n const { timestampEnforcer, approvalRevocationEnforcer, nonceEnforcer } =\n contractAddresses;\n\n return {\n permissionType: 'token-approval-revocation',\n contractAddresses,\n optionalEnforcers: [\n timestampEnforcer, // expiry rule\n ],\n requiredEnforcers: {\n [approvalRevocationEnforcer]: 1,\n [nonceEnforcer]: 1,\n },\n rules: [expiryRule],\n validateAndDecodeData,\n };\n}\n\n/**\n * Decodes token-approval-revocation permission data from caveats; throws on invalid.\n *\n * @param caveats - Caveats from the permission context (checksummed).\n * @param contractAddresses - Checksummed enforcer addresses for the chain.\n * @returns Decoded approval-revocation capability flags.\n */\nfunction validateAndDecodeData(\n caveats: ChecksumCaveat[],\n contractAddresses: ChecksumEnforcersByChainId,\n): DecodedPermission['permission']['data'] {\n const { approvalRevocationEnforcer } = contractAddresses;\n\n const terms = getTermsByEnforcer({\n caveats,\n enforcer: approvalRevocationEnforcer,\n });\n\n if (terms === '0x') {\n throw new Error('Invalid ApprovalRevocation terms: must be greater than 0');\n }\n\n const mask = hexToNumber(terms);\n\n if (mask > MAX_APPROVAL_REVOCATION_MASK) {\n throw new Error(\n `Invalid ApprovalRevocation terms: must be less than or equal to ${MAX_APPROVAL_REVOCATION_MASK}`,\n );\n }\n\n if (mask === 0) {\n throw new Error('Invalid ApprovalRevocation terms: must be greater than 0');\n }\n\n return {\n erc20Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Erc20Approve),\n erc721Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Erc721Approve),\n erc721SetApprovalForAll: isFlagEnabled(\n mask,\n ApprovalRevocationFlag.Erc721SetApprovalForAll,\n ),\n permit2Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Permit2Approve),\n permit2Lockdown: isFlagEnabled(\n mask,\n ApprovalRevocationFlag.Permit2Lockdown,\n ),\n permit2InvalidateNonces: isFlagEnabled(\n mask,\n ApprovalRevocationFlag.Permit2InvalidateNonces,\n ),\n };\n}\n\nfunction isFlagEnabled(mask: number, flag: number): boolean {\n return (mask & flag) === flag;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.cjs","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n BasePermission,\n MetaMaskBasePermissionData,\n PermissionRequest,\n PermissionTypes,\n Rule,\n} from '@metamask/7715-permission-types';\nimport type {
|
|
1
|
+
{"version":3,"file":"types.cjs","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n BasePermission,\n MetaMaskBasePermissionData,\n PermissionRequest,\n PermissionTypes,\n Rule,\n} from '@metamask/7715-permission-types';\nimport type {\n ApprovalRevocationTerms,\n Caveat,\n} from '@metamask/delegation-core';\nimport type { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';\nimport type { Hex } from '@metamask/utils';\n\nexport type DeployedContractsByName =\n (typeof DELEGATOR_CONTRACTS)[number][number];\n\n/**\n * Permission type for an unbounded ERC-20 token allowance.\n *\n * Encoded on-chain as an ERC20PeriodTransferEnforcer caveat with\n * `periodDuration` set to `UINT256_MAX` so that the allowance never resets\n * within any realistic time horizon.\n *\n * Not yet defined in `@metamask/7715-permission-types`, so declared locally.\n */\ntype Erc20TokenAllowancePermission = BasePermission & {\n type: 'erc20-token-allowance';\n data: MetaMaskBasePermissionData & {\n allowanceAmount: Hex;\n startTime?: number | null;\n tokenAddress: Hex;\n };\n};\n\n/**\n * Permission type for an unbounded native token allowance.\n *\n * Encoded on-chain as a NativeTokenPeriodTransferEnforcer caveat with\n * `periodDuration` set to `UINT256_MAX`.\n *\n * Not yet defined in `@metamask/7715-permission-types`, so declared locally.\n */\ntype NativeTokenAllowancePermission = BasePermission & {\n type: 'native-token-allowance';\n data: MetaMaskBasePermissionData & {\n allowanceAmount: Hex;\n startTime?: number | null;\n };\n};\n\n/**\n * Permission type for token approval revocation.\n *\n * Not yet defined in `@metamask/7715-permission-types`, so declared locally.\n */\ntype TokenApprovalRevocationPermission = BasePermission & {\n type: 'token-approval-revocation';\n data: MetaMaskBasePermissionData & ApprovalRevocationTerms;\n};\n\n/**\n * Extended permission union, including types not yet published in\n * `@metamask/7715-permission-types` but supported by this package's decoder.\n */\ntype ExtendedPermissionTypes =\n | PermissionTypes\n | Erc20TokenAllowancePermission\n | NativeTokenAllowancePermission\n | TokenApprovalRevocationPermission;\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<PermissionTypes>,\n 'chainId' | 'from' | 'to'\n> & {\n permission: Omit<\n PermissionRequest<PermissionTypes>['permission'],\n 'isAdjustmentAllowed' | 'type' | 'data'\n > & {\n type: ExtendedPermissionTypes['type'];\n data: ExtendedPermissionTypes['data'];\n // PermissionRequest type does not work well without the specific permission type, so we amend it here\n justification?: string;\n };\n /**\n * @deprecated Use `rules` instead.\n */\n expiry: number | null;\n origin: string;\n /** Rules recovered from caveats (e.g. redeemer allowlist). */\n rules?: Rule[];\n};\n\n/**\n * Supported permission type identifiers that can be decoded from a permission context.\n */\nexport type PermissionType = DecodedPermission['permission']['type'];\n\n/**\n * Checksummed enforcer contract addresses for a chain (from getChecksumEnforcersByChainId).\n */\nexport type ChecksumEnforcersByChainId = {\n erc20StreamingEnforcer: Hex;\n erc20PeriodicEnforcer: Hex;\n nativeTokenStreamingEnforcer: Hex;\n nativeTokenPeriodicEnforcer: Hex;\n approvalRevocationEnforcer: Hex;\n exactCalldataEnforcer: Hex;\n valueLteEnforcer: Hex;\n timestampEnforcer: Hex;\n nonceEnforcer: Hex;\n allowedCalldataEnforcer: Hex;\n allowedTargetsEnforcer: Hex;\n redeemerEnforcer: Hex;\n};\n\n/** Caveat with checksummed enforcer address; used by rule decode functions. */\nexport type ChecksumCaveat = Caveat<Hex>;\n\n/**\n * Result of validating and decoding permission terms from caveats.\n * When valid, includes expiry and decoded data; when invalid, includes the error.\n */\nexport type ValidateAndDecodeResult =\n | {\n isValid: true;\n expiry: number | null;\n data: DecodedPermission['permission']['data'];\n rules?: Rule[];\n }\n | { isValid: false; error: Error };\n\n/**\n * A decoder that defines the required and optional enforcers for a permission\n * type, and provides methods to test whether caveat addresses match the\n * permission and to validate and decode permission terms from caveats.\n */\nexport type PermissionDecoder = {\n permissionType: PermissionType;\n requiredEnforcers: Map<Hex, number>;\n optionalEnforcers: Set<Hex>;\n /**\n * Returns true if the given caveat addresses (enforcer addresses) match this\n * decoder (required enforcers present with correct multiplicity, no\n * forbidden enforcers).\n */\n caveatAddressesMatch: (caveatAddresses: Hex[]) => boolean;\n /**\n * Validates and decodes permission terms from the caveats. Returns a result\n * object with isValid; when valid, includes expiry and data.\n */\n validateAndDecodePermission: (\n caveats: Caveat<Hex>[],\n ) => ValidateAndDecodeResult;\n};\n\n/**\n * A function that inspects checksummed caveats and optionally produces a\n * {@link Rule} (e.g. redeemer, payee, expiry). Each rule decoder is\n * responsible for a single rule type and is composed by\n * `makePermissionDecoder` to populate the permission's `rules` array. The\n * `expiry` rule, in addition to being appended to `rules`, has its value\n * hoisted onto the top-level `expiry` field of the decoded permission.\n */\nexport type RuleDecoder = (args: {\n contractAddresses: ChecksumEnforcersByChainId;\n caveats: ChecksumCaveat[];\n requiredEnforcers: Map<Hex, number>;\n}) => Rule | null;\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BasePermission, MetaMaskBasePermissionData, PermissionRequest, PermissionTypes, Rule } from "@metamask/7715-permission-types";
|
|
2
|
-
import type { Caveat } from "@metamask/delegation-core";
|
|
2
|
+
import type { ApprovalRevocationTerms, Caveat } from "@metamask/delegation-core";
|
|
3
3
|
import type { DELEGATOR_CONTRACTS } from "@metamask/delegation-deployments";
|
|
4
4
|
import type { Hex } from "@metamask/utils";
|
|
5
5
|
export type DeployedContractsByName = (typeof DELEGATOR_CONTRACTS)[number][number];
|
|
@@ -35,11 +35,20 @@ type NativeTokenAllowancePermission = BasePermission & {
|
|
|
35
35
|
startTime?: number | null;
|
|
36
36
|
};
|
|
37
37
|
};
|
|
38
|
+
/**
|
|
39
|
+
* Permission type for token approval revocation.
|
|
40
|
+
*
|
|
41
|
+
* Not yet defined in `@metamask/7715-permission-types`, so declared locally.
|
|
42
|
+
*/
|
|
43
|
+
type TokenApprovalRevocationPermission = BasePermission & {
|
|
44
|
+
type: 'token-approval-revocation';
|
|
45
|
+
data: MetaMaskBasePermissionData & ApprovalRevocationTerms;
|
|
46
|
+
};
|
|
38
47
|
/**
|
|
39
48
|
* Extended permission union, including types not yet published in
|
|
40
49
|
* `@metamask/7715-permission-types` but supported by this package's decoder.
|
|
41
50
|
*/
|
|
42
|
-
type ExtendedPermissionTypes = PermissionTypes | Erc20TokenAllowancePermission | NativeTokenAllowancePermission;
|
|
51
|
+
type ExtendedPermissionTypes = PermissionTypes | Erc20TokenAllowancePermission | NativeTokenAllowancePermission | TokenApprovalRevocationPermission;
|
|
43
52
|
/**
|
|
44
53
|
* A partially reconstructed permission object decoded from a permission context.
|
|
45
54
|
*
|
|
@@ -48,8 +57,10 @@ type ExtendedPermissionTypes = PermissionTypes | Erc20TokenAllowancePermission |
|
|
|
48
57
|
* augments the result with an explicit `expiry` property derived from the
|
|
49
58
|
* `TimestampEnforcer` terms, as well as the `origin` property.
|
|
50
59
|
*/
|
|
51
|
-
export type DecodedPermission = Pick<PermissionRequest<
|
|
52
|
-
permission: Omit<PermissionRequest<
|
|
60
|
+
export type DecodedPermission = Pick<PermissionRequest<PermissionTypes>, 'chainId' | 'from' | 'to'> & {
|
|
61
|
+
permission: Omit<PermissionRequest<PermissionTypes>['permission'], 'isAdjustmentAllowed' | 'type' | 'data'> & {
|
|
62
|
+
type: ExtendedPermissionTypes['type'];
|
|
63
|
+
data: ExtendedPermissionTypes['data'];
|
|
53
64
|
justification?: string;
|
|
54
65
|
};
|
|
55
66
|
/**
|
|
@@ -72,6 +83,7 @@ export type ChecksumEnforcersByChainId = {
|
|
|
72
83
|
erc20PeriodicEnforcer: Hex;
|
|
73
84
|
nativeTokenStreamingEnforcer: Hex;
|
|
74
85
|
nativeTokenPeriodicEnforcer: Hex;
|
|
86
|
+
approvalRevocationEnforcer: Hex;
|
|
75
87
|
exactCalldataEnforcer: Hex;
|
|
76
88
|
valueLteEnforcer: Hex;
|
|
77
89
|
timestampEnforcer: Hex;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,0BAA0B,EAC1B,iBAAiB,EACjB,eAAe,EACf,IAAI,EACL,wCAAwC;AACzC,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,0BAA0B,EAC1B,iBAAiB,EACjB,eAAe,EACf,IAAI,EACL,wCAAwC;AACzC,OAAO,KAAK,EACV,uBAAuB,EACvB,MAAM,EACP,kCAAkC;AACnC,OAAO,KAAK,EAAE,mBAAmB,EAAE,yCAAyC;AAC5E,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,MAAM,MAAM,uBAAuB,GACjC,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AAE/C;;;;;;;;GAQG;AACH,KAAK,6BAA6B,GAAG,cAAc,GAAG;IACpD,IAAI,EAAE,uBAAuB,CAAC;IAC9B,IAAI,EAAE,0BAA0B,GAAG;QACjC,eAAe,EAAE,GAAG,CAAC;QACrB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC1B,YAAY,EAAE,GAAG,CAAC;KACnB,CAAC;CACH,CAAC;AAEF;;;;;;;GAOG;AACH,KAAK,8BAA8B,GAAG,cAAc,GAAG;IACrD,IAAI,EAAE,wBAAwB,CAAC;IAC/B,IAAI,EAAE,0BAA0B,GAAG;QACjC,eAAe,EAAE,GAAG,CAAC;QACrB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;KAC3B,CAAC;CACH,CAAC;AAEF;;;;GAIG;AACH,KAAK,iCAAiC,GAAG,cAAc,GAAG;IACxD,IAAI,EAAE,2BAA2B,CAAC;IAClC,IAAI,EAAE,0BAA0B,GAAG,uBAAuB,CAAC;CAC5D,CAAC;AAEF;;;GAGG;AACH,KAAK,uBAAuB,GACxB,eAAe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;AAEzC;;;GAGG;AACH,MAAM,MAAM,uBAAuB,GAC/B;IACE,OAAO,EAAE,IAAI,CAAC;IACd,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,IAAI,EAAE,iBAAiB,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC;IAC9C,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC;CAChB,GACD;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC;AAErC;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,cAAc,EAAE,cAAc,CAAC;IAC/B,iBAAiB,EAAE,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IACpC,iBAAiB,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;IAC5B;;;;OAIG;IACH,oBAAoB,EAAE,CAAC,eAAe,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC;IAC1D;;;OAGG;IACH,2BAA2B,EAAE,CAC3B,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,KACnB,uBAAuB,CAAC;CAC9B,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE;IAC/B,iBAAiB,EAAE,0BAA0B,CAAC;IAC9C,OAAO,EAAE,cAAc,EAAE,CAAC;IAC1B,iBAAiB,EAAE,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;CACrC,KAAK,IAAI,GAAG,IAAI,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BasePermission, MetaMaskBasePermissionData, PermissionRequest, PermissionTypes, Rule } from "@metamask/7715-permission-types";
|
|
2
|
-
import type { Caveat } from "@metamask/delegation-core";
|
|
2
|
+
import type { ApprovalRevocationTerms, Caveat } from "@metamask/delegation-core";
|
|
3
3
|
import type { DELEGATOR_CONTRACTS } from "@metamask/delegation-deployments";
|
|
4
4
|
import type { Hex } from "@metamask/utils";
|
|
5
5
|
export type DeployedContractsByName = (typeof DELEGATOR_CONTRACTS)[number][number];
|
|
@@ -35,11 +35,20 @@ type NativeTokenAllowancePermission = BasePermission & {
|
|
|
35
35
|
startTime?: number | null;
|
|
36
36
|
};
|
|
37
37
|
};
|
|
38
|
+
/**
|
|
39
|
+
* Permission type for token approval revocation.
|
|
40
|
+
*
|
|
41
|
+
* Not yet defined in `@metamask/7715-permission-types`, so declared locally.
|
|
42
|
+
*/
|
|
43
|
+
type TokenApprovalRevocationPermission = BasePermission & {
|
|
44
|
+
type: 'token-approval-revocation';
|
|
45
|
+
data: MetaMaskBasePermissionData & ApprovalRevocationTerms;
|
|
46
|
+
};
|
|
38
47
|
/**
|
|
39
48
|
* Extended permission union, including types not yet published in
|
|
40
49
|
* `@metamask/7715-permission-types` but supported by this package's decoder.
|
|
41
50
|
*/
|
|
42
|
-
type ExtendedPermissionTypes = PermissionTypes | Erc20TokenAllowancePermission | NativeTokenAllowancePermission;
|
|
51
|
+
type ExtendedPermissionTypes = PermissionTypes | Erc20TokenAllowancePermission | NativeTokenAllowancePermission | TokenApprovalRevocationPermission;
|
|
43
52
|
/**
|
|
44
53
|
* A partially reconstructed permission object decoded from a permission context.
|
|
45
54
|
*
|
|
@@ -48,8 +57,10 @@ type ExtendedPermissionTypes = PermissionTypes | Erc20TokenAllowancePermission |
|
|
|
48
57
|
* augments the result with an explicit `expiry` property derived from the
|
|
49
58
|
* `TimestampEnforcer` terms, as well as the `origin` property.
|
|
50
59
|
*/
|
|
51
|
-
export type DecodedPermission = Pick<PermissionRequest<
|
|
52
|
-
permission: Omit<PermissionRequest<
|
|
60
|
+
export type DecodedPermission = Pick<PermissionRequest<PermissionTypes>, 'chainId' | 'from' | 'to'> & {
|
|
61
|
+
permission: Omit<PermissionRequest<PermissionTypes>['permission'], 'isAdjustmentAllowed' | 'type' | 'data'> & {
|
|
62
|
+
type: ExtendedPermissionTypes['type'];
|
|
63
|
+
data: ExtendedPermissionTypes['data'];
|
|
53
64
|
justification?: string;
|
|
54
65
|
};
|
|
55
66
|
/**
|
|
@@ -72,6 +83,7 @@ export type ChecksumEnforcersByChainId = {
|
|
|
72
83
|
erc20PeriodicEnforcer: Hex;
|
|
73
84
|
nativeTokenStreamingEnforcer: Hex;
|
|
74
85
|
nativeTokenPeriodicEnforcer: Hex;
|
|
86
|
+
approvalRevocationEnforcer: Hex;
|
|
75
87
|
exactCalldataEnforcer: Hex;
|
|
76
88
|
valueLteEnforcer: Hex;
|
|
77
89
|
timestampEnforcer: Hex;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,0BAA0B,EAC1B,iBAAiB,EACjB,eAAe,EACf,IAAI,EACL,wCAAwC;AACzC,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,0BAA0B,EAC1B,iBAAiB,EACjB,eAAe,EACf,IAAI,EACL,wCAAwC;AACzC,OAAO,KAAK,EACV,uBAAuB,EACvB,MAAM,EACP,kCAAkC;AACnC,OAAO,KAAK,EAAE,mBAAmB,EAAE,yCAAyC;AAC5E,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,MAAM,MAAM,uBAAuB,GACjC,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AAE/C;;;;;;;;GAQG;AACH,KAAK,6BAA6B,GAAG,cAAc,GAAG;IACpD,IAAI,EAAE,uBAAuB,CAAC;IAC9B,IAAI,EAAE,0BAA0B,GAAG;QACjC,eAAe,EAAE,GAAG,CAAC;QACrB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC1B,YAAY,EAAE,GAAG,CAAC;KACnB,CAAC;CACH,CA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C9C,OAAO,EAAE,cAAc,EAAE,CAAC;IAC1B,iBAAiB,EAAE,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;CACrC,KAAK,IAAI,GAAG,IAAI,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n BasePermission,\n MetaMaskBasePermissionData,\n PermissionRequest,\n PermissionTypes,\n Rule,\n} from '@metamask/7715-permission-types';\nimport type {
|
|
1
|
+
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n BasePermission,\n MetaMaskBasePermissionData,\n PermissionRequest,\n PermissionTypes,\n Rule,\n} from '@metamask/7715-permission-types';\nimport type {\n ApprovalRevocationTerms,\n Caveat,\n} from '@metamask/delegation-core';\nimport type { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';\nimport type { Hex } from '@metamask/utils';\n\nexport type DeployedContractsByName =\n (typeof DELEGATOR_CONTRACTS)[number][number];\n\n/**\n * Permission type for an unbounded ERC-20 token allowance.\n *\n * Encoded on-chain as an ERC20PeriodTransferEnforcer caveat with\n * `periodDuration` set to `UINT256_MAX` so that the allowance never resets\n * within any realistic time horizon.\n *\n * Not yet defined in `@metamask/7715-permission-types`, so declared locally.\n */\ntype Erc20TokenAllowancePermission = BasePermission & {\n type: 'erc20-token-allowance';\n data: MetaMaskBasePermissionData & {\n allowanceAmount: Hex;\n startTime?: number | null;\n tokenAddress: Hex;\n };\n};\n\n/**\n * Permission type for an unbounded native token allowance.\n *\n * Encoded on-chain as a NativeTokenPeriodTransferEnforcer caveat with\n * `periodDuration` set to `UINT256_MAX`.\n *\n * Not yet defined in `@metamask/7715-permission-types`, so declared locally.\n */\ntype NativeTokenAllowancePermission = BasePermission & {\n type: 'native-token-allowance';\n data: MetaMaskBasePermissionData & {\n allowanceAmount: Hex;\n startTime?: number | null;\n };\n};\n\n/**\n * Permission type for token approval revocation.\n *\n * Not yet defined in `@metamask/7715-permission-types`, so declared locally.\n */\ntype TokenApprovalRevocationPermission = BasePermission & {\n type: 'token-approval-revocation';\n data: MetaMaskBasePermissionData & ApprovalRevocationTerms;\n};\n\n/**\n * Extended permission union, including types not yet published in\n * `@metamask/7715-permission-types` but supported by this package's decoder.\n */\ntype ExtendedPermissionTypes =\n | PermissionTypes\n | Erc20TokenAllowancePermission\n | NativeTokenAllowancePermission\n | TokenApprovalRevocationPermission;\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<PermissionTypes>,\n 'chainId' | 'from' | 'to'\n> & {\n permission: Omit<\n PermissionRequest<PermissionTypes>['permission'],\n 'isAdjustmentAllowed' | 'type' | 'data'\n > & {\n type: ExtendedPermissionTypes['type'];\n data: ExtendedPermissionTypes['data'];\n // PermissionRequest type does not work well without the specific permission type, so we amend it here\n justification?: string;\n };\n /**\n * @deprecated Use `rules` instead.\n */\n expiry: number | null;\n origin: string;\n /** Rules recovered from caveats (e.g. redeemer allowlist). */\n rules?: Rule[];\n};\n\n/**\n * Supported permission type identifiers that can be decoded from a permission context.\n */\nexport type PermissionType = DecodedPermission['permission']['type'];\n\n/**\n * Checksummed enforcer contract addresses for a chain (from getChecksumEnforcersByChainId).\n */\nexport type ChecksumEnforcersByChainId = {\n erc20StreamingEnforcer: Hex;\n erc20PeriodicEnforcer: Hex;\n nativeTokenStreamingEnforcer: Hex;\n nativeTokenPeriodicEnforcer: Hex;\n approvalRevocationEnforcer: Hex;\n exactCalldataEnforcer: Hex;\n valueLteEnforcer: Hex;\n timestampEnforcer: Hex;\n nonceEnforcer: Hex;\n allowedCalldataEnforcer: Hex;\n allowedTargetsEnforcer: Hex;\n redeemerEnforcer: Hex;\n};\n\n/** Caveat with checksummed enforcer address; used by rule decode functions. */\nexport type ChecksumCaveat = Caveat<Hex>;\n\n/**\n * Result of validating and decoding permission terms from caveats.\n * When valid, includes expiry and decoded data; when invalid, includes the error.\n */\nexport type ValidateAndDecodeResult =\n | {\n isValid: true;\n expiry: number | null;\n data: DecodedPermission['permission']['data'];\n rules?: Rule[];\n }\n | { isValid: false; error: Error };\n\n/**\n * A decoder that defines the required and optional enforcers for a permission\n * type, and provides methods to test whether caveat addresses match the\n * permission and to validate and decode permission terms from caveats.\n */\nexport type PermissionDecoder = {\n permissionType: PermissionType;\n requiredEnforcers: Map<Hex, number>;\n optionalEnforcers: Set<Hex>;\n /**\n * Returns true if the given caveat addresses (enforcer addresses) match this\n * decoder (required enforcers present with correct multiplicity, no\n * forbidden enforcers).\n */\n caveatAddressesMatch: (caveatAddresses: Hex[]) => boolean;\n /**\n * Validates and decodes permission terms from the caveats. Returns a result\n * object with isValid; when valid, includes expiry and data.\n */\n validateAndDecodePermission: (\n caveats: Caveat<Hex>[],\n ) => ValidateAndDecodeResult;\n};\n\n/**\n * A function that inspects checksummed caveats and optionally produces a\n * {@link Rule} (e.g. redeemer, payee, expiry). Each rule decoder is\n * responsible for a single rule type and is composed by\n * `makePermissionDecoder` to populate the permission's `rules` array. The\n * `expiry` rule, in addition to being appended to `rules`, has its value\n * hoisted onto the top-level `expiry` field of the decoded permission.\n */\nexport type RuleDecoder = (args: {\n contractAddresses: ChecksumEnforcersByChainId;\n caveats: ChecksumCaveat[];\n requiredEnforcers: Map<Hex, number>;\n}) => Rule | null;\n"]}
|
|
@@ -8,6 +8,7 @@ const utils_1 = require("@metamask/utils");
|
|
|
8
8
|
const ENFORCER_CONTRACT_NAMES = {
|
|
9
9
|
ERC20PeriodTransferEnforcer: 'ERC20PeriodTransferEnforcer',
|
|
10
10
|
ERC20StreamingEnforcer: 'ERC20StreamingEnforcer',
|
|
11
|
+
ApprovalRevocationEnforcer: 'ApprovalRevocationEnforcer',
|
|
11
12
|
ExactCalldataEnforcer: 'ExactCalldataEnforcer',
|
|
12
13
|
NativeTokenPeriodTransferEnforcer: 'NativeTokenPeriodTransferEnforcer',
|
|
13
14
|
NativeTokenStreamingEnforcer: 'NativeTokenStreamingEnforcer',
|
|
@@ -63,6 +64,7 @@ const getChecksumEnforcersByChainId = (contracts) => {
|
|
|
63
64
|
const erc20PeriodicEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ERC20PeriodTransferEnforcer);
|
|
64
65
|
const nativeTokenStreamingEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NativeTokenStreamingEnforcer);
|
|
65
66
|
const nativeTokenPeriodicEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NativeTokenPeriodTransferEnforcer);
|
|
67
|
+
const approvalRevocationEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ApprovalRevocationEnforcer);
|
|
66
68
|
// general enforcers
|
|
67
69
|
const exactCalldataEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ExactCalldataEnforcer);
|
|
68
70
|
const valueLteEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ValueLteEnforcer);
|
|
@@ -76,6 +78,7 @@ const getChecksumEnforcersByChainId = (contracts) => {
|
|
|
76
78
|
erc20PeriodicEnforcer,
|
|
77
79
|
nativeTokenStreamingEnforcer,
|
|
78
80
|
nativeTokenPeriodicEnforcer,
|
|
81
|
+
approvalRevocationEnforcer,
|
|
79
82
|
exactCalldataEnforcer,
|
|
80
83
|
valueLteEnforcer,
|
|
81
84
|
timestampEnforcer,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":";;;AACA,2CAAkE;AAQlE;;GAEG;AACH,MAAM,uBAAuB,GAA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AAC,CAAC;QACtC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,cAAc,CAAC,CAAC;QACxD,KAAK,IAAI,cAAc,CAAC;QACxB,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,EAAW,CAAC,CAAC;IACnC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAVD,4BAUC","sourcesContent":["import type { Caveat } from '@metamask/delegation-core';\nimport { getChecksumAddress, hexToNumber } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type {\n ChecksumEnforcersByChainId,\n DeployedContractsByName,\n} from './types';\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 AllowedCalldataEnforcer: 'AllowedCalldataEnforcer',\n AllowedTargetsEnforcer: 'AllowedTargetsEnforcer',\n RedeemerEnforcer: 'RedeemerEnforcer',\n};\n\n/**\n * 32 bytes of zero (0x + 64 hex chars).\n */\nexport const ZERO_32_BYTES =\n '0x0000000000000000000000000000000000000000000000000000000000000000' as const;\n\n/**\n * Maximum unsigned 256-bit integer encoded as 32 bytes (0x + 64 hex chars).\n */\nexport const UINT256_MAX =\n '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' as const;\n\n/** AllowedCalldataEnforcer terms for ERC20 approve selector. */\nexport const ERC20_APPROVE_SELECTOR_TERMS =\n '0x0000000000000000000000000000000000000000000000000000000000000000095ea7b3' as const;\n\n/** AllowedCalldataEnforcer terms for ERC20 approve zero amount. */\nexport const ERC20_APPROVE_ZERO_AMOUNT_TERMS =\n '0x00000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000000' as const;\n\n/** Maximum period duration in seconds. */\nexport const MAX_PERIOD_DURATION = 10 * 365 * 24 * 60 * 60; // 10 years in seconds\n\n/**\n * Get the byte length of a hex string.\n *\n * @param hexString - The hex string to get the byte length of.\n * @returns The byte length of the hex string.\n */\nexport const getByteLength = (hexString: Hex): number => {\n return (hexString.length - 2) / 2;\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): ChecksumEnforcersByChainId => {\n const getChecksumContractAddress = (contractName: string): Hex => {\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 const allowedCalldataEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.AllowedCalldataEnforcer,\n );\n\n const allowedTargetsEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.AllowedTargetsEnforcer,\n );\n\n const redeemerEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.RedeemerEnforcer,\n );\n\n return {\n erc20StreamingEnforcer,\n erc20PeriodicEnforcer,\n nativeTokenStreamingEnforcer,\n nativeTokenPeriodicEnforcer,\n exactCalldataEnforcer,\n valueLteEnforcer,\n timestampEnforcer,\n nonceEnforcer,\n allowedCalldataEnforcer,\n allowedTargetsEnforcer,\n redeemerEnforcer,\n };\n};\n\n/**\n * Extracts the expiry timestamp from TimestampEnforcer caveat terms.\n * Terms are 32 bytes: first 16 bytes timestampAfterThreshold (must be 0),\n * last 16 bytes timestampBeforeThreshold (expiry).\n *\n * @param terms - The hex-encoded terms from a TimestampEnforcer caveat.\n * @returns The expiry timestamp in seconds.\n * @throws If terms are invalid.\n */\nexport const extractExpiryFromCaveatTerms = (terms: Hex): number => {\n if (terms.length !== 66) {\n throw new Error(\n `Invalid TimestampEnforcer terms length: expected 66 characters (0x + 64 hex), got ${terms.length}`,\n );\n }\n const [after, before] = splitHex(terms, [16, 16]);\n if (hexToNumber(after) !== 0) {\n throw new Error('Invalid expiry: timestampAfterThreshold must be 0');\n }\n const expiry = hexToNumber(before);\n if (expiry === 0) {\n throw new Error(\n 'Invalid expiry: timestampBeforeThreshold must be greater than 0',\n );\n }\n return expiry;\n};\n\n/**\n * Builds enforcer counts and set from caveat addresses (checksummed).\n * Used by caveatAddressesMatch.\n *\n * @param caveatAddresses - List of enforcer contract addresses (hex).\n * @returns Counts per enforcer and set of unique enforcers.\n */\nexport function buildEnforcerCountsAndSet(caveatAddresses: Hex[]): {\n counts: Map<Hex, number>;\n enforcersSet: Set<Hex>;\n} {\n const counts = new Map<Hex, number>();\n for (const addr of caveatAddresses.map(getChecksumAddress)) {\n counts.set(addr, (counts.get(addr) ?? 0) + 1);\n }\n return { counts, enforcersSet: new Set(counts.keys()) };\n}\n\n/**\n * Returns true if the given counts/set match the rule (required counts exact,\n * no enforcer outside required + optional).\n *\n * @param counts - Map of enforcer address to occurrence count.\n * @param enforcersSet - Set of unique enforcer addresses present.\n * @param requiredEnforcers - Map of required enforcer to required count.\n * @param optionalEnforcers - Set of optional enforcer addresses.\n * @returns True if the counts match the rule.\n */\nexport function enforcersMatchRule(\n counts: Map<Hex, number>,\n enforcersSet: Set<Hex>,\n requiredEnforcers: Map<Hex, number>,\n optionalEnforcers: Set<Hex>,\n): boolean {\n const allowedEnforcers = new Set<Hex>([\n ...optionalEnforcers,\n ...requiredEnforcers.keys(),\n ]);\n for (const addr of enforcersSet) {\n if (!allowedEnforcers.has(addr)) {\n return false;\n }\n }\n for (const [addr, requiredCount] of requiredEnforcers.entries()) {\n if ((counts.get(addr) ?? 0) !== requiredCount) {\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 const);\n }\n return parts;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":";;;AACA,2CAAkE;AAQlE;;GAEG;AACH,MAAM,uBAAuB,GAAG;IAC9B,2BAA2B,EAAE,6BAA6B;IAC1D,sBAAsB,EAAE,wBAAwB;IAChD,0BAA0B,EAAE,4BAA4B;IACxD,qBAAqB,EAAE,uBAAuB;IAC9C,iCAAiC,EAAE,mCAAmC;IACtE,4BAA4B,EAAE,8BAA8B;IAC5D,iBAAiB,EAAE,mBAAmB;IACtC,gBAAgB,EAAE,kBAAkB;IACpC,aAAa,EAAE,eAAe;IAC9B,uBAAuB,EAAE,yBAAyB;IAClD,sBAAsB,EAAE,wBAAwB;IAChD,gBAAgB,EAAE,kBAAkB;CACrC,CAAC;AAEF;;GAEG;AACU,QAAA,aAAa,GACxB,oEAA6E,CAAC;AAEhF;;GAEG;AACU,QAAA,WAAW,GACtB,oEAA6E,CAAC;AAEhF,gEAAgE;AACn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CAAC;AACd,CAAC;AArBD,gDAqBC;AAED;;;;;;;;GAQG;AACH,SAAgB,kBAAkB,CAA0C,EAC1E,OAAO,EACP,QAAQ,EACR,eAAe,GAKhB;IACC,MAAM,eAAe,GAAG,OAAO,CAAC,MAAM,CACpC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,KAAK,QAAQ,CACzC,CAAC;IAEF,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjC,IAAI,eAAe,IAAI,IAAI,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;QACrC,CAAC;QACD,OAAO,IAAwD,CAAC;IAClE,CAAC;IAED,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;IAED,OAAO,eAAe,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AAClC,CAAC;AAzBD,gDAyBC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,QAAQ,CAAC,KAAU,EAAE,OAAiB;IACpD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,MAAM,KAAK,GAAU,EAAE,CAAC;IACxB,KAAK,MAAM,UAAU,IAAI,OAAO,EAAE,CAAC;QACjC,MAAM,cAAc,GAAG,UAAU,GAAG,CAAC,CAAC;QACtC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,cAAc,CAAC,CAAC;QACxD,KAAK,IAAI,cAAc,CAAC;QACxB,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,EAAW,CAAC,CAAC;IACnC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAVD,4BAUC","sourcesContent":["import type { Caveat } from '@metamask/delegation-core';\nimport { getChecksumAddress, hexToNumber } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type {\n ChecksumEnforcersByChainId,\n DeployedContractsByName,\n} from './types';\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 ApprovalRevocationEnforcer: 'ApprovalRevocationEnforcer',\n ExactCalldataEnforcer: 'ExactCalldataEnforcer',\n NativeTokenPeriodTransferEnforcer: 'NativeTokenPeriodTransferEnforcer',\n NativeTokenStreamingEnforcer: 'NativeTokenStreamingEnforcer',\n TimestampEnforcer: 'TimestampEnforcer',\n ValueLteEnforcer: 'ValueLteEnforcer',\n NonceEnforcer: 'NonceEnforcer',\n AllowedCalldataEnforcer: 'AllowedCalldataEnforcer',\n AllowedTargetsEnforcer: 'AllowedTargetsEnforcer',\n RedeemerEnforcer: 'RedeemerEnforcer',\n};\n\n/**\n * 32 bytes of zero (0x + 64 hex chars).\n */\nexport const ZERO_32_BYTES =\n '0x0000000000000000000000000000000000000000000000000000000000000000' as const;\n\n/**\n * Maximum unsigned 256-bit integer encoded as 32 bytes (0x + 64 hex chars).\n */\nexport const UINT256_MAX =\n '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' as const;\n\n/** AllowedCalldataEnforcer terms for ERC20 approve selector. */\nexport const ERC20_APPROVE_SELECTOR_TERMS =\n '0x0000000000000000000000000000000000000000000000000000000000000000095ea7b3' as const;\n\n/** AllowedCalldataEnforcer terms for ERC20 approve zero amount. */\nexport const ERC20_APPROVE_ZERO_AMOUNT_TERMS =\n '0x00000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000000' as const;\n\n/** Maximum period duration in seconds. */\nexport const MAX_PERIOD_DURATION = 10 * 365 * 24 * 60 * 60; // 10 years in seconds\n\n/**\n * Get the byte length of a hex string.\n *\n * @param hexString - The hex string to get the byte length of.\n * @returns The byte length of the hex string.\n */\nexport const getByteLength = (hexString: Hex): number => {\n return (hexString.length - 2) / 2;\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): ChecksumEnforcersByChainId => {\n const getChecksumContractAddress = (contractName: string): Hex => {\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 const approvalRevocationEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.ApprovalRevocationEnforcer,\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 const allowedCalldataEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.AllowedCalldataEnforcer,\n );\n\n const allowedTargetsEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.AllowedTargetsEnforcer,\n );\n\n const redeemerEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.RedeemerEnforcer,\n );\n\n return {\n erc20StreamingEnforcer,\n erc20PeriodicEnforcer,\n nativeTokenStreamingEnforcer,\n nativeTokenPeriodicEnforcer,\n approvalRevocationEnforcer,\n exactCalldataEnforcer,\n valueLteEnforcer,\n timestampEnforcer,\n nonceEnforcer,\n allowedCalldataEnforcer,\n allowedTargetsEnforcer,\n redeemerEnforcer,\n };\n};\n\n/**\n * Extracts the expiry timestamp from TimestampEnforcer caveat terms.\n * Terms are 32 bytes: first 16 bytes timestampAfterThreshold (must be 0),\n * last 16 bytes timestampBeforeThreshold (expiry).\n *\n * @param terms - The hex-encoded terms from a TimestampEnforcer caveat.\n * @returns The expiry timestamp in seconds.\n * @throws If terms are invalid.\n */\nexport const extractExpiryFromCaveatTerms = (terms: Hex): number => {\n if (terms.length !== 66) {\n throw new Error(\n `Invalid TimestampEnforcer terms length: expected 66 characters (0x + 64 hex), got ${terms.length}`,\n );\n }\n const [after, before] = splitHex(terms, [16, 16]);\n if (hexToNumber(after) !== 0) {\n throw new Error('Invalid expiry: timestampAfterThreshold must be 0');\n }\n const expiry = hexToNumber(before);\n if (expiry === 0) {\n throw new Error(\n 'Invalid expiry: timestampBeforeThreshold must be greater than 0',\n );\n }\n return expiry;\n};\n\n/**\n * Builds enforcer counts and set from caveat addresses (checksummed).\n * Used by caveatAddressesMatch.\n *\n * @param caveatAddresses - List of enforcer contract addresses (hex).\n * @returns Counts per enforcer and set of unique enforcers.\n */\nexport function buildEnforcerCountsAndSet(caveatAddresses: Hex[]): {\n counts: Map<Hex, number>;\n enforcersSet: Set<Hex>;\n} {\n const counts = new Map<Hex, number>();\n for (const addr of caveatAddresses.map(getChecksumAddress)) {\n counts.set(addr, (counts.get(addr) ?? 0) + 1);\n }\n return { counts, enforcersSet: new Set(counts.keys()) };\n}\n\n/**\n * Returns true if the given counts/set match the rule (required counts exact,\n * no enforcer outside required + optional).\n *\n * @param counts - Map of enforcer address to occurrence count.\n * @param enforcersSet - Set of unique enforcer addresses present.\n * @param requiredEnforcers - Map of required enforcer to required count.\n * @param optionalEnforcers - Set of optional enforcer addresses.\n * @returns True if the counts match the rule.\n */\nexport function enforcersMatchRule(\n counts: Map<Hex, number>,\n enforcersSet: Set<Hex>,\n requiredEnforcers: Map<Hex, number>,\n optionalEnforcers: Set<Hex>,\n): boolean {\n const allowedEnforcers = new Set<Hex>([\n ...optionalEnforcers,\n ...requiredEnforcers.keys(),\n ]);\n for (const addr of enforcersSet) {\n if (!allowedEnforcers.has(addr)) {\n return false;\n }\n }\n for (const [addr, requiredCount] of requiredEnforcers.entries()) {\n if ((counts.get(addr) ?? 0) !== requiredCount) {\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 const);\n }\n return parts;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,kCAAkC;AAExD,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EACV,0BAA0B,EAC1B,uBAAuB,EACxB,oBAAgB;
|
|
1
|
+
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,kCAAkC;AAExD,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EACV,0BAA0B,EAC1B,uBAAuB,EACxB,oBAAgB;AAoBjB;;GAEG;AACH,eAAO,MAAM,aAAa,sEACqD,CAAC;AAEhF;;GAEG;AACH,eAAO,MAAM,WAAW,sEACuD,CAAC;AAEhF,gEAAgE;AAChE,eAAO,MAAM,4BAA4B,8EAC8C,CAAC;AAExF,mEAAmE;AACnE,eAAO,MAAM,+BAA+B,sIACmG,CAAC;AAEhJ,0CAA0C;AAC1C,eAAO,MAAM,mBAAmB,QAA0B,CAAC;AAE3D;;;;;GAKG;AACH,eAAO,MAAM,aAAa,cAAe,GAAG,KAAG,MAE9C,CAAC;AAEF;;;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AiB,EAAE,GAAG,CAAC,GAAG,CAAC,GAC1B,OAAO,CAgBT;AAED;;;;;;;;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"}
|
|
@@ -5,6 +5,7 @@ import { getChecksumAddress, hexToNumber } from "@metamask/utils";
|
|
|
5
5
|
const ENFORCER_CONTRACT_NAMES = {
|
|
6
6
|
ERC20PeriodTransferEnforcer: 'ERC20PeriodTransferEnforcer',
|
|
7
7
|
ERC20StreamingEnforcer: 'ERC20StreamingEnforcer',
|
|
8
|
+
ApprovalRevocationEnforcer: 'ApprovalRevocationEnforcer',
|
|
8
9
|
ExactCalldataEnforcer: 'ExactCalldataEnforcer',
|
|
9
10
|
NativeTokenPeriodTransferEnforcer: 'NativeTokenPeriodTransferEnforcer',
|
|
10
11
|
NativeTokenStreamingEnforcer: 'NativeTokenStreamingEnforcer',
|
|
@@ -59,6 +60,7 @@ export const getChecksumEnforcersByChainId = (contracts) => {
|
|
|
59
60
|
const erc20PeriodicEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ERC20PeriodTransferEnforcer);
|
|
60
61
|
const nativeTokenStreamingEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NativeTokenStreamingEnforcer);
|
|
61
62
|
const nativeTokenPeriodicEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NativeTokenPeriodTransferEnforcer);
|
|
63
|
+
const approvalRevocationEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ApprovalRevocationEnforcer);
|
|
62
64
|
// general enforcers
|
|
63
65
|
const exactCalldataEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ExactCalldataEnforcer);
|
|
64
66
|
const valueLteEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ValueLteEnforcer);
|
|
@@ -72,6 +74,7 @@ export const getChecksumEnforcersByChainId = (contracts) => {
|
|
|
72
74
|
erc20PeriodicEnforcer,
|
|
73
75
|
nativeTokenStreamingEnforcer,
|
|
74
76
|
nativeTokenPeriodicEnforcer,
|
|
77
|
+
approvalRevocationEnforcer,
|
|
75
78
|
exactCalldataEnforcer,
|
|
76
79
|
valueLteEnforcer,
|
|
77
80
|
timestampEnforcer,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,wBAAwB;AAQlE;;GAEG;AACH,MAAM,uBAAuB,GAAG;IAC9B,2BAA2B,EAAE,6BAA6B;IAC1D,sBAAsB,EAAE,wBAAwB;IAChD,qBAAqB,EAAE,uBAAuB;IAC9C,iCAAiC,EAAE,mCAAmC;IACtE,4BAA4B,EAAE,8BAA8B;IAC5D,iBAAiB,EAAE,mBAAmB;IACtC,gBAAgB,EAAE,kBAAkB;IACpC,aAAa,EAAE,eAAe;IAC9B,uBAAuB,EAAE,yBAAyB;IAClD,sBAAsB,EAAE,wBAAwB;IAChD,gBAAgB,EAAE,kBAAkB;CACrC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GACxB,oEAA6E,CAAC;AAEhF;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GACtB,oEAA6E,CAAC;AAEhF,gEAAgE;AAChE,MA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G,OAAO,CAAC,MAAM,CACpC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,KAAK,QAAQ,CACzC,CAAC;IAEF,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjC,IAAI,eAAe,IAAI,IAAI,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;QACrC,CAAC;QACD,OAAO,IAAwD,CAAC;IAClE,CAAC;IAED,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;IAED,OAAO,eAAe,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AAClC,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAU,EAAE,OAAiB;IACpD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,MAAM,KAAK,GAAU,EAAE,CAAC;IACxB,KAAK,MAAM,UAAU,IAAI,OAAO,EAAE,CAAC;QACjC,MAAM,cAAc,GAAG,UAAU,GAAG,CAAC,CAAC;QACtC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,cAAc,CAAC,CAAC;QACxD,KAAK,IAAI,cAAc,CAAC;QACxB,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,EAAW,CAAC,CAAC;IACnC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import type { Caveat } from '@metamask/delegation-core';\nimport { getChecksumAddress, hexToNumber } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type {\n ChecksumEnforcersByChainId,\n DeployedContractsByName,\n} from './types';\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 AllowedCalldataEnforcer: 'AllowedCalldataEnforcer',\n AllowedTargetsEnforcer: 'AllowedTargetsEnforcer',\n RedeemerEnforcer: 'RedeemerEnforcer',\n};\n\n/**\n * 32 bytes of zero (0x + 64 hex chars).\n */\nexport const ZERO_32_BYTES =\n '0x0000000000000000000000000000000000000000000000000000000000000000' as const;\n\n/**\n * Maximum unsigned 256-bit integer encoded as 32 bytes (0x + 64 hex chars).\n */\nexport const UINT256_MAX =\n '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' as const;\n\n/** AllowedCalldataEnforcer terms for ERC20 approve selector. */\nexport const ERC20_APPROVE_SELECTOR_TERMS =\n '0x0000000000000000000000000000000000000000000000000000000000000000095ea7b3' as const;\n\n/** AllowedCalldataEnforcer terms for ERC20 approve zero amount. */\nexport const ERC20_APPROVE_ZERO_AMOUNT_TERMS =\n '0x00000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000000' as const;\n\n/** Maximum period duration in seconds. */\nexport const MAX_PERIOD_DURATION = 10 * 365 * 24 * 60 * 60; // 10 years in seconds\n\n/**\n * Get the byte length of a hex string.\n *\n * @param hexString - The hex string to get the byte length of.\n * @returns The byte length of the hex string.\n */\nexport const getByteLength = (hexString: Hex): number => {\n return (hexString.length - 2) / 2;\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): ChecksumEnforcersByChainId => {\n const getChecksumContractAddress = (contractName: string): Hex => {\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 const allowedCalldataEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.AllowedCalldataEnforcer,\n );\n\n const allowedTargetsEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.AllowedTargetsEnforcer,\n );\n\n const redeemerEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.RedeemerEnforcer,\n );\n\n return {\n erc20StreamingEnforcer,\n erc20PeriodicEnforcer,\n nativeTokenStreamingEnforcer,\n nativeTokenPeriodicEnforcer,\n exactCalldataEnforcer,\n valueLteEnforcer,\n timestampEnforcer,\n nonceEnforcer,\n allowedCalldataEnforcer,\n allowedTargetsEnforcer,\n redeemerEnforcer,\n };\n};\n\n/**\n * Extracts the expiry timestamp from TimestampEnforcer caveat terms.\n * Terms are 32 bytes: first 16 bytes timestampAfterThreshold (must be 0),\n * last 16 bytes timestampBeforeThreshold (expiry).\n *\n * @param terms - The hex-encoded terms from a TimestampEnforcer caveat.\n * @returns The expiry timestamp in seconds.\n * @throws If terms are invalid.\n */\nexport const extractExpiryFromCaveatTerms = (terms: Hex): number => {\n if (terms.length !== 66) {\n throw new Error(\n `Invalid TimestampEnforcer terms length: expected 66 characters (0x + 64 hex), got ${terms.length}`,\n );\n }\n const [after, before] = splitHex(terms, [16, 16]);\n if (hexToNumber(after) !== 0) {\n throw new Error('Invalid expiry: timestampAfterThreshold must be 0');\n }\n const expiry = hexToNumber(before);\n if (expiry === 0) {\n throw new Error(\n 'Invalid expiry: timestampBeforeThreshold must be greater than 0',\n );\n }\n return expiry;\n};\n\n/**\n * Builds enforcer counts and set from caveat addresses (checksummed).\n * Used by caveatAddressesMatch.\n *\n * @param caveatAddresses - List of enforcer contract addresses (hex).\n * @returns Counts per enforcer and set of unique enforcers.\n */\nexport function buildEnforcerCountsAndSet(caveatAddresses: Hex[]): {\n counts: Map<Hex, number>;\n enforcersSet: Set<Hex>;\n} {\n const counts = new Map<Hex, number>();\n for (const addr of caveatAddresses.map(getChecksumAddress)) {\n counts.set(addr, (counts.get(addr) ?? 0) + 1);\n }\n return { counts, enforcersSet: new Set(counts.keys()) };\n}\n\n/**\n * Returns true if the given counts/set match the rule (required counts exact,\n * no enforcer outside required + optional).\n *\n * @param counts - Map of enforcer address to occurrence count.\n * @param enforcersSet - Set of unique enforcer addresses present.\n * @param requiredEnforcers - Map of required enforcer to required count.\n * @param optionalEnforcers - Set of optional enforcer addresses.\n * @returns True if the counts match the rule.\n */\nexport function enforcersMatchRule(\n counts: Map<Hex, number>,\n enforcersSet: Set<Hex>,\n requiredEnforcers: Map<Hex, number>,\n optionalEnforcers: Set<Hex>,\n): boolean {\n const allowedEnforcers = new Set<Hex>([\n ...optionalEnforcers,\n ...requiredEnforcers.keys(),\n ]);\n for (const addr of enforcersSet) {\n if (!allowedEnforcers.has(addr)) {\n return false;\n }\n }\n for (const [addr, requiredCount] of requiredEnforcers.entries()) {\n if ((counts.get(addr) ?? 0) !== requiredCount) {\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 const);\n }\n return parts;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,wBAAwB;AAQlE;;GAEG;AACH,MAAM,uBAAuB,GAAG;IAC9B,2BAA2B,EAAE,6BAA6B;IAC1D,sBAAsB,EAAE,wBAAwB;IAChD,0BAA0B,EAAE,4BAA4B;IACxD,qBAAqB,EAAE,uBAAuB;IAC9C,iCAAiC,EAAE,mCAAmC;IACtE,4BAA4B,EAAE,8BAA8B;IAC5D,iBAAiB,EAAE,mBAAmB;IACtC,gBAAgB,EAAE,kBAAkB;IACpC,aAAa,EAAE,eAAe;IAC9B,uBAAuB,EAAE,yBAAyB;IAClD,sBAAsB,EAAE,wBAAwB;IAChD,gBAAgB,EAAE,kBAAkB;CACrC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GACxB,oEAA6E,CAAC;AAEhF;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GACtB,oEAA6E,CAAC;AAEhF,gEAAgE;AAChE,MAAM,CAAC,MAAM,4BAA4B,GACvC,4EAAqF,CAAC;AAExF,mEAAmE;AACnE,MAAM,CAAC,MAAM,+BAA+B,GAC1C,oIAA6I,CAAC;AAEhJ,0CAA0C;AAC1C,MAAM,CAAC,MAAM,mBAAmB,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,sBAAsB;AAElF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,SAAc,EAAU,EAAE;IACtD,OAAO,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;AACpC,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAA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K,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;IAED,OAAO,eAAe,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AAClC,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAU,EAAE,OAAiB;IACpD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,MAAM,KAAK,GAAU,EAAE,CAAC;IACxB,KAAK,MAAM,UAAU,IAAI,OAAO,EAAE,CAAC;QACjC,MAAM,cAAc,GAAG,UAAU,GAAG,CAAC,CAAC;QACtC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,cAAc,CAAC,CAAC;QACxD,KAAK,IAAI,cAAc,CAAC;QACxB,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,EAAW,CAAC,CAAC;IACnC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import type { Caveat } from '@metamask/delegation-core';\nimport { getChecksumAddress, hexToNumber } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type {\n ChecksumEnforcersByChainId,\n DeployedContractsByName,\n} from './types';\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 ApprovalRevocationEnforcer: 'ApprovalRevocationEnforcer',\n ExactCalldataEnforcer: 'ExactCalldataEnforcer',\n NativeTokenPeriodTransferEnforcer: 'NativeTokenPeriodTransferEnforcer',\n NativeTokenStreamingEnforcer: 'NativeTokenStreamingEnforcer',\n TimestampEnforcer: 'TimestampEnforcer',\n ValueLteEnforcer: 'ValueLteEnforcer',\n NonceEnforcer: 'NonceEnforcer',\n AllowedCalldataEnforcer: 'AllowedCalldataEnforcer',\n AllowedTargetsEnforcer: 'AllowedTargetsEnforcer',\n RedeemerEnforcer: 'RedeemerEnforcer',\n};\n\n/**\n * 32 bytes of zero (0x + 64 hex chars).\n */\nexport const ZERO_32_BYTES =\n '0x0000000000000000000000000000000000000000000000000000000000000000' as const;\n\n/**\n * Maximum unsigned 256-bit integer encoded as 32 bytes (0x + 64 hex chars).\n */\nexport const UINT256_MAX =\n '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' as const;\n\n/** AllowedCalldataEnforcer terms for ERC20 approve selector. */\nexport const ERC20_APPROVE_SELECTOR_TERMS =\n '0x0000000000000000000000000000000000000000000000000000000000000000095ea7b3' as const;\n\n/** AllowedCalldataEnforcer terms for ERC20 approve zero amount. */\nexport const ERC20_APPROVE_ZERO_AMOUNT_TERMS =\n '0x00000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000000' as const;\n\n/** Maximum period duration in seconds. */\nexport const MAX_PERIOD_DURATION = 10 * 365 * 24 * 60 * 60; // 10 years in seconds\n\n/**\n * Get the byte length of a hex string.\n *\n * @param hexString - The hex string to get the byte length of.\n * @returns The byte length of the hex string.\n */\nexport const getByteLength = (hexString: Hex): number => {\n return (hexString.length - 2) / 2;\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): ChecksumEnforcersByChainId => {\n const getChecksumContractAddress = (contractName: string): Hex => {\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 const approvalRevocationEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.ApprovalRevocationEnforcer,\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 const allowedCalldataEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.AllowedCalldataEnforcer,\n );\n\n const allowedTargetsEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.AllowedTargetsEnforcer,\n );\n\n const redeemerEnforcer = getChecksumContractAddress(\n ENFORCER_CONTRACT_NAMES.RedeemerEnforcer,\n );\n\n return {\n erc20StreamingEnforcer,\n erc20PeriodicEnforcer,\n nativeTokenStreamingEnforcer,\n nativeTokenPeriodicEnforcer,\n approvalRevocationEnforcer,\n exactCalldataEnforcer,\n valueLteEnforcer,\n timestampEnforcer,\n nonceEnforcer,\n allowedCalldataEnforcer,\n allowedTargetsEnforcer,\n redeemerEnforcer,\n };\n};\n\n/**\n * Extracts the expiry timestamp from TimestampEnforcer caveat terms.\n * Terms are 32 bytes: first 16 bytes timestampAfterThreshold (must be 0),\n * last 16 bytes timestampBeforeThreshold (expiry).\n *\n * @param terms - The hex-encoded terms from a TimestampEnforcer caveat.\n * @returns The expiry timestamp in seconds.\n * @throws If terms are invalid.\n */\nexport const extractExpiryFromCaveatTerms = (terms: Hex): number => {\n if (terms.length !== 66) {\n throw new Error(\n `Invalid TimestampEnforcer terms length: expected 66 characters (0x + 64 hex), got ${terms.length}`,\n );\n }\n const [after, before] = splitHex(terms, [16, 16]);\n if (hexToNumber(after) !== 0) {\n throw new Error('Invalid expiry: timestampAfterThreshold must be 0');\n }\n const expiry = hexToNumber(before);\n if (expiry === 0) {\n throw new Error(\n 'Invalid expiry: timestampBeforeThreshold must be greater than 0',\n );\n }\n return expiry;\n};\n\n/**\n * Builds enforcer counts and set from caveat addresses (checksummed).\n * Used by caveatAddressesMatch.\n *\n * @param caveatAddresses - List of enforcer contract addresses (hex).\n * @returns Counts per enforcer and set of unique enforcers.\n */\nexport function buildEnforcerCountsAndSet(caveatAddresses: Hex[]): {\n counts: Map<Hex, number>;\n enforcersSet: Set<Hex>;\n} {\n const counts = new Map<Hex, number>();\n for (const addr of caveatAddresses.map(getChecksumAddress)) {\n counts.set(addr, (counts.get(addr) ?? 0) + 1);\n }\n return { counts, enforcersSet: new Set(counts.keys()) };\n}\n\n/**\n * Returns true if the given counts/set match the rule (required counts exact,\n * no enforcer outside required + optional).\n *\n * @param counts - Map of enforcer address to occurrence count.\n * @param enforcersSet - Set of unique enforcer addresses present.\n * @param requiredEnforcers - Map of required enforcer to required count.\n * @param optionalEnforcers - Set of optional enforcer addresses.\n * @returns True if the counts match the rule.\n */\nexport function enforcersMatchRule(\n counts: Map<Hex, number>,\n enforcersSet: Set<Hex>,\n requiredEnforcers: Map<Hex, number>,\n optionalEnforcers: Set<Hex>,\n): boolean {\n const allowedEnforcers = new Set<Hex>([\n ...optionalEnforcers,\n ...requiredEnforcers.keys(),\n ]);\n for (const addr of enforcersSet) {\n if (!allowedEnforcers.has(addr)) {\n return false;\n }\n }\n for (const [addr, requiredCount] of requiredEnforcers.entries()) {\n if ((counts.get(addr) ?? 0) !== requiredCount) {\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 const);\n }\n return parts;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/gator-permissions-controller",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0-preview-1275d0fda",
|
|
4
4
|
"description": "Controller for managing gator permissions with profile sync integration",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ethereum",
|
|
@@ -53,17 +53,17 @@
|
|
|
53
53
|
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@metamask/7715-permission-types": "^0.
|
|
56
|
+
"@metamask/7715-permission-types": "^0.7.1",
|
|
57
57
|
"@metamask/abi-utils": "^2.0.3",
|
|
58
58
|
"@metamask/base-controller": "^9.1.0",
|
|
59
|
-
"@metamask/delegation-core": "^2.
|
|
60
|
-
"@metamask/delegation-deployments": "^1.
|
|
59
|
+
"@metamask/delegation-core": "^2.2.1",
|
|
60
|
+
"@metamask/delegation-deployments": "^1.4.0",
|
|
61
61
|
"@metamask/messenger": "^1.2.0",
|
|
62
62
|
"@metamask/network-controller": "^32.0.0",
|
|
63
63
|
"@metamask/snaps-controllers": "^19.0.0",
|
|
64
64
|
"@metamask/snaps-sdk": "^11.0.0",
|
|
65
65
|
"@metamask/snaps-utils": "^12.1.2",
|
|
66
|
-
"@metamask/transaction-controller": "^
|
|
66
|
+
"@metamask/transaction-controller": "^66.0.0",
|
|
67
67
|
"@metamask/utils": "^11.9.0"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|