Parsing validator key


I know this forum is more about Cosmos, but I cannot find any specific forum for Tendermint, so I’m trying here.

I’m writing a Java application connected to Tendermint via ABCI. Inside the initChain callback, I want to create objects of class corresponding to the public keys of the initial validators of the chain. Since the keys are in ED25519 format, I use the bouncycastle library for dealing with them. I wrote the following code:

public void initChain(RequestInitChain req, StreamObserver<ResponseInitChain> responseObserver) {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance("Ed25519", "BC");
    	for (ValidatorUpdate validator: req.getValidatorsList()) {
    		System.out.println("key type: " + validator.getPubKey().getType());
    		ByteString data = validator.getPubKey().getData();
    		byte[] bytes = data.toByteArray();
    		String address = new String(Hex.encode(sha256.hash(bytes))).substring(0, 40);
    		System.out.println("address: " + address);
    		String publicKeyBase64 = new String(Base64.getEncoder().encode(bytes));
	     	System.out.println("pubKey in Base64: " + publicKeyBase64);
    		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
            // exception at the line below
    		PublicKey publicKey = keyFactory.generatePublic(keySpec);

The code compiles correctly. At run time, it prints the correct strings for key type (“ed25519”), address and public key in Base64 format. They actually coincide with what I see in the genesis.json file. However, the execution ends with an exception at the last line of the loop. It seems that Java does not recognize the spec of the public key: encoded key spec not recognized: failed to construct sequence from byte[]: DER length more than 4 bytes: 105

Any idea of what I’m doing wrong here?