1

Here some java code. What i need to change to get compressed public address in getPublicAddress?

import com.google.bitcoin.core.Address;
import com.google.bitcoin.core.ECKey;
import com.google.bitcoin.params.MainNetParams;
import org.spongycastle.crypto.digests.RIPEMD160Digest;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtil {

    public static byte[] RIPEMD160(byte[] input) {
        RIPEMD160Digest d = new RIPEMD160Digest();

        d.update(input, 0, input.length);

        byte[] o = new byte[d.getDigestSize()];

        d.doFinal(o, 0);

        return o;
    }

    public static byte[] SHA256(byte[] input) {
        byte[] hash = null;

        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(input);
            hash = md.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return hash;
    }

    public static ECKey createAddress(byte[] secret) {
        byte[] hash = SHA256(secret);

        ECKey key = new ECKey(hash, null);

        return key;
    }

    public static String getPublicAddress(String input) {
        ECKey address = createAddress(input.getBytes());

        byte[] hash160 = RIPEMD160(SHA256(address.getPubKey()));

        Address addr = new Address(MainNetParams.get(), hash160);

        return addr.toString();
    }

}
  • this question is a bit "non logic". Normally one does not convert between uncompressed and compressed pubkeys. The danger is in creating unspendable outputs. Instead, one would derive compressed or uncompressed pubkeys from the corresponding private keys (here people sometimes talk about compressed or uncompressed WIF keys). On WIF keys there was just a thread here: bitcoin.stackexchange.com/questions/68065/… – pebwindkraft Jan 10 '18 at 13:26
  • Why non logic? That is the question - to derive compressed pubkey (in getPublicAddress?) from corresponding private key from createAddress. – krab Jan 10 '18 at 14:51
  • Need to get "new Eckey" with compressed marker, then compressed WIF public key in getPublicAddress. – krab Jan 10 '18 at 15:02
  • ah, didn't get this from the question :-) To get to the compressed pubkey you need to have a "compressed WIF" key. This is derived from private key, with a hex "01" at the end, and base58 encoded. I am not programming in Java, and don't have the ECKey library details, especially as I don't see any base58 encoding - so ECKey createAddress() returns your key, which I assume is the 32 Bytes hex key format. Then the function address.getPubKey() seems to derive a standard pubkey (uncompressed). Maybe the library has address.getCompressedPubKey() ? – pebwindkraft Jan 10 '18 at 21:02
  • no, bullshit... did some re-reading. The WIF-c key is only needed to "advise" the wallet, to create privkeys in compressed format. At the end, when uncompressed pubkey is created, it's hex representation is "04" + "x" + "y". A compressed pubkey is then a "02" (for even y) or "03" (uneven) + "x". I leave it to the JAVA experts of these libraries, where to find the correct code ... – pebwindkraft Jan 10 '18 at 21:56
1
Address address = new Address(params, dk.decompress().getPubKeyHash());

where dk is the DeterministicKey

or

key.decompress().getPubKey();

where key is the ECKey

1

In case some one want to implements compression in their code:

private static final String EVEN = "02";
private static final String ODD = "03";

public String compressPublicKey(String toCompress) {
    if (Integer.parseInt(toCompress.substring(128, 130), 16) % 2 == 0)
        return  EVEN + toCompress.substring(2, 66);
    return ODD + toCompress.substring(2, 66);
} 
0
private static final String EVEN = "02";
private static final String ODD = "03";

public String compressPublicKey(String toCompress) {
    if (Integer.parseInt(toCompress.substring(128, 130), 16) % 2 == 0)
        return  EVEN + toCompress.substring(2, 66);
    return ODD + toCompress.substring(2, 66);
} 1
โหวต
Address address = new Address(params, dk.decompress().getPubKeyHash())key.decompress().getPubKey();

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.