2

I'm facing an issue regarding generating address from private key.

I got the master private key from electrum as well as from bitcoin core that is starts from

xprv9s21xxxxxxxxxxxxxxxxxxxxxxxxxxxx

in bitcoinJ:

ECKey key=ECKey.fromPrivate(prv); // it accepts bytes[] or BigInteger

how can i convert 9s21xxxxxxxxxxxxxxxxx into bytes[] or BigInteger.

Try:

String prvkey=9s21xxxxxxxxxxxxxxxxxxxxx
BigInteger bytes=new BigInteger(priv,16);

it throws exception as it can't convert due to number format.

Try 2:

byte[] bytes=prvkey.getBytes(StandardCharsets.UTF_16);

it generates valid address from ECKey and i send transaction to that address via electrum. but wallet didn't receive money. Don't know where the money gone.

what should i do to convert master private key into BigInteger or bytes[]

PS: I'm beginner in cryptocurrency

Muhammad Saad
  • 713
  • 1
  • 9
  • 31

1 Answers1

-1

Convert string private key into bytes[]:

ECKey key = ECKey.fromPrivate(prv.getBytes());

Or, convert string private key into BigInteger:

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

Example from bitcoinj's github repo

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
  • i generated address from your methods and send coins via electrum to that address. – Muhammad Saad Mar 21 '18 at 07:46
  • after confirmation,i'll tell you whether the transactions goes to actual account which prvkey belongs or some other cloud account – Muhammad Saad Mar 21 '18 at 07:47
  • Why would it go to "other cloud accounts"? If you generated the private key from one of your local wallets (core, electrum), then it should go to those address – denvercoder9 Mar 21 '18 at 07:48
  • You can accept this answer if it answered your question. Also, check out the example code I linked. That example also shows how to generate legacy addresses and make a transaction – denvercoder9 Mar 21 '18 at 07:49
  • still transaction didn't go that actual account. goes into some cloud account – Muhammad Saad Mar 21 '18 at 07:51
  • You're sending some funds from addrA to addrB, right? Where did you create both addresses from? And post the tx link – denvercoder9 Mar 21 '18 at 07:55
  • lemme clear more. I've 2 electrum accounts A and B. i got the prvkey of A and got address using bitcoinJ (ECKey). now i send money from B accounts to that address which is generated from ECkey – Muhammad Saad Mar 21 '18 at 07:58
  • This is out of scope of this question. Let's move to chat – denvercoder9 Mar 21 '18 at 07:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167239/discussion-between-rafid059-and-muhammad-saad). – denvercoder9 Mar 21 '18 at 07:59