From 040a53f408c1c6d05cccd3b25bb8a6cb5fee08d6 Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Mon, 25 Sep 2017 22:53:30 -0500 Subject: [PATCH] Adding memo deserialization --- .../api/GetRelativeAccountHistory.java | 2 + .../api/SubscriptionMessagesHub.java | 2 + .../graphenej/objects/Memo.java | 34 +++++++++ .../operations/TransferOperation.java | 7 ++ .../api/GetRelativeAccountHistoryTest.java | 72 +++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistoryTest.java diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java index 4228288..dd023a4 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistory.java @@ -20,6 +20,7 @@ import de.bitsharesmunich.graphenej.models.ApiCall; import de.bitsharesmunich.graphenej.models.BaseResponse; import de.bitsharesmunich.graphenej.models.HistoricalTransfer; import de.bitsharesmunich.graphenej.models.WitnessResponse; +import de.bitsharesmunich.graphenej.objects.Memo; import de.bitsharesmunich.graphenej.operations.TransferOperation; /** @@ -116,6 +117,7 @@ public class GetRelativeAccountHistory extends BaseGrapheneHandler { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(TransferOperation.class, new TransferOperation.TransferDeserializer()); gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); + gsonBuilder.registerTypeAdapter(Memo.class, new Memo.MemoDeserializer()); WitnessResponse> transfersResponse = gsonBuilder.create().fromJson(response, RelativeAccountHistoryResponse); mListener.onSuccess(transfersResponse); } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java index 605a649..95c39f4 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/api/SubscriptionMessagesHub.java @@ -25,6 +25,7 @@ import de.bitsharesmunich.graphenej.models.ApiCall; import de.bitsharesmunich.graphenej.models.DynamicGlobalProperties; import de.bitsharesmunich.graphenej.models.SubscriptionResponse; import de.bitsharesmunich.graphenej.models.WitnessResponse; +import de.bitsharesmunich.graphenej.objects.Memo; import de.bitsharesmunich.graphenej.operations.LimitOrderCreateOperation; import de.bitsharesmunich.graphenej.operations.TransferOperation; @@ -91,6 +92,7 @@ public class SubscriptionMessagesHub extends BaseGrapheneHandler implements Subs builder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); builder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountSimpleDeserializer()); builder.registerTypeAdapter(DynamicGlobalProperties.class, new DynamicGlobalProperties.DynamicGlobalPropertiesDeserializer()); + builder.registerTypeAdapter(Memo.class, new Memo.MemoDeserializer()); this.gson = builder.create(); } diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/objects/Memo.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/objects/Memo.java index e84cb51..a618ba2 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/objects/Memo.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/objects/Memo.java @@ -1,17 +1,25 @@ package de.bitsharesmunich.graphenej.objects; 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.JsonObject; +import com.google.gson.JsonParseException; + import de.bitsharesmunich.graphenej.Address; import de.bitsharesmunich.graphenej.PublicKey; import de.bitsharesmunich.graphenej.Util; import de.bitsharesmunich.graphenej.errors.ChecksumException; +import de.bitsharesmunich.graphenej.errors.MalformedAddressException; import de.bitsharesmunich.graphenej.interfaces.ByteSerializable; import de.bitsharesmunich.graphenej.interfaces.JsonSerializable; +import de.bitsharesmunich.graphenej.operations.TransferOperation; + import org.bitcoinj.core.ECKey; import org.spongycastle.math.ec.ECPoint; +import java.lang.reflect.Type; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; @@ -269,4 +277,30 @@ public class Memo implements ByteSerializable, JsonSerializable { } return memoObject; } + + /** + * Class used to deserialize a memo + */ + public static class MemoDeserializer implements JsonDeserializer { + + @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; + } + } } \ No newline at end of file diff --git a/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/TransferOperation.java b/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/TransferOperation.java index 85bf2d4..89d5b14 100644 --- a/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/TransferOperation.java +++ b/graphenej/src/main/java/de/bitsharesmunich/graphenej/operations/TransferOperation.java @@ -191,6 +191,13 @@ public class TransferOperation extends BaseOperation { UserAccount from = new UserAccount(jsonObject.get(KEY_FROM).getAsString()); UserAccount to = new UserAccount(jsonObject.get(KEY_TO).getAsString()); TransferOperation transfer = new TransferOperation(from, to, amount, fee); + + // If the transfer had a memo, deserialize it + if(jsonObject.has(KEY_MEMO)){ + Memo memo = context.deserialize(jsonObject.get(KEY_MEMO), Memo.class); + transfer.setMemo(memo); + } + return transfer; } } diff --git a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistoryTest.java b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistoryTest.java new file mode 100644 index 0000000..c3ab49e --- /dev/null +++ b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetRelativeAccountHistoryTest.java @@ -0,0 +1,72 @@ +package de.bitsharesmunich.graphenej.api; + +import com.neovisionaries.ws.client.WebSocketException; + +import org.junit.Test; + +import java.util.List; +import de.bitsharesmunich.graphenej.UserAccount; +import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; +import de.bitsharesmunich.graphenej.models.BaseResponse; +import de.bitsharesmunich.graphenej.models.HistoricalTransfer; +import de.bitsharesmunich.graphenej.models.WitnessResponse; +import de.bitsharesmunich.graphenej.operations.TransferOperation; + +/** + * Created by nelson on 9/25/17. + */ + +public class GetRelativeAccountHistoryTest extends BaseApiTest { + private final String TAG = this.getClass().getName(); + + private int HISTORICAL_TRANSFER_BATCH_SIZE = 10; + private final UserAccount bilthon_7 = new UserAccount("1.2.140994"); + private int historicalTransferCount; + + @Test + public void testRelativeAccountHistory(){ + int start = 0; + GetRelativeAccountHistory mGetRelativeAccountHistory = new GetRelativeAccountHistory(bilthon_7, 0, HISTORICAL_TRANSFER_BATCH_SIZE, start, mTransferHistoryListener); + mWebSocket.addListener(mGetRelativeAccountHistory); + + try{ + mWebSocket.connect(); + synchronized (this){ + wait(); + } + }catch (WebSocketException e) { + System.out.println("WebSocketException. Msg: " + e.getMessage()); + } catch (InterruptedException e) { + System.out.println("InterruptedException. Msg: "+e.getMessage()); + } + } + + /** + * Callback activated once we get a response back from the full node telling us about the + * transfer history of the current account. + */ + private WitnessResponseListener mTransferHistoryListener = new WitnessResponseListener() { + + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("mTransferHistoryListener.onSuccess"); + historicalTransferCount++; + WitnessResponse> resp = response; + for(HistoricalTransfer historicalTransfer : resp.result){ + if(historicalTransfer.getOperation() != null){ + System.out.println("Got transfer operation!"); + TransferOperation transferOperation = historicalTransfer.getOperation(); + System.out.println(String.format("%s - > %s, memo: %s", + transferOperation.getFrom().getObjectId(), + transferOperation.getTo().getObjectId(), + transferOperation.getMemo() == null ? "*" : transferOperation.getMemo().getStringMessage())); + } + } + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("onError. Msg: "+error.message); + } + }; +}