feat: adds utility functions to create caveats array for each permission type#265
Conversation
16da695 to
05387b5
Compare
| // delegation-core accepts bigint for encoding although the type is `number`. | ||
| periodDuration: BigInt(UINT256_MAX) as unknown as number, |
There was a problem hiding this comment.
this is a temporary measure, until we migrate to the fixed allowance enforcer.
| terms: createNativeTokenPeriodTransferTerms({ | ||
| periodAmount: BigInt(allowanceAmount), | ||
| // delegation-core accepts bigint for encoding although the type is `number`. | ||
| periodDuration: BigInt(UINT256_MAX) as unknown as number, |
There was a problem hiding this comment.
this is a temporary measure, until we migrate to the fixed allowance enforcer.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 488e6ce. Configure here.
mj-kiwi
left a comment
There was a problem hiding this comment.
Some functions are labeled async yet contain no await calls. Should we turn them sync instead?
mj-kiwi
left a comment
There was a problem hiding this comment.
LGTM, it would be good if the following comments could be addressed
…ask/7715-permission-types package Amend comment to reflect that both undefined and null are removed from union types.
…validateAndDecode function
| export function makeErc20TokenStreamDecoderConfig( | ||
| contractAddresses: ChecksumEnforcersByChainId, | ||
| ): MakePermissionDecoderConfig { | ||
| ): PermissionDecoderConfig { |
There was a problem hiding this comment.
I believe the other six caveats use MakePermissionDecoderConfig instead of PermissionDecoderConfig. These two types are identical, just with different names. We could leave it as is, but updating all instances to MakePermissionDecoderConfig would be better for consistency.
There was a problem hiding this comment.
Oops - I removed the duplicate type, and settled on PermissionDecoderConfig as I felt that the Make prefix was unnecessary.

📝 Description
This PR adds utility functions to create caveats array for each permission type to @metamask/7715-permission-types. These were migrated from @metamask/gator-permissions-snap.
🔄 What Changed?
Adds new utility functions that accept a populated permission object, and specific caveat addresses, and returns the
Caveat<Hex>[]representing that permission.createErc20TokenStreamCaveats()createErc20TokenPeriodicCaveats()createErc20TokenAllowanceCaveats()createNativeTokenStreamCaveats()createNativeTokenPeriodicCaveats()createNativeTokenAllowanceCaveats()createTokenApprovalRevocationCaveats()🚀 Why?
Presently when adding new permission types to MetaMask Advanced Permissions, changes must be made across a number of different repositories. By centralising the permission definitions into a single repository, it allows us to add a permission definition in one place (@metamask/7715-permission-types), and simply update dependencies across @metamask/gator-permissions-controller, @metamask/gator-permissions-snap, @metamask/smart-accounts-kit, and MetaMask clients.
This change is migrating one of the components needed to achieve that vision.
🧪 How to Test?
The changes in this PR are integrated into @metamask/gator-permissions-snap in this draft PR MetaMask/snap-7715-permissions#363. Adding a reference to @metamask/7715-permission-types with the changes from this PR, then validate with the following steps:
List any breaking changes:
📋 Checklist
Check off completed items:
🔗 Related Issues
Link to related issues:
Closes #
Related to #
📚 Additional Notes
Any additional information, concerns, or context:
Note
Medium Risk
Incorrect caveat encoding or validation could grant wrong token/native limits or approvals on-chain; changes are additive with broad test coverage but touch security-sensitive permission construction.
Overview
Adds encode-path helpers in
@metamask/7715-permission-typesso consumers can turn a fully specified ERC-7715 permission into theCaveat[]used on-chain, mirroring the existing decoder logic. Sevencreate*Caveats()functions cover ERC-20/native stream, periodic, and allowance types plus token-approval-revocation, each paired with a small*Enforcerstype for the enforcer addresses they need.Inputs are typed with a new exported
Populated<T>helper (recursive required fields, no null/undefined). Builders validate amounts, durations, stream bounds, and start times before calling@metamask/delegation-coreterm encoders; ERC-20 paths also attach a zerovalueLtecaveat, native paths attach emptyexactCalldata, and revocation builds a single approval-revocation caveat.Public exports are wired through
src/index.tsandpermissions/index.ts, changelog is updated, and vitest suites exercise happy paths and validation errors. Decoder factories now returnPermissionDecoderConfigdirectly and theMakePermissionDecoderConfigalias is removed.Reviewed by Cursor Bugbot for commit 939ce23. Bugbot is set up for automated code reviews on this repo. Configure here.