-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathCreateAccountWithCodeDelegationExample.java
More file actions
101 lines (84 loc) · 3.58 KB
/
Copy pathCreateAccountWithCodeDelegationExample.java
File metadata and controls
101 lines (84 loc) · 3.58 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
// 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.Objects;
/**
* HIP-1340: EOA Code Delegation.
*
* <p>How to create an account with a delegation address.
*/
class CreateAccountWithCodeDelegationExample {
/*
* 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("Create Account With Code Delegation Example Start!");
/*
* Step 0:
* Create and configure the SDK Client.
*/
Client client = ClientHelper.forName(HEDERA_NETWORK);
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
client.setLogger(new Logger(LogLevel.valueOf(SDK_LOG_LEVEL)));
/*
* Step 1:
* Generate an ED25519 key pair for the new account.
*/
PrivateKey privateKey = PrivateKey.generateED25519();
PublicKey publicKey = privateKey.getPublicKey();
/*
* Step 2:
* Create a new account with a delegation address.
*/
var transaction = new AccountCreateTransaction()
.setKeyWithoutAlias(publicKey)
.setDelegationAddress("0x1111111111111111111111111111111111111111")
.freezeWith(client);
var delegationAddr = transaction.getDelegationAddress();
System.out.println("Delegation address: " + delegationAddr);
var response = transaction.execute(client);
var accountId = Objects.requireNonNull(response.getReceipt(client).accountId);
System.out.println("Created account with ID: " + accountId);
var info = new AccountInfoQuery().setAccountId(accountId).execute(client);
System.out.println("AccountInfo.delegationAddress: " + info.delegationAddress);
/*
* Clean up:
* Delete created account.
*/
new AccountDeleteTransaction()
.setTransferAccountId(OPERATOR_ID)
.setAccountId(accountId)
.freezeWith(client)
.sign(privateKey)
.execute(client)
.getReceipt(client);
client.close();
System.out.println("Create Account With Code Delegation Example Complete!");
}
}