EncryptedPrivateKeyConverterFactory.java

  1. /*
  2.  * @copyright defined in LICENSE.txt
  3.  */

  4. package hera.transport;

  5. import static hera.api.model.BytesValue.of;
  6. import static hera.util.BytesValueUtils.append;
  7. import static hera.util.BytesValueUtils.trimPrefix;
  8. import static hera.util.EncodingUtils.encodeHexa;
  9. import static hera.util.TransportUtils.copyFrom;
  10. import static org.slf4j.LoggerFactory.getLogger;

  11. import hera.api.function.Function1;
  12. import hera.api.model.BytesValue;
  13. import hera.api.model.EncryptedPrivateKey;
  14. import hera.spec.resolver.EncryptedPrivateKeySpec;
  15. import hera.util.HexUtils;
  16. import org.slf4j.Logger;
  17. import types.Rpc;

  18. public class EncryptedPrivateKeyConverterFactory {

  19.   protected final transient Logger logger = getLogger(getClass());

  20.   protected final Function1<EncryptedPrivateKey, Rpc.SingleBytes> domainConverter =
  21.       new Function1<EncryptedPrivateKey, Rpc.SingleBytes>() {

  22.         @Override
  23.         public Rpc.SingleBytes apply(final EncryptedPrivateKey domainEncryptedPrivateKey) {
  24.           if (logger.isTraceEnabled()) {
  25.             logger.trace("Domain encrypted privateKey to convert. with checksum: {}, hexa: {}",
  26.                 domainEncryptedPrivateKey, encodeHexa(domainEncryptedPrivateKey.getBytesValue()));
  27.           }
  28.           if (domainEncryptedPrivateKey.getBytesValue().isEmpty()) {
  29.             return Rpc.SingleBytes.newBuilder()
  30.                 .setValue(copyFrom(domainEncryptedPrivateKey.getBytesValue())).build();
  31.           }
  32.           final BytesValue withVersion = domainEncryptedPrivateKey.getBytesValue();
  33.           final BytesValue withoutVersion = trimPrefix(withVersion);
  34.           final Rpc.SingleBytes rpcEncryptedPrivateKey =
  35.               Rpc.SingleBytes.newBuilder().setValue(copyFrom(withoutVersion)).build();
  36.           if (logger.isTraceEnabled()) {
  37.             logger.trace("Rpc encrypted private key convert. hexa: {}",
  38.                 HexUtils.encode(rpcEncryptedPrivateKey.getValue().toByteArray()));
  39.           }
  40.           return rpcEncryptedPrivateKey;
  41.         }
  42.       };

  43.   protected final Function1<Rpc.SingleBytes, EncryptedPrivateKey> rpcConverter =
  44.       new Function1<Rpc.SingleBytes, EncryptedPrivateKey>() {

  45.         @Override
  46.         public EncryptedPrivateKey apply(final Rpc.SingleBytes rpcEncryptedPrivateKey) {
  47.           if (logger.isTraceEnabled()) {
  48.             logger.trace("Rpc encrypted privateKey to convert. hexa: {}",
  49.                 HexUtils.encode(rpcEncryptedPrivateKey.getValue().toByteArray()));
  50.           }
  51.           if (rpcEncryptedPrivateKey.getValue().isEmpty()) {
  52.             return new EncryptedPrivateKey(BytesValue.EMPTY);
  53.           }
  54.           final byte[] withoutVersion = rpcEncryptedPrivateKey.getValue().toByteArray();
  55.           final byte[] withVersion = append(withoutVersion, EncryptedPrivateKeySpec.PREFIX);
  56.           final EncryptedPrivateKey domainEncryptedPrivateKey =
  57.               new EncryptedPrivateKey(of(withVersion));
  58.           if (logger.isTraceEnabled()) {
  59.             logger.trace("Domain encrypted private key converted. with checksum: {}, hexa: {}",
  60.                 domainEncryptedPrivateKey, encodeHexa(domainEncryptedPrivateKey.getBytesValue()));
  61.           }
  62.           return domainEncryptedPrivateKey;
  63.         }
  64.       };

  65.   public ModelConverter<EncryptedPrivateKey, Rpc.SingleBytes> create() {
  66.     return new ModelConverter<EncryptedPrivateKey, Rpc.SingleBytes>(domainConverter, rpcConverter);
  67.   }

  68. }