forked from hashgraph/hedera-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign-transaction.js
65 lines (49 loc) · 1.71 KB
/
sign-transaction.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
import {
Client,
PrivateKey,
AccountCreateTransaction,
Hbar,
AccountId,
KeyList,
TransferTransaction,
} from "@hashgraph/sdk";
import * as dotenv from "dotenv";
dotenv.config();
let user1Key;
let user2Key;
async function main() {
let client;
try {
client = Client.forName(process.env.HEDERA_NETWORK).setOperator(
AccountId.fromString(process.env.OPERATOR_ID),
PrivateKey.fromString(process.env.OPERATOR_KEY)
);
} catch (error) {
throw new Error(
"Environment; variables HEDERA_NETWORK, OPERATOR_ID, and OPERATOR_KEY are required."
);
}
user1Key = PrivateKey.generate();
user2Key = PrivateKey.generate();
// create a multi-sig account
const keyList = new KeyList([user1Key, user2Key]);
const createAccountTransaction = new AccountCreateTransaction()
.setInitialBalance(new Hbar(2)) // 5 h
.setKey(keyList);
const response = await createAccountTransaction.execute(client);
let receipt = await response.getReceipt(client);
console.log(`account id = ${receipt.accountId.toString()}`);
// create a transfer from new account to 0.0.3
const transferTransaction = new TransferTransaction()
.setNodeAccountIds([new AccountId(3)])
.addHbarTransfer(receipt.accountId, -1)
.addHbarTransfer("0.0.3", 1)
.freezeWith(client);
await transferTransaction.signWithOperator(client);
user1Key.signTransaction(transferTransaction);
user2Key.signTransaction(transferTransaction);
const result = await transferTransaction.execute(client);
receipt = await result.getReceipt(client);
console.log(receipt.status.toString());
}
void main();