Adding memo deserialization
This commit is contained in:
parent
302194539e
commit
040a53f408
5 changed files with 117 additions and 0 deletions
|
@ -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<List<HistoricalTransfer>> transfersResponse = gsonBuilder.create().fromJson(response, RelativeAccountHistoryResponse);
|
||||
mListener.onSuccess(transfersResponse);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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<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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<List<HistoricalTransfer>> 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);
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue