/
decodePermissions.ts
118 lines (104 loc) · 4.67 KB
/
decodePermissions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// import { PERMISSIONS } from '@lukso/lsp-smart-contracts';
import { BytesLike, ZeroHash, isHexString, stripZerosLeft, toBeHex } from 'ethers';
import { PERMISSIONS } from '@lukso/lsp-smart-contracts';
import { LSP6PermissionName } from '../../types';
/**
* Decode a hex value, containing a `BitArray` of permissions. The `AddressPermissions:Permissions:<address>` can be decoded using this function.
*
* @since v0.0.2
* @category LSP6
* @param permissions A hex value, containing a BitArray of permissions.
* @param decodedPermissionsType Optional param, defaults to `LSP6PermissionName`.
* Can be used to specfiy the type of the return array. Options:
* - `BytesLike`
* - `bigint`
* - `boolean`
* - `LSP6PermissionName`
*
* @return An array of decoded permissions.
*
* @throws
*
* @see https://github.com/lukso-network/LIPs/blob/main/LSPs/LSP-6-KeyManager.md
* @example
* ```ts
* decodePermissions([
* `0x0000000000000000000000000000000000000000000000000000000000040803`
* ]) => new Set([ CHANGEOWNER, ADDCONTROLLER, CALL, SETDATA ])
* decodePermissions([
* `0x0000000000000000000000000000000000000000000000000000000000040803`,
* 'LSP6PermissionName'
* ]) => new Set([ CHANGEOWNER, ADDCONTROLLER, CALL, SETDATA ])
* decodePermissions([
* `0x0000000000000000000000000000000000000000000000000000000000040803`,
* 'BytesLike'
* ]) =>
* new Set([
* '0x0000000000000000000000000000000000000000000000000000000000000001',
* '0x0000000000000000000000000000000000000000000000000000000000000002',
* '0x0000000000000000000000000000000000000000000000000000000000000800',
* '0x0000000000000000000000000000000000000000000000000000000000040000',
* ])
* decodePermissions([
* `0x0000000000000000000000000000000000000000000000000000000000040803`,
* 'bigint'
* ]) => new Set([ 1n, 2n, 2048n, 262144n ])
* decodePermissions([
* `0x0000000000000000000000000000000000000000000000000000000000000003`,
* 'boolean'
* ]) => [ true, true, false, false, false, false, false, false ]
* ```
*/
export function decodePermissions(
permissions: string | BytesLike,
decodedPermissionsType?: 'BytesLike' | 'bigint' | 'boolean' | 'LSP6PermissionName',
): Set<BytesLike | bigint | LSP6PermissionName> | boolean[] {
if (permissions === ZeroHash || permissions === '') {
throw new Error('There are no permissions to decode');
}
if (!isHexString(permissions)) {
throw new Error(`Permissions is not hex. permissions: '${permissions}'`);
}
decodedPermissionsType = decodedPermissionsType ? decodedPermissionsType : 'LSP6PermissionName';
const zeroStrippedPermissions = stripZerosLeft(permissions);
const bigIntPermissions = BigInt(zeroStrippedPermissions);
if (decodedPermissionsType === 'boolean') {
const decodedPermissions: boolean[] = [];
for (let index = 0; index < zeroStrippedPermissions.length * 4; index++) {
const bigIntPermission = (BigInt(1) << BigInt(index)) & bigIntPermissions;
const booleanPermissions = bigIntPermission !== BigInt(0);
decodedPermissions[index] = booleanPermissions;
}
return decodedPermissions;
} else {
const decodedPermissionsSet: Set<BytesLike | bigint | LSP6PermissionName> = new Set([]);
for (let index = 0; index < zeroStrippedPermissions.length * 4; index++) {
const bigIntPermission = (BigInt(1) << BigInt(index)) & bigIntPermissions;
if (decodedPermissionsType === 'BytesLike') {
const bytesLikePermissions = toBeHex(bigIntPermission, 32);
if (bytesLikePermissions !== ZeroHash) {
decodedPermissionsSet.add(bytesLikePermissions);
}
} else if (decodedPermissionsType === 'bigint') {
if (bigIntPermission !== BigInt(0)) {
decodedPermissionsSet.add(bigIntPermission);
}
} else if (decodedPermissionsType === 'LSP6PermissionName') {
const bytesLikePermissions = toBeHex(bigIntPermission, 32);
if (bytesLikePermissions !== ZeroHash) {
const permissionName = Object.getOwnPropertyNames(PERMISSIONS).filter(
(permission) => PERMISSIONS[permission] === bytesLikePermissions,
)[0];
if (permissionName) {
decodedPermissionsSet.add(permissionName);
} else {
throw new Error(
`LSP6 permission does not exist. Permission: ${bytesLikePermissions}`,
);
}
}
}
}
return decodedPermissionsSet;
}
}