Skip to content

Commit

Permalink
refactor: move all permissions functionalities under permissions.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed May 17, 2024
1 parent 07e123a commit 18b8a93
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 67 deletions.
17 changes: 11 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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
Expand Down
60 changes: 0 additions & 60 deletions src/lib/detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
);
});
};
59 changes: 58 additions & 1 deletion src/lib/permissions.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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
);
});
};

0 comments on commit 18b8a93

Please sign in to comment.