GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — hedera-docs/.github/examples/java/CreateAccountDemo.java at main · hashgraph/hedera-docs

Expand file tree

/

CreateAccountDemo.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
67 lines (52 loc) · 2.73 KB
/

CreateAccountDemo.java

Copy path

File metadata and controls

67 lines (52 loc) · 2.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import com.hedera.hashgraph.sdk.*;
import java.net.http.*;
import java.net.URI;
import com.google.gson.*;
public class CreateAccountDemo {
public static void main(String[] args) throws Exception {
// .env-provided
AccountId operatorId = AccountId.fromString(System.getenv("OPERATOR_ID"));
PrivateKey operatorKey = PrivateKey.fromStringECDSA(System.getenv("OPERATOR_KEY"));
String network = System.getenv().getOrDefault("HEDERA_NETWORK", "local");
String mirrorNodeUrl = System.getenv().getOrDefault(
"MIRROR_NODE_URL",
"http://localhost:8080/api/v1"
);
if (operatorId == null || operatorKey == null) {
throw new IllegalStateException("OPERATOR_ID / OPERATOR_KEY not set");
}
Client client =
"local".equalsIgnoreCase(network)
? Client.forNetwork(java.util.Map.of("127.0.0.1:50211", new AccountId(3)))
: Client.forTestnet();
client.setOperator(operatorId, operatorKey);
PrivateKey newPrivateKey = PrivateKey.generateECDSA();
PublicKey newPublicKey = newPrivateKey.getPublicKey();
AccountCreateTransaction transaction = new AccountCreateTransaction()
.setKeyWithAlias(newPublicKey)
.setInitialBalance(new Hbar(20));
TransactionResponse txResponse = transaction.execute(client);
TransactionReceipt receipt = txResponse.getReceipt(client);
AccountId newAccountId = receipt.accountId;
System.out.println("\nHedera account created: " + newAccountId);
System.out.println("EVM Address: 0x" + newPublicKey.toEvmAddress() + "\n");
System.out.println("\nWaiting for Mirror Node to update...\n");
Thread.sleep(10000);
String url = mirrorNodeUrl + "/balances?account.id=" + newAccountId;
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
Gson gson = new Gson();
JsonObject data = gson.fromJson(response.body(), JsonObject.class);
if (data.has("balances") && data.getAsJsonArray("balances").size() > 0) {
JsonArray balances = data.getAsJsonArray("balances");
JsonObject accountBalance = balances.get(0).getAsJsonObject();
long tinybars = accountBalance.get("balance").getAsLong();
double hbar = tinybars / 100_000_000.0;
System.out.println("Account balance: " + hbar + " ℏ\n");
} else {
System.out.println("Account balance not yet available in Mirror Node");
}
client.close();
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.