|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 | package com.hedera.hashgraph.sdk.examples; |
3 | 3 |
|
| 4 | +import com.hedera.hashgraph.sdk.EvmAddress; |
4 | 5 | import com.hedera.hashgraph.sdk.PrivateKey; |
5 | 6 | import com.hedera.hashgraph.sdk.PublicKey; |
6 | 7 |
|
7 | 8 | /** |
8 | | - * How to generate ED25519 key pair. |
| 9 | + * How to generate an ECDSA (secp256k1) key pair and derive the EVM address from the public key. |
| 10 | + * |
| 11 | + * <p>ECDSA keys with an EVM address derived from the public key are the recommended choice for new |
| 12 | + * Hedera accounts when you want compatibility with Ethereum tooling (wallets, Hardhat, ethers.js, |
| 13 | + * etc.). For legacy Hedera-only flows you can still use {@link PrivateKey#generateED25519()}. |
9 | 14 | */ |
10 | 15 | class GenerateKeyExample { |
11 | 16 |
|
12 | 17 | public static void main(String[] args) { |
13 | | - System.out.println("Generate ED25519 Private And Public Key Pair Example Start!"); |
| 18 | + System.out.println("Generate ECDSA key pair and EVM address example start"); |
14 | 19 |
|
15 | | - System.out.println("Generating the ED25519 private key..."); |
16 | | - PrivateKey privateKey = PrivateKey.generateED25519(); |
17 | | - System.out.println("Private Key: " + privateKey); |
| 20 | + System.out.println("Generating an ECDSA (secp256k1) private key..."); |
| 21 | + PrivateKey privateKey = PrivateKey.generateECDSA(); |
| 22 | + System.out.println("Private key: " + privateKey); |
18 | 23 |
|
19 | | - System.out.println("Deriving a public key from the above private key"); |
| 24 | + System.out.println("Deriving the public key from the private key"); |
20 | 25 | PublicKey publicKey = privateKey.getPublicKey(); |
21 | 26 | System.out.println("Public key: " + publicKey); |
22 | 27 |
|
23 | | - System.out.println("Generate ED25519 Private And Public Key Pair Example Complete!"); |
| 28 | + System.out.println("Deriving the EVM address (last 20 bytes of Keccak-256 of the uncompressed public key)"); |
| 29 | + EvmAddress evmAddress = publicKey.toEvmAddress(); |
| 30 | + System.out.println("EVM address: 0x" + evmAddress); |
| 31 | + |
| 32 | + System.out.println("Generate ECDSA key pair and EVM address example complete"); |
24 | 33 | } |
25 | 34 | } |
0 commit comments