-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathKeyHelper.sol
166 lines (147 loc) · 5.53 KB
/
KeyHelper.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.5.0 <0.9.0;
pragma experimental ABIEncoderV2;
import './HederaTokenService.sol';
abstract contract KeyHelper is HederaTokenService {
using Bits for uint256;
address supplyContract;
mapping(KeyType => uint256) keyTypes;
enum KeyType {
ADMIN,
KYC,
FREEZE,
WIPE,
SUPPLY,
FEE,
PAUSE
}
enum KeyValueType {
INHERIT_ACCOUNT_KEY,
CONTRACT_ID,
ED25519,
SECP256K1,
DELEGETABLE_CONTRACT_ID
}
constructor() {
keyTypes[KeyType.ADMIN] = 1;
keyTypes[KeyType.KYC] = 2;
keyTypes[KeyType.FREEZE] = 4;
keyTypes[KeyType.WIPE] = 8;
keyTypes[KeyType.SUPPLY] = 16;
keyTypes[KeyType.FEE] = 32;
keyTypes[KeyType.PAUSE] = 64;
}
function getDefaultKeys() internal view returns (IHederaTokenService.TokenKey[] memory keys) {
keys = new IHederaTokenService.TokenKey[](2);
keys[0] = getSingleKey(KeyType.KYC, KeyValueType.CONTRACT_ID, '');
keys[1] = IHederaTokenService.TokenKey(
getDuplexKeyType(KeyType.SUPPLY, KeyType.PAUSE),
getKeyValueType(KeyValueType.CONTRACT_ID, '')
);
}
function getAllTypeKeys(KeyValueType keyValueType, bytes memory key)
internal
view
returns (IHederaTokenService.TokenKey[] memory keys)
{
keys = new IHederaTokenService.TokenKey[](1);
keys[0] = IHederaTokenService.TokenKey(getAllKeyTypes(), getKeyValueType(keyValueType, key));
}
function getCustomSingleTypeKeys(
KeyType keyType,
KeyValueType keyValueType,
bytes memory key
) internal view returns (IHederaTokenService.TokenKey[] memory keys) {
keys = new IHederaTokenService.TokenKey[](1);
keys[0] = IHederaTokenService.TokenKey(getKeyType(keyType), getKeyValueType(keyValueType, key));
}
function getCustomDuplexTypeKeys(
KeyType firstType,
KeyType secondType,
KeyValueType keyValueType,
bytes memory key
) internal view returns (IHederaTokenService.TokenKey[] memory keys) {
keys = new IHederaTokenService.TokenKey[](1);
keys[0] = IHederaTokenService.TokenKey(
getDuplexKeyType(firstType, secondType),
getKeyValueType(keyValueType, key)
);
}
function getSingleKey(
KeyType keyType,
KeyValueType keyValueType,
bytes memory key
) internal view returns (IHederaTokenService.TokenKey memory tokenKey) {
tokenKey = IHederaTokenService.TokenKey(getKeyType(keyType), getKeyValueType(keyValueType, key));
}
function getSingleKey(
KeyType keyType,
KeyValueType keyValueType,
address key
) internal view returns (IHederaTokenService.TokenKey memory tokenKey) {
tokenKey = IHederaTokenService.TokenKey(getKeyType(keyType), getKeyValueType(keyValueType, key));
}
function getSingleKey(
KeyType firstType,
KeyType secondType,
KeyValueType keyValueType,
bytes memory key
) internal view returns (IHederaTokenService.TokenKey memory tokenKey) {
tokenKey = IHederaTokenService.TokenKey(
getDuplexKeyType(firstType, secondType),
getKeyValueType(keyValueType, key)
);
}
function getDuplexKeyType(KeyType firstType, KeyType secondType) internal pure returns (uint256 keyType) {
keyType = keyType.setBit(uint8(firstType));
keyType = keyType.setBit(uint8(secondType));
}
function getAllKeyTypes() internal pure returns (uint256 keyType) {
keyType = keyType.setBit(uint8(KeyType.ADMIN));
keyType = keyType.setBit(uint8(KeyType.KYC));
keyType = keyType.setBit(uint8(KeyType.FREEZE));
keyType = keyType.setBit(uint8(KeyType.WIPE));
keyType = keyType.setBit(uint8(KeyType.SUPPLY));
keyType = keyType.setBit(uint8(KeyType.FEE));
keyType = keyType.setBit(uint8(KeyType.PAUSE));
}
function getKeyType(KeyType keyType) internal view returns (uint256) {
return keyTypes[keyType];
}
function getKeyValueType(KeyValueType keyValueType, bytes memory key)
internal
view
returns (IHederaTokenService.KeyValue memory keyValue)
{
if (keyValueType == KeyValueType.INHERIT_ACCOUNT_KEY) {
keyValue.inheritAccountKey = true;
} else if (keyValueType == KeyValueType.CONTRACT_ID) {
keyValue.contractId = supplyContract;
} else if (keyValueType == KeyValueType.ED25519) {
keyValue.ed25519 = key;
} else if (keyValueType == KeyValueType.SECP256K1) {
keyValue.ECDSA_secp256k1 = key;
} else if (keyValueType == KeyValueType.DELEGETABLE_CONTRACT_ID) {
keyValue.delegatableContractId = supplyContract;
}
}
function getKeyValueType(KeyValueType keyValueType, address keyAddress)
internal
pure
returns (IHederaTokenService.KeyValue memory keyValue)
{
if (keyValueType == KeyValueType.CONTRACT_ID) {
keyValue.contractId = keyAddress;
} else if (keyValueType == KeyValueType.DELEGETABLE_CONTRACT_ID) {
keyValue.delegatableContractId = keyAddress;
}
}
}
library Bits {
uint256 internal constant ONE = uint256(1);
// Sets the bit at the given 'index' in 'self' to '1'.
// Returns the modified value.
function setBit(uint256 self, uint8 index) internal pure returns (uint256) {
return self | (ONE << index);
}
}