content: package org.adridadou.ethereum.keystore;
  
  import org.adridadou.ethereum.EthereumFacade;
  import org.adridadou.ethereum.values.EthAccount;
  import org.adridadou.exception.EthereumApiException;
  import org.ethereum.crypto.ECKey;
  import org.spongycastle.crypto.digests.SHA3Digest;
  import org.spongycastle.util.encoders.Hex;
  import org.web3j.crypto.WalletUtils;
  
  import java.io.File;
  import java.math.BigInteger;
  import java.util.Arrays;
  import java.util.List;
  import java.util.Optional;
  import java.util.stream.Collectors;
  
  /**
   * Created by davidroon on 26.12.16.
   * This code is released under Apache 2 license
   */
  public class AccountProvider {
  
      public static final int BIT_LENGTH = 256;
  
      private AccountProvider() {}
  
      public static EthAccount fromPrivateKey(final byte[] privateKey) {
          return new EthAccount(new BigInteger(1, privateKey));
      }
  
      public static EthAccount fromPrivateKey(final String privateKey) {
          return AccountProvider.fromPrivateKey(Hex.decode(privateKey));
      }
  
      public static EthAccount fromECKey(final ECKey ecKey) {
          return new EthAccount(ecKey.getPrivKey());
      }
  
      public static EthAccount fromSeed(final String id) {
          return AccountProvider.fromPrivateKey(doSha3(id.getBytes(EthereumFacade.CHARSET)));
      }
  
      public static SecureKey fromKeystore(final File file) {
          return new FileSecureKey(file);
      }
  
      public static List<SecureKey> listMainKeystores() {
          return listKeystores(new File(WalletUtils.getMainnetKeyDirectory()));
      }
  
      public static List<SecureKey> listRopstenKeystores() {
          return listKeystores(new File(WalletUtils.getTestnetKeyDirectory()));
      }
  
      public static List<SecureKey> listKeystores(final File directory) {
          File[] files = Optional.ofNullable(directory.listFiles()).orElseThrow(() -> new EthereumApiException("cannot find the folder " + WalletUtils.getMainnetKeyDirectory()));
          return Arrays.stream(files)
                  .filter(File::isFile)
                  .map(AccountProvider::fromKeystore)
                  .collect(Collectors.toList());
      }
  
      private static byte[] doSha3(byte[] message) {
          SHA3Digest digest = new SHA3Digest(BIT_LENGTH);
          byte[] hash = new byte[digest.getDigestSize()];
  
          if (message.length != 0) {
              digest.update(message, 0, message.length);
          }
          digest.doFinal(hash, 0);
          return hash;
      }
  }
  