-1

I have a bitcoin private key which is 66 characters long, including a 0x prefix and I need to convert it to a ECKey. I tried this:

String private = "0x..."; // 66 characters total
DumpedPrivateKey dpk = DumpedPrivateKey.fromBase58(null, wif);
ECKey key = dpk.getKey();

it throws

org.bitcoinj.core.AddressFormatException: Illegal character 0 at position 0

If I remove the 0x the same error is thrown for any other 0 in the private string.

The 66 characters long key is something that comes from outside of our system, so out of my control. What am I missing here? How do I convert it so I could use the ECKey to sign transactions?

Eddy
  • 3,533
  • 13
  • 59
  • 89
  • 1
    It looks like it is expecting the key in base58, which does not include the `0` character. It seems to be expecting wallet import format keys instead. Either convert your hex key to a WIF one, or use a method to import it from hex if available – Raghav Sood Oct 08 '18 at 15:23
  • @RaghavSood thanks. How do I convert the key to WIF? – Eddy Oct 09 '18 at 06:10

1 Answers1

-1

Importing it from hex(Byte Array) will solve the problem. See the code below

ECKey key = ECKey.fromPrivate(privateKeyByteArrayHere);

Do not include preceding 80 and post fixing 01/Checksum to the byte array(byte[ ]).

Lakshitha Kanchana
  • 844
  • 2
  • 12
  • 32