@metamask-previews/gator-permissions-controller 5.0.2-preview-4f4c98e → 5.0.2-preview-eb263b0ee
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 -4
- package/dist/GatorPermissionsController-method-action-types.cjs +7 -0
- package/dist/GatorPermissionsController-method-action-types.cjs.map +1 -0
- package/dist/{GatorPermissionsController-method-action-types.d.ts → GatorPermissionsController-method-action-types.d.cts} +2 -2
- package/dist/GatorPermissionsController-method-action-types.d.cts.map +1 -0
- package/dist/GatorPermissionsController-method-action-types.d.mts +118 -0
- package/dist/GatorPermissionsController-method-action-types.d.mts.map +1 -0
- package/dist/{GatorPermissionsController-method-action-types.js → GatorPermissionsController-method-action-types.mjs} +1 -1
- package/dist/GatorPermissionsController-method-action-types.mjs.map +1 -0
- package/dist/GatorPermissionsController.cjs +589 -0
- package/dist/GatorPermissionsController.cjs.map +1 -0
- package/dist/{GatorPermissionsController.d.ts → GatorPermissionsController.d.cts} +12 -12
- package/dist/GatorPermissionsController.d.cts.map +1 -0
- package/dist/GatorPermissionsController.d.mts +219 -0
- package/dist/GatorPermissionsController.d.mts.map +1 -0
- package/dist/{GatorPermissionsController.js → GatorPermissionsController.mjs} +12 -12
- package/dist/GatorPermissionsController.mjs.map +1 -0
- package/dist/constants.cjs +29 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/{constants.d.ts → constants.d.cts} +4 -4
- package/dist/constants.d.cts.map +1 -0
- package/dist/constants.d.mts +26 -0
- package/dist/constants.d.mts.map +1 -0
- package/dist/{constants.js → constants.mjs} +1 -1
- package/dist/constants.mjs.map +1 -0
- package/dist/decodePermission/decodePermission.cjs +110 -0
- package/dist/decodePermission/decodePermission.cjs.map +1 -0
- package/dist/decodePermission/{decodePermission.d.ts → decodePermission.d.cts} +3 -3
- package/dist/decodePermission/decodePermission.d.cts.map +1 -0
- package/dist/decodePermission/decodePermission.d.mts +72 -0
- package/dist/decodePermission/decodePermission.d.mts.map +1 -0
- package/dist/decodePermission/{decodePermission.js → decodePermission.mjs} +3 -3
- package/dist/decodePermission/decodePermission.mjs.map +1 -0
- package/dist/decodePermission/decoders/index.cjs +26 -0
- package/dist/decodePermission/decoders/index.cjs.map +1 -0
- package/dist/decodePermission/decoders/{index.d.ts → index.d.cts} +3 -3
- package/dist/decodePermission/decoders/index.d.cts.map +1 -0
- package/dist/decodePermission/decoders/index.d.mts +20 -0
- package/dist/decodePermission/decoders/index.d.mts.map +1 -0
- package/dist/decodePermission/decoders/{index.js → index.mjs} +3 -3
- package/dist/decodePermission/decoders/index.mjs.map +1 -0
- package/dist/decodePermission/decoders/makePermissionDecoder.cjs +108 -0
- package/dist/decodePermission/decoders/makePermissionDecoder.cjs.map +1 -0
- package/dist/decodePermission/decoders/{makePermissionDecoder.d.ts → makePermissionDecoder.d.cts} +3 -3
- package/dist/decodePermission/decoders/makePermissionDecoder.d.cts.map +1 -0
- package/dist/decodePermission/decoders/makePermissionDecoder.d.mts +46 -0
- package/dist/decodePermission/decoders/makePermissionDecoder.d.mts.map +1 -0
- package/dist/decodePermission/decoders/{makePermissionDecoder.js → makePermissionDecoder.mjs} +4 -4
- package/dist/decodePermission/decoders/makePermissionDecoder.mjs.map +1 -0
- package/dist/decodePermission/enforcerAddresses.cjs +39 -0
- package/dist/decodePermission/enforcerAddresses.cjs.map +1 -0
- package/dist/decodePermission/{enforcerAddresses.d.ts → enforcerAddresses.d.cts} +3 -3
- package/dist/decodePermission/enforcerAddresses.d.cts.map +1 -0
- package/dist/decodePermission/enforcerAddresses.d.mts +13 -0
- package/dist/decodePermission/enforcerAddresses.d.mts.map +1 -0
- package/dist/decodePermission/{enforcerAddresses.js → enforcerAddresses.mjs} +4 -4
- package/dist/decodePermission/enforcerAddresses.mjs.map +1 -0
- package/dist/decodePermission/index.cjs +10 -0
- package/dist/decodePermission/index.cjs.map +1 -0
- package/dist/decodePermission/{index.d.ts → index.d.cts} +4 -4
- package/dist/decodePermission/index.d.cts.map +1 -0
- package/dist/decodePermission/index.d.mts +4 -0
- package/dist/decodePermission/index.d.mts.map +1 -0
- package/dist/decodePermission/index.mjs +3 -0
- package/dist/decodePermission/index.mjs.map +1 -0
- package/dist/decodePermission/types.cjs +3 -0
- package/dist/decodePermission/types.cjs.map +1 -0
- package/dist/decodePermission/{types.d.ts → types.d.cts} +4 -4
- package/dist/decodePermission/types.d.cts.map +1 -0
- package/dist/decodePermission/types.d.mts +64 -0
- package/dist/decodePermission/types.d.mts.map +1 -0
- package/dist/decodePermission/types.mjs +2 -0
- package/dist/decodePermission/types.mjs.map +1 -0
- package/dist/decodePermission/utils.cjs +125 -0
- package/dist/decodePermission/utils.cjs.map +1 -0
- package/dist/decodePermission/{utils.d.ts → utils.d.cts} +3 -3
- package/dist/decodePermission/utils.d.cts.map +1 -0
- package/dist/decodePermission/utils.d.mts +68 -0
- package/dist/decodePermission/utils.d.mts.map +1 -0
- package/dist/decodePermission/{utils.js → utils.mjs} +2 -2
- package/dist/decodePermission/utils.mjs.map +1 -0
- package/dist/errors.cjs +54 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/{errors.d.ts → errors.d.cts} +3 -3
- package/dist/errors.d.cts.map +1 -0
- package/dist/errors.d.mts +39 -0
- package/dist/errors.d.mts.map +1 -0
- package/dist/{errors.js → errors.mjs} +2 -2
- package/dist/errors.mjs.map +1 -0
- package/dist/index.cjs +14 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{index.d.ts → index.d.cts} +10 -10
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +3 -0
- package/dist/index.mjs.map +1 -0
- package/dist/logger.cjs +10 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/{logger.d.ts → logger.d.cts} +3 -2
- package/dist/logger.d.cts.map +1 -0
- package/dist/logger.d.mts +7 -0
- package/dist/logger.d.mts.map +1 -0
- package/dist/{logger.js → logger.mjs} +2 -2
- package/dist/logger.mjs.map +1 -0
- package/dist/payeeRule.cjs +3 -0
- package/dist/payeeRule.cjs.map +1 -0
- package/dist/{payeeRule.d.ts → payeeRule.d.cts} +2 -2
- package/dist/payeeRule.d.cts.map +1 -0
- package/dist/payeeRule.d.mts +12 -0
- package/dist/payeeRule.d.mts.map +1 -0
- package/dist/payeeRule.mjs +2 -0
- package/dist/payeeRule.mjs.map +1 -0
- package/dist/permissionOnChainStatus.cjs +140 -0
- package/dist/permissionOnChainStatus.cjs.map +1 -0
- package/dist/{permissionOnChainStatus.d.ts → permissionOnChainStatus.d.cts} +5 -5
- package/dist/permissionOnChainStatus.d.cts.map +1 -0
- package/dist/permissionOnChainStatus.d.mts +73 -0
- package/dist/permissionOnChainStatus.d.mts.map +1 -0
- package/dist/{permissionOnChainStatus.js → permissionOnChainStatus.mjs} +7 -7
- package/dist/permissionOnChainStatus.mjs.map +1 -0
- package/dist/redeemerRule.cjs +3 -0
- package/dist/redeemerRule.cjs.map +1 -0
- package/dist/{redeemerRule.d.ts → redeemerRule.d.cts} +2 -2
- package/dist/redeemerRule.d.cts.map +1 -0
- package/dist/redeemerRule.d.mts +12 -0
- package/dist/redeemerRule.d.mts.map +1 -0
- package/dist/redeemerRule.mjs +2 -0
- package/dist/redeemerRule.mjs.map +1 -0
- package/dist/types.cjs +28 -0
- package/dist/types.cjs.map +1 -0
- package/dist/{types.d.ts → types.d.cts} +4 -4
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +161 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/{types.js → types.mjs} +1 -1
- package/dist/types.mjs.map +1 -0
- package/dist/utils.cjs +41 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/{utils.d.ts → utils.d.cts} +5 -5
- package/dist/utils.d.cts.map +1 -0
- package/dist/utils.d.mts +22 -0
- package/dist/utils.d.mts.map +1 -0
- package/dist/{utils.js → utils.mjs} +4 -4
- package/dist/utils.mjs.map +1 -0
- package/package.json +16 -12
- package/dist/GatorPermissionsController-method-action-types.d.ts.map +0 -1
- package/dist/GatorPermissionsController-method-action-types.js.map +0 -1
- package/dist/GatorPermissionsController.d.ts.map +0 -1
- package/dist/GatorPermissionsController.js.map +0 -1
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/decodePermission/decodePermission.d.ts.map +0 -1
- package/dist/decodePermission/decodePermission.js.map +0 -1
- package/dist/decodePermission/decoders/index.d.ts.map +0 -1
- package/dist/decodePermission/decoders/index.js.map +0 -1
- package/dist/decodePermission/decoders/makePermissionDecoder.d.ts.map +0 -1
- package/dist/decodePermission/decoders/makePermissionDecoder.js.map +0 -1
- package/dist/decodePermission/enforcerAddresses.d.ts.map +0 -1
- package/dist/decodePermission/enforcerAddresses.js.map +0 -1
- package/dist/decodePermission/index.d.ts.map +0 -1
- package/dist/decodePermission/index.js +0 -3
- package/dist/decodePermission/index.js.map +0 -1
- package/dist/decodePermission/types.d.ts.map +0 -1
- package/dist/decodePermission/types.js +0 -2
- package/dist/decodePermission/types.js.map +0 -1
- package/dist/decodePermission/utils.d.ts.map +0 -1
- package/dist/decodePermission/utils.js.map +0 -1
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -3
- package/dist/index.js.map +0 -1
- package/dist/logger.d.ts.map +0 -1
- package/dist/logger.js.map +0 -1
- package/dist/payeeRule.d.ts.map +0 -1
- package/dist/payeeRule.js +0 -2
- package/dist/payeeRule.js.map +0 -1
- package/dist/permissionOnChainStatus.d.ts.map +0 -1
- package/dist/permissionOnChainStatus.js.map +0 -1
- package/dist/redeemerRule.d.ts.map +0 -1
- package/dist/redeemerRule.js +0 -2
- package/dist/redeemerRule.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makePermissionDecoder.mjs","sourceRoot":"","sources":["../../../src/decodePermission/decoders/makePermissionDecoder.ts"],"names":[],"mappings":"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","sourcesContent":["import type {\n PermissionDecoderConfig,\n Rule,\n} from '@metamask/7715-permission-types';\nimport type { Caveat } from '@metamask/delegation-core';\nimport { getChecksumAddress, isStrictHexString } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport { EXECUTION_PERMISSION_EXPIRY_RULE_TYPE } from '../../constants.js';\nimport type { PermissionDecoder, ValidateAndDecodeResult } from '../types.js';\nimport { buildEnforcerCountsAndSet, enforcersMatchRule } from '../utils.js';\n\n/**\n * Address checksumming in the permission decode flow.\n *\n * Enforcer addresses come from three sources, each of which may use a\n * different hex encoding (EIP-55 checksummed vs lowercase). Comparisons rely\n * on strict equality, so we normalize at function boundaries instead of\n * requiring callers to pass a specific encoding. `getChecksumAddress` is\n * idempotent, so repeated normalization is intentional and cheap.\n *\n * 1. **Canonical contract addresses** (`EnforcerAddressesByName`): normalized\n * in {@link toEnforcerAddressesByName}, again in\n * `makePermissionDecoderConfigs` (`checksumEnforcerAddresses` in\n * `@metamask/7715-permission-types`), and once more below when building\n * each decoder's required/optional enforcer sets.\n *\n * 2. **Delegation caveat enforcers**: read from the encoded permission\n * context with unpredictable casing. Normalized in\n * {@link buildEnforcerCountsAndSet} during address matching and again in\n * `validateAndDecodePermission` before term decoding (rule decoders and\n * `getTermsByEnforcer` compare enforcer addresses with `===`).\n *\n * 3. **Decoder required/optional enforcers**: populated from contract\n * addresses by `makePermissionDecoderConfigs`; normalized at decoder\n * construction so `caveatAddressesMatch` compares like-for-like with\n * caveat addresses from source (2).\n */\n\n/**\n * Creates a single {@link PermissionDecoder} with the given type, enforcer\n * sets, rule decoders, and decode/validate callback.\n *\n * @param config - The configuration describing the permission type's\n * enforcers, rule decoders, and data decoder. See\n * {@link PermissionDecoderConfig} for field documentation.\n * @param config.permissionType - The type of permission to decode.\n * @param config.contractAddresses - Checksummed enforcer addresses for the chain.\n * @param config.optionalEnforcers - Optional enforcers for the permission.\n * @param config.requiredEnforcers - Required enforcers for the permission.\n * @param config.rules - Rule decoders for the permission.\n * @param config.validateAndDecodeData - Data decoder for the permission.\n * @returns A {@link PermissionDecoder} with `caveatAddressesMatch` and\n * `validateAndDecodePermission`.\n */\nexport function makePermissionDecoder({\n permissionType,\n contractAddresses,\n optionalEnforcers,\n requiredEnforcers,\n rules,\n validateAndDecodeData,\n}: PermissionDecoderConfig): PermissionDecoder {\n const optionalEnforcersSet = new Set(\n optionalEnforcers.map(getChecksumAddress),\n );\n const requiredEnforcersMap = new Map(\n Object.entries(requiredEnforcers).map(([enforcer, count]) => [\n getChecksumAddress(enforcer as Hex),\n count,\n ]),\n );\n\n const caveatAddressesMatch = (caveatAddresses: Hex[]): boolean => {\n const { counts, enforcersSet } = buildEnforcerCountsAndSet(caveatAddresses);\n\n return enforcersMatchRule(\n counts,\n enforcersSet,\n requiredEnforcersMap,\n optionalEnforcersSet,\n );\n };\n\n const validateAndDecodePermission = (\n caveats: Caveat<Hex>[],\n ): ValidateAndDecodeResult => {\n const checksumCaveats: Caveat<Hex>[] = caveats.map((caveat) => ({\n ...caveat,\n enforcer: getChecksumAddress(caveat.enforcer),\n }));\n try {\n const invalidTerms = checksumCaveats.filter(\n // isStrictHexString rejects '0x' which is a valid terms value\n ({ terms }) => terms !== '0x' && !isStrictHexString(terms),\n );\n\n if (invalidTerms.length > 0) {\n throw new Error('Invalid terms: must be a hex string');\n }\n\n let expiry: number | null = null;\n const decodedRules: Rule[] = [];\n\n for (const decode of rules) {\n const rule = decode({\n contractAddresses,\n caveats: checksumCaveats,\n requiredEnforcers: requiredEnforcersMap,\n });\n\n if (rule === null) {\n continue;\n }\n\n decodedRules.push(rule);\n\n if (rule.type === EXECUTION_PERMISSION_EXPIRY_RULE_TYPE) {\n expiry = rule.data.timestamp as number;\n }\n }\n\n const data = validateAndDecodeData(checksumCaveats, contractAddresses);\n\n return {\n isValid: true,\n expiry,\n data,\n rules: decodedRules.length > 0 ? decodedRules : undefined,\n };\n } catch (caughtError) {\n return { isValid: false, error: caughtError as Error };\n }\n };\n\n return {\n permissionType,\n caveatAddressesMatch,\n validateAndDecodePermission,\n optionalEnforcers: optionalEnforcersSet,\n requiredEnforcers: requiredEnforcersMap,\n };\n}\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toEnforcerAddressesByName = exports.delegationContractsByChainId = void 0;
|
|
4
|
+
const delegation_deployments_1 = require("@metamask/delegation-deployments");
|
|
5
|
+
const utils_1 = require("@metamask/utils");
|
|
6
|
+
const constants_js_1 = require("../constants.cjs");
|
|
7
|
+
// @metamask/delegation-deployments exports a very loosely typed object. We assert a more narrow typing here.
|
|
8
|
+
exports.delegationContractsByChainId = delegation_deployments_1.DELEGATOR_CONTRACTS[constants_js_1.DELEGATION_FRAMEWORK_VERSION];
|
|
9
|
+
const getChecksumContractAddress = (contracts, contractName) => {
|
|
10
|
+
const address = contracts[contractName];
|
|
11
|
+
if (!address) {
|
|
12
|
+
throw new Error(`Contract not found: ${contractName}`);
|
|
13
|
+
}
|
|
14
|
+
return (0, utils_1.getChecksumAddress)(address);
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Converts delegation-deployments enforcer addresses to the canonical shape
|
|
18
|
+
* expected by `@metamask/7715-permission-types`, checksumming each address.
|
|
19
|
+
*
|
|
20
|
+
* @param contracts - Enforcer addresses keyed by delegation-deployments names.
|
|
21
|
+
* @returns Checksummed enforcer addresses keyed by permission-types names.
|
|
22
|
+
* @throws If an expected enforcer contract is not found.
|
|
23
|
+
*/
|
|
24
|
+
const toEnforcerAddressesByName = (contracts) => ({
|
|
25
|
+
allowedCalldataEnforcer: getChecksumContractAddress(contracts, 'AllowedCalldataEnforcer'),
|
|
26
|
+
allowedTargetsEnforcer: getChecksumContractAddress(contracts, 'AllowedTargetsEnforcer'),
|
|
27
|
+
redeemerEnforcer: getChecksumContractAddress(contracts, 'RedeemerEnforcer'),
|
|
28
|
+
erc20StreamingEnforcer: getChecksumContractAddress(contracts, 'ERC20StreamingEnforcer'),
|
|
29
|
+
erc20PeriodTransferEnforcer: getChecksumContractAddress(contracts, 'ERC20PeriodTransferEnforcer'),
|
|
30
|
+
nativeTokenStreamingEnforcer: getChecksumContractAddress(contracts, 'NativeTokenStreamingEnforcer'),
|
|
31
|
+
nativeTokenPeriodTransferEnforcer: getChecksumContractAddress(contracts, 'NativeTokenPeriodTransferEnforcer'),
|
|
32
|
+
approvalRevocationEnforcer: getChecksumContractAddress(contracts, 'ApprovalRevocationEnforcer'),
|
|
33
|
+
exactCalldataEnforcer: getChecksumContractAddress(contracts, 'ExactCalldataEnforcer'),
|
|
34
|
+
valueLteEnforcer: getChecksumContractAddress(contracts, 'ValueLteEnforcer'),
|
|
35
|
+
timestampEnforcer: getChecksumContractAddress(contracts, 'TimestampEnforcer'),
|
|
36
|
+
nonceEnforcer: getChecksumContractAddress(contracts, 'NonceEnforcer'),
|
|
37
|
+
});
|
|
38
|
+
exports.toEnforcerAddressesByName = toEnforcerAddressesByName;
|
|
39
|
+
//# sourceMappingURL=enforcerAddresses.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enforcerAddresses.cjs","sourceRoot":"","sources":["../../src/decodePermission/enforcerAddresses.ts"],"names":[],"mappings":";;;AACA,6EAAuE;AACvE,2CAAqD;AAGrD,mDAA+D;AAG/D,6GAA6G;AAChG,QAAA,4BAA4B,GAAG,4CAAmB,CAC7D,2CAA4B,CACmC,CAAC;AAElE,MAAM,0BAA0B,GAAG,CACjC,SAAuD,EACvD,YAAgE,EAC3D,EAAE;IACP,MAAM,OAAO,GAAG,SAAS,CAAC,YAAY,CAAC,CAAC;IAExC,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,uBAAuB,YAAY,EAAE,CAAC,CAAC;IACzD,CAAC;IAED,OAAO,IAAA,0BAAkB,EAAC,OAAO,CAAC,CAAC;AACrC,CAAC,CAAC;AAEF;;;;;;;GAOG;AACI,MAAM,yBAAyB,GAAG,CACvC,SAAuD,EAC9B,EAAE,CAAC,CAAC;IAC7B,uBAAuB,EAAE,0BAA0B,CACjD,SAAS,EACT,yBAAyB,CAC1B;IACD,sBAAsB,EAAE,0BAA0B,CAChD,SAAS,EACT,wBAAwB,CACzB;IACD,gBAAgB,EAAE,0BAA0B,CAAC,SAAS,EAAE,kBAAkB,CAAC;IAC3E,sBAAsB,EAAE,0BAA0B,CAChD,SAAS,EACT,wBAAwB,CACzB;IACD,2BAA2B,EAAE,0BAA0B,CACrD,SAAS,EACT,6BAA6B,CAC9B;IACD,4BAA4B,EAAE,0BAA0B,CACtD,SAAS,EACT,8BAA8B,CAC/B;IACD,iCAAiC,EAAE,0BAA0B,CAC3D,SAAS,EACT,mCAAmC,CACpC;IACD,0BAA0B,EAAE,0BAA0B,CACpD,SAAS,EACT,4BAA4B,CAC7B;IACD,qBAAqB,EAAE,0BAA0B,CAC/C,SAAS,EACT,uBAAuB,CACxB;IACD,gBAAgB,EAAE,0BAA0B,CAAC,SAAS,EAAE,kBAAkB,CAAC;IAC3E,iBAAiB,EAAE,0BAA0B,CAAC,SAAS,EAAE,mBAAmB,CAAC;IAC7E,aAAa,EAAE,0BAA0B,CAAC,SAAS,EAAE,eAAe,CAAC;CACtE,CAAC,CAAC;AAvCU,QAAA,yBAAyB,6BAuCnC","sourcesContent":["import type { EnforcerAddressesByName } from '@metamask/7715-permission-types';\nimport { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';\nimport { getChecksumAddress } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport { DELEGATION_FRAMEWORK_VERSION } from '../constants.js';\nimport type { DelegationDeploymentsEnforcerAddressesByName } from '../types.js';\n\n// @metamask/delegation-deployments exports a very loosely typed object. We assert a more narrow typing here.\nexport const delegationContractsByChainId = DELEGATOR_CONTRACTS[\n DELEGATION_FRAMEWORK_VERSION\n] as Record<number, DelegationDeploymentsEnforcerAddressesByName>;\n\nconst getChecksumContractAddress = (\n contracts: DelegationDeploymentsEnforcerAddressesByName,\n contractName: keyof DelegationDeploymentsEnforcerAddressesByName,\n): 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/**\n * Converts delegation-deployments enforcer addresses to the canonical shape\n * expected by `@metamask/7715-permission-types`, checksumming each address.\n *\n * @param contracts - Enforcer addresses keyed by delegation-deployments names.\n * @returns Checksummed enforcer addresses keyed by permission-types names.\n * @throws If an expected enforcer contract is not found.\n */\nexport const toEnforcerAddressesByName = (\n contracts: DelegationDeploymentsEnforcerAddressesByName,\n): EnforcerAddressesByName => ({\n allowedCalldataEnforcer: getChecksumContractAddress(\n contracts,\n 'AllowedCalldataEnforcer',\n ),\n allowedTargetsEnforcer: getChecksumContractAddress(\n contracts,\n 'AllowedTargetsEnforcer',\n ),\n redeemerEnforcer: getChecksumContractAddress(contracts, 'RedeemerEnforcer'),\n erc20StreamingEnforcer: getChecksumContractAddress(\n contracts,\n 'ERC20StreamingEnforcer',\n ),\n erc20PeriodTransferEnforcer: getChecksumContractAddress(\n contracts,\n 'ERC20PeriodTransferEnforcer',\n ),\n nativeTokenStreamingEnforcer: getChecksumContractAddress(\n contracts,\n 'NativeTokenStreamingEnforcer',\n ),\n nativeTokenPeriodTransferEnforcer: getChecksumContractAddress(\n contracts,\n 'NativeTokenPeriodTransferEnforcer',\n ),\n approvalRevocationEnforcer: getChecksumContractAddress(\n contracts,\n 'ApprovalRevocationEnforcer',\n ),\n exactCalldataEnforcer: getChecksumContractAddress(\n contracts,\n 'ExactCalldataEnforcer',\n ),\n valueLteEnforcer: getChecksumContractAddress(contracts, 'ValueLteEnforcer'),\n timestampEnforcer: getChecksumContractAddress(contracts, 'TimestampEnforcer'),\n nonceEnforcer: getChecksumContractAddress(contracts, 'NonceEnforcer'),\n});\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { EnforcerAddressesByName } from
|
|
2
|
-
import type { DelegationDeploymentsEnforcerAddressesByName } from
|
|
1
|
+
import type { EnforcerAddressesByName } from "@metamask/7715-permission-types";
|
|
2
|
+
import type { DelegationDeploymentsEnforcerAddressesByName } from "../types.cjs";
|
|
3
3
|
export declare const delegationContractsByChainId: Record<number, DelegationDeploymentsEnforcerAddressesByName>;
|
|
4
4
|
/**
|
|
5
5
|
* Converts delegation-deployments enforcer addresses to the canonical shape
|
|
@@ -10,4 +10,4 @@ export declare const delegationContractsByChainId: Record<number, DelegationDepl
|
|
|
10
10
|
* @throws If an expected enforcer contract is not found.
|
|
11
11
|
*/
|
|
12
12
|
export declare const toEnforcerAddressesByName: (contracts: DelegationDeploymentsEnforcerAddressesByName) => EnforcerAddressesByName;
|
|
13
|
-
//# sourceMappingURL=enforcerAddresses.d.
|
|
13
|
+
//# sourceMappingURL=enforcerAddresses.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enforcerAddresses.d.cts","sourceRoot":"","sources":["../../src/decodePermission/enforcerAddresses.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,wCAAwC;AAM/E,OAAO,KAAK,EAAE,4CAA4C,EAAE,qBAAoB;AAGhF,eAAO,MAAM,4BAA4B,8DAEwB,CAAC;AAelE;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,cACzB,4CAA4C,KACtD,uBAqCD,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EnforcerAddressesByName } from "@metamask/7715-permission-types";
|
|
2
|
+
import type { DelegationDeploymentsEnforcerAddressesByName } from "../types.mjs";
|
|
3
|
+
export declare const delegationContractsByChainId: Record<number, DelegationDeploymentsEnforcerAddressesByName>;
|
|
4
|
+
/**
|
|
5
|
+
* Converts delegation-deployments enforcer addresses to the canonical shape
|
|
6
|
+
* expected by `@metamask/7715-permission-types`, checksumming each address.
|
|
7
|
+
*
|
|
8
|
+
* @param contracts - Enforcer addresses keyed by delegation-deployments names.
|
|
9
|
+
* @returns Checksummed enforcer addresses keyed by permission-types names.
|
|
10
|
+
* @throws If an expected enforcer contract is not found.
|
|
11
|
+
*/
|
|
12
|
+
export declare const toEnforcerAddressesByName: (contracts: DelegationDeploymentsEnforcerAddressesByName) => EnforcerAddressesByName;
|
|
13
|
+
//# sourceMappingURL=enforcerAddresses.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enforcerAddresses.d.mts","sourceRoot":"","sources":["../../src/decodePermission/enforcerAddresses.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,wCAAwC;AAM/E,OAAO,KAAK,EAAE,4CAA4C,EAAE,qBAAoB;AAGhF,eAAO,MAAM,4BAA4B,8DAEwB,CAAC;AAelE;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,cACzB,4CAA4C,KACtD,uBAqCD,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DELEGATOR_CONTRACTS } from
|
|
2
|
-
import { getChecksumAddress } from
|
|
3
|
-
import { DELEGATION_FRAMEWORK_VERSION } from
|
|
1
|
+
import { DELEGATOR_CONTRACTS } from "@metamask/delegation-deployments";
|
|
2
|
+
import { getChecksumAddress } from "@metamask/utils";
|
|
3
|
+
import { DELEGATION_FRAMEWORK_VERSION } from "../constants.mjs";
|
|
4
4
|
// @metamask/delegation-deployments exports a very loosely typed object. We assert a more narrow typing here.
|
|
5
5
|
export const delegationContractsByChainId = DELEGATOR_CONTRACTS[DELEGATION_FRAMEWORK_VERSION];
|
|
6
6
|
const getChecksumContractAddress = (contracts, contractName) => {
|
|
@@ -32,4 +32,4 @@ export const toEnforcerAddressesByName = (contracts) => ({
|
|
|
32
32
|
timestampEnforcer: getChecksumContractAddress(contracts, 'TimestampEnforcer'),
|
|
33
33
|
nonceEnforcer: getChecksumContractAddress(contracts, 'NonceEnforcer'),
|
|
34
34
|
});
|
|
35
|
-
//# sourceMappingURL=enforcerAddresses.
|
|
35
|
+
//# sourceMappingURL=enforcerAddresses.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enforcerAddresses.mjs","sourceRoot":"","sources":["../../src/decodePermission/enforcerAddresses.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,yCAAyC;AACvE,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AAGrD,OAAO,EAAE,4BAA4B,EAAE,yBAAwB;AAG/D,6GAA6G;AAC7G,MAAM,CAAC,MAAM,4BAA4B,GAAG,mBAAmB,CAC7D,4BAA4B,CACmC,CAAC;AAElE,MAAM,0BAA0B,GAAG,CACjC,SAAuD,EACvD,YAAgE,EAC3D,EAAE;IACP,MAAM,OAAO,GAAG,SAAS,CAAC,YAAY,CAAC,CAAC;IAExC,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,uBAAuB,YAAY,EAAE,CAAC,CAAC;IACzD,CAAC;IAED,OAAO,kBAAkB,CAAC,OAAO,CAAC,CAAC;AACrC,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CACvC,SAAuD,EAC9B,EAAE,CAAC,CAAC;IAC7B,uBAAuB,EAAE,0BAA0B,CACjD,SAAS,EACT,yBAAyB,CAC1B;IACD,sBAAsB,EAAE,0BAA0B,CAChD,SAAS,EACT,wBAAwB,CACzB;IACD,gBAAgB,EAAE,0BAA0B,CAAC,SAAS,EAAE,kBAAkB,CAAC;IAC3E,sBAAsB,EAAE,0BAA0B,CAChD,SAAS,EACT,wBAAwB,CACzB;IACD,2BAA2B,EAAE,0BAA0B,CACrD,SAAS,EACT,6BAA6B,CAC9B;IACD,4BAA4B,EAAE,0BAA0B,CACtD,SAAS,EACT,8BAA8B,CAC/B;IACD,iCAAiC,EAAE,0BAA0B,CAC3D,SAAS,EACT,mCAAmC,CACpC;IACD,0BAA0B,EAAE,0BAA0B,CACpD,SAAS,EACT,4BAA4B,CAC7B;IACD,qBAAqB,EAAE,0BAA0B,CAC/C,SAAS,EACT,uBAAuB,CACxB;IACD,gBAAgB,EAAE,0BAA0B,CAAC,SAAS,EAAE,kBAAkB,CAAC;IAC3E,iBAAiB,EAAE,0BAA0B,CAAC,SAAS,EAAE,mBAAmB,CAAC;IAC7E,aAAa,EAAE,0BAA0B,CAAC,SAAS,EAAE,eAAe,CAAC;CACtE,CAAC,CAAC","sourcesContent":["import type { EnforcerAddressesByName } from '@metamask/7715-permission-types';\nimport { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';\nimport { getChecksumAddress } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport { DELEGATION_FRAMEWORK_VERSION } from '../constants.js';\nimport type { DelegationDeploymentsEnforcerAddressesByName } from '../types.js';\n\n// @metamask/delegation-deployments exports a very loosely typed object. We assert a more narrow typing here.\nexport const delegationContractsByChainId = DELEGATOR_CONTRACTS[\n DELEGATION_FRAMEWORK_VERSION\n] as Record<number, DelegationDeploymentsEnforcerAddressesByName>;\n\nconst getChecksumContractAddress = (\n contracts: DelegationDeploymentsEnforcerAddressesByName,\n contractName: keyof DelegationDeploymentsEnforcerAddressesByName,\n): 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/**\n * Converts delegation-deployments enforcer addresses to the canonical shape\n * expected by `@metamask/7715-permission-types`, checksumming each address.\n *\n * @param contracts - Enforcer addresses keyed by delegation-deployments names.\n * @returns Checksummed enforcer addresses keyed by permission-types names.\n * @throws If an expected enforcer contract is not found.\n */\nexport const toEnforcerAddressesByName = (\n contracts: DelegationDeploymentsEnforcerAddressesByName,\n): EnforcerAddressesByName => ({\n allowedCalldataEnforcer: getChecksumContractAddress(\n contracts,\n 'AllowedCalldataEnforcer',\n ),\n allowedTargetsEnforcer: getChecksumContractAddress(\n contracts,\n 'AllowedTargetsEnforcer',\n ),\n redeemerEnforcer: getChecksumContractAddress(contracts, 'RedeemerEnforcer'),\n erc20StreamingEnforcer: getChecksumContractAddress(\n contracts,\n 'ERC20StreamingEnforcer',\n ),\n erc20PeriodTransferEnforcer: getChecksumContractAddress(\n contracts,\n 'ERC20PeriodTransferEnforcer',\n ),\n nativeTokenStreamingEnforcer: getChecksumContractAddress(\n contracts,\n 'NativeTokenStreamingEnforcer',\n ),\n nativeTokenPeriodTransferEnforcer: getChecksumContractAddress(\n contracts,\n 'NativeTokenPeriodTransferEnforcer',\n ),\n approvalRevocationEnforcer: getChecksumContractAddress(\n contracts,\n 'ApprovalRevocationEnforcer',\n ),\n exactCalldataEnforcer: getChecksumContractAddress(\n contracts,\n 'ExactCalldataEnforcer',\n ),\n valueLteEnforcer: getChecksumContractAddress(contracts, 'ValueLteEnforcer'),\n timestampEnforcer: getChecksumContractAddress(contracts, 'TimestampEnforcer'),\n nonceEnforcer: getChecksumContractAddress(contracts, 'NonceEnforcer'),\n});\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPermissionDecodersForContracts = exports.selectUniqueDecoderAndDecodedPermission = exports.reconstructDecodedPermission = exports.findDecodersWithMatchingCaveatAddresses = void 0;
|
|
4
|
+
var decodePermission_js_1 = require("./decodePermission.cjs");
|
|
5
|
+
Object.defineProperty(exports, "findDecodersWithMatchingCaveatAddresses", { enumerable: true, get: function () { return decodePermission_js_1.findDecodersWithMatchingCaveatAddresses; } });
|
|
6
|
+
Object.defineProperty(exports, "reconstructDecodedPermission", { enumerable: true, get: function () { return decodePermission_js_1.reconstructDecodedPermission; } });
|
|
7
|
+
Object.defineProperty(exports, "selectUniqueDecoderAndDecodedPermission", { enumerable: true, get: function () { return decodePermission_js_1.selectUniqueDecoderAndDecodedPermission; } });
|
|
8
|
+
var index_js_1 = require("./decoders/index.cjs");
|
|
9
|
+
Object.defineProperty(exports, "createPermissionDecodersForContracts", { enumerable: true, get: function () { return index_js_1.createPermissionDecodersForContracts; } });
|
|
10
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/decodePermission/index.ts"],"names":[],"mappings":";;;AAAA,8DAI+B;AAH7B,8IAAA,uCAAuC,OAAA;AACvC,mIAAA,4BAA4B,OAAA;AAC5B,8IAAA,uCAAuC,OAAA;AAEzC,iDAA2E;AAAlE,gIAAA,oCAAoC,OAAA","sourcesContent":["export {\n findDecodersWithMatchingCaveatAddresses,\n reconstructDecodedPermission,\n selectUniqueDecoderAndDecodedPermission,\n} from './decodePermission.js';\nexport { createPermissionDecodersForContracts } from './decoders/index.js';\n\nexport type {\n DecodedPermission,\n PermissionDecoder,\n ValidateAndDecodeResult,\n} from './types.js';\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { findDecodersWithMatchingCaveatAddresses, reconstructDecodedPermission, selectUniqueDecoderAndDecodedPermission, } from
|
|
2
|
-
export { createPermissionDecodersForContracts } from
|
|
3
|
-
export type { DecodedPermission, PermissionDecoder, ValidateAndDecodeResult, } from
|
|
4
|
-
//# sourceMappingURL=index.d.
|
|
1
|
+
export { findDecodersWithMatchingCaveatAddresses, reconstructDecodedPermission, selectUniqueDecoderAndDecodedPermission, } from "./decodePermission.cjs";
|
|
2
|
+
export { createPermissionDecodersForContracts } from "./decoders/index.cjs";
|
|
3
|
+
export type { DecodedPermission, PermissionDecoder, ValidateAndDecodeResult, } from "./types.cjs";
|
|
4
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/decodePermission/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uCAAuC,EACvC,4BAA4B,EAC5B,uCAAuC,GACxC,+BAA8B;AAC/B,OAAO,EAAE,oCAAoC,EAAE,6BAA4B;AAE3E,YAAY,EACV,iBAAiB,EACjB,iBAAiB,EACjB,uBAAuB,GACxB,oBAAmB"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { findDecodersWithMatchingCaveatAddresses, reconstructDecodedPermission, selectUniqueDecoderAndDecodedPermission, } from "./decodePermission.mjs";
|
|
2
|
+
export { createPermissionDecodersForContracts } from "./decoders/index.mjs";
|
|
3
|
+
export type { DecodedPermission, PermissionDecoder, ValidateAndDecodeResult, } from "./types.mjs";
|
|
4
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/decodePermission/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uCAAuC,EACvC,4BAA4B,EAC5B,uCAAuC,GACxC,+BAA8B;AAC/B,OAAO,EAAE,oCAAoC,EAAE,6BAA4B;AAE3E,YAAY,EACV,iBAAiB,EACjB,iBAAiB,EACjB,uBAAuB,GACxB,oBAAmB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/decodePermission/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uCAAuC,EACvC,4BAA4B,EAC5B,uCAAuC,EACxC,+BAA8B;AAC/B,OAAO,EAAE,oCAAoC,EAAE,6BAA4B","sourcesContent":["export {\n findDecodersWithMatchingCaveatAddresses,\n reconstructDecodedPermission,\n selectUniqueDecoderAndDecodedPermission,\n} from './decodePermission.js';\nexport { createPermissionDecodersForContracts } from './decoders/index.js';\n\nexport type {\n DecodedPermission,\n PermissionDecoder,\n ValidateAndDecodeResult,\n} from './types.js';\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.cjs","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n PermissionRequest,\n PermissionTypes,\n Rule,\n} from '@metamask/7715-permission-types';\nimport type { Caveat } from '@metamask/delegation-core';\nimport type { Hex } from '@metamask/utils';\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: PermissionTypes['type'];\n data: PermissionTypes['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 * 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"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { PermissionRequest, PermissionTypes, Rule } from
|
|
2
|
-
import type { Caveat } from
|
|
3
|
-
import type { Hex } from
|
|
1
|
+
import type { PermissionRequest, PermissionTypes, Rule } from "@metamask/7715-permission-types";
|
|
2
|
+
import type { Caveat } from "@metamask/delegation-core";
|
|
3
|
+
import type { Hex } from "@metamask/utils";
|
|
4
4
|
/**
|
|
5
5
|
* A partially reconstructed permission object decoded from a permission context.
|
|
6
6
|
*
|
|
@@ -61,4 +61,4 @@ export type PermissionDecoder = {
|
|
|
61
61
|
*/
|
|
62
62
|
validateAndDecodePermission: (caveats: Caveat<Hex>[]) => ValidateAndDecodeResult;
|
|
63
63
|
};
|
|
64
|
-
//# sourceMappingURL=types.d.
|
|
64
|
+
//# sourceMappingURL=types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,eAAe,EACf,IAAI,EACL,wCAAwC;AACzC,OAAO,KAAK,EAAE,MAAM,EAAE,kCAAkC;AACxD,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAG3C;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAClC,iBAAiB,CAAC,eAAe,CAAC,EAClC,SAAS,GAAG,MAAM,GAAG,IAAI,CAC1B,GAAG;IACF,UAAU,EAAE,IAAI,CACd,iBAAiB,CAAC,eAAe,CAAC,CAAC,YAAY,CAAC,EAChD,qBAAqB,GAAG,MAAM,GAAG,MAAM,CACxC,GAAG;QACF,IAAI,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;QAC9B,IAAI,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;QAE9B,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB,CAAC;IACF;;OAEG;IACH,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,8DAA8D;IAC9D,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC;CAChB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,iBAAiB,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC;AAErE;;;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"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { PermissionRequest, PermissionTypes, Rule } from "@metamask/7715-permission-types";
|
|
2
|
+
import type { Caveat } from "@metamask/delegation-core";
|
|
3
|
+
import type { Hex } from "@metamask/utils";
|
|
4
|
+
/**
|
|
5
|
+
* A partially reconstructed permission object decoded from a permission context.
|
|
6
|
+
*
|
|
7
|
+
* This mirrors the shape of {@link PermissionRequest} for fields that can be
|
|
8
|
+
* deterministically recovered from the encoded permission context, and it
|
|
9
|
+
* augments the result with an explicit `expiry` property derived from the
|
|
10
|
+
* `TimestampEnforcer` terms, as well as the `origin` property.
|
|
11
|
+
*/
|
|
12
|
+
export type DecodedPermission = Pick<PermissionRequest<PermissionTypes>, 'chainId' | 'from' | 'to'> & {
|
|
13
|
+
permission: Omit<PermissionRequest<PermissionTypes>['permission'], 'isAdjustmentAllowed' | 'type' | 'data'> & {
|
|
14
|
+
type: PermissionTypes['type'];
|
|
15
|
+
data: PermissionTypes['data'];
|
|
16
|
+
justification?: string;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* @deprecated Use `rules` instead.
|
|
20
|
+
*/
|
|
21
|
+
expiry: number | null;
|
|
22
|
+
origin: string;
|
|
23
|
+
/** Rules recovered from caveats (e.g. redeemer allowlist). */
|
|
24
|
+
rules?: Rule[];
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Supported permission type identifiers that can be decoded from a permission context.
|
|
28
|
+
*/
|
|
29
|
+
export type PermissionType = DecodedPermission['permission']['type'];
|
|
30
|
+
/**
|
|
31
|
+
* Result of validating and decoding permission terms from caveats.
|
|
32
|
+
* When valid, includes expiry and decoded data; when invalid, includes the error.
|
|
33
|
+
*/
|
|
34
|
+
export type ValidateAndDecodeResult = {
|
|
35
|
+
isValid: true;
|
|
36
|
+
expiry: number | null;
|
|
37
|
+
data: DecodedPermission['permission']['data'];
|
|
38
|
+
rules?: Rule[];
|
|
39
|
+
} | {
|
|
40
|
+
isValid: false;
|
|
41
|
+
error: Error;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* A decoder that defines the required and optional enforcers for a permission
|
|
45
|
+
* type, and provides methods to test whether caveat addresses match the
|
|
46
|
+
* permission and to validate and decode permission terms from caveats.
|
|
47
|
+
*/
|
|
48
|
+
export type PermissionDecoder = {
|
|
49
|
+
permissionType: PermissionType;
|
|
50
|
+
requiredEnforcers: Map<Hex, number>;
|
|
51
|
+
optionalEnforcers: Set<Hex>;
|
|
52
|
+
/**
|
|
53
|
+
* Returns true if the given caveat addresses (enforcer addresses) match this
|
|
54
|
+
* decoder (required enforcers present with correct multiplicity, no
|
|
55
|
+
* forbidden enforcers).
|
|
56
|
+
*/
|
|
57
|
+
caveatAddressesMatch: (caveatAddresses: Hex[]) => boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Validates and decodes permission terms from the caveats. Returns a result
|
|
60
|
+
* object with isValid; when valid, includes expiry and data.
|
|
61
|
+
*/
|
|
62
|
+
validateAndDecodePermission: (caveats: Caveat<Hex>[]) => ValidateAndDecodeResult;
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,eAAe,EACf,IAAI,EACL,wCAAwC;AACzC,OAAO,KAAK,EAAE,MAAM,EAAE,kCAAkC;AACxD,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAG3C;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAClC,iBAAiB,CAAC,eAAe,CAAC,EAClC,SAAS,GAAG,MAAM,GAAG,IAAI,CAC1B,GAAG;IACF,UAAU,EAAE,IAAI,CACd,iBAAiB,CAAC,eAAe,CAAC,CAAC,YAAY,CAAC,EAChD,qBAAqB,GAAG,MAAM,GAAG,MAAM,CACxC,GAAG;QACF,IAAI,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;QAC9B,IAAI,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;QAE9B,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB,CAAC;IACF;;OAEG;IACH,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,8DAA8D;IAC9D,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC;CAChB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,iBAAiB,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC;AAErE;;;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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../../src/decodePermission/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n PermissionRequest,\n PermissionTypes,\n Rule,\n} from '@metamask/7715-permission-types';\nimport type { Caveat } from '@metamask/delegation-core';\nimport type { Hex } from '@metamask/utils';\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: PermissionTypes['type'];\n data: PermissionTypes['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 * 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"]}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.splitHex = exports.getTermsByEnforcer = exports.enforcersMatchRule = exports.buildEnforcerCountsAndSet = exports.extractExpiryFromCaveatTerms = void 0;
|
|
4
|
+
const utils_1 = require("@metamask/utils");
|
|
5
|
+
/**
|
|
6
|
+
* Extracts the expiry timestamp from TimestampEnforcer caveat terms.
|
|
7
|
+
* Terms are 32 bytes: first 16 bytes timestampAfterThreshold (must be 0),
|
|
8
|
+
* last 16 bytes timestampBeforeThreshold (expiry).
|
|
9
|
+
*
|
|
10
|
+
* @param terms - The hex-encoded terms from a TimestampEnforcer caveat.
|
|
11
|
+
* @returns The expiry timestamp in seconds.
|
|
12
|
+
* @throws If terms are invalid.
|
|
13
|
+
*/
|
|
14
|
+
const extractExpiryFromCaveatTerms = (terms) => {
|
|
15
|
+
if (terms.length !== 66) {
|
|
16
|
+
throw new Error(`Invalid TimestampEnforcer terms length: expected 66 characters (0x + 64 hex), got ${terms.length}`);
|
|
17
|
+
}
|
|
18
|
+
const [after, before] = splitHex(terms, [16, 16]);
|
|
19
|
+
if ((0, utils_1.hexToNumber)(after) !== 0) {
|
|
20
|
+
throw new Error('Invalid expiry: timestampAfterThreshold must be 0');
|
|
21
|
+
}
|
|
22
|
+
const expiry = (0, utils_1.hexToNumber)(before);
|
|
23
|
+
if (expiry === 0) {
|
|
24
|
+
throw new Error('Invalid expiry: timestampBeforeThreshold must be greater than 0');
|
|
25
|
+
}
|
|
26
|
+
return expiry;
|
|
27
|
+
};
|
|
28
|
+
exports.extractExpiryFromCaveatTerms = extractExpiryFromCaveatTerms;
|
|
29
|
+
/**
|
|
30
|
+
* Builds enforcer counts and set from caveat addresses (checksummed).
|
|
31
|
+
* Used by caveatAddressesMatch.
|
|
32
|
+
*
|
|
33
|
+
* @param caveatAddresses - List of enforcer contract addresses (hex).
|
|
34
|
+
* @returns Counts per enforcer and set of unique enforcers.
|
|
35
|
+
*/
|
|
36
|
+
function buildEnforcerCountsAndSet(caveatAddresses) {
|
|
37
|
+
const counts = new Map();
|
|
38
|
+
for (const addr of caveatAddresses.map(utils_1.getChecksumAddress)) {
|
|
39
|
+
counts.set(addr, (counts.get(addr) ?? 0) + 1);
|
|
40
|
+
}
|
|
41
|
+
return { counts, enforcersSet: new Set(counts.keys()) };
|
|
42
|
+
}
|
|
43
|
+
exports.buildEnforcerCountsAndSet = buildEnforcerCountsAndSet;
|
|
44
|
+
/**
|
|
45
|
+
* Returns true if the given counts/set match the rule (required counts exact,
|
|
46
|
+
* no enforcer outside required + optional).
|
|
47
|
+
*
|
|
48
|
+
* @param counts - Map of enforcer address to occurrence count.
|
|
49
|
+
* @param enforcersSet - Set of unique enforcer addresses present.
|
|
50
|
+
* @param requiredEnforcers - Map of required enforcer to required count.
|
|
51
|
+
* @param optionalEnforcers - Set of optional enforcer addresses.
|
|
52
|
+
* @returns True if the counts match the rule.
|
|
53
|
+
*/
|
|
54
|
+
function enforcersMatchRule(counts, enforcersSet, requiredEnforcers, optionalEnforcers) {
|
|
55
|
+
const allowedEnforcers = new Set([
|
|
56
|
+
...optionalEnforcers,
|
|
57
|
+
...requiredEnforcers.keys(),
|
|
58
|
+
]);
|
|
59
|
+
for (const addr of enforcersSet) {
|
|
60
|
+
if (!allowedEnforcers.has(addr)) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
for (const [addr, requiredCount] of requiredEnforcers.entries()) {
|
|
65
|
+
if ((counts.get(addr) ?? 0) !== requiredCount) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
exports.enforcersMatchRule = enforcersMatchRule;
|
|
72
|
+
/**
|
|
73
|
+
* Gets the terms for a given enforcer from a list of caveats.
|
|
74
|
+
*
|
|
75
|
+
* @param args - The arguments to this function.
|
|
76
|
+
* @param args.throwIfNotFound - Whether to throw an error if no matching enforcer is found. Default is true.
|
|
77
|
+
* @param args.caveats - The list of caveats to search.
|
|
78
|
+
* @param args.enforcer - The enforcer to search for.
|
|
79
|
+
* @returns The terms for the given enforcer.
|
|
80
|
+
*/
|
|
81
|
+
function getTermsByEnforcer({ caveats, enforcer, throwIfNotFound, }) {
|
|
82
|
+
const matchingCaveats = caveats.filter((caveat) => caveat.enforcer === enforcer);
|
|
83
|
+
if (matchingCaveats.length === 0) {
|
|
84
|
+
if (throwIfNotFound ?? true) {
|
|
85
|
+
throw new Error('Invalid caveats');
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
if (matchingCaveats.length > 1) {
|
|
90
|
+
throw new Error('Invalid caveats');
|
|
91
|
+
}
|
|
92
|
+
return matchingCaveats[0].terms;
|
|
93
|
+
}
|
|
94
|
+
exports.getTermsByEnforcer = getTermsByEnforcer;
|
|
95
|
+
/**
|
|
96
|
+
* Splits a 0x-prefixed hex string into parts according to the provided byte lengths.
|
|
97
|
+
*
|
|
98
|
+
* Each entry in `lengths` represents a part length in bytes; internally this is
|
|
99
|
+
* multiplied by 2 to derive the number of hexadecimal characters to slice. The
|
|
100
|
+
* returned substrings do not include the `0x` prefix and preserve leading zeros.
|
|
101
|
+
*
|
|
102
|
+
* Note: This function does not perform input validation (e.g., verifying the
|
|
103
|
+
* payload length equals the sum of requested lengths). Callers are expected to
|
|
104
|
+
* provide well-formed inputs.
|
|
105
|
+
*
|
|
106
|
+
* Example:
|
|
107
|
+
* splitHex('0x12345678', [1, 3]) => ['0x12', '0x345678']
|
|
108
|
+
*
|
|
109
|
+
* @param value - The 0x-prefixed hex string to split.
|
|
110
|
+
* @param lengths - The lengths of each part, in bytes.
|
|
111
|
+
* @returns An array of hex substrings (each with `0x` prefix), one for each part.
|
|
112
|
+
*/
|
|
113
|
+
function splitHex(value, lengths) {
|
|
114
|
+
let start = 2;
|
|
115
|
+
const parts = [];
|
|
116
|
+
for (const partLength of lengths) {
|
|
117
|
+
const partCharLength = partLength * 2;
|
|
118
|
+
const part = value.slice(start, start + partCharLength);
|
|
119
|
+
start += partCharLength;
|
|
120
|
+
parts.push(`0x${part}`);
|
|
121
|
+
}
|
|
122
|
+
return parts;
|
|
123
|
+
}
|
|
124
|
+
exports.splitHex = splitHex;
|
|
125
|
+
//# sourceMappingURL=utils.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":"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","sourcesContent":["import type { Caveat } from '@metamask/delegation-core';\nimport { getChecksumAddress, 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,5 +1,5 @@
|
|
|
1
|
-
import type { Caveat } from
|
|
2
|
-
import type { Hex } from
|
|
1
|
+
import type { Caveat } from "@metamask/delegation-core";
|
|
2
|
+
import type { Hex } from "@metamask/utils";
|
|
3
3
|
/**
|
|
4
4
|
* Extracts the expiry timestamp from TimestampEnforcer caveat terms.
|
|
5
5
|
* Terms are 32 bytes: first 16 bytes timestampAfterThreshold (must be 0),
|
|
@@ -65,4 +65,4 @@ export declare function getTermsByEnforcer<TThrowIfNotFound extends boolean = tr
|
|
|
65
65
|
* @returns An array of hex substrings (each with `0x` prefix), one for each part.
|
|
66
66
|
*/
|
|
67
67
|
export declare function splitHex(value: Hex, lengths: number[]): Hex[];
|
|
68
|
-
//# sourceMappingURL=utils.d.
|
|
68
|
+
//# sourceMappingURL=utils.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,kCAAkC;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,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,GAAG,EAAE,CAU7D"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Caveat } from "@metamask/delegation-core";
|
|
2
|
+
import type { Hex } from "@metamask/utils";
|
|
3
|
+
/**
|
|
4
|
+
* Extracts the expiry timestamp from TimestampEnforcer caveat terms.
|
|
5
|
+
* Terms are 32 bytes: first 16 bytes timestampAfterThreshold (must be 0),
|
|
6
|
+
* last 16 bytes timestampBeforeThreshold (expiry).
|
|
7
|
+
*
|
|
8
|
+
* @param terms - The hex-encoded terms from a TimestampEnforcer caveat.
|
|
9
|
+
* @returns The expiry timestamp in seconds.
|
|
10
|
+
* @throws If terms are invalid.
|
|
11
|
+
*/
|
|
12
|
+
export declare const extractExpiryFromCaveatTerms: (terms: Hex) => number;
|
|
13
|
+
/**
|
|
14
|
+
* Builds enforcer counts and set from caveat addresses (checksummed).
|
|
15
|
+
* Used by caveatAddressesMatch.
|
|
16
|
+
*
|
|
17
|
+
* @param caveatAddresses - List of enforcer contract addresses (hex).
|
|
18
|
+
* @returns Counts per enforcer and set of unique enforcers.
|
|
19
|
+
*/
|
|
20
|
+
export declare function buildEnforcerCountsAndSet(caveatAddresses: Hex[]): {
|
|
21
|
+
counts: Map<Hex, number>;
|
|
22
|
+
enforcersSet: Set<Hex>;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Returns true if the given counts/set match the rule (required counts exact,
|
|
26
|
+
* no enforcer outside required + optional).
|
|
27
|
+
*
|
|
28
|
+
* @param counts - Map of enforcer address to occurrence count.
|
|
29
|
+
* @param enforcersSet - Set of unique enforcer addresses present.
|
|
30
|
+
* @param requiredEnforcers - Map of required enforcer to required count.
|
|
31
|
+
* @param optionalEnforcers - Set of optional enforcer addresses.
|
|
32
|
+
* @returns True if the counts match the rule.
|
|
33
|
+
*/
|
|
34
|
+
export declare function enforcersMatchRule(counts: Map<Hex, number>, enforcersSet: Set<Hex>, requiredEnforcers: Map<Hex, number>, optionalEnforcers: Set<Hex>): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Gets the terms for a given enforcer from a list of caveats.
|
|
37
|
+
*
|
|
38
|
+
* @param args - The arguments to this function.
|
|
39
|
+
* @param args.throwIfNotFound - Whether to throw an error if no matching enforcer is found. Default is true.
|
|
40
|
+
* @param args.caveats - The list of caveats to search.
|
|
41
|
+
* @param args.enforcer - The enforcer to search for.
|
|
42
|
+
* @returns The terms for the given enforcer.
|
|
43
|
+
*/
|
|
44
|
+
export declare function getTermsByEnforcer<TThrowIfNotFound extends boolean = true>({ caveats, enforcer, throwIfNotFound, }: {
|
|
45
|
+
caveats: Caveat<Hex>[];
|
|
46
|
+
enforcer: Hex;
|
|
47
|
+
throwIfNotFound?: TThrowIfNotFound;
|
|
48
|
+
}): TThrowIfNotFound extends true ? Hex : Hex | null;
|
|
49
|
+
/**
|
|
50
|
+
* Splits a 0x-prefixed hex string into parts according to the provided byte lengths.
|
|
51
|
+
*
|
|
52
|
+
* Each entry in `lengths` represents a part length in bytes; internally this is
|
|
53
|
+
* multiplied by 2 to derive the number of hexadecimal characters to slice. The
|
|
54
|
+
* returned substrings do not include the `0x` prefix and preserve leading zeros.
|
|
55
|
+
*
|
|
56
|
+
* Note: This function does not perform input validation (e.g., verifying the
|
|
57
|
+
* payload length equals the sum of requested lengths). Callers are expected to
|
|
58
|
+
* provide well-formed inputs.
|
|
59
|
+
*
|
|
60
|
+
* Example:
|
|
61
|
+
* splitHex('0x12345678', [1, 3]) => ['0x12', '0x345678']
|
|
62
|
+
*
|
|
63
|
+
* @param value - The 0x-prefixed hex string to split.
|
|
64
|
+
* @param lengths - The lengths of each part, in bytes.
|
|
65
|
+
* @returns An array of hex substrings (each with `0x` prefix), one for each part.
|
|
66
|
+
*/
|
|
67
|
+
export declare function splitHex(value: Hex, lengths: number[]): Hex[];
|
|
68
|
+
//# sourceMappingURL=utils.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,kCAAkC;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,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,GAAG,EAAE,CAU7D"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getChecksumAddress, hexToNumber } from
|
|
1
|
+
import { getChecksumAddress, hexToNumber } from "@metamask/utils";
|
|
2
2
|
/**
|
|
3
3
|
* Extracts the expiry timestamp from TimestampEnforcer caveat terms.
|
|
4
4
|
* Terms are 32 bytes: first 16 bytes timestampAfterThreshold (must be 0),
|
|
@@ -114,4 +114,4 @@ export function splitHex(value, lengths) {
|
|
|
114
114
|
}
|
|
115
115
|
return parts;
|
|
116
116
|
}
|
|
117
|
-
//# sourceMappingURL=utils.
|
|
117
|
+
//# sourceMappingURL=utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../src/decodePermission/utils.ts"],"names":[],"mappings":"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","sourcesContent":["import type { Caveat } from '@metamask/delegation-core';\nimport { getChecksumAddress, 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"]}
|