Reorganizing the code a bit
This commit is contained in:
parent
8a5c7e51a4
commit
8c929e2dc0
6 changed files with 145 additions and 238 deletions
|
@ -33,8 +33,6 @@ public class Address {
|
|||
|
||||
private ECKey key;
|
||||
private String prefix;
|
||||
private String accountName = null;
|
||||
private String accountId = null;
|
||||
|
||||
public Address(ECKey key) {
|
||||
this.key = key;
|
||||
|
@ -56,110 +54,4 @@ public class Address {
|
|||
byte[] pubKeyChecksummed = Bytes.concat(pubKey, Arrays.copyOfRange(checksum, 0, 4));
|
||||
return this.prefix + Base58.encode(pubKeyChecksummed);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -72,7 +72,9 @@ public class Main {
|
|||
|
||||
// test.testBip39Opertion();
|
||||
|
||||
test.testAccountNamebyAddress();
|
||||
// test.testAccountNamebyAddress();
|
||||
|
||||
test.testAccountNameById();
|
||||
|
||||
// test.testRelativeAccountHistory();
|
||||
}
|
||||
|
|
|
@ -55,15 +55,19 @@ public class Test {
|
|||
System.out.println("Got account properties");
|
||||
System.out.println("id: " + accountProperties.id);
|
||||
} else if (response.result.getClass() == ArrayList.class) {
|
||||
List l = (List) response.result;
|
||||
if (l.size() > 0) {
|
||||
|
||||
if (l.get(0).getClass() == AssetAmount.class) {
|
||||
AssetAmount assetAmount = (AssetAmount) l.get(0);
|
||||
List list = (List) response.result;
|
||||
if (list.size() > 0) {
|
||||
if(list.get(0) instanceof AccountProperties){
|
||||
List<AccountProperties> accountPropertiesList = list;
|
||||
for(AccountProperties accountProperties : accountPropertiesList){
|
||||
System.out.println("Account id: "+accountProperties.id);
|
||||
}
|
||||
}else if (list.get(0) instanceof AssetAmount) {
|
||||
AssetAmount assetAmount = (AssetAmount) list.get(0);
|
||||
System.out.println("Got fee");
|
||||
System.out.println("amount: " + assetAmount.getAmount() + ", asset id: " + assetAmount.getAsset().getObjectId());
|
||||
} else if (l.get(0).getClass() == ArrayList.class) {
|
||||
List sl = (List) l.get(0);
|
||||
} else if (list.get(0).getClass() == ArrayList.class) {
|
||||
List sl = (List) list.get(0);
|
||||
if (sl.size() > 0) {
|
||||
String accountId = (String) sl.get(0);
|
||||
System.out.println("account id : " + accountId);
|
||||
|
@ -606,11 +610,8 @@ public class Test {
|
|||
|
||||
public void testAccountNamebyAddress() {
|
||||
BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0);
|
||||
Address address = new Address(brainKey.getPrivateKey());
|
||||
address.getAccountName();
|
||||
/*try {
|
||||
BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0);
|
||||
Address address = new Address(brainKey.getPrivateKey());
|
||||
Address address = new Address(brainKey.getPrivateKey());
|
||||
try {
|
||||
// Create a custom SSL context.
|
||||
SSLContext context = null;
|
||||
context = NaiveSSLContext.getInstance("TLS");
|
||||
|
@ -620,8 +621,7 @@ public class Test {
|
|||
factory.setSSLContext(context);
|
||||
|
||||
WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL);
|
||||
mWebSocket.addListener(new GetAccountsByAddress(address.toString(), mListener));
|
||||
System.out.println("Before connecting");
|
||||
mWebSocket.addListener(new GetAccountsByAddress(address, mListener));
|
||||
mWebSocket.connect();
|
||||
} catch (IOException e) {
|
||||
System.out.println("IOException. Msg: " + e.getMessage());
|
||||
|
@ -629,7 +629,29 @@ public class Test {
|
|||
System.out.println("WebSocketException. Msg: " + e.getMessage());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage());
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
public void testAccountNameById(){
|
||||
try {
|
||||
// Create a custom SSL context.
|
||||
SSLContext context = null;
|
||||
context = NaiveSSLContext.getInstance("TLS");
|
||||
WebSocketFactory factory = new WebSocketFactory();
|
||||
|
||||
// Set the custom SSL context.
|
||||
factory.setSSLContext(context);
|
||||
|
||||
WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL);
|
||||
mWebSocket.addListener(new GetAccountNameById("1.2.138632", mListener));
|
||||
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 void testRelativeAccountHistory() {
|
||||
|
|
|
@ -77,12 +77,15 @@ public class ApiCall implements JsonSerializable {
|
|||
methodParams.add((String) this.params.get(i));
|
||||
}else if(this.params.get(i) instanceof ArrayList){
|
||||
// Other times it might be an array
|
||||
System.out.println("es un arreglo");
|
||||
JsonArray array = new JsonArray();
|
||||
ArrayList<JsonSerializable> listArgument = (ArrayList<JsonSerializable>) this.params.get(i);
|
||||
ArrayList<Serializable> listArgument = (ArrayList<Serializable>) this.params.get(i);
|
||||
for(int l = 0; l < listArgument.size(); l++){
|
||||
JsonSerializable s = listArgument.get(l);
|
||||
array.add(s.toJsonObject());
|
||||
Serializable element = listArgument.get(l);
|
||||
if(element instanceof JsonSerializable)
|
||||
array.add(((JsonSerializable) element).toJsonObject());
|
||||
else if(element instanceof String){
|
||||
array.add((String) element);
|
||||
}
|
||||
}
|
||||
methodParams.add(array);
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import com.google.gson.reflect.TypeToken;
|
|||
import com.luminiasoft.bitshares.RPC;
|
||||
import com.luminiasoft.bitshares.interfaces.JsonSerializable;
|
||||
import com.luminiasoft.bitshares.interfaces.WitnessResponseListener;
|
||||
import com.luminiasoft.bitshares.models.AccountProperties;
|
||||
import com.luminiasoft.bitshares.models.ApiCall;
|
||||
import com.luminiasoft.bitshares.models.BaseResponse;
|
||||
import com.luminiasoft.bitshares.models.WitnessResponse;
|
||||
|
@ -37,51 +38,50 @@ public class GetAccountNameById extends WebSocketAdapter {
|
|||
@Override
|
||||
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
|
||||
ArrayList<Serializable> accountParams = new ArrayList();
|
||||
ArrayList<JsonSerializable> paramAddress = new ArrayList();
|
||||
paramAddress.add(new JsonSerializable() {
|
||||
@Override
|
||||
public String toJsonString() {
|
||||
return accountID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement toJsonObject() {
|
||||
return new JsonParser().parse(accountID);
|
||||
}
|
||||
});
|
||||
ArrayList<Serializable> paramAddress = new ArrayList();
|
||||
paramAddress.add(accountID);
|
||||
accountParams.add(paramAddress);
|
||||
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_ACCOUNTS, accountParams, "2.0", 1);
|
||||
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_ACCOUNTS, accountParams, RPC.VERSION, 1);
|
||||
websocket.sendText(getAccountByAddress.toJsonString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
|
||||
System.out.println(frame.toString());
|
||||
System.out.println("<<< "+frame.getPayloadText());
|
||||
String response = frame.getPayloadText();
|
||||
Gson gson = new Gson();
|
||||
|
||||
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<Object>>>() {
|
||||
}.getType();
|
||||
|
||||
WitnessResponse<WitnessResponse<List<Object>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse);
|
||||
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<AccountProperties>>>() {}.getType();
|
||||
WitnessResponse<WitnessResponse<List<AccountProperties>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse);
|
||||
|
||||
if (witnessResponse.error != null) {
|
||||
this.mListener.onError(witnessResponse.error);
|
||||
} else {
|
||||
this.mListener.onSuccess(witnessResponse);
|
||||
}
|
||||
|
||||
websocket.disconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {
|
||||
if(frame.isTextFrame())
|
||||
System.out.println(">>> "+frame.getPayloadText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
|
||||
System.out.println("onError");
|
||||
mListener.onError(new BaseResponse.Error(cause.getMessage()));
|
||||
websocket.disconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
|
||||
System.out.println("handleCallbackError");
|
||||
StackTraceElement[] stack = cause.getStackTrace();
|
||||
for(StackTraceElement element : stack) {
|
||||
System.out.println("> "+element.getClassName()+"."+element.getMethodName()+" : "+element.getLineNumber());
|
||||
}
|
||||
mListener.onError(new BaseResponse.Error(cause.getMessage()));
|
||||
websocket.disconnect();
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.google.gson.Gson;
|
|||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.luminiasoft.bitshares.Address;
|
||||
import com.luminiasoft.bitshares.RPC;
|
||||
import com.luminiasoft.bitshares.interfaces.JsonSerializable;
|
||||
import com.luminiasoft.bitshares.interfaces.WitnessResponseListener;
|
||||
|
@ -27,51 +28,38 @@ import java.util.Map;
|
|||
*/
|
||||
public class GetAccountsByAddress extends WebSocketAdapter {
|
||||
|
||||
private String publicKey;
|
||||
private Address address;
|
||||
private WitnessResponseListener mListener;
|
||||
|
||||
public GetAccountsByAddress(String publicKey, WitnessResponseListener listener) {
|
||||
this.publicKey = publicKey;
|
||||
public GetAccountsByAddress(Address address, WitnessResponseListener listener) {
|
||||
this.address = address;
|
||||
this.mListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
|
||||
ArrayList<Serializable> accountParams = new ArrayList();
|
||||
ArrayList<JsonSerializable> paramAddress = new ArrayList();
|
||||
paramAddress.add(new JsonSerializable() {
|
||||
@Override
|
||||
public String toJsonString() {
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement toJsonObject() {
|
||||
return new JsonParser().parse(publicKey);
|
||||
}
|
||||
});
|
||||
ArrayList<Serializable> paramAddress = new ArrayList();
|
||||
paramAddress.add(address.toString());
|
||||
accountParams.add(paramAddress);
|
||||
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_KEY_REFERENCES, accountParams, "2.0", 1);
|
||||
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_KEY_REFERENCES, accountParams, RPC.VERSION, 1);
|
||||
websocket.sendText(getAccountByAddress.toJsonString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
|
||||
System.out.println(frame.toString());
|
||||
System.out.println("<<< "+frame.getPayloadText());
|
||||
String response = frame.getPayloadText();
|
||||
Gson gson = new Gson();
|
||||
|
||||
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<Object>>>() {
|
||||
}.getType();
|
||||
|
||||
WitnessResponse<WitnessResponse<List<Object>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse);
|
||||
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<String>>>(){}.getType();
|
||||
WitnessResponse<WitnessResponse<List<String>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse);
|
||||
|
||||
if (witnessResponse.error != null) {
|
||||
this.mListener.onError(witnessResponse.error);
|
||||
} else {
|
||||
this.mListener.onSuccess(witnessResponse);
|
||||
}
|
||||
|
||||
websocket.disconnect();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue