forked from hiero-ledger/hiero-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduled-transaction-multi-sig-threshold.js
131 lines (116 loc) · 4.2 KB
/
scheduled-transaction-multi-sig-threshold.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
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
require("dotenv").config();
const {
Client,
PrivateKey,
AccountId,
KeyList,
AccountCreateTransaction,
Hbar,
AccountBalanceQuery,
TransferTransaction,
ScheduleSignTransaction,
ScheduleInfoQuery,
TransactionRecordQuery,
} = require("@hashgraph/sdk");
async function main() {
// set up client
let client;
try {
client = Client.forName(process.env.HEDERA_NETWORK).setOperator(
AccountId.fromString(process.env.OPERATOR_ID),
PrivateKey.fromString(process.env.OPERATOR_KEY)
);
} catch {
throw new Error(
"Environment variables HEDERA_NETWORK, OPERATOR_ID, and OPERATOR_KEY are required."
);
}
// generate keys
const privateKeyList = [];
const publicKeyList = [];
for (let i = 0; i < 4; i++) {
const privateKey = PrivateKey.generate();
const publicKey = privateKey.publicKey;
privateKeyList.push(privateKey);
publicKeyList.push(publicKey);
console.log(`${i + 1}. public key: ${publicKey}`);
console.log(`${i + 1}. private key: ${privateKey}`);
}
const thresholdKey = new KeyList(publicKeyList, 3);
// create multi-sig account
const txAccountCreate = await new AccountCreateTransaction()
.setKey(thresholdKey)
.setInitialBalance(Hbar.fromTinybars(1))
.setAccountMemo("3-of-4 multi-sig account")
.execute(client);
const txAccountCreateReceipt = await txAccountCreate.getReceipt(client);
const multiSigAccountId = txAccountCreateReceipt.accountId;
console.log("3-of-4 multi-sig account ID: " + multiSigAccountId);
await queryBalance(multiSigAccountId, client);
// schedule crypto transfer from multi-sig account to operator account
const txSchedule = await (
await new TransferTransaction()
.addHbarTransfer(multiSigAccountId, Hbar.fromTinybars(-1))
.addHbarTransfer(client.operatorAccountId, Hbar.fromTinybars(1))
.schedule() // create schedule
.freezeWith(client)
.sign(privateKeyList[0])
) // add 1. signature
.execute(client);
const txScheduleReceipt = await txSchedule.getReceipt(client);
console.log("Schedule status: " + txScheduleReceipt.status.toString());
const scheduleId = txScheduleReceipt.scheduleId;
console.log("Schedule ID: " + scheduleId);
const scheduledTxId = txScheduleReceipt.scheduledTransactionId;
console.log("Scheduled tx ID: " + scheduledTxId);
// add 2. signature
const txScheduleSign1 = await (
await new ScheduleSignTransaction()
.setScheduleId(scheduleId)
.freezeWith(client)
.sign(privateKeyList[1])
).execute(client);
const txScheduleSign1Receipt = await txScheduleSign1.getReceipt(client);
console.log(
"1. ScheduleSignTransaction status: " +
txScheduleSign1Receipt.status.toString()
);
await queryBalance(multiSigAccountId, client);
// add 3. signature to trigger scheduled tx
const txScheduleSign2 = await (
await new ScheduleSignTransaction()
.setScheduleId(scheduleId)
.freezeWith(client)
.sign(privateKeyList[2])
).execute(client);
const txScheduleSign2Receipt = await txScheduleSign2.getReceipt(client);
console.log(
"2. ScheduleSignTransaction status: " +
txScheduleSign2Receipt.status.toString()
);
await queryBalance(multiSigAccountId, client);
// query schedule
const scheduleInfo = await new ScheduleInfoQuery()
.setScheduleId(scheduleId)
.execute(client);
console.log(scheduleInfo);
// query triggered scheduled tx
const recordScheduledTx = await new TransactionRecordQuery()
.setTransactionId(scheduledTxId)
.execute(client);
console.log(recordScheduledTx);
}
async function queryBalance(accoundId, client) {
const accountBalance = await new AccountBalanceQuery()
.setAccountId(accoundId)
.execute(client);
console.log(
"Balance of account " +
accoundId +
": " +
accountBalance.hbars.toTinybars() +
" tinybar"
);
return accountBalance;
}
main();