Author

Topic: How to get an address from a raw hexadecimal key using Java (Read 167 times)

member
Activity: 96
Merit: 36
// without the bitcoinj library


 Smiley
thank you very much. I don't know if this works. Because overnight I wrote my own version Grin  But thanks)
full member
Activity: 161
Merit: 168
// without the bitcoinj library


Code:
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
import org.bouncycastle.math.ec.ECPoint;




public class Test
{



public static void main(String args[])
{
System.out.println(wizard("SALAMANDRA"));
}



private static String wizard(String input)
{        
byte[] secret = hexStringToByteArray(getHashSHA256(input));
return createAddress(secret, true);
}



private static String createAddress(byte[] secret, boolean compressed)
{
byte[] pub = getPublicKey(secret, compressed);
byte[] h160 = getHashRIPEMD160(getHashSHA256(pub));
return getBase58Address(h160);
}

   
private static String getBase58Address(byte[] hash160)
{
String h160 = byteArrayToHexString(hash160);
String prefix = "00";
String addr   = prefix+h160;
String hash   = getHashSHA256_from_HexString(getHashSHA256_from_HexString(addr));
return hexStringToBase58(addr+hash.substring(0,8));
}


public static byte[] getPublicKey(byte[] privateKey, boolean compressed)
{
ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp256k1");
ECPoint pointQ = spec.getG().multiply(new BigInteger(1, privateKey));
return pointQ.getEncoded(compressed);
}


public static String getHashSHA256_from_HexString(String str)
{
 byte[] b = getHashSHA256(hexStringToByteArray(str));
 return byteArrayToHexString(b);
}


private static byte[] getHashSHA256(byte[] b)
{
MessageDigest sha;
try
{
sha = MessageDigest.getInstance("SHA-256");
return sha.digest(b);
}
catch (NoSuchAlgorithmException e)
{
System.out.println("Error getHashSHA256()");
System.out.println(e.getMessage());
return null;
}
}


private static String getHashSHA256(String str)
{
try
{
byte[] b = getHashSHA256((str).getBytes("UTF-8"));
return byteArrayToHexString(b);
}
catch (UnsupportedEncodingException e) {
System.out.println("Error getHashSHA256()");
System.out.println(e.getMessage());
return "-1";
}

}


public static byte[] getHashRIPEMD160(byte[] b)
{
 RIPEMD160Digest ripemd = new RIPEMD160Digest();
      ripemd.update (b, 0, b.length);
      byte[] hash160 = new byte[ripemd.getDigestSize()];
      ripemd.doFinal (hash160, 0);
 return hash160;
}


private static byte[] hexStringToByteArray(String hex)
{                                                                    
if((hex.length()%2)==1) throw new IllegalArgumentException("Ungerade String-Zeichenfolge!");
int l = hex.length();
byte[] data = new byte[l/2];
for (int i = 0; i < l; i += 2)
{
data[i/2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i+1), 16));
}
return data;
}


public static String byteArrayToHexString(byte[] a)
{
StringBuilder sb = new StringBuilder(a.length * 2);
for(byte b: a)
sb.append(String.format("%02x", b));
return sb.toString();
}



public static String hexStringToBase58(String str)
{
   byte[] b = hexStringToByteArray(str);
   char[] c = toBase58(b);
   return String.valueOf(c);
}


public static char[] toBase58(byte[] k)  
{
     char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
     BigInteger z = new BigInteger(1,k);
     BigInteger z1;
     BigInteger rest = new BigInteger("0");
     BigInteger base = new BigInteger("58");
     int laenge=0;
     z1=z;          
     for(double i=1; i>0;)
     {
        z1 = z1.divide(base);
        i  = z1.doubleValue();
        laenge++;
     }          
     char[] Key = new char[laenge];            
     for(int i=laenge; i>0; i--)
     {
          rest = z.mod(base);
          Key[i-1] = ALPHABET[rest.intValue()];
          z =  z.divide(base);
     }
     int nullLänge = 0;
     for(int i=0; k[i]==0 && i     char[] EINS   = {'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'};
     char[] KeyOut = new char[nullLänge + Key.length];
     System.arraycopy(Key, 0, KeyOut, nullLänge, Key.length);
     System.arraycopy(EINS, 0, KeyOut, 0, nullLänge);
     return KeyOut;
}
}


Output: 1MBdPCzhggNVaLC1tYcFRWopVM4n4wMAYH
member
Activity: 180
Merit: 38
That key can represent many addresses.
You have to specify the compression type, false for uncompressed pubkey and true for compressed pubkey.
After that you will have a keypair with either a uncompressed or a compressed public key which you then convert into an address by various means depending of the address you need.
member
Activity: 96
Merit: 36
I know a little Java, and it looks like you have the SHA256 of a public key already. You need to find a RIPEMD160 package and a Base58Check package in order to make the address. Then supply the getBytes() byte array of the SHA256 to the RIPEMD160 package, then pass the byte result of that to the Base58Check library.

Rosetta Code has a good Java example snippet for doing both of these (ripemd160 - this uses BouncyCastle -,  base58check)

Which version of Java are you using? They should both work on Java 11.


I did just that.  https://bitcointalk.org/index.php?topic=5321980.new#new  but it doesn't work with such a key.
This only works if WIF is input. not  hexadecimal key. As a result, I get the wrong address format that I need ((
legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
I know a little Java, and it looks like you have the SHA256 of a public key already. You need to find a RIPEMD160 package and a Base58Check package in order to make the address. Then supply the getBytes() byte array of the SHA256 to the RIPEMD160 package, then pass the byte result of that to the Base58Check library.

Rosetta Code has a good Java example snippet for doing both of these (ripemd160 - this uses BouncyCastle -,  base58check)

Which version of Java are you using? They should both work on Java 11.
member
Activity: 96
Merit: 36
there is a key like this 1d89fc086b5f387f7db22703c222834ea96ace5ef2d8cb5ac687906eaac014cc

obtained with String key = org.apache.commons.codec.digest.DigestUtils.sha256Hex(SomeString);

I tried to use the bitcoinj library, but there you can submit either hash160 or WIF to the input, or I don't understand something.

 byte[] hash160=key.getBytes()  is not working
and if i put WIF i get another adress.

grateful for any help.
Jump to: