AbstractSerializedKeyTaggedHexDeserializer.java

  1. package com.syntifi.casper.sdk.jackson.deserializer;

  2. import java.io.IOException;
  3. import java.security.NoSuchAlgorithmException;

  4. import com.fasterxml.jackson.core.JsonParser;
  5. import com.fasterxml.jackson.databind.DeserializationContext;
  6. import com.fasterxml.jackson.databind.JsonDeserializer;
  7. import com.fasterxml.jackson.databind.JsonNode;
  8. import com.syntifi.casper.sdk.exception.DeserializationException;
  9. import com.syntifi.casper.sdk.exception.InvalidByteStringException;
  10. import com.syntifi.casper.sdk.exception.NoSuchKeyTagException;
  11. import com.syntifi.casper.sdk.model.clvalue.encdec.StringByteHelper;
  12. import com.syntifi.casper.sdk.model.key.AbstractSerializedKeyTaggedHex;
  13. import com.syntifi.casper.sdk.model.key.Tag;

  14. /**
  15.  * Customize the mapping of Casper's Hex String preceded by the crypto
  16.  * algorithm tag such as PublicKey/Signature
  17.  *
  18.  * @author Alexandre Carvalho
  19.  * @author Andre Bertolace
  20.  * @since 0.0.1
  21.  */
  22. public abstract class AbstractSerializedKeyTaggedHexDeserializer<T extends AbstractSerializedKeyTaggedHex<S>, S extends Tag>
  23.         extends JsonDeserializer<T> {

  24.     @Override
  25.     public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
  26.         JsonNode node = p.getCodec().readTree(p);

  27.         T object = this.getInstanceOf();

  28.         try {
  29.             byte[] bytes = StringByteHelper.hexStringToByteArray(node.asText());
  30.             this.loadKey(object, bytes);
  31.         } catch (NoSuchAlgorithmException | NoSuchKeyTagException | InvalidByteStringException e) {
  32.             throw new DeserializationException("Problem deserializing Algorithm tagged hex string", e);
  33.         }

  34.         return object;
  35.     }

  36.     protected abstract T getInstanceOf();

  37.     protected abstract void loadKey(T key, byte[] bytes) throws NoSuchAlgorithmException, NoSuchKeyTagException;
  38. }