graphenej/src/main/java/com/luminiasoft/bitshares/Address.java

166 lines
6.4 KiB
Java
Raw Normal View History

2016-11-23 19:45:35 +00:00
package com.luminiasoft.bitshares;
import com.google.common.primitives.Bytes;
2016-11-26 22:39:38 +00:00
import com.google.gson.internal.LinkedTreeMap;
import static com.luminiasoft.bitshares.Test.OPENLEDGER_WITNESS_URL;
import com.luminiasoft.bitshares.interfaces.WitnessResponseListener;
import com.luminiasoft.bitshares.models.BaseResponse;
import com.luminiasoft.bitshares.models.WitnessResponse;
import com.luminiasoft.bitshares.test.NaiveSSLContext;
import com.luminiasoft.bitshares.ws.GetAccountNameById;
import com.luminiasoft.bitshares.ws.GetAccountsByAddress;
import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketException;
import com.neovisionaries.ws.client.WebSocketFactory;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
2016-11-23 19:45:35 +00:00
import org.bitcoinj.core.Base58;
import org.bitcoinj.core.ECKey;
import org.spongycastle.crypto.digests.RIPEMD160Digest;
import java.util.Arrays;
2016-11-26 22:39:38 +00:00
import java.util.List;
import javafx.util.Pair;
import javax.net.ssl.SSLContext;
2016-11-23 19:45:35 +00:00
/**
* Class used to encapsulate address-related operations.
*/
public class Address {
2016-11-26 22:39:38 +00:00
2016-11-23 19:45:35 +00:00
public final static String DEFAULT_PREFIX = "BTS";
private ECKey key;
private String prefix;
2016-11-26 22:39:38 +00:00
private String accountName = null;
private String accountId = null;
2016-11-23 19:45:35 +00:00
2016-11-26 22:39:38 +00:00
public Address(ECKey key) {
2016-11-23 19:45:35 +00:00
this.key = key;
this.prefix = DEFAULT_PREFIX;
}
2016-11-26 22:39:38 +00:00
public Address(ECKey key, String prefix) {
2016-11-23 19:45:35 +00:00
this.key = key;
this.prefix = prefix;
}
@Override
2016-11-26 22:39:38 +00:00
public String toString() {
2016-11-23 19:45:35 +00:00
byte[] pubKey = key.getPubKey();
byte[] checksum = new byte[160 / 8];
RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
ripemd160Digest.update(pubKey, 0, pubKey.length);
ripemd160Digest.doFinal(checksum, 0);
byte[] pubKeyChecksummed = Bytes.concat(pubKey, Arrays.copyOfRange(checksum, 0, 4));
return this.prefix + Base58.encode(pubKeyChecksummed);
}
2016-11-26 22:39:38 +00:00
public void getAccountDetail() {
try {
SSLContext context = NaiveSSLContext.getInstance("TLS");
WebSocketFactory factory = new WebSocketFactory();
factory.setSSLContext(context);
WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL);
mWebSocket.addListener(new GetAccountsByAddress(this.toString(), accountIdListener));
System.out.println("Before connecting");
mWebSocket.connect();
} catch (IOException e) {
System.out.println("IOException. Msg: " + e.getMessage());
} catch (WebSocketException e) {
System.out.println("WebSocketException. Msg: " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage());
}
}
public String getAccountName() {
return accountName;
}
public String getAccountId() {
return accountId;
}
WitnessResponseListener accountIdListener = new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
if (response.result.getClass() == ArrayList.class) {
List l = (List) response.result;
if (l.size() > 0) {
if (l.get(0).getClass() == ArrayList.class) {
List sl = (List) l.get(0);
if (sl.size() > 0) {
accountId = (String) sl.get(0);
try {
// Create a custom SSL context.
SSLContext context = NaiveSSLContext.getInstance("TLS");
WebSocketFactory factory = new WebSocketFactory();
factory.setSSLContext(context);
WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL);
mWebSocket.addListener(new GetAccountNameById(accountId, accountListener));
mWebSocket.connect();
} catch (IOException e) {
System.out.println("IOException. Msg: " + e.getMessage());
} catch (WebSocketException e) {
System.out.println("WebSocketException. Msg: " + e.getMessage());
} catch (NoSuchAlgorithmException ex) {
}
} else {
//TODO Error empty answer
}
} else {
//TODO Error bad type of answer
System.out.println("Got empty list!");
}
} else {
//TODO Error bad type of answer
System.out.println("Got empty list!");
}
} else {
//TODO Error in response
System.out.println("accountIdListener Got other: " + response.result.getClass());
}
}
@Override
public void onError(BaseResponse.Error error) {
System.out.println("onError. message: " + error.message);
}
};
WitnessResponseListener accountListener = new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
if (response.result.getClass() == ArrayList.class) {
List l = (List) response.result;
if (l.size() > 0) {
System.out.println("list class " + l.get(0).getClass());
if (l.get(0).getClass() == LinkedTreeMap.class) {
LinkedTreeMap ltm = (LinkedTreeMap) l.get(0);
accountName = (String) ltm.get("name");
} else {
//TODO Error bad type of answer
System.out.println("Got bad type!");
}
} else {
//TODO Error bad type of answer
System.out.println("Got empty list!");
}
} else {
//TODO Error in response
System.out.println("accountIdListener Got other: " + response.result.getClass());
}
}
@Override
public void onError(BaseResponse.Error error) {
System.out.println("onError. message: " + error.message);
}
};
2016-11-23 19:45:35 +00:00
}