forked from hashgraph/hedera-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-account.js
57 lines (43 loc) · 1.6 KB
/
delete-account.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
import {
Wallet,
LocalProvider,
PrivateKey,
AccountCreateTransaction,
AccountDeleteTransaction,
Hbar,
} from "@hashgraph/sdk";
import dotenv from "dotenv";
dotenv.config();
async function main() {
if (process.env.OPERATOR_ID == null || process.env.OPERATOR_KEY == null) {
throw new Error(
"Environment variables OPERATOR_ID, and OPERATOR_KEY are required."
);
}
const wallet = new Wallet(
process.env.OPERATOR_ID,
process.env.OPERATOR_KEY,
new LocalProvider()
);
const newKey = PrivateKey.generate();
console.log(`private key = ${newKey.toString()}`);
console.log(`public key = ${newKey.publicKey.toString()}`);
let transaction = await new AccountCreateTransaction()
.setInitialBalance(new Hbar(10)) // 10 h
.setKey(newKey.publicKey)
.freezeWithSigner(wallet);
transaction = await transaction.signWithSigner(wallet);
const response = await transaction.executeWithSigner(wallet);
const receipt = await response.getReceiptWithSigner(wallet);
console.log(`created account id = ${receipt.accountId.toString()}`);
transaction = await new AccountDeleteTransaction()
.setNodeAccountIds([response.nodeId])
.setAccountId(receipt.accountId)
.setTransferAccountId(wallet.getAccountId())
.freezeWithSigner(wallet);
transaction = await transaction.signWithSigner(wallet);
newKey.signTransaction(transaction);
await transaction.executeWithSigner(wallet);
console.log(`deleted account id = ${receipt.accountId.toString()}`);
}
void main();