1

From the steps in the wiki, take this example private key:

0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

When I import this into Electrum, I find these values:

Address:

1GAehh7TsJAHuUAeKZcXf5CnwuGuGgyX2S

Public Key:

04d0de0aaeaefad02b8bdc8a01a1b8b11c696bd3d66a2c5f10780d95b7df42645cd85228a6fb29940e858e7e55842ae2bd115d1ed7cc0e82d934e929c97648cb0a

How can I determine the address from the key (without using Electrum)?

I understand that the derivation route is private key -> public key -> address, so I've found a way to generate the public key in Java using BitcoinJ:

BigInteger privkey = new BigInteger("0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D", 16);
byte[] bytes = org.bitcoinj.core.ECKey.publicKeyFromPrivate(privkey, false);
String publickey = new BigInteger(1, bytes).toString(16);
while(publickey.length() < 130){
    publickey = "0" + publickey;
}
Log.d("public",publickey);

It gives the correct public key as shown above, so I just need to find a way to get the address.

I don't know much about different address formats, but my goal is to use the address to check the balance using some public API like blockchain.com. I guess this is the P2PKH-type address.

I tried this:

NetworkParameters params = MainNetParams.get();
ECKey key = ECKey.fromPrivate(privkey);
String address = LegacyAddress.fromKey(params, key).toString();
Log.d("address",address);

Which gives me this address:

1LoVGDgRs9hTfTNJNuXKSpywcbdvwRXpmK

But the address is different.

4
  • 1
    Do you mean 1... address, that is compressed or uncompressed, or a SegWit native (Bech32) address or SegWit nested legacy compatible 3... adress. Or even an m-of-n multisig address? If you just want an address, this should help: gobittest.appspot.com/Address The link to the answer is in your question, how can we help? Where are you stuck?
    – MCCCS
    Dec 20, 2020 at 13:13
  • @MCCCS I added more details, but I want to do it programatically in Java Dec 20, 2020 at 14:31
  • 1
    Does this help: github.com/bitcoinj/bitcoinj/blob/master/examples/src/main/java/… ?
    – MCCCS
    Dec 20, 2020 at 14:37
  • @MCCCS I tried it but it gives a different address with a different balance, so I assume it's an unrelated address. I added my code above Dec 20, 2020 at 14:48

1 Answer 1

3

Both "1LoVG.." and the other one are valid legacy (1...) addresses made using the same private key. They are different since one of them uses the compressed public key (you might want to look this up) while 1GAehh7 uses the uncompressed public key. Always use the same compress-format for an address, and it's advisable to create new addresses in compressed format since it saves bytes and transaction fees.

To make the uncompressed-public-keyed address (compressed is the default in BitcoinJ):

ECKey key = ECKey.fromPrivate(privkey);
key = key.decompress();
String address = LegacyAddress.fromKey(params, key).toString();

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.