diff --git a/src/main/java/com/luminiasoft/bitshares/Main.java b/src/main/java/com/luminiasoft/bitshares/Main.java index ead7ba6..01de501 100644 --- a/src/main/java/com/luminiasoft/bitshares/Main.java +++ b/src/main/java/com/luminiasoft/bitshares/Main.java @@ -68,8 +68,9 @@ public class Main { // test.testRandomNumberGeneration(); - test.testBrainKeyOperations(false); +// test.testBrainKeyOperations(false); // test.testBip39Opertion(); + test.testAccountNamebyAddress(); } } diff --git a/src/main/java/com/luminiasoft/bitshares/RPC.java b/src/main/java/com/luminiasoft/bitshares/RPC.java index 389d5a1..c146973 100644 --- a/src/main/java/com/luminiasoft/bitshares/RPC.java +++ b/src/main/java/com/luminiasoft/bitshares/RPC.java @@ -10,4 +10,6 @@ public class RPC { public static final String CALL_GET_DYNAMIC_GLOBAL_PROPERTIES = "get_dynamic_global_properties"; public static final String CALL_BROADCAST_TRANSACTION = "broadcast_transaction"; public static final String CALL_GET_REQUIRED_FEES = "get_required_fees"; + public static final String CALL_GET_ACCOUNTS = "get_accounts"; + public static final String CALL_GET_KEY_REFERENCES = "get_key_references"; } diff --git a/src/main/java/com/luminiasoft/bitshares/Test.java b/src/main/java/com/luminiasoft/bitshares/Test.java index 2fca809..7b9776f 100644 --- a/src/main/java/com/luminiasoft/bitshares/Test.java +++ b/src/main/java/com/luminiasoft/bitshares/Test.java @@ -8,6 +8,7 @@ import com.luminiasoft.bitshares.errors.MalformedTransactionException; import com.luminiasoft.bitshares.interfaces.WitnessResponseListener; import com.luminiasoft.bitshares.models.*; import com.luminiasoft.bitshares.ws.GetAccountByName; +import com.luminiasoft.bitshares.ws.GetAccountsByAddress; import com.luminiasoft.bitshares.ws.GetRequiredFees; import com.luminiasoft.bitshares.ws.TransactionBroadcastSequence; import com.neovisionaries.ws.client.*; @@ -544,9 +545,9 @@ public class Test { // Address generation test Address address = new Address(key); - System.out.println("Block explorer's address: "+address); + System.out.println("Block explorer's address: " + address); - System.out.println("Wif: : "+brainKey.getWalletImportFormat()); + System.out.println("Wif: : " + brainKey.getWalletImportFormat()); } catch (FileNotFoundException e) { System.out.println("FileNotFoundException. Msg: " + e.getMessage()); } catch (IOException e) { @@ -567,4 +568,20 @@ public class Test { public void testBip39Opertion() { BIP39 bip39 = new BIP39(Main.BIP39_KEY, ""); } + + public void testAccountNamebyAddress() { + BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0); + Address address = new Address(brainKey.getPrivateKey()); + try { + WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(5000); + WebSocket mWebSocket = factory.createSocket(WITNESS_URL); + byte[] key = brainKey.getPrivateKey().getPubKey(); + mWebSocket.addListener(new GetAccountsByAddress(key, mListener)); + mWebSocket.connect(); + } catch (IOException e) { + System.out.println("IOException. Msg: " + e.getMessage()); + } catch (WebSocketException e) { + System.out.println("WebSocketException. Msg: " + e.getMessage()); + } + } } diff --git a/src/main/java/com/luminiasoft/bitshares/ws/GetAccountsByAddress.java b/src/main/java/com/luminiasoft/bitshares/ws/GetAccountsByAddress.java new file mode 100644 index 0000000..b5f3daf --- /dev/null +++ b/src/main/java/com/luminiasoft/bitshares/ws/GetAccountsByAddress.java @@ -0,0 +1,72 @@ +package com.luminiasoft.bitshares.ws; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.luminiasoft.bitshares.RPC; +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; +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 nelson on 11/15/16. + */ +public class GetAccountsByAddress extends WebSocketAdapter { + + private byte[] publicKey; + private WitnessResponseListener mListener; + + public GetAccountsByAddress(byte[] publicKey, WitnessResponseListener listener) { + this.publicKey = publicKey; + this.mListener = listener; + } + + @Override + public void onConnected(WebSocket websocket, Map> headers) throws Exception { + ArrayList accountParams = new ArrayList(); + accountParams.add(this.publicKey); + ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_KEY_REFERENCES, accountParams, "2.1", 1); + websocket.sendText(getAccountByAddress.toJsonString()); + } + + @Override + public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { + String response = frame.getPayloadText(); + Gson gson = new Gson(); + + Type GetAccountByAddressResponse = new TypeToken>>>() { + }.getType(); + WitnessResponse>>> 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(); + } +}