Skip to content

Commit da5cce6

Browse files
authored
Update StakePoolVerificationKeyParser.java (#656)
2 parents 5a49e83 + c8e1fd9 commit da5cce6

1 file changed

Lines changed: 48 additions & 17 deletions

File tree

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,69 @@
11
//DEPS com.bloxbean.cardano:cardano-client-lib:0.6.3
2-
//JAVA 24
2+
//JAVA 21
33

44
import static com.bloxbean.cardano.client.util.HexUtil.decodeHexString;
55
import static com.bloxbean.cardano.client.util.HexUtil.encodeHexString;
66
import static com.bloxbean.cardano.client.crypto.Bech32.encode;
77
import static com.bloxbean.cardano.client.crypto.Bech32.decode;
88
import static com.bloxbean.cardano.client.crypto.Blake2bUtil.blake2bHash224;
99

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+
*/
1015
public class StakePoolVerificationKeyParser {
1116

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+
1220
public static void main(String[] args) {
1321
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+
);
1527
}
16-
var poolVerificationKey = args[0];
17-
var poolVerifcationKeyStripped = poolVerificationKey.substring(4);
1828

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+
}
2136

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

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());
2743

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+
}
2949

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");
3259

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

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+
}
3869
}

0 commit comments

Comments
 (0)