From 5e300f0bb437b80a44e1e569045aa36043c9a350 Mon Sep 17 00:00:00 2001 From: CJ42 Date: Mon, 13 May 2024 17:55:15 +0100 Subject: [PATCH] refactor: move all permissions functionalities under `permissions.ts` --- src/index.ts | 17 +++++++----- src/lib/detector.ts | 60 ------------------------------------------ src/lib/permissions.ts | 59 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 67 deletions(-) diff --git a/src/index.ts b/src/index.ts index cd2ce112..ca62e8c6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -67,13 +67,14 @@ import { encodeValueContent, decodeValueContent, } from './lib/encoder'; +import { internalSupportsInterface } from './lib/detector'; +import { decodeMappingKey } from './lib/decodeMappingKey'; import { - internalSupportsInterface, + encodePermissions, + decodePermissions, checkPermissions, mapPermission, -} from './lib/detector'; -import { decodeMappingKey } from './lib/decodeMappingKey'; -import { encodePermissions, decodePermissions } from './lib/permissions'; +} from './lib/permissions'; import { AssetURLEncode } from './types/encodeData'; import { URLDataToEncode, URLDataWithHash, Verification } from './types'; @@ -99,8 +100,12 @@ export { decodeValueContent, } from './lib/encoder'; export { getDataFromExternalSources } from './lib/getDataFromExternalSources'; -export { encodePermissions, decodePermissions } from './lib/permissions'; -export { checkPermissions } from './lib/detector'; +export { + encodePermissions, + decodePermissions, + checkPermissions, + mapPermission, +} from './lib/permissions'; export { getSchema } from './lib/schemaParser'; // PRIVATE FUNCTION diff --git a/src/lib/detector.ts b/src/lib/detector.ts index 35df9a67..e7a6e1b9 100644 --- a/src/lib/detector.ts +++ b/src/lib/detector.ts @@ -21,9 +21,6 @@ * @date 2022 */ -import { isHexStrict } from 'web3-utils'; -import { LSP6_DEFAULT_PERMISSIONS } from '../constants/constants'; - import { AddressProviderOptions, INTERFACE_IDS_0_12_0, @@ -57,60 +54,3 @@ export const internalSupportsInterface = async ( throw new Error(`Error checking the interface: ${error}`); } }; - -/** - * @notice Map a permission to its corresponding bytes32 representation. - * @param permission The permission string to be mapped. - * @return The bytes32 representation of the permission. - * @dev Return null if the input is not a known permission name or a valid 32-byte hex string. - */ -export function mapPermission(permission: string): string | null { - if (!LSP6_DEFAULT_PERMISSIONS[permission] && !isHexStrict(permission)) { - return null; - } - return LSP6_DEFAULT_PERMISSIONS[permission] || permission; -} - -/** - * @notice Check if the required permissions are included in the granted permissions. - * @param requiredPermissions An array of required permissions or a single required permission. - * @param grantedPermissions The granted permissions as a 32-byte hex string. - * @return A boolean value indicating whether the required permissions are included in the granted permissions. - * @dev Throws an error if the grantedPermissions input is not a valid 32-byte hex string. - */ -export const checkPermissions = ( - requiredPermissions: string[] | string, - grantedPermissions: string, -): boolean => { - // Validate the grantedPermissions string - if (!isHexStrict(grantedPermissions)) { - throw new Error( - 'Invalid grantedPermissions string. It must be a valid 32-byte hex string.', - ); - } - - // Convert requiredPermissions to an array if it's a single string - const requiredPermissionArray: string[] = Array.isArray(requiredPermissions) - ? requiredPermissions - : [requiredPermissions]; - - // Map the literal permissions to their bytes32 representation - const mappedPermissionArray: (string | null)[] = - requiredPermissionArray.map(mapPermission); - - // Perform the AND operation check for each required permission - return mappedPermissionArray.every((requiredPermission: string | null) => { - if (!requiredPermission) { - throw new Error( - `Invalid permission string: ${requiredPermission}. It must be a valid 32-byte hex string or a known permission name.`, - ); - } - const requiredPermissionBigInt = BigInt(requiredPermission); - const grantedPermissionsBigInt = BigInt(grantedPermissions); - - return ( - (requiredPermissionBigInt & grantedPermissionsBigInt) === - requiredPermissionBigInt - ); - }); -}; diff --git a/src/lib/permissions.ts b/src/lib/permissions.ts index b35fb55e..2d680119 100644 --- a/src/lib/permissions.ts +++ b/src/lib/permissions.ts @@ -1,4 +1,4 @@ -import { hexToNumber, leftPad, numberToHex } from 'web3-utils'; +import { hexToNumber, isHexStrict, leftPad, numberToHex } from 'web3-utils'; import { LSP6_DEFAULT_PERMISSIONS } from '../constants/constants'; import { Permissions } from '../types/Method'; @@ -80,3 +80,60 @@ export function decodePermissions(permissionHex: string) { return result; } + +/** + * @notice Map a permission to its corresponding bytes32 representation. + * @param permission The permission string to be mapped. + * @return The bytes32 representation of the permission. + * @dev Return null if the input is not a known permission name or a valid 32-byte hex string. + */ +export function mapPermission(permission: string): string | null { + if (!LSP6_DEFAULT_PERMISSIONS[permission] && !isHexStrict(permission)) { + return null; + } + return LSP6_DEFAULT_PERMISSIONS[permission] || permission; +} + +/** + * @notice Check if the required permissions are included in the granted permissions. + * @param requiredPermissions An array of required permissions or a single required permission. + * @param grantedPermissions The granted permissions as a 32-byte hex string. + * @return A boolean value indicating whether the required permissions are included in the granted permissions. + * @dev Throws an error if the grantedPermissions input is not a valid 32-byte hex string. + */ +export const checkPermissions = ( + requiredPermissions: string[] | string, + grantedPermissions: string, +): boolean => { + // Validate the grantedPermissions string + if (!isHexStrict(grantedPermissions)) { + throw new Error( + 'Invalid grantedPermissions string. It must be a valid 32-byte hex string.', + ); + } + + // Convert requiredPermissions to an array if it's a single string + const requiredPermissionArray: string[] = Array.isArray(requiredPermissions) + ? requiredPermissions + : [requiredPermissions]; + + // Map the literal permissions to their bytes32 representation + const mappedPermissionArray: (string | null)[] = + requiredPermissionArray.map(mapPermission); + + // Perform the AND operation check for each required permission + return mappedPermissionArray.every((requiredPermission: string | null) => { + if (!requiredPermission) { + throw new Error( + `Invalid permission string: ${requiredPermission}. It must be a valid 32-byte hex string or a known permission name.`, + ); + } + const requiredPermissionBigInt = BigInt(requiredPermission); + const grantedPermissionsBigInt = BigInt(grantedPermissions); + + return ( + (requiredPermissionBigInt & grantedPermissionsBigInt) === + requiredPermissionBigInt + ); + }); +};