Get account name WS

master
Henry Varona 2016-11-26 18:39:38 -04:00
parent 2b2195754a
commit 8a5c7e51a4
6 changed files with 298 additions and 23 deletions

View File

@ -1,33 +1,53 @@
package com.luminiasoft.bitshares;
import com.google.common.primitives.Bytes;
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;
import org.bitcoinj.core.Base58;
import org.bitcoinj.core.ECKey;
import org.spongycastle.crypto.digests.RIPEMD160Digest;
import java.util.Arrays;
import java.util.List;
import javafx.util.Pair;
import javax.net.ssl.SSLContext;
/**
* Class used to encapsulate address-related operations.
*/
public class Address {
public final static String DEFAULT_PREFIX = "BTS";
private ECKey key;
private String prefix;
private String accountName = null;
private String accountId = null;
public Address(ECKey key){
public Address(ECKey key) {
this.key = key;
this.prefix = DEFAULT_PREFIX;
}
public Address(ECKey key, String prefix){
public Address(ECKey key, String prefix) {
this.key = key;
this.prefix = prefix;
}
@Override
public String toString(){
public String toString() {
byte[] pubKey = key.getPubKey();
byte[] checksum = new byte[160 / 8];
RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
@ -36,4 +56,110 @@ 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);
}
};
}

View File

@ -6,10 +6,10 @@ 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 = "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 BRAIN_KEY = "SIVER TIKKER FOGO HOMINAL PRAYER LUTEIN SMALLY ACARID MEROPIA TRANCE BOGONG IDDAT HICKORY SOUTANE MOOD DOWSER";
//public static final String BRAIN_KEY = "SIVER TIKKER FOGO HOMINAL PRAYER LUTEIN SMALLY ACARID MEROPIA TRANCE BOGONG IDDAT HICKORY SOUTANE MOOD DOWSER";
public static final String BIP39_KEY = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
@ -72,8 +72,8 @@ public class Main {
// test.testBip39Opertion();
// test.testAccountNamebyAddress();
test.testAccountNamebyAddress();
test.testRelativeAccountHistory();
// test.testRelativeAccountHistory();
}
}

View File

@ -27,6 +27,8 @@ import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by nelson on 11/9/16.
@ -47,6 +49,7 @@ public class Test {
@Override
public void onSuccess(WitnessResponse response) {
if (response.result.getClass() == AccountProperties.class) {
AccountProperties accountProperties = (AccountProperties) response.result;
System.out.println("Got account properties");
@ -54,10 +57,37 @@ public class Test {
} 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);
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);
if (sl.size() > 0) {
String accountId = (String) sl.get(0);
System.out.println("account id : " + accountId);
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(accountId, null));
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) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} else {
System.out.println("Got empty list!");
@ -576,21 +606,33 @@ public class Test {
public void testAccountNamebyAddress() {
BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0);
Address address = new Address(brainKey.getPrivateKey());
try {
WebSocket mWebSocket = new WebSocketFactory().createSocket(WITNESS_URL);
byte[] key = brainKey.getPrivateKey().getPubKey();
mWebSocket.addListener(new GetAccountsByAddress(key, mListener));
Address address = new Address(brainKey.getPrivateKey());
address.getAccountName();
/*try {
BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0);
Address address = new Address(brainKey.getPrivateKey());
// 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 GetAccountsByAddress(address.toString(), mListener));
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 void testRelativeAccountHistory(){
public void testRelativeAccountHistory() {
GetRelativeAccountHistory relativeAccountHistory = new GetRelativeAccountHistory(new UserAccount("1.2.138632"), mListener);
try {
// Create a custom SSL context.
@ -605,11 +647,11 @@ public class Test {
mWebSocket.addListener(relativeAccountHistory);
mWebSocket.connect();
} catch (IOException e) {
System.out.println("IOException. Msg: "+e.getMessage());
System.out.println("IOException. Msg: " + e.getMessage());
} catch (WebSocketException e) {
System.out.println("WebSocketException. Msg: "+e.getMessage());
System.out.println("WebSocketException. Msg: " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException. Msg: "+e.getMessage());
System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage());
}
}
}

View File

@ -77,6 +77,7 @@ 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);
for(int l = 0; l < listArgument.size(); l++){
@ -89,6 +90,7 @@ public class ApiCall implements JsonSerializable {
paramsArray.add(methodParams);
obj.add(KEY_PARAMS, paramsArray);
obj.addProperty(KEY_JSON_RPC, this.jsonrpc);
System.out.println("JSON Object: " + obj.toString());
return obj;
}

View File

@ -0,0 +1,88 @@
package com.luminiasoft.bitshares.ws;
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.RPC;
import com.luminiasoft.bitshares.interfaces.JsonSerializable;
import com.luminiasoft.bitshares.interfaces.WitnessResponseListener;
import com.luminiasoft.bitshares.models.ApiCall;
import com.luminiasoft.bitshares.models.BaseResponse;
import com.luminiasoft.bitshares.models.WitnessResponse;
import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketAdapter;
import com.neovisionaries.ws.client.WebSocketException;
import com.neovisionaries.ws.client.WebSocketFrame;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
* @author henry
*/
public class GetAccountNameById extends WebSocketAdapter {
private String accountID;
private WitnessResponseListener mListener;
public GetAccountNameById(String accountID, WitnessResponseListener listener) {
this.accountID = accountID;
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 accountID;
}
@Override
public JsonElement toJsonObject() {
return new JsonParser().parse(accountID);
}
});
accountParams.add(paramAddress);
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_ACCOUNTS, accountParams, "2.0", 1);
websocket.sendText(getAccountByAddress.toJsonString());
}
@Override
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
System.out.println(frame.toString());
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);
if (witnessResponse.error != null) {
this.mListener.onError(witnessResponse.error);
} else {
this.mListener.onSuccess(witnessResponse);
}
websocket.disconnect();
}
@Override
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
mListener.onError(new BaseResponse.Error(cause.getMessage()));
websocket.disconnect();
}
@Override
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
mListener.onError(new BaseResponse.Error(cause.getMessage()));
websocket.disconnect();
}
}

View File

@ -1,8 +1,11 @@
package com.luminiasoft.bitshares.ws;
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.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;
@ -24,10 +27,10 @@ import java.util.Map;
*/
public class GetAccountsByAddress extends WebSocketAdapter {
private byte[] publicKey;
private String publicKey;
private WitnessResponseListener mListener;
public GetAccountsByAddress(byte[] publicKey, WitnessResponseListener listener) {
public GetAccountsByAddress(String publicKey, WitnessResponseListener listener) {
this.publicKey = publicKey;
this.mListener = listener;
}
@ -35,19 +38,33 @@ public class GetAccountsByAddress extends WebSocketAdapter {
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<Serializable> accountParams = new ArrayList();
accountParams.add(this.publicKey);
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_KEY_REFERENCES, accountParams, "2.1", 1);
ArrayList<JsonSerializable> paramAddress = new ArrayList();
paramAddress.add(new JsonSerializable() {
@Override
public String toJsonString() {
return publicKey;
}
@Override
public JsonElement toJsonObject() {
return new JsonParser().parse(publicKey);
}
});
accountParams.add(paramAddress);
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_KEY_REFERENCES, accountParams, "2.0", 1);
websocket.sendText(getAccountByAddress.toJsonString());
}
@Override
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
System.out.println(frame.toString());
String response = frame.getPayloadText();
Gson gson = new Gson();
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<List<Object>>>>() {
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<Object>>>() {
}.getType();
WitnessResponse<WitnessResponse<List<List<Object>>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse);
WitnessResponse<WitnessResponse<List<Object>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse);
if (witnessResponse.error != null) {
this.mListener.onError(witnessResponse.error);