1

I use Java and bitcoinj. I have a private key

String str="5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf";

I try to get compress and decompress 2 adresses in this format:

1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm  1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH

I do the first step, try get ECKey:

NetworkParameters params = MainNetParams.get();
      ECKey key;
  if (str.length() == 51 || str.length() == 52) {
      DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, str);
      key = dumpedPrivateKey.getKey();            //ERROR
  } else {
      BigInteger privKey = Base58.decodeToBigInteger(str);
      key = ECKey.fromPrivate(privKey);
  }

. . .

and I get an exception:

Exception in thread "main" java.lang.IllegalArgumentException
   at com.google.common.base.Preconditions.checkArgument(Preconditions.java:128)
   at org.bitcoinj.core.ECKey.<init>(ECKey.java:195)
   at org.bitcoinj.core.ECKey.fromPrivate(ECKey.java:243)
   at org.bitcoinj.core.ECKey.fromPrivate(ECKey.java:259)
   at org.bitcoinj.core.DumpedPrivateKey.getKey(DumpedPrivateKey.java:101)
   at com.example.demo2.controllers.AdressFromKey.main(AdressFromKey.java:35)

What am I doing wrong?

Glorfindel
  • 529
  • 3
  • 8
  • 20

1 Answers1

1

In bitcoinj, there is a check that indicates an error if the private key is 0 or 1. The above private key is 1, so it is an error.

https://github.com/bitcoinj/bitcoinj/blob/04378aa6389deb5143f66cf2ab0abec52dc9d394/core/src/main/java/org/bitcoinj/core/ECKey.java#L190-L194

  • Thanks a lot. My task turned out to be very successful Undecided

    I am trying to generate these keys https://keys.lol/bitcoin/407532363493837873358792558699156305516644599981797776434520630335882679096

    and get the corresponding addresses. They all knock out a bug.

    Maybe there is some way to hardcode and get these addresses without libraries?

    – Samanta Fox Nov 29 '21 at 07:04
  • It's not impossible if you implement what the library does on your own, but it's easy to use. – Shigeyuki Azuchi Nov 30 '21 at 13:27