Merging differences

This commit is contained in:
Nelson R. Perez 2016-11-23 14:50:21 -05:00
commit 95f19b7b74
4 changed files with 179 additions and 36 deletions

View file

@ -2,9 +2,12 @@ package com.luminiasoft.bitshares;
import java.util.Arrays;
import java.util.Base64;
import org.bitcoinj.core.Base58;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.crypto.HDKeyDerivation;
import org.bitcoinj.crypto.MnemonicCode;
import org.spongycastle.crypto.digests.RIPEMD160Digest;
import org.spongycastle.crypto.digests.SHA512Digest;
/**
*
@ -12,7 +15,7 @@ import org.bitcoinj.crypto.MnemonicCode;
*/
public class BIP39 {
private ECKey mPrivateKey;
private final ECKey mPrivateKey;
public BIP39(String words, String passphrase) {
@ -21,8 +24,56 @@ public class BIP39 {
}
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() {
return mPrivateKey;
}
}

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

@ -1,6 +1,5 @@
package com.luminiasoft.bitshares;
import com.google.common.primitives.Bytes;
import com.google.common.primitives.UnsignedLong;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@ -525,7 +524,8 @@ public class Test {
brainKey = new BrainKey(Main.BRAIN_KEY, 0);
}
ECKey key = brainKey.getPrivateKey();
System.out.println("Private key :"+Util.bytesToHex(key.getSecretBytes()));
System.out.println("Private key");
System.out.println(Util.bytesToHex(key.getSecretBytes()));
String wif = key.getPrivateKeyAsWiF(NetworkParameters.fromID(NetworkParameters.ID_MAINNET));
System.out.println("wif compressed: " + wif);
String wif2 = key.decompress().getPrivateKeyAsWiF(NetworkParameters.fromID(NetworkParameters.ID_MAINNET));
@ -536,18 +536,38 @@ public class Test {
byte[] pubKey2 = key.getPubKey();
System.out.println("compressed public key: " + Base58.encode(pubKey2));
System.out.println("pub key decompressed : "+Util.bytesToHex(pubKey1));
System.out.println("pub key compressed : "+Util.bytesToHex(pubKey2));
System.out.println("pub key compressed : " + Util.bytesToHex(pubKey1));
System.out.println("pub key uncompressed : " + Util.bytesToHex(pubKey2));
// Generating address
byte[] checksum = new byte[160 / 8];
byte[] pubKey3 = key.getPubKeyPoint().getEncoded(true);
System.out.println("pub key compressed : " + Base58.encode(pubKey3));
// Address generation test
RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
ripemd160Digest.reset();
ripemd160Digest.update(pubKey2, 0, pubKey2.length);
ripemd160Digest.doFinal(checksum, 0);
byte[] pubKeyChecksummed = Bytes.concat(pubKey2, Arrays.copyOfRange(checksum, 0, 4));
String address = "BTS" + Base58.encode(pubKeyChecksummed);
System.out.println("Address : "+address);
SHA512Digest sha512Digest = new SHA512Digest();
sha512Digest.update(pubKey2, 0, pubKey2.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);
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());
Address address = new Address(key);
System.out.println("Block explorer's address: "+address);
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException. Msg: " + e.getMessage());
@ -556,6 +576,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, "");
}