forked from LIT-Protocol/agent-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKPTools.sol
105 lines (89 loc) · 3.3 KB
/
PKPTools.sol
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract PKPTools {
address public immutable safeAddress;
struct Action {
bool permitted;
string description;
bool exists;
}
mapping(string => Action) public permittedActions;
string[] public actionCIDs;
event ActionPermissionSet(string indexed ipfsCid, bool permitted, string description);
event ActionRemoved(string indexed ipfsCid);
constructor(address _safeAddress) {
safeAddress = _safeAddress;
}
modifier onlySafe() {
require(msg.sender == safeAddress, "Only Safe can call this");
_;
}
function setPermittedAction(
string calldata ipfsCid,
bool permitted,
string calldata description
) external onlySafe {
if (!permittedActions[ipfsCid].exists) {
actionCIDs.push(ipfsCid);
}
permittedActions[ipfsCid] = Action(permitted, description, true);
emit ActionPermissionSet(ipfsCid, permitted, description);
}
function removePermittedAction(string calldata ipfsCid) external onlySafe {
require(permittedActions[ipfsCid].exists, "Action does not exist");
// Remove from actionCIDs array
for (uint256 i = 0; i < actionCIDs.length; i++) {
if (keccak256(bytes(actionCIDs[i])) == keccak256(bytes(ipfsCid))) {
// Move the last element to the position being deleted
actionCIDs[i] = actionCIDs[actionCIDs.length - 1];
// Remove the last element
actionCIDs.pop();
break;
}
}
// Remove from mapping
delete permittedActions[ipfsCid];
emit ActionRemoved(ipfsCid);
}
function removeAllPermittedActions() external onlySafe {
for (uint256 i = actionCIDs.length; i > 0; i--) {
string memory cid = actionCIDs[i - 1];
delete permittedActions[cid];
emit ActionRemoved(cid);
}
delete actionCIDs;
}
function isPermittedAction(string calldata ipfsCid) external view returns (bool) {
return permittedActions[ipfsCid].permitted;
}
function getAllPermittedActions() external view returns (
string[] memory cids,
bool[] memory permissions,
string[] memory descriptions
) {
// Count actually permitted actions
uint256 count = 0;
for (uint256 i = 0; i < actionCIDs.length; i++) {
if (permittedActions[actionCIDs[i]].permitted) {
count++;
}
}
// Create arrays of correct size
cids = new string[](count);
permissions = new bool[](count);
descriptions = new string[](count);
// Fill arrays only with permitted actions
uint256 index = 0;
for (uint256 i = 0; i < actionCIDs.length; i++) {
string memory cid = actionCIDs[i];
Action memory action = permittedActions[cid];
if (action.permitted) {
cids[index] = cid;
permissions[index] = true;
descriptions[index] = action.description;
index++;
}
}
return (cids, permissions, descriptions);
}
}