address calculated

This commit is contained in:
Henry Varona 2016-11-23 15:07:54 -04:30
parent ec4f0704d6
commit 88d6f7640c
3 changed files with 799 additions and 715 deletions

View file

@ -10,11 +10,24 @@ import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Base64;
import org.bitcoinj.core.Base58;
import org.bitcoinj.core.BitcoinSerializer;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.StoredBlock;
import org.bitcoinj.core.VerificationException;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.BlockStoreException;
import org.bitcoinj.utils.MonetaryFormat;
import org.spongycastle.crypto.digests.RIPEMD160Digest;
import org.spongycastle.crypto.digests.SHA512Digest;
/**
* Class used to encapsulate all BrainKey-related operations.
*/
public class BrainKey {
// The size of the word dictionary
public static final int DICT_WORD_COUNT = 49744;
@ -25,7 +38,9 @@ public class BrainKey {
/**
* Method that will generate a random brain key
* @param words The list of words from the graphene specification dictionary.
*
* @param words The list of words from the graphene specification
* dictionary.
* @return A random sequence of words
*/
public static String suggest(String words) {
@ -46,8 +61,9 @@ public class BrainKey {
}
/**
* BrainKey constructor that takes as argument a specific brain key word sequence and generates the
* private key and address from that.
* BrainKey constructor that takes as argument a specific brain key word
* sequence and generates the private key and address from that.
*
* @param words The brain key specifying the private key
* @param sequence Sequence number
*/
@ -59,6 +75,7 @@ public class BrainKey {
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] result = sha256.digest(bytes);
mPrivateKey = ECKey.fromPrivate(result);
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgotithmException. Msg: " + e.getMessage());
} catch (UnsupportedEncodingException e) {
@ -66,8 +83,52 @@ public class BrainKey {
}
}
public String getPublicKey() {
return Base64.getEncoder().encodeToString(mPrivateKey.getPubKey());
public String getUncompressedAddress() {
RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
SHA512Digest sha512Digest = new SHA512Digest();
sha512Digest.update(mPrivateKey.decompress().getPubKey(), 0, mPrivateKey.decompress().getPubKey().length);
byte[] intermediate = new byte[512 / 8];
sha512Digest.doFinal(intermediate, 0);
ripemd160Digest.update(intermediate, 0, intermediate.length);
byte[] output = new byte[160 / 8];
ripemd160Digest.doFinal(output, 0);
String encoded = Base58.encode(output);
byte[] checksum = new byte[(160 / 8) + 4];
System.arraycopy(calculateChecksum(output), 0, checksum, checksum.length - 4, 4);
System.arraycopy(output, 0, checksum, 0, output.length);
return ("BTS" + Base58.encode(checksum));
}
public String getAddress() {
RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
SHA512Digest sha512Digest = new SHA512Digest();
sha512Digest.update(mPrivateKey.getPubKey(), 0, mPrivateKey.getPubKey().length);
byte[] intermediate = new byte[512 / 8];
sha512Digest.doFinal(intermediate, 0);
ripemd160Digest.update(intermediate, 0, intermediate.length);
byte[] output = new byte[160 / 8];
ripemd160Digest.doFinal(output, 0);
String encoded = Base58.encode(output);
byte[] checksum = new byte[(160 / 8) + 4];
System.arraycopy(calculateChecksum(output), 0, checksum, checksum.length - 4, 4);
System.arraycopy(output, 0, checksum, 0, output.length);
return ("BTS" + Base58.encode(checksum));
}
public byte[] calculateChecksum(byte[] input) {
byte[] answer = new byte[4];
RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
ripemd160Digest.update(input, 0, input.length);
byte[] output = new byte[160 / 8];
ripemd160Digest.doFinal(output, 0);
System.arraycopy(output, 0, answer, 0, 4);
return answer;
}
public byte[] getPublicKey() {
return mPrivateKey.getPubKey();
}
public ECKey getPrivateKey() {

View file

@ -7,6 +7,7 @@ import java.io.IOException;
public class Main {
// Brain key from Nelson's app referencing the bilthon-83 account
public static final String BRAIN_KEY = "PUMPER ISOTOME SERE STAINER CLINGER MOONLIT CHAETA UPBRIM AEDILIC BERTHER NIT SHAP SAID SHADING JUNCOUS CHOUGH";
//public static final String BRAIN_KEY = "TWIXT SERMO TRILLI AUDIO PARDED PLUMET BIWA REHUNG MAUDLE VALVULA OUTBURN FEWNESS ALIENER UNTRACE PRICH TROKER";
public static final String BIP39_KEY = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";

View file

@ -545,7 +545,7 @@ public class Test {
// Address generation test
RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
SHA512Digest sha512Digest = new SHA512Digest();
sha512Digest.update(pubKey1, 0, pubKey1.length);
sha512Digest.update(pubKey2, 0, pubKey2.length);
byte[] intermediate = new byte[512 / 8];
sha512Digest.doFinal(intermediate, 0);
ripemd160Digest.update(intermediate, 0, intermediate.length);
@ -554,6 +554,18 @@ public class Test {
System.out.println("output after : " + Util.bytesToHex(output));
String encoded = Base58.encode(output);
System.out.println("base 58: " + encoded);
byte[] checksum = new byte[(160 / 8) + 4];
System.arraycopy(calculateChecksum(output), 0, checksum, checksum.length - 4, 4);
System.arraycopy(output, 0, checksum, 0, output.length);
System.out.println("BTS" + Base58.encode(checksum));
System.out.println("Compress Adress : " + brainKey.getAddress());
System.out.println("Uncompress Adress : " + brainKey.getUncompressedAddress());
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException. Msg: " + e.getMessage());
} catch (IOException e) {
@ -561,6 +573,16 @@ public class Test {
}
}
public byte[] calculateChecksum(byte[] input) {
byte[] answer = new byte[4];
RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
ripemd160Digest.update(input, 0, input.length);
byte[] output = new byte[160 / 8];
ripemd160Digest.doFinal(output, 0);
System.arraycopy(output, 0, answer, 0, 4);
return answer;
}
public void testBip39Opertion() {
BIP39 bip39 = new BIP39(Main.BIP39_KEY, "");
}