Added BIP39 ECKey creation
This commit is contained in:
parent
a611f9b388
commit
51bed577aa
3 changed files with 605 additions and 573 deletions
23
src/main/java/com/luminiasoft/bitshares/BIP39.java
Normal file
23
src/main/java/com/luminiasoft/bitshares/BIP39.java
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package com.luminiasoft.bitshares;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import org.bitcoinj.core.ECKey;
|
||||||
|
import org.bitcoinj.crypto.HDKeyDerivation;
|
||||||
|
import org.bitcoinj.crypto.MnemonicCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author hvarona
|
||||||
|
*/
|
||||||
|
public class BIP39 {
|
||||||
|
|
||||||
|
private ECKey mPrivateKey;
|
||||||
|
|
||||||
|
public BIP39(String words, String passphrase) {
|
||||||
|
|
||||||
|
byte[] seed = MnemonicCode.toSeed(Arrays.asList(words.split(" ")), passphrase);
|
||||||
|
mPrivateKey = HDKeyDerivation.createMasterPrivateKey(seed);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ import java.io.IOException;
|
||||||
public class Main {
|
public class Main {
|
||||||
// Brain key from Nelson's app referencing the bilthon-83 account
|
// 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 = "PUMPER ISOTOME SERE STAINER CLINGER MOONLIT CHAETA UPBRIM AEDILIC BERTHER NIT SHAP SAID SHADING JUNCOUS CHOUGH";
|
||||||
|
public static final String BIP39_KEY = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
|
||||||
|
|
||||||
// WIF from Nelson's app referencing the bilthon-83 account
|
// WIF from Nelson's app referencing the bilthon-83 account
|
||||||
public static final String WIF = "5J96pne45qWM1WpektoeazN6k9Mt93jQ7LyueRxFfEMTiy6yxjM";
|
public static final String WIF = "5J96pne45qWM1WpektoeazN6k9Mt93jQ7LyueRxFfEMTiy6yxjM";
|
||||||
|
@ -60,5 +61,6 @@ public class Main {
|
||||||
// test.testRandomNumberGeneration();
|
// test.testRandomNumberGeneration();
|
||||||
|
|
||||||
test.testBrainKeyOperations();
|
test.testBrainKeyOperations();
|
||||||
|
test.testBip39Opertion();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import java.util.*;
|
||||||
* Created by nelson on 11/9/16.
|
* Created by nelson on 11/9/16.
|
||||||
*/
|
*/
|
||||||
public class Test {
|
public class Test {
|
||||||
|
|
||||||
public static final String WITNESS_URL = "ws://api.devling.xyz:8088";
|
public static final String WITNESS_URL = "ws://api.devling.xyz:8088";
|
||||||
private Transaction transaction;
|
private Transaction transaction;
|
||||||
|
|
||||||
|
@ -33,34 +34,33 @@ public class Test {
|
||||||
return transaction;
|
return transaction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private WitnessResponseListener mListener = new WitnessResponseListener() {
|
private WitnessResponseListener mListener = new WitnessResponseListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(WitnessResponse response) {
|
public void onSuccess(WitnessResponse response) {
|
||||||
if(response.result.getClass() == AccountProperties.class){
|
if (response.result.getClass() == AccountProperties.class) {
|
||||||
AccountProperties accountProperties = (AccountProperties) response.result;
|
AccountProperties accountProperties = (AccountProperties) response.result;
|
||||||
System.out.println("Got account properties");
|
System.out.println("Got account properties");
|
||||||
System.out.println("id: "+accountProperties.id);
|
System.out.println("id: " + accountProperties.id);
|
||||||
}else if(response.result.getClass() == ArrayList.class){
|
} else if (response.result.getClass() == ArrayList.class) {
|
||||||
List l = (List) response.result;
|
List l = (List) response.result;
|
||||||
if(l.size() > 0){
|
if (l.size() > 0) {
|
||||||
if(l.get(0).getClass() == AssetAmount.class){
|
if (l.get(0).getClass() == AssetAmount.class) {
|
||||||
AssetAmount assetAmount = (AssetAmount) l.get(0);
|
AssetAmount assetAmount = (AssetAmount) l.get(0);
|
||||||
System.out.println("Got fee");
|
System.out.println("Got fee");
|
||||||
System.out.println("amount: "+assetAmount.getAmount()+", asset id: "+assetAmount.getAsset().getObjectId());
|
System.out.println("amount: " + assetAmount.getAmount() + ", asset id: " + assetAmount.getAsset().getObjectId());
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
System.out.println("Got empty list!");
|
System.out.println("Got empty list!");
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
System.out.println("Got other: "+response.result.getClass());
|
System.out.println("Got other: " + response.result.getClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(BaseResponse.Error error) {
|
public void onError(BaseResponse.Error error) {
|
||||||
System.out.println("onError. message: "+error.message);
|
System.out.println("onError. message: " + error.message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -115,9 +115,10 @@ public class Test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (recId == -1)
|
if (recId == -1) {
|
||||||
throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
|
throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
int headerByte = recId + 27 + (sk.isCompressed() ? 4 : 0);
|
int headerByte = recId + 27 + (sk.isCompressed() ? 4 : 0);
|
||||||
byte[] sigData = new byte[65]; // 1 header + 32 bytes for R + 32 bytes for S
|
byte[] sigData = new byte[65]; // 1 header + 32 bytes for R + 32 bytes for S
|
||||||
sigData[0] = (byte) headerByte;
|
sigData[0] = (byte) headerByte;
|
||||||
|
@ -207,7 +208,8 @@ public class Test {
|
||||||
}
|
}
|
||||||
if (baseResponse.id == 2) {
|
if (baseResponse.id == 2) {
|
||||||
String payload = String.format(getDynamicParameters, 3);
|
String payload = String.format(getDynamicParameters, 3);
|
||||||
Type ApiIdResponse = new TypeToken<WitnessResponse<Integer>>() {}.getType();
|
Type ApiIdResponse = new TypeToken<WitnessResponse<Integer>>() {
|
||||||
|
}.getType();
|
||||||
WitnessResponse<Integer> witnessResponse = gson.fromJson(response, ApiIdResponse);
|
WitnessResponse<Integer> witnessResponse = gson.fromJson(response, ApiIdResponse);
|
||||||
networkBroadcastApiId = witnessResponse.result.intValue();
|
networkBroadcastApiId = witnessResponse.result.intValue();
|
||||||
System.out.println(">>");
|
System.out.println(">>");
|
||||||
|
@ -325,10 +327,11 @@ public class Test {
|
||||||
System.out.println(call);
|
System.out.println(call);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNetworkBroadcastDeserialization(){
|
public void testNetworkBroadcastDeserialization() {
|
||||||
String response = "{\"id\":2,\"result\":2}";
|
String response = "{\"id\":2,\"result\":2}";
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
Type ApiIdResponse = new TypeToken<WitnessResponse<Integer>>() {}.getType();
|
Type ApiIdResponse = new TypeToken<WitnessResponse<Integer>>() {
|
||||||
|
}.getType();
|
||||||
WitnessResponse<Integer> witnessResponse = gson.fromJson(response, ApiIdResponse);
|
WitnessResponse<Integer> witnessResponse = gson.fromJson(response, ApiIdResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,16 +343,17 @@ public class Test {
|
||||||
|
|
||||||
public void testRequiredFeesResponse() {
|
public void testRequiredFeesResponse() {
|
||||||
String response = "{\"id\":1,\"result\":[{\"amount\":264174,\"asset_id\":\"1.3.0\"}]}";
|
String response = "{\"id\":1,\"result\":[{\"amount\":264174,\"asset_id\":\"1.3.0\"}]}";
|
||||||
Type AccountLookupResponse = new TypeToken<WitnessResponse<List<AssetAmount>>>() {}.getType();
|
Type AccountLookupResponse = new TypeToken<WitnessResponse<List<AssetAmount>>>() {
|
||||||
|
}.getType();
|
||||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||||
gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer());
|
gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer());
|
||||||
WitnessResponse<List<AssetAmount>> witnessResponse = gsonBuilder.create().fromJson(response, AccountLookupResponse);
|
WitnessResponse<List<AssetAmount>> witnessResponse = gsonBuilder.create().fromJson(response, AccountLookupResponse);
|
||||||
for(AssetAmount assetAmount : witnessResponse.result){
|
for (AssetAmount assetAmount : witnessResponse.result) {
|
||||||
System.out.println("asset : "+assetAmount.toJsonString());
|
System.out.println("asset : " + assetAmount.toJsonString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testTransactionBroadcastSequence(){
|
public void testTransactionBroadcastSequence() {
|
||||||
String url = "ws://api.devling.xyz:8088";
|
String url = "ws://api.devling.xyz:8088";
|
||||||
WitnessResponseListener listener = new WitnessResponseListener() {
|
WitnessResponseListener listener = new WitnessResponseListener() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -364,7 +368,7 @@ public class Test {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
try{
|
try {
|
||||||
Transaction transaction = new TransferTransactionBuilder()
|
Transaction transaction = new TransferTransactionBuilder()
|
||||||
.setSource(new UserAccount("1.2.138632"))
|
.setSource(new UserAccount("1.2.138632"))
|
||||||
.setDestination(new UserAccount("1.2.129848"))
|
.setDestination(new UserAccount("1.2.129848"))
|
||||||
|
@ -384,46 +388,47 @@ public class Test {
|
||||||
mWebSocket.addListener(new TransactionBroadcastSequence(transaction, listener));
|
mWebSocket.addListener(new TransactionBroadcastSequence(transaction, listener));
|
||||||
mWebSocket.connect();
|
mWebSocket.connect();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("IOException. Msg: "+e.getMessage());
|
System.out.println("IOException. Msg: " + e.getMessage());
|
||||||
} catch (WebSocketException e) {
|
} catch (WebSocketException e) {
|
||||||
System.out.println("WebSocketException. Msg: "+e.getMessage());
|
System.out.println("WebSocketException. Msg: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}catch(MalformedTransactionException e){
|
} catch (MalformedTransactionException e) {
|
||||||
System.out.println("MalformedTransactionException. Msg: "+e.getMessage());
|
System.out.println("MalformedTransactionException. Msg: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAccountLookupDeserialization(){
|
public void testAccountLookupDeserialization() {
|
||||||
String response = "{\"id\":1,\"result\":[[\"ken\",\"1.2.3111\"],[\"ken-1\",\"1.2.101491\"],[\"ken-k\",\"1.2.108646\"]]}";
|
String response = "{\"id\":1,\"result\":[[\"ken\",\"1.2.3111\"],[\"ken-1\",\"1.2.101491\"],[\"ken-k\",\"1.2.108646\"]]}";
|
||||||
Type AccountLookupResponse = new TypeToken<WitnessResponse<List<List<String>>>>() {}.getType();
|
Type AccountLookupResponse = new TypeToken<WitnessResponse<List<List<String>>>>() {
|
||||||
|
}.getType();
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
WitnessResponse<List<List<String>>> witnessResponse = gson.fromJson(response, AccountLookupResponse);
|
WitnessResponse<List<List<String>>> witnessResponse = gson.fromJson(response, AccountLookupResponse);
|
||||||
for(int i = 0; i < witnessResponse.result.size(); i++){
|
for (int i = 0; i < witnessResponse.result.size(); i++) {
|
||||||
System.out.println("suggested name: "+witnessResponse.result.get(i).get(0));
|
System.out.println("suggested name: " + witnessResponse.result.get(i).get(0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPrivateKeyManipulations(){
|
public void testPrivateKeyManipulations() {
|
||||||
ECKey privateKey = DumpedPrivateKey.fromBase58(null, Main.WIF).getKey();
|
ECKey privateKey = DumpedPrivateKey.fromBase58(null, Main.WIF).getKey();
|
||||||
System.out.println("private key..............: "+Util.bytesToHex(privateKey.getSecretBytes()));
|
System.out.println("private key..............: " + Util.bytesToHex(privateKey.getSecretBytes()));
|
||||||
System.out.println("public key uncompressed..: "+Util.bytesToHex(privateKey.getPubKey()));
|
System.out.println("public key uncompressed..: " + Util.bytesToHex(privateKey.getPubKey()));
|
||||||
System.out.println("public key compressed....: "+Util.bytesToHex(privateKey.getPubKeyPoint().getEncoded(true)));
|
System.out.println("public key compressed....: " + Util.bytesToHex(privateKey.getPubKeyPoint().getEncoded(true)));
|
||||||
System.out.println("base58...................: "+Base58.encode(privateKey.getPubKeyPoint().getEncoded(true)));
|
System.out.println("base58...................: " + Base58.encode(privateKey.getPubKeyPoint().getEncoded(true)));
|
||||||
System.out.println("base58...................: "+Base58.encode(privateKey.getPubKey()));
|
System.out.println("base58...................: " + Base58.encode(privateKey.getPubKey()));
|
||||||
String brainKeyWords = "PUMPER ISOTOME SERE STAINER CLINGER MOONLIT CHAETA UPBRIM AEDILIC BERTHER NIT SHAP SAID SHADING JUNCOUS CHOUGH";
|
String brainKeyWords = "PUMPER ISOTOME SERE STAINER CLINGER MOONLIT CHAETA UPBRIM AEDILIC BERTHER NIT SHAP SAID SHADING JUNCOUS CHOUGH";
|
||||||
BrainKey brainKey = new BrainKey(brainKeyWords, 0);
|
BrainKey brainKey = new BrainKey(brainKeyWords, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetAccountByName(){
|
public void testGetAccountByName() {
|
||||||
try {
|
try {
|
||||||
WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(5000);
|
WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(5000);
|
||||||
WebSocket mWebSocket = factory.createSocket(WITNESS_URL);
|
WebSocket mWebSocket = factory.createSocket(WITNESS_URL);
|
||||||
mWebSocket.addListener(new GetAccountByName("bilthon-83", mListener));
|
mWebSocket.addListener(new GetAccountByName("bilthon-83", mListener));
|
||||||
mWebSocket.connect();
|
mWebSocket.connect();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("IOException. Msg: "+e.getMessage());
|
System.out.println("IOException. Msg: " + e.getMessage());
|
||||||
} catch (WebSocketException e) {
|
} catch (WebSocketException e) {
|
||||||
System.out.println("WebSocketException. Msg: "+e.getMessage());
|
System.out.println("WebSocketException. Msg: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -447,14 +452,14 @@ public class Test {
|
||||||
mWebSocket.addListener(new GetRequiredFees(operations, asset, mListener));
|
mWebSocket.addListener(new GetRequiredFees(operations, asset, mListener));
|
||||||
mWebSocket.connect();
|
mWebSocket.connect();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("IOException. Msg: "+e.getMessage());
|
System.out.println("IOException. Msg: " + e.getMessage());
|
||||||
} catch (WebSocketException e) {
|
} catch (WebSocketException e) {
|
||||||
System.out.println("WebSocketException. Msg: "+e.getMessage());
|
System.out.println("WebSocketException. Msg: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRandomNumberGeneration(){
|
public void testRandomNumberGeneration() {
|
||||||
byte[] seed = new byte[] { new Long(System.nanoTime()).byteValue() };
|
byte[] seed = new byte[]{new Long(System.nanoTime()).byteValue()};
|
||||||
doCountTest(new SHA512Digest(), seed);
|
doCountTest(new SHA512Digest(), seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,11 +474,9 @@ public class Test {
|
||||||
|
|
||||||
generator.addSeedMaterial(seed);
|
generator.addSeedMaterial(seed);
|
||||||
|
|
||||||
for (int i = 0; i != 1000000; i++)
|
for (int i = 0; i != 1000000; i++) {
|
||||||
{
|
|
||||||
generator.nextBytes(output);
|
generator.nextBytes(output);
|
||||||
for (int j = 0; j != output.length; j++)
|
for (int j = 0; j != output.length; j++) {
|
||||||
{
|
|
||||||
averages[j] += output[j] & 0xff;
|
averages[j] += output[j] & 0xff;
|
||||||
ands[j] &= output[j];
|
ands[j] &= output[j];
|
||||||
xors[j] ^= output[j];
|
xors[j] ^= output[j];
|
||||||
|
@ -485,7 +488,7 @@ public class Test {
|
||||||
if ((averages[i] / 1000000) != 127) {
|
if ((averages[i] / 1000000) != 127) {
|
||||||
System.out.println("average test failed for " + digest.getAlgorithmName());
|
System.out.println("average test failed for " + digest.getAlgorithmName());
|
||||||
}
|
}
|
||||||
System.out.println("averages["+i+"] / 1000000: "+averages[i] / 1000000);
|
System.out.println("averages[" + i + "] / 1000000: " + averages[i] / 1000000);
|
||||||
if (ands[i] != 0) {
|
if (ands[i] != 0) {
|
||||||
System.out.println("and test failed for " + digest.getAlgorithmName());
|
System.out.println("and test failed for " + digest.getAlgorithmName());
|
||||||
}
|
}
|
||||||
|
@ -499,11 +502,15 @@ public class Test {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The final purpose of this test is to convert the plain brainkey at Main.BRAIN_KEY
|
* The final purpose of this test is to convert the plain brainkey at
|
||||||
* into the WIF at Main.WIF
|
* Main.BRAIN_KEY into the WIF at Main.WIF
|
||||||
*/
|
*/
|
||||||
public void testBrainKeyOperations(){
|
public void testBrainKeyOperations() {
|
||||||
BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0);
|
BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testBip39Opertion() {
|
||||||
|
BIP39 bip39 = new BIP39(Main.BIP39_KEY, "");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue