forked from hashgraph/hedera-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-update-delete-node.js
95 lines (85 loc) · 3.31 KB
/
create-update-delete-node.js
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
import {
Client,
PrivateKey,
AccountId,
NodeCreateTransaction,
NodeUpdateTransaction,
NodeDeleteTransaction,
ServiceEndpoint,
} from "@hashgraph/sdk";
import dotenv from "dotenv";
dotenv.config();
async function main() {
if (
process.env.OPERATOR_ID == null ||
process.env.OPERATOR_KEY == null ||
process.env.HEDERA_NETWORK == null
) {
throw new Error(
"Environment variables OPERATOR_ID, HEDERA_NETWORK, and OPERATOR_KEY are required.",
);
}
const network = process.env.HEDERA_NETWORK;
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringDer(process.env.OPERATOR_KEY);
const client = Client.forName(network).setOperator(operatorId, operatorKey);
// Transaction parameters
const accountId = AccountId.fromString("0.0.999");
const description = "This is a description of the node.";
const newDescription = "This is new a description of the node.";
const ipAddressV4 = Uint8Array.of(127, 0, 0, 1);
const port = 50211;
const gossipEndpoint = new ServiceEndpoint()
.setIpAddressV4(ipAddressV4)
.setPort(port);
const gossipEndpoints = [gossipEndpoint];
const serviceEndpoint = new ServiceEndpoint()
.setIpAddressV4(ipAddressV4)
.setPort(port);
const serviceEndpoints = [serviceEndpoint];
const gossipCaCertificate = new Uint8Array();
const certificateHash = new Uint8Array();
const adminKey = PrivateKey.generate();
// 1. Create a new node
console.log("Creating a new node...");
const createTransaction = new NodeCreateTransaction()
.setAccountId(accountId)
.setDescription(description)
.setGossipEndpoints(gossipEndpoints)
.setServiceEndpoints(serviceEndpoints)
.setGossipCaCertificate(gossipCaCertificate)
.setCertificateHash(certificateHash)
.setAdminKey(adminKey);
const createTransactionResponse = await createTransaction.execute(client);
const createTransactionReceipt =
await createTransactionResponse.getReceipt(client);
const nodeId = createTransactionReceipt.nodeId;
console.log(
`Node create transaction status: ${createTransactionReceipt.status.toString()}`,
);
console.log(
`Node has been created successfully with node id: ${nodeId.toString()}`,
);
// 2. Update the node
console.log("Updating the node...");
const updateTransaction = new NodeUpdateTransaction()
.setNodeId(nodeId)
.setDescription(newDescription);
const updateTrasnactionResponse = await updateTransaction.execute(client);
const updateTrasnactionReceipt =
await updateTrasnactionResponse.getReceipt(client);
console.log(
`Node update transaction status: ${updateTrasnactionReceipt.status.toString()}`,
);
// 3. Delete the node
console.log("Deleting the node...");
const deleteTransaction = new NodeDeleteTransaction().setNodeId(nodeId);
const deleteTransactionResponse = await deleteTransaction.execute(client);
const deleteTransactionReceipt =
await deleteTransactionResponse.getReceipt(client);
console.log(
`Node delete transaction status: ${deleteTransactionReceipt.status.toString()}`,
);
client.close();
}
void main();