-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathserialize-deserialize-11.js
69 lines (57 loc) · 2.03 KB
/
serialize-deserialize-11.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
import {
AccountId,
Wallet,
PrivateKey,
LocalProvider,
Transaction,
FileCreateTransaction,
LogLevel,
Logger,
} from "@hashgraph/sdk";
import dotenv from "dotenv";
/**
* @description Serialize and deserialize so-called incomplete transaction (chunked), set node account ids and execute it
*/
async function main() {
// Ensure required environment variables are available
dotenv.config();
if (
!process.env.OPERATOR_KEY ||
!process.env.OPERATOR_ID ||
!process.env.HEDERA_NETWORK
) {
throw new Error("Please set required keys in .env file.");
}
// Configure client using environment variables
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringED25519(process.env.OPERATOR_KEY);
const provider = new LocalProvider();
const infoLogger = new Logger(LogLevel.Info);
provider.setLogger(infoLogger);
const wallet = new Wallet(operatorId, operatorKey, provider);
try {
// 1. Create transaction
const transaction = new FileCreateTransaction()
.setKeys([wallet.getAccountKey()])
.setContents("[e2e::FileCreateTransaction]");
// 2. Serialize transaction into bytes
const transactionBytes = transaction.toBytes();
// 3. Deserialize transaction from bytes
let transactionFromBytes = Transaction.fromBytes(transactionBytes);
// 4. Set node accoutn ids
transactionFromBytes.setNodeAccountIds([new AccountId(3)]);
// 5. Freeze, sign and execute transaction
const response = await (
await (
await transactionFromBytes.freezeWithSigner(wallet)
).signWithSigner(wallet)
).executeWithSigner(wallet);
// 6. Get a receipt
const receipt = await response.getReceiptWithSigner(wallet);
console.log(`Transaction status: ${receipt.status.toString()}!`);
} catch (error) {
console.log(error);
}
provider.close();
}
void main();