forked from hiero-ledger/hiero-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchange-or-remove-token-keys.js
More file actions
205 lines (179 loc) · 8.33 KB
/
change-or-remove-token-keys.js
File metadata and controls
205 lines (179 loc) · 8.33 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import {
AccountId,
Client,
PrivateKey,
Logger,
LogLevel,
PublicKey,
KeyList,
TokenUpdateTransaction,
TokenKeyValidation,
TokenCreateTransaction,
TokenType,
TokenInfoQuery,
} from "@hashgraph/sdk";
import dotenv from "dotenv";
/**
* @description Change ot remove token keys
*/
async function main() {
// Ensure required environment variables are available
dotenv.config();
if (
!process.env.OPERATOR_KEY ||
!process.env.OPERATOR_ID ||
!process.env.HEDERA_NETWORK
) {
throw new Error("Please set required keys in .env file.");
}
const network = process.env.HEDERA_NETWORK;
// Configure client using environment variables
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringECDSA(process.env.OPERATOR_KEY);
const client = Client.forName(network).setOperator(operatorId, operatorKey);
// Set logger
const infoLogger = new Logger(LogLevel.Info);
client.setLogger(infoLogger);
const adminKey = PrivateKey.generateECDSA();
const supplyKey = PrivateKey.generateECDSA();
const newSupplyKey = PrivateKey.generateECDSA();
const wipeKey = PrivateKey.generateECDSA();
const freezeKey = PrivateKey.generateECDSA();
const pauseKey = PrivateKey.generateECDSA();
const feeScheduleKey = PrivateKey.generateECDSA();
const metadataKey = PrivateKey.generateECDSA();
// This HIP introduces ability to remove lower-privilege keys (Wipe, KYC, Freeze, Pause, Supply, Fee Schedule, Metadata) from a Token:
// - using an update with the empty KeyList;
const emptyKeyList = KeyList.of();
// - updating with an “invalid” key such as an Ed25519 0x0000000000000000000000000000000000000000000000000000000000000000 public key,
// since it is (presumably) impossible to find the 32-byte string whose SHA-512 hash begins with 32 bytes of zeros.
const unusableKey = PublicKey.unusableKey();
console.log("=====================================================");
console.log("Initializing token keys...");
console.log("-----------------------------------------------------");
console.log("Admin key:", adminKey.publicKey.toString());
console.log("Supply key:", supplyKey.publicKey.toString());
console.log("New supply key:", newSupplyKey.publicKey.toString());
console.log("Wipe key:", wipeKey.publicKey.toString());
console.log("Freeze key:", freezeKey.publicKey.toString());
console.log("Pause key:", pauseKey.publicKey.toString());
console.log("Fee schedule key:", feeScheduleKey.publicKey.toString());
console.log("Metadata key:", metadataKey.publicKey.toString());
console.log("Unusable key:", unusableKey.toString());
console.log("=====================================================");
console.log("\n");
let token, tokenInfo, response, receipt, update;
console.log("=====================================================");
console.log("Creating token...");
console.log("-----------------------------------------------------");
token = new TokenCreateTransaction()
.setTokenName("Token")
.setTokenSymbol("T")
.setTokenType(TokenType.NonFungibleUnique)
.setTreasuryAccountId(operatorId)
.setAdminKey(adminKey)
.setWipeKey(wipeKey)
.setFreezeKey(freezeKey)
.setPauseKey(pauseKey)
.setSupplyKey(supplyKey)
.setFeeScheduleKey(feeScheduleKey)
.setMetadataKey(metadataKey)
.freezeWith(client);
response = await (await token.sign(adminKey)).execute(client);
receipt = await response.getReceipt(client);
console.log("Token create transction status:", receipt.status.toString());
const tokenId = receipt.tokenId;
console.log("Token id:", tokenId.toString());
console.log("=====================================================");
console.log("\n");
console.log("=====================================================");
console.log("Token keys:");
console.log("-----------------------------------------------------");
tokenInfo = await new TokenInfoQuery().setTokenId(tokenId).execute(client);
console.log("Token admin key:", tokenInfo.adminKey.toString());
console.log("Token pause key:", tokenInfo.pauseKey.toString());
console.log("Token freeze key:", tokenInfo.freezeKey.toString());
console.log("Token wipe key:", tokenInfo.wipeKey.toString());
console.log("Token supply key:", tokenInfo.supplyKey.toString());
console.log("Token fee schedule key:", tokenInfo.feeScheduleKey.toString());
console.log("Token metadata key:", tokenInfo.metadataKey.toString());
console.log("=====================================================");
console.log("\n");
console.log("=====================================================");
console.log("Removing Wipe Key...");
console.log("-----------------------------------------------------");
update = new TokenUpdateTransaction()
.setTokenId(tokenId)
.setWipeKey(emptyKeyList)
.setKeyVerificationMode(TokenKeyValidation.FullValidation)
.freezeWith(client);
response = await (await update.sign(adminKey)).execute(client);
receipt = await response.getReceipt(client);
console.log("Token update transaction status:", receipt.status.toString());
tokenInfo = await new TokenInfoQuery().setTokenId(tokenId).execute(client);
console.log("Token wipeKey is", tokenInfo.wipeKey);
console.log("=====================================================");
console.log("\n");
console.log("=====================================================");
console.log("Removing Admin Key...");
console.log("-----------------------------------------------------");
update = new TokenUpdateTransaction()
.setTokenId(tokenId)
.setAdminKey(emptyKeyList)
.setKeyVerificationMode(TokenKeyValidation.NoValidation)
.freezeWith(client);
response = await (await update.sign(adminKey)).execute(client);
receipt = await response.getReceipt(client);
console.log("Token update transaction status:", receipt.status.toString());
tokenInfo = await new TokenInfoQuery().setTokenId(tokenId).execute(client);
console.log("Token adminKey is", tokenInfo.adminKey);
console.log("=====================================================");
console.log("\n");
console.log("=====================================================");
console.log("Updating Supply Key...");
console.log("-----------------------------------------------------");
console.log(
"Token supplyKey (before update) is",
tokenInfo.supplyKey.toString(),
);
update = new TokenUpdateTransaction()
.setTokenId(tokenId)
.setSupplyKey(newSupplyKey)
.setKeyVerificationMode(TokenKeyValidation.FullValidation)
.freezeWith(client);
response = await (
await (await update.sign(supplyKey)).sign(newSupplyKey)
).execute(client);
receipt = await response.getReceipt(client);
console.log("Token update transaction status:", receipt.status.toString());
tokenInfo = await new TokenInfoQuery().setTokenId(tokenId).execute(client);
console.log(
"Token suppleyKey (after update) is",
tokenInfo.supplyKey.toString(),
);
console.log("=====================================================");
console.log("\n");
console.log("=====================================================");
console.log("Updating Supply Key to unusable format...");
console.log("-----------------------------------------------------");
console.log(
"Token supplyKey (before update) is",
tokenInfo.supplyKey.toString(),
);
update = new TokenUpdateTransaction()
.setTokenId(tokenId)
.setSupplyKey(unusableKey)
.setKeyVerificationMode(TokenKeyValidation.NoValidation)
.freezeWith(client);
response = await (await update.sign(newSupplyKey)).execute(client);
receipt = await response.getReceipt(client);
console.log("Token update transaction status:", receipt.status.toString());
tokenInfo = await new TokenInfoQuery().setTokenId(tokenId).execute(client);
console.log(
"Token suppleyKey (after update) is",
tokenInfo.supplyKey.toString(),
);
console.log("=====================================================");
client.close();
}
void main();