novitski / bitcoinj

Automatically exported from code.google.com/p/bitcoinj
Apache License 2.0
0 stars 0 forks source link

Private Key Address Format Inconsistent #385

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm not sure this is a bitcoinj problem vs a bitcoin world in general problem.

Lets say you go to bitaddress.org and generate a paper wallet.  As an example, 
I am given:
Address:
1L5y7XiyfEXzUsECo6UmdVRaXn5jFodC58
Private key:
5Jpc8vPN46yMAKWwqTxooMzmUUqxNrcj6uXJTUjZ5f34ZGbvXMF

If I take that private key over to 'Wallet Details' I can see that there is 
also a compressed address that corresponds to the same key:
1EyohuP1khgPYLjbRa5tYD9VVmfpJyaTVV

All well and good.  Lets see what bitcoinj does with it:
$ ./wallet-tool --wallet=test.wallet --chain=test.chain --peers=localhost 
--action=CREATE

$ ./wallet-tool --wallet=test.wallet --chain=test.chain --peers=localhost 
--action=ADD_KEY --privkey=5Jpc8vPN46yMAKWwqTxooMzmUUqxNrcj6uXJTUjZ5f34ZGbvXMF
addr:1CzoBzTRbrpb5nBhwcHtvJHjTTGa2Jkjct 
pub:04fd89b128ff02928ca93777a042787aac42d63f528be92af7a8bc72abebf33643c762a8d8fa
e210324eba301ae52460783d54b60f4bfd5a4a7966f514feb93c40

Here we see a completely different address from the two above.

In my experimentation, I've found that if I put in what bitaddress.org is 
calling the "Private Key WIF (compressed, 52 characters base58)" in this case 
L1f85xdhvhMhz8jB2v1KZdfyJHu5RTzgq4pk3sQpbYt6hAoWLTtc it gives me the compressed 
address at least:
$ ./wallet-tool --wallet=test.wallet --chain=test.chain --peers=localhost 
--action=ADD_KEY --privkey=L1f85xdhvhMhz8jB2v1KZdfyJHu5RTzgq4pk3sQpbYt6hAoWLTtc
addr:1EyohuP1khgPYLjbRa5tYD9VVmfpJyaTVV 
pub:03c554e014828a1d540ad341923fff763a973ae2c8965ff902638bafac2436135f

How do I import this key to use the uncompressed address?

What would happen if I sent bitcoins to the address 
'1CzoBzTRbrpb5nBhwcHtvJHjTTGa2Jkjct' from the above import?  Would that work?

Original issue reported on code.google.com by fireduck@gmail.com on 11 Apr 2013 at 9:20

GoogleCodeExporter commented 9 years ago
Right, there's a lot of inconsistency around how private keys are represented.

I have no idea what "Private Key WIF" means.

Wallet-Tool currently assumes all pubkeys are compressed. To get the 
uncompressed form, you need to write your own code and use

 public ECKey(BigInteger privKey, byte[] pubKey, boolean compressed) {}

Set privKey to be the private key, pubKey to null, compressed to false.

Yes, I know the ECKey API is a mess. Will fix one day.

Once you convert that ECKey to an address, it should match up. Does that help?

Original comment by hearn@google.com on 12 Apr 2013 at 9:35

GoogleCodeExporter commented 9 years ago
So how to generate uncompressed private key string which can be inserted to, 
say, armory?

Original comment by valle.ke...@gmail.com on 19 Apr 2013 at 6:11

GoogleCodeExporter commented 9 years ago
Armory should just be fixed to understand compressed keys. Maybe we'd make 
compression optional in future bitcoinj releases but there isn't really any 
advantage to doing so, other software should just be upgraded to handle it.

Original comment by hearn@google.com on 22 Apr 2013 at 11:33

GoogleCodeExporter commented 9 years ago

Original comment by hearn@google.com on 17 Jun 2013 at 1:43

GoogleCodeExporter commented 9 years ago
all it needs is to remove the version byte from the Base58 decoded array before 
creating the key.

import java.util.Arrays;
import java.util.Scanner;

import org.bitcoinj.core.AddressFormatException;
import org.bitcoinj.core.Base58;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.params.MainNetParams;

public class MyClass {

    /**
     * @param args
     * @throws AddressFormatException 
     */
    public static void main(String[] args) throws AddressFormatException {
        Scanner in = new Scanner(System.in);
        System.out.println("enter private key:");
        byte[] g = Arrays.copyOfRange(Base58.decodeChecked(in.nextLine()), 1, 33);
        ECKey priv = ECKey.fromPrivate(g, false);
        String myAddr = priv.toAddress(MainNetParams.get()).toString();
        System.out.println(myAddr);
    }

}

Original comment by oha...@gmail.com on 25 Jan 2015 at 12:31