This commit is contained in:
Javier Varona 2017-10-01 15:58:52 -04:00
commit dff2dae254
5 changed files with 55 additions and 19 deletions

View file

@ -0,0 +1,13 @@
package cy.agorise.graphenej;
/**
* Enum-type used to specify the different roles of an authority.
*
* @see <a href="https://bitshares.org/doxygen/authority_8hpp_source.html">Authority</a>
*/
public enum AuthorityType {
OWNER,
ACTIVE,
MEMO
}

View file

@ -20,6 +20,7 @@ import cy.agorise.graphenej.models.ApiCall;
import cy.agorise.graphenej.models.BaseResponse; import cy.agorise.graphenej.models.BaseResponse;
import cy.agorise.graphenej.models.HistoricalTransfer; import cy.agorise.graphenej.models.HistoricalTransfer;
import cy.agorise.graphenej.models.WitnessResponse; import cy.agorise.graphenej.models.WitnessResponse;
import cy.agorise.graphenej.objects.Memo;
import cy.agorise.graphenej.operations.TransferOperation; import cy.agorise.graphenej.operations.TransferOperation;
/** /**
@ -161,6 +162,7 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler {
GsonBuilder gsonBuilder = new GsonBuilder(); GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(TransferOperation.class, new TransferOperation.TransferDeserializer()); gsonBuilder.registerTypeAdapter(TransferOperation.class, new TransferOperation.TransferDeserializer());
gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer());
gsonBuilder.registerTypeAdapter(Memo.class, new Memo.MemoDeserializer());
WitnessResponse<List<HistoricalTransfer>> transfersResponse = gsonBuilder.create().fromJson(response, RelativeAccountHistoryResponse); WitnessResponse<List<HistoricalTransfer>> transfersResponse = gsonBuilder.create().fromJson(response, RelativeAccountHistoryResponse);
mListener.onSuccess(transfersResponse); mListener.onSuccess(transfersResponse);
} }

View file

@ -26,6 +26,7 @@ import cy.agorise.graphenej.models.ApiCall;
import cy.agorise.graphenej.models.DynamicGlobalProperties; import cy.agorise.graphenej.models.DynamicGlobalProperties;
import cy.agorise.graphenej.models.SubscriptionResponse; import cy.agorise.graphenej.models.SubscriptionResponse;
import cy.agorise.graphenej.models.WitnessResponse; import cy.agorise.graphenej.models.WitnessResponse;
import cy.agorise.graphenej.objects.Memo;
import cy.agorise.graphenej.operations.LimitOrderCreateOperation; import cy.agorise.graphenej.operations.LimitOrderCreateOperation;
import cy.agorise.graphenej.operations.TransferOperation; import cy.agorise.graphenej.operations.TransferOperation;
@ -91,6 +92,7 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs
builder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); builder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer());
builder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountSimpleDeserializer()); builder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountSimpleDeserializer());
builder.registerTypeAdapter(DynamicGlobalProperties.class, new DynamicGlobalProperties.DynamicGlobalPropertiesDeserializer()); builder.registerTypeAdapter(DynamicGlobalProperties.class, new DynamicGlobalProperties.DynamicGlobalPropertiesDeserializer());
builder.registerTypeAdapter(Memo.class, new Memo.MemoDeserializer());
this.gson = builder.create(); this.gson = builder.create();
} }

View file

@ -1,17 +1,25 @@
package cy.agorise.graphenej.objects; package cy.agorise.graphenej.objects;
import com.google.common.primitives.Bytes; import com.google.common.primitives.Bytes;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import cy.agorise.graphenej.Address; import cy.agorise.graphenej.Address;
import cy.agorise.graphenej.PublicKey; import cy.agorise.graphenej.PublicKey;
import cy.agorise.graphenej.Util; import cy.agorise.graphenej.Util;
import cy.agorise.graphenej.errors.ChecksumException; import cy.agorise.graphenej.errors.ChecksumException;
import cy.agorise.graphenej.errors.MalformedAddressException;
import cy.agorise.graphenej.interfaces.ByteSerializable; import cy.agorise.graphenej.interfaces.ByteSerializable;
import cy.agorise.graphenej.interfaces.JsonSerializable; import cy.agorise.graphenej.interfaces.JsonSerializable;
import cy.agorise.graphenej.operations.TransferOperation;
import org.bitcoinj.core.ECKey; import org.bitcoinj.core.ECKey;
import org.spongycastle.math.ec.ECPoint; import org.spongycastle.math.ec.ECPoint;
import java.lang.reflect.Type;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
@ -269,4 +277,30 @@ public class Memo implements ByteSerializable, JsonSerializable {
} }
return memoObject; return memoObject;
} }
/**
* Class used to deserialize a memo
*/
public static class MemoDeserializer implements JsonDeserializer<Memo> {
@Override
public Memo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String fromAddress = jsonObject.get(KEY_FROM).getAsString();
String toAddress = jsonObject.get(KEY_TO).getAsString();
long nonce = jsonObject.get(KEY_NONCE).getAsLong();
String msg = jsonObject.get(KEY_MESSAGE).getAsString();
Memo memo = null;
try{
Address from = new Address(fromAddress);
Address to = new Address(toAddress);
byte[] message = Util.hexToBytes(msg);
memo = new Memo(from, to, nonce, message);
}catch(MalformedAddressException e){
System.out.println("MalformedAddressException. Msg: "+e.getMessage());
}
return memo;
}
}
} }

View file

@ -1,7 +1,6 @@
package cy.agorise.graphenej.operations; package cy.agorise.graphenej.operations;
import com.google.common.primitives.Bytes; import com.google.common.primitives.Bytes;
import com.google.common.primitives.UnsignedLong;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializationContext;
@ -14,13 +13,10 @@ import com.google.gson.JsonSerializer;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import cy.agorise.graphenej.Address;
import cy.agorise.graphenej.AssetAmount; import cy.agorise.graphenej.AssetAmount;
import cy.agorise.graphenej.BaseOperation; import cy.agorise.graphenej.BaseOperation;
import cy.agorise.graphenej.OperationType; import cy.agorise.graphenej.OperationType;
import cy.agorise.graphenej.UserAccount; import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.Util;
import cy.agorise.graphenej.errors.MalformedAddressException;
import cy.agorise.graphenej.objects.Memo; import cy.agorise.graphenej.objects.Memo;
/** /**
@ -171,7 +167,6 @@ public class TransferOperation extends BaseOperation {
@Override @Override
public TransferOperation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public TransferOperation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
System.out.println("Deserialized bitch start. Msg: "+ json.getAsString());
if(json.isJsonArray()){ if(json.isJsonArray()){
// This block is used just to check if we are in the first step of the deserialization // This block is used just to check if we are in the first step of the deserialization
// when we are dealing with an array. // when we are dealing with an array.
@ -197,20 +192,10 @@ public class TransferOperation extends BaseOperation {
UserAccount to = new UserAccount(jsonObject.get(KEY_TO).getAsString()); UserAccount to = new UserAccount(jsonObject.get(KEY_TO).getAsString());
TransferOperation transfer = new TransferOperation(from, to, amount, fee); TransferOperation transfer = new TransferOperation(from, to, amount, fee);
// Deserializing Memo if it exists // If the transfer had a memo, deserialize it
System.out.println("Deserialized bitch. Msg: "+ jsonObject.getAsString()); if(jsonObject.has(KEY_MEMO)){
if(jsonObject.get(KEY_MEMO) != null){ Memo memo = context.deserialize(jsonObject.get(KEY_MEMO), Memo.class);
JsonObject memoObj = jsonObject.get(KEY_MEMO).getAsJsonObject();
try{
Address memoFrom = new Address(memoObj.get(Memo.KEY_FROM).getAsString());
Address memoTo = new Address(memoObj.get(KEY_TO).getAsString());
long nonce = UnsignedLong.valueOf(memoObj.get(Memo.KEY_NONCE).getAsString()).longValue();
byte[] message = Util.hexToBytes(memoObj.get(Memo.KEY_MESSAGE).getAsString());
Memo memo = new Memo(memoFrom, memoTo, nonce, message);
transfer.setMemo(memo); transfer.setMemo(memo);
}catch(MalformedAddressException e){
System.out.println("MalformedAddressException. Msg: "+e.getMessage());
}
} }
return transfer; return transfer;