PrivateKey.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.AbstractPrivateKey;
  5. import com.syntifi.crypto.key.Ed25519PrivateKey;
  6. import com.syntifi.near.api.exception.NoSuchTypeException;
  7. import lombok.Builder;

  8. /**
  9.  * Holds a Near PrivateKey
  10.  *
  11.  * @author Alexandre Carvalho
  12.  * @author Andre Bertolace
  13.  * @since 0.0.1
  14.  */
  15. public class PrivateKey extends KeySig {

  16.     private static final int KEY_SIZE = 32;

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

  25.     @Builder
  26.     public PrivateKey(KeyType keyType, byte[] data) {
  27.         super(keyType, data);
  28.     }

  29.     public AbstractPrivateKey getPrivateKey() {
  30.         if (type == KeyType.ED25519) {
  31.             return new Ed25519PrivateKey(data);
  32.         }
  33.         throw new NoSuchTypeException(String.format("No implementation found for key type %s", type));
  34.     }

  35.     @JsonCreator
  36.     public static PrivateKey getPublicKeyFromJson(String base58String) {
  37.         return PrivateKey.fromEncodedBase58String(base58String, PrivateKey.class);
  38.     }

  39.     @JsonValue
  40.     public String getJsonPublicKey() {
  41.         return this.toEncodedBase58String();
  42.     }
  43. }