forked from hiero-ledger/hiero-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignTransactionExample.java
More file actions
145 lines (126 loc) · 5.36 KB
/
Copy pathSignTransactionExample.java
File metadata and controls
145 lines (126 loc) · 5.36 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SPDX-License-Identifier: Apache-2.0
package com.hedera.hashgraph.sdk.examples;
import com.hedera.hashgraph.sdk.*;
import com.hedera.hashgraph.sdk.logger.LogLevel;
import com.hedera.hashgraph.sdk.logger.Logger;
import io.github.cdimascio.dotenv.Dotenv;
import java.util.Collections;
import java.util.Objects;
/**
* How to sign a transaction with a multi-sig account.
*/
class SignTransactionExample {
/*
* See .env.sample in the examples folder root for how to specify values below
* or set environment variables with the same names.
*/
/**
* Operator's account ID.
* Used to sign and pay for operations on Hedera.
*/
private static final AccountId OPERATOR_ID =
AccountId.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID")));
/**
* Operator's private key.
*/
private static final PrivateKey OPERATOR_KEY =
PrivateKey.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY")));
/**
* HEDERA_NETWORK defaults to testnet if not specified in dotenv file.
* Network can be: localhost, testnet, previewnet or mainnet.
*/
private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet");
/**
* SDK_LOG_LEVEL defaults to SILENT if not specified in dotenv file.
* Log levels can be: TRACE, DEBUG, INFO, WARN, ERROR, SILENT.
* <p>
* Important pre-requisite: set simple logger log level to same level as the SDK_LOG_LEVEL,
* for example via VM options: -Dorg.slf4j.simpleLogger.log.org.hiero=trace
*/
private static final String SDK_LOG_LEVEL = Dotenv.load().get("SDK_LOG_LEVEL", "SILENT");
public static void main(String[] args) throws Exception {
System.out.println("Sign Transaction Example Start!");
/*
* Step 0:
* Create and configure the SDK Client.
*/
Client client = ClientHelper.forName(HEDERA_NETWORK);
// All generated transactions will be paid by this account and signed by this key.
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
// Attach logger to the SDK Client.
client.setLogger(new Logger(LogLevel.valueOf(SDK_LOG_LEVEL)));
/*
* Step 1:
* Generate ED25519 key pairs.
*/
System.out.println("Generating ED25519 key pairs...");
PrivateKey privateKey1 = PrivateKey.generateED25519();
PublicKey publicKey1 = privateKey1.getPublicKey();
PrivateKey privateKey2 = PrivateKey.generateED25519();
PublicKey publicKey2 = privateKey2.getPublicKey();
/*
* Step 2:
* Create a Key List from keys generated in previous step.
*/
System.out.println("Creating a Key List...");
KeyList keylist = new KeyList();
keylist.add(publicKey1);
keylist.add(publicKey2);
System.out.println("Created a Key List: " + keylist);
/*
* Step 3:
* Create a new account with a Key List created in a previous step.
*/
System.out.println("Creating new account...");
TransactionResponse createAccountTxResponse = new AccountCreateTransaction()
.setInitialBalance(Hbar.from(2))
.setKeyWithoutAlias(keylist)
.execute(client);
TransactionReceipt createAccountTxReceipt = createAccountTxResponse.getReceipt(client);
var accountId = createAccountTxReceipt.accountId;
Objects.requireNonNull(accountId);
System.out.println("Created new account with ID: " + accountId);
/*
* Step 4:
* Create a transfer transaction and freeze it with a client.
*/
System.out.println("Creating a transfer transaction...");
TransferTransaction transferTx = new TransferTransaction()
.setNodeAccountIds(Collections.singletonList(new AccountId(0, 0, 3)))
.addHbarTransfer(
Objects.requireNonNull(createAccountTxReceipt.accountId),
Hbar.from(1).negated())
.addHbarTransfer(new AccountId(0, 0, 3), Hbar.from(1))
.freezeWith(client);
/*
* Step 5:
* Sign the transfer transaction with all respective keys (from a Key List).
*/
System.out.println("Signing the transfer transaction...");
transferTx.signWithOperator(client);
privateKey1.signTransaction(transferTx);
privateKey2.signTransaction(transferTx);
/*
* Step 6:
* Execute the transfer transaction and output its status.
*/
System.out.println("Executing the transfer transaction...");
TransactionResponse transferTxResponse = transferTx.execute(client);
TransactionReceipt transferTxReceipt = transferTxResponse.getReceipt(client);
System.out.println("The transfer transaction was complete with status: " + transferTxReceipt.status);
/*
* Clean up:
* Delete created account.
*/
new AccountDeleteTransaction()
.setAccountId(accountId)
.setTransferAccountId(OPERATOR_ID)
.freezeWith(client)
.sign(privateKey1)
.sign(privateKey2)
.execute(client)
.getReceipt(client);
client.close();
System.out.println("Sign Transaction Example Complete!");
}
}