@metamask-previews/gator-permissions-controller 4.2.3-preview-3f58e8b65 → 4.2.3-preview-e0f702919
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 +0 -3
- package/README.md +1 -0
- package/dist/GatorPermissionsController.cjs +12 -11
- package/dist/GatorPermissionsController.cjs.map +1 -1
- package/dist/GatorPermissionsController.d.cts.map +1 -1
- package/dist/GatorPermissionsController.d.mts.map +1 -1
- package/dist/GatorPermissionsController.mjs +9 -8
- package/dist/GatorPermissionsController.mjs.map +1 -1
- package/dist/decodePermission/decoders/erc20PayeeRule.cjs +56 -0
- package/dist/decodePermission/decoders/erc20PayeeRule.cjs.map +1 -0
- package/dist/decodePermission/decoders/erc20PayeeRule.d.cts +22 -0
- package/dist/decodePermission/decoders/erc20PayeeRule.d.cts.map +1 -0
- package/dist/decodePermission/decoders/erc20PayeeRule.d.mts +22 -0
- package/dist/decodePermission/decoders/erc20PayeeRule.d.mts.map +1 -0
- package/dist/decodePermission/decoders/erc20PayeeRule.mjs +52 -0
- package/dist/decodePermission/decoders/erc20PayeeRule.mjs.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenAllowance.cjs +76 -0
- package/dist/decodePermission/decoders/erc20TokenAllowance.cjs.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenAllowance.d.cts +14 -0
- package/dist/decodePermission/decoders/erc20TokenAllowance.d.cts.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenAllowance.d.mts +14 -0
- package/dist/decodePermission/decoders/erc20TokenAllowance.d.mts.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenAllowance.mjs +72 -0
- package/dist/decodePermission/decoders/erc20TokenAllowance.mjs.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenPeriodic.cjs +77 -0
- package/dist/decodePermission/decoders/erc20TokenPeriodic.cjs.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenPeriodic.d.cts +10 -0
- package/dist/decodePermission/decoders/erc20TokenPeriodic.d.cts.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenPeriodic.d.mts +10 -0
- package/dist/decodePermission/decoders/erc20TokenPeriodic.d.mts.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenPeriodic.mjs +73 -0
- package/dist/decodePermission/decoders/erc20TokenPeriodic.mjs.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenRevocation.cjs +62 -0
- package/dist/decodePermission/decoders/erc20TokenRevocation.cjs.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenRevocation.d.cts +15 -0
- package/dist/decodePermission/decoders/erc20TokenRevocation.d.cts.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenRevocation.d.mts +15 -0
- package/dist/decodePermission/decoders/erc20TokenRevocation.d.mts.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenRevocation.mjs +58 -0
- package/dist/decodePermission/decoders/erc20TokenRevocation.mjs.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenStream.cjs +81 -0
- package/dist/decodePermission/decoders/erc20TokenStream.cjs.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenStream.d.cts +10 -0
- package/dist/decodePermission/decoders/erc20TokenStream.d.cts.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenStream.d.mts +10 -0
- package/dist/decodePermission/decoders/erc20TokenStream.d.mts.map +1 -0
- package/dist/decodePermission/decoders/erc20TokenStream.mjs +77 -0
- package/dist/decodePermission/decoders/erc20TokenStream.mjs.map +1 -0
- package/dist/decodePermission/decoders/expiryRule.cjs +35 -0
- package/dist/decodePermission/decoders/expiryRule.cjs.map +1 -0
- package/dist/decodePermission/decoders/expiryRule.d.cts +16 -0
- package/dist/decodePermission/decoders/expiryRule.d.cts.map +1 -0
- package/dist/decodePermission/decoders/expiryRule.d.mts +16 -0
- package/dist/decodePermission/decoders/expiryRule.d.mts.map +1 -0
- package/dist/decodePermission/decoders/expiryRule.mjs +31 -0
- package/dist/decodePermission/decoders/expiryRule.mjs.map +1 -0
- package/dist/decodePermission/decoders/index.cjs +21 -8
- package/dist/decodePermission/decoders/index.cjs.map +1 -1
- package/dist/decodePermission/decoders/index.d.cts +3 -9
- package/dist/decodePermission/decoders/index.d.cts.map +1 -1
- package/dist/decodePermission/decoders/index.d.mts +3 -9
- package/dist/decodePermission/decoders/index.d.mts.map +1 -1
- package/dist/decodePermission/decoders/index.mjs +21 -8
- package/dist/decodePermission/decoders/index.mjs.map +1 -1
- package/dist/decodePermission/decoders/makePermissionDecoder.cjs +3 -32
- package/dist/decodePermission/decoders/makePermissionDecoder.cjs.map +1 -1
- package/dist/decodePermission/decoders/makePermissionDecoder.d.cts +15 -27
- package/dist/decodePermission/decoders/makePermissionDecoder.d.cts.map +1 -1
- package/dist/decodePermission/decoders/makePermissionDecoder.d.mts +15 -27
- package/dist/decodePermission/decoders/makePermissionDecoder.d.mts.map +1 -1
- package/dist/decodePermission/decoders/makePermissionDecoder.mjs +3 -32
- package/dist/decodePermission/decoders/makePermissionDecoder.mjs.map +1 -1
- package/dist/decodePermission/decoders/nativePayeeRule.cjs +44 -0
- package/dist/decodePermission/decoders/nativePayeeRule.cjs.map +1 -0
- package/dist/decodePermission/decoders/nativePayeeRule.d.cts +20 -0
- package/dist/decodePermission/decoders/nativePayeeRule.d.cts.map +1 -0
- package/dist/decodePermission/decoders/nativePayeeRule.d.mts +20 -0
- package/dist/decodePermission/decoders/nativePayeeRule.d.mts.map +1 -0
- package/dist/decodePermission/decoders/nativePayeeRule.mjs +40 -0
- package/dist/decodePermission/decoders/nativePayeeRule.mjs.map +1 -0
- package/dist/decodePermission/decoders/nativeTokenAllowance.cjs +76 -0
- package/dist/decodePermission/decoders/nativeTokenAllowance.cjs.map +1 -0
- package/dist/decodePermission/decoders/nativeTokenAllowance.d.cts +14 -0
- package/dist/decodePermission/decoders/nativeTokenAllowance.d.cts.map +1 -0
- package/dist/decodePermission/decoders/nativeTokenAllowance.d.mts +14 -0
- package/dist/decodePermission/decoders/nativeTokenAllowance.d.mts.map +1 -0
- package/dist/decodePermission/decoders/nativeTokenAllowance.mjs +72 -0
- package/dist/decodePermission/decoders/nativeTokenAllowance.mjs.map +1 -0
- package/dist/decodePermission/decoders/nativeTokenPeriodic.cjs +77 -0
- package/dist/decodePermission/decoders/nativeTokenPeriodic.cjs.map +1 -0
- package/dist/decodePermission/decoders/nativeTokenPeriodic.d.cts +10 -0
- package/dist/decodePermission/decoders/nativeTokenPeriodic.d.cts.map +1 -0
- package/dist/decodePermission/decoders/nativeTokenPeriodic.d.mts +10 -0
- package/dist/decodePermission/decoders/nativeTokenPeriodic.d.mts.map +1 -0
- package/dist/decodePermission/decoders/nativeTokenPeriodic.mjs +73 -0
- package/dist/decodePermission/decoders/nativeTokenPeriodic.mjs.map +1 -0
- package/dist/decodePermission/decoders/nativeTokenStream.cjs +75 -0
- package/dist/decodePermission/decoders/nativeTokenStream.cjs.map +1 -0
- package/dist/decodePermission/decoders/nativeTokenStream.d.cts +10 -0
- package/dist/decodePermission/decoders/nativeTokenStream.d.cts.map +1 -0
- package/dist/decodePermission/decoders/nativeTokenStream.d.mts +10 -0
- package/dist/decodePermission/decoders/nativeTokenStream.d.mts.map +1 -0
- package/dist/decodePermission/decoders/nativeTokenStream.mjs +71 -0
- package/dist/decodePermission/decoders/nativeTokenStream.mjs.map +1 -0
- package/dist/decodePermission/decoders/redeemerRule.cjs +36 -0
- package/dist/decodePermission/decoders/redeemerRule.cjs.map +1 -0
- package/dist/decodePermission/decoders/redeemerRule.d.cts +13 -0
- package/dist/decodePermission/decoders/redeemerRule.d.cts.map +1 -0
- package/dist/decodePermission/decoders/redeemerRule.d.mts +13 -0
- package/dist/decodePermission/decoders/redeemerRule.d.mts.map +1 -0
- package/dist/decodePermission/decoders/redeemerRule.mjs +32 -0
- package/dist/decodePermission/decoders/redeemerRule.mjs.map +1 -0
- 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 +85 -4
- package/dist/decodePermission/types.d.cts.map +1 -1
- package/dist/decodePermission/types.d.mts +85 -4
- package/dist/decodePermission/types.d.mts.map +1 -1
- package/dist/decodePermission/types.mjs.map +1 -1
- package/dist/decodePermission/utils.cjs +88 -1
- package/dist/decodePermission/utils.cjs.map +1 -1
- package/dist/decodePermission/utils.d.cts +31 -0
- package/dist/decodePermission/utils.d.cts.map +1 -1
- package/dist/decodePermission/utils.d.mts +31 -0
- package/dist/decodePermission/utils.d.mts.map +1 -1
- package/dist/decodePermission/utils.mjs +85 -0
- package/dist/decodePermission/utils.mjs.map +1 -1
- package/dist/permissionOnChainStatus.cjs +6 -7
- package/dist/permissionOnChainStatus.cjs.map +1 -1
- package/dist/permissionOnChainStatus.d.cts +4 -3
- package/dist/permissionOnChainStatus.d.cts.map +1 -1
- package/dist/permissionOnChainStatus.d.mts +4 -3
- package/dist/permissionOnChainStatus.d.mts.map +1 -1
- package/dist/permissionOnChainStatus.mjs +7 -8
- package/dist/permissionOnChainStatus.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +0 -4
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +0 -4
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +2 -2
- package/dist/decodePermission/enforcerAddresses.cjs +0 -39
- package/dist/decodePermission/enforcerAddresses.cjs.map +0 -1
- package/dist/decodePermission/enforcerAddresses.d.cts +0 -13
- package/dist/decodePermission/enforcerAddresses.d.cts.map +0 -1
- package/dist/decodePermission/enforcerAddresses.d.mts +0 -13
- package/dist/decodePermission/enforcerAddresses.d.mts.map +0 -1
- package/dist/decodePermission/enforcerAddresses.mjs +0 -35
- package/dist/decodePermission/enforcerAddresses.mjs.map +0 -1
|
@@ -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,qBAAoB;AACjD,OAAO,EAAE,UAAU,EAAE,yBAAwB;AAG7C,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,eAA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E,aAAa,CAAC,IAAI,EAAE,sBAAsB,CAAC,aAAa,CAAC;QACxE,uBAAuB,EAAE,aAAa,CACpC,IAAI,EACJ,sBAAsB,CAAC,uBAAuB,CAC/C;QACD,cAAc,EAAE,aAAa,CAAC,IAAI,EAAE,sBAAsB,CAAC,cAAc,CAAC;QAC1E,eAAe,EAAE,aAAa,CAC5B,IAAI,EACJ,sBAAsB,CAAC,eAAe,CACvC;QACD,uBAAuB,EAAE,aAAa,CACpC,IAAI,EACJ,sBAAsB,CAAC,uBAAuB,CAC/C;KACF,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CAAC,IAAY,EAAE,IAAY;IAC/C,OAAO,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.js';\nimport { getTermsByEnforcer } from '../utils.js';\nimport { expiryRule } from './expiryRule.js';\nimport type { MakePermissionDecoderConfig } from './makePermissionDecoder.js';\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 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,6 +1,54 @@
|
|
|
1
|
-
import type { PermissionRequest, PermissionTypes, Rule } from "@metamask/7715-permission-types";
|
|
2
|
-
import type { Caveat } from "@metamask/delegation-core";
|
|
1
|
+
import type { BasePermission, MetaMaskBasePermissionData, PermissionRequest, PermissionTypes, Rule } from "@metamask/7715-permission-types";
|
|
2
|
+
import type { ApprovalRevocationTerms, Caveat } from "@metamask/delegation-core";
|
|
3
|
+
import type { DELEGATOR_CONTRACTS } from "@metamask/delegation-deployments";
|
|
3
4
|
import type { Hex } from "@metamask/utils";
|
|
5
|
+
export type DeployedContractsByName = (typeof DELEGATOR_CONTRACTS)[number][number];
|
|
6
|
+
/**
|
|
7
|
+
* Permission type for an unbounded ERC-20 token allowance.
|
|
8
|
+
*
|
|
9
|
+
* Encoded on-chain as an ERC20PeriodTransferEnforcer caveat with
|
|
10
|
+
* `periodDuration` set to `UINT256_MAX` so that the allowance never resets
|
|
11
|
+
* within any realistic time horizon.
|
|
12
|
+
*
|
|
13
|
+
* Not yet defined in `@metamask/7715-permission-types`, so declared locally.
|
|
14
|
+
*/
|
|
15
|
+
type Erc20TokenAllowancePermission = BasePermission & {
|
|
16
|
+
type: 'erc20-token-allowance';
|
|
17
|
+
data: MetaMaskBasePermissionData & {
|
|
18
|
+
allowanceAmount: Hex;
|
|
19
|
+
startTime?: number | null;
|
|
20
|
+
tokenAddress: Hex;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Permission type for an unbounded native token allowance.
|
|
25
|
+
*
|
|
26
|
+
* Encoded on-chain as a NativeTokenPeriodTransferEnforcer caveat with
|
|
27
|
+
* `periodDuration` set to `UINT256_MAX`.
|
|
28
|
+
*
|
|
29
|
+
* Not yet defined in `@metamask/7715-permission-types`, so declared locally.
|
|
30
|
+
*/
|
|
31
|
+
type NativeTokenAllowancePermission = BasePermission & {
|
|
32
|
+
type: 'native-token-allowance';
|
|
33
|
+
data: MetaMaskBasePermissionData & {
|
|
34
|
+
allowanceAmount: Hex;
|
|
35
|
+
startTime?: number | null;
|
|
36
|
+
};
|
|
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
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Extended permission union, including types not yet published in
|
|
49
|
+
* `@metamask/7715-permission-types` but supported by this package's decoder.
|
|
50
|
+
*/
|
|
51
|
+
type ExtendedPermissionTypes = PermissionTypes | Erc20TokenAllowancePermission | NativeTokenAllowancePermission | TokenApprovalRevocationPermission;
|
|
4
52
|
/**
|
|
5
53
|
* A partially reconstructed permission object decoded from a permission context.
|
|
6
54
|
*
|
|
@@ -11,8 +59,8 @@ import type { Hex } from "@metamask/utils";
|
|
|
11
59
|
*/
|
|
12
60
|
export type DecodedPermission = Pick<PermissionRequest<PermissionTypes>, 'chainId' | 'from' | 'to'> & {
|
|
13
61
|
permission: Omit<PermissionRequest<PermissionTypes>['permission'], 'isAdjustmentAllowed' | 'type' | 'data'> & {
|
|
14
|
-
type:
|
|
15
|
-
data:
|
|
62
|
+
type: ExtendedPermissionTypes['type'];
|
|
63
|
+
data: ExtendedPermissionTypes['data'];
|
|
16
64
|
justification?: string;
|
|
17
65
|
};
|
|
18
66
|
/**
|
|
@@ -27,6 +75,25 @@ export type DecodedPermission = Pick<PermissionRequest<PermissionTypes>, 'chainI
|
|
|
27
75
|
* Supported permission type identifiers that can be decoded from a permission context.
|
|
28
76
|
*/
|
|
29
77
|
export type PermissionType = DecodedPermission['permission']['type'];
|
|
78
|
+
/**
|
|
79
|
+
* Checksummed enforcer contract addresses for a chain (from getChecksumEnforcersByChainId).
|
|
80
|
+
*/
|
|
81
|
+
export type ChecksumEnforcersByChainId = {
|
|
82
|
+
erc20StreamingEnforcer: Hex;
|
|
83
|
+
erc20PeriodicEnforcer: Hex;
|
|
84
|
+
nativeTokenStreamingEnforcer: Hex;
|
|
85
|
+
nativeTokenPeriodicEnforcer: Hex;
|
|
86
|
+
approvalRevocationEnforcer: Hex;
|
|
87
|
+
exactCalldataEnforcer: Hex;
|
|
88
|
+
valueLteEnforcer: Hex;
|
|
89
|
+
timestampEnforcer: Hex;
|
|
90
|
+
nonceEnforcer: Hex;
|
|
91
|
+
allowedCalldataEnforcer: Hex;
|
|
92
|
+
allowedTargetsEnforcer: Hex;
|
|
93
|
+
redeemerEnforcer: Hex;
|
|
94
|
+
};
|
|
95
|
+
/** Caveat with checksummed enforcer address; used by rule decode functions. */
|
|
96
|
+
export type ChecksumCaveat = Caveat<Hex>;
|
|
30
97
|
/**
|
|
31
98
|
* Result of validating and decoding permission terms from caveats.
|
|
32
99
|
* When valid, includes expiry and decoded data; when invalid, includes the error.
|
|
@@ -61,4 +128,18 @@ export type PermissionDecoder = {
|
|
|
61
128
|
*/
|
|
62
129
|
validateAndDecodePermission: (caveats: Caveat<Hex>[]) => ValidateAndDecodeResult;
|
|
63
130
|
};
|
|
131
|
+
/**
|
|
132
|
+
* A function that inspects checksummed caveats and optionally produces a
|
|
133
|
+
* {@link Rule} (e.g. redeemer, payee, expiry). Each rule decoder is
|
|
134
|
+
* responsible for a single rule type and is composed by
|
|
135
|
+
* `makePermissionDecoder` to populate the permission's `rules` array. The
|
|
136
|
+
* `expiry` rule, in addition to being appended to `rules`, has its value
|
|
137
|
+
* hoisted onto the top-level `expiry` field of the decoded permission.
|
|
138
|
+
*/
|
|
139
|
+
export type RuleDecoder = (args: {
|
|
140
|
+
contractAddresses: ChecksumEnforcersByChainId;
|
|
141
|
+
caveats: ChecksumCaveat[];
|
|
142
|
+
requiredEnforcers: Map<Hex, number>;
|
|
143
|
+
}) => Rule | null;
|
|
144
|
+
export {};
|
|
64
145
|
//# sourceMappingURL=types.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,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,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AChB,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,6 +1,54 @@
|
|
|
1
|
-
import type { PermissionRequest, PermissionTypes, Rule } from "@metamask/7715-permission-types";
|
|
2
|
-
import type { Caveat } from "@metamask/delegation-core";
|
|
1
|
+
import type { BasePermission, MetaMaskBasePermissionData, PermissionRequest, PermissionTypes, Rule } from "@metamask/7715-permission-types";
|
|
2
|
+
import type { ApprovalRevocationTerms, Caveat } from "@metamask/delegation-core";
|
|
3
|
+
import type { DELEGATOR_CONTRACTS } from "@metamask/delegation-deployments";
|
|
3
4
|
import type { Hex } from "@metamask/utils";
|
|
5
|
+
export type DeployedContractsByName = (typeof DELEGATOR_CONTRACTS)[number][number];
|
|
6
|
+
/**
|
|
7
|
+
* Permission type for an unbounded ERC-20 token allowance.
|
|
8
|
+
*
|
|
9
|
+
* Encoded on-chain as an ERC20PeriodTransferEnforcer caveat with
|
|
10
|
+
* `periodDuration` set to `UINT256_MAX` so that the allowance never resets
|
|
11
|
+
* within any realistic time horizon.
|
|
12
|
+
*
|
|
13
|
+
* Not yet defined in `@metamask/7715-permission-types`, so declared locally.
|
|
14
|
+
*/
|
|
15
|
+
type Erc20TokenAllowancePermission = BasePermission & {
|
|
16
|
+
type: 'erc20-token-allowance';
|
|
17
|
+
data: MetaMaskBasePermissionData & {
|
|
18
|
+
allowanceAmount: Hex;
|
|
19
|
+
startTime?: number | null;
|
|
20
|
+
tokenAddress: Hex;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Permission type for an unbounded native token allowance.
|
|
25
|
+
*
|
|
26
|
+
* Encoded on-chain as a NativeTokenPeriodTransferEnforcer caveat with
|
|
27
|
+
* `periodDuration` set to `UINT256_MAX`.
|
|
28
|
+
*
|
|
29
|
+
* Not yet defined in `@metamask/7715-permission-types`, so declared locally.
|
|
30
|
+
*/
|
|
31
|
+
type NativeTokenAllowancePermission = BasePermission & {
|
|
32
|
+
type: 'native-token-allowance';
|
|
33
|
+
data: MetaMaskBasePermissionData & {
|
|
34
|
+
allowanceAmount: Hex;
|
|
35
|
+
startTime?: number | null;
|
|
36
|
+
};
|
|
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
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Extended permission union, including types not yet published in
|
|
49
|
+
* `@metamask/7715-permission-types` but supported by this package's decoder.
|
|
50
|
+
*/
|
|
51
|
+
type ExtendedPermissionTypes = PermissionTypes | Erc20TokenAllowancePermission | NativeTokenAllowancePermission | TokenApprovalRevocationPermission;
|
|
4
52
|
/**
|
|
5
53
|
* A partially reconstructed permission object decoded from a permission context.
|
|
6
54
|
*
|
|
@@ -11,8 +59,8 @@ import type { Hex } from "@metamask/utils";
|
|
|
11
59
|
*/
|
|
12
60
|
export type DecodedPermission = Pick<PermissionRequest<PermissionTypes>, 'chainId' | 'from' | 'to'> & {
|
|
13
61
|
permission: Omit<PermissionRequest<PermissionTypes>['permission'], 'isAdjustmentAllowed' | 'type' | 'data'> & {
|
|
14
|
-
type:
|
|
15
|
-
data:
|
|
62
|
+
type: ExtendedPermissionTypes['type'];
|
|
63
|
+
data: ExtendedPermissionTypes['data'];
|
|
16
64
|
justification?: string;
|
|
17
65
|
};
|
|
18
66
|
/**
|
|
@@ -27,6 +75,25 @@ export type DecodedPermission = Pick<PermissionRequest<PermissionTypes>, 'chainI
|
|
|
27
75
|
* Supported permission type identifiers that can be decoded from a permission context.
|
|
28
76
|
*/
|
|
29
77
|
export type PermissionType = DecodedPermission['permission']['type'];
|
|
78
|
+
/**
|
|
79
|
+
* Checksummed enforcer contract addresses for a chain (from getChecksumEnforcersByChainId).
|
|
80
|
+
*/
|
|
81
|
+
export type ChecksumEnforcersByChainId = {
|
|
82
|
+
erc20StreamingEnforcer: Hex;
|
|
83
|
+
erc20PeriodicEnforcer: Hex;
|
|
84
|
+
nativeTokenStreamingEnforcer: Hex;
|
|
85
|
+
nativeTokenPeriodicEnforcer: Hex;
|
|
86
|
+
approvalRevocationEnforcer: Hex;
|
|
87
|
+
exactCalldataEnforcer: Hex;
|
|
88
|
+
valueLteEnforcer: Hex;
|
|
89
|
+
timestampEnforcer: Hex;
|
|
90
|
+
nonceEnforcer: Hex;
|
|
91
|
+
allowedCalldataEnforcer: Hex;
|
|
92
|
+
allowedTargetsEnforcer: Hex;
|
|
93
|
+
redeemerEnforcer: Hex;
|
|
94
|
+
};
|
|
95
|
+
/** Caveat with checksummed enforcer address; used by rule decode functions. */
|
|
96
|
+
export type ChecksumCaveat = Caveat<Hex>;
|
|
30
97
|
/**
|
|
31
98
|
* Result of validating and decoding permission terms from caveats.
|
|
32
99
|
* When valid, includes expiry and decoded data; when invalid, includes the error.
|
|
@@ -61,4 +128,18 @@ export type PermissionDecoder = {
|
|
|
61
128
|
*/
|
|
62
129
|
validateAndDecodePermission: (caveats: Caveat<Hex>[]) => ValidateAndDecodeResult;
|
|
63
130
|
};
|
|
131
|
+
/**
|
|
132
|
+
* A function that inspects checksummed caveats and optionally produces a
|
|
133
|
+
* {@link Rule} (e.g. redeemer, payee, expiry). Each rule decoder is
|
|
134
|
+
* responsible for a single rule type and is composed by
|
|
135
|
+
* `makePermissionDecoder` to populate the permission's `rules` array. The
|
|
136
|
+
* `expiry` rule, in addition to being appended to `rules`, has its value
|
|
137
|
+
* hoisted onto the top-level `expiry` field of the decoded permission.
|
|
138
|
+
*/
|
|
139
|
+
export type RuleDecoder = (args: {
|
|
140
|
+
contractAddresses: ChecksumEnforcersByChainId;
|
|
141
|
+
caveats: ChecksumCaveat[];
|
|
142
|
+
requiredEnforcers: Map<Hex, number>;
|
|
143
|
+
}) => Rule | null;
|
|
144
|
+
export {};
|
|
64
145
|
//# sourceMappingURL=types.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,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,G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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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\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"]}
|
|
@@ -1,7 +1,94 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.splitHex = exports.getTermsByEnforcer = exports.enforcersMatchRule = exports.buildEnforcerCountsAndSet = exports.extractExpiryFromCaveatTerms = void 0;
|
|
3
|
+
exports.splitHex = exports.getTermsByEnforcer = exports.enforcersMatchRule = exports.buildEnforcerCountsAndSet = exports.extractExpiryFromCaveatTerms = exports.getChecksumEnforcersByChainId = exports.getByteLength = exports.MAX_PERIOD_DURATION = exports.ERC20_APPROVE_ZERO_AMOUNT_TERMS = exports.ERC20_APPROVE_SELECTOR_TERMS = exports.UINT256_MAX = exports.ZERO_32_BYTES = void 0;
|
|
4
4
|
const utils_1 = require("@metamask/utils");
|
|
5
|
+
/**
|
|
6
|
+
* The names of the enforcer contracts for each permission type.
|
|
7
|
+
*/
|
|
8
|
+
const ENFORCER_CONTRACT_NAMES = {
|
|
9
|
+
ERC20PeriodTransferEnforcer: 'ERC20PeriodTransferEnforcer',
|
|
10
|
+
ERC20StreamingEnforcer: 'ERC20StreamingEnforcer',
|
|
11
|
+
ApprovalRevocationEnforcer: 'ApprovalRevocationEnforcer',
|
|
12
|
+
ExactCalldataEnforcer: 'ExactCalldataEnforcer',
|
|
13
|
+
NativeTokenPeriodTransferEnforcer: 'NativeTokenPeriodTransferEnforcer',
|
|
14
|
+
NativeTokenStreamingEnforcer: 'NativeTokenStreamingEnforcer',
|
|
15
|
+
TimestampEnforcer: 'TimestampEnforcer',
|
|
16
|
+
ValueLteEnforcer: 'ValueLteEnforcer',
|
|
17
|
+
NonceEnforcer: 'NonceEnforcer',
|
|
18
|
+
AllowedCalldataEnforcer: 'AllowedCalldataEnforcer',
|
|
19
|
+
AllowedTargetsEnforcer: 'AllowedTargetsEnforcer',
|
|
20
|
+
RedeemerEnforcer: 'RedeemerEnforcer',
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* 32 bytes of zero (0x + 64 hex chars).
|
|
24
|
+
*/
|
|
25
|
+
exports.ZERO_32_BYTES = '0x0000000000000000000000000000000000000000000000000000000000000000';
|
|
26
|
+
/**
|
|
27
|
+
* Maximum unsigned 256-bit integer encoded as 32 bytes (0x + 64 hex chars).
|
|
28
|
+
*/
|
|
29
|
+
exports.UINT256_MAX = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
|
|
30
|
+
/** AllowedCalldataEnforcer terms for ERC20 approve selector. */
|
|
31
|
+
exports.ERC20_APPROVE_SELECTOR_TERMS = '0x0000000000000000000000000000000000000000000000000000000000000000095ea7b3';
|
|
32
|
+
/** AllowedCalldataEnforcer terms for ERC20 approve zero amount. */
|
|
33
|
+
exports.ERC20_APPROVE_ZERO_AMOUNT_TERMS = '0x00000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000000';
|
|
34
|
+
/** Maximum period duration in seconds. */
|
|
35
|
+
exports.MAX_PERIOD_DURATION = 10 * 365 * 24 * 60 * 60; // 10 years in seconds
|
|
36
|
+
/**
|
|
37
|
+
* Get the byte length of a hex string.
|
|
38
|
+
*
|
|
39
|
+
* @param hexString - The hex string to get the byte length of.
|
|
40
|
+
* @returns The byte length of the hex string.
|
|
41
|
+
*/
|
|
42
|
+
const getByteLength = (hexString) => {
|
|
43
|
+
return (hexString.length - 2) / 2;
|
|
44
|
+
};
|
|
45
|
+
exports.getByteLength = getByteLength;
|
|
46
|
+
/**
|
|
47
|
+
* Resolves and returns checksummed addresses of all known enforcer contracts
|
|
48
|
+
* for a given `chainId` under the current delegation framework version.
|
|
49
|
+
*
|
|
50
|
+
* @param contracts - The deployed contracts for the chain.
|
|
51
|
+
* @returns An object mapping enforcer names to checksummed contract addresses.
|
|
52
|
+
* @throws If the chain or an expected enforcer contract is not found.
|
|
53
|
+
*/
|
|
54
|
+
const getChecksumEnforcersByChainId = (contracts) => {
|
|
55
|
+
const getChecksumContractAddress = (contractName) => {
|
|
56
|
+
const address = contracts[contractName];
|
|
57
|
+
if (!address) {
|
|
58
|
+
throw new Error(`Contract not found: ${contractName}`);
|
|
59
|
+
}
|
|
60
|
+
return (0, utils_1.getChecksumAddress)(address);
|
|
61
|
+
};
|
|
62
|
+
// permission type specific enforcers
|
|
63
|
+
const erc20StreamingEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ERC20StreamingEnforcer);
|
|
64
|
+
const erc20PeriodicEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ERC20PeriodTransferEnforcer);
|
|
65
|
+
const nativeTokenStreamingEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NativeTokenStreamingEnforcer);
|
|
66
|
+
const nativeTokenPeriodicEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NativeTokenPeriodTransferEnforcer);
|
|
67
|
+
const approvalRevocationEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ApprovalRevocationEnforcer);
|
|
68
|
+
// general enforcers
|
|
69
|
+
const exactCalldataEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ExactCalldataEnforcer);
|
|
70
|
+
const valueLteEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.ValueLteEnforcer);
|
|
71
|
+
const timestampEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.TimestampEnforcer);
|
|
72
|
+
const nonceEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.NonceEnforcer);
|
|
73
|
+
const allowedCalldataEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.AllowedCalldataEnforcer);
|
|
74
|
+
const allowedTargetsEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.AllowedTargetsEnforcer);
|
|
75
|
+
const redeemerEnforcer = getChecksumContractAddress(ENFORCER_CONTRACT_NAMES.RedeemerEnforcer);
|
|
76
|
+
return {
|
|
77
|
+
erc20StreamingEnforcer,
|
|
78
|
+
erc20PeriodicEnforcer,
|
|
79
|
+
nativeTokenStreamingEnforcer,
|
|
80
|
+
nativeTokenPeriodicEnforcer,
|
|
81
|
+
approvalRevocationEnforcer,
|
|
82
|
+
exactCalldataEnforcer,
|
|
83
|
+
valueLteEnforcer,
|
|
84
|
+
timestampEnforcer,
|
|
85
|
+
nonceEnforcer,
|
|
86
|
+
allowedCalldataEnforcer,
|
|
87
|
+
allowedTargetsEnforcer,
|
|
88
|
+
redeemerEnforcer,
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
exports.getChecksumEnforcersByChainId = getChecksumEnforcersByChainId;
|
|
5
92
|
/**
|
|
6
93
|
* Extracts the expiry timestamp from TimestampEnforcer caveat terms.
|
|
7
94
|
* Terms are 32 bytes: first 16 bytes timestampAfterThreshold (must be 0),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":";;;AACA,2CAAkE;AAGlE;;;;;;;;GAQG;AACI,MAAM,4BAA4B,GAAG,CAAC,KAAU,EAAU,EAAE;IACjE,IAAI,KAAK,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CACb,qFAAqF,KAAK,CAAC,MAAM,EAAE,CACpG,CAAC;IACJ,CAAC;IACD,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;IAClD,IAAI,IAAA,mBAAW,EAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;IACvE,CAAC;IACD,MAAM,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ChecksumAddress, hexToNumber } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\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;AACnD,QAAA,4BAA4B,GACvC,4EAAqF,CAAC;AAExF,mEAAmE;AACtD,QAAA,+BAA+B,GAC1C,oIAA6I,CAAC;AAEhJ,0CAA0C;AAC7B,QAAA,mBAAmB,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,sBAAsB;AAElF;;;;;GAKG;AACI,MAAM,aAAa,GAAG,CAAC,SAAc,EAAU,EAAE;IACtD,OAAO,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;AACpC,CAAC,CAAC;AAFW,QAAA,aAAa,iBAExB;AAEF;;;;;;;GAOG;AACI,MAAM,6BAA6B,GAAG,CAC3C,SAAkC,EACN,EAAE;IAC9B,MAAM,0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AC,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.js';\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,5 +1,36 @@
|
|
|
1
1
|
import type { Caveat } from "@metamask/delegation-core";
|
|
2
2
|
import type { Hex } from "@metamask/utils";
|
|
3
|
+
import type { ChecksumEnforcersByChainId, DeployedContractsByName } from "./types.cjs";
|
|
4
|
+
/**
|
|
5
|
+
* 32 bytes of zero (0x + 64 hex chars).
|
|
6
|
+
*/
|
|
7
|
+
export declare const ZERO_32_BYTES: "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
8
|
+
/**
|
|
9
|
+
* Maximum unsigned 256-bit integer encoded as 32 bytes (0x + 64 hex chars).
|
|
10
|
+
*/
|
|
11
|
+
export declare const UINT256_MAX: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
|
|
12
|
+
/** AllowedCalldataEnforcer terms for ERC20 approve selector. */
|
|
13
|
+
export declare const ERC20_APPROVE_SELECTOR_TERMS: "0x0000000000000000000000000000000000000000000000000000000000000000095ea7b3";
|
|
14
|
+
/** AllowedCalldataEnforcer terms for ERC20 approve zero amount. */
|
|
15
|
+
export declare const ERC20_APPROVE_ZERO_AMOUNT_TERMS: "0x00000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000000";
|
|
16
|
+
/** Maximum period duration in seconds. */
|
|
17
|
+
export declare const MAX_PERIOD_DURATION: number;
|
|
18
|
+
/**
|
|
19
|
+
* Get the byte length of a hex string.
|
|
20
|
+
*
|
|
21
|
+
* @param hexString - The hex string to get the byte length of.
|
|
22
|
+
* @returns The byte length of the hex string.
|
|
23
|
+
*/
|
|
24
|
+
export declare const getByteLength: (hexString: Hex) => number;
|
|
25
|
+
/**
|
|
26
|
+
* Resolves and returns checksummed addresses of all known enforcer contracts
|
|
27
|
+
* for a given `chainId` under the current delegation framework version.
|
|
28
|
+
*
|
|
29
|
+
* @param contracts - The deployed contracts for the chain.
|
|
30
|
+
* @returns An object mapping enforcer names to checksummed contract addresses.
|
|
31
|
+
* @throws If the chain or an expected enforcer contract is not found.
|
|
32
|
+
*/
|
|
33
|
+
export declare const getChecksumEnforcersByChainId: (contracts: DeployedContractsByName) => ChecksumEnforcersByChainId;
|
|
3
34
|
/**
|
|
4
35
|
* Extracts the expiry timestamp from TimestampEnforcer caveat terms.
|
|
5
36
|
* Terms are 32 bytes: first 16 bytes timestampAfterThreshold (must be 0),
|
|
@@ -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;;;;;;;;GAQG;AACH,eAAO,MAAM,4BAA4B,UAAW,GAAG,KAAG,MAiBzD,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CAAC,eAAe,EAAE,GAAG,EAAE,GAAG;IACjE,MAAM,EAAE,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IACzB,YAAY,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;CACxB,CAMA;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,EACxB,YAAY,EAAE,GAAG,CAAC,GAAG,CAAC,EACtB,iBAAiB,EAAE,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,EACnC,iBAAiB,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,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C,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"}
|
|
@@ -1,5 +1,36 @@
|
|
|
1
1
|
import type { Caveat } from "@metamask/delegation-core";
|
|
2
2
|
import type { Hex } from "@metamask/utils";
|
|
3
|
+
import type { ChecksumEnforcersByChainId, DeployedContractsByName } from "./types.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* 32 bytes of zero (0x + 64 hex chars).
|
|
6
|
+
*/
|
|
7
|
+
export declare const ZERO_32_BYTES: "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
8
|
+
/**
|
|
9
|
+
* Maximum unsigned 256-bit integer encoded as 32 bytes (0x + 64 hex chars).
|
|
10
|
+
*/
|
|
11
|
+
export declare const UINT256_MAX: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
|
|
12
|
+
/** AllowedCalldataEnforcer terms for ERC20 approve selector. */
|
|
13
|
+
export declare const ERC20_APPROVE_SELECTOR_TERMS: "0x0000000000000000000000000000000000000000000000000000000000000000095ea7b3";
|
|
14
|
+
/** AllowedCalldataEnforcer terms for ERC20 approve zero amount. */
|
|
15
|
+
export declare const ERC20_APPROVE_ZERO_AMOUNT_TERMS: "0x00000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000000";
|
|
16
|
+
/** Maximum period duration in seconds. */
|
|
17
|
+
export declare const MAX_PERIOD_DURATION: number;
|
|
18
|
+
/**
|
|
19
|
+
* Get the byte length of a hex string.
|
|
20
|
+
*
|
|
21
|
+
* @param hexString - The hex string to get the byte length of.
|
|
22
|
+
* @returns The byte length of the hex string.
|
|
23
|
+
*/
|
|
24
|
+
export declare const getByteLength: (hexString: Hex) => number;
|
|
25
|
+
/**
|
|
26
|
+
* Resolves and returns checksummed addresses of all known enforcer contracts
|
|
27
|
+
* for a given `chainId` under the current delegation framework version.
|
|
28
|
+
*
|
|
29
|
+
* @param contracts - The deployed contracts for the chain.
|
|
30
|
+
* @returns An object mapping enforcer names to checksummed contract addresses.
|
|
31
|
+
* @throws If the chain or an expected enforcer contract is not found.
|
|
32
|
+
*/
|
|
33
|
+
export declare const getChecksumEnforcersByChainId: (contracts: DeployedContractsByName) => ChecksumEnforcersByChainId;
|
|
3
34
|
/**
|
|
4
35
|
* Extracts the expiry timestamp from TimestampEnforcer caveat terms.
|
|
5
36
|
* Terms are 32 bytes: first 16 bytes timestampAfterThreshold (must be 0),
|