From e6c53b074e2050d3a0eec310ab405e2608c30c6f Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Thu, 8 Dec 2016 17:23:38 -0500 Subject: [PATCH] Adding support for the 'lookup_accounts' API call --- .../java/com/luminiasoft/bitshares/Main.java | 3 +- .../java/com/luminiasoft/bitshares/RPC.java | 1 + .../java/com/luminiasoft/bitshares/Test.java | 35 ++++++++ .../luminiasoft/bitshares/UserAccount.java | 45 +++++++++- .../bitshares/ws/LookupAccounts.java | 90 +++++++++++++++++++ 5 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/luminiasoft/bitshares/ws/LookupAccounts.java diff --git a/src/main/java/com/luminiasoft/bitshares/Main.java b/src/main/java/com/luminiasoft/bitshares/Main.java index dff87d5..0218171 100644 --- a/src/main/java/com/luminiasoft/bitshares/Main.java +++ b/src/main/java/com/luminiasoft/bitshares/Main.java @@ -57,7 +57,7 @@ public class Main { // test.testGetAccountByName(); // test.testGetRequiredFees(); // test.testRandomNumberGeneration(); - test.testBrainKeyOperations(false); +// test.testBrainKeyOperations(false); // test.testBip39Opertion(); // test.testAccountNamebyAddress(); // test.testAccountNameById(); @@ -68,5 +68,6 @@ public class Main { // test.testAccountUpdateOperationBroadcast(); // test.testCreateBinFile(); // test.testImportBinFile(); + test.testLookupAccounts(); } } diff --git a/src/main/java/com/luminiasoft/bitshares/RPC.java b/src/main/java/com/luminiasoft/bitshares/RPC.java index feb7d8e..1cfad0a 100644 --- a/src/main/java/com/luminiasoft/bitshares/RPC.java +++ b/src/main/java/com/luminiasoft/bitshares/RPC.java @@ -15,4 +15,5 @@ public class RPC { public static final String CALL_GET_ACCOUNTS = "get_accounts"; public static final String CALL_GET_KEY_REFERENCES = "get_key_references"; public static final String CALL_GET_RELATIVE_ACCOUNT_HISTORY = "get_relative_account_history"; + public static final String LOOKUP_ACCOUNTS = "lookup_accounts"; } diff --git a/src/main/java/com/luminiasoft/bitshares/Test.java b/src/main/java/com/luminiasoft/bitshares/Test.java index 193df2f..286d8fa 100644 --- a/src/main/java/com/luminiasoft/bitshares/Test.java +++ b/src/main/java/com/luminiasoft/bitshares/Test.java @@ -650,4 +650,39 @@ public class Test { System.out.println("WebSocketException. Msg: "+e.getMessage()); } } + + public void testLookupAccounts(){ + WitnessResponseListener listener = new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("onSuccess"); + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("onError"); + } + }; + + SSLContext context = null; + try { + 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 LookupAccounts("bilthon", listener)); + mWebSocket.connect(); + + } catch (NoSuchAlgorithmException e) { + System.out.println("NoSuchAlgorithmException. Msg: "+e.getMessage()); + } catch (WebSocketException e) { + System.out.println("WebSocketException. Msg: "+e.getMessage()); + } catch (IOException e) { + System.out.println("IOException. Msg: "+e.getMessage()); + } + } } diff --git a/src/main/java/com/luminiasoft/bitshares/UserAccount.java b/src/main/java/com/luminiasoft/bitshares/UserAccount.java index e25c261..8ed3743 100644 --- a/src/main/java/com/luminiasoft/bitshares/UserAccount.java +++ b/src/main/java/com/luminiasoft/bitshares/UserAccount.java @@ -1,6 +1,6 @@ package com.luminiasoft.bitshares; -import com.google.gson.JsonObject; +import com.google.gson.*; import com.luminiasoft.bitshares.interfaces.ByteSerializable; import com.luminiasoft.bitshares.interfaces.JsonSerializable; @@ -8,6 +8,7 @@ import java.io.ByteArrayOutputStream; import java.io.DataOutput; import java.io.DataOutputStream; import java.io.IOException; +import java.lang.reflect.Type; /** * Class tha represents a graphene user account. @@ -17,15 +18,35 @@ public class UserAccount extends GrapheneObject implements ByteSerializable, Jso public static final String PROXY_TO_SELF = "1.2.5"; + private String accountName; + /** * Constructor that expects a user account in the string representation. * That is in the 1.2.x format. - * @param id: The string representing the account apiId. + * @param id: The string representing the user account. */ public UserAccount(String id) { super(id); } + /** + * Constructor that expects a user account withe the proper graphene object id and an account name. + * @param id: The string representing the user account. + * @param name: The name of this user account. + */ + public UserAccount(String id, String name){ + super(id); + this.accountName = name; + } + + /** + * Getter for the account name field. + * @return: The name of this account. + */ + public String getAccountName() { + return accountName; + } + @Override public byte[] toBytes() { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); @@ -52,4 +73,24 @@ public class UserAccount extends GrapheneObject implements ByteSerializable, Jso public String toString() { return this.toJsonString(); } + + /** + * Custom deserializer used to deserialize user accounts provided as response from the 'lookup_accounts' api call. + * This response contains serialized user accounts in the form [[{id1},{name1}][{id1},{name1}]]. + * + * For instance: + * [["bilthon-1","1.2.139205"],["bilthon-2","1.2.139207"],["bilthon-2016","1.2.139262"]] + * + * So this class will pick up this data and turn it into a UserAccount object. + */ + public static class UserAccountDeserializer implements JsonDeserializer { + + @Override + public UserAccount deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + JsonArray array = json.getAsJsonArray(); + String name = array.get(0).getAsString(); + String id = array.get(1).getAsString(); + return new UserAccount(id, name); + } + } } diff --git a/src/main/java/com/luminiasoft/bitshares/ws/LookupAccounts.java b/src/main/java/com/luminiasoft/bitshares/ws/LookupAccounts.java new file mode 100644 index 0000000..106a78c --- /dev/null +++ b/src/main/java/com/luminiasoft/bitshares/ws/LookupAccounts.java @@ -0,0 +1,90 @@ +package com.luminiasoft.bitshares.ws; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.reflect.TypeToken; +import com.luminiasoft.bitshares.RPC; +import com.luminiasoft.bitshares.UserAccount; +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; + +/** + * Created by henry on 07/12/16. + */ +public class LookupAccounts extends WebSocketAdapter { + + public static final int DEFAULT_MAX = 1000; + private final String accountName; + private int maxAccounts = DEFAULT_MAX; + private final WitnessResponseListener mListener; + + public LookupAccounts(String accountName, WitnessResponseListener listener){ + this.accountName = accountName; + this.maxAccounts = DEFAULT_MAX; + this.mListener = listener; + } + + public LookupAccounts(String accountName, int maxAccounts, WitnessResponseListener listener){ + this.accountName = accountName; + this.maxAccounts = maxAccounts; + this.mListener = listener; + } + + @Override + public void onConnected(WebSocket websocket, Map> headers) throws Exception { + ArrayList accountParams = new ArrayList<>(); + accountParams.add(this.accountName); + accountParams.add(this.maxAccounts); + ApiCall getAccountByName = new ApiCall(0, RPC.LOOKUP_ACCOUNTS, accountParams, RPC.VERSION, 1); + websocket.sendText(getAccountByName.toJsonString()); + } + + @Override + public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + System.out.println("<<< "+frame.getPayloadText()); + String response = frame.getPayloadText(); + + Type LookupAccountsResponse = new TypeToken>>(){}.getType(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountDeserializer()); + WitnessResponse> witnessResponse = builder.create().fromJson(response, LookupAccountsResponse); + 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 { + 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(); + } +} \ No newline at end of file