diff --git a/src/main/java/de/bitsharesmunich/graphenej/AccountOptions.java b/src/main/java/de/bitsharesmunich/graphenej/AccountOptions.java index 9311c30..8206591 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/AccountOptions.java +++ b/src/main/java/de/bitsharesmunich/graphenej/AccountOptions.java @@ -1,11 +1,11 @@ package de.bitsharesmunich.graphenej; import com.google.common.primitives.Bytes; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import com.google.gson.*; +import de.bitsharesmunich.graphenej.errors.MalformedAddressException; import de.bitsharesmunich.graphenej.interfaces.GrapheneSerializable; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; @@ -38,6 +38,8 @@ public class AccountOptions implements GrapheneSerializable { this.memo_key = memoKey; } + //TODO: Implement constructor that takes a Vote array. + public PublicKey getMemoKey() { return memo_key; } @@ -131,4 +133,25 @@ public class AccountOptions implements GrapheneSerializable { options.add(KEY_EXTENSIONS, extensions.toJsonObject()); return options; } + + /** + * Custom deserializer used while parsing the 'get_account_by_name' API call response. + * TODO: Implement all other details besides the key + */ + public static class AccountOptionsDeserializer implements JsonDeserializer { + + @Override + public AccountOptions deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + JsonObject baseObject = json.getAsJsonObject(); + AccountOptions options; + try { + Address address = new Address(baseObject.get(KEY_MEMO_KEY).getAsString()); + options = new AccountOptions(address.getPublicKey()); + } catch (MalformedAddressException e) { + System.out.println("MalformedAddressException. Msg: "+e.getMessage()); + options = new AccountOptions(); + } + return options; + } + } } diff --git a/src/main/java/de/bitsharesmunich/graphenej/Authority.java b/src/main/java/de/bitsharesmunich/graphenej/Authority.java index 8c70e7b..da01e83 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/Authority.java +++ b/src/main/java/de/bitsharesmunich/graphenej/Authority.java @@ -1,12 +1,11 @@ package de.bitsharesmunich.graphenej; import com.google.common.primitives.Bytes; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import com.google.gson.*; import de.bitsharesmunich.graphenej.errors.MalformedAddressException; import de.bitsharesmunich.graphenej.interfaces.GrapheneSerializable; +import java.lang.reflect.Type; import java.util.*; /** @@ -42,8 +41,12 @@ public class Authority implements GrapheneSerializable { this.weight_threshold = weight_threshold; if(keyAuths != null) this.key_auths = keyAuths; + else + this.key_auths = new HashMap<>(); if(accountAuths != null) this.account_auths = accountAuths; + else + this.account_auths = new HashMap<>(); } public void setKeyAuthorities(HashMap keyAuths){ @@ -58,6 +61,14 @@ public class Authority implements GrapheneSerializable { this.account_auths = accountAuthorities; } + public List getKeyAuths(){ + ArrayList keys = new ArrayList<>(); + for(PublicKey pk : key_auths.keySet()){ + keys.add(pk); + } + return keys; + } + @Override public String toJsonString() { return null; @@ -126,4 +137,43 @@ public class Authority implements GrapheneSerializable { } return Bytes.toArray(byteArray); } + + /** + * Custom deserializer used while parsing the 'get_account_by_name' API call response. + * + * This will deserialize an account authority in the form: + * + * { + * "weight_threshold": 1, + * "account_auths": [], + * "key_auths": [["BTS6yoiaoC4p23n31AV4GnMy5QDh5yUQEUmU4PmNxRQPGg7jjPkBq",1]], + * "address_auths": [] + * } + */ + public static class AuthorityDeserializer implements JsonDeserializer { + + @Override + public Authority deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + JsonObject baseObject = json.getAsJsonObject(); + long weightThreshold = baseObject.get(KEY_WEIGHT_THRESHOLD).getAsLong(); + JsonArray keyAuthArray = baseObject.getAsJsonArray(KEY_KEY_AUTHS); + JsonArray accountAuthArray = baseObject.getAsJsonArray(KEY_ACCOUNT_AUTHS); + HashMap keyAuthMap = new HashMap<>(); + HashMap accountAuthMap = new HashMap<>(); + for(int i = 0; i < keyAuthArray.size(); i++){ + JsonArray subArray = keyAuthArray.get(i).getAsJsonArray(); + String addr = subArray.get(0).getAsString(); + int weight = subArray.get(1).getAsInt(); + try { + keyAuthMap.put(new Address(addr).getPublicKey(), weight); + } catch (MalformedAddressException e) { + System.out.println("MalformedAddressException. Msg: "+e.getMessage()); + } + } + for(int i = 0; i < accountAuthArray.size(); i++){ + //TODO: Implement this + } + return new Authority(weightThreshold, keyAuthMap, accountAuthMap); + } + } } \ No newline at end of file diff --git a/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java b/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java index 3dc1780..8a804de 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java +++ b/src/main/java/de/bitsharesmunich/graphenej/api/GetAccountByName.java @@ -1,7 +1,10 @@ package de.bitsharesmunich.graphenej.api; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; +import de.bitsharesmunich.graphenej.AccountOptions; +import de.bitsharesmunich.graphenej.Authority; import de.bitsharesmunich.graphenej.RPC; import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; import de.bitsharesmunich.graphenej.models.AccountProperties; @@ -43,10 +46,12 @@ public class GetAccountByName extends WebSocketAdapter { @Override public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { String response = frame.getPayloadText(); - Gson gson = new Gson(); + GsonBuilder builder = new GsonBuilder(); Type GetAccountByNameResponse = new TypeToken>(){}.getType(); - WitnessResponse> witnessResponse = gson.fromJson(response, GetAccountByNameResponse); + builder.registerTypeAdapter(Authority.class, new Authority.AuthorityDeserializer()); + builder.registerTypeAdapter(AccountOptions.class, new AccountOptions.AccountOptionsDeserializer()); + WitnessResponse witnessResponse = builder.create().fromJson(response, GetAccountByNameResponse); if(witnessResponse.error != null){ this.mListener.onError(witnessResponse.error); diff --git a/src/main/java/de/bitsharesmunich/graphenej/models/AccountProperties.java b/src/main/java/de/bitsharesmunich/graphenej/models/AccountProperties.java index f2fd595..44946f0 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/models/AccountProperties.java +++ b/src/main/java/de/bitsharesmunich/graphenej/models/AccountProperties.java @@ -1,5 +1,8 @@ package de.bitsharesmunich.graphenej.models; +import de.bitsharesmunich.graphenej.AccountOptions; +import de.bitsharesmunich.graphenej.Authority; + /** * Created by nelson on 11/15/16. */ @@ -13,9 +16,9 @@ public class AccountProperties { public long lifetime_referrer_fee_percentage; public long referrer_rewards_percentage; public String name; - public User owner; - public User active; - public Options options; + public Authority owner; + public Authority active; + public AccountOptions options; public String statistics; public String[] whitelisting_accounts; public String[] blacklisting_accounts; @@ -24,20 +27,4 @@ public class AccountProperties { public Object[] owner_special_authority; public Object[] active_special_authority; public long top_n_control_flags; - - class User { - public long weight_threshold; - public String[] account_auths; //TODO: Check this type - public String[][] key_auths; //TODO: Check how to deserialize this - public String[] address_auths; - } - - class Options { - public String memo_key; - public String voting_account; - public long num_witness; - public long num_committee; - public String[] votes; //TODO: Check this type - public String[] extensions; //TODO: Check this type - } }