-
Notifications
You must be signed in to change notification settings - Fork 39
/
destroy.ts
83 lines (66 loc) · 2.44 KB
/
destroy.ts
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
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { Wallet, initLogger } from '@iota/sdk';
// This example uses secrets in environment variables for simplicity which should not be done in production.
require('dotenv').config({ path: '.env' });
// Run with command:
// yarn run-example ./how_tos/alias/destroy.ts
// In this example we destroy alias.
async function run() {
initLogger();
for (const envVar of [
'WALLET_DB_PATH',
'STRONGHOLD_PASSWORD',
'EXPLORER_URL',
]) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}
try {
// Create the wallet
const wallet = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
});
// Get the account we generated with `01-create-wallet`
const account = await wallet.getAccount('Alice');
// May want to ensure the account is synced before sending a transaction.
let balance = await account.sync();
if (balance.aliases.length == 0) {
throw new Error(`No Alias available in account 'Alice'`);
}
// We try to destroy the first alias in the account
const aliasId = balance.aliases[0];
console.log(
`Aliases BEFORE destroying (${balance.aliases.length}):\n`,
balance.aliases,
);
// To sign a transaction we need to unlock stronghold.
await wallet.setStrongholdPassword(
process.env.STRONGHOLD_PASSWORD as string,
);
console.log('Sending the destroy-alias transaction...');
// Destroy an alias
const transaction = await account
.prepareDestroyAlias(aliasId)
.then((prepared) => prepared.send());
console.log(`Transaction sent: ${transaction.transactionId}`);
// Wait for transaction to get included
const blockId = await account.retryTransactionUntilIncluded(
transaction.transactionId,
);
console.log(
`Block included: ${process.env.EXPLORER_URL}/block/${blockId}`,
);
console.log(`Destroyed alias ${aliasId}`);
balance = await account.sync();
console.log(
`Aliases AFTER destroying (${balance.aliases.length}):\n`,
balance.aliases,
);
} catch (error) {
console.log('Error: ', error);
}
process.exit(0);
}
run();