1
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: https://bitcoin.stackexchange.com/questions/68065/private-key-to-wif-compressed-which-one-is-correct
– pebwindkraft – 2018-01-10T13:26:02.523Why non logic? That is the question - to derive compressed pubkey (in getPublicAddress?) from corresponding private key from createAddress. – krab – 2018-01-10T14:51:58.890
Need to get "new Eckey" with compressed marker, then compressed WIF public key in getPublicAddress. – krab – 2018-01-10T15:02:19.117
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 – 2018-01-10T21:02:54.030
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 – 2018-01-10T21:56:42.903