Improvements in the GetAccountsByAddress class and new UserAccount deserializer

This commit is contained in:
Nelson R. Perez 2016-12-14 16:52:08 -05:00
parent c20ae23921
commit 47fa879ab0
5 changed files with 40 additions and 27 deletions

View file

@ -5,6 +5,10 @@ public class Main {
// Brain key from Nelson's app referencing the bilthon-83 account
public static final String BILTHON_83_BRAIN_KEY = System.getenv("BILTHON_83_BRAIN_KEY");
public static final String BILTHON_83_ORIGINAL_BRAIN_KEY = System.getenv("BILTHON_83_ORIGINAL_BRAIN_KEY");
public static final String BILTHON_1_BRAIN_KEY = System.getenv("BILTHON_1_BRAIN_KEY");
public static final String BILTHON_5_BRAIN_KEY = System.getenv("BILTHON_5_BRAIN_KEY");
public static final String BILTHON_7_BRAIN_KEY = System.getenv("BILTHON_7_BRAIN_KEY");

View file

@ -514,9 +514,11 @@ public class Test {
}
};
BrainKey brainKey = new BrainKey(Main.BILTHON_83_BRAIN_KEY, 0);
BrainKey brainKey = new BrainKey(Main.BILTHON_83_ORIGINAL_BRAIN_KEY, 0);
Address address = new Address(ECKey.fromPublicOnly(brainKey.getPrivateKey().getPubKey()));
try {
// Address address = new Address("BTS5BgjNRDeawGSc1NPk91p2BYYEhJWKgsjLZGDmFgY6uwhAYLy9G");
// Create a custom SSL context.
SSLContext context = null;
context = NaiveSSLContext.getInstance("TLS");
@ -535,6 +537,9 @@ public class Test {
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage());
}
// catch (MalformedAddressException e) {
// System.out.println("MalformedAddressException. Msg: "+e.getMessage());
// }
}
public void testAccountNameById() {
@ -678,9 +683,9 @@ public class Test {
authMap.put(address.getPublicKey(), 1);
Authority authority = new Authority(1, authMap, null);
AccountOptions options = new AccountOptions(address.getPublicKey());
BrainKey brainKey = new BrainKey(Main.BILTHON_7_BRAIN_KEY, 0);
BrainKey brainKey = new BrainKey(Main.BILTHON_1_BRAIN_KEY, 0);
Transaction transaction = new AccountUpdateTransactionBuilder(brainKey.getPrivateKey())
.setAccont(new UserAccount("1.2.140994"))
.setAccont(new UserAccount("1.2.139205"))
.setOwner(authority)
.setActive(authority)
.setOptions(options)
@ -693,7 +698,7 @@ public class Test {
// Set the custom SSL context.
factory.setSSLContext(context);
WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL);
WebSocket mWebSocket = factory.createSocket(BLOCK_PAY_DE);
mWebSocket.addListener(new TransactionBroadcastSequence(transaction, new Asset("1.3.0"), listener));
mWebSocket.connect();

View file

@ -9,6 +9,7 @@ import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
/**
* Class tha represents a graphene user account.
@ -93,4 +94,18 @@ public class UserAccount extends GrapheneObject implements ByteSerializable, Jso
return new UserAccount(id, name);
}
}
/**
* Custom deserializer used to deserialize user accounts as provided by the response of the 'get_key_references' api call.
* This response contains serialized user accounts in the form [["id1","id2"]]
*/
public static class UserAccountSimpleDeserializer implements JsonDeserializer<UserAccount> {
@Override
public UserAccount deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String id = json.getAsString();
System.out.println("id: "+id);
return new UserAccount(id);
}
}
}

View file

@ -1,12 +1,10 @@
package de.bitsharesmunich.graphenej.api;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import de.bitsharesmunich.graphenej.Address;
import de.bitsharesmunich.graphenej.RPC;
import de.bitsharesmunich.graphenej.interfaces.JsonSerializable;
import de.bitsharesmunich.graphenej.UserAccount;
import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener;
import de.bitsharesmunich.graphenej.models.ApiCall;
import de.bitsharesmunich.graphenej.models.BaseResponse;
@ -37,21 +35,11 @@ public class GetAccountsByAddress extends WebSocketAdapter {
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<Serializable> accountParams = new ArrayList();
ArrayList<Serializable> paramAddress = new ArrayList();
paramAddress.add(new JsonSerializable() {
@Override
public String toJsonString() {
return address.toString();
}
@Override
public JsonElement toJsonObject() {
return new JsonParser().parse(address.toString());
}
});
accountParams.add(paramAddress);
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_KEY_REFERENCES, accountParams, RPC.VERSION, 1);
ArrayList<Serializable> params = new ArrayList();
ArrayList<Serializable> addresses = new ArrayList();
addresses.add(address.toString());
params.add(addresses);
ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_KEY_REFERENCES, params, RPC.VERSION, 1);
websocket.sendText(getAccountByAddress.toJsonString());
}
@ -59,10 +47,11 @@ public class GetAccountsByAddress extends WebSocketAdapter {
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
System.out.println("<<< "+frame.getPayloadText());
String response = frame.getPayloadText();
Gson gson = new Gson();
GsonBuilder builder = new GsonBuilder();
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<List<String>>>>(){}.getType();
WitnessResponse<List<List<String>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse);
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<List<UserAccount>>>>(){}.getType();
builder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountSimpleDeserializer());
WitnessResponse<List<List<UserAccount>>> witnessResponse = builder.create().fromJson(response, GetAccountByAddressResponse);
if (witnessResponse.error != null) {
this.mListener.onError(witnessResponse.error);
} else {

View file

@ -42,7 +42,7 @@ public class GetRequiredFees extends WebSocketAdapter {
ArrayList<Serializable> accountParams = new ArrayList<>();
accountParams.add((Serializable) this.operations);
accountParams.add(this.asset.getObjectId());
ApiCall getRequiredFees = new ApiCall(0, RPC.CALL_GET_REQUIRED_FEES, accountParams, "2.0", 1);
ApiCall getRequiredFees = new ApiCall(0, RPC.CALL_GET_REQUIRED_FEES, accountParams, RPC.VERSION, 1);
websocket.sendText(getRequiredFees.toJsonString());
}