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?

1

There are 1 answers

0
Lakshitha Kanchana On

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[ ]).