PublicKey.java

  1. package com.syntifi.near.api.model.key;

  2. import com.fasterxml.jackson.annotation.JsonCreator;
  3. import com.fasterxml.jackson.annotation.JsonValue;
  4. import com.syntifi.crypto.key.AbstractPublicKey;
  5. import com.syntifi.crypto.key.Ed25519PublicKey;
  6. import com.syntifi.near.api.exception.NoSuchTypeException;
  7. import com.syntifi.near.borshj.Borsh;
  8. import lombok.Builder;
  9. import lombok.EqualsAndHashCode;

  10. /**
  11.  * Holds a Near PublicKey
  12.  *
  13.  * @author Alexandre Carvalho
  14.  * @author Andre Bertolace
  15.  * @since 0.0.1
  16.  */
  17. @EqualsAndHashCode(callSuper = true)
  18. public class PublicKey extends KeySig implements Borsh {

  19.     private static final int KEY_SIZE = 32;

  20.     public PublicKey() {
  21.         // This solves the case for borsh deserialization for keys of
  22.         // type ED25591 because to read the 'fixed' byte array we must know
  23.         // its size.
  24.         // If any other key (and signature) is implemented, a different
  25.         // approach is needed (like getters and setters annotation on borsh)
  26.         this.data = new byte[KEY_SIZE];
  27.     }

  28.     /**
  29.      * Instantiate a Public Key
  30.      *
  31.      * @param keyType the KeyType
  32.      * @param data    the key bytes
  33.      */
  34.     @Builder
  35.     public PublicKey(KeyType keyType, byte[] data) {
  36.         super(keyType, data);
  37.     }

  38.     public AbstractPublicKey getPublicKey() {
  39.         if (type == KeyType.ED25519) {
  40.             return new Ed25519PublicKey(data);
  41.         }
  42.         throw new NoSuchTypeException(String.format("No implementation found for key type %s", type));
  43.     }

  44.     @JsonCreator
  45.     public static PublicKey getPublicKeyFromJson(String base58String) {
  46.         return PublicKey.fromEncodedBase58String(base58String, PublicKey.class);
  47.     }

  48.     @JsonValue
  49.     public String getJsonPublicKey() {
  50.         return this.toEncodedBase58String();
  51.     }
  52. }