content: package io.jafka.jeos.impl;
  
  import io.jafka.jeos.LocalApi;
  import io.jafka.jeos.convert.Packer;
  import io.jafka.jeos.core.common.SignArg;
  import io.jafka.jeos.core.common.transaction.PackedTransaction;
  import io.jafka.jeos.core.common.transaction.TransactionAction;
  import io.jafka.jeos.core.common.transaction.TransactionAuthorization;
  import io.jafka.jeos.core.request.chain.json2bin.TransferArg;
  import io.jafka.jeos.core.request.chain.transaction.PushTransactionRequest;
  import io.jafka.jeos.util.KeyUtil;
  import io.jafka.jeos.util.Raw;
  
  import java.nio.ByteBuffer;
  import java.util.Arrays;
  import java.util.List;
  
  /**
   * 
   * @author adyliu (imxylz@gmail.com)
   * @since 2018年9月6日
   */
  public class LocalApiImpl implements LocalApi {
  
      @Override
      public String createPrivateKey() {
          return KeyUtil.createPrivateKey();
      }
  
      @Override
      public String toPublicKey(String privateKey) {
          return KeyUtil.toPublicKey(privateKey);
      }
  
      @Override
      public PushTransactionRequest transfer(SignArg arg, String privateKey, String account, String from, String to, String quantity, String memo) {
          // ① pack transfer data
          TransferArg transferArg = new TransferArg(from, to, quantity, memo);
          String transferData = Packer.packTransfer(transferArg);
          //
  
          // ③ create the authorization
          List<TransactionAuthorization> authorizations = Arrays.asList(new TransactionAuthorization(from, "active"));
  
          // ④ build the all actions
          List<TransactionAction> actions = Arrays.asList(//
                  new TransactionAction(account, "transfer", authorizations, transferData)//
          );
  
          // ⑤ build the packed transaction
          PackedTransaction packedTransaction = new PackedTransaction();
          packedTransaction.setExpiration(arg.getHeadBlockTime().plusSeconds(arg.getExpiredSecond()));
          packedTransaction.setRefBlockNum(arg.getLastIrreversibleBlockNum());
          packedTransaction.setRefBlockPrefix(arg.getRefBlockPrefix());
          
          packedTransaction.setMaxNetUsageWords(0);
          packedTransaction.setMaxCpuUsageMs(0);
          packedTransaction.setDelaySec(0);
          packedTransaction.setActions(actions);
  
          String hash = sign(privateKey, arg, packedTransaction);
          PushTransactionRequest req = new PushTransactionRequest();
          req.setTransaction(packedTransaction);
          req.setSignatures(Arrays.asList(hash));
          return req;
      }
  
      private String sign(String privateKey, SignArg arg, PackedTransaction t) {
  
          Raw raw = Packer.packPackedTransaction(arg.getChainId(), t);
          raw.pack(ByteBuffer.allocate(33).array());// TODO: what's this?
          String hash = KeyUtil.signHash(privateKey, raw.bytes());
          return hash;
      }
  }
  