Skip to content

Commit 0ce8897

Browse files
feat(examples): update GenerateKeyExample to use ECDSA (#2700)
Signed-off-by: Ndacyayisenga-droid <ndacyayinoah@gmail.com>
1 parent 5f01dbb commit 0ce8897

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package com.hedera.hashgraph.sdk.examples;
33

4+
import com.hedera.hashgraph.sdk.EvmAddress;
45
import com.hedera.hashgraph.sdk.PrivateKey;
56
import com.hedera.hashgraph.sdk.PublicKey;
67

78
/**
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()}.
914
*/
1015
class GenerateKeyExample {
1116

1217
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");
1419

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);
1823

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");
2025
PublicKey publicKey = privateKey.getPublicKey();
2126
System.out.println("Public key: " + publicKey);
2227

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");
2433
}
2534
}

0 commit comments

Comments
 (0)