Skip to content

Commit 9571a78

Browse files
authored
chore: replaced deprecated set_key/set_alias with set_key_with_alias … (hiero-ledger#2339)
Signed-off-by: Anshul Kushwaha <137977998+sudo-anshul@users.noreply.github.com>
1 parent 8527cfb commit 9571a78

1 file changed

Lines changed: 19 additions & 35 deletions

File tree

examples/account/account_create_transaction_evm_alias.py

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
"""
2-
32
Example: Create an account using an EVM-style alias (evm_address).
43
54
This example demonstrates:
65
1. Generating an ECDSA key pair (required for EVM compatibility).
7-
2. Deriving the EVM address from the public key.
8-
3. Creating a new Hedera account with that EVM address as its alias.
9-
4. Retrieving the account info to verify the alias.
6+
2. Creating a new Hedera account with an EVM address alias using set_key_with_alias().
7+
3. Retrieving the account info to verify the alias.
108
119
Usage:
1210
uv run examples/account/account_create_transaction_evm_alias.py
@@ -23,7 +21,6 @@
2321
AccountInfo,
2422
AccountInfoQuery,
2523
Client,
26-
EvmAddress,
2724
Hbar,
2825
PrivateKey,
2926
PublicKey,
@@ -40,64 +37,51 @@ def setup_client() -> Client:
4037
return client
4138

4239

43-
def generate_alias_key() -> tuple[PrivateKey, PublicKey, EvmAddress]:
40+
def generate_alias_key() -> tuple[PrivateKey, PublicKey]:
4441
"""
42+
Generate a new ECDSA key pair for account creation with alias.
4543
46-
Generate a new ECDSA key pair and derive its EVM-style alias address.
47-
48-
EVM aliases on Hedera must be derived from an ECDSA (secp256k1) key pair.
49-
The EVM address is the last 20 bytes of the keccak256 hash of the public key.
44+
ECDSA is required for EVM compatibility. The EVM address derivation
45+
is handled internally by set_key_with_alias().
5046
5147
Returns:
52-
tuple: A 3-tuple of:
48+
tuple: A 2-tuple of:
5349
- private_key: The newly generated ECDSA private key.
5450
- public_key: The public key corresponding to the private key.
55-
- evm_address (EvmAddress): The derived EVM address to be used as an alias.
5651
"""
5752
print("\nSTEP 1: Generating a new ECDSA key pair for the account alias...")
5853

5954
# ECDSA is required for EVM compatibility
6055
private_key = PrivateKey.generate("ecdsa")
6156
public_key = private_key.public_key()
6257

63-
# Compute the EVM address from the public key
64-
try:
65-
evm_address = public_key.to_evm_address()
66-
except ValueError as e:
67-
print(f"❌ Error: {e}")
68-
sys.exit(1)
69-
70-
print(f"✅ Generated new ECDSA key pair. EVM Address (alias): {evm_address}")
71-
return private_key, public_key, evm_address
58+
print("✅ Generated new ECDSA key pair")
59+
return private_key, public_key
7260

7361

74-
def create_account_with_alias(
75-
client: Client, private_key: PrivateKey, public_key: PublicKey, evm_address: EvmAddress
76-
) -> AccountId:
62+
def create_account_with_alias(client: Client, private_key: PrivateKey, public_key: PublicKey) -> AccountId:
7763
"""
64+
Create a new Hedera account using an EVM-style alias derived from the public key.
7865
79-
Create a new Hedera account using the provided EVM-style alias.
66+
Uses set_key_with_alias() which internally derives the EVM address from
67+
the provided ECDSA public key.
8068
8169
Important: When creating an account with an alias, the transaction must be
82-
signed by the private key corresponding to that alias. This proves ownership
83-
of the alias (the EVM address) being claimed.
70+
signed by the private key corresponding to that alias.
8471
8572
Args:
8673
client: An initialized `Client` instance with an operator set.
8774
private_key: The newly generated `PrivateKey` corresponding to the
8875
alias public key, used to sign the transaction.
89-
public_key: The public key associated with `private_key`, which is
90-
set as the new account's key.
91-
evm_address: The EVM-style alias (derived from `public_key`) to assign
92-
to the new account.
76+
public_key: The ECDSA public key associated with `private_key`, which is
77+
set as the new account's key and used to derive the alias.
9378
9479
Returns:
9580
The `AccountId` of the newly created account.
96-
9781
"""
9882
print("\nSTEP 2: Creating the account with the EVM address alias...")
9983

100-
transaction = AccountCreateTransaction().set_key(public_key).set_initial_balance(Hbar(5)).set_alias(evm_address)
84+
transaction = AccountCreateTransaction().set_key_with_alias(public_key).set_initial_balance(Hbar(5))
10185

10286
# Sign with new key (required for alias)
10387
# The client.execute() call below will attach the operator signature automatically.
@@ -136,9 +120,9 @@ def main():
136120
"""Orchestrate the example workflow."""
137121
client = setup_client()
138122
try:
139-
private_key, public_key, evm_address = generate_alias_key()
123+
private_key, public_key = generate_alias_key()
140124

141-
new_account_id = create_account_with_alias(client, private_key, public_key, evm_address)
125+
new_account_id = create_account_with_alias(client, private_key, public_key)
142126

143127
account_info = fetch_account_info(client, new_account_id)
144128

0 commit comments

Comments
 (0)