FireStack-Lab / LaksaJ

LaksaJ -- Zilliqa Blockchain Java Library
GNU General Public License v3.0
19 stars 9 forks source link

cannot be used in android #10

Open AndreiD opened 5 years ago

AndreiD commented 5 years ago

upon importing it into an android project I get the following error:

2019-02-25 11:43:28.299 6546-6546/wallet.zilliqa E/System: ******** DEPRECATED FUNCTIONALITY ******** 2019-02-25 11:43:28.299 6546-6546/wallet.zilliqa E/System: * The implementation of the KeyPairGenerator.EC algorithm from 2019-02-25 11:43:28.299 6546-6546/wallet.zilliqa E/System: * the BC provider is deprecated in this version of Android. 2019-02-25 11:43:28.299 6546-6546/wallet.zilliqa E/System: * It will be removed in a future version of Android and your 2019-02-25 11:43:28.299 6546-6546/wallet.zilliqa E/System: * application will no longer be able to request it. Please see 2019-02-25 11:43:28.299 6546-6546/wallet.zilliqa E/System: * https://android-developers.googleblog.com/2018/03/cryptography-changes-in-android-p.html 2019-02-25 11:43:28.299 6546-6546/wallet.zilliqa E/System: * for more details. 2019-02-25 11:43:28.304 6546-6546/wallet.zilliqa W/System.err: java.security.InvalidAlgorithmParameterException: parameter object not a ECParameterSpec 2019-02-25 11:43:28.305 6546-6546/wallet.zilliqa W/System.err: at com.android.org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi$EC.initialize(KeyPairGeneratorSpi.java:156) 2019-02-25 11:43:28.305 6546-6546/wallet.zilliqa W/System.err: at java.security.KeyPairGenerator.initialize(KeyPairGenerator.java:438) 2019-02-25 11:43:28.305 6546-6546/wallet.zilliqa W/System.err: at com.firestack.laksaj.crypto.Schnorr.generateKeyPair(Schnorr.java:39) 2019-02-25 11:43:28.305 6546-6546/wallet.zilliqa W/System.err: at com.firestack.laksaj.crypto.KeyTools.generateKeyPair(KeyTools.java:38)

searching some fixes I came across

Android included a shortened version of Bouncycastle, and there is no full support for ECDSA. You can see in the link that algorithm KeyPairGenerator/ECDSA is not supported, which is the required one to generate ethereum keys.

You can not include directly the bouncycastle library because there is a conflict with the package name org.bouncycastle. I suggest to include spongycastle in your project, which it is a repackaged version of bouncycastle for Android org.spongycastle.

so in android

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");

is not supported.

so the way to transform this into "android" is to

import org.spongycastle.jce.provider.BouncyCastleProvider;
  static {
    Security.addProvider(new BouncyCastleProvider());
  }

but it gives me the same error

any ideas how to make it work in android ?

AndreiD commented 5 years ago

changing the line KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "SC");

gives

java.security.InvalidAlgorithmParameterException: parameter object not a ECParameterSpec

other things I tried:

  private static ECParameterSpec getSecp256k1Spec() {
    org.spongycastle.jce.spec.ECParameterSpec secp256k1_SC = ECNamedCurveTable.getParameterSpec("secp256k1");
    org.spongycastle.math.ec.ECPoint g = secp256k1_SC.getG();
    ECPoint g1 = convertECPoint(g);
    EllipticCurve curve = EC5Util.convertCurve(secp256k1_SC.getCurve(), null);
    BigInteger n = secp256k1_SC.getN();
    int h = secp256k1_SC.getH().intValue();
    return new ECParameterSpec(curve, g1, n, h);
  }

  private static ECPoint convertECPoint(org.spongycastle.math.ec.ECPoint g) {
    return new ECPoint(g.getXCoord().toBigInteger(), g.getYCoord().toBigInteger());
  }

now

   keyPairGenerator.initialize(getSecp256k1Spec());

seems to work but now address returned is null... :|

any ideas how to get an address generated in android ?

renlulu commented 5 years ago

Thank you very much for your feedback, but I have not had any android experience before. I will find a way to double this question(I feel that it may be a problem with some libraries on android platform). If there is a result, I will tell you the first time. Thanks!

AndreiD commented 5 years ago

did you find a solution for this?

bhchhjc commented 5 years ago

I have experienced similar errors. I tried testing on Android P version, but there was an error and gave me the reference URL.

https://android-developers.googleblog.com/2018/03/cryptography-changes-in-android-p.html