Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 48 additions & 17 deletions examples/StakePoolVerificationKeyParser.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,69 @@
//DEPS com.bloxbean.cardano:cardano-client-lib:0.6.3
//JAVA 24
//JAVA 21

import static com.bloxbean.cardano.client.util.HexUtil.decodeHexString;
import static com.bloxbean.cardano.client.util.HexUtil.encodeHexString;
import static com.bloxbean.cardano.client.crypto.Bech32.encode;
import static com.bloxbean.cardano.client.crypto.Bech32.decode;
import static com.bloxbean.cardano.client.crypto.Blake2bUtil.blake2bHash224;

/**
* Utility to convert a hex-encoded Stake Pool Verification Key (potentially CBOR-prefixed)
* into a Cardano Pool Hash (Blake2b-224 hash) and its Bech32 representation ("pool" HRP).
* * Usage: jbang StakePoolVerificationKeyParser.java <HEX_POOL_VERIFICATION_KEY>
*/
public class StakePoolVerificationKeyParser {

private static final int CBOR_PREFIX_LENGTH = 4;
private static final int STANDARD_KEY_LENGTH = 64; // Expected length for a standard hex key

public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Bech32Pool usage, jbang Bech32Pool.java 582060afbe982faaee34b02ad0e75cd50d5d7a734f5daaf7b67bc8c492eb5299af2b");
// Throwing an exception is cleaner than printing to System.err and returning
throw new IllegalArgumentException(
"Usage: jbang StakePoolVerificationKeyParser.java <HEX_POOL_VERIFICATION_KEY>\n" +
"Example: jbang StakePoolVerificationKeyParser.java 582060afbe982faaee34b02ad0e75cd50d5d7a734f5daaf7b67bc8c492eb5299af2b"
);
}
var poolVerificationKey = args[0];
var poolVerifcationKeyStripped = poolVerificationKey.substring(4);

System.out.println("pool_verification_key: " + poolVerificationKey);
System.out.println("pool_verification_key_stripped: " + poolVerifcationKeyStripped);
final var poolVerificationKey = args[0];

if (poolVerificationKey.length() < STANDARD_KEY_LENGTH) {
throw new IllegalArgumentException(
"Invalid key length: Expected at least 64 hex characters (32 bytes), but got " + poolVerificationKey.length()
);
}

System.out.println("pool_verification_key_length: " + poolVerificationKey.length());
System.out.println("pool_verification_key_stripped_length: " + poolVerifcationKeyStripped.length());
// The key is assumed to be an extended format (e.g., CBOR-prefixed `5820...`),
// requiring the first 4 hex characters (2 bytes) to be stripped before hashing.
final var poolVerifcationKeyStripped = poolVerificationKey.substring(CBOR_PREFIX_LENGTH);

var poolVerificationKeyBlake224Hash = blake2bHash224(decodeHexString(poolVerificationKey));
var poolVerificationKeyBlake224HashStripped = blake2bHash224(decodeHexString(poolVerifcationKeyStripped));
System.out.println("Input_Pool_Verification_Key: " + poolVerificationKey);
System.out.println("Input_Key_Length: " + poolVerificationKey.length());

var poolVerificationKeyBech32 = encode(poolVerificationKeyBlake224HashStripped, "pool");
// Log the stripped key only if it was actually stripped
if (poolVerificationKey.length() != poolVerifcationKeyStripped.length()) {
System.out.println("Stripped_Key: " + poolVerifcationKeyStripped);
System.out.println("Stripped_Key_Length: " + poolVerifcationKeyStripped.length());
}

System.out.println("pool_hash: " + encodeHexString(poolVerificationKeyBlake224HashStripped));
System.out.println("pool_hash_as_bech32: " + poolVerificationKeyBech32);
// Decode the stripped hex string to bytes and compute the Blake2b-224 hash.
// This 28-byte hash is the canonical Pool Hash.
final var keyBytesStripped = decodeHexString(poolVerifcationKeyStripped);
final var poolHashBytes = blake2bHash224(keyBytesStripped);

final var poolHashHex = encodeHexString(poolHashBytes);

// Encode the pool hash bytes using the "pool" Human-Readable Part (HRP)
final var poolHashBech32 = encode(poolHashBytes, "pool");

var decodedBech32 = decode(poolVerificationKeyBech32);
System.out.println("decoded_bech32_hrp: " + decodedBech32.hrp);
System.out.println("decoded_bech32_data: " + encodeHexString(decodedBech32.data));
}
System.out.println("--- Results ---");
System.out.println("Pool_Hash (Hex): " + poolHashHex);
System.out.println("Pool_Hash (Bech32): " + poolHashBech32);

// Optional: Decode the Bech32 string to verify
final var decodedBech32 = decode(poolHashBech32);
System.out.println("Decoded_Bech32_HRP: " + decodedBech32.hrp);
System.out.println("Decoded_Bech32_Data (Hex): " + encodeHexString(decodedBech32.data));
}
}