-
-
Notifications
You must be signed in to change notification settings - Fork 282
Add decoders for token-approval-revocation permission type #8823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9114503
Add decoders for token-approval-revocation permission type
jeffsmale90 4e2ccd1
Fix linting
jeffsmale90 fe34a8f
Add changelog entry
jeffsmale90 b8dc019
Bump delegation dependencies:
jeffsmale90 4c87d7f
Fix some tests, add validation for empty terms, and fix some linting
jeffsmale90 cecc5c0
Bump @metamask/7715-permission-types from ^0.7.0 to ^0.7.1
jeffsmale90 bbef885
Deduplicated ApprovalRevocationTerms type definition
jeffsmale90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
...ator-permissions-controller/src/decodePermission/decoders/tokenApprovalRevocation.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import { createTimestampTerms } from '@metamask/delegation-core'; | ||
| import { | ||
| CHAIN_ID, | ||
| DELEGATOR_CONTRACTS, | ||
| } from '@metamask/delegation-deployments'; | ||
|
|
||
| import { createPermissionDecodersForContracts } from '.'; | ||
| import { getChecksumEnforcersByChainId } from '../utils'; | ||
|
|
||
| describe('token-approval-revocation decoder', () => { | ||
| const chainId = CHAIN_ID.sepolia; | ||
| const contracts = DELEGATOR_CONTRACTS['1.3.0'][chainId]; | ||
| const { timestampEnforcer, approvalRevocationEnforcer, nonceEnforcer } = | ||
| getChecksumEnforcersByChainId(contracts); | ||
| const permissionDecoders = createPermissionDecodersForContracts(contracts); | ||
| const decoder = permissionDecoders.find( | ||
| (candidate) => candidate.permissionType === 'token-approval-revocation', | ||
| ); | ||
|
|
||
| if (!decoder) { | ||
| throw new Error('Decoder not found'); | ||
| } | ||
|
|
||
| const expiryCaveat = { | ||
| enforcer: timestampEnforcer, | ||
| terms: createTimestampTerms({ | ||
| afterThreshold: 0, | ||
| beforeThreshold: 1720000, | ||
| }), | ||
| args: '0x' as const, | ||
| }; | ||
|
|
||
| it('rejects empty terms', () => { | ||
| const caveats = [ | ||
| expiryCaveat, | ||
| { | ||
| enforcer: approvalRevocationEnforcer, | ||
| terms: '0x' as const, | ||
| args: '0x' as const, | ||
| }, | ||
| { | ||
| enforcer: nonceEnforcer, | ||
| terms: '0x' as const, | ||
| args: '0x' as const, | ||
| }, | ||
| ]; | ||
|
|
||
| const result = decoder.validateAndDecodePermission(caveats); | ||
| expect(result.isValid).toBe(false); | ||
|
|
||
| if (result.isValid) { | ||
| throw new Error('Expected invalid result'); | ||
| } | ||
|
|
||
| expect(result.error.message).toContain( | ||
| 'Invalid ApprovalRevocation terms: must be greater than 0', | ||
| ); | ||
| }); | ||
|
|
||
| it('rejects terms whose mask exceeds the supported max', () => { | ||
| const caveats = [ | ||
| expiryCaveat, | ||
| { | ||
| enforcer: approvalRevocationEnforcer, | ||
| terms: '0x40' as const, | ||
| args: '0x' as const, | ||
| }, | ||
| { | ||
| enforcer: nonceEnforcer, | ||
| terms: '0x' as const, | ||
| args: '0x' as const, | ||
| }, | ||
| ]; | ||
|
|
||
| const result = decoder.validateAndDecodePermission(caveats); | ||
| expect(result.isValid).toBe(false); | ||
|
|
||
| if (result.isValid) { | ||
| throw new Error('Expected invalid result'); | ||
| } | ||
|
|
||
| expect(result.error.message).toContain( | ||
| 'Invalid ApprovalRevocation terms: must be less than or equal to 63', | ||
| ); | ||
| }); | ||
|
|
||
| it('successfully decodes valid token-approval-revocation caveats', () => { | ||
| const caveats = [ | ||
| expiryCaveat, | ||
| { | ||
| enforcer: approvalRevocationEnforcer, | ||
| terms: '0x01' as const, | ||
| args: '0x' as const, | ||
| }, | ||
| { | ||
| enforcer: nonceEnforcer, | ||
| terms: '0x' as const, | ||
| args: '0x' as const, | ||
| }, | ||
| ]; | ||
|
|
||
| const result = decoder.validateAndDecodePermission(caveats); | ||
| expect(result.isValid).toBe(true); | ||
|
|
||
| if (!result.isValid) { | ||
| throw new Error('Expected valid result'); | ||
| } | ||
|
|
||
| expect(result.expiry).toBe(1720000); | ||
| expect(result.data).toStrictEqual({ | ||
| erc20Approve: true, | ||
| erc721Approve: false, | ||
| erc721SetApprovalForAll: false, | ||
| permit2Approve: false, | ||
| permit2Lockdown: false, | ||
| permit2InvalidateNonces: false, | ||
| }); | ||
| expect(result.rules).toStrictEqual([ | ||
| { | ||
| type: 'expiry', | ||
| data: { timestamp: 1720000 }, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('decodes all supported flags from the terms bitmask', () => { | ||
| const caveats = [ | ||
| expiryCaveat, | ||
| { | ||
| enforcer: approvalRevocationEnforcer, | ||
| terms: '0x3f' as const, | ||
| args: '0x' as const, | ||
| }, | ||
| { | ||
| enforcer: nonceEnforcer, | ||
| terms: '0x' as const, | ||
| args: '0x' as const, | ||
| }, | ||
| ]; | ||
|
|
||
| const result = decoder.validateAndDecodePermission(caveats); | ||
| expect(result.isValid).toBe(true); | ||
|
|
||
| if (!result.isValid) { | ||
| throw new Error('Expected valid result'); | ||
| } | ||
|
|
||
| expect(result.data).toStrictEqual({ | ||
| erc20Approve: true, | ||
| erc721Approve: true, | ||
| erc721SetApprovalForAll: true, | ||
| permit2Approve: true, | ||
| permit2Lockdown: true, | ||
| permit2InvalidateNonces: true, | ||
| }); | ||
| }); | ||
| }); |
108 changes: 108 additions & 0 deletions
108
...ges/gator-permissions-controller/src/decodePermission/decoders/tokenApprovalRevocation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* eslint-disable no-bitwise */ | ||
| import { hexToNumber } from '@metamask/utils'; | ||
|
|
||
| import type { | ||
| ChecksumCaveat, | ||
| ChecksumEnforcersByChainId, | ||
| DecodedPermission, | ||
| } from '../types'; | ||
| import { getTermsByEnforcer } from '../utils'; | ||
| import { expiryRule } from './expiryRule'; | ||
| import type { MakePermissionDecoderConfig } from './makePermissionDecoder'; | ||
|
|
||
| enum ApprovalRevocationFlag { | ||
| Erc20Approve = 0x01, | ||
| Erc721Approve = 0x02, | ||
| Erc721SetApprovalForAll = 0x04, | ||
| Permit2Approve = 0x08, | ||
| Permit2Lockdown = 0x10, | ||
| Permit2InvalidateNonces = 0x20, | ||
| } | ||
|
|
||
| const MAX_APPROVAL_REVOCATION_MASK = | ||
| ApprovalRevocationFlag.Permit2InvalidateNonces | | ||
| ApprovalRevocationFlag.Permit2Lockdown | | ||
| ApprovalRevocationFlag.Permit2Approve | | ||
| ApprovalRevocationFlag.Erc721SetApprovalForAll | | ||
| ApprovalRevocationFlag.Erc721Approve | | ||
| ApprovalRevocationFlag.Erc20Approve; | ||
|
|
||
| /** | ||
| * Builds the configuration for the token-approval-revocation permission decoder. | ||
| * | ||
| * @param contractAddresses - Checksummed enforcer addresses for the chain. | ||
| * @returns The token-approval-revocation permission decoder configuration. | ||
| */ | ||
| export function makeTokenApprovalRevocationDecoderConfig( | ||
| contractAddresses: ChecksumEnforcersByChainId, | ||
| ): MakePermissionDecoderConfig { | ||
| const { timestampEnforcer, approvalRevocationEnforcer, nonceEnforcer } = | ||
| contractAddresses; | ||
|
|
||
| return { | ||
| permissionType: 'token-approval-revocation', | ||
| contractAddresses, | ||
| optionalEnforcers: [ | ||
| timestampEnforcer, // expiry rule | ||
| ], | ||
| requiredEnforcers: { | ||
| [approvalRevocationEnforcer]: 1, | ||
| [nonceEnforcer]: 1, | ||
| }, | ||
| rules: [expiryRule], | ||
| validateAndDecodeData, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Decodes token-approval-revocation permission data from caveats; throws on invalid. | ||
| * | ||
| * @param caveats - Caveats from the permission context (checksummed). | ||
| * @param contractAddresses - Checksummed enforcer addresses for the chain. | ||
| * @returns Decoded approval-revocation capability flags. | ||
| */ | ||
| function validateAndDecodeData( | ||
| caveats: ChecksumCaveat[], | ||
| contractAddresses: ChecksumEnforcersByChainId, | ||
| ): DecodedPermission['permission']['data'] { | ||
| const { approvalRevocationEnforcer } = contractAddresses; | ||
|
|
||
| const terms = getTermsByEnforcer({ | ||
| caveats, | ||
| enforcer: approvalRevocationEnforcer, | ||
| }); | ||
|
|
||
| const mask = hexToNumber(terms); | ||
|
|
||
| if (mask > MAX_APPROVAL_REVOCATION_MASK) { | ||
| throw new Error( | ||
| `Invalid ApprovalRevocation terms: must be less than or equal to ${MAX_APPROVAL_REVOCATION_MASK}`, | ||
| ); | ||
| } | ||
|
|
||
| if (mask === 0) { | ||
| throw new Error('Invalid ApprovalRevocation terms: must be greater than 0'); | ||
| } | ||
|
|
||
| return { | ||
| erc20Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Erc20Approve), | ||
| erc721Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Erc721Approve), | ||
| erc721SetApprovalForAll: isFlagEnabled( | ||
| mask, | ||
| ApprovalRevocationFlag.Erc721SetApprovalForAll, | ||
| ), | ||
| permit2Approve: isFlagEnabled(mask, ApprovalRevocationFlag.Permit2Approve), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question here, the name of |
||
| permit2Lockdown: isFlagEnabled( | ||
| mask, | ||
| ApprovalRevocationFlag.Permit2Lockdown, | ||
| ), | ||
| permit2InvalidateNonces: isFlagEnabled( | ||
| mask, | ||
| ApprovalRevocationFlag.Permit2InvalidateNonces, | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| function isFlagEnabled(mask: number, flag: number): boolean { | ||
| return (mask & flag) === flag; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.