@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.
Files changed (187) hide show
  1. package/CHANGELOG.md +0 -4
  2. package/dist/GatorPermissionsController-method-action-types.cjs +7 -0
  3. package/dist/GatorPermissionsController-method-action-types.cjs.map +1 -0
  4. package/dist/{GatorPermissionsController-method-action-types.d.ts → GatorPermissionsController-method-action-types.d.cts} +2 -2
  5. package/dist/GatorPermissionsController-method-action-types.d.cts.map +1 -0
  6. package/dist/GatorPermissionsController-method-action-types.d.mts +118 -0
  7. package/dist/GatorPermissionsController-method-action-types.d.mts.map +1 -0
  8. package/dist/{GatorPermissionsController-method-action-types.js → GatorPermissionsController-method-action-types.mjs} +1 -1
  9. package/dist/GatorPermissionsController-method-action-types.mjs.map +1 -0
  10. package/dist/GatorPermissionsController.cjs +589 -0
  11. package/dist/GatorPermissionsController.cjs.map +1 -0
  12. package/dist/{GatorPermissionsController.d.ts → GatorPermissionsController.d.cts} +12 -12
  13. package/dist/GatorPermissionsController.d.cts.map +1 -0
  14. package/dist/GatorPermissionsController.d.mts +219 -0
  15. package/dist/GatorPermissionsController.d.mts.map +1 -0
  16. package/dist/{GatorPermissionsController.js → GatorPermissionsController.mjs} +12 -12
  17. package/dist/GatorPermissionsController.mjs.map +1 -0
  18. package/dist/constants.cjs +29 -0
  19. package/dist/constants.cjs.map +1 -0
  20. package/dist/{constants.d.ts → constants.d.cts} +4 -4
  21. package/dist/constants.d.cts.map +1 -0
  22. package/dist/constants.d.mts +26 -0
  23. package/dist/constants.d.mts.map +1 -0
  24. package/dist/{constants.js → constants.mjs} +1 -1
  25. package/dist/constants.mjs.map +1 -0
  26. package/dist/decodePermission/decodePermission.cjs +110 -0
  27. package/dist/decodePermission/decodePermission.cjs.map +1 -0
  28. package/dist/decodePermission/{decodePermission.d.ts → decodePermission.d.cts} +3 -3
  29. package/dist/decodePermission/decodePermission.d.cts.map +1 -0
  30. package/dist/decodePermission/decodePermission.d.mts +72 -0
  31. package/dist/decodePermission/decodePermission.d.mts.map +1 -0
  32. package/dist/decodePermission/{decodePermission.js → decodePermission.mjs} +3 -3
  33. package/dist/decodePermission/decodePermission.mjs.map +1 -0
  34. package/dist/decodePermission/decoders/index.cjs +26 -0
  35. package/dist/decodePermission/decoders/index.cjs.map +1 -0
  36. package/dist/decodePermission/decoders/{index.d.ts → index.d.cts} +3 -3
  37. package/dist/decodePermission/decoders/index.d.cts.map +1 -0
  38. package/dist/decodePermission/decoders/index.d.mts +20 -0
  39. package/dist/decodePermission/decoders/index.d.mts.map +1 -0
  40. package/dist/decodePermission/decoders/{index.js → index.mjs} +3 -3
  41. package/dist/decodePermission/decoders/index.mjs.map +1 -0
  42. package/dist/decodePermission/decoders/makePermissionDecoder.cjs +108 -0
  43. package/dist/decodePermission/decoders/makePermissionDecoder.cjs.map +1 -0
  44. package/dist/decodePermission/decoders/{makePermissionDecoder.d.ts → makePermissionDecoder.d.cts} +3 -3
  45. package/dist/decodePermission/decoders/makePermissionDecoder.d.cts.map +1 -0
  46. package/dist/decodePermission/decoders/makePermissionDecoder.d.mts +46 -0
  47. package/dist/decodePermission/decoders/makePermissionDecoder.d.mts.map +1 -0
  48. package/dist/decodePermission/decoders/{makePermissionDecoder.js → makePermissionDecoder.mjs} +4 -4
  49. package/dist/decodePermission/decoders/makePermissionDecoder.mjs.map +1 -0
  50. package/dist/decodePermission/enforcerAddresses.cjs +39 -0
  51. package/dist/decodePermission/enforcerAddresses.cjs.map +1 -0
  52. package/dist/decodePermission/{enforcerAddresses.d.ts → enforcerAddresses.d.cts} +3 -3
  53. package/dist/decodePermission/enforcerAddresses.d.cts.map +1 -0
  54. package/dist/decodePermission/enforcerAddresses.d.mts +13 -0
  55. package/dist/decodePermission/enforcerAddresses.d.mts.map +1 -0
  56. package/dist/decodePermission/{enforcerAddresses.js → enforcerAddresses.mjs} +4 -4
  57. package/dist/decodePermission/enforcerAddresses.mjs.map +1 -0
  58. package/dist/decodePermission/index.cjs +10 -0
  59. package/dist/decodePermission/index.cjs.map +1 -0
  60. package/dist/decodePermission/{index.d.ts → index.d.cts} +4 -4
  61. package/dist/decodePermission/index.d.cts.map +1 -0
  62. package/dist/decodePermission/index.d.mts +4 -0
  63. package/dist/decodePermission/index.d.mts.map +1 -0
  64. package/dist/decodePermission/index.mjs +3 -0
  65. package/dist/decodePermission/index.mjs.map +1 -0
  66. package/dist/decodePermission/types.cjs +3 -0
  67. package/dist/decodePermission/types.cjs.map +1 -0
  68. package/dist/decodePermission/{types.d.ts → types.d.cts} +4 -4
  69. package/dist/decodePermission/types.d.cts.map +1 -0
  70. package/dist/decodePermission/types.d.mts +64 -0
  71. package/dist/decodePermission/types.d.mts.map +1 -0
  72. package/dist/decodePermission/types.mjs +2 -0
  73. package/dist/decodePermission/types.mjs.map +1 -0
  74. package/dist/decodePermission/utils.cjs +125 -0
  75. package/dist/decodePermission/utils.cjs.map +1 -0
  76. package/dist/decodePermission/{utils.d.ts → utils.d.cts} +3 -3
  77. package/dist/decodePermission/utils.d.cts.map +1 -0
  78. package/dist/decodePermission/utils.d.mts +68 -0
  79. package/dist/decodePermission/utils.d.mts.map +1 -0
  80. package/dist/decodePermission/{utils.js → utils.mjs} +2 -2
  81. package/dist/decodePermission/utils.mjs.map +1 -0
  82. package/dist/errors.cjs +54 -0
  83. package/dist/errors.cjs.map +1 -0
  84. package/dist/{errors.d.ts → errors.d.cts} +3 -3
  85. package/dist/errors.d.cts.map +1 -0
  86. package/dist/errors.d.mts +39 -0
  87. package/dist/errors.d.mts.map +1 -0
  88. package/dist/{errors.js → errors.mjs} +2 -2
  89. package/dist/errors.mjs.map +1 -0
  90. package/dist/index.cjs +14 -0
  91. package/dist/index.cjs.map +1 -0
  92. package/dist/{index.d.ts → index.d.cts} +10 -10
  93. package/dist/index.d.cts.map +1 -0
  94. package/dist/index.d.mts +10 -0
  95. package/dist/index.d.mts.map +1 -0
  96. package/dist/index.mjs +3 -0
  97. package/dist/index.mjs.map +1 -0
  98. package/dist/logger.cjs +10 -0
  99. package/dist/logger.cjs.map +1 -0
  100. package/dist/{logger.d.ts → logger.d.cts} +3 -2
  101. package/dist/logger.d.cts.map +1 -0
  102. package/dist/logger.d.mts +7 -0
  103. package/dist/logger.d.mts.map +1 -0
  104. package/dist/{logger.js → logger.mjs} +2 -2
  105. package/dist/logger.mjs.map +1 -0
  106. package/dist/payeeRule.cjs +3 -0
  107. package/dist/payeeRule.cjs.map +1 -0
  108. package/dist/{payeeRule.d.ts → payeeRule.d.cts} +2 -2
  109. package/dist/payeeRule.d.cts.map +1 -0
  110. package/dist/payeeRule.d.mts +12 -0
  111. package/dist/payeeRule.d.mts.map +1 -0
  112. package/dist/payeeRule.mjs +2 -0
  113. package/dist/payeeRule.mjs.map +1 -0
  114. package/dist/permissionOnChainStatus.cjs +140 -0
  115. package/dist/permissionOnChainStatus.cjs.map +1 -0
  116. package/dist/{permissionOnChainStatus.d.ts → permissionOnChainStatus.d.cts} +5 -5
  117. package/dist/permissionOnChainStatus.d.cts.map +1 -0
  118. package/dist/permissionOnChainStatus.d.mts +73 -0
  119. package/dist/permissionOnChainStatus.d.mts.map +1 -0
  120. package/dist/{permissionOnChainStatus.js → permissionOnChainStatus.mjs} +7 -7
  121. package/dist/permissionOnChainStatus.mjs.map +1 -0
  122. package/dist/redeemerRule.cjs +3 -0
  123. package/dist/redeemerRule.cjs.map +1 -0
  124. package/dist/{redeemerRule.d.ts → redeemerRule.d.cts} +2 -2
  125. package/dist/redeemerRule.d.cts.map +1 -0
  126. package/dist/redeemerRule.d.mts +12 -0
  127. package/dist/redeemerRule.d.mts.map +1 -0
  128. package/dist/redeemerRule.mjs +2 -0
  129. package/dist/redeemerRule.mjs.map +1 -0
  130. package/dist/types.cjs +28 -0
  131. package/dist/types.cjs.map +1 -0
  132. package/dist/{types.d.ts → types.d.cts} +4 -4
  133. package/dist/types.d.cts.map +1 -0
  134. package/dist/types.d.mts +161 -0
  135. package/dist/types.d.mts.map +1 -0
  136. package/dist/{types.js → types.mjs} +1 -1
  137. package/dist/types.mjs.map +1 -0
  138. package/dist/utils.cjs +41 -0
  139. package/dist/utils.cjs.map +1 -0
  140. package/dist/{utils.d.ts → utils.d.cts} +5 -5
  141. package/dist/utils.d.cts.map +1 -0
  142. package/dist/utils.d.mts +22 -0
  143. package/dist/utils.d.mts.map +1 -0
  144. package/dist/{utils.js → utils.mjs} +4 -4
  145. package/dist/utils.mjs.map +1 -0
  146. package/package.json +16 -12
  147. package/dist/GatorPermissionsController-method-action-types.d.ts.map +0 -1
  148. package/dist/GatorPermissionsController-method-action-types.js.map +0 -1
  149. package/dist/GatorPermissionsController.d.ts.map +0 -1
  150. package/dist/GatorPermissionsController.js.map +0 -1
  151. package/dist/constants.d.ts.map +0 -1
  152. package/dist/constants.js.map +0 -1
  153. package/dist/decodePermission/decodePermission.d.ts.map +0 -1
  154. package/dist/decodePermission/decodePermission.js.map +0 -1
  155. package/dist/decodePermission/decoders/index.d.ts.map +0 -1
  156. package/dist/decodePermission/decoders/index.js.map +0 -1
  157. package/dist/decodePermission/decoders/makePermissionDecoder.d.ts.map +0 -1
  158. package/dist/decodePermission/decoders/makePermissionDecoder.js.map +0 -1
  159. package/dist/decodePermission/enforcerAddresses.d.ts.map +0 -1
  160. package/dist/decodePermission/enforcerAddresses.js.map +0 -1
  161. package/dist/decodePermission/index.d.ts.map +0 -1
  162. package/dist/decodePermission/index.js +0 -3
  163. package/dist/decodePermission/index.js.map +0 -1
  164. package/dist/decodePermission/types.d.ts.map +0 -1
  165. package/dist/decodePermission/types.js +0 -2
  166. package/dist/decodePermission/types.js.map +0 -1
  167. package/dist/decodePermission/utils.d.ts.map +0 -1
  168. package/dist/decodePermission/utils.js.map +0 -1
  169. package/dist/errors.d.ts.map +0 -1
  170. package/dist/errors.js.map +0 -1
  171. package/dist/index.d.ts.map +0 -1
  172. package/dist/index.js +0 -3
  173. package/dist/index.js.map +0 -1
  174. package/dist/logger.d.ts.map +0 -1
  175. package/dist/logger.js.map +0 -1
  176. package/dist/payeeRule.d.ts.map +0 -1
  177. package/dist/payeeRule.js +0 -2
  178. package/dist/payeeRule.js.map +0 -1
  179. package/dist/permissionOnChainStatus.d.ts.map +0 -1
  180. package/dist/permissionOnChainStatus.js.map +0 -1
  181. package/dist/redeemerRule.d.ts.map +0 -1
  182. package/dist/redeemerRule.js +0 -2
  183. package/dist/redeemerRule.js.map +0 -1
  184. package/dist/types.d.ts.map +0 -1
  185. package/dist/types.js.map +0 -1
  186. package/dist/utils.d.ts.map +0 -1
  187. package/dist/utils.js.map +0 -1
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Delegation framework version used to select the correct deployed enforcer
3
+ * contract addresses from `@metamask/delegation-deployments`.
4
+ */
5
+ export declare const DELEGATION_FRAMEWORK_VERSION = "1.3.0";
6
+ /**
7
+ * `Rule.type` / `wallet_getSupportedExecutionPermissions` `ruleTypes` entry for
8
+ * redeemer allowlists (RedeemerEnforcer). Hosts should advertise this for every
9
+ * supported execution permission type.
10
+ */
11
+ export declare const EXECUTION_PERMISSION_REDEEMER_RULE_TYPE: "redeemer";
12
+ /**
13
+ * `Rule.type` / `wallet_getSupportedExecutionPermissions` `ruleTypes` entry for
14
+ * payee allowlists (AllowedCalldataEnforcer / AllowedTargetsEnforcer). Hosts
15
+ * should advertise this for every supported execution permission type that supports
16
+ * payee restrictions.
17
+ */
18
+ export declare const EXECUTION_PERMISSION_PAYEE_RULE_TYPE: "payee";
19
+ /**
20
+ * `Rule.type` / `wallet_getSupportedExecutionPermissions` `ruleTypes` entry for
21
+ * permission expiry derived from a TimestampEnforcer caveat. The decoded
22
+ * permission additionally hoists the expiry value onto its top-level `expiry`
23
+ * field for convenience.
24
+ */
25
+ export declare const EXECUTION_PERMISSION_EXPIRY_RULE_TYPE: "expiry";
26
+ //# sourceMappingURL=constants.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.mts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,4BAA4B,UAAU,CAAC;AAEpD;;;;GAIG;AACH,eAAO,MAAM,uCAAuC,YAAsB,CAAC;AAE3E;;;;;GAKG;AACH,eAAO,MAAM,oCAAoC,SAAmB,CAAC;AAErE;;;;;GAKG;AACH,eAAO,MAAM,qCAAqC,UAAoB,CAAC"}
@@ -23,4 +23,4 @@ export const EXECUTION_PERMISSION_PAYEE_RULE_TYPE = 'payee';
23
23
  * field for convenience.
24
24
  */
25
25
  export const EXECUTION_PERMISSION_EXPIRY_RULE_TYPE = 'expiry';
26
- //# sourceMappingURL=constants.js.map
26
+ //# sourceMappingURL=constants.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.mjs","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,OAAO,CAAC;AAEpD;;;;GAIG;AACH,MAAM,CAAC,MAAM,uCAAuC,GAAG,UAAmB,CAAC;AAE3E;;;;;GAKG;AACH,MAAM,CAAC,MAAM,oCAAoC,GAAG,OAAgB,CAAC;AAErE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qCAAqC,GAAG,QAAiB,CAAC","sourcesContent":["/**\n * Delegation framework version used to select the correct deployed enforcer\n * contract addresses from `@metamask/delegation-deployments`.\n */\nexport const DELEGATION_FRAMEWORK_VERSION = '1.3.0';\n\n/**\n * `Rule.type` / `wallet_getSupportedExecutionPermissions` `ruleTypes` entry for\n * redeemer allowlists (RedeemerEnforcer). Hosts should advertise this for every\n * supported execution permission type.\n */\nexport const EXECUTION_PERMISSION_REDEEMER_RULE_TYPE = 'redeemer' as const;\n\n/**\n * `Rule.type` / `wallet_getSupportedExecutionPermissions` `ruleTypes` entry for\n * payee allowlists (AllowedCalldataEnforcer / AllowedTargetsEnforcer). Hosts\n * should advertise this for every supported execution permission type that supports\n * payee restrictions.\n */\nexport const EXECUTION_PERMISSION_PAYEE_RULE_TYPE = 'payee' as const;\n\n/**\n * `Rule.type` / `wallet_getSupportedExecutionPermissions` `ruleTypes` entry for\n * permission expiry derived from a TimestampEnforcer caveat. The decoded\n * permission additionally hoists the expiry value onto its top-level `expiry`\n * field for convenience.\n */\nexport const EXECUTION_PERMISSION_EXPIRY_RULE_TYPE = 'expiry' as const;\n"]}
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.reconstructDecodedPermission = exports.selectUniqueDecoderAndDecodedPermission = exports.findDecodersWithMatchingCaveatAddresses = void 0;
4
+ const delegation_core_1 = require("@metamask/delegation-core");
5
+ const utils_1 = require("@metamask/utils");
6
+ /**
7
+ * Returns every permission decoder whose caveat-address pattern matches the
8
+ * given enforcer list for the chain. Used when more than one permission type
9
+ * can share the same enforcer set; the caller must disambiguate by validating
10
+ * caveat terms (see {@link selectUniqueDecoderAndDecodedPermission}).
11
+ *
12
+ * @param args - The arguments to this function.
13
+ * @param args.enforcers - List of enforcer contract addresses (hex strings).
14
+ * @param args.permissionDecoders - The permission decoders for the chain.
15
+ * @returns All decoders that match, possibly empty.
16
+ */
17
+ const findDecodersWithMatchingCaveatAddresses = ({ enforcers, permissionDecoders, }) => {
18
+ return permissionDecoders.filter((decoder) => decoder.caveatAddressesMatch(enforcers));
19
+ };
20
+ exports.findDecodersWithMatchingCaveatAddresses = findDecodersWithMatchingCaveatAddresses;
21
+ /**
22
+ * Runs {@link PermissionDecoder.validateAndDecodePermission} on each candidate
23
+ * decoder. Use when several decoders share the same caveat addresses.
24
+ *
25
+ * @param args - The arguments to this function.
26
+ * @param args.candidateDecoders - Decoders whose addresses already match the caveats.
27
+ * @param args.caveats - Caveats from the delegation.
28
+ * @returns The unique decoder and decoded expiry/data when exactly one decoder validates.
29
+ * @throws If `candidateDecoders` is empty, if no decoder validates, or if more than one decoder validates.
30
+ */
31
+ const selectUniqueDecoderAndDecodedPermission = ({ candidateDecoders, caveats, }) => {
32
+ if (candidateDecoders.length === 0) {
33
+ throw new Error('Unable to identify permission type');
34
+ }
35
+ const successfulDecodingResult = [];
36
+ const failedAttempts = [];
37
+ for (const decoder of candidateDecoders) {
38
+ const decodeResult = decoder.validateAndDecodePermission(caveats);
39
+ if (decodeResult.isValid) {
40
+ successfulDecodingResult.push({
41
+ decoder,
42
+ rules: decodeResult.rules,
43
+ data: decodeResult.data,
44
+ expiry: decodeResult.expiry,
45
+ });
46
+ }
47
+ else {
48
+ failedAttempts.push({
49
+ permissionType: decoder.permissionType,
50
+ error: decodeResult.error,
51
+ });
52
+ }
53
+ }
54
+ if (successfulDecodingResult.length === 1) {
55
+ return successfulDecodingResult[0];
56
+ }
57
+ if (successfulDecodingResult.length > 1) {
58
+ const types = successfulDecodingResult
59
+ .map((result) => result.decoder.permissionType)
60
+ .join(', ');
61
+ throw new Error(`Multiple permission types validate the same delegation caveats: ${types}`);
62
+ }
63
+ if (failedAttempts.length === 1) {
64
+ throw failedAttempts[0].error;
65
+ }
66
+ const details = failedAttempts
67
+ .map((attempt) => `${String(attempt.permissionType)}: ${attempt.error.message}`)
68
+ .join('; ');
69
+ throw new Error(`No permission type could validate the delegation caveats. Attempts: ${details}`);
70
+ };
71
+ exports.selectUniqueDecoderAndDecodedPermission = selectUniqueDecoderAndDecodedPermission;
72
+ /**
73
+ * Reconstructs a {@link DecodedPermission} object from primitive values
74
+ * obtained while decoding a permission context.
75
+ *
76
+ * @param args - The arguments to this function.
77
+ * @param args.chainId - Chain ID.
78
+ * @param args.permissionType - Identified permission type.
79
+ * @param args.delegator - Address of the account delegating permission.
80
+ * @param args.delegate - Address that will act under the granted permission.
81
+ * @param args.authority - Authority identifier; must be ROOT_AUTHORITY.
82
+ * @param args.expiry - Expiry timestamp (unix seconds) or null if unbounded.
83
+ * @param args.data - Permission-specific decoded data payload.
84
+ * @param args.justification - Human-readable justification for the permission.
85
+ * @param args.specifiedOrigin - The origin reported in the request metadata.
86
+ * @param args.rules - Rules recovered from caveats (e.g. redeemer allowlist).
87
+ *
88
+ * @returns The reconstructed {@link DecodedPermission}.
89
+ */
90
+ const reconstructDecodedPermission = ({ chainId, permissionType, delegator, delegate, authority, expiry, data, justification, specifiedOrigin, rules, }) => {
91
+ if (authority !== delegation_core_1.ROOT_AUTHORITY) {
92
+ throw new Error('Invalid authority');
93
+ }
94
+ const permission = {
95
+ chainId: (0, utils_1.numberToHex)(chainId),
96
+ from: delegator,
97
+ to: delegate,
98
+ permission: {
99
+ type: permissionType,
100
+ data,
101
+ justification,
102
+ },
103
+ expiry,
104
+ origin: specifiedOrigin,
105
+ ...(rules === undefined ? {} : { rules }),
106
+ };
107
+ return permission;
108
+ };
109
+ exports.reconstructDecodedPermission = reconstructDecodedPermission;
110
+ //# sourceMappingURL=decodePermission.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decodePermission.cjs","sourceRoot":"","sources":["../../src/decodePermission/decodePermission.ts"],"names":[],"mappings":";;;AACA,+DAA2D;AAC3D,2CAA8C;AAS9C;;;;;;;;;;GAUG;AACI,MAAM,uCAAuC,GAAG,CAAC,EACtD,SAAS,EACT,kBAAkB,GAInB,EAAuB,EAAE;IACxB,OAAO,kBAAkB,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAC3C,OAAO,CAAC,oBAAoB,CAAC,SAAS,CAAC,CACxC,CAAC;AACJ,CAAC,CAAC;AAVW,QAAA,uCAAuC,2CAUlD;AAcF;;;;;;;;;GASG;AACI,MAAM,uCAAuC,GAAG,CAAC,EACtD,iBAAiB,EACjB,OAAO,GAIR,EAA+B,EAAE;IAChC,IAAI,iBAAiB,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;IACxD,CAAC;IAED,MAAM,wBAAwB,GAAkC,EAAE,CAAC;IAEnE,MAAM,cAAc,GAAuD,EAAE,CAAC;IAE9E,KAAK,MAAM,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uEAAuE,OAAO,EAAE,CACjF,CAAC;AACJ,CAAC,CAAC;AA3DW,QAAA,uCAAuC,2CA2DlD;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACI,MAAM,4BAA4B,GAAG,CAAC,EAC3C,OAAO,EACP,cAAc,EACd,SAAS,EACT,QAAQ,EACR,SAAS,EACT,MAAM,EACN,IAAI,EACJ,aAAa,EACb,eAAe,EACf,KAAK,GAYN,EAAqB,EAAE;IACtB,IAAI,SAAS,KAAK,gCAAc,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;IACvC,CAAC;IAED,MAAM,UAAU,GAAsB;QACpC,OAAO,EAAE,IAAA,mBAAW,EAAC,OAAO,CAAC;QAC7B,IAAI,EAAE,SAAS;QACf,EAAE,EAAE,QAAQ;QACZ,UAAU,EAAE;YACV,IAAI,EAAE,cAAc;YACpB,IAAI;YACJ,aAAa;SACd;QACD,MAAM;QACN,MAAM,EAAE,eAAe;QACvB,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC;KAC1C,CAAC;IAEF,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC;AA1CW,QAAA,4BAA4B,gCA0CvC","sourcesContent":["import type { Caveat, Hex } from '@metamask/delegation-core';\nimport { ROOT_AUTHORITY } from '@metamask/delegation-core';\nimport { numberToHex } from '@metamask/utils';\n\nimport type {\n DecodedPermission,\n PermissionType,\n PermissionDecoder,\n ValidateAndDecodeResult,\n} from './types.js';\n\n/**\n * Returns every permission decoder whose caveat-address pattern matches the\n * given enforcer list for the chain. Used when more than one permission type\n * can share the same enforcer set; the caller must disambiguate by validating\n * caveat terms (see {@link selectUniqueDecoderAndDecodedPermission}).\n *\n * @param args - The arguments to this function.\n * @param args.enforcers - List of enforcer contract addresses (hex strings).\n * @param args.permissionDecoders - The permission decoders for the chain.\n * @returns All decoders that match, possibly empty.\n */\nexport const findDecodersWithMatchingCaveatAddresses = ({\n enforcers,\n permissionDecoders,\n}: {\n enforcers: Hex[];\n permissionDecoders: PermissionDecoder[];\n}): PermissionDecoder[] => {\n return permissionDecoders.filter((decoder) =>\n decoder.caveatAddressesMatch(enforcers),\n );\n};\n\ntype SuccessfulValidateAndDecodeResult = Extract<\n ValidateAndDecodeResult,\n { isValid: true }\n>;\n\ntype DecoderAndDecodedPermission = {\n decoder: PermissionDecoder;\n rules: SuccessfulValidateAndDecodeResult['rules'];\n data: SuccessfulValidateAndDecodeResult['data'];\n expiry: SuccessfulValidateAndDecodeResult['expiry'];\n};\n\n/**\n * Runs {@link PermissionDecoder.validateAndDecodePermission} on each candidate\n * decoder. Use when several decoders share the same caveat addresses.\n *\n * @param args - The arguments to this function.\n * @param args.candidateDecoders - Decoders whose addresses already match the caveats.\n * @param args.caveats - Caveats from the delegation.\n * @returns The unique decoder and decoded expiry/data when exactly one decoder validates.\n * @throws If `candidateDecoders` is empty, if no decoder validates, or if more than one decoder validates.\n */\nexport const selectUniqueDecoderAndDecodedPermission = ({\n candidateDecoders,\n caveats,\n}: {\n candidateDecoders: PermissionDecoder[];\n caveats: Caveat<Hex>[];\n}): DecoderAndDecodedPermission => {\n if (candidateDecoders.length === 0) {\n throw new Error('Unable to identify permission type');\n }\n\n const successfulDecodingResult: DecoderAndDecodedPermission[] = [];\n\n const failedAttempts: { permissionType: PermissionType; error: Error }[] = [];\n\n for (const decoder of candidateDecoders) {\n const decodeResult = decoder.validateAndDecodePermission(caveats);\n if (decodeResult.isValid) {\n successfulDecodingResult.push({\n decoder,\n rules: decodeResult.rules,\n data: decodeResult.data,\n expiry: decodeResult.expiry,\n });\n } else {\n failedAttempts.push({\n permissionType: decoder.permissionType,\n error: decodeResult.error,\n });\n }\n }\n\n if (successfulDecodingResult.length === 1) {\n return successfulDecodingResult[0];\n }\n\n if (successfulDecodingResult.length > 1) {\n const types = successfulDecodingResult\n .map((result) => result.decoder.permissionType)\n .join(', ');\n throw new Error(\n `Multiple permission types validate the same delegation caveats: ${types}`,\n );\n }\n\n if (failedAttempts.length === 1) {\n throw failedAttempts[0].error;\n }\n\n const details = failedAttempts\n .map(\n (attempt) =>\n `${String(attempt.permissionType)}: ${attempt.error.message}`,\n )\n .join('; ');\n\n throw new Error(\n `No permission type could validate the delegation caveats. Attempts: ${details}`,\n );\n};\n\n/**\n * Reconstructs a {@link DecodedPermission} object from primitive values\n * obtained while decoding a permission context.\n *\n * @param args - The arguments to this function.\n * @param args.chainId - Chain ID.\n * @param args.permissionType - Identified permission type.\n * @param args.delegator - Address of the account delegating permission.\n * @param args.delegate - Address that will act under the granted permission.\n * @param args.authority - Authority identifier; must be ROOT_AUTHORITY.\n * @param args.expiry - Expiry timestamp (unix seconds) or null if unbounded.\n * @param args.data - Permission-specific decoded data payload.\n * @param args.justification - Human-readable justification for the permission.\n * @param args.specifiedOrigin - The origin reported in the request metadata.\n * @param args.rules - Rules recovered from caveats (e.g. redeemer allowlist).\n *\n * @returns The reconstructed {@link DecodedPermission}.\n */\nexport const reconstructDecodedPermission = ({\n chainId,\n permissionType,\n delegator,\n delegate,\n authority,\n expiry,\n data,\n justification,\n specifiedOrigin,\n rules,\n}: {\n chainId: number;\n permissionType: PermissionType;\n delegator: Hex;\n delegate: Hex;\n authority: Hex;\n expiry: number | null;\n data: DecodedPermission['permission']['data'];\n justification: string;\n specifiedOrigin: string;\n rules?: DecodedPermission['rules'];\n}): DecodedPermission => {\n if (authority !== ROOT_AUTHORITY) {\n throw new Error('Invalid authority');\n }\n\n const permission: DecodedPermission = {\n chainId: numberToHex(chainId),\n from: delegator,\n to: delegate,\n permission: {\n type: permissionType,\n data,\n justification,\n },\n expiry,\n origin: specifiedOrigin,\n ...(rules === undefined ? {} : { rules }),\n };\n\n return permission;\n};\n"]}
@@ -1,5 +1,5 @@
1
- import type { Caveat, Hex } from '@metamask/delegation-core';
2
- import type { DecodedPermission, PermissionType, PermissionDecoder, ValidateAndDecodeResult } from './types.js';
1
+ import type { Caveat, Hex } from "@metamask/delegation-core";
2
+ import type { DecodedPermission, PermissionType, PermissionDecoder, ValidateAndDecodeResult } from "./types.cjs";
3
3
  /**
4
4
  * Returns every permission decoder whose caveat-address pattern matches the
5
5
  * given enforcer list for the chain. Used when more than one permission type
@@ -69,4 +69,4 @@ export declare const reconstructDecodedPermission: ({ chainId, permissionType, d
69
69
  rules?: DecodedPermission['rules'];
70
70
  }) => DecodedPermission;
71
71
  export {};
72
- //# sourceMappingURL=decodePermission.d.ts.map
72
+ //# sourceMappingURL=decodePermission.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decodePermission.d.cts","sourceRoot":"","sources":["../../src/decodePermission/decodePermission.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,kCAAkC;AAI7D,OAAO,KAAK,EACV,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EACjB,uBAAuB,EACxB,oBAAmB;AAEpB;;;;;;;;;;GAUG;AACH,eAAO,MAAM,uCAAuC;eAIvC,GAAG,EAAE;wBACI,iBAAiB,EAAE;MACrC,iBAAiB,EAIpB,CAAC;AAEF,KAAK,iCAAiC,GAAG,OAAO,CAC9C,uBAAuB,EACvB;IAAE,OAAO,EAAE,IAAI,CAAA;CAAE,CAClB,CAAC;AAEF,KAAK,2BAA2B,GAAG;IACjC,OAAO,EAAE,iBAAiB,CAAC;IAC3B,KAAK,EAAE,iCAAiC,CAAC,OAAO,CAAC,CAAC;IAClD,IAAI,EAAE,iCAAiC,CAAC,MAAM,CAAC,CAAC;IAChD,MAAM,EAAE,iCAAiC,CAAC,QAAQ,CAAC,CAAC;CACrD,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,uCAAuC;uBAI/B,iBAAiB,EAAE;aAC7B,OAAO,GAAG,CAAC,EAAE;MACpB,2BAqDH,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,4BAA4B;aAY9B,MAAM;oBACC,cAAc;eACnB,GAAG;cACJ,GAAG;eACF,GAAG;YACN,MAAM,GAAG,IAAI;UACf,iBAAiB,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC;mBAC9B,MAAM;qBACJ,MAAM;YACf,iBAAiB,CAAC,OAAO,CAAC;MAChC,iBAoBH,CAAC"}
@@ -0,0 +1,72 @@
1
+ import type { Caveat, Hex } from "@metamask/delegation-core";
2
+ import type { DecodedPermission, PermissionType, PermissionDecoder, ValidateAndDecodeResult } from "./types.mjs";
3
+ /**
4
+ * Returns every permission decoder whose caveat-address pattern matches the
5
+ * given enforcer list for the chain. Used when more than one permission type
6
+ * can share the same enforcer set; the caller must disambiguate by validating
7
+ * caveat terms (see {@link selectUniqueDecoderAndDecodedPermission}).
8
+ *
9
+ * @param args - The arguments to this function.
10
+ * @param args.enforcers - List of enforcer contract addresses (hex strings).
11
+ * @param args.permissionDecoders - The permission decoders for the chain.
12
+ * @returns All decoders that match, possibly empty.
13
+ */
14
+ export declare const findDecodersWithMatchingCaveatAddresses: ({ enforcers, permissionDecoders, }: {
15
+ enforcers: Hex[];
16
+ permissionDecoders: PermissionDecoder[];
17
+ }) => PermissionDecoder[];
18
+ type SuccessfulValidateAndDecodeResult = Extract<ValidateAndDecodeResult, {
19
+ isValid: true;
20
+ }>;
21
+ type DecoderAndDecodedPermission = {
22
+ decoder: PermissionDecoder;
23
+ rules: SuccessfulValidateAndDecodeResult['rules'];
24
+ data: SuccessfulValidateAndDecodeResult['data'];
25
+ expiry: SuccessfulValidateAndDecodeResult['expiry'];
26
+ };
27
+ /**
28
+ * Runs {@link PermissionDecoder.validateAndDecodePermission} on each candidate
29
+ * decoder. Use when several decoders share the same caveat addresses.
30
+ *
31
+ * @param args - The arguments to this function.
32
+ * @param args.candidateDecoders - Decoders whose addresses already match the caveats.
33
+ * @param args.caveats - Caveats from the delegation.
34
+ * @returns The unique decoder and decoded expiry/data when exactly one decoder validates.
35
+ * @throws If `candidateDecoders` is empty, if no decoder validates, or if more than one decoder validates.
36
+ */
37
+ export declare const selectUniqueDecoderAndDecodedPermission: ({ candidateDecoders, caveats, }: {
38
+ candidateDecoders: PermissionDecoder[];
39
+ caveats: Caveat<Hex>[];
40
+ }) => DecoderAndDecodedPermission;
41
+ /**
42
+ * Reconstructs a {@link DecodedPermission} object from primitive values
43
+ * obtained while decoding a permission context.
44
+ *
45
+ * @param args - The arguments to this function.
46
+ * @param args.chainId - Chain ID.
47
+ * @param args.permissionType - Identified permission type.
48
+ * @param args.delegator - Address of the account delegating permission.
49
+ * @param args.delegate - Address that will act under the granted permission.
50
+ * @param args.authority - Authority identifier; must be ROOT_AUTHORITY.
51
+ * @param args.expiry - Expiry timestamp (unix seconds) or null if unbounded.
52
+ * @param args.data - Permission-specific decoded data payload.
53
+ * @param args.justification - Human-readable justification for the permission.
54
+ * @param args.specifiedOrigin - The origin reported in the request metadata.
55
+ * @param args.rules - Rules recovered from caveats (e.g. redeemer allowlist).
56
+ *
57
+ * @returns The reconstructed {@link DecodedPermission}.
58
+ */
59
+ export declare const reconstructDecodedPermission: ({ chainId, permissionType, delegator, delegate, authority, expiry, data, justification, specifiedOrigin, rules, }: {
60
+ chainId: number;
61
+ permissionType: PermissionType;
62
+ delegator: Hex;
63
+ delegate: Hex;
64
+ authority: Hex;
65
+ expiry: number | null;
66
+ data: DecodedPermission['permission']['data'];
67
+ justification: string;
68
+ specifiedOrigin: string;
69
+ rules?: DecodedPermission['rules'];
70
+ }) => DecodedPermission;
71
+ export {};
72
+ //# sourceMappingURL=decodePermission.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decodePermission.d.mts","sourceRoot":"","sources":["../../src/decodePermission/decodePermission.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,kCAAkC;AAI7D,OAAO,KAAK,EACV,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EACjB,uBAAuB,EACxB,oBAAmB;AAEpB;;;;;;;;;;GAUG;AACH,eAAO,MAAM,uCAAuC;eAIvC,GAAG,EAAE;wBACI,iBAAiB,EAAE;MACrC,iBAAiB,EAIpB,CAAC;AAEF,KAAK,iCAAiC,GAAG,OAAO,CAC9C,uBAAuB,EACvB;IAAE,OAAO,EAAE,IAAI,CAAA;CAAE,CAClB,CAAC;AAEF,KAAK,2BAA2B,GAAG;IACjC,OAAO,EAAE,iBAAiB,CAAC;IAC3B,KAAK,EAAE,iCAAiC,CAAC,OAAO,CAAC,CAAC;IAClD,IAAI,EAAE,iCAAiC,CAAC,MAAM,CAAC,CAAC;IAChD,MAAM,EAAE,iCAAiC,CAAC,QAAQ,CAAC,CAAC;CACrD,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,uCAAuC;uBAI/B,iBAAiB,EAAE;aAC7B,OAAO,GAAG,CAAC,EAAE;MACpB,2BAqDH,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,4BAA4B;aAY9B,MAAM;oBACC,cAAc;eACnB,GAAG;cACJ,GAAG;eACF,GAAG;YACN,MAAM,GAAG,IAAI;UACf,iBAAiB,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC;mBAC9B,MAAM;qBACJ,MAAM;YACf,iBAAiB,CAAC,OAAO,CAAC;MAChC,iBAoBH,CAAC"}
@@ -1,5 +1,5 @@
1
- import { ROOT_AUTHORITY } from '@metamask/delegation-core';
2
- import { numberToHex } from '@metamask/utils';
1
+ import { ROOT_AUTHORITY } from "@metamask/delegation-core";
2
+ import { numberToHex } from "@metamask/utils";
3
3
  /**
4
4
  * Returns every permission decoder whose caveat-address pattern matches the
5
5
  * given enforcer list for the chain. Used when more than one permission type
@@ -101,4 +101,4 @@ export const reconstructDecodedPermission = ({ chainId, permissionType, delegato
101
101
  };
102
102
  return permission;
103
103
  };
104
- //# sourceMappingURL=decodePermission.js.map
104
+ //# sourceMappingURL=decodePermission.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decodePermission.mjs","sourceRoot":"","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aAAa;SACd;QACD,MAAM;QACN,MAAM,EAAE,eAAe;QACvB,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC;KAC1C,CAAC;IAEF,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC","sourcesContent":["import type { Caveat, Hex } from '@metamask/delegation-core';\nimport { ROOT_AUTHORITY } from '@metamask/delegation-core';\nimport { numberToHex } from '@metamask/utils';\n\nimport type {\n DecodedPermission,\n PermissionType,\n PermissionDecoder,\n ValidateAndDecodeResult,\n} from './types.js';\n\n/**\n * Returns every permission decoder whose caveat-address pattern matches the\n * given enforcer list for the chain. Used when more than one permission type\n * can share the same enforcer set; the caller must disambiguate by validating\n * caveat terms (see {@link selectUniqueDecoderAndDecodedPermission}).\n *\n * @param args - The arguments to this function.\n * @param args.enforcers - List of enforcer contract addresses (hex strings).\n * @param args.permissionDecoders - The permission decoders for the chain.\n * @returns All decoders that match, possibly empty.\n */\nexport const findDecodersWithMatchingCaveatAddresses = ({\n enforcers,\n permissionDecoders,\n}: {\n enforcers: Hex[];\n permissionDecoders: PermissionDecoder[];\n}): PermissionDecoder[] => {\n return permissionDecoders.filter((decoder) =>\n decoder.caveatAddressesMatch(enforcers),\n );\n};\n\ntype SuccessfulValidateAndDecodeResult = Extract<\n ValidateAndDecodeResult,\n { isValid: true }\n>;\n\ntype DecoderAndDecodedPermission = {\n decoder: PermissionDecoder;\n rules: SuccessfulValidateAndDecodeResult['rules'];\n data: SuccessfulValidateAndDecodeResult['data'];\n expiry: SuccessfulValidateAndDecodeResult['expiry'];\n};\n\n/**\n * Runs {@link PermissionDecoder.validateAndDecodePermission} on each candidate\n * decoder. Use when several decoders share the same caveat addresses.\n *\n * @param args - The arguments to this function.\n * @param args.candidateDecoders - Decoders whose addresses already match the caveats.\n * @param args.caveats - Caveats from the delegation.\n * @returns The unique decoder and decoded expiry/data when exactly one decoder validates.\n * @throws If `candidateDecoders` is empty, if no decoder validates, or if more than one decoder validates.\n */\nexport const selectUniqueDecoderAndDecodedPermission = ({\n candidateDecoders,\n caveats,\n}: {\n candidateDecoders: PermissionDecoder[];\n caveats: Caveat<Hex>[];\n}): DecoderAndDecodedPermission => {\n if (candidateDecoders.length === 0) {\n throw new Error('Unable to identify permission type');\n }\n\n const successfulDecodingResult: DecoderAndDecodedPermission[] = [];\n\n const failedAttempts: { permissionType: PermissionType; error: Error }[] = [];\n\n for (const decoder of candidateDecoders) {\n const decodeResult = decoder.validateAndDecodePermission(caveats);\n if (decodeResult.isValid) {\n successfulDecodingResult.push({\n decoder,\n rules: decodeResult.rules,\n data: decodeResult.data,\n expiry: decodeResult.expiry,\n });\n } else {\n failedAttempts.push({\n permissionType: decoder.permissionType,\n error: decodeResult.error,\n });\n }\n }\n\n if (successfulDecodingResult.length === 1) {\n return successfulDecodingResult[0];\n }\n\n if (successfulDecodingResult.length > 1) {\n const types = successfulDecodingResult\n .map((result) => result.decoder.permissionType)\n .join(', ');\n throw new Error(\n `Multiple permission types validate the same delegation caveats: ${types}`,\n );\n }\n\n if (failedAttempts.length === 1) {\n throw failedAttempts[0].error;\n }\n\n const details = failedAttempts\n .map(\n (attempt) =>\n `${String(attempt.permissionType)}: ${attempt.error.message}`,\n )\n .join('; ');\n\n throw new Error(\n `No permission type could validate the delegation caveats. Attempts: ${details}`,\n );\n};\n\n/**\n * Reconstructs a {@link DecodedPermission} object from primitive values\n * obtained while decoding a permission context.\n *\n * @param args - The arguments to this function.\n * @param args.chainId - Chain ID.\n * @param args.permissionType - Identified permission type.\n * @param args.delegator - Address of the account delegating permission.\n * @param args.delegate - Address that will act under the granted permission.\n * @param args.authority - Authority identifier; must be ROOT_AUTHORITY.\n * @param args.expiry - Expiry timestamp (unix seconds) or null if unbounded.\n * @param args.data - Permission-specific decoded data payload.\n * @param args.justification - Human-readable justification for the permission.\n * @param args.specifiedOrigin - The origin reported in the request metadata.\n * @param args.rules - Rules recovered from caveats (e.g. redeemer allowlist).\n *\n * @returns The reconstructed {@link DecodedPermission}.\n */\nexport const reconstructDecodedPermission = ({\n chainId,\n permissionType,\n delegator,\n delegate,\n authority,\n expiry,\n data,\n justification,\n specifiedOrigin,\n rules,\n}: {\n chainId: number;\n permissionType: PermissionType;\n delegator: Hex;\n delegate: Hex;\n authority: Hex;\n expiry: number | null;\n data: DecodedPermission['permission']['data'];\n justification: string;\n specifiedOrigin: string;\n rules?: DecodedPermission['rules'];\n}): DecodedPermission => {\n if (authority !== ROOT_AUTHORITY) {\n throw new Error('Invalid authority');\n }\n\n const permission: DecodedPermission = {\n chainId: numberToHex(chainId),\n from: delegator,\n to: delegate,\n permission: {\n type: permissionType,\n data,\n justification,\n },\n expiry,\n origin: specifiedOrigin,\n ...(rules === undefined ? {} : { rules }),\n };\n\n return permission;\n};\n"]}
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createPermissionDecodersForContracts = void 0;
4
+ const _7715_permission_types_1 = require("@metamask/7715-permission-types");
5
+ const makePermissionDecoder_js_1 = require("./makePermissionDecoder.cjs");
6
+ /**
7
+ * Builds the canonical set of permission decoders for a chain.
8
+ *
9
+ * Each decoder specifies the `permissionType`, required/optional enforcers,
10
+ * and provides `caveatAddressesMatch` and `validateAndDecodePermission` so the
11
+ * entire decode flow can be driven by the decoders.
12
+ *
13
+ * `contracts` should already be checksummed (see {@link toEnforcerAddressesByName}).
14
+ * `makePermissionDecoderConfigs` checksums them again, and {@link makePermissionDecoder}
15
+ * normalizes enforcer sets once more — see the module comment in
16
+ * `makePermissionDecoder.ts` for why checksumming is layered this way.
17
+ *
18
+ * @param contracts - The deployed enforcer addresses for the chain.
19
+ * @returns A list of permission decoders used to identify and decode permission types.
20
+ * @throws Propagates any errors from resolving enforcer addresses.
21
+ */
22
+ const createPermissionDecodersForContracts = (contracts) => {
23
+ return (0, _7715_permission_types_1.makePermissionDecoderConfigs)(contracts).map(makePermissionDecoder_js_1.makePermissionDecoder);
24
+ };
25
+ exports.createPermissionDecodersForContracts = createPermissionDecodersForContracts;
26
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/decodePermission/decoders/index.ts"],"names":[],"mappings":";;;AAAA,4EAA+E;AAI/E,0EAAmE;AAEnE;;;;;;;;;;;;;;;GAeG;AACI,MAAM,oCAAoC,GAAG,CAClD,SAAkC,EACb,EAAE;IACvB,OAAO,IAAA,qDAA4B,EAAC,SAAS,CAAC,CAAC,GAAG,CAAC,gDAAqB,CAAC,CAAC;AAC5E,CAAC,CAAC;AAJW,QAAA,oCAAoC,wCAI/C","sourcesContent":["import { makePermissionDecoderConfigs } from '@metamask/7715-permission-types';\nimport type { EnforcerAddressesByName } from '@metamask/7715-permission-types';\n\nimport type { PermissionDecoder } from '../types.js';\nimport { makePermissionDecoder } from './makePermissionDecoder.js';\n\n/**\n * Builds the canonical set of permission decoders for a chain.\n *\n * Each decoder specifies the `permissionType`, required/optional enforcers,\n * and provides `caveatAddressesMatch` and `validateAndDecodePermission` so the\n * entire decode flow can be driven by the decoders.\n *\n * `contracts` should already be checksummed (see {@link toEnforcerAddressesByName}).\n * `makePermissionDecoderConfigs` checksums them again, and {@link makePermissionDecoder}\n * normalizes enforcer sets once more — see the module comment in\n * `makePermissionDecoder.ts` for why checksumming is layered this way.\n *\n * @param contracts - The deployed enforcer addresses for the chain.\n * @returns A list of permission decoders used to identify and decode permission types.\n * @throws Propagates any errors from resolving enforcer addresses.\n */\nexport const createPermissionDecodersForContracts = (\n contracts: EnforcerAddressesByName,\n): PermissionDecoder[] => {\n return makePermissionDecoderConfigs(contracts).map(makePermissionDecoder);\n};\n"]}
@@ -1,5 +1,5 @@
1
- import type { EnforcerAddressesByName } from '@metamask/7715-permission-types';
2
- import type { PermissionDecoder } from '../types.js';
1
+ import type { EnforcerAddressesByName } from "@metamask/7715-permission-types";
2
+ import type { PermissionDecoder } from "../types.cjs";
3
3
  /**
4
4
  * Builds the canonical set of permission decoders for a chain.
5
5
  *
@@ -17,4 +17,4 @@ import type { PermissionDecoder } from '../types.js';
17
17
  * @throws Propagates any errors from resolving enforcer addresses.
18
18
  */
19
19
  export declare const createPermissionDecodersForContracts: (contracts: EnforcerAddressesByName) => PermissionDecoder[];
20
- //# sourceMappingURL=index.d.ts.map
20
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/decodePermission/decoders/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,wCAAwC;AAE/E,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAoB;AAGrD;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,oCAAoC,cACpC,uBAAuB,KACjC,iBAAiB,EAEnB,CAAC"}
@@ -0,0 +1,20 @@
1
+ import type { EnforcerAddressesByName } from "@metamask/7715-permission-types";
2
+ import type { PermissionDecoder } from "../types.mjs";
3
+ /**
4
+ * Builds the canonical set of permission decoders for a chain.
5
+ *
6
+ * Each decoder specifies the `permissionType`, required/optional enforcers,
7
+ * and provides `caveatAddressesMatch` and `validateAndDecodePermission` so the
8
+ * entire decode flow can be driven by the decoders.
9
+ *
10
+ * `contracts` should already be checksummed (see {@link toEnforcerAddressesByName}).
11
+ * `makePermissionDecoderConfigs` checksums them again, and {@link makePermissionDecoder}
12
+ * normalizes enforcer sets once more — see the module comment in
13
+ * `makePermissionDecoder.ts` for why checksumming is layered this way.
14
+ *
15
+ * @param contracts - The deployed enforcer addresses for the chain.
16
+ * @returns A list of permission decoders used to identify and decode permission types.
17
+ * @throws Propagates any errors from resolving enforcer addresses.
18
+ */
19
+ export declare const createPermissionDecodersForContracts: (contracts: EnforcerAddressesByName) => PermissionDecoder[];
20
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/decodePermission/decoders/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,wCAAwC;AAE/E,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAoB;AAGrD;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,oCAAoC,cACpC,uBAAuB,KACjC,iBAAiB,EAEnB,CAAC"}
@@ -1,5 +1,5 @@
1
- import { makePermissionDecoderConfigs } from '@metamask/7715-permission-types';
2
- import { makePermissionDecoder } from './makePermissionDecoder.js';
1
+ import { makePermissionDecoderConfigs } from "@metamask/7715-permission-types";
2
+ import { makePermissionDecoder } from "./makePermissionDecoder.mjs";
3
3
  /**
4
4
  * Builds the canonical set of permission decoders for a chain.
5
5
  *
@@ -19,4 +19,4 @@ import { makePermissionDecoder } from './makePermissionDecoder.js';
19
19
  export const createPermissionDecodersForContracts = (contracts) => {
20
20
  return makePermissionDecoderConfigs(contracts).map(makePermissionDecoder);
21
21
  };
22
- //# sourceMappingURL=index.js.map
22
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/decodePermission/decoders/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,4BAA4B,EAAE,wCAAwC;AAI/E,OAAO,EAAE,qBAAqB,EAAE,oCAAmC;AAEnE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,oCAAoC,GAAG,CAClD,SAAkC,EACb,EAAE;IACvB,OAAO,4BAA4B,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;AAC5E,CAAC,CAAC","sourcesContent":["import { makePermissionDecoderConfigs } from '@metamask/7715-permission-types';\nimport type { EnforcerAddressesByName } from '@metamask/7715-permission-types';\n\nimport type { PermissionDecoder } from '../types.js';\nimport { makePermissionDecoder } from './makePermissionDecoder.js';\n\n/**\n * Builds the canonical set of permission decoders for a chain.\n *\n * Each decoder specifies the `permissionType`, required/optional enforcers,\n * and provides `caveatAddressesMatch` and `validateAndDecodePermission` so the\n * entire decode flow can be driven by the decoders.\n *\n * `contracts` should already be checksummed (see {@link toEnforcerAddressesByName}).\n * `makePermissionDecoderConfigs` checksums them again, and {@link makePermissionDecoder}\n * normalizes enforcer sets once more — see the module comment in\n * `makePermissionDecoder.ts` for why checksumming is layered this way.\n *\n * @param contracts - The deployed enforcer addresses for the chain.\n * @returns A list of permission decoders used to identify and decode permission types.\n * @throws Propagates any errors from resolving enforcer addresses.\n */\nexport const createPermissionDecodersForContracts = (\n contracts: EnforcerAddressesByName,\n): PermissionDecoder[] => {\n return makePermissionDecoderConfigs(contracts).map(makePermissionDecoder);\n};\n"]}
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makePermissionDecoder = void 0;
4
+ const utils_1 = require("@metamask/utils");
5
+ const constants_js_1 = require("../../constants.cjs");
6
+ const utils_js_1 = require("../utils.cjs");
7
+ /**
8
+ * Address checksumming in the permission decode flow.
9
+ *
10
+ * Enforcer addresses come from three sources, each of which may use a
11
+ * different hex encoding (EIP-55 checksummed vs lowercase). Comparisons rely
12
+ * on strict equality, so we normalize at function boundaries instead of
13
+ * requiring callers to pass a specific encoding. `getChecksumAddress` is
14
+ * idempotent, so repeated normalization is intentional and cheap.
15
+ *
16
+ * 1. **Canonical contract addresses** (`EnforcerAddressesByName`): normalized
17
+ * in {@link toEnforcerAddressesByName}, again in
18
+ * `makePermissionDecoderConfigs` (`checksumEnforcerAddresses` in
19
+ * `@metamask/7715-permission-types`), and once more below when building
20
+ * each decoder's required/optional enforcer sets.
21
+ *
22
+ * 2. **Delegation caveat enforcers**: read from the encoded permission
23
+ * context with unpredictable casing. Normalized in
24
+ * {@link buildEnforcerCountsAndSet} during address matching and again in
25
+ * `validateAndDecodePermission` before term decoding (rule decoders and
26
+ * `getTermsByEnforcer` compare enforcer addresses with `===`).
27
+ *
28
+ * 3. **Decoder required/optional enforcers**: populated from contract
29
+ * addresses by `makePermissionDecoderConfigs`; normalized at decoder
30
+ * construction so `caveatAddressesMatch` compares like-for-like with
31
+ * caveat addresses from source (2).
32
+ */
33
+ /**
34
+ * Creates a single {@link PermissionDecoder} with the given type, enforcer
35
+ * sets, rule decoders, and decode/validate callback.
36
+ *
37
+ * @param config - The configuration describing the permission type's
38
+ * enforcers, rule decoders, and data decoder. See
39
+ * {@link PermissionDecoderConfig} for field documentation.
40
+ * @param config.permissionType - The type of permission to decode.
41
+ * @param config.contractAddresses - Checksummed enforcer addresses for the chain.
42
+ * @param config.optionalEnforcers - Optional enforcers for the permission.
43
+ * @param config.requiredEnforcers - Required enforcers for the permission.
44
+ * @param config.rules - Rule decoders for the permission.
45
+ * @param config.validateAndDecodeData - Data decoder for the permission.
46
+ * @returns A {@link PermissionDecoder} with `caveatAddressesMatch` and
47
+ * `validateAndDecodePermission`.
48
+ */
49
+ function makePermissionDecoder({ permissionType, contractAddresses, optionalEnforcers, requiredEnforcers, rules, validateAndDecodeData, }) {
50
+ const optionalEnforcersSet = new Set(optionalEnforcers.map(utils_1.getChecksumAddress));
51
+ const requiredEnforcersMap = new Map(Object.entries(requiredEnforcers).map(([enforcer, count]) => [
52
+ (0, utils_1.getChecksumAddress)(enforcer),
53
+ count,
54
+ ]));
55
+ const caveatAddressesMatch = (caveatAddresses) => {
56
+ const { counts, enforcersSet } = (0, utils_js_1.buildEnforcerCountsAndSet)(caveatAddresses);
57
+ return (0, utils_js_1.enforcersMatchRule)(counts, enforcersSet, requiredEnforcersMap, optionalEnforcersSet);
58
+ };
59
+ const validateAndDecodePermission = (caveats) => {
60
+ const checksumCaveats = caveats.map((caveat) => ({
61
+ ...caveat,
62
+ enforcer: (0, utils_1.getChecksumAddress)(caveat.enforcer),
63
+ }));
64
+ try {
65
+ const invalidTerms = checksumCaveats.filter(
66
+ // isStrictHexString rejects '0x' which is a valid terms value
67
+ ({ terms }) => terms !== '0x' && !(0, utils_1.isStrictHexString)(terms));
68
+ if (invalidTerms.length > 0) {
69
+ throw new Error('Invalid terms: must be a hex string');
70
+ }
71
+ let expiry = null;
72
+ const decodedRules = [];
73
+ for (const decode of rules) {
74
+ const rule = decode({
75
+ contractAddresses,
76
+ caveats: checksumCaveats,
77
+ requiredEnforcers: requiredEnforcersMap,
78
+ });
79
+ if (rule === null) {
80
+ continue;
81
+ }
82
+ decodedRules.push(rule);
83
+ if (rule.type === constants_js_1.EXECUTION_PERMISSION_EXPIRY_RULE_TYPE) {
84
+ expiry = rule.data.timestamp;
85
+ }
86
+ }
87
+ const data = validateAndDecodeData(checksumCaveats, contractAddresses);
88
+ return {
89
+ isValid: true,
90
+ expiry,
91
+ data,
92
+ rules: decodedRules.length > 0 ? decodedRules : undefined,
93
+ };
94
+ }
95
+ catch (caughtError) {
96
+ return { isValid: false, error: caughtError };
97
+ }
98
+ };
99
+ return {
100
+ permissionType,
101
+ caveatAddressesMatch,
102
+ validateAndDecodePermission,
103
+ optionalEnforcers: optionalEnforcersSet,
104
+ requiredEnforcers: requiredEnforcersMap,
105
+ };
106
+ }
107
+ exports.makePermissionDecoder = makePermissionDecoder;
108
+ //# sourceMappingURL=makePermissionDecoder.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makePermissionDecoder.cjs","sourceRoot":"","sources":["../../../src/decodePermission/decoders/makePermissionDecoder.ts"],"names":[],"mappings":";;;AAKA,2CAAwE;AAGxE,sDAA2E;AAE3E,2CAA4E;AAE5E;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,qBAAqB,CAAC,EACpC,cAAc,EACd,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,KAAK,EACL,qBAAqB,GACG;IACxB,MAAM,oBAAoB,GAAG,IAAI,GAAG,CAClC,iBAAiB,CAAC,GAAG,CAAC,0BAAkB,CAAC,CAC1C,CAAC;IACF,MAAM,oBAAoB,GAAG,IAAI,GAAG,CAClC,MAAM,CAA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CAAC;gBAEH,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;oBAClB,SAAS;gBACX,CAAC;gBAED,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBAExB,IAAI,IAAI,CAAC,IAAI,KAAK,oDAAqC,EAAE,CAAC;oBACxD,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,SAAmB,CAAC;gBACzC,CAAC;YACH,CAAC;YAED,MAAM,IAAI,GAAG,qBAAqB,CAAC,eAAe,EAAE,iBAAiB,CAAC,CAAC;YAEvE,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,MAAM;gBACN,IAAI;gBACJ,KAAK,EAAE,YAAY,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,SAAS;aAC1D,CAAC;QACJ,CAAC;QAAC,OAAO,WAAW,EAAE,CAAC;YACrB,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,WAAoB,EAAE,CAAC;QACzD,CAAC;IACH,CAAC,CAAC;IAEF,OAAO;QACL,cAAc;QACd,oBAAoB;QACpB,2BAA2B;QAC3B,iBAAiB,EAAE,oBAAoB;QACvC,iBAAiB,EAAE,oBAAoB;KACxC,CAAC;AACJ,CAAC;AAvFD,sDAuFC","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"]}
@@ -1,5 +1,5 @@
1
- import type { PermissionDecoderConfig } from '@metamask/7715-permission-types';
2
- import type { PermissionDecoder } from '../types.js';
1
+ import type { PermissionDecoderConfig } from "@metamask/7715-permission-types";
2
+ import type { PermissionDecoder } from "../types.cjs";
3
3
  /**
4
4
  * Address checksumming in the permission decode flow.
5
5
  *
@@ -43,4 +43,4 @@ import type { PermissionDecoder } from '../types.js';
43
43
  * `validateAndDecodePermission`.
44
44
  */
45
45
  export declare function makePermissionDecoder({ permissionType, contractAddresses, optionalEnforcers, requiredEnforcers, rules, validateAndDecodeData, }: PermissionDecoderConfig): PermissionDecoder;
46
- //# sourceMappingURL=makePermissionDecoder.d.ts.map
46
+ //# sourceMappingURL=makePermissionDecoder.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makePermissionDecoder.d.cts","sourceRoot":"","sources":["../../../src/decodePermission/decoders/makePermissionDecoder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,uBAAuB,EAExB,wCAAwC;AAMzC,OAAO,KAAK,EAAE,iBAAiB,EAA2B,qBAAoB;AAG9E;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,qBAAqB,CAAC,EACpC,cAAc,EACd,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,KAAK,EACL,qBAAqB,GACtB,EAAE,uBAAuB,GAAG,iBAAiB,CAgF7C"}
@@ -0,0 +1,46 @@
1
+ import type { PermissionDecoderConfig } from "@metamask/7715-permission-types";
2
+ import type { PermissionDecoder } from "../types.mjs";
3
+ /**
4
+ * Address checksumming in the permission decode flow.
5
+ *
6
+ * Enforcer addresses come from three sources, each of which may use a
7
+ * different hex encoding (EIP-55 checksummed vs lowercase). Comparisons rely
8
+ * on strict equality, so we normalize at function boundaries instead of
9
+ * requiring callers to pass a specific encoding. `getChecksumAddress` is
10
+ * idempotent, so repeated normalization is intentional and cheap.
11
+ *
12
+ * 1. **Canonical contract addresses** (`EnforcerAddressesByName`): normalized
13
+ * in {@link toEnforcerAddressesByName}, again in
14
+ * `makePermissionDecoderConfigs` (`checksumEnforcerAddresses` in
15
+ * `@metamask/7715-permission-types`), and once more below when building
16
+ * each decoder's required/optional enforcer sets.
17
+ *
18
+ * 2. **Delegation caveat enforcers**: read from the encoded permission
19
+ * context with unpredictable casing. Normalized in
20
+ * {@link buildEnforcerCountsAndSet} during address matching and again in
21
+ * `validateAndDecodePermission` before term decoding (rule decoders and
22
+ * `getTermsByEnforcer` compare enforcer addresses with `===`).
23
+ *
24
+ * 3. **Decoder required/optional enforcers**: populated from contract
25
+ * addresses by `makePermissionDecoderConfigs`; normalized at decoder
26
+ * construction so `caveatAddressesMatch` compares like-for-like with
27
+ * caveat addresses from source (2).
28
+ */
29
+ /**
30
+ * Creates a single {@link PermissionDecoder} with the given type, enforcer
31
+ * sets, rule decoders, and decode/validate callback.
32
+ *
33
+ * @param config - The configuration describing the permission type's
34
+ * enforcers, rule decoders, and data decoder. See
35
+ * {@link PermissionDecoderConfig} for field documentation.
36
+ * @param config.permissionType - The type of permission to decode.
37
+ * @param config.contractAddresses - Checksummed enforcer addresses for the chain.
38
+ * @param config.optionalEnforcers - Optional enforcers for the permission.
39
+ * @param config.requiredEnforcers - Required enforcers for the permission.
40
+ * @param config.rules - Rule decoders for the permission.
41
+ * @param config.validateAndDecodeData - Data decoder for the permission.
42
+ * @returns A {@link PermissionDecoder} with `caveatAddressesMatch` and
43
+ * `validateAndDecodePermission`.
44
+ */
45
+ export declare function makePermissionDecoder({ permissionType, contractAddresses, optionalEnforcers, requiredEnforcers, rules, validateAndDecodeData, }: PermissionDecoderConfig): PermissionDecoder;
46
+ //# sourceMappingURL=makePermissionDecoder.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makePermissionDecoder.d.mts","sourceRoot":"","sources":["../../../src/decodePermission/decoders/makePermissionDecoder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,uBAAuB,EAExB,wCAAwC;AAMzC,OAAO,KAAK,EAAE,iBAAiB,EAA2B,qBAAoB;AAG9E;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,qBAAqB,CAAC,EACpC,cAAc,EACd,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,KAAK,EACL,qBAAqB,GACtB,EAAE,uBAAuB,GAAG,iBAAiB,CAgF7C"}
@@ -1,6 +1,6 @@
1
- import { getChecksumAddress, isStrictHexString } from '@metamask/utils';
2
- import { EXECUTION_PERMISSION_EXPIRY_RULE_TYPE } from '../../constants.js';
3
- import { buildEnforcerCountsAndSet, enforcersMatchRule } from '../utils.js';
1
+ import { getChecksumAddress, isStrictHexString } from "@metamask/utils";
2
+ import { EXECUTION_PERMISSION_EXPIRY_RULE_TYPE } from "../../constants.mjs";
3
+ import { buildEnforcerCountsAndSet, enforcersMatchRule } from "../utils.mjs";
4
4
  /**
5
5
  * Address checksumming in the permission decode flow.
6
6
  *
@@ -101,4 +101,4 @@ export function makePermissionDecoder({ permissionType, contractAddresses, optio
101
101
  requiredEnforcers: requiredEnforcersMap,
102
102
  };
103
103
  }
104
- //# sourceMappingURL=makePermissionDecoder.js.map
104
+ //# sourceMappingURL=makePermissionDecoder.mjs.map