Added some ws functions
This commit is contained in:
parent
4c105de826
commit
e9c71b5145
4 changed files with 95 additions and 3 deletions
|
@ -68,8 +68,9 @@ public class Main {
|
|||
|
||||
// test.testRandomNumberGeneration();
|
||||
|
||||
test.testBrainKeyOperations(false);
|
||||
// test.testBrainKeyOperations(false);
|
||||
|
||||
// test.testBip39Opertion();
|
||||
test.testAccountNamebyAddress();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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);
|
||||
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<WitnessResponse<List<List<Object>>>>() {
|
||||
}.getType();
|
||||
WitnessResponse<WitnessResponse<List<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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue