Skip to content

Commit 0ab7995

Browse files
emiliyankhendrikebbersmustafauzunn
authored
feat: HIP-1340 (#2569)
Signed-off-by: emiliyank <e.kadiyski@gmail.com> Signed-off-by: Mustafa Uzun <mustafa.uzun@limechain.tech> Co-authored-by: Hendrik Ebbers <hendrik.ebbers@web.de> Co-authored-by: Mustafa Uzun <mustafa.uzun@limechain.tech>
1 parent 599f39c commit 0ab7995

13 files changed

Lines changed: 1055 additions & 18 deletions

File tree

examples/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ abstract class RunAllExample : DefaultTask() {
8585
.filter { it != "AccountHooksExample" }
8686
.filter { it != "ContractHooksExample" }
8787
.filter { it != "TransferTransactionHooksExample" }
88+
.filter { it != "FeeEstimateQueryExample" }
89+
// remove after official release
90+
.filter { it != "CreateAccountWithCodeDelegationExample" }
91+
.filter { it != "UpdateAccountToClearCodeDelegationExample" }
92+
.filter { it != "UpdateAccountToSetCodeDelegationExample" }
8893
.toList()
8994

9095
exampleClasses.forEach { className ->
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.hashgraph.sdk.examples;
3+
4+
import com.hedera.hashgraph.sdk.*;
5+
import com.hedera.hashgraph.sdk.logger.LogLevel;
6+
import com.hedera.hashgraph.sdk.logger.Logger;
7+
import io.github.cdimascio.dotenv.Dotenv;
8+
import java.util.Objects;
9+
10+
/**
11+
* HIP-1340: EOA Code Delegation.
12+
*
13+
* <p>How to create an account with a delegation address.
14+
*/
15+
class CreateAccountWithCodeDelegationExample {
16+
17+
/*
18+
* See .env.sample in the examples folder root for how to specify values below
19+
* or set environment variables with the same names.
20+
*/
21+
22+
/**
23+
* Operator's account ID.
24+
* Used to sign and pay for operations on Hedera.
25+
*/
26+
private static final AccountId OPERATOR_ID =
27+
AccountId.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID")));
28+
29+
/** Operator's private key. */
30+
private static final PrivateKey OPERATOR_KEY =
31+
PrivateKey.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY")));
32+
33+
/**
34+
* HEDERA_NETWORK defaults to testnet if not specified in dotenv file.
35+
* Network can be: localhost, testnet, previewnet or mainnet.
36+
*/
37+
private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet");
38+
39+
/**
40+
* SDK_LOG_LEVEL defaults to SILENT if not specified in dotenv file.
41+
* Log levels can be: TRACE, DEBUG, INFO, WARN, ERROR, SILENT.
42+
* <p>
43+
* Important pre-requisite: set simple logger log level to same level as the SDK_LOG_LEVEL,
44+
* for example via VM options: -Dorg.slf4j.simpleLogger.log.org.hiero=trace
45+
*/
46+
private static final String SDK_LOG_LEVEL = Dotenv.load().get("SDK_LOG_LEVEL", "SILENT");
47+
48+
public static void main(String[] args) throws Exception {
49+
System.out.println("Create Account With Code Delegation Example Start!");
50+
51+
/*
52+
* Step 0:
53+
* Create and configure the SDK Client.
54+
*/
55+
Client client = ClientHelper.forName(HEDERA_NETWORK);
56+
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
57+
client.setLogger(new Logger(LogLevel.valueOf(SDK_LOG_LEVEL)));
58+
59+
/*
60+
* Step 1:
61+
* Generate an ED25519 key pair for the new account.
62+
*/
63+
PrivateKey privateKey = PrivateKey.generateED25519();
64+
PublicKey publicKey = privateKey.getPublicKey();
65+
66+
/*
67+
* Step 2:
68+
* Create a new account with a delegation address.
69+
*/
70+
var transaction = new AccountCreateTransaction()
71+
.setKeyWithoutAlias(publicKey)
72+
.setDelegationAddress("0x1111111111111111111111111111111111111111")
73+
.freezeWith(client);
74+
75+
var delegationAddr = transaction.getDelegationAddress();
76+
System.out.println("Delegation address: " + delegationAddr);
77+
78+
var response = transaction.execute(client);
79+
var accountId = Objects.requireNonNull(response.getReceipt(client).accountId);
80+
System.out.println("Created account with ID: " + accountId);
81+
82+
var info = new AccountInfoQuery().setAccountId(accountId).execute(client);
83+
System.out.println("AccountInfo.delegationAddress: " + info.delegationAddress);
84+
85+
/*
86+
* Clean up:
87+
* Delete created account.
88+
*/
89+
new AccountDeleteTransaction()
90+
.setTransferAccountId(OPERATOR_ID)
91+
.setAccountId(accountId)
92+
.freezeWith(client)
93+
.sign(privateKey)
94+
.execute(client)
95+
.getReceipt(client);
96+
97+
client.close();
98+
99+
System.out.println("Create Account With Code Delegation Example Complete!");
100+
}
101+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.hashgraph.sdk.examples;
3+
4+
import com.hedera.hashgraph.sdk.*;
5+
import com.hedera.hashgraph.sdk.logger.LogLevel;
6+
import com.hedera.hashgraph.sdk.logger.Logger;
7+
import io.github.cdimascio.dotenv.Dotenv;
8+
import java.util.Objects;
9+
10+
/**
11+
* HIP-1340: EOA Code Delegation.
12+
*
13+
* <p>How to clear an existing delegation address on an account by submitting
14+
* an AccountUpdateTransaction with the zero EVM address.
15+
*/
16+
class UpdateAccountToClearCodeDelegationExample {
17+
18+
/*
19+
* See .env.sample in the examples folder root for how to specify values below
20+
* or set environment variables with the same names.
21+
*/
22+
23+
/**
24+
* Operator's account ID.
25+
* Used to sign and pay for operations on Hedera.
26+
*/
27+
private static final AccountId OPERATOR_ID =
28+
AccountId.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID")));
29+
30+
/** Operator's private key. */
31+
private static final PrivateKey OPERATOR_KEY =
32+
PrivateKey.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY")));
33+
34+
/**
35+
* HEDERA_NETWORK defaults to testnet if not specified in dotenv file.
36+
* Network can be: localhost, testnet, previewnet or mainnet.
37+
*/
38+
private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet");
39+
40+
/**
41+
* SDK_LOG_LEVEL defaults to SILENT if not specified in dotenv file.
42+
* Log levels can be: TRACE, DEBUG, INFO, WARN, ERROR, SILENT.
43+
* <p>
44+
* Important pre-requisite: set simple logger log level to same level as the SDK_LOG_LEVEL,
45+
* for example via VM options: -Dorg.slf4j.simpleLogger.log.org.hiero=trace
46+
*/
47+
private static final String SDK_LOG_LEVEL = Dotenv.load().get("SDK_LOG_LEVEL", "SILENT");
48+
49+
public static void main(String[] args) throws Exception {
50+
System.out.println("Update Account To Clear Code Delegation Example Start!");
51+
52+
/*
53+
* Step 0:
54+
* Create and configure the SDK Client.
55+
*/
56+
Client client = ClientHelper.forName(HEDERA_NETWORK);
57+
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
58+
client.setLogger(new Logger(LogLevel.valueOf(SDK_LOG_LEVEL)));
59+
60+
/*
61+
* Step 1:
62+
* Create an account with a delegation address already set, so the
63+
* subsequent clear has something meaningful to remove.
64+
*/
65+
PrivateKey accountKey = PrivateKey.generateED25519();
66+
PublicKey accountPublicKey = accountKey.getPublicKey();
67+
68+
var createResponse = new AccountCreateTransaction()
69+
.setKeyWithoutAlias(accountPublicKey)
70+
.setDelegationAddress("0x1111111111111111111111111111111111111111")
71+
.execute(client);
72+
var accountId = Objects.requireNonNull(createResponse.getReceipt(client).accountId);
73+
System.out.println("Created account with ID: " + accountId);
74+
75+
var infoBeforeClear = new AccountInfoQuery().setAccountId(accountId).execute(client);
76+
System.out.println("AccountInfo.delegationAddress before clear: " + infoBeforeClear.delegationAddress);
77+
78+
/*
79+
* Step 2:
80+
* Clear the delegation address by submitting an AccountUpdateTransaction
81+
* with the zero EVM address (0x00…00). The SDK detects all-zero bytes
82+
* and sends an empty value, which the network treats as a request to
83+
* remove the existing delegation.
84+
*
85+
* AccountUpdateTransaction must be signed by the account's key.
86+
*/
87+
var transaction = new AccountUpdateTransaction()
88+
.setAccountId(accountId)
89+
.setDelegationAddress("0x0000000000000000000000000000000000000000")
90+
.freezeWith(client);
91+
92+
// Access delegation address via getter — null because the zero address was normalized to "clear"
93+
System.out.println("Delegation address on transaction: " + transaction.getDelegationAddress());
94+
95+
transaction.sign(accountKey).execute(client).getReceipt(client);
96+
97+
var infoAfterClear = new AccountInfoQuery().setAccountId(accountId).execute(client);
98+
System.out.println("AccountInfo.delegationAddress after clear: " + infoAfterClear.delegationAddress);
99+
100+
/*
101+
* Clean up:
102+
* Delete created account.
103+
*/
104+
new AccountDeleteTransaction()
105+
.setTransferAccountId(OPERATOR_ID)
106+
.setAccountId(accountId)
107+
.freezeWith(client)
108+
.sign(accountKey)
109+
.execute(client)
110+
.getReceipt(client);
111+
112+
client.close();
113+
114+
System.out.println("Update Account To Clear Code Delegation Example Complete!");
115+
}
116+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.hashgraph.sdk.examples;
3+
4+
import com.hedera.hashgraph.sdk.*;
5+
import com.hedera.hashgraph.sdk.logger.LogLevel;
6+
import com.hedera.hashgraph.sdk.logger.Logger;
7+
import io.github.cdimascio.dotenv.Dotenv;
8+
import java.util.Objects;
9+
10+
/**
11+
* HIP-1340: EOA Code Delegation.
12+
*
13+
* <p>How to update an existing account to set a delegation address.
14+
*/
15+
class UpdateAccountToSetCodeDelegationExample {
16+
17+
/*
18+
* See .env.sample in the examples folder root for how to specify values below
19+
* or set environment variables with the same names.
20+
*/
21+
22+
/**
23+
* Operator's account ID.
24+
* Used to sign and pay for operations on Hedera.
25+
*/
26+
private static final AccountId OPERATOR_ID =
27+
AccountId.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID")));
28+
29+
/** Operator's private key. */
30+
private static final PrivateKey OPERATOR_KEY =
31+
PrivateKey.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY")));
32+
33+
/**
34+
* HEDERA_NETWORK defaults to testnet if not specified in dotenv file.
35+
* Network can be: localhost, testnet, previewnet or mainnet.
36+
*/
37+
private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet");
38+
39+
/**
40+
* SDK_LOG_LEVEL defaults to SILENT if not specified in dotenv file.
41+
* Log levels can be: TRACE, DEBUG, INFO, WARN, ERROR, SILENT.
42+
* <p>
43+
* Important pre-requisite: set simple logger log level to same level as the SDK_LOG_LEVEL,
44+
* for example via VM options: -Dorg.slf4j.simpleLogger.log.org.hiero=trace
45+
*/
46+
private static final String SDK_LOG_LEVEL = Dotenv.load().get("SDK_LOG_LEVEL", "SILENT");
47+
48+
public static void main(String[] args) throws Exception {
49+
System.out.println("Update Account To Set Code Delegation Example Start!");
50+
51+
/*
52+
* Step 0:
53+
* Create and configure the SDK Client.
54+
*/
55+
Client client = ClientHelper.forName(HEDERA_NETWORK);
56+
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
57+
client.setLogger(new Logger(LogLevel.valueOf(SDK_LOG_LEVEL)));
58+
59+
/*
60+
* Step 1:
61+
* Create an account we can update.
62+
*/
63+
PrivateKey accountKey = PrivateKey.generateED25519();
64+
PublicKey accountPublicKey = accountKey.getPublicKey();
65+
66+
var createResponse = new AccountCreateTransaction()
67+
.setKeyWithoutAlias(accountPublicKey)
68+
.execute(client);
69+
var accountId = Objects.requireNonNull(createResponse.getReceipt(client).accountId);
70+
System.out.println("Created account with ID: " + accountId);
71+
72+
/*
73+
* Step 2:
74+
* Update the account to set a delegation address.
75+
*
76+
* AccountUpdateTransaction must be signed by the account's key.
77+
*/
78+
var transaction = new AccountUpdateTransaction()
79+
.setAccountId(accountId)
80+
.setDelegationAddress("0x2222222222222222222222222222222222222222")
81+
.freezeWith(client);
82+
83+
// Access delegation address via getter
84+
System.out.println("Delegation address: " + transaction.getDelegationAddress());
85+
86+
var response = transaction.sign(accountKey).execute(client);
87+
response.getReceipt(client);
88+
89+
var info = new AccountInfoQuery().setAccountId(accountId).execute(client);
90+
System.out.println("AccountInfo.delegationAddress: " + info.delegationAddress);
91+
92+
/*
93+
* Clean up:
94+
* Delete created account.
95+
*/
96+
new AccountDeleteTransaction()
97+
.setTransferAccountId(OPERATOR_ID)
98+
.setAccountId(accountId)
99+
.freezeWith(client)
100+
.sign(accountKey)
101+
.execute(client)
102+
.getReceipt(client);
103+
104+
client.close();
105+
106+
System.out.println("Update Account To Set Code Delegation Example Complete!");
107+
}
108+
}

0 commit comments

Comments
 (0)