-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken-admin-keylist.js
More file actions
121 lines (101 loc) · 4.38 KB
/
token-admin-keylist.js
File metadata and controls
121 lines (101 loc) · 4.38 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
console.clear();
require("dotenv").config();
const {
AccountId,
PrivateKey,
Client,
TokenCreateTransaction,
TokenUpdateTransaction,
TokenType,
Hbar,
TokenSupplyType,
AccountCreateTransaction,
KeyList
} = require("@hashgraph/sdk");
// Configure accounts and client, and generate needed keys
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PVKEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
client.setDefaultMaxTransactionFee(new Hbar(100));
const adminKey = PrivateKey.generateED25519();
const randomKey = PrivateKey.generateED25519();
const treasuryKey = PrivateKey.generateED25519();
const key1 = PrivateKey.generateED25519();
const key2 = PrivateKey.generateED25519();
const newSupplyKey = PrivateKey.generateED25519();
async function main() {
// Create accounts
console.log(`- Creating accounts...`);
const [adminAccStatus, adminId] = await accountCreatorFcn(adminKey, 3);
console.log(`- Created admin account ${adminId} that has a balance of 3ℏ`);
const [key1AccStatus, key1Id] = await accountCreatorFcn(key1, 3);
console.log(`- Created account 1: ${key1Id} that has a balance of 3ℏ`);
const [key2AccStatus, key2Id] = await accountCreatorFcn(key2, 3);
console.log(`- Created account 2: ${key2Id} that has a balance of 3ℏ`);
const [randomAccStatus, randomId] = await accountCreatorFcn(randomKey, 3);
console.log(`- Created random account ${randomId} that has a balance of 3ℏ`);
const [treasuryAccStatus, treasuryId] = await accountCreatorFcn(treasuryKey, 3);
console.log(`- Created treasury account ${treasuryId} that has a balance of 3ℏ`);
// Setting node IDs
// It is required to set the account ID of the node(s) the transaction will be submitted to when freezing a transaction for signatures.
const nodeId = [];
nodeId.push(new AccountId(3));
// Create keylist
console.log(`- Generating keylist...`);
const keyList = new KeyList([key1.publicKey, key2.publicKey], 2);
// Create NFT
console.log(`\n- Creating NFT (with all token keys set)`);
let nftCreate = await new TokenCreateTransaction()
.setNodeAccountIds(nodeId)
.setTokenName("Fall Collection")
.setTokenSymbol("LEAF")
.setTokenType(TokenType.NonFungibleUnique)
.setDecimals(0)
.setInitialSupply(0)
.setTreasuryAccountId(treasuryId) // needs to sign
.setSupplyType(TokenSupplyType.Finite)
.setMaxSupply(5)
// Set keys
.setAdminKey(keyList)
.setFreezeKey(randomKey)
.setKycKey(randomKey)
.setWipeKey(randomKey)
.setSupplyKey(randomKey)
.setPauseKey(randomKey)
.setFeeScheduleKey(randomKey)
.freezeWith(client)
.sign(treasuryKey);
// Adding multisig signatures
const sig1 = key1.signTransaction(nftCreate);
const sig2 = key2.signTransaction(nftCreate);
const nftCreateTxSign = nftCreate.addSignature(key1.publicKey, sig1).addSignature(key2.publicKey, sig2);
let nftCreateSubmit = await nftCreateTxSign.execute(client);
let nftCreateRx = await nftCreateSubmit.getReceipt(client);
let tokenId = nftCreateRx.tokenId;
console.log(`- Created NFT with Token ID: ${tokenId}`);
// Update supply key from token using token update transaction (not possible to remove by setting to "null")
console.log('\n- Updating token with new supply key');
let tokenUpdateTx = await new TokenUpdateTransaction()
.setNodeAccountIds(nodeId)
.setTokenId(tokenId)
.setSupplyKey(newSupplyKey) // if you set this to null, nothing happens
.freezeWith(client);
// Only admin key (multisig) needs to sign
const signature1 = key1.signTransaction(tokenUpdateTx);
const signature2 = key2.signTransaction(tokenUpdateTx);
const tokenUpdateTxSign = tokenUpdateTx.addSignature(key1.publicKey, signature1).addSignature(key2.publicKey, signature2)
let tokenUpdateTxSubmit = await tokenUpdateTxSign.execute(client);
let tokenUpdateTxRx = await tokenUpdateTxSubmit.getReceipt(client);
console.log('- Updated supply key');
client.close();
// ACCOUNT CREATOR FUNCTION ==========================================
async function accountCreatorFcn(pvKey, iBal) {
const response = await new AccountCreateTransaction()
.setInitialBalance(new Hbar(iBal))
.setKey(pvKey.publicKey)
.execute(client);
const receipt = await response.getReceipt(client);
return [receipt.status, receipt.accountId];
}
}
main();