1

Can someone please explain how to use the following constructor (listed here):

ECKey(BigInteger priv, org.spongycastle.math.ec.ECPoint pub) 

Say my private key is: priv = "L4...5".

  1. How do I get the big integer representation of it (in other words, and if I understood correctly, is there a function to go from check58 to biginteger?) , and

  2. what about the second parameter?

Thanks

1
+50

If all you are after is getting an ECKey object for that private key, then Bitcoinj has a DumpedPrivateKey class for just such an occasion.

import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.DumpedPrivateKey;
import org.bitcoinj.params.MainNetParams;

public class Example {

    public static void main(String[] args) {
        String priv = "L4...5";
        ECKey key = DumpedPrivateKey.fromBase58(MainNetParams.get(), priv).getKey();
    }

}

If you would like to use an ECKey constructor explicitly, then you should be able to get the code you need from here:

https://github.com/bitcoinj/bitcoinj/blob/release-0.14/core/src/main/java/org/bitcoinj/core/DumpedPrivateKey.java#L70-L91


With respect to the constructor you listed, it is a protected constructor, so it's probably not one you would be looking to use. It lists a private key and public key parameters. The private key may be null if the ECKey instance is only going to be used for verifying signatures, not actually producing them. The public key must not be null, and it saves time by not needing to re-compute the public key, which is a CPU intensive operation.

Note that most of the public ECKey constructors are deprecated. The preferred way of creating the ECKey objects now is to use the static factory methods.

0

You can call the public static method of ECKeyclass, fromPrivate(byte[] privKeyBytes, boolean compressed). The public key is calculated from it (this is slow), either compressed or not. Eg.,

        String privateKey = "Your Private Key";
        byte[] b = privateKey.getBytes();
        ECKey key = ECKey.fromPrivate(b, true);

or

BigInteger privKey = Base58.decodeToBigInteger(privateKey );
key = ECKey.fromPrivate(privKey);

https://bitcoinj.github.io/javadoc/0.13/org/bitcoinj/core/ECKey.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.