address calculated BIP39

This commit is contained in:
Henry Varona 2016-11-23 15:08:50 -04:30
parent 88d6f7640c
commit 8de4e42a07

View file

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