|
1 | 1 | //DEPS com.bloxbean.cardano:cardano-client-lib:0.6.3 |
2 | | -//JAVA 24 |
| 2 | +//JAVA 21 |
3 | 3 |
|
4 | 4 | import static com.bloxbean.cardano.client.util.HexUtil.decodeHexString; |
5 | 5 | import static com.bloxbean.cardano.client.util.HexUtil.encodeHexString; |
6 | 6 | import static com.bloxbean.cardano.client.crypto.Bech32.encode; |
7 | 7 | import static com.bloxbean.cardano.client.crypto.Bech32.decode; |
8 | 8 | import static com.bloxbean.cardano.client.crypto.Blake2bUtil.blake2bHash224; |
9 | 9 |
|
| 10 | +/** |
| 11 | + * Utility to convert a hex-encoded Stake Pool Verification Key (potentially CBOR-prefixed) |
| 12 | + * into a Cardano Pool Hash (Blake2b-224 hash) and its Bech32 representation ("pool" HRP). |
| 13 | + * * Usage: jbang StakePoolVerificationKeyParser.java <HEX_POOL_VERIFICATION_KEY> |
| 14 | + */ |
10 | 15 | public class StakePoolVerificationKeyParser { |
11 | 16 |
|
| 17 | + private static final int CBOR_PREFIX_LENGTH = 4; |
| 18 | + private static final int STANDARD_KEY_LENGTH = 64; // Expected length for a standard hex key |
| 19 | + |
12 | 20 | public static void main(String[] args) { |
13 | 21 | if (args.length != 1) { |
14 | | - System.err.println("Bech32Pool usage, jbang Bech32Pool.java 582060afbe982faaee34b02ad0e75cd50d5d7a734f5daaf7b67bc8c492eb5299af2b"); |
| 22 | + // Throwing an exception is cleaner than printing to System.err and returning |
| 23 | + throw new IllegalArgumentException( |
| 24 | + "Usage: jbang StakePoolVerificationKeyParser.java <HEX_POOL_VERIFICATION_KEY>\n" + |
| 25 | + "Example: jbang StakePoolVerificationKeyParser.java 582060afbe982faaee34b02ad0e75cd50d5d7a734f5daaf7b67bc8c492eb5299af2b" |
| 26 | + ); |
15 | 27 | } |
16 | | - var poolVerificationKey = args[0]; |
17 | | - var poolVerifcationKeyStripped = poolVerificationKey.substring(4); |
18 | 28 |
|
19 | | - System.out.println("pool_verification_key: " + poolVerificationKey); |
20 | | - System.out.println("pool_verification_key_stripped: " + poolVerifcationKeyStripped); |
| 29 | + final var poolVerificationKey = args[0]; |
| 30 | + |
| 31 | + if (poolVerificationKey.length() < STANDARD_KEY_LENGTH) { |
| 32 | + throw new IllegalArgumentException( |
| 33 | + "Invalid key length: Expected at least 64 hex characters (32 bytes), but got " + poolVerificationKey.length() |
| 34 | + ); |
| 35 | + } |
21 | 36 |
|
22 | | - System.out.println("pool_verification_key_length: " + poolVerificationKey.length()); |
23 | | - System.out.println("pool_verification_key_stripped_length: " + poolVerifcationKeyStripped.length()); |
| 37 | + // The key is assumed to be an extended format (e.g., CBOR-prefixed `5820...`), |
| 38 | + // requiring the first 4 hex characters (2 bytes) to be stripped before hashing. |
| 39 | + final var poolVerifcationKeyStripped = poolVerificationKey.substring(CBOR_PREFIX_LENGTH); |
24 | 40 |
|
25 | | - var poolVerificationKeyBlake224Hash = blake2bHash224(decodeHexString(poolVerificationKey)); |
26 | | - var poolVerificationKeyBlake224HashStripped = blake2bHash224(decodeHexString(poolVerifcationKeyStripped)); |
| 41 | + System.out.println("Input_Pool_Verification_Key: " + poolVerificationKey); |
| 42 | + System.out.println("Input_Key_Length: " + poolVerificationKey.length()); |
27 | 43 |
|
28 | | - var poolVerificationKeyBech32 = encode(poolVerificationKeyBlake224HashStripped, "pool"); |
| 44 | + // Log the stripped key only if it was actually stripped |
| 45 | + if (poolVerificationKey.length() != poolVerifcationKeyStripped.length()) { |
| 46 | + System.out.println("Stripped_Key: " + poolVerifcationKeyStripped); |
| 47 | + System.out.println("Stripped_Key_Length: " + poolVerifcationKeyStripped.length()); |
| 48 | + } |
29 | 49 |
|
30 | | - System.out.println("pool_hash: " + encodeHexString(poolVerificationKeyBlake224HashStripped)); |
31 | | - System.out.println("pool_hash_as_bech32: " + poolVerificationKeyBech32); |
| 50 | + // Decode the stripped hex string to bytes and compute the Blake2b-224 hash. |
| 51 | + // This 28-byte hash is the canonical Pool Hash. |
| 52 | + final var keyBytesStripped = decodeHexString(poolVerifcationKeyStripped); |
| 53 | + final var poolHashBytes = blake2bHash224(keyBytesStripped); |
| 54 | + |
| 55 | + final var poolHashHex = encodeHexString(poolHashBytes); |
| 56 | + |
| 57 | + // Encode the pool hash bytes using the "pool" Human-Readable Part (HRP) |
| 58 | + final var poolHashBech32 = encode(poolHashBytes, "pool"); |
32 | 59 |
|
33 | | - var decodedBech32 = decode(poolVerificationKeyBech32); |
34 | | - System.out.println("decoded_bech32_hrp: " + decodedBech32.hrp); |
35 | | - System.out.println("decoded_bech32_data: " + encodeHexString(decodedBech32.data)); |
36 | | - } |
| 60 | + System.out.println("--- Results ---"); |
| 61 | + System.out.println("Pool_Hash (Hex): " + poolHashHex); |
| 62 | + System.out.println("Pool_Hash (Bech32): " + poolHashBech32); |
37 | 63 |
|
| 64 | + // Optional: Decode the Bech32 string to verify |
| 65 | + final var decodedBech32 = decode(poolHashBech32); |
| 66 | + System.out.println("Decoded_Bech32_HRP: " + decodedBech32.hrp); |
| 67 | + System.out.println("Decoded_Bech32_Data (Hex): " + encodeHexString(decodedBech32.data)); |
| 68 | + } |
38 | 69 | } |
0 commit comments